Skip to content

Commit 513918f

Browse files
committed
Add JUnit Tests for Ui and Parser class
1 parent 3f9747d commit 513918f

File tree

9 files changed

+159
-14
lines changed

9 files changed

+159
-14
lines changed

src/main/java/spike/Spike.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import spike.command.Command;
44
import spike.command.ExitCommand;
5+
import spike.exception.SpikeException;
56
import spike.parser.Parser;
67
import spike.storage.Storage;
78
import spike.task.*;
89
import spike.ui.Ui;
910

10-
import java.util.Scanner;
11-
1211
public class Spike {
1312
private Storage storage;
1413
private Ui ui;
@@ -25,7 +24,8 @@ public Spike(String directory, String filePath) {
2524
}
2625
}
2726

28-
public void run() {
27+
private void run() {
28+
ui.greet(tasks.getListSize());
2929
Command command;
3030
do {
3131
String input = ui.getCommand();

src/main/java/spike/command/ListCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package spike.command;
22

3-
import spike.Spike;
43
import spike.task.Task;
54
import spike.task.TaskList;
65

src/main/java/spike/exception/SpikeException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package spike;
1+
package spike.exception;
22

33
/**
44
* Class for all Spike specific exceptions

src/main/java/spike/parser/Parser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package spike.parser;
22

3-
import spike.SpikeException;
3+
import spike.exception.SpikeException;
44
import spike.command.*;
55
import spike.task.*;
66

@@ -166,7 +166,7 @@ private Command parseExit() {
166166
/**
167167
* Parses date and time input by user and returns valid LocalDateTime object
168168
*/
169-
public static LocalDateTime parseDateTime(String s, DateTimeFormatter dtf) {
169+
private static LocalDateTime parseDateTime(String s, DateTimeFormatter dtf) {
170170
try {
171171
LocalDateTime dateTime = LocalDateTime.parse(s, dtf);
172172
return dateTime;
@@ -179,7 +179,7 @@ public static LocalDateTime parseDateTime(String s, DateTimeFormatter dtf) {
179179
* Checks whether the input string is integer.
180180
* If yes, return it, else return -1.
181181
*/
182-
public static int isInt(String str) {
182+
private static int isInt(String str) {
183183
try {
184184
int x = Integer.parseInt(str);
185185
return x; // it is an integer
@@ -193,7 +193,7 @@ public static int isInt(String str) {
193193
* Checks whether it is a valid command.
194194
* If valid, return that command enum number, else return null.
195195
*/
196-
public CommandName validateCommand(String input) {
196+
private CommandName validateCommand(String input) {
197197
for (CommandName c : CommandName.values()) {
198198
if (c.getCommand().equals(input)) {
199199
return c;

src/main/java/spike/storage/Storage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package spike.storage;
22

3-
import spike.*;
3+
import spike.exception.SpikeException;
44
import spike.task.*;
55

66
import java.io.File;

src/main/java/spike/ui/Ui.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package spike.ui;
22

3+
import java.io.InputStream;
34
import java.io.PrintStream;
45
import java.util.Scanner;
56

@@ -13,7 +14,17 @@ public class Ui {
1314
public Ui() {
1415
this.in = new Scanner(System.in);
1516
this.out = System.out;
16-
greet();
17+
}
18+
19+
/**
20+
* Initializes Ui for unit testing.
21+
*
22+
* @param inputStream
23+
* @param printStream
24+
*/
25+
public Ui(InputStream inputStream, PrintStream printStream) {
26+
this.in = new Scanner(inputStream);
27+
this.out = printStream;
1728
}
1829

1930
/**
@@ -36,8 +47,12 @@ public String getCommand() {
3647
*
3748
* @return
3849
*/
39-
private void greet() {
40-
printMsg("Hello! I am Spike ⊂( ・ ̫・)⊃ Nice to meet you!\nWhat can I do for you?");
50+
public void greet(int taskListSize) {
51+
if (taskListSize > 0) {
52+
printMsg("Welcome back! Enter 'list' command to see your task list.");
53+
} else {
54+
printMsg("Hello! I am Spike ⊂( ・ ̫・)⊃ Nice to meet you!\nWhat can I do for you?");
55+
}
4156
}
4257

4358
/**
@@ -51,7 +66,7 @@ private boolean isEmptyCommand(String inputLine) {
5166
* Shows error in loading task file.
5267
*/
5368
public void showLoadingError() {
54-
out.println("Sorry, I couldn't create the task list file for you.");
69+
printMsg("Sorry, I couldn't create the task list file for you.");
5570
}
5671

5772
/**

src/test/java/spike/SpikeTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package spike;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
8+
public class SpikeTest {
9+
@Test
10+
public void dummyTest() {
11+
assertEquals(2, 2);
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package spike.parser;
2+
3+
import org.junit.jupiter.api.Test;
4+
import spike.command.IncorrectCommand;
5+
import spike.task.TaskList;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class ParserTest {
10+
@Test
11+
public void parseCommand_invalidCommand_incorrectCommandObject() {
12+
Parser parser = new Parser();
13+
Boolean isIncorrectCommand = parser.parseCommand("random text",
14+
new TaskList()) instanceof IncorrectCommand;
15+
assertEquals(true, isIncorrectCommand);
16+
}
17+
}

src/test/java/spike/ui/UiTest.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)