Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code coverage #125

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import seedu.address.model.person.Person;

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
* Finds and lists all persons in in address book whose attributes match the predicate.
* Keyword matching is case insensitive.
*/
public class FindCommand extends Command {
Expand All @@ -25,8 +25,12 @@ public class FindCommand extends Command {
+ "Example: " + COMMAND_WORD + " alice bob charlie";

private final Predicate<Person> predicate;

/**
* Finds and lists all persons in address book whose attributes match the predicate.
* Keyword matching is case insensitive.
*/
public FindCommand(Predicate<Person> predicate) {
requireNonNull(predicate);
this.predicate = predicate;
}

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/seedu/address/logic/parser/KeywordParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,27 @@ public class KeywordParser {
public static Predicate<Person> parseInput(String[] input) {
Pattern nricPattern = Pattern.compile("^[ST]\\d{7}[A-Z]$");
Pattern genderPattern = Pattern.compile("^([MF])$");
Pattern bloodtypePattern = Pattern.compile("^Blood Type (A\\+|A-|B\\+|B-|AB\\+|AB-|O\\+|O-)$");
Pattern bloodtypePattern = Pattern.compile("^(A\\+|A-|B\\+|B-|AB\\+|AB-|O\\+|O-)$");

Matcher genderMatcher = genderPattern.matcher(input[0]);
Matcher nricMatcher = nricPattern.matcher(input[0]);
Matcher bloodtypeMatcher = bloodtypePattern.matcher(input[0]);

if (input.length >= 3) {
Matcher bloodtypeMatcher = bloodtypePattern.matcher(input[2]);
if (bloodtypeMatcher.matches()) {
return new BloodTypePredicate(input[2]);
} else {
return new NameContainsKeywordsPredicate(Arrays.asList(input));
}
}

if (nricMatcher.matches()) {
return new IcContainsKeywordsPredicate(input[0]);
} else if (genderMatcher.matches()) {
return new GenderPredicate(input[0]);
} else if (bloodtypeMatcher.matches()) {
return new BloodTypePredicate(input[0]);
} else {
return new NameContainsKeywordsPredicate(Arrays.asList(input));
}

return new NameContainsKeywordsPredicate(Arrays.asList(input));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public class BloodTypePredicate implements Predicate<Person> {
* Constructs a predicate that tests that a {@code Person}'s {@code Gender} matches either male or female.
*/
public BloodTypePredicate(String keywords) {
String[] trimmed = keywords.split(" ");
this.keywords = trimmed[2];
this.keywords = keywords;
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/model/person/GenderPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ public boolean equals(Object other) {
public String toString() {
return new ToStringBuilder(this).add("keywords", keywords).toString();
}


}
52 changes: 50 additions & 2 deletions src/test/java/seedu/address/logic/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalAddressBook.getTypicalAddressBook;
import static seedu.address.testutil.TypicalDoctor.ALLEN;
import static seedu.address.testutil.TypicalDoctor.BOYD;
import static seedu.address.testutil.TypicalDoctor.CARLOS;
import static seedu.address.testutil.TypicalDoctor.DAVID;
import static seedu.address.testutil.TypicalDoctor.GREG;
import static seedu.address.testutil.TypicalPatient.ALICE;
import static seedu.address.testutil.TypicalPatient.BENSON;
import static seedu.address.testutil.TypicalPatient.CARL;
import static seedu.address.testutil.TypicalPatient.DANIEL;
import static seedu.address.testutil.TypicalPatient.ELLE;
import static seedu.address.testutil.TypicalPatient.FIONA;
import static seedu.address.testutil.TypicalPatient.GEORGE;

import java.util.Arrays;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;

import seedu.address.logic.parser.KeywordParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
Expand All @@ -30,6 +41,11 @@ public class FindCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

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

@Test
public void equals() {
GenderPredicate firstPredicate =
Expand Down Expand Up @@ -58,7 +74,7 @@ public void equals() {
}

@Test
public void execute_multipleKeywords_multiplePersonsFound() {
public void execute_multipleKeywords_multiplePersonsFound() throws ParseException {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
Predicate<Person> predicate = preparePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(predicate);
Expand All @@ -67,6 +83,38 @@ public void execute_multipleKeywords_multiplePersonsFound() {
assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPatientList());
}

@Test
public void execute_findNric_personFound() throws ParseException {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 1);
Predicate<Person> predicate = preparePredicate("T0131267K");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(ALICE), model.getFilteredPatientList());
}

@Test
public void execute_findGender_personFound() throws ParseException {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 9);
Predicate<Person> predicate = preparePredicate("M");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(BENSON, CARL, DANIEL, GEORGE),
model.getFilteredPatientList());
assertEquals(Arrays.asList(BOYD, CARLOS, DAVID, GREG, ALLEN), model.getFilteredDoctorList());
}

@Test
public void execute_findBloodType_personFound() throws ParseException {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 1);
Predicate<Person> predicate = preparePredicate("Blood Type AB+");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL), model.getFilteredPatientList());
}

@Test
public void toStringMethod() {
IcContainsKeywordsPredicate predicate = new IcContainsKeywordsPredicate("keyword");
Expand All @@ -78,7 +126,7 @@ public void toStringMethod() {
/**
* Parses {@code userInput} into a {@code Predicate<Person>}.
*/
private Predicate<Person> preparePredicate(String userInput) {
private Predicate<Person> preparePredicate(String userInput) throws ParseException {
return KeywordParser.parseInput(userInput.split("\\s"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

public class AddDoctorCommandParserTest {
private AddDoctorCommandParser parser = new AddDoctorCommandParser();

@Test
public void parse_compulsoryFieldMissing_failure() {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddDoctorCommand.MESSAGE_USAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public void parseCommand_findGender() throws Exception {
assertEquals(new FindCommand(KeywordParser.parseInput(keywords)), command);
}

@Test
public void parseCommand_findBloodType() throws Exception {
String[] keywords = {"Blood", "Type", "A+"};
FindCommand command = (FindCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " Blood Type A+");
assertEquals(new FindCommand(KeywordParser.parseInput(keywords)), command);
}

@Test
public void parseCommand_help() throws Exception {
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD) instanceof HelpCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.parser.exceptions.ParseException;

public class FindCommandParserTest {

private FindCommandParser parser = new FindCommandParser();
private String[] testInput = {"Alice", "Bob"};
private String[] testInput1 = {"Alice", "Bob"};

@Test
public void parse_emptyArg_throwsParseException() {
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

@Test
public void parse_validArgs_returnsFindCommand() {
public void parse_validArgs_returnsFindCommand() throws ParseException {
// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(KeywordParser.parseInput(testInput));
new FindCommand(KeywordParser.parseInput(testInput1));
assertParseSuccess(parser, "Alice Bob", expectedFindCommand);

// multiple whitespaces between keywords
Expand Down
76 changes: 62 additions & 14 deletions src/test/java/seedu/address/logic/parser/KeywordParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

import java.util.Arrays;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;
Expand All @@ -15,24 +14,73 @@


public class KeywordParserTest {
@Test
public void parseInputWithName_returnsNamePredicate() {
String[] input = {"John", "Doe"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof NameContainsKeywordsPredicate);
}

@Test
public void parseInputWithIC_returnsIcPredicate() {
String[] input = {"S1234567A"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof IcContainsKeywordsPredicate);
}

@Test
public void parseInputWithGender_returnsGenderPredicate() {
String[] input = {"M"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof GenderPredicate);
}

@Test
public void parseInputWithBloodType_returnsBloodTypePredicate() {
String[] input = {"Blood", "Type", "A+"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof BloodTypePredicate);
}

@Test
public void test_userInput_returnsCorrectFindCommand() {
String[] testIcInput = {"T1234567G"};
String[] testGenderInput = {"M"};
String[] testBloodTypeInput = {"Blood Type A+"};
String[] testNameInput = {"Alice", "Bob"};
public void parseInputWithInvalidGender_returnsNamePredicate() {
String[] input = {"X"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof NameContainsKeywordsPredicate);
}

Predicate<Person> testPredicate1 = new IcContainsKeywordsPredicate("T1234567G");
assertTrue(KeywordParser.parseInput(testIcInput).equals(testPredicate1));
@Test
public void parseInputWithInvalidBloodType_returnsNamePredicate() {
String[] input = {"Blood", "Type", "C"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof NameContainsKeywordsPredicate);
}

Predicate<Person> testPredicate2 = new GenderPredicate("M");
assertTrue(KeywordParser.parseInput(testGenderInput).equals(testPredicate2));
@Test
public void parseInputWithMultipleKeywords_returnsNamePredicate() {
String[] input = {"John", "Doe", "Carl"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof NameContainsKeywordsPredicate);
}

Predicate<Person> testPredicate3 = new BloodTypePredicate("Blood Type A+");
assertTrue(KeywordParser.parseInput(testBloodTypeInput).equals(testPredicate3));
@Test
public void parseInputWithIcAndGender_returnsIcPredicate() {
String[] input = {"S1234567A", "M"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof IcContainsKeywordsPredicate);
}

Predicate<Person> testPredicate4 = new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob"));
assertTrue(KeywordParser.parseInput(testNameInput).equals(testPredicate4));
@Test
public void parseInputWithIcAndBloodType_returnsIcPredicate() {
String[] input = {"S1234567A", "A+"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof IcContainsKeywordsPredicate);
}

@Test
public void parseInputWithGenderAndBloodType_returnsGenderPredicate() {
String[] input = {"M", "A+"};
Predicate<Person> predicate = KeywordParser.parseInput(input);
assertTrue(predicate instanceof GenderPredicate);
}
}
Loading
Loading