Rev 2927: * The ``exclude_pattern`` and ``random_order`` parameters to the function in http://people.ubuntu.com/~robertc/baz2.0/in-module-parameterisation

Robert Collins robertc at robertcollins.net
Sun Oct 21 01:27:06 BST 2007


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

------------------------------------------------------------
revno: 2927
revision-id: robertc at robertcollins.net-20071021002645-gyplhevz6aghcrbw
parent: robertc at robertcollins.net-20071020234331-djq3v6djt8dptt22
committer: Robert Collins <robertc at robertcollins.net>
branch nick: in-module-parameterisation
timestamp: Sun 2007-10-21 10:26:45 +1000
message:
  * The ``exclude_pattern`` and ``random_order`` parameters to the function
    ``bzrlib.tests.filter_suite_by_re`` have been deprecated. (Robert Collins)
  * The method ``bzrlib.tests.sort_suite_by_re`` has been deprecated. It is 
    replaced by the new helper methods added in this release. (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-20 23:43:31 +0000
+++ b/NEWS	2007-10-21 00:26:45 +0000
@@ -276,9 +276,11 @@
    * New transport implementation ``trace+`` which is useful for testing,
      logging activity taken to its _activity attribute. (Robert Collins)
 
-   * The ``exclude_pattern`` parameter to the ``bzrlib.tests.`` functions
-     ``filter_suite_by_re`` and ``sort_suite_by_re`` has been deprecated.
-     (Robert Collins)
+   * The ``exclude_pattern`` and ``random_order`` parameters to the function
+     ``bzrlib.tests.filter_suite_by_re`` have been deprecated. (Robert Collins)
+
+   * The method ``bzrlib.tests.sort_suite_by_re`` has been deprecated. It is 
+     replaced by the new helper methods added in this release. (Robert Collins)
 
    * When running bzr commands within the test suite, internal exceptions are
      not caught and reported in the usual way, but rather allowed to propagate

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-10-20 23:43:31 +0000
+++ b/bzrlib/tests/__init__.py	2007-10-21 00:26:45 +0000
@@ -78,8 +78,9 @@
 from bzrlib import symbol_versioning
 from bzrlib.symbol_versioning import (
     DEPRECATED_PARAMETER,
+    deprecated_function,
+    deprecated_method,
     deprecated_passed,
-    deprecated_method,
     zero_ninetyone,
     zero_ninetytwo,
     )
@@ -2197,7 +2198,7 @@
 
 
 def filter_suite_by_re(suite, pattern, exclude_pattern=DEPRECATED_PARAMETER,
-                       random_order=False):
+                       random_order=DEPRECATED_PARAMETER):
     """Create a test suite by filtering another one.
     
     :param suite:           the source suite
@@ -2205,8 +2206,9 @@
     :param exclude_pattern: A pattern that names must not match. This parameter
         is deprecated as of bzrlib 0.92. Please use the separate function
         exclude_tests_by_re instead.
-    :param random_order:    if True, tests in the new suite will be put in
-                            random order
+    :param random_order:    If True, tests in the new suite will be put in
+        random order. This parameter is deprecated as of bzrlib 0.92. Please
+        use the separate function randomise_suite instead.
     :returns: the newly created suite
     """ 
     if deprecated_passed(exclude_pattern):
@@ -2222,8 +2224,12 @@
         if filter_re.search(test_id):
             result.append(test)
     result_suite = TestUtil.TestSuite(result)
-    if random_order:
-        result_suite = randomise_suite(result_suite)
+    if deprecated_passed(random_order):
+        symbol_versioning.warn(
+            zero_ninetytwo % "passing random_order to filter_suite_by_re",
+                DeprecationWarning, stacklevel=2)
+        if random_order:
+            result_suite = randomise_suite(result_suite)
     return result_suite
 
 
@@ -2246,16 +2252,38 @@
     return TestUtil.TestSuite(result)
 
 
+def preserve_input(something):
+    """A helper for performing test suite transformation chains.
+
+    :param something: Anything you want to preserve.
+    :return: Something.
+    """
+    return something
+
+
 def randomise_suite(suite):
-    """Return a new TestSuite with suite's tests in random order."""
+    """Return a new TestSuite with suite's tests in random order.
+    
+    The tests in the input suite are flattened into a single suite in order to
+    accomplish this. Any nested TestSuites are removed to provide global
+    randomness.
+    """
     tests = list(iter_suite_tests(suite))
     random.shuffle(tests)
     return TestUtil.TestSuite(tests)
 
 
