Skip to content

Commit

Permalink
Merge pull request #100 from Pinran-J/add-testcases-notes
Browse files Browse the repository at this point in the history
Add testcases for notes, updated deleteNote/editNote implementation
  • Loading branch information
Pinran-J authored Oct 27, 2022
2 parents ef26f39 + 94150c3 commit 2591db4
Show file tree
Hide file tree
Showing 11 changed files with 816 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import static java.util.Objects.requireNonNull;

import javafx.collections.ObservableList;
import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
Expand Down Expand Up @@ -32,13 +33,13 @@ public DeleteNoteCommand(Index targetIndex) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
ObservableList<Note> notebook = model.getAddressBook().getNoteBook();
List<Note> lastShownList = model.getFilteredNoteList();

if (targetIndex.getZeroBased() >= notebook.size()) {
if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_NOTE_DISPLAYED_INDEX);
}

Note noteToDelete = notebook.get(targetIndex.getZeroBased());
Note noteToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deleteNote(noteToDelete);

// Remove unused tags from UniqueTagMapping
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/logic/commands/EditNoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ private static Note createEditedNote(Note noteToEdit, EditNoteDescriptor editNot
return new Note(updatedTitle, updatedContent, updatedTags);
}

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

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

// state check
EditNoteCommand e = (EditNoteCommand) other;
return index.equals(e.index)
&& editNoteDescriptor.equals(e.editNoteDescriptor);
}


/**
* Stores the details to edit the note with. Each non-empty field value will replace the
Expand Down
240 changes: 240 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddNoteCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,69 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
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.List;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddCommandParser;
import seedu.address.logic.parser.AddNoteCommandParser;
import seedu.address.logic.parser.CliSyntax;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.note.Note;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;
import seedu.address.testutil.NoteBuilder;
import seedu.address.testutil.PersonBuilder;

public class AddNoteCommandTest {

@Test
public void constructor_nullNote_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddNoteCommand(null));
}

@Test
public void execute_noteAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingNoteAdded modelStub = new ModelStubAcceptingNoteAdded();
Note validNote = new NoteBuilder().build();

CommandResult commandResult = new AddNoteCommand(validNote).execute(modelStub);

assertEquals(String.format(AddNoteCommand.MESSAGE_SUCCESS, validNote), commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validNote), modelStub.notesAdded);

}

@Test
public void execute_duplicateNote_throwsCommandException() {
Note validNote = new NoteBuilder().build();
AddNoteCommand addNoteCommand = new AddNoteCommand(validNote);
ModelStub modelStub = new ModelStubWithNote(validNote);

assertThrows(CommandException.class, AddNoteCommand.MESSAGE_DUPLICATE_NOTE, () ->
addNoteCommand.execute(modelStub));
}

@Test
public void execute_addNoteWithTag_addsTagIntoTagMapping() {
Model model = new ModelManager();
Expand Down Expand Up @@ -86,4 +129,201 @@ public void execute_addNoteWithTag_tagAlreadyExistsInTagMappingDueToPerson() thr
assertSame(tagFromTagMapping, tagFromNotes);
}

@Test
public void equals() {
Note meeting = new NoteBuilder().withTitle("Meeting").build();
Note event = new NoteBuilder().withTitle("Event").build();
AddNoteCommand addMeetingNoteCommand = new AddNoteCommand(meeting);
AddNoteCommand addEventNoteCommand = new AddNoteCommand(event);


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

// same values -> returns true
AddNoteCommand addMeetingNoteCommandCopy = new AddNoteCommand(meeting);
assertTrue(addMeetingNoteCommand.equals(addMeetingNoteCommandCopy));

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

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

// different person -> returns false
assertFalse(addMeetingNoteCommand.equals(addEventNoteCommand));
}

/**
* 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 addNote(Note note) {
throw new AssertionError("This method should not be called.");
}

@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 hasNote(Note note) {
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 deleteNote(Note target) {
throw new AssertionError("This method should not be called.");
}

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

@Override
public void setNote(Note target, Note editedNote) {
throw new AssertionError("This method should not be called.");
}

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

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

@Override
public ObservableMap<String, Tag> getTagMapping() {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean notebookContainsTag(Tag tag) {
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<Note> getFilteredNoteList() {
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.");
}

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

/**
* A Model stub that contains a single existing note.
*/
private class ModelStubWithNote extends ModelStub {
private final Note note;

ModelStubWithNote(Note note) {
requireNonNull(note);
this.note = note;
}

@Override
public boolean hasNote(Note note) {
requireNonNull(note);
return this.note.isSameNote(note);
}
}

/**
* A Model stub that always accept the note being added.
*/
private class ModelStubAcceptingNoteAdded extends ModelStub {
final ArrayList<Note> notesAdded = new ArrayList<>();

@Override
public boolean hasNote(Note note) {
requireNonNull(note);
return notesAdded.stream().anyMatch(note::isSameNote);
}

@Override
public void addNote(Note note) {
requireNonNull(note);
notesAdded.add(note);
}

@Override
public ObservableMap<String, Tag> getTagMapping() {
return FXCollections.observableHashMap();
}

@Override
public ReadOnlyAddressBook getAddressBook() {
return new AddressBook();
}
}

}
Loading

0 comments on commit 2591db4

Please sign in to comment.