← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~rvb/testtools/testtools-contains-all into lp:testtools

 

Raphaël Badin has proposed merging lp:~rvb/testtools/testtools-contains-all into lp:testtools.

Requested reviews:
  testtools committers (testtools-committers)

For more details, see:
https://code.launchpad.net/~rvb/testtools/testtools-contains-all/+merge/104065

This branch adds a new (shortcut) matcher ContainsAll. ContainsAll(items) that can be used instead of using MatchesAll(*[Contains(item) for item in items])).  We use it quite a lot in MAAS so we figured we could submit it upstream.

Drive-by fix: removed unused import.
-- 
https://code.launchpad.net/~rvb/testtools/testtools-contains-all/+merge/104065
Your team testtools developers is subscribed to branch lp:testtools.
=== modified file 'testtools/matchers.py'
--- testtools/matchers.py	2012-04-27 13:41:16 +0000
+++ testtools/matchers.py	2012-04-30 07:40:29 +0000
@@ -16,6 +16,7 @@
     'AllMatch',
     'Annotate',
     'Contains',
+    'ContainsAll',
     'DirExists',
     'DocTestMatches',
     'EndsWith',
@@ -664,6 +665,11 @@
         return None
 
 
+def ContainsAll(items):
+    """Checks whether a list of things is contained in another thing."""
+    return MatchesAll(*[Contains(item) for item in items], first_only=False)
+
+
 class StartsWith(Matcher):
     """Checks whether one string starts with another."""
 

=== modified file 'testtools/tests/test_matchers.py'
--- testtools/tests/test_matchers.py	2011-12-21 01:28:38 +0000
+++ testtools/tests/test_matchers.py	2012-04-30 07:40:29 +0000
@@ -12,7 +12,6 @@
 
 from testtools import (
     Matcher, # check that Matcher is exposed at the top level for docs.
-    skipIf,
     TestCase,
     )
 from testtools.compat import (
@@ -29,6 +28,7 @@
     AnnotatedMismatch,
     _BinaryMismatch,
     Contains,
+    ContainsAll,
     DirContains,
     DirExists,
     DocTestMatches,
@@ -409,6 +409,23 @@
     describe_examples = [("1 not in 2", 2, Contains(1))]
 
 
+class TestContainsAllInterface(TestCase, TestMatchersInterface):
+
+    matches_matcher = ContainsAll(['foo', 'bar'])
+    matches_matches = ['foobar', 'foozbar', 'bar foo']
+    matches_mismatches = ['f', 'foo', 'foob', 'baz']
+
+    str_examples = [(
+        "MatchesAll(Contains('foo'), Contains('bar'))",
+        ContainsAll(['foo', 'bar'])),
+        ]
+
+    describe_examples = [("""Differences: [
+'baz' not in 'foo'
+]""",
+    'foo', ContainsAll(['foo', 'baz']))]
+
+
 def make_error(type, *args, **kwargs):
     try:
         raise type(*args, **kwargs)


Follow ups