Skip to content

Commit

Permalink
add junit test to increase codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
Kappaccinoh committed Mar 26, 2024
1 parent 8572cbf commit 53174da
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 8 deletions.
51 changes: 51 additions & 0 deletions src/test/java/seedu/address/commons/util/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,57 @@ public void containsWordIgnoreCase_validInputs_correctResult() {
assertTrue(StringUtil.containsWordIgnoreCase("AAA bBb ccc bbb", "bbB"));
}

//---------------- Tests for containsSubstringIgnoreCase --------------------------------------

/*
* Valid equivalence partitions for substring:
* - any substring
* - substring containing symbols/numbers
* - substring with leading/trailing spaces
*
* Valid equivalence partitions for sentence:
* - empty string
* - one word
* - multiple words
* - sentence with extra spaces
*
* Possible scenarios returning true:
* - matches substring at the beginning of the sentence
* - matches substring at the end of the sentence
* - matches substring in the middle of the sentence
* - matches multiple occurrences of the substring
*
* Possible scenarios returning false:
* - substring partially matches a word in the sentence
* - word in the sentence partially matches the substring
*
* The test method below tries to verify all above with a reasonably low number of test cases.
*/

@Test
public void containsSubstringIgnoreCase_validInputs_correctResult() {

// Empty sentence
assertFalse(StringUtil.containsSubstringIgnoreCase("", "abc")); // Boundary case
assertFalse(StringUtil.containsSubstringIgnoreCase(" ", "123"));

// Matches a partial substring only
// Sentence word bigger than substring
assertTrue(StringUtil.containsSubstringIgnoreCase("aaa bbb ccc", "bb"));
// Substring bigger than sentence word
assertFalse(StringUtil.containsSubstringIgnoreCase("aaa bbb ccc", "bbbb"));

// Matches substring in the sentence, different upper/lower case letters
assertTrue(StringUtil.containsSubstringIgnoreCase("aaa bBb ccc", "Bbb")); // First word (boundary case)
assertTrue(StringUtil.containsSubstringIgnoreCase("aaa bBb ccc@1", "CCc@1")); // Last word (boundary case)
assertTrue(StringUtil.containsSubstringIgnoreCase(" AAA bBb ccc ", "aaa")); // Sentence has extra spaces
assertTrue(StringUtil.containsSubstringIgnoreCase("Aaa", "aaa")); // Only one word in sentence (boundary case)
assertTrue(StringUtil.containsSubstringIgnoreCase("aaa bbb ccc", " ccc ")); // Leading/trailing spaces

// Matches multiple substrings in sentence
assertTrue(StringUtil.containsSubstringIgnoreCase("AAA bBb ccc bbb", "bbB"));
}

//---------------- Tests for getDetails --------------------------------------

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,90 @@ public void test_nameDoesNotContainKeywords_returnsFalse() {
// Non-matching keyword
predicate = new DoctorContainsKeywordsPredicate(Arrays.asList("Carol"));
assertFalse(predicate.test(new DoctorBuilder().withName("Alice Bob").build()));
}

@Test
public void test_nricContainsKeywords_returnsTrue() {
// One NRIC
DoctorContainsKeywordsPredicate predicate =
new DoctorContainsKeywordsPredicate(Collections.singletonList("T1234567A"));
assertTrue(predicate.test(new DoctorBuilder().withNric("T1234567A").build()));

// NRIC substring
predicate = new DoctorContainsKeywordsPredicate(Collections.singletonList("1234"));
assertTrue(predicate.test(new DoctorBuilder().withNric("T1234567A").build()));

// Multiple NRICs
predicate = new DoctorContainsKeywordsPredicate(Arrays.asList("T0170911E", "S1234567A"));
assertTrue(predicate.test(new DoctorBuilder().withNric("T0170911E").build()));
}

@Test
public void test_nricContainsKeywords_returnsFalse() {
// Zero keywords
DoctorContainsKeywordsPredicate predicate =
new DoctorContainsKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new DoctorBuilder().withNric("T0170911E").build()));

// Non-matching keyword
predicate = new DoctorContainsKeywordsPredicate(Arrays.asList("T0170911E"));
assertFalse(predicate.test(new DoctorBuilder().withNric("S1234567A").build()));
}

@Test
public void test_dobContainsKeywords_returnsTrue() {
// One dob
DoctorContainsKeywordsPredicate predicate =
new DoctorContainsKeywordsPredicate(Collections.singletonList("2001-01-01"));
assertTrue(predicate.test(new DoctorBuilder().withDoB("2001-01-01").build()));

// dob substring
predicate = new DoctorContainsKeywordsPredicate(Collections.singletonList("01"));
assertTrue(predicate.test(new DoctorBuilder().withDoB("2001-01-01").build()));

// multiple dob
predicate = new DoctorContainsKeywordsPredicate(Arrays.asList("2001-01-01", "2001-01-02"));
assertTrue(predicate.test(new DoctorBuilder().withDoB("2001-01-01").build()));
}

// // Keywords match phone, email and address, but does not match name
// predicate = new NameContainsKeywordsPredicate(Arrays.asList("12345", "alice@email.com", "Main", "Street"));
// assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345")
// .withEmail("alice@email.com").withAddress("Main Street").build()));
@Test
public void test_dobContainsKeywords_returnsFalse() {
// Zero keywords
DoctorContainsKeywordsPredicate predicate =
new DoctorContainsKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new DoctorBuilder().withDoB("2001-01-01").build()));

