Skip to content

Commit

Permalink
Resolve checkstyle issues and add JUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yyihaoc committed Oct 23, 2024
1 parent 111dbaa commit 2618e0c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 24 deletions.
18 changes: 12 additions & 6 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ public FindCommand parse(String args) throws ParseException {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

List<String> nameKeywords = argumentMultimap.getAllValues(PREFIX_NAME);
List<String> roleKeywords = argumentMultimap.getAllValues(PREFIX_ROLE);
List<String> telegramKeywords = argumentMultimap.getAllValues(PREFIX_TELEGRAM);

boolean hasEmptyInput = nameKeywords.stream().anyMatch(str -> str.trim().isEmpty())
|| roleKeywords.stream().anyMatch(str -> str.trim().isEmpty())
|| telegramKeywords.stream().anyMatch(str -> str.trim().isEmpty());
boolean hasAnyEmptyInput = hasEmptyInput(nameKeywords)
|| hasEmptyInput(roleKeywords)
|| hasEmptyInput(telegramKeywords);

if (hasEmptyInput) {
if (hasAnyEmptyInput) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

Expand All @@ -61,7 +62,12 @@ public static boolean areSomePrefixesPresent(ArgumentMultimap argumentMultimap,
return Stream.of(prefixes).anyMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}



/**
* Returns true if any of the element in the list contains empty {@code String} values in the given
* {@code List<String>}
*/
public static boolean hasEmptyInput(List<String> list) {
return list.stream().map(String::trim).anyMatch(str -> str.trim().isEmpty());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public boolean equals(Object other) {
return false;
}

TelegramContainsKeywordsPredicate otherTelegramContainsKeywordsPredicate = (TelegramContainsKeywordsPredicate) other;
TelegramContainsKeywordsPredicate otherTelegramContainsKeywordsPredicate =
(TelegramContainsKeywordsPredicate) other;
return keywords.equals(otherTelegramContainsKeywordsPredicate.keywords);
}

Expand Down
64 changes: 49 additions & 15 deletions src/test/java/seedu/address/logic/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import seedu.address.model.UserPrefs;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.RoleContainsKeywordsPredicate;
import seedu.address.model.person.TelegramContainsKeywordsPredicate;

/**
* Contains integration tests (interaction with the Model) for {@code FindCommand}.
Expand All @@ -37,22 +38,30 @@ public void equals() {
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
RoleContainsKeywordsPredicate firstRolePredicate =
new RoleContainsKeywordsPredicate(Collections.singletonList("first"));
TelegramContainsKeywordsPredicate firstTelegramPredicate =
new TelegramContainsKeywordsPredicate(Collections.singletonList("first"));

NameContainsKeywordsPredicate secondNamePredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));
RoleContainsKeywordsPredicate secondRolePredicate =
new RoleContainsKeywordsPredicate(Collections.singletonList("second"));
TelegramContainsKeywordsPredicate secondTelegramPredicate =
new TelegramContainsKeywordsPredicate(Collections.singletonList("second"));

FindCommand findFirstCommand = new FindCommand(firstNamePredicate, firstRolePredicate);
FindCommand findSecondCommand = new FindCommand(secondNamePredicate, secondRolePredicate);
FindCommand findThirdCommand = new FindCommand(firstNamePredicate, secondRolePredicate);
FindCommand findFourthCommand = new FindCommand(secondNamePredicate, firstRolePredicate);
FindCommand findFirstCommand = new FindCommand(firstNamePredicate, firstRolePredicate, firstTelegramPredicate);
FindCommand findSecondCommand = new FindCommand(secondNamePredicate,
secondRolePredicate, secondTelegramPredicate);
FindCommand findThirdCommand = new FindCommand(firstNamePredicate,
secondRolePredicate, secondTelegramPredicate);
FindCommand findFourthCommand = new FindCommand(secondNamePredicate,
firstRolePredicate, firstTelegramPredicate);

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

// same values -> returns true
FindCommand findFirstCommandCopy = new FindCommand(firstNamePredicate, firstRolePredicate);
FindCommand findFirstCommandCopy = new FindCommand(firstNamePredicate,
firstRolePredicate, firstTelegramPredicate);
assertTrue(findFirstCommand.equals(findFirstCommandCopy));

// different types -> returns false
Expand All @@ -76,8 +85,9 @@ public void execute_zeroNameAndRoleKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate(" ");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate(" ");
FindCommand command = new FindCommand(namePredicate, rolePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate));
TelegramContainsKeywordsPredicate telePredicate = prepareTelegramPredicate(" ");
FindCommand command = new FindCommand(namePredicate, rolePredicate, telePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate).or(telePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}
Expand All @@ -87,7 +97,8 @@ public void execute_multipleNameKeywordsOnly_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate("Kurz Elle Kunz");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate(" ");
FindCommand command = new FindCommand(namePredicate, rolePredicate);
TelegramContainsKeywordsPredicate telePredicate = prepareTelegramPredicate(" ");
FindCommand command = new FindCommand(namePredicate, rolePredicate, telePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList());
Expand All @@ -98,19 +109,33 @@ public void execute_multipleRoleKeywordsOnly_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate(" ");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate("friend");
FindCommand command = new FindCommand(namePredicate, rolePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate));
TelegramContainsKeywordsPredicate telePredicate = prepareTelegramPredicate(" ");
FindCommand command = new FindCommand(namePredicate, rolePredicate, telePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate).or(telePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(JAVIER, LENOR), model.getFilteredPersonList());
}

@Test
public void execute_multipleRoleAndRoleKeywords_multiplePersonsFound() {
public void execute_multipleTelegramKeywordsOnly_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate(" ");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate(" ");
TelegramContainsKeywordsPredicate telePredicate = prepareTelegramPredicate("lenor javiertan");
FindCommand command = new FindCommand(namePredicate, rolePredicate, telePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate).or(telePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(JAVIER, LENOR), model.getFilteredPersonList());
}

@Test
public void execute_multipleNameAndRoleAndTelegramKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate("javier");
RoleContainsKeywordsPredicate rolePredicate = prepareRolePredicate("relative");
FindCommand command = new FindCommand(namePredicate, rolePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate));
TelegramContainsKeywordsPredicate telePredicate = prepareTelegramPredicate("javiertan");
FindCommand command = new FindCommand(namePredicate, rolePredicate, telePredicate);
expectedModel.updateFilteredPersonList(namePredicate.or(rolePredicate).or(telePredicate));
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(JAVIER, KELLY, LENOR), model.getFilteredPersonList());
}
Expand All @@ -119,9 +144,11 @@ public void execute_multipleRoleAndRoleKeywords_multiplePersonsFound() {
public void toStringMethod() {
NameContainsKeywordsPredicate namePredicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
RoleContainsKeywordsPredicate rolePredicate = new RoleContainsKeywordsPredicate(Arrays.asList("friend"));
FindCommand findCommand = new FindCommand(namePredicate, rolePredicate);
TelegramContainsKeywordsPredicate telePredicate = new TelegramContainsKeywordsPredicate(
Arrays.asList("georgebest"));
FindCommand findCommand = new FindCommand(namePredicate, rolePredicate, telePredicate);
String expected = FindCommand.class.getCanonicalName() + "{namePredicate=" + namePredicate
+ ", rolePredicate=" + rolePredicate + "}";
+ ", rolePredicate=" + rolePredicate + ", telegramPredicate=" + telePredicate + "}";
assertEquals(expected, findCommand.toString());
}

Expand All @@ -138,4 +165,11 @@ private NameContainsKeywordsPredicate prepareNamePredicate(String userInput) {
private RoleContainsKeywordsPredicate prepareRolePredicate(String userInput) {
return new RoleContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}

/**
* Parses {@code userInput} into a {@code TelegramContainsKeywordsPredicate}.
*/
private TelegramContainsKeywordsPredicate prepareTelegramPredicate(String userInput) {
return new TelegramContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.model.person.RoleContainsKeywordsPredicate;
import seedu.address.model.person.TelegramContainsKeywordsPredicate;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.PersonBuilder;
import seedu.address.testutil.PersonUtil;
Expand Down Expand Up @@ -82,7 +83,8 @@ public void parseCommand_find() throws Exception {
.collect(Collectors.joining(" ")));

assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords),
new RoleContainsKeywordsPredicate(List.of())), command);
new RoleContainsKeywordsPredicate(List.of()),
new TelegramContainsKeywordsPredicate(List.of())), command);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.RoleContainsKeywordsPredicate;
import seedu.address.model.person.TelegramContainsKeywordsPredicate;

