From 2370922c54fba18f983804d50d33596374feacb3 Mon Sep 17 00:00:00 2001 From: Jonas Hasaas <jonas.hasaas@student.uib.no> Date: Sun, 11 Feb 2024 23:35:18 +0100 Subject: [PATCH] Obligatoriske del av lab 3 godgjent i tester --- .../java/no/uib/inf101/terminal/CmdEcho.java | 20 +++++ .../java/no/uib/inf101/terminal/Command.java | 6 ++ .../java/no/uib/inf101/terminal/Main.java | 7 +- .../no/uib/inf101/terminal/SimpleShell.java | 20 +++-- .../no/uib/inf101/terminal/TextAnswers.java | 8 +- .../no/uib/inf101/terminal/TestCmdEcho.java | 38 ++++----- .../inf101/terminal/TestSimpleShellEcho.java | 44 +++++----- .../terminal/TestSimpleShellStarter.java | 80 +++++++++---------- 8 files changed, 130 insertions(+), 93 deletions(-) create mode 100644 src/main/java/no/uib/inf101/terminal/CmdEcho.java create mode 100644 src/main/java/no/uib/inf101/terminal/Command.java diff --git a/src/main/java/no/uib/inf101/terminal/CmdEcho.java b/src/main/java/no/uib/inf101/terminal/CmdEcho.java new file mode 100644 index 0000000..22b3251 --- /dev/null +++ b/src/main/java/no/uib/inf101/terminal/CmdEcho.java @@ -0,0 +1,20 @@ +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 + diff --git a/src/main/java/no/uib/inf101/terminal/Command.java b/src/main/java/no/uib/inf101/terminal/Command.java new file mode 100644 index 0000000..1f5abc2 --- /dev/null +++ b/src/main/java/no/uib/inf101/terminal/Command.java @@ -0,0 +1,6 @@ +package no.uib.inf101.terminal; + +public interface Command { + String run(String[] args); + String getName(); +} diff --git a/src/main/java/no/uib/inf101/terminal/Main.java b/src/main/java/no/uib/inf101/terminal/Main.java index 48d6772..f9a78ed 100644 --- a/src/main/java/no/uib/inf101/terminal/Main.java +++ b/src/main/java/no/uib/inf101/terminal/Main.java @@ -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 diff --git a/src/main/java/no/uib/inf101/terminal/SimpleShell.java b/src/main/java/no/uib/inf101/terminal/SimpleShell.java index 85fb46d..e4a26af 100644 --- a/src/main/java/no/uib/inf101/terminal/SimpleShell.java +++ b/src/main/java/no/uib/inf101/terminal/SimpleShell.java @@ -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); diff --git a/src/main/java/no/uib/inf101/terminal/TextAnswers.java b/src/main/java/no/uib/inf101/terminal/TextAnswers.java index 7c54793..5d0b087 100644 --- a/src/main/java/no/uib/inf101/terminal/TextAnswers.java +++ b/src/main/java/no/uib/inf101/terminal/TextAnswers.java @@ -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; } diff --git a/src/test/java/no/uib/inf101/terminal/TestCmdEcho.java b/src/test/java/no/uib/inf101/terminal/TestCmdEcho.java index 8ba7367..a466258 100644 --- a/src/test/java/no/uib/inf101/terminal/TestCmdEcho.java +++ b/src/test/java/no/uib/inf101/terminal/TestCmdEcho.java @@ -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)); + } } diff --git a/src/test/java/no/uib/inf101/terminal/TestSimpleShellEcho.java b/src/test/java/no/uib/inf101/terminal/TestSimpleShellEcho.java index 1783ebb..93a8ef8 100644 --- a/src/test/java/no/uib/inf101/terminal/TestSimpleShellEcho.java +++ b/src/test/java/no/uib/inf101/terminal/TestSimpleShellEcho.java @@ -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()); + } } diff --git a/src/test/java/no/uib/inf101/terminal/TestSimpleShellStarter.java b/src/test/java/no/uib/inf101/terminal/TestSimpleShellStarter.java index a483993..c688050 100644 --- a/src/test/java/no/uib/inf101/terminal/TestSimpleShellStarter.java +++ b/src/test/java/no/uib/inf101/terminal/TestSimpleShellStarter.java @@ -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()); } -- GitLab