// Non-matching keyword
predicate = new DoctorContainsKeywordsPredicate(Arrays.asList("22"));
assertFalse(predicate.test(new DoctorBuilder().withDoB("2001-01-01").build()));
}

@Test
public void test_phoneContainsKeywords_returnsTrue() {
// One phone
DoctorContainsKeywordsPredicate predicate =
new DoctorContainsKeywordsPredicate(Collections.singletonList("91234567"));
assertTrue(predicate.test(new DoctorBuilder().withPhone("91234567").build()));

// phone substring
predicate = new DoctorContainsKeywordsPredicate(Collections.singletonList("4567"));
assertTrue(predicate.test(new DoctorBuilder().withPhone("91234567").build()));

// multiple phone numbers
predicate = new DoctorContainsKeywordsPredicate(Arrays.asList("91234567", "99991111"));
assertTrue(predicate.test(new DoctorBuilder().withPhone("91234567").build()));
}

@Test
public void test_phoneContainsKeywords_returnsFalse() {
// Zero keywords
DoctorContainsKeywordsPredicate predicate =
new DoctorContainsKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new DoctorBuilder().withPhone("91234567").build()));

// Non-matching keyword
predicate = new DoctorContainsKeywordsPredicate(Arrays.asList("7654"));
assertFalse(predicate.test(new DoctorBuilder().withPhone("91234567").build()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,90 @@ public void test_nameDoesNotContainKeywords_returnsFalse() {
// Non-matching keyword
predicate = new PatientContainsKeywordsPredicate(Arrays.asList("Carol"));
assertFalse(predicate.test(new PatientBuilder().withName("Alice Bob").build()));
}

@Test
public void test_nricContainsKeywords_returnsTrue() {
// One NRIC
PatientContainsKeywordsPredicate predicate =
new PatientContainsKeywordsPredicate(Collections.singletonList("T1234567A"));
assertTrue(predicate.test(new PatientBuilder().withNric("T1234567A").build()));

// NRIC substring
predicate = new PatientContainsKeywordsPredicate(Collections.singletonList("1234"));
assertTrue(predicate.test(new PatientBuilder().withNric("T1234567A").build()));

// Multiple NRICs
predicate = new PatientContainsKeywordsPredicate(Arrays.asList("T0170911E", "S1234567A"));
assertTrue(predicate.test(new PatientBuilder().withNric("T0170911E").build()));
}

@Test
public void test_nricContainsKeywords_returnsFalse() {
// Zero keywords
PatientContainsKeywordsPredicate predicate =
new PatientContainsKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new PatientBuilder().withNric("T0170911E").build()));

// Non-matching keyword
predicate = new PatientContainsKeywordsPredicate(Arrays.asList("T0170911E"));
assertFalse(predicate.test(new PatientBuilder().withNric("S1234567A").build()));
}

@Test
public void test_dobContainsKeywords_returnsTrue() {
// One dob
PatientContainsKeywordsPredicate predicate =
new PatientContainsKeywordsPredicate(Collections.singletonList("2001-01-01"));
assertTrue(predicate.test(new PatientBuilder().withDoB("2001-01-01").build()));

// dob substring
predicate = new PatientContainsKeywordsPredicate(Collections.singletonList("01"));
assertTrue(predicate.test(new PatientBuilder().withDoB("2001-01-01").build()));

// multiple dob
predicate = new PatientContainsKeywordsPredicate(Arrays.asList("2001-01-01", "2001-01-02"));
assertTrue(predicate.test(new PatientBuilder().withDoB("2001-01-01").build()));
}

// // Keywords match phone, email and address, but does not match name
// predicate = new NameContainsKeywordsPredicate(Arrays.asList("12345", "alice@email.com", "Main", "Street"));
// assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345")
// .withEmail("alice@email.com").withAddress("Main Street").build()));
@Test
public void test_dobContainsKeywords_returnsFalse() {
// Zero keywords
PatientContainsKeywordsPredicate predicate =
new PatientContainsKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new PatientBuilder().withDoB("2001-01-01").build()));

// Non-matching keyword
predicate = new PatientContainsKeywordsPredicate(Arrays.asList("22"));
assertFalse(predicate.test(new PatientBuilder().withDoB("2001-01-01").build()));
}

@Test
public void test_phoneContainsKeywords_returnsTrue() {
// One phone
PatientContainsKeywordsPredicate predicate =
new PatientContainsKeywordsPredicate(Collections.singletonList("91234567"));
assertTrue(predicate.test(new PatientBuilder().withPhone("91234567").build()));

// phone substring
predicate = new PatientContainsKeywordsPredicate(Collections.singletonList("4567"));
assertTrue(predicate.test(new PatientBuilder().withPhone("91234567").build()));

// multiple phone numbers
predicate = new PatientContainsKeywordsPredicate(Arrays.asList("91234567", "99991111"));
assertTrue(predicate.test(new PatientBuilder().withPhone("91234567").build()));
}

@Test
public void test_phoneContainsKeywords_returnsFalse() {
// Zero keywords
PatientContainsKeywordsPredicate predicate =
new PatientContainsKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new PatientBuilder().withPhone("91234567").build()));

// Non-matching keyword
predicate = new PatientContainsKeywordsPredicate(Arrays.asList("7654"));
assertFalse(predicate.test(new PatientBuilder().withPhone("91234567").build()));
}

@Test
Expand Down

0 comments on commit 53174da

Please sign in to comment.