public class FindCommandParserTest {

Expand Down Expand Up @@ -40,10 +41,14 @@ public void parse_emptyArgumentAfterPrefix_throwsParseException() {
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
assertParseFailure(parser, "r/ ",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
assertParseFailure(parser, "t/ ",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
assertParseFailure(parser, "r/member n/ ",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
assertParseFailure(parser, "r/ n/james ",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
assertParseFailure(parser, "r/ n/james t/ ",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

@Test
Expand All @@ -60,19 +65,34 @@ public void parse_emptyRoleKeyword_throwsParseException() {
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_emptyTelegramKeyword_throwsParseException() {
// Test where the telegram field is empty
String userInput = " n/John Doe t/";
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_emptyNameAndRoleKeywords_throwsParseException() {
// Test where both name and role fields are empty
String userInput = " n/ r/ "; // both contain only whitespace
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_emptyNameAndRoleAndTelegramKeywords_throwsParseException() {
// Test where all name, role and telegram fields are empty
String userInput = " n/ r/ t/ ";
assertThrows(ParseException.class, () -> parser.parse(userInput));
}

@Test
public void parse_validArgs_returnsFindCommand() {
// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(List.of("chen", "albert")),
new RoleContainsKeywordsPredicate(List.of("member", "exco")));
new RoleContainsKeywordsPredicate(List.of("member", "exco")),
new TelegramContainsKeywordsPredicate(List.of()));
assertParseSuccess(parser, " n/chen n/albert r/member r/exco ", expectedFindCommand);

// multiple whitespaces between keywords
Expand Down

0 comments on commit 2618e0c

Please sign in to comment.