-def sort_suite_by_re(suite, pattern, exclude_pattern=DEPRECATED_PARAMETER,
+ at deprecated_function(zero_ninetytwo)
+def sort_suite_by_re(suite, pattern, exclude_pattern=None,
                      random_order=False, append_rest=True):
-    """Create a test suite by sorting another one.
+    """DEPRECATED: Create a test suite by sorting another one.
+
+    This method has been decomposed into separate helper methods that should be
+    called directly:
+     - filter_suite_by_re
+     - exclude_tests_by_re
+     - randomise_suite
+     - split_suite_by_re
     
     :param suite:           the source suite
     :param pattern:         pattern that names must match in order to go
@@ -2268,22 +2296,17 @@
                             just an ordering directive
     :returns: the newly created suite
     """ 
-    if deprecated_passed(exclude_pattern):
-        symbol_versioning.warn(
-            zero_ninetytwo % "passing exclude_pattern to filter_suite_by_re",
-                DeprecationWarning, stacklevel=2)
-        if exclude_pattern is not None:
-            suite = exclude_tests_by_re(suite, exclude_pattern)
+    if exclude_pattern is not None:
+        suite = exclude_tests_by_re(suite, exclude_pattern)
+    if random_order:
+        order_changer = randomise_suite
+    else:
+        order_changer = preserve_input
     if append_rest:
-        suites = split_suite_by_re(suite, pattern)
+        suites = map(order_changer, split_suite_by_re(suite, pattern))
+        return TestUtil.TestSuite(suites)
     else:
-        suites = [filter_suite_by_re(suite, pattern)]
-    out_tests = []
-    for suite in suites:
-        if random_order:
-            suite = randomise_suite(suite)
-        out_tests.append(suite)
-    return TestUtil.TestSuite(out_tests)
+        return order_changer(filter_suite_by_re(suite, pattern))
 
 
 def split_suite_by_re(suite, pattern):
@@ -2349,13 +2372,17 @@
         random.seed(random_seed)
     # Customise the list of tests if requested
     if exclude_pattern is not None:
-        suite = exclude_pattern(suite, pattern)
+        suite = exclude_tests_by_re(suite, exclude_pattern)
+    if random_order:
+        order_changer = randomise_suite
+    else:
+        order_changer = preserve_input
     if pattern != '.*' or random_order:
         if matching_tests_first:
-            suite = sort_suite_by_re(suite, pattern, random_order=random_order)
+            suites = map(order_change, rsplit_suite_by_re(suite, pattern))
+            suite = TestUtil.TestSuite(suites)
         else:
-            suite = filter_suite_by_re(suite, pattern,
-                random_order=random_order)
+            suite = order_changer(filter_suite_by_re(suite, pattern))
     result = runner.run(suite)
 
     if strict:

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2007-10-20 23:01:54 +0000
+++ b/bzrlib/tests/test_selftest.py	2007-10-21 00:26:45 +0000
@@ -36,8 +36,9 @@
 from bzrlib.progress import _BaseProgressBar
 from bzrlib.repofmt import weaverepo
 from bzrlib.symbol_versioning import (
+        zero_eleven,
+        zero_ninetytwo,
         zero_ten,
-        zero_eleven,
         )
 from bzrlib.tests import (
                           ChrootedTestCase,
@@ -57,6 +58,7 @@
                           exclude_tests_by_re,
                           filter_suite_by_re,
                           iter_suite_tests,
+                          preserve_input,
                           randomise_suite,
                           sort_suite_by_re,
                           split_suite_by_re,
@@ -1671,6 +1673,11 @@
         self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
             'TestSelftestFiltering.test_filter_suite_by_re'])
 
+    def test_preserve_input(self):
+        # NB: Surely this is something in the stdlib to do this?
+        self.assertTrue(self.suite is preserve_input(self.suite))
+        self.assertTrue("@#$" is preserve_input("@#$"))
+
     def test_randomise_suite(self):
         randomised_suite = randomise_suite(self.suite)
         # randomising should not add or remove test names.
@@ -1688,7 +1695,8 @@
             len(self._test_ids(randomised_suite)))
 
     def test_sort_suite_by_re(self):
-        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
+        sorted_suite = self.applyDeprecated(zero_ninetytwo,
+            sort_suite_by_re, self.suite, 'test_filter')
         sorted_names = self._test_ids(sorted_suite)
         self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
             'TestSelftestFiltering.test_filter_suite_by_re')



More information about the bazaar-commits mailing list