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

Robert Collins robertc at robertcollins.net
Sun Oct 21 03:09:20 BST 2007


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

------------------------------------------------------------
revno: 2928
revision-id: robertc at robertcollins.net-20071021020852-1o0jfwqhyb04fp0f
parent: robertc at robertcollins.net-20071021002645-gyplhevz6aghcrbw
committer: Robert Collins <robertc at robertcollins.net>
branch nick: in-module-parameterisation
timestamp: Sun 2007-10-21 12:08:52 +1000
message:
  * New helper function ``bzrlib.tests.filter_suite_by_condition`` which
    generalises the ``filter_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 00:26:45 +0000
+++ b/NEWS	2007-10-21 02:08:52 +0000
@@ -263,6 +263,9 @@
 
   TESTING:
 
+   * New helper function ``bzrlib.tests.filter_suite_by_condition`` which
+     generalises the ``filter_suite_by_re`` function. (Robert Collins)
+
    * New helper method ``bzrlib.tests.exclude_tests_by_re`` which gives a new
      TestSuite that does not contain tests from the input that matched a
      regular expression. (Robert Collins)

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-10-21 00:26:45 +0000
+++ b/bzrlib/tests/__init__.py	2007-10-21 02:08:52 +0000
@@ -2197,6 +2197,22 @@
             self.transport_readonly_server = HttpServer
 
 
+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.
+    :return: A suite which contains the tests found in suite that pass
+        condition.
+    """ 
+    result = []
+    for test in iter_suite_tests(suite):
+        if condition(test):
+            result.append(test)
+    return TestUtil.TestSuite(result)
+
+
 def filter_suite_by_re(suite, pattern, exclude_pattern=DEPRECATED_PARAMETER,
                        random_order=DEPRECATED_PARAMETER):
     """Create a test suite by filtering another one.
@@ -2217,13 +2233,11 @@
                 DeprecationWarning, stacklevel=2)
         if exclude_pattern is not None:
             suite = exclude_tests_by_re(suite, exclude_pattern)
-    result = []
     filter_re = re.compile(pattern)
-    for test in iter_suite_tests(suite):
+    def condition(test):
         test_id = test.id()
-        if filter_re.search(test_id):
-            result.append(test)
-    result_suite = TestUtil.TestSuite(result)
+        return filter_re.search(test_id)
+    result_suite = filter_suite_by_condition(suite, condition)
     if deprecated_passed(random_order):
         symbol_versioning.warn(
             zero_ninetytwo % "passing random_order to filter_suite_by_re",

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2007-10-21 00:26:45 +0000
+++ b/bzrlib/tests/test_selftest.py	2007-10-21 02:08:52 +0000
@@ -56,6 +56,7 @@
                           TextTestRunner,
                           UnavailableFeature,
                           exclude_tests_by_re,
+                          filter_suite_by_condition,
                           filter_suite_by_re,
                           iter_suite_tests,
                           preserve_input,
@@ -1667,8 +1668,15 @@
         remaining_names.remove(excluded_name)
         self.assertEqual(remaining_names, self._test_ids(filtered_suite))
 
+    def test_filter_suite_by_condition(self):
+        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
+            'test_filter_suite_by_condition')
+        filtered_suite = filter_suite_by_condition(self.suite,
+            lambda x:x.id() == test_name)
+        self.assertEqual([test_name], self._test_ids(filtered_suite))
+
     def test_filter_suite_by_re(self):
-        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
+        filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
         filtered_names = self._test_ids(filtered_suite)
         self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
             'TestSelftestFiltering.test_filter_suite_by_re'])
@@ -1696,7 +1704,7 @@
 
     def test_sort_suite_by_re(self):
         sorted_suite = self.applyDeprecated(zero_ninetytwo,
-            sort_suite_by_re, self.suite, 'test_filter')
+            sort_suite_by_re, self.suite, 'test_filter_suite_by_r')
         sorted_names = self._test_ids(sorted_suite)
         self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
             'TestSelftestFiltering.test_filter_suite_by_re')
@@ -1704,7 +1712,7 @@
 
     def test_split_suit_by_re(self):
         self.all_names = self._test_ids(self.suite)
-        split_suite = split_suite_by_re(self.suite, 'test_filter')
+        split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
         filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
             'test_filter_suite_by_re')
         self.assertEqual([filtered_name], self._test_ids(split_suite[0]))



More information about the bazaar-commits mailing list