Skip to content

Commit

Permalink
Update Test Cases for Parser class
Browse files Browse the repository at this point in the history
  • Loading branch information
MAOXIONGKAI committed Sep 18, 2024
1 parent 84b000e commit a8ad60a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/gopher/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public static String[] parseUpdateEventTaskCommand(String[] tokens)
* @throws FileCorruptedException if file read failed
*/
public static ArrayList<Task> parseSavedTaskData(String taskData)
throws FileCorruptedException, InvalidTokenException {
throws FileCorruptedException {
ArrayList<Task> tasks = new ArrayList<>();
String[] taskRows = taskData.split("\n");
String[] tokens;
Expand Down Expand Up @@ -456,6 +456,7 @@ public static ArrayList<Task> parseSavedTaskData(String taskData)
} catch (UnknownCommandException
| EmptyTaskDescriptionException
| MissingTokenException
| InvalidTokenException
| InvalidDurationException e) {
throw new FileCorruptedException();
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/gopher/storage/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public static ArrayList<Task> loadTasks() {
: "Task save file should exist when loading task";
String taskString = Files.readString(TASK_FILE);
return Parser.parseSavedTaskData(taskString);
} catch (IOException | InvalidTokenException
| ArrayIndexOutOfBoundsException e) {
} catch (IOException | ArrayIndexOutOfBoundsException e) {
throw new FileCorruptedException();
}
}
Expand Down
105 changes: 105 additions & 0 deletions src/test/java/gopher/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

import gopher.exception.FileCorruptedException;
import org.junit.jupiter.api.Test;

import gopher.exception.InvalidTokenException;
import gopher.exception.MissingTaskNumberException;

public class ParserTest {
Expand Down Expand Up @@ -108,4 +110,107 @@ public void parseDeleteCommand_multipleTaskNumber_parseSuccess()
throws MissingTaskNumberException {
assertArrayEquals(new int[]{1, 2, 3}, Parser.parseDeleteCommand("delete 1 2 3"));
}

@Test
public void parseUpdateTodoCommand_validCommand_parseSuccess()
throws InvalidTokenException {
String[] tokens = new String[]{"update", "2", "Hello", "World"};
assertArrayEquals(new String[]{"Hello World"}, Parser.parseUpdateTodoTaskCommand(tokens));
}

@Test
public void parseUpdateTodoCommand_inputWithByToken_exceptionThrown() {
String[] tokens = new String[]{"update", "4", "Test", "/by", "2024-09-11"};
assertThrows(InvalidTokenException.class, () -> {
Parser.parseUpdateTodoTaskCommand(tokens);
});
}

@Test
public void parseUpdateDeadlineCommand_validCommand_parseSuccess()
throws InvalidTokenException {
String[] tokens = new String[]{"update", "3", "/by", "2024-09-11"};
assertArrayEquals(new String[]{"", "2024-09-11"},
Parser.parseUpdateDeadlineTaskCommand(tokens));
}

@Test
public void parseUpdateDeadlineCommand_inputWithToToken_exceptionThrown() {
assertThrows(InvalidTokenException.class, () -> {
String[] tokens = new String[]{"update", "3", "/to", "2024"};
Parser.parseUpdateDeadlineTaskCommand(tokens);
});
}

@Test
public void parseUpdateEventCommand_validCommand_parseSuccess()
throws InvalidTokenException {
String[] tokens = new String[]{"update", "5", "Event", "1", "/from",
"2024-09-11", "15:00", "/to", "2024-09-12", "16:00"};
assertArrayEquals(new String[]{"Event 1", "2024-09-11 15:00", "2024-09-12 16:00"},
Parser.parseUpdateEventTaskCommand(tokens));
}

@Test
public void parseUpdateEventCommand_inputWithByToken_exceptionThrown() {
assertThrows(InvalidTokenException.class, () -> {
String[] tokens = new String[]{"update", "5", "Event", "1", "/from",
"2024-09-11", "15:00", "/to", "2024-09-12",
"16:00", "/by", "2024-09-15"};
Parser.parseUpdateEventTaskCommand(tokens);
});
}

@Test
public void parseSavedTaskData_corruptedFile_exceptionThrown() {
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
Parser.parseSavedTaskData("""
Hello World
""");
});
}

@Test
public void parseMarkCommand_validCommand_parseSuccess()
throws MissingTaskNumberException {
assertArrayEquals(new int[]{1, 3, 5}, Parser.parseMarkCommand("mark 1 3 5"));
}

@Test
public void parseMarkCommand_missingTaskNumber_exceptionThrown() {
assertThrows(MissingTaskNumberException.class, () -> {
Parser.parseMarkCommand("mark");
});
}

@Test
public void parseUnmarkCommand_validCommand_parseSuccess()
throws MissingTaskNumberException {
assertArrayEquals(new int[]{2, 4, 6}, Parser.parseMarkCommand("unmark 2 4 6"));
}

@Test
public void parseUnmarkCommand_missingTaskNumber_exceptionThrown() {
assertThrows(MissingTaskNumberException.class, () -> {
Parser.parseMarkCommand("unmark");
});
}

@Test
public void parseDeleteCommand_validCommand_parseSuccess()
throws MissingTaskNumberException {
assertArrayEquals(new int[]{1, 2, 3}, Parser.parseMarkCommand("delete 1 2 3"));
}

@Test
public void parseDeleteCommand_missingTaskNumber_exceptionThrown() {
assertThrows(MissingTaskNumberException.class, () -> {
Parser.parseMarkCommand("delete");
});
}

@Test
public void parseFindCommand_validCommand_parseSuccess() {
assertEquals("Hello World 123", Parser.parseFindCommand("find Hello World 123"));
}
}

0 comments on commit a8ad60a

Please sign in to comment.