Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added test cases for other utility classes #225

Merged
merged 3 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,16 @@ For example, if your monthly budget is $1000, and you have already spent $500, y
This gives you an idea of how much of your monthly budget you have used up.


<div markdown="block" class="alert alert-warning">

**:exclamation: Caution**<br>

Even if you have exceeded your budget, this statistic will reflect that you have fully utilised your budget, and will remain at `100%`.

</div>



<p align="right">
<a href="#top">Back to Top </a>
</p>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fasttrack/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static Category parseCategory(String categoryName) throws ParseException
public static UserDefinedCategory parseCategory(String category, String summary) throws ParseException {
requireNonNull(category);
String trimmedCategory = category.trim();
if (!Category.isValidCategoryName(category)) {
if (!Category.isValidCategoryName(trimmedCategory)) {
throw new ParseException(Category.MESSAGE_CONSTRAINTS);
}
return new UserDefinedCategory(trimmedCategory, summary);
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/fasttrack/ui/CategoryListPanel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package fasttrack.ui;

import java.util.logging.Logger;

import fasttrack.commons.core.LogsCenter;
import fasttrack.model.category.Category;
import fasttrack.model.expense.Expense;
import javafx.collections.ObservableList;
Expand All @@ -16,7 +13,6 @@
*/
public class CategoryListPanel extends UiPart<Region> {
private static final String FXML = "CategoryListPanel.fxml";
private final Logger logger = LogsCenter.getLogger(CategoryListPanel.class);
private final ObservableList<Expense> expenseObservableList;

@FXML
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/fasttrack/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ public class CommandBox extends UiPart<Region> {
/**
* Creates a {@code CommandBox} with the given {@code CommandExecutor}.
*/
public CommandBox(CommandExecutor commandExecutor) {
public CommandBox(CommandExecutor commandExecutor, boolean initialiseAutocompletion) {
super(FXML);
this.commandExecutor = commandExecutor;
// calls #setStyleToDefault() whenever there is a change to the text of the command box.
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
initialiseAutocompleteHandler();
if (initialiseAutocompletion) {
initialiseAutocompleteHandler();
}
}


/**
* Handles the Enter button pressed event.
*/
@FXML
private void handleCommandEntered() {
public void handleCommandEntered() {
String commandText = commandTextField.getText();
if (commandText.equals("")) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fasttrack/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void fillInnerParts() {
StatusBarFooter statusBarFooter = new StatusBarFooter(logic.getAddressBookFilePath());
statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot());

CommandBox commandBox = new CommandBox(this::executeCommand);
CommandBox commandBox = new CommandBox(this::executeCommand, true);
SuggestionListPanel suggestionListPanel = new SuggestionListPanel(logic.getFilteredCategoryList(), commandBox);


Expand Down
22 changes: 12 additions & 10 deletions src/test/java/fasttrack/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import fasttrack.commons.core.index.Index;
import fasttrack.logic.parser.exceptions.ParseException;
import fasttrack.model.category.MiscellaneousCategory;
import fasttrack.model.category.UserDefinedCategory;
import fasttrack.model.expense.Price;
import fasttrack.model.expense.RecurringExpenseType;

Expand Down Expand Up @@ -65,16 +67,16 @@ public void parsePrice_invalidInput_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parsePrice("0"));
}

//@Test
//public void parseCategoryWithSummary_validInput_success() throws ParseException {
// // leading and trailing whitespace
// assertEquals(new UserDefinedCategory("category", "abc"), ParserUtil.parseCategory(" category ", "abc"));
// // miscellaneous category
// assertEquals(new MiscellaneousCategory(), ParserUtil.parseCategory("miscellaneous"));
// UserDefinedCategory category = ParserUtil.parseCategory("food", "for dining");
// assertEquals("food", category.getCategoryName());
// assertEquals("for dining", category.getSummary());
//}
@Test
public void parseCategoryWithSummary_validInput_success() throws ParseException {
// leading and trailing whitespace
assertEquals(new UserDefinedCategory("category", "abc"), ParserUtil.parseCategory(" category ", "abc"));
// miscellaneous category
assertEquals(new MiscellaneousCategory(), ParserUtil.parseCategory("miscellaneous"));
UserDefinedCategory category = ParserUtil.parseCategory("food", "for dining");
assertEquals("food", category.getCategoryName());
assertEquals("for dining", category.getSummary());
}
Comment on lines +70 to +79

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ggwp


@Test
public void parseCategory_invalidInput_throwsParseException() {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/fasttrack/model/util/CommandUtilityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fasttrack.model.util;

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

import java.time.LocalDate;

import org.junit.jupiter.api.Test;

class CommandUtilityTest {

@Test
void testParseDateFromUserInput_validFormats_success() {
assertEquals(LocalDate.of(2023, 3, 1), CommandUtility.parseDateFromUserInput("1/3/23"));
assertEquals(LocalDate.of(2023, 6, 5), CommandUtility.parseDateFromUserInput("5/6/2023"));
assertEquals(LocalDate.of(2022, 7, 16), CommandUtility.parseDateFromUserInput("16/7/22"));
assertEquals(LocalDate.of(2021, 12, 31), CommandUtility.parseDateFromUserInput("31/12/2021"));
}
@Test
void testParseDateFromUserInput_invalidFormats_throwsException() {
assertThrows(IllegalArgumentException.class, () -> CommandUtility.parseDateFromUserInput("32/2/2022"));
assertThrows(IllegalArgumentException.class, () -> CommandUtility.parseDateFromUserInput("12/13/2023"));
assertThrows(IllegalArgumentException.class, () -> CommandUtility.parseDateFromUserInput("12w/12/23a"));
assertThrows(IllegalArgumentException.class, () -> CommandUtility.parseDateFromUserInput("12/c12/23a"));
assertThrows(IllegalArgumentException.class, () -> CommandUtility.parseDateFromUserInput("2023-4-31"));
}

}
26 changes: 26 additions & 0 deletions src/test/java/fasttrack/model/util/StorageUtilityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fasttrack.model.util;

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

import java.time.LocalDate;

import org.junit.jupiter.api.Test;


public class StorageUtilityTest {

@Test
public void testParseDateFromJson_validDate() {
LocalDate expectedDate = LocalDate.of(2023, 4, 30);
LocalDate parsedDate = StorageUtility.parseDateFromJson("2023-04-30");
assertEquals(expectedDate, parsedDate);
}

@Test
public void testParseDateFromJson_invalidDate() {
assertThrows(java.time.format.DateTimeParseException.class, () -> {
StorageUtility.parseDateFromJson("20-4-31");
});
}
}
52 changes: 52 additions & 0 deletions src/test/java/fasttrack/model/util/UserInterfaceUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fasttrack.model.util;

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

import java.time.LocalDate;

import org.junit.jupiter.api.Test;

public class UserInterfaceUtilTest {

@Test
public void testParseDate_validDate_success() {
LocalDate date1 = LocalDate.of(2023, 4, 10);
String formattedDate1 = UserInterfaceUtil.parseDate(date1);
assertEquals("10/04/23", formattedDate1);
LocalDate date2 = LocalDate.of(2023, 12, 31);
String formattedDate2 = UserInterfaceUtil.parseDate(date2);
assertEquals("31/12/23", formattedDate2);
}

@Test
public void testParsePrice_validInput_success() {
double amount1 = 10.00;
String formattedAmount1 = UserInterfaceUtil.parsePrice(amount1);
assertEquals("$10.00", formattedAmount1);

double amount2 = -5.50;
String formattedAmount2 = UserInterfaceUtil.parsePrice(amount2);
assertEquals("$-5.50", formattedAmount2);

double amount3 = 3.14159;
String formattedAmount3 = UserInterfaceUtil.parsePrice(amount3);
assertEquals("$3.14", formattedAmount3);
}

@Test
public void testCapitalizeFirstLetter_validInput_success() {
String input1 = "hello";
String capitalized1 = UserInterfaceUtil.capitalizeFirstLetter(input1);
assertEquals("Hello", capitalized1);

String input2 = "wORLD";
String capitalized2 = UserInterfaceUtil.capitalizeFirstLetter(input2);
assertEquals("WORLD", capitalized2);

String input3 = "TEST";
String capitalized3 = UserInterfaceUtil.capitalizeFirstLetter(input3);
assertEquals("TEST", capitalized3);
}

}

87 changes: 87 additions & 0 deletions src/test/java/fasttrack/ui/CategoryCardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fasttrack.ui;

import static fasttrack.testutil.TypicalCategories.FOOD;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import fasttrack.model.category.Category;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.control.Label;


public class CategoryCardTest {

private Category category;
private int displayedIndex;
private int associatedExpenseCount;

@BeforeEach
public void setUp() {
category = FOOD;
displayedIndex = 1;
associatedExpenseCount = 3;
// Initialise fake JavaFX environment
new JFXPanel();
}

@Test
public void testCategoryCard_validData_success() {
CategoryCard categoryCard = new CategoryCard(category, displayedIndex, associatedExpenseCount);
CompletableFuture<Void> future = new CompletableFuture<>();
Platform.runLater(() -> {
try {
// Test that the category name label is set correctly
Label categoryNameLabel = (Label) categoryCard.getRoot().lookup("#categoryName");
assertEquals("Food", categoryNameLabel.getText());

// Test that the index label is set correctly
Label indexLabel = (Label) categoryCard.getRoot().lookup("#id");
assertEquals("1. ", indexLabel.getText());

// Test that the expense count label is set correctly
Label expenseCountLabel = (Label) categoryCard.getRoot().lookup("#expenseCount");
assertEquals("3", expenseCountLabel.getText());
future.complete(null);
} catch (AssertionFailedError e) {
future.completeExceptionally(e);
}
});
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
fail("Assertion error thrown in Platform.runLater thread: " + e.getMessage());
}
}

@Test
public void testEquals_validCategoryCard_success() {
CategoryCard categoryCard1 = new CategoryCard(category, displayedIndex, associatedExpenseCount);
CategoryCard categoryCard2 = new CategoryCard(category, displayedIndex + 1, associatedExpenseCount - 1);
CategoryCard categoryCard3 = new CategoryCard(category, displayedIndex + 1, associatedExpenseCount - 1);
CompletableFuture<Void> future = new CompletableFuture<>();
Platform.runLater(() -> {
try {
assertEquals(categoryCard1, categoryCard1);
assertNotEquals(categoryCard1, categoryCard2);
assertEquals(categoryCard2, categoryCard3);
future.complete(null);
} catch (AssertionFailedError e) {
future.completeExceptionally(e);
}
});
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
fail("Assertion error thrown in Platform.runLater thread: " + e.getMessage());
}
}
}
82 changes: 82 additions & 0 deletions src/test/java/fasttrack/ui/CategoryListPanelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fasttrack.ui;

import static fasttrack.testutil.TypicalCategories.FOOD;
import static fasttrack.testutil.TypicalCategories.TECH;
import static fasttrack.testutil.TypicalExpenses.APPLE;
import static fasttrack.testutil.TypicalExpenses.CHERRY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import fasttrack.model.category.Category;
import fasttrack.model.expense.Expense;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.control.ListView;


public class CategoryListPanelTest {

private CategoryListPanel categoryListPanel;
private ObservableList<Category> categories;
private ObservableList<Expense> expenses;

@BeforeEach
public void setUp() {
categories = FXCollections.observableArrayList(FOOD, TECH);
expenses = FXCollections.observableArrayList(APPLE, CHERRY);
// Initialise fake JavaFX environment
new JFXPanel();
}

@Test
public void categoryListView_validCategories_countEqual() {
CompletableFuture<Void> future = new CompletableFuture<>();
categoryListPanel = new CategoryListPanel(categories, expenses);
Platform.runLater(() -> {
try {
// Test that the number of categories is correct
ListView<?> categoryListView = (ListView<?>) categoryListPanel.getRoot().lookup("#categoryListView");
assertEquals(categories.size(), categoryListView.getItems().size());
future.complete(null);
} catch (AssertionFailedError e) {
future.completeExceptionally(e);
}
});
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
fail("Assertion error thrown in Platform.runLater thread: " + e.getMessage());
}
}

@Test
public void categoryListView_emptyList_countZero() {
categories = FXCollections.observableArrayList();
expenses = FXCollections.observableArrayList();
CompletableFuture<Void> future = new CompletableFuture<>();
categoryListPanel = new CategoryListPanel(categories, expenses);
Platform.runLater(() -> {
try {
ListView<?> categoryListView = (ListView<?>) categoryListPanel.getRoot().lookup("#categoryListView");
assertEquals(0, categoryListView.getItems().size());
future.complete(null);
} catch (AssertionFailedError e) {
future.completeExceptionally(e);
}
});
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
fail("Assertion error thrown in Platform.runLater thread: " + e.getMessage());
}
}
}
Loading