Permalink
Browse files

Merge pull request #235 from paulroub/233-and-search

Match IOS search-with-and functionality
  • Loading branch information...
2 parents 402429a + 30820bf commit e73045dfe8a874a29e5b0f4a6584e7b494383b6f @ginatrapani ginatrapani committed Jan 24, 2012
@@ -34,27 +34,32 @@
class ByTextFilter implements Filter<Task> {
private String text;
private boolean caseSensitive;
+ private String[] parts;
public ByTextFilter(String text, boolean caseSensitive) {
if (text == null) {
text = "";
}
this.text = caseSensitive ? text : text.toUpperCase();
this.caseSensitive = caseSensitive;
+
+ this.parts = this.text.split("\\s");
}
@Override
public boolean apply(Task input) {
- if (text.length() <= 0) {
- return true;
- }
-
String taskText = caseSensitive ? input.getText() : input.getText()
.toUpperCase();
- if (taskText.contains(text)) {
- return true;
+
+ for ( int i = 0; i < parts.length; ++i )
+ {
+ String part = this.parts[i];
+
+ if ((part.length() > 0) && ! taskText.contains(part))
+ return(false);
}
- return false;
+
+ return true;
}
/* FOR TESTING ONLY, DO NOT USE IN APPLICATION */
@@ -100,4 +100,39 @@ public void testFilter_abcText_containsTaskTextNotPadded_wrongCase_caseInSensiti
assertTrue("apply was not true",
filter.apply(new Task(1, "helloABCworld")));
}
+
+ private void shouldMatch(String pattern, String rawText, boolean cs) {
+ ByTextFilter filter = new ByTextFilter(pattern, cs);
+ assertTrue(String.format("'%s' should match '%s'", pattern, rawText),
+ filter.apply(new Task(1, rawText)));
+ }
+
+ private void shouldNotMatch(String pattern, String rawText, boolean cs) {
+ ByTextFilter filter = new ByTextFilter(pattern, cs);
+ assertFalse(
+ String.format("'%s' should not match '%s'", pattern, rawText),
+ filter.apply(new Task(1, rawText)));
+ }
+
+ public void testFilter_andCaseSensitive() {
+ this.shouldMatch("abc xyz", "abc xyz", true);
+ this.shouldMatch("abc xyz", "abcxyz", true);
+ this.shouldMatch("abc xyz", "xyz abc", true);
+ this.shouldNotMatch("abc xyz", "xyz", true);
+ this.shouldNotMatch("abc xyz", "ABC xyz", true);
+ }
+
+ public void testFilter_andCaseInsensitive() {
+ this.shouldMatch("abc xyz", "abc xyz", false);
+ this.shouldMatch("abc xyz", "abcxyz", false);
+ this.shouldMatch("abc xyz", "xyz abc", false);
+ this.shouldNotMatch("abc xyz", "xyz", false);
+ this.shouldMatch("abc xyz", "ABC xyz", false);
+ }
+
+ public void testFilter_andIgnoreWhitespace() {
+ this.shouldMatch("abc ", "abc", true);
+ this.shouldMatch(" abc", "abc", true);
+ this.shouldMatch(" abc ", "abc", true);
+ }
}

0 comments on commit e73045d

Please sign in to comment.