Skip to content

Commit

Permalink
A-JUnit: Add JUnit Tess
Browse files Browse the repository at this point in the history
  • Loading branch information
ShengXue97 committed Jan 28, 2020
1 parent 4808802 commit de94e48
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/data/tasks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ T|✕|todo
D|✕|finishpower|22 Oct. 2019 19:00pm
D|✕|finish cool yay|02 Oct. 2019 19:00pm
E|✕|watch fireworks with lin|01 Dec. 2019 19:00pm to 10 Dec. 2019 19:00pm
T|✕|sleep
22 changes: 17 additions & 5 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,45 @@

import tool.*;

import java.util.Scanner;

public class Duke {
public Duke(){ }

public void run(){
public String run(String inputString, boolean isTest){
UI.UIString = "";
TaskList taskList = new TaskList();
Storage storage = new Storage("src/data/tasks.txt", taskList);
UI ui = new UI(storage);

storage.readFromFile();
if (!isTest){
storage.readFromFile();
}

Parser parser = new Parser(ui, taskList);

ui.printWelcomeMessage();
boolean isExit = false;
Scanner stringScanner = new Scanner(inputString);

while (!isExit) {
String fullCommand = ui.readCommand();
String fullCommand = ui.readCommand(stringScanner, isTest);
ui.printLine(); // show the divider line ("_______")
Command c = parser.parse(fullCommand);
isExit = c.execute(taskList, ui);
ui.printLine();
storage.saveToFile();

if (!isTest){
storage.saveToFile();
}
}

return UI.UIString;
}

public static void main(String[] args) {
Duke duke = new Duke();
duke.run();
duke.run("", false);
}
}

11 changes: 0 additions & 11 deletions src/main/java/tool/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public boolean execute(TaskList taskList, UI ui) {
for (int i = 0; i < taskList.size(); i++) {
ui.print(String.valueOf(i + 1) + "." + taskList.get(i));
}
ui.printLine();
return false;
case "done":
try {
Expand All @@ -47,10 +46,8 @@ public boolean execute(TaskList taskList, UI ui) {
"you have.");
} else {
taskList.get(num - 1).markAsDone();
ui.printLine();
ui.print("Nice! I've marked this task as done:");
ui.print(taskList.get(num - 1).toString());
ui.printLine();
}
} catch (NumberFormatException ex) {
//handle exception here
Expand All @@ -69,11 +66,9 @@ public boolean execute(TaskList taskList, UI ui) {
"you have.");
} else {
Task removedTask = taskList.remove(num - 1);
ui.printLine();
ui.print("Noted. I've removed this task: ");
ui.print(removedTask.toString());
ui.print("Now you have " + String.valueOf(taskList.size()) + " tasks in the list.");
ui.printLine();
}
} catch (NumberFormatException ex) {
//handle exception here
Expand All @@ -83,11 +78,9 @@ public boolean execute(TaskList taskList, UI ui) {
case "todo":
Task newToDo = new ToDos(arguments);
taskList.add(newToDo);
ui.printLine();
ui.print("Got it. I've added this task:");
ui.print(newToDo.toString());
ui.print("Now you have " + String.valueOf(taskList.size()) + " tasks in the list.");
ui.printLine();
return false;
case "deadline":
if (!arguments.contains("/by ")) {
Expand All @@ -109,11 +102,9 @@ public boolean execute(TaskList taskList, UI ui) {
String newDateTime = datetimeParsed.format(DateTimeFormatter.ofPattern("dd MMM yyyy HH:mma"));
Task newDeadline = new Deadlines(description, newDateTime);
taskList.add(newDeadline);
ui.printLine();
ui.print("Got it. I've added this task:");
ui.print(newDeadline.toString());
ui.print("Now you have " + String.valueOf(taskList.size()) + " tasks in the list.");
ui.printLine();
}
return false;
case "event":
Expand Down Expand Up @@ -145,11 +136,9 @@ public boolean execute(TaskList taskList, UI ui) {

Task newTask = new Events(description, newDateTime);
taskList.add(newTask);
ui.printLine();
ui.print("Got it. I've added this task:");
ui.print(newTask.toString());
ui.print("Now you have " + String.valueOf(taskList.size()) + " tasks in the list.");
ui.printLine();
}
return false;
default:
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/tool/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
import java.util.Scanner;

public class UI {
public static String UIString = "";
private static String indent = " ";
private static String line = " ____________________________________________________________";
private Storage storage;
private Scanner sc = new Scanner(System.in);
private Scanner userScanner = new Scanner(System.in);
public UI(Storage storage){
this.storage = storage;
}

public String readCommand(){
return sc.nextLine();
public String readCommand(Scanner stringScanner, boolean isTest){
if (stringScanner.hasNextLine()){
return stringScanner.nextLine();
} else {
if (!isTest){
return userScanner.nextLine();
} else {
return "bye";
}
}
}


Expand All @@ -28,16 +37,19 @@ public void printWelcomeMessage(){

public void printLine(){
System.out.println(line);
UIString += line + "\n";
}

public void print(String message){
System.out.println(indent + message);
UIString += indent + message+ "\n";
}

public void printError(String message){
System.out.println(line);
System.out.println(indent + message);
System.out.println(line);
UIString += line + "\n" + indent + message + "\n" + line;
}

}
94 changes: 94 additions & 0 deletions src/test/java/duke/DukeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package duke;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DukeTest {
@Test
public void testDelete(){
Duke duke = new Duke();
String actual = duke.run("todo sleep 100 years\ndelete 1\nlist", true);
String expected = " ____________________________________________________________\n" +
" Got it. I've added this task:\n" +
" [T][✕] sleep 100 years\n" +
" Now you have 1 tasks in the list.\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Noted. I've removed this task: \n" +
" [T][✕] sleep 100 years\n" +
" Now you have 0 tasks in the list.\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Here are the tasks in your list:\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Bye. Hope to see you again soon!\n" +
" ____________________________________________________________\n";
assertEquals(expected, actual);
}

@Test
public void testDone(){
Duke duke = new Duke();
String actual = duke.run("todo sleep 100 years\nevent fireworks /at 2019-10-12 20:00 to 2019-10-12 21:00\ndone 1\nlist", true);
String expected = " ____________________________________________________________\n" +
" Got it. I've added this task:\n" +
" [T][✕] sleep 100 years\n" +
" Now you have 1 tasks in the list.\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Got it. I've added this task:\n" +
" [E][✕] fireworks (at: 12 Oct. 2019 20:00pm to 12 Oct. 2019 21:00pm)\n" +
" Now you have 2 tasks in the list.\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Nice! I've marked this task as done:\n" +
" [T][✓] sleep 100 years\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Here are the tasks in your list:\n" +
" 1.[T][✓] sleep 100 years\n" +
" 2.[E][✕] fireworks (at: 12 Oct. 2019 20:00pm to 12 Oct. 2019 21:00pm)\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Bye. Hope to see you again soon!\n" +
" ____________________________________________________________\n";
assertEquals(expected, actual);
}

@Test
public void testTasks(){
Duke duke = new Duke();
String actual = duke.run("todo study 24 hours\ndeadline get six packs /by 2020-02-29 09:00\nevent holidaysss /at 2020-05-01 20:00 to 2019-08-01 20:00\ndone 1\nlist", true);
String expected = " ____________________________________________________________\n" +
" Got it. I've added this task:\n" +
" [T][✕] study 24 hours\n" +
" Now you have 1 tasks in the list.\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Got it. I've added this task:\n" +
" [D][✕] get six packs (by: 29 Feb. 2020 09:00am)\n" +
" Now you have 2 tasks in the list.\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Got it. I've added this task:\n" +
" [E][✕] holidaysss (at: 01 May 2020 20:00pm to 01 Aug. 2019 20:00pm)\n" +
" Now you have 3 tasks in the list.\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Nice! I've marked this task as done:\n" +
" [T][✓] study 24 hours\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Here are the tasks in your list:\n" +
" 1.[T][✓] study 24 hours\n" +
" 2.[D][✕] get six packs (by: 29 Feb. 2020 09:00am)\n" +
" 3.[E][✕] holidaysss (at: 01 May 2020 20:00pm to 01 Aug. 2019 20:00pm)\n" +
" ____________________________________________________________\n" +
" ____________________________________________________________\n" +
" Bye. Hope to see you again soon!\n" +
" ____________________________________________________________\n";
assertEquals(expected, actual);
}
}

0 comments on commit de94e48

Please sign in to comment.