Skip to content
Snippets Groups Projects
Commit c8835647 authored by Torstein Strømme's avatar Torstein Strømme
Browse files

version 0.1 starter code

parents
No related branches found
No related tags found
No related merge requests found
package no.uib.inf101.terminal;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
public class TestSimpleShellEcho {
// @Test
// public void testEchoInSimpleShell() {
// SimpleShell shell = new SimpleShell();
// shell.installCommand(new CmdEcho());
//
// shell.keyPressed('e');
// shell.keyPressed('c');
// shell.keyPressed('h');
// shell.keyPressed('o');
// shell.keyPressed(' ');
// shell.keyPressed('f');
// shell.keyPressed('o');
// shell.keyPressed('o');
// shell.keyPressed(' ');
// shell.keyPressed('b');
// shell.keyPressed('a');
// shell.keyPressed('r');
// shell.keyPressed('\n');
//
// String expected = "$ echo foo bar \nfoo bar \n$ ";
// assertEquals(expected, shell.getScreenContent());
// }
}
package no.uib.inf101.terminal;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestSimpleShellStarter {
static final String orgCwd = System.getProperty("user.dir");
static final String DIR = "testdir";
static final String SUBDIR = "subdir";
private File dir;
private File subdir;
private SimpleShell shell;
////////////////////////////////////////////////////////////////////////
//////// The tests ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
@Test
public void testDoNothing() {
assertEquals("$ ", shell.whatTheScreenLooksLike());
}
@Test
public void testWriteFoo() {
shell.aKeyIsPressed('f');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('o');
assertEquals("$ foo", shell.whatTheScreenLooksLike());
}
@Test
public void testIllegalCommand() {
shell.aKeyIsPressed('f');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('\n');
assertEquals("$ foo\nCommand not found: \"foo\"\n$ ", shell.whatTheScreenLooksLike());
}
@Test
public void testPwd() throws IOException {
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
String expected = "$ pwd" + "\n" + this.dir.getCanonicalPath() + "\n$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
}
@Test
public void testLs() throws IOException {
shell.aKeyIsPressed('l');
shell.aKeyIsPressed('s');
shell.aKeyIsPressed('\n');
String expected = "$ ls\n" + SUBDIR + " \n$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
}
@Test
public void testCd() throws IOException {
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
shell.aKeyIsPressed('c');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed(' ');
shell.aKeyIsPressed('s');
shell.aKeyIsPressed('u');
shell.aKeyIsPressed('b');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('i');
shell.aKeyIsPressed('r');
shell.aKeyIsPressed('\n');
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
String expected = "$ pwd\n"
+ this.dir.getCanonicalPath() + "\n"
+ "$ cd subdir\n"
+ "$ pwd\n"
+ this.subdir.getCanonicalPath() + "\n"
+ "$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
}
////////////////////////////////////////////////////////////////////////
//////// Preparing the tests //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
@BeforeEach
public void setUp() throws IOException {
// Set up test directory
File cwd = new File(orgCwd);
this.dir = new File(cwd, DIR);
this.subdir = new File(this.dir, SUBDIR);
this.dir.mkdir();
this.subdir.mkdir();
System.setProperty("user.dir", this.dir.getCanonicalPath());
this.shell = new SimpleShell();
}
@AfterEach
public void tearDown() {
File cwd = new File(System.getProperty("user.dir"));
File testDir = new File(cwd, DIR);
deleteFolderAndItsContent(testDir);
System.setProperty("user.dir", orgCwd);
this.shell = null;
}
private void deleteFolderAndItsContent(File file) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
deleteFolderAndItsContent(f);
}
}
file.delete();
}
}
package no.uib.inf101.terminal;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestTextAnswers {
@Test
public void testQ1() {
assertEquals("CommandLineInterface", TextAnswers.q1);
}
@Test
public void testQ2() {
assertEquals("DummyShell", TextAnswers.q2);
}
@Test
public void testQ3() {
assertEquals(true, TextAnswers.q3, "Every interface defines a type, so "
+ "since CommandLineInterface is an interface, it is also a type.");
}
@Test
public void testQ4() {
assertEquals(true, TextAnswers.q4, "Every class also defines a "
+ "type, so since DummyShell is a class, it must also be a type.");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment