Skip to content

Commit

Permalink
Merge pull request #31 from vvhuiling/JUnit
Browse files Browse the repository at this point in the history
Add JUnit for Parse.parseTaskIndex
  • Loading branch information
spaceman03 authored Oct 18, 2023
2 parents 25ef375 + 85118bd commit ddcd241
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/main/java/seedu/nuscents/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static seedu.nuscents.ui.Messages.MESSAGE_FATAL_ERROR;
import static seedu.nuscents.ui.Messages.MESSAGE_INVALID_DATE;
import static seedu.nuscents.ui.Messages.MESSAGE_INVALID_INDEX;
import static seedu.nuscents.ui.Messages.MESSAGE_INVALID_INDEX_ARGUMENTS;

public class Parser {
private static final String DATE_PATTERN1 = "\\d{1,2}-\\d{1,2}-\\d{4}"; // dd-mm-yyyy
Expand Down Expand Up @@ -157,11 +158,15 @@ public static int parseTaskIndex(String arguments) throws IndexOutOfBoundsExcept
if (arguments == null) {
throw new NuscentsException(MESSAGE_EMPTY_INDEX);
}
int taskIndex = Integer.parseInt(arguments);
if (taskIndex > Transaction.getTransactionCount() || taskIndex <= 0) {
throw new IndexOutOfBoundsException(MESSAGE_INVALID_INDEX);
try {
int taskIndex = Integer.parseInt(arguments);
if (taskIndex > Transaction.getTransactionCount() || taskIndex <= 0) {
throw new IndexOutOfBoundsException(MESSAGE_INVALID_INDEX);
}
return taskIndex;
} catch (NumberFormatException e) {
throw new NuscentsException(MESSAGE_INVALID_INDEX_ARGUMENTS);
}
return taskIndex;
}

public static String parseFind(String arguments) throws NuscentsException {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/nuscents/ui/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public class Messages {
public static final String MESSAGE_EMPTY_TO = "OOPS!!! The /to parameter of an event cannot be empty.";
public static final String MESSAGE_INVALID_DATE = "OOPS!!! The format of the date is invalid.";
public static final String MESSAGE_INVALID_INDEX = "OOPS!!! This is an invalid transaction index.";
public static final String MESSAGE_INVALID_INDEX_ARGUMENTS = "OOPS!!! The input should consist only of digits.";
public static final String MESSAGE_EMPTY_LIST = "You have not made any transactions!";
public static final String MESSAGE_EMPTY_KEYWORD = "OOPS!!! The keyword of a find command cannot be empty.";
public static final String MESSAGE_EMPTY_INDEX = "OOPS!!! The index of a mark/unmark command cannot be empty.";
public static final String MESSAGE_EMPTY_INDEX = "OOPS!!! The index of a delete/view command cannot be empty.";
public static final String MESSAGE_FATAL_ERROR = "OOPS!!! Fatal error occurred...";
public static final String MESSAGE_READ_ERROR = "OOPS!!! A problem occurred while reading the data file.";
}
35 changes: 35 additions & 0 deletions src/test/java/seedu/nuscents/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.nuscents.parser;

import org.junit.jupiter.api.Test;
import seedu.nuscents.data.Transaction;
import seedu.nuscents.data.Allowance;
import seedu.nuscents.data.Expense;
import seedu.nuscents.data.exception.NuscentsException;
Expand Down Expand Up @@ -77,6 +78,40 @@ public void parseExpense_invalidDateTime_exceptionThrown() {
assertEquals("OOPS!!! The format of the date is invalid.", exception.getMessage());
}

@Test
public void parseTaskIndex_validInput_success() throws NuscentsException, IndexOutOfBoundsException {
String input = "1";
Transaction transaction = new Transaction("test description");
assertEquals(1, Parser.parseTaskIndex(input));
}

@Test
public void parseTaskIndex_outOfBoundsIndex_exceptionThrown() {
String input = "-6";
Exception exception = assertThrows(IndexOutOfBoundsException.class, () -> {
Parser.parseTaskIndex(input);
});
assertEquals("OOPS!!! This is an invalid transaction index.", exception.getMessage());
}

@Test
public void parseTaskIndex_invalidArgument_exceptionThrown() {
String input = "InvalidArgument";
Exception exception = assertThrows(NuscentsException.class, () -> {
Parser.parseTaskIndex(input);
});
assertEquals("OOPS!!! The input should consist only of digits.", exception.getMessage());
}

@Test
public void parseTaskIndex_emptyArgument_exceptionThrown() {
String input = null;
Exception exception = assertThrows(NuscentsException.class, () -> {
Parser.parseTaskIndex(input);
});
assertEquals("OOPS!!! The index of a delete/view command cannot be empty.", exception.getMessage());
}

@Test
public void parseCommand_helpCommandWithCorrectInput_returnsHelpCommand() throws Exception {
Command result = Parser.parseCommand("help", null);
Expand Down

0 comments on commit ddcd241

Please sign in to comment.