Skip to content

Commit

Permalink
Update and create test files for Note function
Browse files Browse the repository at this point in the history
Update and create test files for Note function for testing purposes.

Remarks: Refer to issue #48 for more info
  • Loading branch information
Darren-Tung committed Oct 9, 2024
1 parent 0d3132f commit 188a6e7
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 24 deletions.
20 changes: 10 additions & 10 deletions src/main/java/seedu/address/logic/commands/NoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@
import seedu.address.model.person.Note;

/**
* Changes the remark of an existing person in the address book.
* Changes the note of an existing person in the address book.
*/
public class NoteCommand extends Command {

public static final String COMMAND_WORD = "remark";
public static final String COMMAND_WORD = "note";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the remark of the person identified "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the note of the person identified "
+ "by the index number used in the last person listing. "
+ "Existing remark will be overwritten by the input.\n"
+ "Existing note will be overwritten by the input.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ PREFIX_NOTE + "[REMARK]\n"
+ PREFIX_NOTE + "[NOTE]\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_NOTE + "Likes to swim.";

public static final String MESSAGE_ADD_REMARK_SUCCESS = "Added remark to Person: %1$s";
public static final String MESSAGE_DELETE_REMARK_SUCCESS = "Removed remark from Person: %1$s";
public static final String MESSAGE_ADD_NOTE_SUCCESS = "Added note to Person: %1$s";
public static final String MESSAGE_DELETE_NOTE_SUCCESS = "Removed note from Person: %1$s";

private final Index index;
private final Note note;

/**
* @param index of the person in the filtered person list to edit the remark
* @param index of the person in the filtered person list to edit the note
* @param note of the person to be updated to
*/
public NoteCommand(Index index, Note note) {
Expand Down Expand Up @@ -64,11 +64,11 @@ public CommandResult execute(Model model) throws CommandException {
}

/**
* Generates a command execution success message based on whether the remark is added to or removed from
* Generates a command execution success message based on whether the note is added to or removed from
* {@code personToEdit}.
*/
private String generateSuccessMessage(Person personToEdit) {
String message = !note.value.isEmpty() ? MESSAGE_ADD_REMARK_SUCCESS : MESSAGE_DELETE_REMARK_SUCCESS;
String message = !note.value.isEmpty() ? MESSAGE_ADD_NOTE_SUCCESS : MESSAGE_DELETE_NOTE_SUCCESS;
return String.format(message, personToEdit);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"phone": "94351253",
"email": "[email protected]",
"address": "123, Jurong West Ave 6, #08-111",
"note": "",
"tags": [ "friends" ]
}, {
"name": "Alice Pauline",
"phone": "94351253",
"email": "[email protected]",
"address": "4th street"
"address": "4th street",
"note": ""
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,49 @@
"phone" : "94351253",
"email" : "[email protected]",
"address" : "123, Jurong West Ave 6, #08-111",
"note": "",
"tags" : [ "friends" ]
}, {
"name" : "Benson Meier",
"phone" : "98765432",
"email" : "[email protected]",
"address" : "311, Clementi Ave 2, #02-25",
"note": "",
"tags" : [ "owesMoney", "friends" ]
}, {
"name" : "Carl Kurz",
"phone" : "95352563",
"email" : "[email protected]",
"address" : "wall street",
"note": "",
"tags" : [ ]
}, {
"name" : "Daniel Meier",
"phone" : "87652533",
"email" : "[email protected]",
"address" : "10th street",
"note": "",
"tags" : [ "friends" ]
}, {
"name" : "Elle Meyer",
"phone" : "9482224",
"email" : "[email protected]",
"address" : "michegan ave",
"note": "",
"tags" : [ ]
}, {
"name" : "Fiona Kunz",
"phone" : "9482427",
"email" : "[email protected]",
"address" : "little tokyo",
"note": "",
"tags" : [ ]
}, {
"name" : "George Best",
"phone" : "9482442",
"email" : "[email protected]",
"address" : "4th street",
"note": "",
"tags" : [ ]
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class CommandTestUtil {
public static final String VALID_EMAIL_BOB = "[email protected]";
public static final String VALID_ADDRESS_AMY = "Block 312, Amy Street 1";
public static final String VALID_ADDRESS_BOB = "Block 123, Bobby Street 3";
public static final String VALID_NOTE_AMY = "Super confident";
public static final String VALID_NOTE_BOB = "Amazing energy and charisma";
public static final String VALID_TAG_HUSBAND = "husband";
public static final String VALID_TAG_FRIEND = "friend";

Expand Down
134 changes: 134 additions & 0 deletions src/test/java/seedu/address/logic/commands/NoteCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
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.VALID_NOTE_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.model.AddressBook;
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.person.Note;
import seedu.address.testutil.PersonBuilder;

/**
* Contains integration tests (interaction with the Model) and unit tests for NoteCommand.
*/
public class NoteCommandTest {
private static final String NOTE_STUB = "Some note";

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

@Test
public void execute_addNoteUnfilteredList_success() {
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(firstPerson).withNote(NOTE_STUB).build();

NoteCommand noteCommand = new NoteCommand(INDEX_FIRST_PERSON, new Note(editedPerson.getNote().value));

String expectedMessage = String.format(NoteCommand.MESSAGE_ADD_NOTE_SUCCESS, editedPerson);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.setPerson(firstPerson, editedPerson);

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

@Test
public void execute_deleteNoteUnfilteredList_success() {
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(firstPerson).withNote("").build();

NoteCommand noteCommand = new NoteCommand(INDEX_FIRST_PERSON,
new Note(editedPerson.getNote().toString()));

String expectedMessage = String.format(NoteCommand.MESSAGE_DELETE_NOTE_SUCCESS, editedPerson);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.setPerson(firstPerson, editedPerson);

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

@Test
public void execute_filteredList_success() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);

Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()))
.withNote(NOTE_STUB).build();

NoteCommand noteCommand = new NoteCommand(INDEX_FIRST_PERSON, new Note(editedPerson.getNote().value));

String expectedMessage = String.format(NoteCommand.MESSAGE_ADD_NOTE_SUCCESS, editedPerson);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.setPerson(firstPerson, editedPerson);

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

@Test
public void execute_invalidPersonIndexUnfilteredList_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1);
NoteCommand noteCommand = new NoteCommand(outOfBoundIndex, new Note(VALID_NOTE_BOB));

assertCommandFailure(noteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

/**
* Edit filtered list where index is larger than size of filtered list,
* but smaller than size of address book
*/
@Test
public void execute_invalidPersonIndexFilteredList_failure() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);
Index outOfBoundIndex = INDEX_SECOND_PERSON;
// ensures that outOfBoundIndex is still in bounds of address book list
assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size());

NoteCommand noteCommand = new NoteCommand(outOfBoundIndex, new Note(VALID_NOTE_BOB));

assertCommandFailure(noteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void equals() {
final NoteCommand standardCommand = new NoteCommand(INDEX_FIRST_PERSON,
new Note(VALID_NOTE_AMY));

// same values -> returns true
NoteCommand commandWithSameValues = new NoteCommand(INDEX_FIRST_PERSON,
new Note(VALID_NOTE_AMY));
assertTrue(standardCommand.equals(commandWithSameValues));

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

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

// different types -> returns false
assertFalse(standardCommand.equals(new ClearCommand()));

// different index -> returns false
assertFalse(standardCommand.equals(new NoteCommand(INDEX_SECOND_PERSON,
new Note(VALID_NOTE_AMY))));

// different note -> returns false
assertFalse(standardCommand.equals(new NoteCommand(INDEX_FIRST_PERSON,
new Note(VALID_NOTE_BOB))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

Expand All @@ -22,9 +23,11 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.NoteCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.model.person.Note;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.PersonBuilder;
import seedu.address.testutil.PersonUtil;
Expand Down Expand Up @@ -88,6 +91,14 @@ public void parseCommand_list() throws Exception {
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand);
}

@Test
public void parseCommand_remark() throws Exception {
final Note note = new Note("Some remark.");
NoteCommand command = (NoteCommand) parser.parseCommand(NoteCommand.COMMAND_WORD + " "
+ INDEX_FIRST_PERSON.getOneBased() + " " + PREFIX_NOTE + note.value);
assertEquals(new NoteCommand(INDEX_FIRST_PERSON, note), command);
}

@Test
public void parseCommand_unrecognisedInput_throwsParseException() {
assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), ()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.NoteCommand;
import seedu.address.model.person.Note;

public class RemarkCommandParserTest {
private NoteCommandParser parser = new NoteCommandParser();
private final String nonEmptyRemark = "Some remark.";

@Test
public void parse_indexSpecified_success() {
// have note
Index targetIndex = INDEX_FIRST_PERSON;
String userInput = targetIndex.getOneBased() + " " + PREFIX_NOTE + nonEmptyRemark;
NoteCommand expectedCommand = new NoteCommand(INDEX_FIRST_PERSON, new Note(nonEmptyRemark));
assertParseSuccess(parser, userInput, expectedCommand);

// no note
userInput = targetIndex.getOneBased() + " " + PREFIX_NOTE;
expectedCommand = new NoteCommand(INDEX_FIRST_PERSON, new Note(""));
assertParseSuccess(parser, userInput, expectedCommand);
}

@Test
public void parse_missingCompulsoryField_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, NoteCommand.MESSAGE_USAGE);

// no parameters
assertParseFailure(parser, NoteCommand.COMMAND_WORD, expectedMessage);

// no index
assertParseFailure(parser, NoteCommand.COMMAND_WORD + " " + nonEmptyRemark, expectedMessage);
}
}
31 changes: 31 additions & 0 deletions src/test/java/seedu/address/model/person/NoteTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.address.model.person;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class NoteTest {

@Test
public void equals() {
Note note = new Note("Hello");

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

// same values -> returns true
Note noteCopy = new Note(note.value);
assertTrue(note.equals(noteCopy));

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

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

// different note -> returns false
Note differentNote = new Note("Bye");
assertFalse(note.equals(differentNote));
}
}
6 changes: 4 additions & 2 deletions src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.ALICE;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void isSamePerson() {

// same name, all other attributes different -> returns true
Person editedAlice = new PersonBuilder(ALICE).withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB)
.withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_HUSBAND).build();
.withAddress(VALID_ADDRESS_BOB).withNote(VALID_NOTE_BOB).withTags(VALID_TAG_HUSBAND).build();
assertTrue(ALICE.isSamePerson(editedAlice));

// different name, all other attributes same -> returns false
Expand Down Expand Up @@ -93,7 +94,8 @@ public void equals() {
@Test
public void toStringMethod() {
String expected = Person.class.getCanonicalName() + "{name=" + ALICE.getName() + ", phone=" + ALICE.getPhone()
+ ", email=" + ALICE.getEmail() + ", address=" + ALICE.getAddress() + ", tags=" + ALICE.getTags() + "}";
+ ", email=" + ALICE.getEmail() + ", address=" + ALICE.getAddress() + ", note=" + ALICE.getNote()
+ ", tags=" + ALICE.getTags() + "}";
assertEquals(expected, ALICE.toString());
}
}
Loading

0 comments on commit 188a6e7

Please sign in to comment.