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 #34 from Alfred-Goh02/branch-J-Unit
Add JUnit test Validator and Manager
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 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
74 changes: 74 additions & 0 deletions
74
src/test/java/seedu/budgetbuddy/transaction/income/IncomeManagerTest.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,74 @@ | ||
package seedu.budgetbuddy.transaction.income; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
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; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class IncomeManagerTest { | ||
|
||
private IncomeManager incomeManager; | ||
private ArrayList<Income> incomes; | ||
private int numberOfIncomes; | ||
|
||
@BeforeEach | ||
void initializeTestContent() { | ||
incomes = new ArrayList<>(); | ||
numberOfIncomes = 0; | ||
incomeManager = new IncomeManager(incomes, numberOfIncomes); | ||
} | ||
|
||
@Test | ||
void addIncome_validIncome_incomeAddedSuccessfully() { | ||
Income income = new Income("Salary", 5000, LocalDate.of(2024, 10, 1)); | ||
incomeManager.addIncome(income); | ||
|
||
assertEquals(1, incomeManager.getNumberOfIncomes()); | ||
assertEquals(income, incomeManager.getIncomes().get(0)); | ||
} | ||
|
||
@Test | ||
void deleteIncome_validIndex_incomeDeletedSuccessfully() { | ||
Income income = new Income("Salary", 5000, LocalDate.of(2024, 10, 1)); | ||
incomeManager.addIncome(income); | ||
|
||
// Now deleting the income at index 0 (since it's the first item) | ||
incomeManager.deleteIncome(0); | ||
|
||
assertEquals(0, incomeManager.getNumberOfIncomes()); | ||
assertTrue(incomeManager.getIncomes().isEmpty()); | ||
} | ||
|
||
@Test | ||
void listIncomes_withIncomes_displaysIncomes() { | ||
Income income1 = new Income("Salary", 5000, LocalDate.of(2024, 10, 1)); | ||
Income income2 = new Income("Bonus", 1000, LocalDate.of(2024, 10, 15)); | ||
incomeManager.addIncome(income1); | ||
incomeManager.addIncome(income2); | ||
|
||
// Test output to UI, this can be validated through mocking the Ui class | ||
incomeManager.listIncomes(); | ||
// assert the UI displays correct output | ||
} | ||
|
||
@Test | ||
void displayIncomeWithMonth_emptyMonth_displaysNoIncomeMessage() { | ||
YearMonth month = YearMonth.of(2024, 9); | ||
incomeManager.displayIncomeWithMonth(month); | ||
|
||
// assert the UI displays the empty message | ||
} | ||
|
||
@Test | ||
void getYearMonthFromDate_validDate_returnsCorrectYearMonth() { | ||
LocalDate date = LocalDate.of(2024, 10, 1); | ||
YearMonth expected = YearMonth.of(2024, 10); | ||
assertEquals(expected, IncomeManager.getYearMonthFromDate(date)); | ||
} | ||
} | ||
|
82 changes: 82 additions & 0 deletions
82
src/test/java/seedu/budgetbuddy/validators/AddExpenseValidatorTest.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,82 @@ | ||
package seedu.budgetbuddy.validators; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import seedu.budgetbuddy.commands.AddExpenseCommand; | ||
import seedu.budgetbuddy.commands.Command; | ||
import seedu.budgetbuddy.commands.IncorrectCommand; | ||
import seedu.budgetbuddy.transaction.expense.Category; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class AddExpenseValidatorTest { | ||
|
||
@Test | ||
void processCommand_invalidAmount_returnsIncorrectCommand() { | ||
Command result = AddExpenseValidator.processCommand("add expense Lunch a/abc d/12/10/2024 c/FOOD"); | ||
|
||
// Check if result is an IncorrectCommand and compare the message | ||
assertTrue(result instanceof IncorrectCommand); | ||
String message = ((IncorrectCommand) result).getFeedbackToUser(); | ||
assertEquals("Invalid amount format. Amount should be a positive number.", message); | ||
} | ||
|
||
@Test | ||
void processCommand_noDescription_returnsIncorrectCommand() { | ||
Command result = AddExpenseValidator.processCommand("add expense a/100 d/12/10/2024 c/FOOD"); | ||
|
||
// Check if result is an IncorrectCommand and compare the message | ||
assertTrue(result instanceof IncorrectCommand); | ||
String message = ((IncorrectCommand) result).getFeedbackToUser(); | ||
assertEquals("Description cannot be empty.", message); | ||
} | ||
|
||
@Test | ||
void processCommand_invalidDate_returnsIncorrectCommand() { | ||
Command result = AddExpenseValidator.processCommand("add expense Lunch a/100 d/invalid_date c/FOOD"); | ||
|
||
// Check if result is an IncorrectCommand and compare the message | ||
assertTrue(result instanceof IncorrectCommand); | ||
String message = ((IncorrectCommand) result).getFeedbackToUser(); | ||
assertEquals("Invalid date format. Use d/dd/MM/yyyy.", message); | ||
} | ||
|
||
@Test | ||
void processCommand_noAmount_returnsIncorrectCommand() { | ||
Command result = AddExpenseValidator.processCommand("add expense Lunch d/12/10/2024 c/FOOD"); | ||
|
||
// Check if result is an IncorrectCommand and compare the message | ||
assertTrue(result instanceof IncorrectCommand); | ||
String message = ((IncorrectCommand) result).getFeedbackToUser(); | ||
assertEquals("Amount not entered.", message); | ||
} | ||
|
||
@Test | ||
void processCommand_negativeAmount_returnsIncorrectCommand() { | ||
Command result = AddExpenseValidator.processCommand("add expense Lunch a/-50 d/12/10/2024 c/FOOD"); | ||
|
||
// Check if result is an IncorrectCommand and compare the message | ||
assertTrue(result instanceof IncorrectCommand); | ||
String message = ((IncorrectCommand) result).getFeedbackToUser(); | ||
assertEquals("Amount must be a positive value.", message); | ||
} | ||
|
||
|
||
@Test | ||
void processCommand_validCommand_returnsAddExpenseCommand() { | ||
Command result = AddExpenseValidator.processCommand("add expense Lunch a/100 d/12/10/2024 c/FOOD"); | ||
|
||
// Check if the result is an AddExpenseCommand (a valid command) | ||
assertTrue(result instanceof AddExpenseCommand); | ||
AddExpenseCommand command = (AddExpenseCommand) result; | ||
|
||
// Verify the details of the created command | ||
assertEquals("Lunch", command.getDescription()); | ||
assertEquals(100, command.getAmount()); | ||
assertEquals(LocalDate.of(2024, 10, 12), command.getDate()); | ||
assertEquals(Category.FOOD, command.getCategory()); | ||
} | ||
} | ||
|