Skip to content

Commit

Permalink
Merge pull request #196 from woojiahao/testing/improvements
Browse files Browse the repository at this point in the history
Improving unit testing
  • Loading branch information
woojiahao authored Nov 10, 2023
2 parents 8b70d95 + 84a3b7e commit 7e69600
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 34 deletions.

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/java/unicash/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,6 @@ public static ReadOnlyUniCash getSampleUniCash() {
return sample;
}

public static ReadOnlyUniCash getTestUniCash() {
UniCash sample = new UniCash();
for (var transaction : getTestTransactions()) {
sample.addTransaction(transaction);
}
return sample;
}

/**
* Returns a category set containing the list of strings given.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public void constructor_nullTransaction_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddTransactionCommand(null));
}

@Test
public void execute_nullModel_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddTransactionCommand(NUS).execute(null));
}

@Test
public void execute_transactionAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingTransactionAdded modelStub = new ModelStubAcceptingTransactionAdded();
Expand Down Expand Up @@ -80,9 +85,6 @@ public void equals() {
AddTransactionCommand addNusCommandCopy = new AddTransactionCommand(NUS);
assertEquals(addNusCommand, addNusCommandCopy);

// different types -> returns false
assertNotEquals(1, addNusCommand);

// null -> returns false
assertNotEquals(null, addNusCommand);

Expand Down
7 changes: 0 additions & 7 deletions src/test/java/unicash/logic/commands/HelpCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import unicash.model.Model;
import unicash.model.ModelManager;



public class HelpCommandTest {

@Test
Expand Down Expand Up @@ -173,10 +171,8 @@ public void execute_clearBudgetCommand_success() {
public void execute_resetCommand_success() {
Model model = new ModelManager();
Model expectedModel = new ModelManager();
System.out.println("&*&*&* " + ResetCommand.MESSAGE_USAGE);
CommandResult expectedCommandResult =
new CommandResult(ResetCommand.MESSAGE_USAGE);
System.out.println("***" + expectedCommandResult);
assertCommandSuccess(new HelpCommand(CommandType.RESET.getMainCommandWord()),
model, expectedCommandResult, expectedModel);
}
Expand Down Expand Up @@ -241,9 +237,6 @@ public void equals() {
HelpCommand helpCommandEditCopy = new HelpCommand(EditTransactionCommand.COMMAND_WORD);
assertEquals(helpCommandEdit, helpCommandEditCopy);

// different types -> returns false
assertNotEquals(1, helpCommandEdit);

// null -> returns false
assertNotEquals(null, helpCommandEdit);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,16 @@ public void parse_invalidValue_failure() {
assertParseFailure(parser, TRANSACTION_NAME_DESC_NUS + AMOUNT_DESC_NUS + DATETIME_DESC_NUS
+ INVALID_TYPE_DESC, Type.MESSAGE_CONSTRAINTS);
}

@Test
public void parse_nonEmptyPreamble_throwsParseException() {
assertParseFailure(
parser,
VALID_TRANSACTION_NAME_NUS
+ TRANSACTION_NAME_DESC_NUS
+ AMOUNT_DESC_NUS
+ TYPE_DESC_EXPENSE,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTransactionCommand.MESSAGE_USAGE)
);
}
}
9 changes: 9 additions & 0 deletions src/test/java/unicash/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ public void parseAmount_null_throwsNullPointerException() {
@Test
public void parseAmount_invalidValue_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parseAmount(INVALID_AMOUNT));
assertThrows(ParseException.class, () -> ParserUtil.parseAmount("$hi"));
assertThrows(ParseException.class, () -> ParserUtil.parseAmount("^55.13"));
}

@Test
public void parseAmount_validValueWithoutWhitespace_returnsTransactionName() throws Exception {
Amount expectedAmount = new Amount(3.0);
assertEquals(expectedAmount, ParserUtil.parseAmount(VALID_AMOUNT));
assertEquals(expectedAmount, ParserUtil.parseAmount("$3.00"));
}

@Test
Expand Down Expand Up @@ -180,6 +183,12 @@ public void parseDateTime_validValueWithWhitespace_returnsTrimmedTransactionName
assertEquals(expectedDateTime, ParserUtil.parseDateTime(dateTimeWithWhiteSpace));
}

@Test
public void parseDateTime_empty_returnsDefaultDateTime() throws Exception {
DateTime expected = new DateTime(WHITESPACE);
assertEquals(expected, ParserUtil.parseDateTime(WHITESPACE));
}

@Test
public void parseCategory_empty_doesNotThrow() throws Exception {
assertDoesNotThrow(() -> ParserUtil.parseDateTime(""));
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/unicash/logic/parser/PrefixTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package unicash.logic.parser;

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

import org.junit.jupiter.api.Test;

public class PrefixTest {
@Test
public void hashCode_nullPrefix_returnsZero() {
var prefix = new Prefix(null);
assertEquals(0, prefix.hashCode());
}

@Test
public void hashCode_nonNullPrefix_returnsNonZero() {
var prefix = new Prefix("p/");
assertTrue(prefix.hashCode() != 0);
}
}
97 changes: 91 additions & 6 deletions src/test/java/unicash/model/category/UniqueCategoryListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Collections;
import java.util.List;

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

import unicash.model.category.exceptions.CategoryNotFoundException;
Expand All @@ -22,7 +23,14 @@

public class UniqueCategoryListTest {

private final UniqueCategoryList uniqueCategoryList = new UniqueCategoryList();
private UniqueCategoryList uniqueCategoryList;

@BeforeEach
public void init() {
// Initialize the global unit category list this way to avoid accidentally mutating it
// across unit tests
uniqueCategoryList = new UniqueCategoryList();
}

@Test
public void constructor_duplicate_throwsDuplicateCategoryException() {
Expand All @@ -41,6 +49,15 @@ public void constructor_moreThanMaximumAllowed_throwsMaxCategoryException() {
assertThrows(MaxCategoryException.class, () -> new UniqueCategoryList(categoryList));
}

@Test
public void constructor_validList_success() {
var categories = List.of(EDUCATION, ENTERTAINMENT);
var list = new UniqueCategoryList(categories);
for (var category : categories) {
assertTrue(list.contains(category));
}
}

@Test
public void contains_nullCategory_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> uniqueCategoryList.contains(null));
Expand All @@ -54,6 +71,7 @@ public void contains_categoryNotInList_returnsFalse() {
@Test
public void contains_categoryInList_returnsTrue() {
uniqueCategoryList.add(ENTERTAINMENT);
uniqueCategoryList.add(EDUCATION);
assertTrue(uniqueCategoryList.contains(ENTERTAINMENT));
}

Expand All @@ -76,6 +94,14 @@ public void add_maxCategory_throwsMaxCategoryException() {
assertThrows(MaxCategoryException.class, () -> uniqueCategoryList.add(new Category("test5")));
}

@Test
public void add_success_addsToCategoryList() {
assertTrue(uniqueCategoryList.isEmpty());
uniqueCategoryList.add(ENTERTAINMENT);
assertFalse(uniqueCategoryList.isEmpty());
assertTrue(uniqueCategoryList.contains(ENTERTAINMENT));
}

@Test
public void setCategory_nullTargetCategory_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> uniqueCategoryList.setCategory(null, ENTERTAINMENT));
Expand Down Expand Up @@ -194,19 +220,68 @@ public void toStringMethod() {
}

@Test
public void getSize_test() {
public void getSize_emptyList_returnsSizeZero() {
UniqueCategoryList categoryList = new UniqueCategoryList();
assertEquals(categoryList.getSize(), 0);
}

@Test
public void emptyList_test() {
public void getSize_emptyList_returnsNonZeroSize() {
var categories = List.of(EDUCATION);
var list = new UniqueCategoryList(categories);
assertEquals(list.getSize(), 1);
}

@Test
public void isEmpty_withNoElements_returnsTrue() {
UniqueCategoryList categoryList = new UniqueCategoryList();
assertTrue(categoryList.isEmpty());
}

@Test
public void joinCategoriesAsStringTest() {
public void isEmpty_withElements_returnsFalse() {
var categories = List.of(EDUCATION);
var list = new UniqueCategoryList(categories);
assertFalse(list.isEmpty());
}

@Test
public void isMax_notMax_returnsFalse() {
uniqueCategoryList.add(new Category("a"));
uniqueCategoryList.add(new Category("b"));
uniqueCategoryList.add(new Category("c"));
uniqueCategoryList.add(new Category("d"));
assertFalse(uniqueCategoryList.isMax());

}

@Test
public void isMax_equalsMax_returnsTrue() {
uniqueCategoryList.add(new Category("a"));
uniqueCategoryList.add(new Category("b"));
uniqueCategoryList.add(new Category("c"));
uniqueCategoryList.add(new Category("d"));
uniqueCategoryList.add(new Category("e"));
assertTrue(uniqueCategoryList.isMax());
}

@Test
public void isMoreThanMax_less_returnsFalse() {
var list = List.of(EDUCATION);
assertFalse(UniqueCategoryList.isMoreThanMax(list));
}

@Test
public void isMoreThanMax_more_returnsTrue() {
var list = new ArrayList<Category>();
for (int i = 0; i < 10; i++) {
list.add(new Category("Test" + i));
}
assertTrue(UniqueCategoryList.isMoreThanMax(list));
}

@Test
public void joinCategoriesAsString_withElements_returnsJoinedString() {
UniqueCategoryList categoryList = new UniqueCategoryList();
categoryList.add(ENTERTAINMENT);
categoryList.add(EDUCATION);
Expand All @@ -215,7 +290,12 @@ public void joinCategoriesAsStringTest() {
}

@Test
public void joinCategoriesAsListTest() {
public void joinCategoriesAsString_noElements_returnsEmptyString() {
assertEquals("", uniqueCategoryList.joinCategoriesAsString());
}

@Test
public void joinCategoriesAsList_withElements_returnsNewList() {
UniqueCategoryList categoryUniqueList = new UniqueCategoryList();
categoryUniqueList.add(ENTERTAINMENT);
categoryUniqueList.add(EDUCATION);
Expand All @@ -233,6 +313,11 @@ public void joinCategoriesAsListTest() {
categoryUniqueList.joinCategoriesAsList().toString());
}

@Test
public void joinCategoriesAsList_noElements_returnsEmptyList() {
assertEquals(0, uniqueCategoryList.joinCategoriesAsList().size());
}

@Test
public void equals() {
// same object -> returns true
Expand All @@ -253,7 +338,7 @@ public void equals() {
// null -> returns false
assertNotEquals(null, categoryList);

assertFalse(categoryList.equals(null));
assertFalse(categoryList.equals(5));
}

@Test
Expand Down
16 changes: 13 additions & 3 deletions src/test/java/unicash/model/transaction/TransactionListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static unicash.testutil.TypicalTransactions.BUYING_GROCERIES;
import static unicash.testutil.TypicalTransactions.INTERN;
import static unicash.testutil.TypicalTransactions.NUS;
import static unicash.testutil.TypicalTransactions.SHOPPING;
import static unicash.testutil.TypicalTransactions.getMaxTransactionList;
import static unicash.testutil.TypicalTransactions.getTypicalTransactions;

Expand All @@ -18,6 +19,7 @@
import java.util.Iterator;
import java.util.List;

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

import unicash.model.transaction.exceptions.MaxTransactionException;
Expand All @@ -26,7 +28,12 @@

public class TransactionListTest {

private final TransactionList transactionList = new TransactionList();
private TransactionList transactionList;

@BeforeEach
public void init() {
transactionList = new TransactionList();
}

@Test
public void contains_nullTransaction_throwsNullPointerException() {
Expand All @@ -41,13 +48,15 @@ public void contains_transactionNotInList_returnsFalse() {
@Test
public void contains_transactionInList_returnsTrue() {
transactionList.add(NUS);
transactionList.add(SHOPPING);
assertTrue(transactionList.contains(NUS));
}

@Test
public void contains_differentTransaction_returnsFalse() {
transactionList.add(NUS);
Transaction editedNus = new TransactionBuilder(NUS).withAmount(VALID_AMOUNT_INTERN)
Transaction editedNus = new TransactionBuilder(NUS)
.withAmount(VALID_AMOUNT_INTERN)
.build();
assertFalse(transactionList.contains(editedNus));
}
Expand Down Expand Up @@ -84,6 +93,7 @@ public void setTransaction_targetTransactionNotInList_throwsTransactionNotFoundE
assertThrows(TransactionNotFoundException.class, () -> transactionList.setTransaction(NUS, NUS));
}

// TODO: Add explicit unit tests for the case of duplicate transactions and editing one of them
@Test
public void setTransaction_editedTransactionIsSameTransaction_success() {
transactionList.add(NUS);
Expand Down Expand Up @@ -226,7 +236,7 @@ public void equals() {
// null -> returns false
assertNotEquals(null, transactionList);

assertFalse(transactionList.equals(null));
assertFalse(transactionList.equals(5));
}

@Test
Expand Down

0 comments on commit 7e69600

Please sign in to comment.