package ch.deletescape.jterm.commandcontexts;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class DirectoriesTest {
  @Rule
  public TemporaryFolder tempFolder = new TemporaryFolder();

  @Test
  public void cdTest() throws IOException {
    Directories directories = new Directories();
    String path = tempFolder.newFolder().getAbsolutePath();

    assertThat(directories.cd(path), is(path));
  }

  @Test
  public void lsTest() throws Exception {
    Directories directories = new Directories();
    File folder = tempFolder.newFolder();
    new File(folder, "test").createNewFile();
    new File(folder, "test2").createNewFile();
    String path = folder.getAbsolutePath();

    assertThat(directories.ls(path), is("test\ntest2\n"));
  }

  @Test
  public void lsOnFileTest() throws Exception {
    Directories directories = new Directories();
    File folder = tempFolder.newFolder();
    File file = new File(folder, "test");
    file.createNewFile();
    String path = file.getAbsolutePath();

    assertThat(directories.ls(path), is(path));
  }

  @Test
  public void lsOnNotExistingPath() throws Exception {
    Locale.setDefault(Locale.ROOT);
    Directories directories = new Directories();
    File folder = tempFolder.newFolder();
    String path = new File(folder, "test").getAbsolutePath();

    assertThat(directories.ls(path), is("Error: Path \"" + path + "\" couldn't be found!"));
  }

  @Test
  public void mkdirTest() throws Exception {
    Directories directories = new Directories();
    File folder = tempFolder.newFolder();
    File newFolder = new File(folder, "test");

    directories.mkdir(newFolder.getAbsolutePath());

    assertThat(newFolder.exists(), is(true));
  }

  @Test
  public void mkdirOnAlreadyExistingFolder() throws Exception {
    Locale.setDefault(Locale.ROOT);
    Directories directories = new Directories();
    File folder = tempFolder.newFolder();
    File newFolder = new File(folder, "test");
    newFolder.createNewFile();
    String path = newFolder.getAbsolutePath();
    assertThat(directories.mkdir(path), is("Error: Directory \"" + path + "\" already exists!"));
  }
}

@@ -598,32 +598,309 @@
 is(path));%0A  %7D%0A%0A
+  @Test%0A  public void cdIntoFile() throws IOException %7B%0A    Locale.setDefault(Locale.ROOT);%0A    Directories directories = new Directories();%0A    String path = tempFolder.newFile().getAbsolutePath();%0A%0A    assertThat(directories.cd(path), is(path + %22 is not a directory%22));%0A  %7D%0A%0A
   @Test%0A  public

add last test for Directories