forked from nus-cs2103-AY2324S1/ip
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package duke; | ||
|
||
import duke.command.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ParserTest { | ||
|
||
@Test | ||
public void testParseByeCommand() { | ||
try { | ||
Command command = Parser.parse("bye"); | ||
assertTrue(command instanceof ExitCommand); | ||
} catch (DukeException e) { | ||
fail("Exception should not be thrown for 'bye' command."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseListCommand() { | ||
try { | ||
Command command = Parser.parse("list"); | ||
assertTrue(command instanceof ListCommand); | ||
} catch (DukeException e) { | ||
fail("Exception should not be thrown for 'list' command."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseUnknownCommand() { | ||
try { | ||
Command command = Parser.parse("invalidcommand"); | ||
fail("Exception should be thrown for unknown command."); | ||
} catch (DukeException e) { | ||
assertEquals(":( OOPS!!! I'm sorry, but I don't know what that means :-(", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseDeadlineCommand() { | ||
try { | ||
Command command = Parser.parse("deadline Submit report /by 2023-08-31 23:59"); | ||
assertTrue(command instanceof AddCommand); | ||
} catch (DukeException e) { | ||
fail("Exception should not be thrown for valid 'deadline' command."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseEventCommand() { | ||
try { | ||
Command command = Parser.parse("event Team meeting /from 2023-09-01 14:00 /to 2023-09-01 15:30"); | ||
assertTrue(command instanceof AddCommand); | ||
} catch (DukeException e) { | ||
fail("Exception should not be thrown for valid 'event' command."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseMarkCommand() { | ||
try { | ||
Command command = Parser.parse("mark 2"); | ||
assertTrue(command instanceof MarkCommand); | ||
} catch (DukeException e) { | ||
fail("Exception should not be thrown for valid 'mark' command."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseUnmarkCommand() { | ||
try { | ||
Command command = Parser.parse("unmark 3"); | ||
assertTrue(command instanceof UnmarkCommand); | ||
} catch (DukeException e) { | ||
fail("Exception should not be thrown for valid 'unmark' command."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseDeleteCommand() { | ||
try { | ||
Command command = Parser.parse("delete 1"); | ||
assertTrue(command instanceof DeleteCommand); | ||
} catch (DukeException e) { | ||
fail("Exception should not be thrown for valid 'delete' command."); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseInvalidTodoCommand() { | ||
try { | ||
// No description provided for todo task | ||
Parser.parse("todo"); | ||
fail("Exception should be thrown for empty 'todo' description."); | ||
} catch (DukeException e) { | ||
assertEquals(":( OOPS!!! The description of a todo cannot be empty.", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseInvalidDeadlineCommand() { | ||
try { | ||
// Missing '/by' | ||
Parser.parse("deadline Submit report 2023-08-31 23:59"); | ||
fail("Exception should be thrown for missing '/by' in 'deadline' command."); | ||
} catch (DukeException e) { | ||
assertEquals(":( OOPS!!! Deadlines must have a /by.", e.getMessage()); | ||
} | ||
|
||
try { | ||
// Empty '/by' | ||
Parser.parse("deadline Submit report /by"); | ||
fail("Exception should be thrown for empty '/by' in 'deadline' command."); | ||
} catch (DukeException e) { | ||
assertEquals(":( OOPS!!! Deadlines must have a /by.", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testParseInvalidEventCommand() { | ||
try { | ||
// Missing '/from' and '/to' | ||
Parser.parse("event Team meeting"); | ||
fail("Exception should be thrown for missing '/from' and '/to' in 'event' command."); | ||
} catch (DukeException e) { | ||
assertEquals(":( OOPS!!! Events must have a /from and /to.", e.getMessage()); | ||
} | ||
|
||
try { | ||
// Empty '/from' or '/to' | ||
Parser.parse("event Team meeting /from /to 2023-09-01 15:30"); | ||
fail("Exception should be thrown for empty '/from' in 'event' command."); | ||
} catch (DukeException e) { | ||
assertEquals(":( OOPS!!! Events must have a /from and /to.", e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package duke; | ||
|
||
import duke.TaskList; | ||
import duke.Ui; | ||
import duke.task.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import java.io.*; | ||
import java.util.ArrayList; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class StorageTest { | ||
private static final String TEST_FOLDER = "test_folder"; | ||
private static final String TEST_FILE = "test_file.txt"; | ||
|
||
private Storage storage; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
storage = new Storage(TEST_FOLDER, TEST_FILE); | ||
} | ||
|
||
@Test | ||
public void testLoadFile() { | ||
try { | ||
ArrayList<String> testData = new ArrayList<>(); | ||
testData.add("Test line 1"); | ||
testData.add("Test line 2"); | ||
|
||
// Create a test file with some data | ||
File directory = new File(TEST_FOLDER); | ||
directory.mkdirs(); | ||
File file = new File(directory, TEST_FILE); | ||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { | ||
for (String line : testData) { | ||
writer.write(line); | ||
writer.newLine(); | ||
} | ||
} | ||
|
||
ArrayList<String> loadedData = storage.loadFile(); | ||
|
||
assertIterableEquals(testData, loadedData); | ||
} catch (Exception e) { | ||
fail("Exception thrown: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSaveFile() { | ||
try { | ||
TaskList taskList = new TaskList(); | ||
Ui ui = new Ui(); | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); | ||
LocalDateTime time = LocalDateTime.parse("2023-08-25 12:30", formatter); | ||
|
||
ArrayList<Task> tasks = new ArrayList<>(); | ||
tasks.add(new ToDo("read book")); | ||
tasks.add(new Deadline("return book", time)); | ||
tasks.add(new Event("book event", time, time)); | ||
|
||
for (Task task : tasks) { | ||
taskList.addTask(task, ui); | ||
} | ||
|
||
storage.saveFile(taskList); | ||
|
||
// Check if the file was saved correctly | ||
File directory = new File(TEST_FOLDER); | ||
File file = new File(directory, TEST_FILE); | ||
|
||
assertTrue(file.exists()); | ||
|
||
ArrayList<String> loadedData = new ArrayList<>(); | ||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
loadedData.add(line); | ||
} | ||
} | ||
|
||
ArrayList<String> savedData = new ArrayList<>(); | ||
for (Task task : tasks) { | ||
savedData.add(task.toSaveLine()); | ||
} | ||
|
||
assertIterableEquals(savedData, loadedData); | ||
} catch (Exception e) { | ||
fail("Exception thrown: " + e.getMessage()); | ||
} | ||
} | ||
} |