Browse Source

Refactor: init DiffMarker object with flags.

Do not test internal methods.
Matt Wang 6 years ago
parent
commit
31c3213ad0
2 changed files with 42 additions and 48 deletions
  1. 28
    24
      cdiff.py
  2. 14
    24
      tests/test_cdiff.py

+ 28
- 24
cdiff.py View File

460
 
460
 
461
 class DiffMarker(object):
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
         """Returns a generator"""
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
         else:
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
     def _markup_traditional(self, diff):
478
     def _markup_traditional(self, diff):
477
         """Returns a generator"""
479
         """Returns a generator"""
506
                 else:
508
                 else:
507
                     yield self._markup_common(' ' + old[1])
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
         """Returns a generator"""
512
         """Returns a generator"""
511
 
513
 
512
         def _normalize(line):
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
             """Wrap input text which contains mdiff tags, markup at the
521
             """Wrap input text which contains mdiff tags, markup at the
518
             meantime
522
             meantime
519
             """
523
             """
558
         num_width = max(len(str(max1)), len(str(max2)))
562
         num_width = max(len(str(max1)), len(str(max2)))
559
 
563
 
560
         # Set up line width
564
         # Set up line width
565
+        width = self._width
561
         if width <= 0:
566
         if width <= 0:
562
             # Autodetection of text width according to terminal size
567
             # Autodetection of text width according to terminal size
563
             try:
568
             try:
615
                         left = self._markup_old(left)
620
                         left = self._markup_old(left)
616
                         right = ''
621
                         right = ''
617
                     else:
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
                 else:
625
                 else:
621
                     left = self._markup_common(left)
626
                     left = self._markup_common(left)
622
                     right = self._markup_common(right)
627
                     right = self._markup_common(right)
623
 
628
 
624
-                if wrap:
629
+                if self._wrap:
625
                     # Need to wrap long lines, so here we'll iterate,
630
                     # Need to wrap long lines, so here we'll iterate,
626
                     # shaving off `width` chars from both left and right
631
                     # shaving off `width` chars from both left and right
627
                     # strings, until both are empty. Also, line number needs to
632
                     # strings, until both are empty. Also, line number needs to
718
         pager_cmd, stdin=subprocess.PIPE, stdout=sys.stdout)
723
         pager_cmd, stdin=subprocess.PIPE, stdout=sys.stdout)
719
 
724
 
720
     diffs = DiffParser(stream).get_diff_generator()
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
     pager.stdin.close()
733
     pager.stdin.close()
730
     pager.wait()
734
     pager.wait()

+ 14
- 24
tests/test_cdiff.py View File

135
             ['header\n'], '--- old\n', '+++ new\n', [hunk])
135
             ['header\n'], '--- old\n', '+++ new\n', [hunk])
136
         return diff
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
     def test_markup_traditional_hunk_header(self):
138
     def test_markup_traditional_hunk_header(self):
149
         hunk = cdiff.Hunk(['hunk header\n'], '@@ -0 +0 @@\n', (0, 0), (0, 0))
139
         hunk = cdiff.Hunk(['hunk header\n'], '@@ -0 +0 @@\n', (0, 0), (0, 0))
150
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
140
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
151
         marker = cdiff.DiffMarker()
141
         marker = cdiff.DiffMarker()
152
 
142
 
153
-        out = list(marker._markup_traditional(diff))
143
+        out = list(marker.markup(diff))
154
         self.assertEqual(len(out), 4)
144
         self.assertEqual(len(out), 4)
155
 
145
 
156
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
146
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
164
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
154
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
165
         marker = cdiff.DiffMarker()
155
         marker = cdiff.DiffMarker()
166
 
156
 
167
-        out = list(marker._markup_traditional(diff))
157
+        out = list(marker.markup(diff))
168
         self.assertEqual(len(out), 4)
158
         self.assertEqual(len(out), 4)
169
 
159
 
170
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
160
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
178
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
168
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
179
         marker = cdiff.DiffMarker()
169
         marker = cdiff.DiffMarker()
180
 
170
 
181
-        out = list(marker._markup_traditional(diff))
171
+        out = list(marker.markup(diff))
182
         self.assertEqual(len(out), 4)
172
         self.assertEqual(len(out), 4)
183
 
173
 
184
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
174
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
194
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
184
         diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
195
         marker = cdiff.DiffMarker()
185
         marker = cdiff.DiffMarker()
196
 
186
 
197
-        out = list(marker._markup_traditional(diff))
187
+        out = list(marker.markup(diff))
198
         self.assertEqual(len(out), 6)
188
         self.assertEqual(len(out), 6)
199
 
189
 
200
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
190
         self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
212
 
202
 
213
     def test_markup_side_by_side_padded(self):
203
     def test_markup_side_by_side_padded(self):
214
         diff = self._init_diff()
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
         self.assertEqual(len(out), 11)
208
         self.assertEqual(len(out), 11)
219
 
209
 
220
         sys.stdout.write('\n')
210
         sys.stdout.write('\n')
266
     # This test is not valid anymore
256
     # This test is not valid anymore
267
     def __test_markup_side_by_side_neg_width(self):
257
     def __test_markup_side_by_side_neg_width(self):
268
         diff = self._init_diff()
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
         self.assertEqual(len(out), 11)
261
         self.assertEqual(len(out), 11)
272
 
262
 
273
         self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
263
         self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
310
 
300
 
311
     def test_markup_side_by_side_off_by_one(self):
301
     def test_markup_side_by_side_off_by_one(self):
312
         diff = self._init_diff()
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
         self.assertEqual(len(out), 11)
305
         self.assertEqual(len(out), 11)
316
 
306
 
317
         sys.stdout.write('\n')
307
         sys.stdout.write('\n')
361
 
351
 
362
     def test_markup_side_by_side_wrapped(self):
352
     def test_markup_side_by_side_wrapped(self):
363
         diff = self._init_diff()
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
         self.assertEqual(len(out), 11)
356
         self.assertEqual(len(out), 11)
367
 
357
 
368
         sys.stdout.write('\n')
358
         sys.stdout.write('\n')
413
 
403
 
414
     def test_markup_side_by_side_tabbed(self):
404
     def test_markup_side_by_side_tabbed(self):
415
         diff = self._init_diff()
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
         self.assertEqual(len(out), 11)
408
         self.assertEqual(len(out), 11)
419
 
409
 
420
         sys.stdout.write('\n')
410
         sys.stdout.write('\n')