From 27e21bf35cd92e54175c1013f030a3903f1e880f Mon Sep 17 00:00:00 2001 From: LimZiJia Date: Fri, 16 Feb 2024 01:54:43 +0800 Subject: [PATCH] Add CI and fix tests --- .github/workflows/gradle.yml | 34 +++++ data/duke.txt | 8 - src/test/duke/ParserTest.java | 247 ++++++++++++++----------------- src/test/tasks/TaskListTest.java | 17 ++- 4 files changed, 157 insertions(+), 149 deletions(-) create mode 100644 .github/workflows/gradle.yml diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 0000000000..391c46b4fe --- /dev/null +++ b/.github/workflows/gradle.yml @@ -0,0 +1,34 @@ +name: Java CI + +on: [push, pull_request] + +jobs: + build: + strategy: + matrix: + platform: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.platform }} + + steps: + - name: Set up repository + uses: actions/checkout@master + + - name: Set up repository + uses: actions/checkout@master + with: + ref: master + + - name: Merge to master + run: git checkout --progress --force ${{ github.sha }} + + - name: Validate Gradle Wrapper + uses: gradle/wrapper-validation-action@v1 + + - name: Setup JDK 11 + uses: actions/setup-java@v1 + with: + java-version: '11' + java-package: jdk+fx + + - name: Build and check with Gradle + run: ./gradlew check diff --git a/data/duke.txt b/data/duke.txt index bf3ff19534..e69de29bb2 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1,8 +0,0 @@ -T,0,2 -T,0,3 -T,0,5 -T,0,4 -T,0,4 -T,0,s -T,0,s -T,0,a diff --git a/src/test/duke/ParserTest.java b/src/test/duke/ParserTest.java index 5dbf7c391e..c96ad47859 100644 --- a/src/test/duke/ParserTest.java +++ b/src/test/duke/ParserTest.java @@ -13,6 +13,8 @@ import java.util.Arrays; import java.util.List; +import duke.duke.Duke; +import duke.ui.Skibidi; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; @@ -30,31 +32,33 @@ @ExtendWith(MockitoExtension.class) class ParserTest { private Parser p = new Parser(); + private Duke d = new Duke("/data/", "duke.txt"); @Test void byeShouldReturnNull() { - TaskList tl = new TaskList(); - String in = "bye"; - Storage store = new Storage(); - assertNull(p.parse(in, tl, store)); + String input = "bye"; + assertEquals(Skibidi.BYE, p.parse(input)); } @Test void listShouldCallPrintList() { TaskList tl = mock(TaskList.class); - String in = "list"; - Storage store = new Storage(); - p.parse(in, tl, store); + Duke.tasks = tl; + String input = "list"; + p.parse(input); verify(tl).printList(); } @Test void saveShouldCallStorageSaveSuccess() { TaskList tl = new TaskList(); - String in = "save"; + String input = "save"; Storage store = mock(Storage.class); + + Duke.storage = store; + Duke.tasks = tl; boolean thrown = false; try { - p.parse(in, tl, store); + p.parse(input); verify(store).save(tl); } catch (IOException e) { thrown = true; @@ -65,13 +69,16 @@ void saveShouldCallStorageSaveSuccess() { @Test void markShouldReturnMarkedTasks() { List lstBefore = new ArrayList<>(Arrays.asList(new Todo("task1"), new Todo("task2"))); - List lstAfter = new ArrayList<>(Arrays.asList(new Todo("task1"), + List lstAfter = new ArrayList<>(Arrays.asList( + new Todo("task1"), new Todo(true, "task2"))); TaskList tl1 = new TaskList(lstBefore); TaskList tl2 = new TaskList(lstAfter); - String in = "mark 2"; - Storage store = new Storage(); - tl1 = p.parse(in, tl1, store); + + String input = "mark 2"; + Duke.tasks = tl1; + + p.parse(input); assertEquals(tl2, tl1); } @@ -82,9 +89,11 @@ void unmarkShouldReturnUnmarkedTasks() { List lstAfter = new ArrayList<>(Arrays.asList(new Todo("task1"), new Todo("task2"))); TaskList tl1 = new TaskList(lstBefore); TaskList tl2 = new TaskList(lstAfter); - String in = "unmark 2"; - Storage store = new Storage(); - tl1 = p.parse(in, tl1, store); + + String input = "unmark 2"; + Duke.tasks = tl1; + + p.parse(input); assertEquals(tl2, tl1); } @@ -94,9 +103,11 @@ void deleteShouldReturnDeletedTaskList() { List lstAfter = new ArrayList<>(Arrays.asList(new Todo("task1"))); TaskList tl1 = new TaskList(lstBefore); TaskList tl2 = new TaskList(lstAfter); - String in = "delete 2"; - Storage store = new Storage(); - tl1 = p.parse(in, tl1, store); + + String input = "delete 2"; + Duke.tasks = tl1; + + p.parse(input); assertEquals(tl2, tl1); } @@ -106,8 +117,11 @@ void todoShouldAddTodoTask() { List lstAfter = new ArrayList<>(Arrays.asList(new Todo("task1"), new Todo("task2"))); TaskList tl1 = new TaskList(lstBefore); TaskList tl2 = new TaskList(lstAfter); - String in = "todo task2"; - tl1 = p.addTask(in, tl1); + + String input = "todo task2"; + Duke.tasks = tl1; + + p.parse(input); assertEquals(tl2, tl1); } @@ -118,8 +132,11 @@ void deadlineShouldAddDeadlineTask() { new Todo("task1"), new Deadline("task2", "2024-01-31"))); TaskList tl1 = new TaskList(lstBefore); TaskList tl2 = new TaskList(lstAfter); - String in = "deadline task2 /by 2024-01-31"; - tl1 = p.addTask(in, tl1); + + String input = "deadline task2 /by 2024-01-31"; + Duke.tasks = tl1; + + p.parse(input); assertEquals(tl2, tl1); } @@ -130,164 +147,128 @@ void eventShouldAddEventTask() { new Todo("task1"), new Event("task2", "2024-01-31", "2024-02-01"))); TaskList tl1 = new TaskList(lstBefore); TaskList tl2 = new TaskList(lstAfter); - String in = "event task2 /from 2024-01-31 /to 2024-02-01"; - tl1 = p.addTask(in, tl1); + String input = "event task2 /from 2024-01-31 /to 2024-02-01"; + Duke.tasks = tl1; + + p.parse(input); assertEquals(tl2, tl1); } @Test void emptyTodoShouldThrowEmptyArgumentException() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "todo "; - p.addTask(in, tl); - } catch (DukeEmptyArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "todo "; + + String reply = p.parse(input); + + assertEquals("There is an argument that is empty!!!", reply); } @Test void emptyDeadlineShouldThrowEmptyArgumentException() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "deadline /by 2024-01-01"; - p.addTask(in, tl); - } catch (DukeEmptyArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "deadline /by 2024-01-01"; + + String reply = p.parse(input); + + assertEquals("There is an argument that is empty!!!", reply); } @Test void emptyDeadlineDateTimeShouldThrowEmptyArgumentException() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "deadline test /by "; - p.addTask(in, tl); - } catch (DukeEmptyArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "deadline test /by "; + + String reply = p.parse(input); + + assertEquals("There is an argument that is empty!!!", reply); } @Test void erroneousDeadlineShouldThrowErroneousArgumentException() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "deadline test /by2024-01-01"; - p.addTask(in, tl); - } catch (DukeErroneousArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "deadline test /b 2024-01-01"; + + String reply = p.parse(input); + + assertEquals("There is an argument in the wrong format!!!", reply); } @Test void erroneousDeadLineFormatShouldThrowDateTimeException() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "deadline test /by 2024-0-01"; - p.addTask(in, tl); - } catch (DateTimeException e) { - thrown = true; - } - assertTrue(thrown); + String input = "deadline test /by 2024-0-01"; + + String reply = p.parse(input); + + String expected = "The format of your date is wrong! Make sure it is of the form 'yyyy-MM-dd'.\n" + + "More specifically: \n" + + "Text '2024-0-01' could not be parsed at index 5"; + + assertEquals(expected, reply); } @Test void emptyEventShouldThrowEmptyArgumentException() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "event /from 2024-01-01 /to 2024-01-02"; - p.addTask(in, tl); - } catch (DukeEmptyArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "event /from 2024-01-01 /to 2024-01-02"; + + String reply = p.parse(input); + + assertEquals("There is an argument that is empty!!!", reply); } @Test void emptyEventDateTimeShouldThrowEmptyArgumentException1() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "event test /from /to 2024-01-02"; - p.addTask(in, tl); - } catch (DukeEmptyArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "event test /from /to 2024-01-02"; + + String reply = p.parse(input); + + assertEquals("There is an argument that is empty!!!", reply); } @Test void emptyEventDateTimeShouldThrowEmptyArgumentException2() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "event test /from 2024-01-02 /to "; - p.addTask(in, tl); - } catch (DukeEmptyArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "event test /from 2024-01-02 /to "; + + String reply = p.parse(input); + + assertEquals("There is an argument that is empty!!!", reply); } @Test void erroneousEventShouldThrowErroneousArgumentException1() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "event test /from2024-01-01 /to 2024-01-02"; - p.addTask(in, tl); - } catch (DukeErroneousArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "event test /fro 2024-01-01 /to 2024-01-02"; + + String reply = p.parse(input); + + assertEquals("There is an argument in the wrong format!!!", reply); } @Test void erroneousEventShouldThrowErroneousArgumentException2() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "event test /from 2024-01-01 /to2024-01-02"; - p.addTask(in, tl); - } catch (DukeErroneousArgumentException e) { - thrown = true; - } - assertTrue(thrown); + String input = "event test /from 2024-01-01 /t 2024-01-02"; + + String reply = p.parse(input); + + assertEquals("There is an argument in the wrong format!!!", reply); } @Test void erroneousEventFormatShouldThrowDateTimeException1() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "event test /from 2024-00-01 /to 2024-01-02"; - p.addTask(in, tl); - } catch (DateTimeException e) { - thrown = true; - } - assertTrue(thrown); + String input = "event test /from 2024-00-01 /to 2024-01-02"; + + String reply = p.parse(input); + String expected = "The format of your date is wrong! Make sure it is of the form 'yyyy-MM-dd'.\n" + + "More specifically: \n" + + "Text '2024-00-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 0"; + + assertEquals(expected, reply); } @Test void erroneousEventFormatShouldThrowDateTimeException2() { - boolean thrown = false; - try { - TaskList tl = new TaskList(); - String in = "event test /from 2024-01-01 /to 2024-00-02"; - p.addTask(in, tl); - } catch (DateTimeException e) { - thrown = true; - } - assertTrue(thrown); + String input = "event test /from 2024-01-01 /to 2024-00-02"; + + String reply = p.parse(input); + String expected = "The format of your date is wrong! Make sure it is of the form 'yyyy-MM-dd'.\n" + + "More specifically: \n" + + "Text '2024-00-02' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 0"; + + assertEquals(expected, reply); } } diff --git a/src/test/tasks/TaskListTest.java b/src/test/tasks/TaskListTest.java index 106d3017cd..18d856cf8d 100644 --- a/src/test/tasks/TaskListTest.java +++ b/src/test/tasks/TaskListTest.java @@ -8,6 +8,7 @@ import java.util.Arrays; import java.util.List; +import duke.duke.Duke; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,11 +40,10 @@ void printListShouldPrintList() { Todo todo = new Todo("task1"); List lst = new ArrayList<>(List.of(todo)); TaskList tl = new TaskList(lst); - tl.printList(); - assertEquals("Here are the tasks in your list:" - + System.lineSeparator() + String output = tl.printList(); + assertEquals("Here are the tasks in your list:\n" + "1. " - + todo, outContent.toString()); + + todo, output); } @Test @@ -66,13 +66,14 @@ void findShouldFindTasksAndPrint() { Event event = new Event("task3", "2024-01-01", "2024-01-02"); List lst = new ArrayList<>(Arrays.asList(todo, deadline, event)); TaskList tl = new TaskList(lst); - tl.find("find task"); - assertEquals("Here are the matching tasks in your list:" - + System.lineSeparator() + Duke.tasks = tl; + + String output = tl.find("task"); + assertEquals("Here are the matching tasks in your list:\n" + "1. " + deadline + "2. " - + event, outContent.toString()); + + event, output); } }