Skip to content

Commit

Permalink
Merge pull request #60 from ddx-510/V13_Add_test
Browse files Browse the repository at this point in the history
V13 add test for Tag commands
  • Loading branch information
ddx authored Mar 22, 2022
2 parents b6ba0c8 + f05320d commit 76b1f11
Show file tree
Hide file tree
Showing 15 changed files with 706 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

public class AddTagCommandParser {
public class AddTagCommandParser implements Parser<AddTagCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public boolean equals(Object obj) {
ModelManager other = (ModelManager) obj;
return addressBook.equals(other.addressBook)
&& userPrefs.equals(other.userPrefs)
&& filteredPersons.equals(other.filteredPersons);
&& filteredPersons.equals(other.filteredPersons)
&& tags.equals(other.tags);
}

}
221 changes: 221 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddTagCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;
import seedu.address.testutil.TagBuilder;



public class AddTagCommandTest {
@Test
public void constructor_nullTag_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddTagCommand(null));
}

@Test
public void execute_tagAcceptedByModel_addSuccessful() throws Exception {
AddTagCommandTest.ModelStubAcceptingTagAdded modelStub = new AddTagCommandTest.ModelStubAcceptingTagAdded();
Tag validTag = new TagBuilder().build();

CommandResult commandResult = new AddTagCommand(validTag).execute(modelStub);

assertEquals(String.format(AddTagCommand.MESSAGE_SUCCESS, validTag), commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validTag), modelStub.tagsAdded);
}

@Test
public void execute_duplicateTag_throwsCommandException() {
Tag validTag = new TagBuilder().build();
AddTagCommand addTagCommand = new AddTagCommand(validTag);
AddTagCommandTest.ModelStub modelStub = new AddTagCommandTest.ModelStubWithTag(validTag);

assertThrows(CommandException.class, AddTagCommand.MESSAGE_DUPLICATE_TAG, () ->
addTagCommand.execute(modelStub));
}

@Test
public void equals() {
Tag aTag = new TagBuilder().withTagName("ATag").build();
Tag bTag = new TagBuilder().withTagName("BTag").build();
AddTagCommand addATagCommand = new AddTagCommand(aTag);
AddTagCommand addBTagCommand = new AddTagCommand(bTag);

// same object -> returns true
assertTrue(addATagCommand.equals(addATagCommand));

// same values -> returns true
AddTagCommand addATagCommandCopy = new AddTagCommand(aTag);
assertTrue(addATagCommand.equals(addATagCommandCopy));

// different types -> returns false
assertFalse(addATagCommand.equals(1));

// null -> returns false
assertFalse(addATagCommand.equals(null));

// different tag -> returns false
assertFalse(addATagCommand.equals(addBTagCommand));
}

/**
* A default model stub that have all of the methods failing.
*/
private class ModelStub implements Model {
@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
throw new AssertionError("This method should not be called.");
}

@Override
public ReadOnlyUserPrefs getUserPrefs() {
throw new AssertionError("This method should not be called.");
}

@Override
public GuiSettings getGuiSettings() {
throw new AssertionError("This method should not be called.");
}

@Override
public void setGuiSettings(GuiSettings guiSettings) {
throw new AssertionError("This method should not be called.");
}

@Override
public Path getAddressBookFilePath() {
throw new AssertionError("This method should not be called.");
}

