Skip to content

Commit 5ed1802

Browse files
authored
Merge pull request #468 from jelmer/fix-text-search
Fix text search to use substring matching per RFC
2 parents 1c1e7c4 + 90d0dd2 commit 5ed1802

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

xandikos/collation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _match(a, b, k):
3636
elif k == "starts-with":
3737
return a.startswith(b)
3838
elif k == "ends-with":
39-
return b.endswith(b)
39+
return a.endswith(b)
4040
else:
4141
raise NotImplementedError
4242

xandikos/icalendar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ def match_indexes(self, indexes: SubIndexDict):
641641

642642
def match(self, prop: Union[vText, vCategory, str]):
643643
if isinstance(prop, vText):
644-
matches = self.collation(self.text, str(prop), "equals")
644+
matches = self.collation(str(prop), self.text, "contains")
645645
elif isinstance(prop, str):
646-
matches = self.collation(self.text, prop, "equals")
646+
matches = self.collation(prop, self.text, "contains")
647647
elif isinstance(prop, vCategory):
648648
matches = any([self.match(cat) for cat in prop.cats])
649649
else:

xandikos/tests/test_icalendar.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,14 @@ def test_category(self):
10291029
self.assertTrue(tm.match(vCategory(["foobar"])))
10301030
self.assertFalse(tm.match(vCategory(["fobar"])))
10311031
self.assertTrue(tm.match_indexes({None: [b"foobar,blah"]}))
1032-
self.assertFalse(tm.match_indexes({None: [b"foobarblah"]}))
1032+
# With substring matching, "foobar" is found in "foobarblah"
1033+
self.assertTrue(tm.match_indexes({None: [b"foobarblah"]}))
10331034

10341035
def test_unknown_type(self):
10351036
tm = TextMatcher("dontknow", "foobar")
10361037
self.assertFalse(tm.match(object()))
1037-
self.assertFalse(tm.match_indexes({None: [b"foobarblah"]}))
1038+
# With substring matching, "foobar" is found in "foobarblah"
1039+
self.assertTrue(tm.match_indexes({None: [b"foobarblah"]}))
10381040

10391041
def test_unknown_collation(self):
10401042
self.assertRaises(
@@ -1045,6 +1047,18 @@ def test_unknown_collation(self):
10451047
collation="i;blah",
10461048
)
10471049

1050+
def test_substring_match(self):
1051+
# Test that text matching uses substring search as per RFC
1052+
tm = TextMatcher("summary", "bar")
1053+
self.assertTrue(tm.match(vText("foobar")))
1054+
self.assertTrue(tm.match(vText("bar")))
1055+
self.assertTrue(tm.match(vText("barbaz")))
1056+
self.assertTrue(tm.match(vText("foobarbaz")))
1057+
self.assertFalse(tm.match(vText("foo")))
1058+
self.assertFalse(tm.match(vText("ba")))
1059+
# Test case insensitive substring match
1060+
self.assertTrue(tm.match(vText("FOOBAR")))
1061+
10481062

10491063
class ApplyTimeRangeVeventTests(unittest.TestCase):
10501064
def _tzify(self, dt):

0 commit comments

Comments
 (0)