Skip to content
Snippets Groups Projects
Commit 2370922c authored by Jonas.Hasaas's avatar Jonas.Hasaas
Browse files

Obligatoriske del av lab 3 godgjent i tester

parent 4ebc3f91
Branches main
No related tags found
No related merge requests found
package no.uib.inf101.terminal;
public class CmdEcho implements Command {
@Override
public String getName() {
return "echo";
}
@Override
public String run(String[] args) {
StringBuilder result = new StringBuilder();
for (String arg : args) {
result.append(arg).append(" ");
}
return result.toString();
}
}
//ny fil oprettet av meg // lagt in kode fra chatgpt
package no.uib.inf101.terminal;
public interface Command {
String run(String[] args);
String getName();
}
......@@ -6,11 +6,12 @@ package no.uib.inf101.terminal;
public class Main {
public static void main(String[] args) {
// Create a new shell
CommandLineInterface shell = new DummyShell();
SimpleShell shell = new SimpleShell();
shell.installCommand(new CmdEcho());
// Run the shell in the terminal GUI
Terminal gui = new Terminal(shell);
gui.run();
}
}
\ No newline at end of file
......@@ -15,11 +15,16 @@ package no.uib.inf101.terminal;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
// TODO 1: Let SimpleShell implement CommandLineInterface
public class SimpleShell {
public class SimpleShell implements CommandLineInterface {
private final HashMap<String, Command> allCommands = new HashMap<>();
public void installCommand(Command command) {
this.allCommands.put(command.getName(), command);
}
//////////////////////////////////////////////////////////////////////
/// Instance variables ///////////////////////////////////////////////
......@@ -55,7 +60,8 @@ public class SimpleShell {
*
* @param key The key that was pressed
*/
public void aKeyIsPressed(char key) {
@Override
public void keyPressed(char key) {
if (key == '\n') {
this.processCurrentCommandLine();
} else if (key >= ' ' && key <= '~') {
......@@ -71,9 +77,10 @@ public class SimpleShell {
*
* @return the text
*/
public String whatTheScreenLooksLike() {
@Override
public String getScreenContent() {
String s = "";
for (String line : this.history) {
for (String line : this.history) {
s += line;
}
s += this.prompt;
......@@ -122,7 +129,10 @@ public class SimpleShell {
private String executeCommand(String commandName, String[] args) {
// TODO 6: Call run on Command object for given commandName if present
// TODO 7-8-9: Remove if's for cd, ls, pwd once installed as commands
if (Objects.equals(commandName, "pwd")) {
Command command = this.allCommands.get(commandName);
if (command != null) {
return command.run(args);
} else if (Objects.equals(commandName, "pwd")) {
return this.doPwd(args);
} else if (Objects.equals(commandName, "cd")) {
return this.doCd(args);
......
......@@ -13,7 +13,7 @@ public class TextAnswers {
*
* What is the type of the variable cli?
*/
static final String q1 = "";
static final String q1 = "CommandLineInterface";
/**
* Consider the following code:
......@@ -21,7 +21,7 @@ public class TextAnswers {
*
* In which class is the object cli refers to?
*/
static final String q2 = "";
static final String q2 = "DummyShell";
/**
* Consider the following code:
......@@ -29,7 +29,7 @@ public class TextAnswers {
*
* true or false: CommandLineInterface is both a type and an interface.
*/
static final Boolean q3 = null;
static final Boolean q3 = true;
/**
* Consider the following code:
......@@ -37,6 +37,6 @@ public class TextAnswers {
*
* true or false: DummyShell is both a class and a type.
*/
static final Boolean q4 = null;
static final Boolean q4 = true;
}
......@@ -5,23 +5,23 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestCmdEcho {
// @Test
// public void testGetName() {
// Command echo = new CmdEcho();
// assertEquals("echo", echo.getName());
// }
//
// @Test
// public void testRun() {
// Command echo = new CmdEcho();
//
// String[] args = new String[] {"foo", "bar"};
// assertEquals("foo bar ", echo.run(args));
//
// args = new String[] {"hello"};
// assertEquals("hello ", echo.run(args));
//
// args = new String[] {};
// assertEquals("", echo.run(args));
// }
@Test
public void testGetName() {
Command echo = new CmdEcho();
assertEquals("echo", echo.getName());
}
@Test
public void testRun() {
Command echo = new CmdEcho();
String[] args = new String[] {"foo", "bar"};
assertEquals("foo bar ", echo.run(args));
args = new String[] {"hello"};
assertEquals("hello ", echo.run(args));
args = new String[] {};
assertEquals("", echo.run(args));
}
}
......@@ -11,26 +11,26 @@ 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());
// }
@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());
}
}
......@@ -23,71 +23,71 @@ public class TestSimpleShellStarter {
@Test
public void testDoNothing() {
assertEquals("$ ", shell.whatTheScreenLooksLike());
assertEquals("$ ", shell.getScreenContent());
}
@Test
public void testWriteFoo() {
shell.aKeyIsPressed('f');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('o');
shell.keyPressed('f');
shell.keyPressed('o');
shell.keyPressed('o');
assertEquals("$ foo", shell.whatTheScreenLooksLike());
assertEquals("$ foo", shell.getScreenContent());
}
@Test
public void testIllegalCommand() {
shell.aKeyIsPressed('f');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('o');
shell.aKeyIsPressed('\n');
shell.keyPressed('f');
shell.keyPressed('o');
shell.keyPressed('o');
shell.keyPressed('\n');
assertEquals("$ foo\nCommand not found: \"foo\"\n$ ", shell.whatTheScreenLooksLike());
assertEquals("$ foo\nCommand not found: \"foo\"\n$ ", shell.getScreenContent());
}
@Test
public void testPwd() throws IOException {
shell.aKeyIsPressed('p');
shell.aKeyIsPressed('w');
shell.aKeyIsPressed('d');
shell.aKeyIsPressed('\n');
shell.keyPressed('p');
shell.keyPressed('w');
shell.keyPressed('d');
shell.keyPressed('\n');
String expected = "$ pwd" + "\n" + this.dir.getCanonicalPath() + "\n$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
assertEquals(expected, shell.getScreenContent());
}
@Test
public void testLs() throws IOException {
shell.aKeyIsPressed('l');
shell.aKeyIsPressed('s');
shell.aKeyIsPressed('\n');
shell.keyPressed('l');
shell.keyPressed('s');
shell.keyPressed('\n');
String expected = "$ ls\n" + SUBDIR + " \n$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
assertEquals(expected, shell.getScreenContent());
}
@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');
shell.keyPressed('p');
shell.keyPressed('w');
shell.keyPressed('d');
shell.keyPressed('\n');
shell.keyPressed('c');
shell.keyPressed('d');
shell.keyPressed(' ');
shell.keyPressed('s');
shell.keyPressed('u');
shell.keyPressed('b');
shell.keyPressed('d');
shell.keyPressed('i');
shell.keyPressed('r');
shell.keyPressed('\n');
shell.keyPressed('p');
shell.keyPressed('w');
shell.keyPressed('d');
shell.keyPressed('\n');
String expected = "$ pwd\n"
......@@ -97,7 +97,7 @@ public class TestSimpleShellStarter {
+ this.subdir.getCanonicalPath() + "\n"
+ "$ ";
assertEquals(expected, shell.whatTheScreenLooksLike());
assertEquals(expected, shell.getScreenContent());
}
......
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