Преглед на файлове

Refactor: init DiffMarker object with flags.

Do not test internal methods.
Matt Wang преди 6 години
родител
ревизия
31c3213ad0
променени са 2 файла, в които са добавени 42 реда и са изтрити 48 реда
  1. 28
    24
      cdiff.py
  2. 14
    24
      tests/test_cdiff.py

+ 28
- 24
cdiff.py Целия файл

@@ -460,18 +460,20 @@ class DiffParser(object):
460 460
 
461 461
 class DiffMarker(object):
462 462
 
463
-    def markup(self, diffs, side_by_side=False, width=0, tab_width=8,
464
-               wrap=False):
463
+    def __init__(self, side_by_side=False, width=0, tab_width=8, wrap=False):
464
+        self._side_by_side = side_by_side
465
+        self._width = width
466
+        self._tab_width = tab_width
467
+        self._wrap = wrap
468
+
469
+    def markup(self, diff):
465 470
         """Returns a generator"""
466
-        if side_by_side:
467
-            for diff in diffs:
468
-                for line in self._markup_side_by_side(diff, width, tab_width,
469
-                                                      wrap):
470
-                    yield line
471
+        if self._side_by_side:
472
+            for line in self._markup_side_by_side(diff):
473
+                yield line
471 474
         else:
472
-            for diff in diffs:
473
-                for line in self._markup_traditional(diff):
474
-                    yield line
475
+            for line in self._markup_traditional(diff):
476
+                yield line
475 477
 
476 478
     def _markup_traditional(self, diff):
477 479
         """Returns a generator"""
@@ -506,14 +508,16 @@ class DiffMarker(object):
506 508
                 else:
507 509
                     yield self._markup_common(' ' + old[1])
508 510
 
509
-    def _markup_side_by_side(self, diff, width, tab_width, wrap):
511
+    def _markup_side_by_side(self, diff):
510 512
         """Returns a generator"""
511 513
 
512 514
         def _normalize(line):
513
-            return line.replace(
514
-                '\t', ' ' * tab_width).replace('\n', '').replace('\r', '')
515
+            return (line
516
+                    .replace('\t', ' ' * self._tab_width)
517
+                    .replace('\n', '')
518
+                    .replace('\r', ''))
515 519
 
516
-        def _fit_with_marker_mix(text, base_color, width):
520
+        def _fit_with_marker_mix(text, base_color):
517 521
             """Wrap input text which contains mdiff tags, markup at the
518 522
             meantime
519 523
             """
@@ -558,6 +562,7 @@ class DiffMarker(object):
558 562
         num_width = max(len(str(max1)), len(str(max2)))
559 563
 
560 564
         # Set up line width
565
+        width = self._width
561 566
         if width <= 0:
562 567
             # Autodetection of text width according to terminal size
563 568
             try:
@@ -615,13 +620,13 @@ class DiffMarker(object):
615 620
                         left = self._markup_old(left)
616 621
                         right = ''
617 622
                     else:
618
-                        left = _fit_with_marker_mix(left, 'red', width)
619
-                        right = _fit_with_marker_mix(right, 'green', width)
623
+                        left = _fit_with_marker_mix(left, 'red')
624
+                        right = _fit_with_marker_mix(right, 'green')
620 625
                 else:
621 626
                     left = self._markup_common(left)
622 627
                     right = self._markup_common(right)
623 628
 
624
-                if wrap:
629
+                if self._wrap:
625 630
                     # Need to wrap long lines, so here we'll iterate,
626 631
                     # shaving off `width` chars from both left and right
627 632
                     # strings, until both are empty. Also, line number needs to
@@ -718,13 +723,12 @@ def markup_to_pager(stream, opts):
718 723
         pager_cmd, stdin=subprocess.PIPE, stdout=sys.stdout)
719 724
 
720 725
     diffs = DiffParser(stream).get_diff_generator()
721
-    marker = DiffMarker()
722
-    color_diff = marker.markup(diffs, side_by_side=opts.side_by_side,
723
-                               width=opts.width, tab_width=opts.tab_width,
724
-                               wrap=opts.wrap)
725
-
726
-    for line in color_diff:
727
-        pager.stdin.write(line.encode('utf-8'))
726
+    for diff in diffs:
727
+        marker = DiffMarker(side_by_side=opts.side_by_side, width=opts.width,
728
+                            tab_width=opts.tab_width, wrap=opts.wrap)
729
+        color_diff = marker.markup(diff)
730
+        for line in color_diff:
731
+            pager.stdin.write(line.encode('utf-8'))
728 732
 
729 733
     pager.stdin.close()
730 734
     pager.wait()

+ 14
- 24
tests/test_cdiff.py Целия файл

@@ -135,22 +135,12 @@ class DiffMarkupTest(unittest.TestCase):
135 135
             ['header\n'], '--- old\n', '+++ new\n', [hunk])
136 136
         return diff
137 137
 
