Skip to content

Commit

Permalink
Add More Tests for Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
josepholim committed Oct 16, 2023
1 parent b60311f commit dc5758a
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddNoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.note.Note;
Expand Down Expand Up @@ -42,4 +43,30 @@ public CommandResult execute(Model model) throws CommandException {

return new CommandResult(MESSAGE_SUCCESS + toAdd.getTitle());
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;

Check warning on line 50 in src/main/java/seedu/address/logic/commands/AddNoteCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AddNoteCommand.java#L50

Added line #L50 was not covered by tests
}

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

Check warning on line 55 in src/main/java/seedu/address/logic/commands/AddNoteCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AddNoteCommand.java#L55

Added line #L55 was not covered by tests
}

AddNoteCommand otherAddNoteCommand = (AddNoteCommand) other;

boolean equalToAdd = toAdd.equals(otherAddNoteCommand.toAdd);
boolean equalContactId = (contactId == otherAddNoteCommand.contactId);
return equalToAdd && equalContactId;
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", toAdd)
.add("contactId", contactId)
.toString();

Check warning on line 70 in src/main/java/seedu/address/logic/commands/AddNoteCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AddNoteCommand.java#L67-L70

Added lines #L67 - L70 were not covered by tests
}
}
27 changes: 27 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteNoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
Expand Down Expand Up @@ -42,4 +43,30 @@ public CommandResult execute(Model model) throws CommandException {

return new CommandResult(MESSAGE_SUCCESS + this.noteIdToDelete);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;

Check warning on line 50 in src/main/java/seedu/address/logic/commands/DeleteNoteCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/DeleteNoteCommand.java#L50

Added line #L50 was not covered by tests
}

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

Check warning on line 55 in src/main/java/seedu/address/logic/commands/DeleteNoteCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/DeleteNoteCommand.java#L55

Added line #L55 was not covered by tests
}

DeleteNoteCommand otherDeleteNoteCommand = (DeleteNoteCommand) other;

boolean equalNoteIdToDelete = (noteIdToDelete == otherDeleteNoteCommand.noteIdToDelete);
boolean equalContactId = (contactId == otherDeleteNoteCommand.contactId);
return equalNoteIdToDelete && equalContactId;
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("noteIdToDelete", noteIdToDelete)
.add("contactId", contactId)
.toString();

Check warning on line 70 in src/main/java/seedu/address/logic/commands/DeleteNoteCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/DeleteNoteCommand.java#L67-L70

Added lines #L67 - L70 were not covered by tests
}
}
20 changes: 20 additions & 0 deletions src/main/java/seedu/address/model/note/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@ public String getUiText() {
String result = this.getTitle() + " (content: " + this.getContent() + ")";
return result;
}