@Override
public void setAddressBookFilePath(Path addressBookFilePath) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addPerson(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addTag(Tag tag) {

}

@Override
public void setAddressBook(ReadOnlyAddressBook newData) {
throw new AssertionError("This method should not be called.");
}

@Override
public ReadOnlyAddressBook getAddressBook() {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasPerson(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasTag(Tag tag) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deletePerson(Person target) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deleteTag(Tag target) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setPerson(Person target, Person editedTag) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setTag(Tag target, Tag editedTag) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Person> getFilteredPersonList() {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Tag> getTagList() {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}
}

/**
* A Model stub that contains a single tag.
*/
private class ModelStubWithTag extends AddTagCommandTest.ModelStub {
private final Tag tag;

ModelStubWithTag(Tag tag) {
requireNonNull(tag);
this.tag = tag;
}

@Override
public boolean hasTag(Tag tag) {
requireNonNull(tag);
return this.tag.isSameTag(tag);
}
}

/**
* A Model stub that always accept the tag being added.
*/
private class ModelStubAcceptingTagAdded extends AddTagCommandTest.ModelStub {
final ArrayList<Tag> tagsAdded = new ArrayList<>();

@Override
public boolean hasTag(Tag tag) {
requireNonNull(tag);
return tagsAdded.stream().anyMatch(tag::isSameTag);
}

@Override
public void addTag(Tag tag) {
requireNonNull(tag);
tagsAdded.add(tag);
}

@Override
public ReadOnlyAddressBook getAddressBook() {
return new AddressBook();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class CommandTestUtil {
public static final String INVALID_EMAIL_DESC = " " + PREFIX_EMAIL + "bob!yahoo"; // missing '@' symbol
public static final String INVALID_ADDRESS_DESC = " " + PREFIX_ADDRESS; // empty string not allowed for addresses
public static final String INVALID_TAG_DESC = " " + PREFIX_TAG + "hubby*"; // '*' not allowed in tags
public static final String INVALID_TAG_DESC_FRIEND = " " + PREFIX_TAG + "friend*"; // '*' not allowed in tags

public static final String PREAMBLE_WHITESPACE = "\t \r \n";
public static final String PREAMBLE_NON_EMPTY = "NonEmptyPreamble";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_TAG;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_TAG;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.HashSet;
import java.util.Set;

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;


/**
* Contains integration tests (interaction with the Model) and unit tests for
* {@code DeleteCommand}.
*/
public class DeleteTagCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_validIndexUnfilteredList_success() {
Tag tagToDelete = model.getTagList().get(INDEX_FIRST_TAG.getZeroBased());
DeleteTagCommand deleteTagCommand = new DeleteTagCommand(INDEX_FIRST_TAG);

String expectedMessage = String.format(DeleteTagCommand.MESSAGE_DELETE_TAG_SUCCESS, tagToDelete);

ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
// delete tag
expectedModel.deleteTag(tagToDelete);
// delete tag from each person
for (Person currPerson : expectedModel.getAddressBook().getPersonList()) {
Set<Tag> tempTags = currPerson.getTags();
Set<Tag> tagCopy = new HashSet<>(tempTags);
tagCopy.removeIf(t -> t.isSameTag(tagToDelete));
Person newPerson = new Person(currPerson.getName(), currPerson.getPhone(),
currPerson.getEmail(), currPerson.getAddress(), tagCopy);
expectedModel.setPerson(currPerson, newPerson);
}
assertCommandSuccess(deleteTagCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_invalidIndexUnfilteredList_throwsCommandException() {
Index outOfBoundIndex = Index.fromOneBased(model.getTagList().size() + 1);
DeleteTagCommand deleteTagCommand = new DeleteTagCommand(outOfBoundIndex);

assertCommandFailure(deleteTagCommand, model, Messages.MESSAGE_INVALID_TAG_DISPLAYED_INDEX);
}

@Test
public void equals() {
DeleteTagCommand deleteFirstTagCommand = new DeleteTagCommand(INDEX_FIRST_TAG);
DeleteTagCommand deleteSecondTagCommand = new DeleteTagCommand(INDEX_SECOND_TAG);

// same object -> returns true
assertTrue(deleteFirstTagCommand.equals(deleteFirstTagCommand));

// same values -> returns true
DeleteTagCommand deleteFirstTagCommandCopy = new DeleteTagCommand(INDEX_FIRST_TAG);
assertTrue(deleteFirstTagCommand.equals(deleteFirstTagCommandCopy));

// different types -> returns false
assertFalse(deleteFirstTagCommand.equals(1));

// null -> returns false
assertFalse(deleteFirstTagCommand.equals(null));

// different tag -> returns false
assertFalse(deleteFirstTagCommand.equals(deleteSecondTagCommand));
}

/**
* Updates {@code model}'s filtered list to show no one.
*/
private void showNoTag(Model model) {
assertTrue(model.getTagList().isEmpty());
}
}
41 changes: 41 additions & 0 deletions src/test/java/seedu/address/logic/commands/ListTagCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.List;

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

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.tag.Tag;

/**
* Contains integration tests (interaction with the Model) and unit tests for ListTagCommand.
*/
public class ListTagCommandTest {

private Model model;
private Model expectedModel;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
}

@Test
public void execute_listIsNotFiltered_showsSameList() {
// get success message in String
List<Tag> tags = expectedModel.getTagList();
StringBuilder result = new StringBuilder(ListTagCommand.MESSAGE_SUCCESS);
for (Tag tag : tags) {
result.append(" ").append(tag.getTagName());
}
result.append(" ]");
assertCommandSuccess(new ListTagCommand(), model, result.toString(), expectedModel);
}
}
Loading

0 comments on commit 76b1f11

Please sign in to comment.