[CalendarServer-changes] [13012] twext/trunk/twext/who
source_changes at macosforge.org
source_changes at macosforge.org
Thu Mar 27 17:26:55 PDT 2014
Revision: 13012
http://trac.calendarserver.org//changeset/13012
Author: wsanchez at apple.com
Date: 2014-03-27 17:26:55 -0700 (Thu, 27 Mar 2014)
Log Message:
-----------
Add MatchExpression.match()
Modified Paths:
--------------
twext/trunk/twext/who/expression.py
twext/trunk/twext/who/test/test_expression.py
Modified: twext/trunk/twext/who/expression.py
===================================================================
--- twext/trunk/twext/who/expression.py 2014-03-27 18:03:46 UTC (rev 13011)
+++ twext/trunk/twext/who/expression.py 2014-03-28 00:26:55 UTC (rev 13012)
@@ -242,3 +242,56 @@
flags=flags,
)
)
+
+
+ def match(self, value):
+ """
+ Test whether this expression's field value matches against a given
+ value according to this expression's match type and match flags.
+
+ @param value: The value to match against.
+ @type value: L{object}
+
+ @return: C{True} if this expression matches C{value}; L{False}
+ otherwise.
+ @rtype: L{bool}
+ """
+ predicate = MatchFlags.predicator(self.flags)
+ normalize = MatchFlags.normalizer(self.flags)
+
+ def match(a, b):
+ matchType = self.matchType
+
+ # import pdb; pdb.set_trace()
+
+ if matchType == MatchType.equals:
+ return a == b
+
+ if matchType == MatchType.startsWith:
+ return a.startswith(b)
+
+ if matchType == MatchType.endsWith:
+ return a.endswith(b)
+
+ if matchType == MatchType.contains:
+ return b in a
+
+ if matchType == MatchType.lessThan:
+ return a < b
+
+ if matchType == MatchType.greaterThan:
+ return a > b
+
+ if matchType == MatchType.lessThanOrEqualTo:
+ return a <= b
+
+ if matchType == MatchType.greaterThanOrEqualTo:
+ return a >= b
+
+ raise NotImplementedError(
+ "Unknown match type: {0!r}".format(matchType)
+ )
+
+ return predicate(match(
+ normalize(value), normalize(self.fieldValue)
+ ))
Modified: twext/trunk/twext/who/test/test_expression.py
===================================================================
--- twext/trunk/twext/who/test/test_expression.py 2014-03-27 18:03:46 UTC (rev 13011)
+++ twext/trunk/twext/who/test/test_expression.py 2014-03-28 00:26:55 UTC (rev 13012)
@@ -111,6 +111,7 @@
)),
)
+
def test_repr_type(self):
"""
L{MatchExpression.__repr__} emits match type.
@@ -124,6 +125,7 @@
)),
)
+
def test_repr_flags(self):
"""
L{MatchExpression.__repr__} emits flags.
@@ -147,3 +149,101 @@
flags=(MatchFlags.NOT | MatchFlags.caseInsensitive),
)),
)
+
+
+ def test_match_equals(self):
+ """
+ L{MatchExpression.match} with L{MatchType.equals}.
+ """
+ expression = MatchExpression(
+ FieldName.fullNames, u"Morgen",
+ matchType=MatchType.equals
+ )
+ self.assertTrue(expression.match(u"Morgen"))
+ self.assertFalse(expression.match(u"Wilfredo"))
+
+
+ def test_match_startsWith(self):
+ """
+ L{MatchExpression.match} with L{MatchType.startsWith}.
+ """
+ expression = MatchExpression(
+ FieldName.fullNames, u"Mor",
+ matchType=MatchType.startsWith
+ )
+ self.assertTrue(expression.match(u"Morgen"))
+ self.assertFalse(expression.match(u"Wilfredo"))
+
+
+ def test_match_endsWith(self):
+ """
+ L{MatchExpression.match} with L{MatchType.endsWith}.
+ """
+ expression = MatchExpression(
+ FieldName.fullNames, u"gen",
+ matchType=MatchType.endsWith
+ )
+ self.assertTrue(expression.match(u"Morgen"))
+ self.assertFalse(expression.match(u"Wilfredo"))
+
+
+ def test_match_contains(self):
+ """
+ L{MatchExpression.match} with L{MatchType.contains}.
+ """
+ expression = MatchExpression(
+ FieldName.fullNames, u"org",
+ matchType=MatchType.contains
+ )
+ self.assertTrue(expression.match(u"Morgen"))
+ self.assertFalse(expression.match(u"Wilfredo"))
+
+
+ def test_match_lessThan(self):
+ """
+ L{MatchExpression.match} with L{MatchType.lessThan}.
+ """
+ expression = MatchExpression(
+ FieldName.fullNames, u"Morgen",
+ matchType=MatchType.lessThan
+ )
+ self.assertTrue(expression.match(u"Glyph")) # Sorry, Glyph
+ self.assertFalse(expression.match(u"Wilfredo"))
+
+
+ def test_match_greaterThan(self):
+ """
+ L{MatchExpression.match} with L{MatchType.greaterThan}.
+ """
+ expression = MatchExpression(
+ FieldName.fullNames, u"Morgen",
+ matchType=MatchType.greaterThan
+ )
+ self.assertTrue(expression.match(u"Wilfredo")) # Woot!
+ self.assertFalse(expression.match(u"Glyph"))
+
+
+ def test_match_lessThanOrEqualTo(self):
+ """
+ L{MatchExpression.match} with L{MatchType.lessThanOrEqualTo}.
+ """
+ expression = MatchExpression(
+ FieldName.fullNames, u"Morgen",
+ matchType=MatchType.lessThanOrEqualTo
+ )
+ self.assertTrue(expression.match(u"Glyph"))
+ self.assertTrue(expression.match(u"Morgen"))
+ self.assertFalse(expression.match(u"Wilfredo"))
+
+
+ def test_match_greaterThanOrEqualTo(self):
+ """
+ L{MatchExpression.match} with L{MatchType.greaterThanOrEqualTo}.
+ """
+ expression = MatchExpression(
+ FieldName.fullNames, u"Morgen",
+ matchType=MatchType.greaterThanOrEqualTo
+ )
+ self.assertTrue(expression.match(u"Wilfredo"))
+ self.assertTrue(expression.match(u"Morgen"))
+ self.assertFalse(expression.match(u"Glyph"))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140327/dee0ece2/attachment.html>
More information about the calendarserver-changes
mailing list