Skip to content

Commit

Permalink
Add NricTest and additional test cases in NricMatchesPredicateTest an…
Browse files Browse the repository at this point in the history
…d ViewCommandTest
  • Loading branch information
Dominic-Khoo committed Oct 10, 2024
1 parent 1e00dcb commit 24a9526
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/test/java/seedu/address/logic/commands/ViewCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.logic.commands;

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.logic.Messages.MESSAGE_NO_PERSON_FOUND;
Expand Down Expand Up @@ -71,4 +72,14 @@ public void execute_invalidNric_noPersonFound() {
assertCommandFailure(viewCommand, model, MESSAGE_NO_PERSON_FOUND);
}

@Test
public void toStringMethod() {
NricMatchesPredicate predicate = new NricMatchesPredicate("S1234567A");
ViewCommand viewCommand = new ViewCommand("S1234567A");

String expectedString = ViewCommand.class.getCanonicalName() + "{predicate=" + predicate + "}";

assertEquals(expectedString, viewCommand.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void test_nricMatches_returnsTrue() {
}

@Test
public void test_nricMatches_caseInsensitive_returnsTrue() {
public void test_caseInsensitive_returnsTrue() {
NricMatchesPredicate predicate = new NricMatchesPredicate("s1234567a");
Person person = new PersonBuilder().withNric("S1234567A").build();

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

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

import org.junit.jupiter.api.Test;

public class NricTest {

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

@Test
public void constructor_invalidNric_throwsIllegalArgumentException() {
String invalidNric = "A123"; // Clearly invalid NRIC format
assertThrows(IllegalArgumentException.class, () -> new Nric(invalidNric));
}

@Test
public void isValidNric() {
// null NRIC
assertThrows(NullPointerException.class, () -> Nric.isValidNric(null));

// invalid NRICs
assertFalse(Nric.isValidNric("")); // empty string
assertFalse(Nric.isValidNric(" ")); // spaces only
assertFalse(Nric.isValidNric("S12345678")); // 8 numbers
assertFalse(Nric.isValidNric("S123456E")); // only 7 characters total
assertFalse(Nric.isValidNric("S12345E7A")); // non-digit in the middle
assertFalse(Nric.isValidNric("1234567A")); // no alphabet at the start
assertFalse(Nric.isValidNric("S1234567")); // no alphabet at the end
assertFalse(Nric.isValidNric("S1234567*")); // invalid character at the end

// valid NRICs
assertTrue(Nric.isValidNric("S1234567A"));
assertTrue(Nric.isValidNric("T7654321Z"));
assertTrue(Nric.isValidNric("F0987654B")); // mixed with different first alphabets
assertTrue(Nric.isValidNric("G2345678K")); // different ending alphabet
assertTrue(Nric.isValidNric("s1234567a")); // lowercase letters at start and end
}

@Test
public void equals() {
Nric nric = new Nric("S1234567A");

// same values -> returns true
assertTrue(nric.equals(new Nric("S1234567A")));

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

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

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

// different values -> returns false
assertFalse(nric.equals(new Nric("T7654321Z")));
}

@Test
public void toStringMethod() {
Nric nric = new Nric("S1234567A");
assertTrue(nric.toString().equals("S1234567A"));
}
}

0 comments on commit 24a9526

Please sign in to comment.