Rev 3893: Refactor delta.show() tests. in file:///net/bigmamac/Volumes/home/vila/src/bzr/cases/3232-spurious-conflict/

Vincent Ladeuil v.ladeuil+lp at free.fr
Thu Jan 22 10:49:13 GMT 2009


At file:///net/bigmamac/Volumes/home/vila/src/bzr/cases/3232-spurious-conflict/

------------------------------------------------------------
revno: 3893
revision-id: v.ladeuil+lp at free.fr-20090122104907-v0jsuvgmk2gx25dq
parent: v.ladeuil+lp at free.fr-20090122101300-ngpxt42zur06y0s9
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: delta-show-with-path-filter
timestamp: Thu 2009-01-22 11:49:07 +0100
message:
  Refactor delta.show() tests.
  
  * bzrlib/tests/test_delta.py:
  (load_tests): Created to handle parametrized tests.
  (delta_show_scenarios): Create scenarios for short and long status
  forms.
  (TestDeltaShow): Rewrite tessts as parametrized ones.
-------------- next part --------------
=== modified file 'bzrlib/tests/test_delta.py'
--- a/bzrlib/tests/test_delta.py	2008-12-10 09:33:06 +0000
+++ b/bzrlib/tests/test_delta.py	2009-01-22 10:49:07 +0000
@@ -24,6 +24,22 @@
     )
 
 
+def load_tests(standard_tests, module, loader):
+    """Multiply tests for tranport implementations."""
+    result = loader.suiteClass()
+    delta_show_tests, other_tests = tests.split_suite_by_condition(
+        standard_tests,
+        tests.condition_isinstance(TestDeltaShow))
+
+    result.addTests(other_tests)
+
+    adapter = tests.TestScenarioApplier()
+    adapter.scenarios = delta_show_scenarios()
+    tests.adapt_tests(delta_show_tests, adapter, result)
+
+    return result
+
+
 class InstrumentedReporter(object):
     def __init__(self):
         self.calls = []
@@ -241,6 +257,14 @@
         self.assertTrue(delta.touches_file_id('file-id'))
 
 
+def delta_show_scenarios():
+    """Create scenarios for delta.show() tests"""
+    scenarios = []
+    scenarios.append(('short', dict(short_status=True)))
+    scenarios.append(('long', dict(short_status=False)))
+    return scenarios
+
+
 class TestDeltaShow(tests.TestCaseWithTransport):
 
     def _get_delta(self):
@@ -257,65 +281,79 @@
                ['f1-id', 'f2-id', 'f3-id', 'f4-id', 'dir-id'])
         wt.commit('commit one', rev_id='1')
 
-        long_status = """added:
+        repo = wt.branch.repository
+        delta = wt.changes_from(repo.revision_tree(_mod_revision.NULL_REVISION))
+
+        if self.short_status:
+            expected_status = """A  dir/
+A  f1
+A  f2
+A  f3
+A  f4
+"""
+        else:
+            expected_status = """added:
   dir/
   f1
   f2
   f3
   f4
 """
-        short_status = """A  dir/
-A  f1
-A  f2
-A  f3
-A  f4
-"""
-
-        repo = wt.branch.repository
-        d = wt.changes_from(repo.revision_tree(_mod_revision.NULL_REVISION))
-        return d, long_status, short_status
-
-    def test_delta_show_short_status_no_filter(self):
-        d, long_status, short_status = self._get_delta()
-        out = StringIO()
-        d.show(out, short_status=True)
-        self.assertEquals(short_status, out.getvalue())
-
-    def test_delta_show_long_status_no_filter(self):
-        d, long_status, short_status = self._get_delta()
-        out = StringIO()
-        d.show(out, short_status=False)
-        self.assertEquals(long_status, out.getvalue())
-
-    def test_delta_show_no_filter(self):
-        d, long_status, short_status = self._get_delta()
-        out = StringIO()
-        def not_a_filter(path, file_id):
+        return delta, expected_status
+
+    def _show(self, delta, **kwargs):
+        out = StringIO()
+        delta.show(out, short_status=self.short_status, **kwargs)
+        return out.getvalue()
+
+    # This kind of duplicate some knowledge embedded in delta.show(), but at
+    # leasst that gives a single point to modify if tests break.
+    long_status_form = dict(A='added',
+                            D='removed',
+                            K='kind changed',
+                            M='modified',
+                            R='renamed',
+                            )
+
+    def _build_status(self, short_action, file_list):
+        """Imitate statuses produced by delta.show()
+
+        We don't try to be perfect, instead we do the minimal work to allow
+        tests to be written for short and long status but don't go into details
+        about path formatting for example. I.e. we don't try to emulate
+        show_path().
+        """
+        if self.short_status:
+            status= ''
+            file_statuses = ['%s  %s\n' % (short_action, f) for  f in file_list]
+        else:
+            status = '%s:\n' % self.long_status_form[short_action]
+            file_statuses = ['  %s\n' % f for  f in file_list]
+        status += ''.join(file_statuses)
+        return status
+
+    def test_without_filter(self):
+        delta, expected_status = self._get_delta()
+        actual_status = self._show(delta)
+        self.assertEquals(expected_status, actual_status)
+
+    def test_with_dummy_filter(self):
+        delta, expected_status = self._get_delta()
+        def dummy_filter(path, file_id):
             return True
-        d.show(out, short_status=True, filter=not_a_filter)
-        self.assertEquals(short_status, out.getvalue())
-
-    def test_delta_show_short_status_single_file_filter(self):
-        d, long_status, short_status = self._get_delta()
-        out = StringIO()
-        def only_f2(path, file_id):
-            return path == 'f2'
-        d.show(out, short_status=True, filter=only_f2)
-        self.assertEquals("A  f2\n", out.getvalue())
-
-    def test_delta_show_long_status_single_file_filter(self):
-        d, long_status, short_status = self._get_delta()
-        out = StringIO()
-        def only_f2(path, file_id):
-            return path == 'f2'
-        d.show(out, short_status=False, filter=only_f2)
-        self.assertEquals("added:\n  f2\n", out.getvalue())
-
-    def test_delta_show_short_status_single_file_id_filter(self):
-        d, long_status, short_status = self._get_delta()
-        out = StringIO()
+        actual_status = self._show(delta, filter=dummy_filter)
+        self.assertEquals(expected_status, actual_status)
+
+    def test_with_single_file_filter(self):
+        delta, _ = self._get_delta()
+        def only_f2(path, file_id):
+            return path == 'f2'
+        actual_status = self._show(delta, filter=only_f2)
+        self.assertEquals(self._build_status('A', ['f2']), actual_status)
+
+    def test_with_single_file_id_filter(self):
+        delta, _ = self._get_delta()
         def only_f2_id(path, file_id):
             return file_id == 'f2-id'
-        d.show(out, short_status=True, filter=only_f2_id)
-        self.assertEquals("A  f2\n", out.getvalue())
-
+        actual_status = self._show(delta, filter=only_f2_id)
+        self.assertEquals(self._build_status('A', ['f2']), actual_status)



More information about the bazaar-commits mailing list