Skip to content

Commit

Permalink
Merge pull request #169 from CS2113-AY1819S1-T09-4/Writing_Sort_Tests
Browse files Browse the repository at this point in the history
SortCommand and Parser Tests Done
  • Loading branch information
tztzt committed Nov 6, 2018
2 parents 44f84d4 + 5726922 commit ed57a14
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class SortCommand extends Command {
+ "Example: " + COMMAND_WORD + " " + CATEGORY_NAME + " " + DESCENDING_CONDITION;

public static final Set<String> CATEGORY_SET = new HashSet<>(Arrays.asList(CATEGORY_NAME, CATEGORY_MONEYFLOW,
CATEGORY_MONEY, CATEGORY_DATE));
CATEGORY_MONEY, SortCommand.CATEGORY_DATE));
public static final Set<String> ORDER_SET = new HashSet<>(Arrays.asList(DESCENDING_CONDITION, ASCENDING_CONDITION));

private final String category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static seedu.planner.testutil.TypicalLimits.LIMIT_ALL_DIFFERENT;
import static seedu.planner.testutil.TypicalLimits.LIMIT_DATE_END_DIFF;
import static seedu.planner.testutil.TypicalLimits.LIMIT_SINGLE_DATE_100;
import static seedu.planner.testutil.TypicalLimits.LIMIT_SINGLE_DATE_All_DIFF;
import static seedu.planner.testutil.TypicalLimits.LIMIT_SINGLE_DATE_ALL_DIFF;
import static seedu.planner.testutil.TypicalRecords.getTypicalFinancialPlanner;

