forked from nus-cs2103-AY2122S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from ddx-510/V13_Add_test
V13 add test for Tag commands
- Loading branch information
Showing
15 changed files
with
706 additions
and
3 deletions.
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
221 changes: 221 additions & 0 deletions
221
src/test/java/seedu/address/logic/commands/AddTagCommandTest.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,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(); | ||
} | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
src/test/java/seedu/address/logic/commands/DeleteTagCommandTest.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,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
41
src/test/java/seedu/address/logic/commands/ListTagCommandTest.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,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); | ||
} | ||
} |
Oops, something went wrong.