Skip to content

Commit

Permalink
Merge pull request #189 from koh-jx/add-Tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
mingyi456 authored Nov 4, 2021
2 parents fc864e9 + 524c85c commit d733c65
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is out of bounds";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d person(s) listed!";
public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The task index provided is out of bounds";
public static final String MESSAGE_TASKS_LISTED_OVERVIEW = "%1$d tasks listed!";
public static final String MESSAGE_TASKS_LISTED_OVERVIEW = "%1$d task(s) listed!";
public static final String MESSAGE_UNABLE_TO_EXECUTE = "This command has already been executed!";
public static final String MESSAGE_UNABLE_TO_UNDO = "This command has already been undone!";

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class FindCommand extends UndoableCommand {
);

private final NameContainsKeywordsPredicate predicate;

private Predicate<? super Person> previousPredicate;
private String displayedMessage;

/**
* Constructor to initialise a FindCommand object.
Expand All @@ -46,15 +46,15 @@ protected CommandResult executeDo(Model model) {
requireNonNull(model);
this.previousPredicate = model.getFilteredPersonPredicate();
model.updateFilteredPersonList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
displayedMessage = String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW,
model.getFilteredPersonList().size());
return new CommandResult(displayedMessage);
}

@Override
protected CommandResult executeUndo(Model model) {
model.updateFilteredPersonList(this.previousPredicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
return new CommandResult(displayedMessage);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PREAMBLE;
import static seedu.address.logic.parser.CommandArgument.filled;
import static seedu.address.logic.parser.CommandArgument.requiredSingle;
import static seedu.address.logic.parser.CommandArgument.optionalSingle;

import java.util.Objects;
import java.util.Optional;

import seedu.address.commons.core.Messages;
Expand All @@ -26,13 +27,14 @@ public class FindTaskCommand extends TaskCommand {
FULL_COMMAND_WORD,
"Finds all tasks whose names or description contain any of the specified keywords (case-insensitive)\n"
+ "and displays them as a list with index numbers.",
requiredSingle(PREFIX_PREAMBLE, "KEYWORD [MORE_KEYWORDS]...")
optionalSingle(PREFIX_PREAMBLE, "KEYWORD [MORE_KEYWORDS]...")
).withExample(
filled(PREFIX_PREAMBLE, "CS2103 CS2106 PC3130")
);

private final TaskContainsKeywordsPredicate predicate;
private TaskFilter prevPredicate;
private String displayedString;

/**
* Contructor for command. Sets previous predicate to null.
Expand Down Expand Up @@ -67,8 +69,9 @@ protected CommandResult executeDo(Model model) throws CommandException {
model.addTaskFilter(TaskFilters.FILTER_KEYWORDS.apply(predicate));
}

return new CommandResult(String.format(Messages.MESSAGE_TASKS_LISTED_OVERVIEW,
model.getFilteredTaskList().size()));
displayedString = String.format(Messages.MESSAGE_TASKS_LISTED_OVERVIEW,
model.getFilteredTaskList().size());
return new CommandResult(displayedString);
}

@Override
Expand All @@ -77,9 +80,32 @@ protected CommandResult executeUndo(Model model) throws CommandException {
.filter(filter -> filter instanceof KeywordTaskFilter)
.findFirst().ifPresent(model::removeTaskFilter);

Optional.of(prevPredicate).ifPresent(model::addTaskFilter);
Optional.ofNullable(prevPredicate).ifPresent(model::addTaskFilter);

return new CommandResult(String.format(Messages.MESSAGE_TASKS_LISTED_OVERVIEW,
model.getFilteredTaskList().size()));
return new CommandResult(displayedString);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof FindTaskCommand)) {
return false;
}

// state check
FindTaskCommand e = (FindTaskCommand) other;
boolean isPrevPredicateEquals = Optional.ofNullable(prevPredicate)
.equals(Optional.ofNullable(e.prevPredicate));
return predicate.equals(e.predicate) && isPrevPredicateEquals;
}

@Override
public int hashCode() {
return Objects.hash(predicate, prevPredicate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CONTACT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
Expand Down Expand Up @@ -82,6 +83,9 @@ public class CommandTestUtil {
public static final String TIMESTAMP_DESC_REPORT = " " + PREFIX_TIMESTAMP + "31-12-2021";
public static final String TITLE_DESC_REPORT = " " + PREFIX_TITLE + VALID_TITLE_REPORT;

public static final String CONTACT_DESC_AMY = " " + PREFIX_CONTACT + VALID_NAME_AMY;
public static final String CONTACT_DESC_BOB = " " + PREFIX_CONTACT + VALID_NAME_BOB;

public static final EditTaskCommand.EditTaskDescriptor DESC_REPORT;
public static final EditTaskCommand.EditTaskDescriptor DESC_INTERVIEW;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@ void execute_validTask_taskAdded() {

assertCommandSuccess(addTaskCommand, model, expectedMessage, expectedModel);
}

@Test
void execute_validTaskMultipleContactsTags_taskAdded() {
Task task = new TaskBuilder()
.withContacts("Alice", "Bob")
.withTags("Tag1", "Tag2").build();
AddTaskCommand addTaskCommand = new AddTaskCommand(task);
String expectedMessage = String.format(MESSAGE_SUCCESS, task);
Model expectedModel = new ModelManager(
new AddressBook(model.getAddressBook()), new TaskList(model.getTaskList()), new UserPrefs());
expectedModel.addTask(task);

assertCommandSuccess(addTaskCommand, model, expectedMessage, expectedModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.address.logic.commands.CommandTestUtil.DESC_INTERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.DESC_REPORT;
import static seedu.address.logic.commands.CommandTestUtil.VALID_DESCRIPTION_INTERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_CAREER;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TIMESTAMP_INTERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TITLE_INTERVIEW;
Expand Down Expand Up @@ -53,5 +54,10 @@ public void equals() {
editedReport = new EditTaskDescriptorBuilder(DESC_REPORT)
.withTags(VALID_TAG_CAREER).build();
assertFalse(DESC_REPORT.equals(editedReport));

// different contacts -> returns false
editedReport = new EditTaskDescriptorBuilder(DESC_REPORT)
.withContacts(VALID_NAME_AMY).build();
assertFalse(DESC_REPORT.equals(editedReport));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.Test;

import seedu.address.commons.core.Messages;
import seedu.address.logic.commands.UndoCommand;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
Expand Down Expand Up @@ -54,6 +55,24 @@ void execute_zeroKeywords_allTasksFound() {
assertCommandSuccess(testCommand, model, expectedMessage, expectedModel);
}

@Test
void undo_singleKeywordWithExistingFilter_success() {
Model originalModel = new ModelManager(getTypicalAddressBook(), getTypicalTaskList(), new UserPrefs());

String expectedMessage = String.format(Messages.MESSAGE_TASKS_LISTED_OVERVIEW, 1);
TaskContainsKeywordsPredicate testPredicate = preparePredicate("homework");
FindTaskCommand findTaskCommand = new FindTaskCommand(testPredicate);
expectedModel.updateFilteredTaskList(testPredicate);

assertCommandSuccess(findTaskCommand, model, expectedMessage, expectedModel);
model.getCommandHistory().pushCommand(findTaskCommand);

String successMessage = UndoCommand.MESSAGE_UNDO_SUCCESS + expectedMessage;
assertCommandSuccess(new UndoCommand(), model, successMessage, originalModel);

}



/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import static org.junit.jupiter.api.Assertions.fail;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PREAMBLE;
import static seedu.address.logic.parser.CommandArgument.optionalMultiple;
import static seedu.address.logic.parser.CommandArgument.optionalSingle;
import static seedu.address.logic.parser.CommandArgument.requiredSingle;

import org.junit.jupiter.api.Test;

import seedu.address.logic.parser.exceptions.ArgumentContainsSlashException;
import seedu.address.logic.parser.exceptions.MissingCommandArgumentException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.exceptions.TooManyPrefixesException;
Expand All @@ -31,6 +33,23 @@ public void tokenize_expectingOptionalPreambleValidArguments_success() {
}
}

@Test
public void tokenize_expectingOptionalMultipleValidArguments_success() {
ArgumentTokenizer tokenizer = new ArgumentTokenizer(new CommandSpecification(
null, null,
optionalMultiple(prefix)
));
try {
tokenizer.tokenize(prefix.getPrefix() + "");
tokenizer.tokenize(prefix.getPrefix() + "single");
tokenizer.tokenize(prefix.getPrefix() + "multiple words");
tokenizer.tokenize(prefix.getPrefix() + "multiple"
+ " " + prefix.getPrefix() + "arguments");
} catch (ParseException e) {
fail(e);
}
}

@Test
public void tokenize_expectingEmptyPreambleValidArguments_success() {
ArgumentTokenizer tokenizer = new ArgumentTokenizer(new CommandSpecification(
Expand Down Expand Up @@ -158,4 +177,20 @@ public void tokenize_unexpectedArgument_throwUnwantedArgumentException() {
fail(e);
}
}

@Test
public void tokenize_unexpectedArgument_throwArgumentContainsSlashException() {
ArgumentTokenizer tokenizer = new ArgumentTokenizer(new CommandSpecification(
null, null,
requiredSingle(prefix)
));

try {
tokenizer.tokenize(prefix.getPrefix() + "first/second");
} catch (ArgumentContainsSlashException e) {
// This is expected
} catch (ParseException e) {
fail(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package seedu.address.logic.parser.task;

import static seedu.address.logic.commands.CommandTestUtil.CONTACT_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.CONTACT_DESC_BOB;
import static seedu.address.logic.commands.CommandTestUtil.DESCRIPTION_DESC_REPORT;
import static seedu.address.logic.commands.CommandTestUtil.TAG_DESC_CAREER;
import static seedu.address.logic.commands.CommandTestUtil.TIMESTAMP_DESC_REPORT;
import static seedu.address.logic.commands.CommandTestUtil.VALID_DESCRIPTION_REPORT;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_CAREER;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TIMESTAMP_REPORT;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TITLE_REPORT;
Expand Down Expand Up @@ -52,10 +56,12 @@ void parse_allFieldsPresent_success() {
.withDescription(VALID_DESCRIPTION_REPORT)
.withTimestamp(VALID_TIMESTAMP_REPORT)
.withTags(VALID_TAG_CAREER)
.withContacts(VALID_NAME_AMY, VALID_NAME_BOB)
.build();
AddTaskCommand expectedCommand = new AddTaskCommand(expectedTask);
assertParseSuccess(parser,
VALID_TITLE_REPORT + DESCRIPTION_DESC_REPORT + TIMESTAMP_DESC_REPORT + TAG_DESC_CAREER,
VALID_TITLE_REPORT + DESCRIPTION_DESC_REPORT + TIMESTAMP_DESC_REPORT + TAG_DESC_CAREER
+ CONTACT_DESC_AMY + CONTACT_DESC_BOB,
expectedCommand);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.address.logic.parser.task;

import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import java.util.Arrays;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.task.FindTaskCommand;
import seedu.address.model.task.TaskContainsKeywordsPredicate;

class FindTaskCommandParserTest {
private final FindTaskCommandParser parser = new FindTaskCommandParser();

@Test
public void parse_noArgument_success() {
assertParseSuccess(parser, " ",
new FindTaskCommand(TaskContainsKeywordsPredicate.SHOW_ALL_TASKS_PREDICATE));
}

@Test
public void parse_validArgs_returnsFindCommand() {
FindTaskCommand expectedFindTaskCommand =
new FindTaskCommand(new TaskContainsKeywordsPredicate(Arrays.asList("Homework", "Meeting")));
assertParseSuccess(parser, "Homework Meeting", expectedFindTaskCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, " \n Homework \n \t Meeting \t", expectedFindTaskCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.util.stream.Stream;

import seedu.address.logic.commands.task.EditTaskCommand;
import seedu.address.model.person.Name;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.Contact;
import seedu.address.model.task.Task;
import seedu.address.model.task.Timestamp;

Expand All @@ -30,6 +32,7 @@ public EditTaskDescriptorBuilder(Task task) {
descriptor.setDescription(task.getDescription().orElse(null));
descriptor.setTimestamp(task.getTimestamp().orElse(null));
descriptor.setTags(task.getTags());
descriptor.setContacts(task.getContacts());
}

/**
Expand Down Expand Up @@ -66,6 +69,16 @@ public EditTaskDescriptorBuilder withTags(String... tags) {
return this;
}

/**
* Parses the {@code tags} into a {@code Set<Tag>} and set it to the {@code EditTaskDescriptor}
* that we are building.
*/
public EditTaskDescriptorBuilder withContacts(String... contacts) {
Set<Contact> nameSet = Stream.of(contacts).map(name -> new Contact(new Name(name))).collect(Collectors.toSet());
descriptor.setContacts(nameSet);
return this;
}

public EditTaskCommand.EditTaskDescriptor build() {
return descriptor;
}
Expand Down

0 comments on commit d733c65

Please sign in to comment.