Rev 2930: * New helper function ``bzrlib.tests.condition_id_re`` which helps in http://people.ubuntu.com/~robertc/baz2.0/in-module-parameterisation

Robert Collins robertc at robertcollins.net
Sun Oct 21 11:22:53 BST 2007


At http://people.ubuntu.com/~robertc/baz2.0/in-module-parameterisation

------------------------------------------------------------
revno: 2930
revision-id: robertc at robertcollins.net-20071021102231-yvyauo1uy0oxt04i
parent: robertc at robertcollins.net-20071021021936-jlz4q3xd60fc84z8
committer: Robert Collins <robertc at robertcollins.net>
branch nick: in-module-parameterisation
timestamp: Sun 2007-10-21 20:22:31 +1000
message:
  * New helper function ``bzrlib.tests.condition_id_re`` which helps
    filter tests based on a regular expression search on the tests id.
    (Robert Collins)
  * New helper function ``bzrlib.tests.exclude_suite_by_condition`` which
    generalises the ``exclude_suite_by_re`` function. (Robert Collins)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_selftest.py  test_selftest.py-20051202044319-c110a115d8c0456a
=== modified file 'NEWS'
--- a/NEWS	2007-10-21 02:19:36 +0000
+++ b/NEWS	2007-10-21 10:22:31 +0000
@@ -263,9 +263,16 @@
 
   TESTING:
 
+   * New helper function ``bzrlib.tests.condition_id_re`` which helps
+     filter tests based on a regular expression search on the tests id.
+     (Robert Collins)
+    
    * New helper function ``bzrlib.tests.condition_isinstance`` which helps
      filter tests based on class. (Robert Collins)
     
+   * New helper function ``bzrlib.tests.exclude_suite_by_condition`` which
+     generalises the ``exclude_suite_by_re`` function. (Robert Collins)
+
    * New helper function ``bzrlib.tests.filter_suite_by_condition`` which
      generalises the ``filter_suite_by_re`` function. (Robert Collins)
 

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-10-21 02:19:36 +0000
+++ b/bzrlib/tests/__init__.py	2007-10-21 10:22:31 +0000
@@ -2197,6 +2197,19 @@
             self.transport_readonly_server = HttpServer
 
 
+def condition_id_re(pattern):
+    """Create a condition filter which performs a re check on a test's id.
+    
+    :param pattern: A regular expression string.
+    :return: A callable that returns True if the re matches.
+    """
+    filter_re = re.compile(pattern)
+    def condition(test):
+        test_id = test.id()
+        return filter_re.search(test_id)
+    return condition
+
+
 def condition_isinstance(klass_or_klass_list):
     """Create a condition filter which returns isinstance(param, klass).
     
@@ -2208,12 +2221,28 @@
     return condition
 
 
+def exclude_tests_by_condition(suite, condition):
+    """Create a test suite which excludes some tests from suite.
+
+    :param suite: The suite to get tests from.
+    :param condition: A callable whose result evaluates True when called with a
+        test case which should be excluded from the result.
+    :return: A suite which contains the tests found in suite that fail
+        condition.
+    """
+    result = []
+    for test in iter_suite_tests(suite):
+        if not condition(test):
+            result.append(test)
+    return TestUtil.TestSuite(result)
+
+
 def filter_suite_by_condition(suite, condition):
     """Create a test suite by filtering another one.
     
     :param suite: The source suite.
     :param condition: A callable whose result evaluates True when called with a
-        test case.
+        test case which should be included in the result.
     :return: A suite which contains the tests found in suite that pass
         condition.
     """ 
@@ -2244,10 +2273,7 @@
                 DeprecationWarning, stacklevel=2)
         if exclude_pattern is not None:
             suite = exclude_tests_by_re(suite, exclude_pattern)
-    filter_re = re.compile(pattern)
-    def condition(test):
-        test_id = test.id()
-        return filter_re.search(test_id)
+    condition = condition_id_re(pattern)
     result_suite = filter_suite_by_condition(suite, condition)
     if deprecated_passed(random_order):
         symbol_versioning.warn(
@@ -2268,13 +2294,7 @@
         tests that matched pattern. The order of tests is the same as it was in
         suite.
     """
-    result = []
-    exclude_re = re.compile(pattern)
-    for test in iter_suite_tests(suite):
-        test_id = test.id()
-        if not exclude_re.search(test_id):
-            result.append(test)
-    return TestUtil.TestSuite(result)
+    return exclude_tests_by_condition(suite, condition_id_re(pattern))
 
 
 def preserve_input(something):

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2007-10-21 02:19:36 +0000
+++ b/bzrlib/tests/test_selftest.py	2007-10-21 10:22:31 +0000
@@ -55,7 +55,9 @@
                           TestUtil,
                           TextTestRunner,
                           UnavailableFeature,
+                          condition_id_re,
                           condition_isinstance,
+                          exclude_tests_by_condition,
                           exclude_tests_by_re,
                           filter_suite_by_condition,
                           filter_suite_by_re,
@@ -1657,6 +1659,13 @@
         """Get the ids for the tests in a test suite."""
         return [t.id() for t in iter_suite_tests(test_suite)]
 
+    def test_condition_id_re(self):
+        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
+            'test_condition_id_re')
+        filtered_suite = filter_suite_by_condition(self.suite,
+            condition_id_re('test_condition_id_re'))
+        self.assertEqual([test_name], self._test_ids(filtered_suite))
+
     def test_condition_isinstance(self):
         filtered_suite = filter_suite_by_condition(self.suite,
             condition_isinstance(self.__class__))
@@ -1665,6 +1674,18 @@
         self.assertEqual(self._test_ids(re_filtered),
             self._test_ids(filtered_suite))
 
+    def test_exclude_tests_by_condition(self):
+        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
+            'test_exclude_tests_by_condition')
+        filtered_suite = exclude_tests_by_condition(self.suite,
+            lambda x:x.id() == excluded_name)
+        self.assertEqual(len(self.all_names) - 1,
+            filtered_suite.countTestCases())
+        self.assertFalse(excluded_name in self._test_ids(filtered_suite))
+        remaining_names = list(self.all_names)
+        remaining_names.remove(excluded_name)
+        self.assertEqual(remaining_names, self._test_ids(filtered_suite))
+
     def test_exclude_tests_by_re(self):
         self.all_names = self._test_ids(self.suite)
         filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')



More information about the bazaar-commits mailing list