From 0efd428d4a1f349c9fb036f68fcdbd98e3cf78ee Mon Sep 17 00:00:00 2001 From: TyrusLye Date: Mon, 23 Oct 2023 23:43:11 +0800 Subject: [PATCH] A-Assertions --- build.gradle | 1 + .../java/URBOI_PACKIN/TaskTypes/Event.java | 8 +++ src/test/java/Storage/StorageTest.java | 72 +++++++++++++++++++ src/test/java/TaskTypes/DeadlineTest.java | 39 ++++++++++ src/test/java/TaskTypes/EventTest.java | 35 +++++++++ src/test/java/TaskTypes/ToDoTest.java | 33 +++++++++ tasks.txt | 7 +- 7 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 src/test/java/Storage/StorageTest.java create mode 100644 src/test/java/TaskTypes/DeadlineTest.java create mode 100644 src/test/java/TaskTypes/EventTest.java create mode 100644 src/test/java/TaskTypes/ToDoTest.java diff --git a/build.gradle b/build.gradle index ba99492fd9..2e4d155f90 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,7 @@ dependencies { implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'win' implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac' implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux' + } test { diff --git a/src/main/java/URBOI_PACKIN/TaskTypes/Event.java b/src/main/java/URBOI_PACKIN/TaskTypes/Event.java index f635b5fdfd..c5efbf13da 100644 --- a/src/main/java/URBOI_PACKIN/TaskTypes/Event.java +++ b/src/main/java/URBOI_PACKIN/TaskTypes/Event.java @@ -24,4 +24,12 @@ public String toFileString() { public String toString() { return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; } + + public String getFrom() { + return from; + } + + public String getTo() { + return to; + } } diff --git a/src/test/java/Storage/StorageTest.java b/src/test/java/Storage/StorageTest.java new file mode 100644 index 0000000000..02f5d53b52 --- /dev/null +++ b/src/test/java/Storage/StorageTest.java @@ -0,0 +1,72 @@ +package Storage; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import URBOI_PACKIN.Storage.Storage; +import URBOI_PACKIN.Task; +import URBOI_PACKIN.TaskTypes.Deadline; +import URBOI_PACKIN.TaskTypes.Todo; +import URBOI_PACKIN.TaskTypes.Event; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class StorageTest { + + private static final String TEST_FILE_PATH = "test_tasks.txt"; + + @BeforeEach + public void setUp() { + // Delete the test file if it exists before each test + File file = new File(TEST_FILE_PATH); + if (file.exists()) { + file.delete(); + } + } + + @Test + public void testSaveAndLoadTasks() { + // Create some test tasks + ArrayList tasksToSave = new ArrayList<>(); + tasksToSave.add(new Todo("Test Todo Task")); + tasksToSave.add(new Deadline("Test Deadline Task", LocalDateTime.now())); + tasksToSave.add(new Event("Test Event Task", "2023-10-23 10:00", "2023-10-23 12:00")); + + // Save tasks to the test file + Storage.saveTasksToFile(tasksToSave); + + // Load tasks from the test file + ArrayList loadedTasks = new ArrayList<>(); + Storage.loadTasksFromFile(loadedTasks); + + // Assert that the loaded tasks match the saved tasks + assertEquals(tasksToSave.size(), loadedTasks.size()); + for (int i = 0; i < tasksToSave.size(); i++) { + assertEquals(tasksToSave.get(i).toFileString(), loadedTasks.get(i).toFileString()); + } + } + + + @Test + public void testCreateTaskFromLine() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + String line = "T | 1 | Test Todo Task"; + + // Use reflection to access the private method + Method createTaskFromLine = Storage.class.getDeclaredMethod("createTaskFromLine", String.class); + createTaskFromLine.setAccessible(true); + + // Call the private method to create a task + Task task = (Task) createTaskFromLine.invoke(null, line); + + assertTrue(task instanceof Todo); + assertEquals("Test Todo Task", task.getDescription()); + assertEquals(true, task.isDone()); + } +} diff --git a/src/test/java/TaskTypes/DeadlineTest.java b/src/test/java/TaskTypes/DeadlineTest.java new file mode 100644 index 0000000000..d16da2d829 --- /dev/null +++ b/src/test/java/TaskTypes/DeadlineTest.java @@ -0,0 +1,39 @@ +package TaskTypes; +import URBOI_PACKIN.TaskTypes.Deadline; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DeadlineTest { + private Deadline deadline; + + @BeforeEach + public void setUp() { + // Initialize a Deadline object before each test + LocalDateTime date = LocalDateTime.parse("2023-10-23T14:30", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm")); + deadline = new Deadline("Sample Deadline", date); + } + + @Test + public void testDeadlineConstructor() { + // Check if the Deadline constructor sets the description and date correctly + assertEquals("Sample Deadline", deadline.getDescription()); + //assertEquals(LocalDateTime.parse("2023-10-23T14:30", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm")), deadline.getDate()); + } + + @Test + public void testDeadlineToFileString() { + // Check the toFileString method for a Deadline task + assertEquals("D | 0 | Sample Deadline | 2023-10-23T14:30", deadline.toFileString()); + } + + @Test + public void testDeadlineToString() { + // Check the toString method for a Deadline task + assertEquals("[D][ ] Sample Deadline (by: 2023-10-23T14:30)", deadline.toString()); + } +} diff --git a/src/test/java/TaskTypes/EventTest.java b/src/test/java/TaskTypes/EventTest.java new file mode 100644 index 0000000000..ef0eb16343 --- /dev/null +++ b/src/test/java/TaskTypes/EventTest.java @@ -0,0 +1,35 @@ +package TaskTypes; +import URBOI_PACKIN.TaskTypes.Event; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +public class EventTest { + private Event event; + + @BeforeEach + public void setUp() { + // Initialize an Event object before each test + event = new Event("Sample Event", "2023-10-23 1400", "2023-10-23 1600"); + } + + @Test + public void testEventConstructor() { + // Check if the Event constructor sets the description, from, and to correctly + assertEquals("Sample Event", event.getDescription()); + assertEquals("2023-10-23 1400", event.getFrom()); + assertEquals("2023-10-23 1600", event.getTo()); + } + + @Test + public void testEventToFileString() { + // Check the toFileString method for an Event task + assertEquals("E | 0 | Sample Event | 2023-10-23 1400 | 2023-10-23 1600", event.toFileString()); + } + + @Test + public void testEventToString() { + // Check the toString method for an Event task + assertEquals("[E][ ] Sample Event (from: 2023-10-23 1400 to: 2023-10-23 1600)", event.toString()); + } +} diff --git a/src/test/java/TaskTypes/ToDoTest.java b/src/test/java/TaskTypes/ToDoTest.java new file mode 100644 index 0000000000..c525c0bab9 --- /dev/null +++ b/src/test/java/TaskTypes/ToDoTest.java @@ -0,0 +1,33 @@ +package TaskTypes; +import URBOI_PACKIN.TaskTypes.Todo; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +public class ToDoTest { + private Todo todo; + + @BeforeEach + public void setUp() { + // Initialize a Todo object before each test + todo = new Todo("Sample Todo"); + } + + @Test + public void testTodoConstructor() { + // Check if the Todo constructor sets the description correctly + assertEquals("Sample Todo", todo.getDescription()); + } + + @Test + public void testTodoToFileString() { + // Check the toFileString method for a Todo task + assertEquals("T | 0 | Sample Todo", todo.toFileString()); + } + + @Test + public void testTodoToString() { + // Check the toString method for a Todo task + assertEquals("[T][ ] Sample Todo", todo.toString()); + } +} diff --git a/tasks.txt b/tasks.txt index 6f003db8ce..0ff5103168 100644 --- a/tasks.txt +++ b/tasks.txt @@ -1,4 +1,3 @@ -T | 0 | thing1 -T | 1 | thing2 -T | 0 | thing3 -D | 0 | task | 2023-10-23T14:30 +T | 0 | Test Todo Task +D | 0 | Test Deadline Task | 2023-10-23T23:42:41.212928 +E | 0 | Test Event Task | 2023-10-23 10:00 | 2023-10-23 12:00