diff --git a/src/test/java/parser/ParserTest.java b/src/test/java/parser/ParserTest.java new file mode 100644 index 0000000000..21e32e0c43 --- /dev/null +++ b/src/test/java/parser/ParserTest.java @@ -0,0 +1,29 @@ +package parser; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; + +import static org.junit.Assert.assertEquals; + +public class ParserTest { + @Test + public void detectExitCommand_exitCommand_true() { + String exitCommand = "bye"; + System.setIn(new ByteArrayInputStream(exitCommand.getBytes())); + boolean isExitCommand = new Parser().detectExitCommand(exitCommand); + + assertEquals(true, isExitCommand); + System.setIn(System.in); + } + + @Test + public void detectExitCommand_nonExitCommand_false() { + String nonExitCommand = "hey"; + System.setIn(new ByteArrayInputStream(nonExitCommand.getBytes())); + boolean isExitCommand = new Parser().detectExitCommand(nonExitCommand); + + assertEquals(false, isExitCommand); + System.setIn(System.in); + } +} diff --git a/src/test/java/stub/message/MessageStub.java b/src/test/java/stub/message/MessageStub.java new file mode 100644 index 0000000000..dcb9092a07 --- /dev/null +++ b/src/test/java/stub/message/MessageStub.java @@ -0,0 +1,9 @@ +package stub.message; + +import ui.message.Message; + +public class MessageStub extends Message { + public MessageStub() { + super("hello", ":)"); + } +} diff --git a/src/test/java/ui/UiTest.java b/src/test/java/ui/UiTest.java new file mode 100644 index 0000000000..57769076de --- /dev/null +++ b/src/test/java/ui/UiTest.java @@ -0,0 +1,76 @@ +package ui; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import stub.message.MessageStub; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.Assert.assertEquals; + +public class UiTest { + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private final PrintStream originalErr = System.err; + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @After + public void restoreStreams() { + System.setOut(originalOut); + System.setErr(originalErr); + } + + @Test + public void testShowWelcome() { + new Ui().showWelcome(); + + assertEquals( + "\t_________________________________________________\n" + + "\tHello! I'm Duke, what shall we do today? ٩(。•́‿•̀。)۶\n" + + "\t_________________________________________________\n", + outContent.toString() + ); + } + + @Test + public void testShowGoodbye() { + new Ui().showGoodbye(); + + assertEquals( + "\t_________________________________________________\n" + + "\tBye, see you again ヾ(=´・∀・`=)\n" + + "\t_________________________________________________\n", + outContent.toString() + ); + } + + @Test + public void testShowMessage() { + new Ui().showMessage(new MessageStub()); + + assertEquals("\t_________________________________________________\n" + + "\thello :)\n" + + "\t_________________________________________________\n", + outContent.toString() + ); + } + + @Test + public void testReadInputMessage() { + String inputMessage = "how are you"; + System.setIn(new ByteArrayInputStream(inputMessage.getBytes())); + String inputReadByUi = new Ui().readInputMessage(); + + assertEquals(inputMessage, inputReadByUi); + System.setIn(System.in); + } +}