import org.junit.Test;
Expand Down Expand Up @@ -83,7 +83,7 @@ public void execute_limitDatesPartiallySame_throwCommandException() {
@Test
public void execute_limitSingleDateNotInList_throwCommandException() {
Limit limit = new LimitBuilder(LIMIT_SINGLE_DATE_100).build();
Limit limitNotInside = new LimitBuilder(LIMIT_SINGLE_DATE_All_DIFF).build();
Limit limitNotInside = new LimitBuilder(LIMIT_SINGLE_DATE_ALL_DIFF).build();
DeleteLimitCommand deleteLimitCommand = new DeleteLimitCommand(limitNotInside);
model.addLimit(limit);
assertCommandFailure(deleteLimitCommand, model, commandHistory, Messages.MESSAGE_LIMITS_DO_NOT_EXIST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
import static org.junit.Assert.assertFalse;
import static seedu.planner.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.planner.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.planner.testutil.TypicalLimits.LIMIT_100;
import static seedu.planner.testutil.TypicalLimits.LIMIT_500;
import static seedu.planner.testutil.TypicalLimits.LIMIT_ALL_DIFFERENT;
import static seedu.planner.testutil.TypicalLimits.LIMIT_SINGLE_DATE_100;
import static seedu.planner.testutil.TypicalLimits.LIMIT_SINGLE_DATE_500;
import static seedu.planner.testutil.TypicalLimits.LIMIT_SINGLE_DATE_All_DIFF;
import static seedu.planner.testutil.TypicalRecords.getTypicalFinancialPlanner;

import org.junit.Test;
Expand All @@ -22,6 +16,7 @@
import seedu.planner.model.UserPrefs;
import seedu.planner.model.record.Limit;
import seedu.planner.testutil.LimitBuilder;
import seedu.planner.testutil.TypicalLimits;
//@@Author OscarZeng

public class EditLimitCommandTest {
Expand All @@ -30,8 +25,8 @@ public class EditLimitCommandTest {

@Test
public void execute_hasSameDatesLimitInside_success() {
Limit editedLimit = new LimitBuilder(LIMIT_100).build();
Limit originalLimit = new LimitBuilder(LIMIT_500).build();
Limit editedLimit = new LimitBuilder(TypicalLimits.LIMIT_100).build();
Limit originalLimit = new LimitBuilder(TypicalLimits.LIMIT_500).build();
EditLimitCommand editLimitCommand = new EditLimitCommand(editedLimit);
String expectedMessage = EditLimitCommand.MESSAGE_SUCCESS + "Original Limit:\n"
+ model.generateLimitOutput(model.isExceededLimit(originalLimit),
Expand All @@ -49,8 +44,8 @@ public void execute_hasSameDatesLimitInside_success() {

@Test
public void execute_singleDateLimitInList_success() {
Limit editedLimit = new LimitBuilder(LIMIT_SINGLE_DATE_100).build();
Limit originalLimit = new LimitBuilder(LIMIT_SINGLE_DATE_500).build();
Limit editedLimit = new LimitBuilder(TypicalLimits.LIMIT_SINGLE_DATE_100).build();
Limit originalLimit = new LimitBuilder(TypicalLimits.LIMIT_SINGLE_DATE_500).build();
EditLimitCommand editLimitCommand = new EditLimitCommand(editedLimit);
String expectedMessage = EditLimitCommand.MESSAGE_SUCCESS + "Original Limit:\n"
+ model.generateLimitOutput(model.isExceededLimit(originalLimit),
Expand All @@ -68,8 +63,8 @@ public void execute_singleDateLimitInList_success() {

@Test
public void execute_hasNoSameDatesLimitInside_fail() {
Limit editedLimit = new LimitBuilder(LIMIT_100).build();
Limit originalLimit = new LimitBuilder(LIMIT_ALL_DIFFERENT).build();
Limit editedLimit = new LimitBuilder(TypicalLimits.LIMIT_100).build();
Limit originalLimit = new LimitBuilder(TypicalLimits.LIMIT_ALL_DIFFERENT).build();
EditLimitCommand editLimitCommand = new EditLimitCommand(editedLimit);
String expectedMessage = Messages.MESSAGE_LIMITS_DO_NOT_EXIST;
model.addLimit(originalLimit);
Expand All @@ -79,8 +74,8 @@ public void execute_hasNoSameDatesLimitInside_fail() {

@Test
public void execute_hasNoSameSingleDateLimitInside_fail() {
Limit editedLimit = new LimitBuilder(LIMIT_SINGLE_DATE_100).build();
Limit originalLimit = new LimitBuilder(LIMIT_SINGLE_DATE_All_DIFF).build();
Limit editedLimit = new LimitBuilder(TypicalLimits.LIMIT_SINGLE_DATE_100).build();
Limit originalLimit = new LimitBuilder(TypicalLimits.LIMIT_SINGLE_DATE_ALL_DIFF).build();
EditLimitCommand editLimitCommand = new EditLimitCommand(editedLimit);
String expectedMessage = Messages.MESSAGE_LIMITS_DO_NOT_EXIST;
model.addLimit(originalLimit);
Expand All @@ -90,7 +85,7 @@ public void execute_hasNoSameSingleDateLimitInside_fail() {

@Test
public void execute_emptyLimitList_fail() {
Limit editedLimit = new LimitBuilder(LIMIT_100).build();
Limit editedLimit = new LimitBuilder(TypicalLimits.LIMIT_100).build();
EditLimitCommand editLimitCommand = new EditLimitCommand(editedLimit);
String expectedMessage = Messages.MESSAGE_LIMITS_DO_NOT_EXIST;

Expand All @@ -99,8 +94,8 @@ public void execute_emptyLimitList_fail() {

@Test
public void executeUndoRedo_validLimitEditedExecution_success() throws Exception {
Limit originalLimit = new LimitBuilder(LIMIT_100).build();
Limit editedLimit = new LimitBuilder(LIMIT_500).build();
Limit originalLimit = new LimitBuilder(TypicalLimits.LIMIT_100).build();
Limit editedLimit = new LimitBuilder(TypicalLimits.LIMIT_500).build();
model.addLimit(originalLimit);
EditLimitCommand editLimitCommand = new EditLimitCommand(editedLimit);
Model expectedModel = new ModelManager(model.getFinancialPlanner(), new UserPrefs());
Expand All @@ -121,15 +116,15 @@ public void executeUndoRedo_validLimitEditedExecution_success() throws Exception

@Test
public void equals() {
final EditLimitCommand editLimitCommand2 = new EditLimitCommand(LIMIT_100);
final EditLimitCommand editLimitCommand1 = new EditLimitCommand(LIMIT_SINGLE_DATE_100);
final EditLimitCommand editLimitCommand2 = new EditLimitCommand(TypicalLimits.LIMIT_100);
final EditLimitCommand editLimitCommand1 = new EditLimitCommand(TypicalLimits.LIMIT_SINGLE_DATE_100);

// same limits -> returns true
EditLimitCommand sameLimitCommand = new EditLimitCommand(LIMIT_100);
EditLimitCommand sameLimitCommand = new EditLimitCommand(TypicalLimits.LIMIT_100);
assertTrue(editLimitCommand2.equals(sameLimitCommand));

// same limits -> returns true
EditLimitCommand differentLimitCommand = new EditLimitCommand(LIMIT_ALL_DIFFERENT);
EditLimitCommand differentLimitCommand = new EditLimitCommand(TypicalLimits.LIMIT_ALL_DIFFERENT);
assertFalse(editLimitCommand2.equals(differentLimitCommand));

// same object -> returns true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package seedu.planner.logic.commands;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.planner.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.planner.logic.commands.FindTagCommand.MESSAGE_SUCCESS;
import static seedu.planner.logic.commands.FindTagCommand.convertKeywordsToMessage;
Expand All @@ -14,6 +12,7 @@
import java.util.Arrays;
import java.util.Collections;

import org.junit.Assert;
import org.junit.Test;

import seedu.planner.logic.CommandHistory;
Expand Down Expand Up @@ -45,20 +44,20 @@ public void equals() {
FindTagCommand findSecondCommand = new FindTagCommand(secondPredicate, inputSecondStringArray);

// same object -> returns true
assertTrue(findFirstCommand.equals(findFirstCommand));
Assert.assertEquals(findFirstCommand, findFirstCommand);

// same values -> returns true
FindTagCommand findFirstCommandCopy = new FindTagCommand(firstPredicate, inputFirstStringArray);
assertTrue(findFirstCommand.equals(findFirstCommandCopy));
assertEquals(findFirstCommand, findFirstCommandCopy);

// different types -> returns false
assertFalse(findFirstCommand.equals(1));
Assert.assertNotEquals(1, findFirstCommand);

// null -> returns false
assertFalse(findFirstCommand.equals(null));
Assert.assertNotEquals(null, findFirstCommand);

// different record -> returns false
assertFalse(findFirstCommand.equals(findSecondCommand));
Assert.assertNotEquals(findFirstCommand, findSecondCommand);
}

@Test
Expand Down
85 changes: 85 additions & 0 deletions src/test/java/seedu/planner/logic/commands/SortCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package seedu.planner.logic.commands;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static seedu.planner.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.planner.logic.commands.SortCommand.MESSAGE_SUCCESS;
import static seedu.planner.testutil.TypicalRecords.getTypicalFinancialPlanner;

import org.junit.Test;

import seedu.planner.logic.CommandHistory;
import seedu.planner.model.Model;
import seedu.planner.model.ModelManager;
import seedu.planner.model.UserPrefs;
import seedu.planner.testutil.FinancialPlannerBuilder;

public class SortCommandTest {

private Model model = new ModelManager(getTypicalFinancialPlanner(), new UserPrefs());
private Model emptyModel = new ModelManager(new FinancialPlannerBuilder().build(), new UserPrefs());
private Model expectedEmptyModel = new ModelManager(new FinancialPlannerBuilder().build(), new UserPrefs());

private CommandHistory commandHistory = new CommandHistory();

@Test
public void equals() {
SortCommand sortCommandOne = new SortCommand(SortCommand.CATEGORY_NAME, true);
SortCommand sortCommandTwo = new SortCommand(SortCommand.CATEGORY_MONEY, false);
SortCommand sortCommandOneCopy = new SortCommand(SortCommand.CATEGORY_NAME, true);
SortCommand sortCommandDateTrue = new SortCommand(SortCommand.CATEGORY_DATE, true);
SortCommand sortCommandNameFalse = new SortCommand(SortCommand.CATEGORY_NAME, false);

// same object -> returns true
assertEquals(sortCommandOne, sortCommandOne);
// same values -> returns true
assertEquals(sortCommandOne, sortCommandOneCopy);

// different values -> return false
assertNotEquals(sortCommandOne, sortCommandDateTrue);
assertNotEquals(sortCommandOne, sortCommandNameFalse);
assertNotEquals(sortCommandOne, sortCommandTwo);

// different kind of objects -> return false
assertNotEquals(sortCommandOne, null);
assertNotEquals(sortCommandOne, "not_equal_string");
}

@Test
public void execute_emptyList_listIsUnchanged() {
SortCommand command = new SortCommand("name", true);
String expectedMessage = String.format(SortCommand.MESSAGE_SUCCESS, "name", "ascending");

assertCommandSuccess(command, emptyModel, commandHistory, expectedMessage, expectedEmptyModel);
}

@Test
public void execute_unsortedList_listIsSorted() {
SortCommand command = new SortCommand(SortCommand.CATEGORY_DATE, false);
String expectedMessage = String.format(MESSAGE_SUCCESS, SortCommand.CATEGORY_DATE,
SortCommand.ORDER_DESCENDING);
// sort the initial model
Model expectedSortedModel = getSortedModel(model, SortCommand.CATEGORY_DATE, SortCommand.ORDER_DESCENDING);

assertCommandSuccess(command, model, commandHistory, expectedMessage, expectedSortedModel);
}

@Test
public void execute_sortedList_listIsUnchanged() {
SortCommand command = new SortCommand(SortCommand.CATEGORY_MONEY, true);
String expectedMessage = String.format(SortCommand.MESSAGE_SUCCESS, SortCommand.CATEGORY_MONEY,
SortCommand.ORDER_ASCENDING);
// obtain a sorted model
Model initialSortedModel = getSortedModel(model, SortCommand.CATEGORY_MONEY, SortCommand.ORDER_ASCENDING);
/// try to sorted it again, result should be identical model
Model expectedModel = getSortedModel(initialSortedModel, SortCommand.CATEGORY_MONEY,
SortCommand.ORDER_ASCENDING);

assertCommandSuccess(command, initialSortedModel, commandHistory, expectedMessage, expectedModel);
}

public Model getSortedModel(Model preSortedModel, String category, String order) {
preSortedModel.sortFilteredRecordList(category, (order == SortCommand.ORDER_ASCENDING));
return model;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package seedu.planner.logic.parser;

import static seedu.planner.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.planner.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.planner.logic.parser.CommandParserTestUtil.assertParseSuccess;

import org.junit.Test;

import seedu.planner.logic.commands.Command;
import seedu.planner.logic.commands.SortCommand;

public class SortCommandParserTest {
private SortCommandParser parser = new SortCommandParser();

@Test
public void parse_firstFieldsPresent_success() {
// Category specified
Command command = new SortCommand(SortCommand.CATEGORY_MONEY, true);
String inputCommand = SortCommand.CATEGORY_MONEY;
assertParseSuccess(parser, inputCommand, command);

// Order specified
command = new SortCommand(SortCommand.CATEGORY_NAME, false);
inputCommand = SortCommand.DESCENDING_CONDITION;
assertParseSuccess(parser, inputCommand, command);

// Order specified
command = new SortCommand(SortCommand.CATEGORY_NAME, true);
inputCommand = SortCommand.ASCENDING_CONDITION;
assertParseSuccess(parser, inputCommand, command);
}

@Test
public void parse_bothFieldsPresent_success() {
// Category and order specified
Command command = new SortCommand(SortCommand.CATEGORY_MONEY, false);
String inputCommand = SortCommand.CATEGORY_MONEY + " "
+ SortCommand.DESCENDING_CONDITION;
assertParseSuccess(parser, inputCommand, command);

// Order and category specified
command = new SortCommand(SortCommand.CATEGORY_DATE, true);
inputCommand = SortCommand.ASCENDING_CONDITION + " "
+ SortCommand.CATEGORY_DATE;
assertParseSuccess(parser, inputCommand, command);
}

@Test
public void parse_additionalWhitespacesPresent_success() {
Command command = new SortCommand(SortCommand.CATEGORY_MONEYFLOW, false);
String inputCommand = " " + SortCommand.CATEGORY_MONEYFLOW + " "
+ SortCommand.DESCENDING_CONDITION + " ";
assertParseSuccess(parser, inputCommand, command);
}


@Test
public void parse_noFieldsPresent_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE);
String inputCommand = "";

assertParseFailure(parser, inputCommand, expectedMessage);
}

@Test
public void parse_invalidFieldPresent_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE);
String inputCommand = "invalidword";
assertParseFailure(parser, inputCommand, expectedMessage);

expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE);
inputCommand = SortCommand.ASCENDING_CONDITION + " " + "invalidword";
assertParseFailure(parser, inputCommand, expectedMessage);
}

@Test
public void parse_tooManyFieldsPresent_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE);
String inputCommand = SortCommand.DESCENDING_CONDITION + " " + SortCommand.CATEGORY_DATE + " "
+ "unnecessarystring" + " " + "morestrings";
assertParseFailure(parser, inputCommand, expectedMessage);
}
}
2 changes: 1 addition & 1 deletion src/test/java/seedu/planner/testutil/TypicalLimits.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TypicalLimits {
.withMoneyFlow("-500").build();
public static final Limit LIMIT_SINGLE_DIFF_DATE = new LimitBuilder().withSingleDate("08-05-2018")
.withMoneyFlow("-100").build();
public static final Limit LIMIT_SINGLE_DATE_All_DIFF = new LimitBuilder().withSingleDate("07-03-2018")
public static final Limit LIMIT_SINGLE_DATE_ALL_DIFF = new LimitBuilder().withSingleDate("07-03-2018")
.withMoneyFlow("-300").build();
public static final Limit LIMIT_100 = new LimitBuilder().withDateStart("01-02-2018").withDateEnd("5-02-2018")
.withMoneyFlow("-100").build();
Expand Down

0 comments on commit ed57a14

Please sign in to comment.