Skip to content

Commit

Permalink
Merge pull request #30 from Chinorea/JUnit-Test
Browse files Browse the repository at this point in the history
Add JUnit test for DisplayExpense Feature
  • Loading branch information
Chinorea authored Oct 9, 2024
2 parents c702b91 + 4e848af commit 801219c
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.budgetbuddy.commands;

import seedu.budgetbuddy.Ui;
import seedu.budgetbuddy.transaction.expense.Category;
import seedu.budgetbuddy.transaction.expense.ExpenseManager;

Expand Down Expand Up @@ -56,6 +57,14 @@ public static boolean isCommand(String command) {
return command.startsWith("display expenses");
}

public Category getCategory() {
return category;
}

public YearMonth getMonth() {
return month;
}

/**
* Executes the command to list all expenses by invoking the ExpenseManager's method.
*/
Expand All @@ -64,11 +73,11 @@ public void execute() {
if (category == null && month == null) {
ExpenseManager.listExpenses();
} else if (category == null){
ExpenseManager.displayExpensesWithDate(month);
Ui.displayToUser(ExpenseManager.displayExpensesWithDate(month));
} else if (month == null) {
ExpenseManager.displayExpensesWithCategory(category);
Ui.displayToUser(ExpenseManager.displayExpensesWithCategory(category));
} else {
ExpenseManager.displayExpensesWithCategoryAndDate(category,month);
Ui.displayToUser(ExpenseManager.displayExpensesWithCategoryAndDate(category,month));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public static void listExpenses() {
* Displays each expense with its corresponding number.
* @param category
* @param month
* @return result String to be displayed to user
*/
public static void displayExpensesWithCategoryAndDate(Category category, YearMonth month) {
public static String displayExpensesWithCategoryAndDate(Category category, YearMonth month) {
String result = "";
int counter = 1;
for (Expense expense : expenses) {
Expand All @@ -88,15 +89,16 @@ public static void displayExpensesWithCategoryAndDate(Category category, YearMon
if(result.equals("")) {
result = getEmptyDisplayMessage();
}
Ui.displayToUser(result);
return result;
}

/**
* Display all expense that matches with category field
* Displays each expense with its corresponding number.
* @param category
* @return result String to be displayed to user
*/
public static void displayExpensesWithCategory(Category category) {
public static String displayExpensesWithCategory(Category category) {
String result = "";
int counter = 1;
for (Expense expense : expenses) {
Expand All @@ -108,15 +110,16 @@ public static void displayExpensesWithCategory(Category category) {
if(result.equals("")) {
result = getEmptyDisplayMessage();
}
Ui.displayToUser(result);
return result;
}

/**
* Display all expense that matches with month field
* Displays each expense with its corresponding number.
* @param month
* @return result String to be displayed to user
*/
public static void displayExpensesWithDate(YearMonth month) {
public static String displayExpensesWithDate(YearMonth month) {
String result = "";
int counter = 1;
for (Expense expense : expenses) {
Expand All @@ -128,7 +131,7 @@ public static void displayExpensesWithDate(YearMonth month) {
if(result.equals("")) {
result = getEmptyDisplayMessage();
}
Ui.displayToUser(result);
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package seedu.budgetbuddy.transaction.expense;

import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;

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

class ExpenseManagerTest {

private static int numberOfExpenses = 0;
private static ArrayList<Expense> expenses = new ArrayList<>();
private static ExpenseManager expenseManager;
private static final String EMPTY_DISPLAY_STRING =
"No expense entry with given parameters found, try again with a different parameter.";

void initializeTestContent(){
expenseManager = new ExpenseManager(expenses, numberOfExpenses);
Expense newExpense = new Expense("New Food",
12,
LocalDate.parse("2024-02-12"),
Category.FOOD);
expenseManager.addExpense(newExpense);
}

String getExpectedString(){
String result = "";
int counter = 1;
for (Expense expense : ExpenseManager.getExpenses()) {
result += counter + ". " + expense.toString() + "\n";
counter++;
}
return result;
}

@Test
void displayExpensesWithCategoryAndDate_validMonthCategory_expectOneEntry() {
initializeTestContent();
Category category = Category.FOOD;
YearMonth yearMonth = YearMonth.of(2024, 2);
assertEquals(getExpectedString(),
ExpenseManager.displayExpensesWithCategoryAndDate(category, yearMonth));
}

@Test
void displayExpensesWithCategoryAndDate_incorrectCategory_expectEmptyDisplayString() {
Category category = Category.TRANSPORT;
YearMonth yearMonth = YearMonth.of(2024, 2);
assertEquals(EMPTY_DISPLAY_STRING,
ExpenseManager.displayExpensesWithCategoryAndDate(category, yearMonth));
}

@Test
void displayExpensesWithCategoryAndDate_incorrectMonth_expectEmptyDisplayString() {
Category category = Category.FOOD;
YearMonth yearMonth = YearMonth.of(2024, 1);
assertEquals(EMPTY_DISPLAY_STRING,
ExpenseManager.displayExpensesWithCategoryAndDate(category, yearMonth));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.budgetbuddy.validators;

import org.junit.jupiter.api.Test;
import seedu.budgetbuddy.commands.DisplayExpenseCommand;
import seedu.budgetbuddy.transaction.expense.Category;
import java.time.YearMonth;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.budgetbuddy.validators.DateValidator.validateYearMonth;

class DisplayExpenseValidatorTest {

@Test
void checkDisplayType_categoryIsEmpty_expectNullCategory(){
DisplayExpenseValidator expenseValidator = new DisplayExpenseValidator();
Category category = null;
YearMonth yearMonth = validateYearMonth("02/2024");
DisplayExpenseCommand command = (DisplayExpenseCommand) expenseValidator.checkDisplayType(category, yearMonth);
assertEquals(null, command.getCategory());
assertEquals(yearMonth,command.getMonth());
}

@Test
void checkDisplayType_monthIsEmpty_expectNullMonth(){
DisplayExpenseValidator expenseValidator = new DisplayExpenseValidator();
Category category = Category.FOOD;
YearMonth yearMonth = null;
DisplayExpenseCommand command = (DisplayExpenseCommand) expenseValidator.checkDisplayType(category, yearMonth);
assertEquals(category, command.getCategory());
assertEquals(yearMonth,command.getMonth());
}

@Test
void checkDisplayType_categoryMonthSpecified_expectSameValues(){
DisplayExpenseValidator expenseValidator = new DisplayExpenseValidator();
Category category = Category.FOOD;
YearMonth yearMonth = validateYearMonth("02/2024");
DisplayExpenseCommand command = (DisplayExpenseCommand) expenseValidator.checkDisplayType(category, yearMonth);
assertEquals(category, command.getCategory());
assertEquals(yearMonth,command.getMonth());
}
}

0 comments on commit 801219c

Please sign in to comment.