Skip to content

Commit

Permalink
Add test to Comment.java
Browse files Browse the repository at this point in the history
  • Loading branch information
RuijianLu committed Oct 17, 2024
1 parent 54fda17 commit 76713c3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/model/person/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public boolean equals(Object other) {
}

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

Name otherName = (Name) other;
return fullComment.equals(otherName.fullName);
Comment otherComment = (Comment) other;
return fullComment.equals(otherComment.fullComment);
}

@Override
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/seedu/address/model/person/CommentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package seedu.address.model.person;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;

public class CommentTest {

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

@Test
public void constructor_invalidComment_throwsIllegalArgumentException() {
String invalidComment = "";
assertThrows(IllegalArgumentException.class, () -> new Comment(invalidComment));
}

@Test
public void isValidComment() {
// null name
assertThrows(NullPointerException.class, () -> Comment.isValidComment(null));

// invalid name
assertFalse(Comment.isValidComment("")); // empty string
assertFalse(Comment.isValidComment(" ")); // spaces only
assertFalse(Comment.isValidComment("^")); // only non-alphanumeric characters
assertFalse(Comment.isValidComment("prefer another set of cutlery*")); // contains non-alphanumeric characters

// valid name
assertTrue(Comment.isValidComment("prefer another set of cutlery")); // alphabets only
assertTrue(Comment.isValidComment("12345")); // numbers only
assertTrue(Comment.isValidComment("is 1st member")); // alphanumeric characters
assertTrue(Comment.isValidComment("Prefer Another Set of Cutlery")); // with capital letters
assertTrue(Comment.isValidComment("David Roger Jackson Ray Jr 2nd")); // long names
}

@Test
public void equals() {
Comment comment = new Comment("Valid Comment");

// same values -> returns true
assertTrue(comment.equals(new Comment("Valid Comment")));

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

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

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

// different values -> returns false
assertFalse(comment.equals(new Comment("Other Valid Comment")));
}

}

0 comments on commit 76713c3

Please sign in to comment.