← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~jml/testtools/ends-with-669165 into lp:testtools

 

Jonathan Lange has proposed merging lp:~jml/testtools/ends-with-669165 into lp:testtools.

Requested reviews:
  testtools developers (testtools-dev)
Related bugs:
  #669165 EndsWith matcher
  https://bugs.launchpad.net/bugs/669165


Adds an EndsWith matcher. Pretty dull stuff.
-- 
https://code.launchpad.net/~jml/testtools/ends-with-669165/+merge/42004
Your team testtools developers is requested to review the proposed merge of lp:~jml/testtools/ends-with-669165 into lp:testtools.
=== modified file 'NEWS'
--- NEWS	2010-11-25 11:32:24 +0000
+++ NEWS	2010-11-27 11:39:59 +0000
@@ -22,6 +22,9 @@
 * Fix the runTest parameter of TestCase to actually work, rather than raising
   a TypeError.  (Jonathan Lange, #657760)
 
+* New matcher ``EndsWith`` added to complement the existing ``StartsWith``
+  matcher.  (Jonathan Lange, #669165)
+
 * Non-release snapshots of testtools will now work with buildout.
   (Jonathan Lange, #613734)
 

=== modified file 'testtools/matchers.py'
--- testtools/matchers.py	2010-11-11 17:53:42 +0000
+++ testtools/matchers.py	2010-11-27 11:39:59 +0000
@@ -179,6 +179,22 @@
             self.matchee, self.expected)
 
 
+class DoesNotEndWith(Mismatch):
+
+    def __init__(self, matchee, expected):
+        """Create a DoesNotEndWith Mismatch.
+
+        :param matchee: the string that did not match.
+        :param expected: the string that `matchee` was expected to end with.
+        """
+        self.matchee = matchee
+        self.expected = expected
+
+    def describe(self):
+        return "'%s' does not end with '%s'." % (
+            self.matchee, self.expected)
+
+
 class _BinaryComparison(object):
     """Matcher that compares an object to another object."""
 
@@ -384,6 +400,25 @@
         return None
 
 
+class EndsWith(Matcher):
+    """Checks whether one string starts with another."""
+
+    def __init__(self, expected):
+        """Create a EndsWith Matcher.
+
+        :param expected: the string that matchees should end with.
+        """
+        self.expected = expected
+
+    def __str__(self):
+        return "Ends with '%s'." % self.expected
+
+    def match(self, matchee):
+        if not matchee.endswith(self.expected):
+            return DoesNotEndWith(matchee, self.expected)
+        return None
+
+
 class KeysEqual(Matcher):
     """Checks whether a dict has particular keys."""
 

=== modified file 'testtools/tests/test_matchers.py'
--- testtools/tests/test_matchers.py	2010-11-11 17:53:42 +0000
+++ testtools/tests/test_matchers.py	2010-11-27 11:39:59 +0000
@@ -13,7 +13,9 @@
     Annotate,
     Equals,
     DocTestMatches,
+    DoesNotEndWith,
     DoesNotStartWith,
+    EndsWith,
     KeysEqual,
     Is,
     LessThan,
@@ -411,6 +413,38 @@
         self.assertEqual("bar", mismatch.expected)
 
 
+class DoesNotEndWithTests(TestCase):
+
+    def test_describe(self):
+        mismatch = DoesNotEndWith("fo", "bo")
+        self.assertEqual("'fo' does not end with 'bo'.", mismatch.describe())
+
+
+class EndsWithTests(TestCase):
+
+    def test_str(self):
+        matcher = EndsWith("bar")
+        self.assertEqual("Ends with 'bar'.", str(matcher))
+
+    def test_match(self):
+        matcher = EndsWith("arf")
+        self.assertIs(None, matcher.match("barf"))
+
+    def test_mismatch_returns_does_not_end_with(self):
+        matcher = EndsWith("bar")
+        self.assertIsInstance(matcher.match("foo"), DoesNotEndWith)
+
+    def test_mismatch_sets_matchee(self):
+        matcher = EndsWith("bar")
+        mismatch = matcher.match("foo")
+        self.assertEqual("foo", mismatch.matchee)
+
+    def test_mismatch_sets_expected(self):
+        matcher = EndsWith("bar")
+        mismatch = matcher.match("foo")
+        self.assertEqual("bar", mismatch.expected)
+
+
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)


Follow ups