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

Enhance find command #69

Merged
merged 2 commits into from
Nov 1, 2016
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
27 changes: 24 additions & 3 deletions src/main/java/seedu/malitio/commons/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@
* Helper functions for handling strings.
*/
public class StringUtil {

//@@author a0126633j
/**
* Checks whether any of the query is part of the source string.
*/
public static boolean containsIgnoreCase(String source, String query) {
String[] split = source.toLowerCase().split("\\s+");
List<String> strings = Arrays.asList(split);
return strings.stream().filter(s -> s.equals(query.toLowerCase())).count() > 0;
String[] splitSource = source.toLowerCase().split("\\s+");
String[] splitQuery = query.toLowerCase().split("\\s+");

for(int i = 0; i < splitQuery.length; i++) {
for(int j = 0; j < splitSource.length; j++) {
if (splitSource[j].contains(splitQuery[i])) {
return true;
}
}
}
return false;
}
//@@author

/**
* Returns a detailed message of the t, including the stack trace.
Expand Down Expand Up @@ -54,4 +68,11 @@ public static String removeSlashesAtBeginningOfString(String arg) {
}
return arg;
}
/**
* Reformats a tag string into a string separated by white space.
* e.g. "[cs2103], [cs1010e]" into "cs2103 cs1010e".
*/
public static String reformatTagString(String arg) {
return arg.replaceAll(",", "").replaceAll("\\[|\\]", " ");
}
}
9 changes: 7 additions & 2 deletions src/main/java/seedu/malitio/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,13 @@ private class NameQualifier implements Qualifier {
NameQualifier(Set<String> nameKeyWords) {
this.nameKeyWords = nameKeyWords;
}


//@@author a0126633j
@Override
public boolean run(ReadOnlyFloatingTask task) {
return nameKeyWords.stream()
.filter(keyword -> StringUtil.containsIgnoreCase(task.getName().fullName, keyword))
.filter(keyword -> StringUtil.containsIgnoreCase(task.getName().fullName
+ " " + StringUtil.reformatTagString(task.tagsString()), keyword))
.findAny()
.isPresent();
}
Expand All @@ -372,6 +374,7 @@ public boolean run(ReadOnlyFloatingTask task) {
public boolean run(ReadOnlyDeadline deadline) {
return nameKeyWords.stream()
.filter(keyword -> StringUtil.containsIgnoreCase(deadline.getName().fullName
+ " " + StringUtil.reformatTagString(deadline.tagsString())
+ " " + deadline.getDue().toString(),
keyword))
.findAny()
Expand All @@ -382,6 +385,7 @@ public boolean run(ReadOnlyDeadline deadline) {
public boolean run(ReadOnlyEvent event) {
return nameKeyWords.stream()
.filter(keyword -> StringUtil.containsIgnoreCase(event.getName().fullName
+ " " + StringUtil.reformatTagString(event.tagsString())
+ " " + event.getStart().toString()
+ " " + event.getEnd().toString(),
keyword))
Expand All @@ -395,6 +399,7 @@ public String toString() {
return "name=" + String.join(", ", nameKeyWords);
}
}
//@@author

private class TimeQualifier implements Qualifier {
private DateTime timeKeyWord;
Expand Down
37 changes: 26 additions & 11 deletions src/test/java/guitests/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@
//@@author a0126633j
public class FindCommandTest extends MalitioGuiTest {

//In the tests below, we assume event,floating task and deadline lists are identical, hence to save resources only work on them equally
@Test
public void find_nonEmptyList() throws IllegalArgumentException, IllegalValueException {

assertFindEventResult("find with", td.event1, td.event2); //multiple results
assertFindEventResult("find hello"); //no result
assertFindEventResult("find e with", td.event1, td.event2); //multiple results
assertResultMessage("2 tasks found!");

assertFindFloatingTaskResult("find peN HOMEWORK", td.floatingTask2);
assertFindDeadlineResult("find peN HOMEWORK", td.deadline3, td.deadline5);
assertResultMessage("3 tasks found!");

assertFindDeadlineResult("find 12-25", td.deadline4); //find dates
assertFindEventResult("find 12-25", td.event5);
assertResultMessage("2 tasks found!");

assertFindEventResult("find wedding"); //no result

//find after deleting one result
commandBox.runCommand("list");
Expand All @@ -30,9 +41,7 @@ public void find_nonEmptyList() throws IllegalArgumentException, IllegalValueExc
@Test
public void find_emptyList() throws IllegalArgumentException, IllegalValueException {
commandBox.runCommand("clear");
assertFindFloatingTaskResult("find eat"); //no results
assertFindDeadlineResult("find eat");
assertFindEventResult("find eat");
assertFindEventResult("find eat"); //no result
}

@Test
Expand All @@ -43,32 +52,38 @@ public void find_invalidCommand_fail() {

@Test
public void find_specificTasks() throws IllegalArgumentException, IllegalValueException {
assertFindDeadlineResult("find d SOME", td.deadline2);
assertFindFloatingTaskResult("find f tell", td.floatingTask3);

assertFindEventResult("find e with", td.event1, td.event2); //multiple results
assertResultMessage("2 tasks found!");

assertFindDeadlineResult("find d H", td.deadline1, td.deadline4, td.deadline5);
assertResultMessage("3 tasks found!");

assertFindFloatingTaskResult("find f tell", td.floatingTask3);
assertResultMessage("1 tasks found!");

commandBox.runCommand("find e");
assertResultMessage(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
FindCommand.MESSAGE_USAGE)); // recognise as finding in event but no keywords
}

/**
* Overload functions to assert result in each floating task, deadline and event list is correct
*/
private void assertFindFloatingTaskResult(String command, TestFloatingTask... expectedHits ) {
commandBox.runCommand(command);
assertFloatingTaskListSize(expectedHits.length);
assertResultMessage(expectedHits.length + " tasks found!");

assertTrue(floatingTaskListPanel.isListMatching(expectedHits));
}
private void assertFindDeadlineResult(String command, TestDeadline... expectedHits ) {
commandBox.runCommand(command);
assertDeadlineListSize(expectedHits.length);
assertResultMessage(expectedHits.length + " tasks found!");

assertTrue(deadlineListPanel.isListMatching(expectedHits));
}
private void assertFindEventResult(String command, TestEvent... expectedHits ) throws IllegalArgumentException, IllegalValueException {
commandBox.runCommand(command);
assertEventListSize(expectedHits.length);
assertResultMessage(expectedHits.length + " tasks found!");

assertTrue(eventListPanel.isListMatching(expectedHits));
}
Expand Down