forked from nus-cs2103-AY2223S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
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
shirsho-12
merged 3 commits into
AY2223S2-CS2103T-W09-2:master
from
nicleejy:testing-1.4
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
28 changes: 28 additions & 0 deletions
28
src/test/java/fasttrack/model/util/CommandUtilityTest.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,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
26
src/test/java/fasttrack/model/util/StorageUtilityTest.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,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
52
src/test/java/fasttrack/model/util/UserInterfaceUtilTest.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,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); | ||
} | ||
|
||
} | ||
|
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,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()); | ||
} | ||
} | ||
} |
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 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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ggwp