forked from nus-cs2103-AY2122S1/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JUnit tests for Ui class and some methods in Parser class
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package stub.message; | ||
|
||
import ui.message.Message; | ||
|
||
public class MessageStub extends Message { | ||
public MessageStub() { | ||
super("hello", ":)"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |