Skip to content

Commit

Permalink
Merge pull request #25 from lckjosh/branch-add-transaction-test
Browse files Browse the repository at this point in the history
Add JUnit testing for adding a transaction
  • Loading branch information
spaceman03 authored Oct 17, 2023
2 parents 5b42ba6 + cf2a863 commit 61f4961
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/main/java/seedu/nuscents/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import static seedu.nuscents.commands.ListOfCommands.COMMAND_DELETE;
import static seedu.nuscents.commands.ListOfCommands.COMMAND_FIND;
import static seedu.nuscents.commands.ListOfCommands.COMMAND_HELP;

import static seedu.nuscents.ui.Messages.MESSAGE_EMPTY_ALLOWANCE;
import static seedu.nuscents.ui.Messages.MESSAGE_EMPTY_EXPENSE;
import static seedu.nuscents.ui.Messages.MESSAGE_EMPTY_INDEX;
import static seedu.nuscents.ui.Messages.MESSAGE_EMPTY_KEYWORD;
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;

Expand Down Expand Up @@ -112,10 +112,10 @@ public static Allowance parseAllowance(String arguments) throws NuscentsExceptio
if (arguments == null) {
throw new NuscentsException(MESSAGE_EMPTY_ALLOWANCE);
} else {
String amount = extractValue(arguments, AMT_PATTERN, false);
String date = extractValue(arguments, DATE_PATTERN, false);
String description = extractValue(arguments, DESC_PATTERN, false);
String additionalInformation = extractValue(arguments, NOTE_PATTERN, true);
String amount = extractValue("allowance", arguments, AMT_PATTERN, false);
String date = extractValue("allowance", arguments, DATE_PATTERN, false);
String description = extractValue("allowance", arguments, DESC_PATTERN, false);
String additionalInformation = extractValue("allowance", arguments, NOTE_PATTERN, true);
String format = datePatternValidation(date);
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date formattedDate = parseDate(date, format, formatter);
Expand All @@ -134,10 +134,10 @@ public static Expense parseExpense(String arguments) throws NuscentsException, P
if (arguments == null) {
throw new NuscentsException(MESSAGE_EMPTY_EXPENSE);
} else {
String amount = extractValue(arguments, AMT_PATTERN, false);
String date = extractValue(arguments, DATE_PATTERN, false);
String description = extractValue(arguments, DESC_PATTERN, false);
String additionalInformation = extractValue(arguments, NOTE_PATTERN, true);
String amount = extractValue("expense", arguments, AMT_PATTERN, false);
String date = extractValue("expense", arguments, DATE_PATTERN, false);
String description = extractValue("expense", arguments, DESC_PATTERN, false);
String additionalInformation = extractValue("expense", arguments, NOTE_PATTERN, true);
String format = datePatternValidation(date);
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date formattedDate = parseDate(date, format, formatter);
Expand Down Expand Up @@ -165,14 +165,22 @@ public static String parseFind(String arguments) throws NuscentsException {
}
}

private static String extractValue(String input, String pattern, boolean isOptional) throws NuscentsException {
private static String extractValue(String command, String input, String pattern, boolean isOptional)
throws NuscentsException {
java.util.regex.Pattern p = java.util.regex.Pattern.compile(pattern);
java.util.regex.Matcher m = p.matcher(input);

if (m.find()) {
return m.group(1).trim();
} else if (!isOptional) {
throw new NuscentsException(MESSAGE_EMPTY_ALLOWANCE);
switch (command) {
case "allowance":
throw new NuscentsException(MESSAGE_EMPTY_ALLOWANCE);
case "expense":
throw new NuscentsException(MESSAGE_EMPTY_EXPENSE);
default:
throw new NuscentsException(MESSAGE_FATAL_ERROR);
}
}
return "";
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/nuscents/ui/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ public class Messages {
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_FATAL_ERROR = "OOPS!!! Fatal error occurred...";
public static final String MESSAGE_READ_ERROR = "OOPS!!! A problem occurred while reading the data file.";
}
76 changes: 76 additions & 0 deletions src/test/java/seedu/nuscents/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package seedu.nuscents.parser;

import org.junit.jupiter.api.Test;
import seedu.nuscents.data.Allowance;
import seedu.nuscents.data.Expense;
import seedu.nuscents.data.exception.NuscentsException;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ParserTest {

@Test
public void parseAllowance_validInput_success() throws NuscentsException, ParseException {
String arguments = "/amt 100 /date 15-10-2023 /desc Birthday Gift /note From friends";
Allowance allowance = Parser.parseAllowance(arguments);
assertEquals("100", allowance.getAmount());
SimpleDateFormat formatter = new SimpleDateFormat("d-M-yyyy");
String formattedDate = formatter.format(allowance.getDate());
assertEquals("15-10-2023", formattedDate);
assertEquals("Birthday Gift", allowance.getDescription());
assertEquals("From friends", allowance.getAdditionalInfo());
}

@Test
public void parseExpense_validInput_success() throws NuscentsException, ParseException {
String arguments = "/amt 50 /date 16-10-2023 /desc Dinner /note Alone";
Expense expense = Parser.parseExpense(arguments);
assertEquals("50", expense.getAmount());
SimpleDateFormat formatter = new SimpleDateFormat("d-M-yyyy");
String formattedDate = formatter.format(expense.getDate());
assertEquals("16-10-2023", formattedDate);
assertEquals("Dinner", expense.getDescription());
assertEquals("Alone", expense.getAdditionalInfo());
}


@Test
public void parseAllowance_missingArguments_exceptionThrown() {
String input = "allowance /amt 100 /desc Pocket Money";
Exception exception = assertThrows(NuscentsException.class, () -> {
Parser.parseCommand(input, null);
});
assertEquals("OOPS!!! Invalid input format for adding an allowance.", exception.getMessage());
}

@Test
public void parseExpense_missingArguments_exceptionThrown() {
String input = "expense /amt 50 /desc Dinner";
Exception exception = assertThrows(NuscentsException.class, () -> {
Parser.parseCommand(input, null);
});
assertEquals("OOPS!!! Invalid input format for adding an expense.", exception.getMessage());
}

@Test
public void parseAllowance_invalidDateTime_exceptionThrown() {
String input = "allowance /amt 100 /date 15/10/2023 /desc Lunch";
Exception exception = assertThrows(NuscentsException.class, () -> {
Parser.parseCommand(input, null);
});
assertEquals("OOPS!!! The format of the date is invalid.", exception.getMessage());
}

@Test
public void parseExpense_invalidDateTime_exceptionThrown() {
String input = "expense /amt 50 /date 16.10.2023 /desc Dinner";
Exception exception = assertThrows(NuscentsException.class, () -> {
Parser.parseCommand(input, null);
});
assertEquals("OOPS!!! The format of the date is invalid.", exception.getMessage());
}
}
2 changes: 1 addition & 1 deletion text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ OOPS!!! I'm sorry, but I don't know what that means :-(
OOPS!!! Invalid input format for adding an allowance.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
OOPS!!! Invalid input format for adding an allowance.
OOPS!!! Invalid input format for adding an expense.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Thank you for using NUScents. Hope to see you again soon!
Expand Down

0 comments on commit 61f4961

Please sign in to comment.