Skip to content

Commit

Permalink
Merge pull request #5 from TyrusLye/A-Assertions
Browse files Browse the repository at this point in the history
A-Assertions
  • Loading branch information
TyrusLye committed Oct 23, 2023
2 parents 3c69f79 + 0efd428 commit 70cbc5f
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/URBOI_PACKIN/TaskTypes/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
72 changes: 72 additions & 0 deletions src/test/java/Storage/StorageTest.java
Original file line number Diff line number Diff line change
@@ -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<Task> 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<Task> 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());
}
}
39 changes: 39 additions & 0 deletions src/test/java/TaskTypes/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
35 changes: 35 additions & 0 deletions src/test/java/TaskTypes/EventTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
33 changes: 33 additions & 0 deletions src/test/java/TaskTypes/ToDoTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
7 changes: 3 additions & 4 deletions tasks.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 70cbc5f

Please sign in to comment.