138
-    def test_markup_mix(self):
139
-        marker = cdiff.DiffMarker()
140
-        line = 'foo \x00-del\x01 \x00+add\x01 \x00^chg\x01 bar'
141
-        base_color = 'red'
142
-        self.assertEqual(
143
-            marker._markup_mix(line, base_color),
144
-            '\x1b[31mfoo \x1b[7m\x1b[31mdel\x1b[0m\x1b[31m '
145
-            '\x1b[7m\x1b[31madd\x1b[0m\x1b[31m '
146
-            '\x1b[4m\x1b[31mchg\x1b[0m\x1b[31m bar\x1b[0m')
147
-
148 138
     def test_markup_traditional_hunk_header(self):
149 139
         hunk = cdiff.Hunk(['hunk header\n'], '@@ -0 +0 @@\n', (0, 0), (0, 0))
150 140
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
151 141
         marker = cdiff.DiffMarker()
152 142
 
153
-        out = list(marker._markup_traditional(diff))
143
+        out = list(marker.markup(diff))
154 144
         self.assertEqual(len(out), 4)
155 145
 
156 146
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
@@ -164,7 +154,7 @@ class DiffMarkupTest(unittest.TestCase):
164 154
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
165 155
         marker = cdiff.DiffMarker()
166 156
 
167
-        out = list(marker._markup_traditional(diff))
157
+        out = list(marker.markup(diff))
168 158
         self.assertEqual(len(out), 4)
169 159
 
170 160
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
@@ -178,7 +168,7 @@ class DiffMarkupTest(unittest.TestCase):
178 168
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
179 169
         marker = cdiff.DiffMarker()
180 170
 
181
-        out = list(marker._markup_traditional(diff))
171
+        out = list(marker.markup(diff))
182 172
         self.assertEqual(len(out), 4)
183 173
 
184 174
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
@@ -194,7 +184,7 @@ class DiffMarkupTest(unittest.TestCase):
194 184
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
195 185
         marker = cdiff.DiffMarker()
196 186
 
197
-        out = list(marker._markup_traditional(diff))
187
+        out = list(marker.markup(diff))
198 188
         self.assertEqual(len(out), 6)
199 189
 
200 190
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
@@ -212,9 +202,9 @@ class DiffMarkupTest(unittest.TestCase):
212 202
 
213 203
     def test_markup_side_by_side_padded(self):
214 204
         diff = self._init_diff()
215
-        marker = cdiff.DiffMarker()
205
+        marker = cdiff.DiffMarker(side_by_side=True, width=7)
216 206
 
217
-        out = list(marker._markup_side_by_side(diff, 7, 8, False))
207
+        out = list(marker.markup(diff))
218 208
         self.assertEqual(len(out), 11)
219 209
 
220 210
         sys.stdout.write('\n')
@@ -266,8 +256,8 @@ class DiffMarkupTest(unittest.TestCase):
266 256
     # This test is not valid anymore
267 257
     def __test_markup_side_by_side_neg_width(self):
268 258
         diff = self._init_diff()
269
-        marker = cdiff.DiffMarker()
270
-        out = list(marker._markup_side_by_side(diff, -1, 8, False))
259
+        marker = cdiff.DiffMarker(side_by_side=True, width=-1)
260
+        out = list(marker.markup(diff))
271 261
         self.assertEqual(len(out), 11)
272 262
 
273 263
         self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
@@ -310,8 +300,8 @@ class DiffMarkupTest(unittest.TestCase):
310 300
 
311 301
     def test_markup_side_by_side_off_by_one(self):
312 302
         diff = self._init_diff()
313
-        marker = cdiff.DiffMarker()
314
-        out = list(marker._markup_side_by_side(diff, 6, 8, False))
303
+        marker = cdiff.DiffMarker(side_by_side=True, width=6)
304
+        out = list(marker.markup(diff))
315 305
         self.assertEqual(len(out), 11)
316 306
 
317 307
         sys.stdout.write('\n')
@@ -361,8 +351,8 @@ class DiffMarkupTest(unittest.TestCase):
361 351
 
362 352
     def test_markup_side_by_side_wrapped(self):
363 353
         diff = self._init_diff()
364
-        marker = cdiff.DiffMarker()
365
-        out = list(marker._markup_side_by_side(diff, 5, 8, False))
354
+        marker = cdiff.DiffMarker(side_by_side=True, width=5)
355
+        out = list(marker.markup(diff))
366 356
         self.assertEqual(len(out), 11)
367 357
 
368 358
         sys.stdout.write('\n')
@@ -413,8 +403,8 @@ class DiffMarkupTest(unittest.TestCase):
413 403
 
414 404
     def test_markup_side_by_side_tabbed(self):
415 405
         diff = self._init_diff()
416
-        marker = cdiff.DiffMarker()
417
-        out = list(marker._markup_side_by_side(diff, 8, 2, False))
406
+        marker = cdiff.DiffMarker(side_by_side=True, width=8, tab_width=2)
407
+        out = list(marker.markup(diff))
418 408
         self.assertEqual(len(out), 11)
419 409
 
420 410
         sys.stdout.write('\n')