forked from nus-cs2113-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Chinorea/JUnit-Test
Add JUnit test for DisplayExpense Feature
- Loading branch information
Showing
4 changed files
with
125 additions
and
9 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
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
62 changes: 62 additions & 0 deletions
62
src/test/java/seedu/budgetbuddy/transaction/expense/ExpenseManagerTest.java
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,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)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/seedu/budgetbuddy/validators/DisplayExpenseValidatorTest.java
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,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()); | ||
} | ||
} |