/**
* Returns true if both notes have the same identity and data fields.
* This defines a stronger notion of equality between two notes.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

Note otherNote = (Note) other;
return title.equals(otherNote.title)
&& content.equals(otherNote.content);
}
}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/note/NoteContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ public static NoteContent fromString(String content) {
return new NoteContent(content);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

NoteContent otherNoteContent = (NoteContent) other;
return content.equals(otherNoteContent.content);
}

@Override
public String toString() {
return this.content;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/note/NoteTitle.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ public static NoteTitle fromString(String title) {
return new NoteTitle(title);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

NoteTitle otherNoteTitle = (NoteTitle) other;
return title.equals(otherNoteTitle.title);
}

@Override
public String toString() {
return this.title;
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE_CONTENT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE_TITLE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PERSON_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.testutil.Assert.assertThrows;
Expand Down Expand Up @@ -36,6 +40,14 @@ public class CommandTestUtil {
public static final String VALID_ADDRESS_BOB = "Block 123, Bobby Street 3";
public static final String VALID_TAG_HUSBAND = "husband";
public static final String VALID_TAG_FRIEND = "friend";
public static final String VALID_NOTE_A_PERSON_ID = "1";
public static final String VALID_NOTE_A_TITLE = "Preferred Qualifications";
public static final String VALID_NOTE_A_CONTENT = "Machine Learning Frameworks";
public static final String VALID_NOTE_A_NOTE_ID = "1";
public static final String VALID_NOTE_B_PERSON_ID = "2";
public static final String VALID_NOTE_B_TITLE = "Preferred Major";
public static final String VALID_NOTE_B_CONTENT = "Computer Science";
public static final String VALID_NOTE_B_NOTE_ID = "1";

public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY;
public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB;
Expand All @@ -47,6 +59,14 @@ public class CommandTestUtil {
public static final String ADDRESS_DESC_BOB = " " + PREFIX_ADDRESS + VALID_ADDRESS_BOB;
public static final String TAG_DESC_FRIEND = " " + PREFIX_TAG + VALID_TAG_FRIEND;
public static final String TAG_DESC_HUSBAND = " " + PREFIX_TAG + VALID_TAG_HUSBAND;
public static final String NOTE_A_PERSON_ID_DESC = " " + PREFIX_PERSON_ID + VALID_NOTE_A_PERSON_ID;
public static final String NOTE_A_TITLE_DESC = " " + PREFIX_NOTE_TITLE + VALID_NOTE_A_TITLE;
public static final String NOTE_A_CONTENT_DESC = " " + PREFIX_NOTE_CONTENT + VALID_NOTE_A_CONTENT;
public static final String NOTE_A_NOTE_ID_DESC = " " + PREFIX_NOTE_ID + VALID_NOTE_A_NOTE_ID;
public static final String NOTE_B_PERSON_ID_DESC = " " + PREFIX_PERSON_ID + VALID_NOTE_B_PERSON_ID;
public static final String NOTE_B_TITLE_DESC = " " + PREFIX_NOTE_TITLE + VALID_NOTE_B_TITLE;
public static final String NOTE_B_CONTENT_DESC = " " + PREFIX_NOTE_CONTENT + VALID_NOTE_B_CONTENT;
public static final String NOTE_B_NOTE_ID_DESC = " " + PREFIX_NOTE_ID + VALID_NOTE_B_NOTE_ID;

public static final String INVALID_NAME_DESC = " " + PREFIX_NAME + "James&"; // '&' not allowed in names
public static final String INVALID_PHONE_DESC = " " + PREFIX_PHONE + "911a"; // 'a' not allowed in phones
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_A_CONTENT_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_A_PERSON_ID_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_A_TITLE_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_B_CONTENT_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_B_PERSON_ID_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_B_TITLE_DESC;
import static seedu.address.logic.commands.CommandTestUtil.PREAMBLE_WHITESPACE;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_A_CONTENT;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_A_PERSON_ID;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_A_TITLE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE_CONTENT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE_TITLE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PERSON_ID;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalNotes.NOTE_A;

import org.junit.jupiter.api.Test;

import seedu.address.logic.Messages;
import seedu.address.logic.commands.AddNoteCommand;
import seedu.address.model.note.Note;

public class AddNoteCommandParserTest {
private AddNoteCommandParser parser = new AddNoteCommandParser();

@Test
public void parse_allFieldsPresent_success() {
Note expectedNote = NOTE_A;

// whitespace only preamble
assertParseSuccess(parser, PREAMBLE_WHITESPACE + NOTE_A_PERSON_ID_DESC + NOTE_A_TITLE_DESC
+ NOTE_A_CONTENT_DESC,
new AddNoteCommand(Integer.parseInt(VALID_NOTE_A_PERSON_ID), expectedNote));
}

@Test
public void parse_repeatedNonTagValue_failure() {
String validExpectedNoteString = NOTE_B_PERSON_ID_DESC + NOTE_B_TITLE_DESC + NOTE_B_CONTENT_DESC;

// multiple person ids
assertParseFailure(parser, NOTE_A_PERSON_ID_DESC + validExpectedNoteString,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PERSON_ID));

// multiple titles
assertParseFailure(parser, NOTE_A_TITLE_DESC + validExpectedNoteString,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NOTE_TITLE));

// multiple contents
assertParseFailure(parser, NOTE_A_CONTENT_DESC + validExpectedNoteString,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NOTE_CONTENT));

// multiple fields repeated
assertParseFailure(parser,
validExpectedNoteString + NOTE_A_PERSON_ID_DESC + NOTE_A_TITLE_DESC + NOTE_A_CONTENT_DESC
+ validExpectedNoteString,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PERSON_ID, PREFIX_NOTE_TITLE, PREFIX_NOTE_CONTENT));
}

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

// missing person id prefix
assertParseFailure(parser, VALID_NOTE_A_PERSON_ID + NOTE_A_TITLE_DESC + NOTE_A_CONTENT_DESC,
expectedMessage);

// missing title prefix
assertParseFailure(parser, NOTE_A_PERSON_ID_DESC + VALID_NOTE_A_TITLE + NOTE_A_CONTENT_DESC,
expectedMessage);

// missing content prefix
assertParseFailure(parser, NOTE_A_PERSON_ID_DESC + NOTE_A_TITLE_DESC + VALID_NOTE_A_CONTENT,
expectedMessage);

// all prefixes missing
assertParseFailure(parser, VALID_NOTE_A_PERSON_ID + VALID_NOTE_A_TITLE + VALID_NOTE_A_CONTENT,
expectedMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_A_NOTE_ID_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_A_PERSON_ID_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_B_NOTE_ID_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NOTE_B_PERSON_ID_DESC;
import static seedu.address.logic.commands.CommandTestUtil.PREAMBLE_WHITESPACE;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_A_NOTE_ID;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NOTE_A_PERSON_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PERSON_ID;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import org.junit.jupiter.api.Test;

import seedu.address.logic.Messages;
import seedu.address.logic.commands.DeleteNoteCommand;

public class DeleteNoteCommandParserTest {
private DeleteNoteCommandParser parser = new DeleteNoteCommandParser();

@Test
public void parse_allFieldsPresent_success() {
// whitespace only preamble
assertParseSuccess(parser, PREAMBLE_WHITESPACE + NOTE_A_PERSON_ID_DESC + NOTE_A_NOTE_ID_DESC,
new DeleteNoteCommand(Integer.parseInt(VALID_NOTE_A_PERSON_ID),
Integer.parseInt(VALID_NOTE_A_NOTE_ID)));
}

@Test
public void parse_repeatedNonTagValue_failure() {
String validExpectedNoteString = NOTE_B_PERSON_ID_DESC + NOTE_B_NOTE_ID_DESC;

// multiple person ids
assertParseFailure(parser, NOTE_A_PERSON_ID_DESC + validExpectedNoteString,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PERSON_ID));

// multiple note ids
assertParseFailure(parser, NOTE_A_NOTE_ID_DESC + validExpectedNoteString,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NOTE_ID));

// multiple fields repeated
assertParseFailure(parser,
validExpectedNoteString + NOTE_A_PERSON_ID_DESC + NOTE_A_NOTE_ID_DESC
+ validExpectedNoteString,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PERSON_ID, PREFIX_NOTE_ID));
}

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

// missing person id prefix
assertParseFailure(parser, VALID_NOTE_A_PERSON_ID + NOTE_A_NOTE_ID_DESC,
expectedMessage);

// missing note id prefix
assertParseFailure(parser, NOTE_A_PERSON_ID_DESC + VALID_NOTE_A_NOTE_ID,
expectedMessage);

// all prefixes missing
assertParseFailure(parser, VALID_NOTE_A_PERSON_ID + VALID_NOTE_A_NOTE_ID,
expectedMessage);
}
}
28 changes: 28 additions & 0 deletions src/test/java/seedu/address/model/note/NoteContentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.address.model.note;

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

import org.junit.jupiter.api.Test;

public class NoteContentTest {
@Test
public void equals() {
NoteContent content = NoteContent.fromString("Valid Note Content");

// same values -> returns true
assertTrue(content.equals(NoteContent.fromString("Valid Note Content")));

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

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

// different types -> returns false
assertFalse(content.equals(5.0f));

// different values -> returns false
assertFalse(content.equals(NoteContent.fromString("Other Valid Note Content")));
}
}
Loading

0 comments on commit dc5758a

Please sign in to comment.