From de94e48cdc6b37529c3de88fdefe62041d0d5fbc Mon Sep 17 00:00:00 2001 From: ShengXue97 Date: Tue, 28 Jan 2020 21:07:22 +0800 Subject: [PATCH] A-JUnit: Add JUnit Tess --- src/data/tasks.txt | 1 + src/main/java/duke/Duke.java | 22 ++++++-- src/main/java/tool/Command.java | 11 ---- src/main/java/tool/UI.java | 18 +++++- src/test/java/duke/DukeTest.java | 94 ++++++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 src/test/java/duke/DukeTest.java diff --git a/src/data/tasks.txt b/src/data/tasks.txt index 093a453525..0d6e8975a4 100644 --- a/src/data/tasks.txt +++ b/src/data/tasks.txt @@ -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 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 64eb6e089c..d8c8f593ea 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -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); } } diff --git a/src/main/java/tool/Command.java b/src/main/java/tool/Command.java index e6572773c8..f7c3c330fe 100644 --- a/src/main/java/tool/Command.java +++ b/src/main/java/tool/Command.java @@ -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 { @@ -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 @@ -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 @@ -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 ")) { @@ -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": @@ -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: diff --git a/src/main/java/tool/UI.java b/src/main/java/tool/UI.java index d4a233bb67..7946a4828e 100644 --- a/src/main/java/tool/UI.java +++ b/src/main/java/tool/UI.java @@ -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"; + } + } } @@ -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; } } diff --git a/src/test/java/duke/DukeTest.java b/src/test/java/duke/DukeTest.java new file mode 100644 index 0000000000..1532eab1c8 --- /dev/null +++ b/src/test/java/duke/DukeTest.java @@ -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); + } +}