From fce496651cd6f94531d31ca00fa553bff046ff73 Mon Sep 17 00:00:00 2001 From: Darren159 Date: Fri, 1 Sep 2023 19:48:58 +0800 Subject: [PATCH] Add JUnit tests --- src/test/java/duke/ParserTest.java | 59 +++++++++++++++++++++++++++ src/test/java/duke/UiTest.java | 48 ++++++++++++++++++++++ src/test/java/duke/task/ToDoTest.java | 21 ++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/test/java/duke/ParserTest.java create mode 100644 src/test/java/duke/UiTest.java create mode 100644 src/test/java/duke/task/ToDoTest.java diff --git a/src/test/java/duke/ParserTest.java b/src/test/java/duke/ParserTest.java new file mode 100644 index 0000000000..d5056a18a9 --- /dev/null +++ b/src/test/java/duke/ParserTest.java @@ -0,0 +1,59 @@ +package duke; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import duke.Parser; +import duke.command.AddCommand; +import duke.command.DeleteCommand; +import duke.command.ExitCommand; +import duke.command.ListCommand; +import duke.command.MarkCommand; +import duke.command.UnmarkCommand; +import duke.command.Command; +import duke.exception.InvalidArgumentException; +import org.junit.jupiter.api.Test; + +public class ParserTest { + + @Test + public void testParseListCommand() throws InvalidArgumentException { + Command command = Parser.parse("list"); + assertTrue(command instanceof ListCommand); + } + + @Test + public void testParseDeleteCommand() throws InvalidArgumentException { + Command command = Parser.parse("delete 1"); + assertTrue(command instanceof DeleteCommand); + } + + @Test + public void testParseMarkCommand() throws InvalidArgumentException { + Command command = Parser.parse("mark 1"); + assertTrue(command instanceof MarkCommand); + } + + @Test + public void testParseUnmarkCommand() throws InvalidArgumentException { + Command command = Parser.parse("unmark 1"); + assertTrue(command instanceof UnmarkCommand); + } + + @Test + public void testParseAddCommand() throws InvalidArgumentException { + Command command = Parser.parse("todo Test"); + assertTrue(command instanceof AddCommand); + } + + @Test + public void testParseExitCommand() throws InvalidArgumentException { + Command command = Parser.parse("bye"); + assertTrue(command instanceof ExitCommand); + } + + @Test + public void testInvalidCommand() { + assertThrows(InvalidArgumentException.class, () -> Parser.parse("invalid_command")); + } +} diff --git a/src/test/java/duke/UiTest.java b/src/test/java/duke/UiTest.java new file mode 100644 index 0000000000..1b0c6d7cb4 --- /dev/null +++ b/src/test/java/duke/UiTest.java @@ -0,0 +1,48 @@ +package duke; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class UiTest { + private final PrintStream standardOut = System.out; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @Test + public void testShowWelcome() { + Ui ui = new Ui(); + ui.showWelcome(); + String expected = "__________________________________________________________________________" + + " ___ _ ___ ________ __ ____ __________ ____ _ __\n" + + " / | / | / / | / / __ \\ \\/ / / __ \\ /_ __/ __ \\/ __ \\/ | / /\n" + + " / /| | / |/ / |/ / / / /\\ /_____/ / / /_____/ / / /_/ / / / / |/ / \n" + + " / ___ |/ /| / /| / /_/ / / /_____/ /_/ /_____/ / / _, _/ /_/ / /| / \n" + + "/_/ |_/_/ |_/_/ |_/\\____/ /_/ \\____/ /_/ /_/ |_|\\____/_/ |_/ \n" + + "Hello! I'm ANNOY-O-TRON!\nWhat can I do for you?\n" + + "__________________________________________________________________________"; + assertEquals(expected, outputStreamCaptor.toString().trim()); + } + + @Test + public void testShowBye() { + Ui ui = new Ui(); + ui.showBye(); + String expected = "Bye. Hope to see you again soon!"; + assertEquals(expected, outputStreamCaptor.toString().trim()); + } + + + @AfterEach + public void tearDown() { + System.setOut(standardOut); + } +} diff --git a/src/test/java/duke/task/ToDoTest.java b/src/test/java/duke/task/ToDoTest.java new file mode 100644 index 0000000000..ae52906933 --- /dev/null +++ b/src/test/java/duke/task/ToDoTest.java @@ -0,0 +1,21 @@ +package duke.task; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ToDoTest { + @Test + public void testToString() { + Todo todo = new Todo("Sample Task"); + String expected = "[T][ ] Sample Task"; + assertEquals(expected, todo.toString()); + } + + @Test + public void testToFileString() { + Todo todo = new Todo("Sample Task"); + String expected = "T | 0 | Sample Task"; + assertEquals(expected, todo.toFileString()); + } +}