|
| 1 | +package spike.ui; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.ByteArrayOutputStream; |
| 9 | +import java.io.PrintStream; |
| 10 | +import java.util.Random; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | + |
| 14 | +public class UiTest { |
| 15 | + // For testing system output |
| 16 | + private final PrintStream standardOut = System.out; |
| 17 | + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); |
| 18 | + |
| 19 | + |
| 20 | + @BeforeEach |
| 21 | + public void setUp() { |
| 22 | + System.setOut(new PrintStream(outputStreamCaptor)); |
| 23 | + } |
| 24 | + |
| 25 | + @AfterEach |
| 26 | + public void tearDown() { |
| 27 | + System.setIn(System.in); |
| 28 | + System.setOut(standardOut); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void getCommand_emptyCommand_getNextLine() { |
| 33 | + // For simulating system input |
| 34 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(" \nNext line\n".getBytes()); |
| 35 | + PrintStream printStream = new PrintStream(outputStreamCaptor); |
| 36 | + |
| 37 | + Ui ui = new Ui(inputStream, printStream); |
| 38 | + assertEquals("Next line", ui.getCommand()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void printMsg_anyString_formattedResponse() { |
| 43 | + PrintStream printStream = new PrintStream(outputStreamCaptor); |
| 44 | + |
| 45 | + Ui ui = new Ui(System.in, printStream); |
| 46 | + ui.printMsg("Any string"); |
| 47 | + String expected = "-------------------------------------------------\n" |
| 48 | + + "Any string" |
| 49 | + + "\n" |
| 50 | + + "-------------------------------------------------"; |
| 51 | + assertEquals(expected, outputStreamCaptor.toString().trim()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void getCommand_nonEmptyCommand_getCurrentLine() { |
| 56 | + // For simulating system input |
| 57 | + ByteArrayInputStream inputStream = new ByteArrayInputStream("Current line\n".getBytes()); |
| 58 | + PrintStream printStream = new PrintStream(outputStreamCaptor); |
| 59 | + |
| 60 | + Ui ui = new Ui(inputStream, printStream); |
| 61 | + assertEquals("Current line", ui.getCommand()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void showLoadingError_noInput_errorMsg() { |
| 66 | + PrintStream printStream = new PrintStream(outputStreamCaptor); |
| 67 | + |
| 68 | + Ui ui = new Ui(System.in, printStream); |
| 69 | + ui.showLoadingError(); |
| 70 | + String expected = "-------------------------------------------------\n" |
| 71 | + + "Sorry, I couldn't create the task list file for you." |
| 72 | + + "\n" |
| 73 | + + "-------------------------------------------------"; |
| 74 | + assertEquals(expected, outputStreamCaptor.toString().trim()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void greet_zeroListSize_greetNewUser() { |
| 79 | + Ui ui = new Ui(); |
| 80 | + ui.greet(0); |
| 81 | + String expected = "-------------------------------------------------\n" |
| 82 | + + "Hello! I am Spike ⊂( ・ ̫・)⊃ Nice to meet you!\nWhat can I do for you?" |
| 83 | + + "\n" |
| 84 | + + "-------------------------------------------------"; |
| 85 | + assertEquals(expected, outputStreamCaptor.toString().trim()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void greet_nonZeroListSize_greetOldUser() { |
| 90 | + Random random = new Random(); |
| 91 | + Ui ui = new Ui(); |
| 92 | + ui.greet(random.nextInt(1000) + 1); |
| 93 | + String expected = "-------------------------------------------------\n" |
| 94 | + + "Welcome back! Enter 'list' command to see your task list." |
| 95 | + + "\n" |
| 96 | + + "-------------------------------------------------"; |
| 97 | + assertEquals(expected, outputStreamCaptor.toString().trim()); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | +} |
0 commit comments