1 | n/a | import difflib |
---|
2 | n/a | from test.support import run_unittest, findfile |
---|
3 | n/a | import unittest |
---|
4 | n/a | import doctest |
---|
5 | n/a | import sys |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | class TestWithAscii(unittest.TestCase): |
---|
9 | n/a | def test_one_insert(self): |
---|
10 | n/a | sm = difflib.SequenceMatcher(None, 'b' * 100, 'a' + 'b' * 100) |
---|
11 | n/a | self.assertAlmostEqual(sm.ratio(), 0.995, places=3) |
---|
12 | n/a | self.assertEqual(list(sm.get_opcodes()), |
---|
13 | n/a | [ ('insert', 0, 0, 0, 1), |
---|
14 | n/a | ('equal', 0, 100, 1, 101)]) |
---|
15 | n/a | self.assertEqual(sm.bpopular, set()) |
---|
16 | n/a | sm = difflib.SequenceMatcher(None, 'b' * 100, 'b' * 50 + 'a' + 'b' * 50) |
---|
17 | n/a | self.assertAlmostEqual(sm.ratio(), 0.995, places=3) |
---|
18 | n/a | self.assertEqual(list(sm.get_opcodes()), |
---|
19 | n/a | [ ('equal', 0, 50, 0, 50), |
---|
20 | n/a | ('insert', 50, 50, 50, 51), |
---|
21 | n/a | ('equal', 50, 100, 51, 101)]) |
---|
22 | n/a | self.assertEqual(sm.bpopular, set()) |
---|
23 | n/a | |
---|
24 | n/a | def test_one_delete(self): |
---|
25 | n/a | sm = difflib.SequenceMatcher(None, 'a' * 40 + 'c' + 'b' * 40, 'a' * 40 + 'b' * 40) |
---|
26 | n/a | self.assertAlmostEqual(sm.ratio(), 0.994, places=3) |
---|
27 | n/a | self.assertEqual(list(sm.get_opcodes()), |
---|
28 | n/a | [ ('equal', 0, 40, 0, 40), |
---|
29 | n/a | ('delete', 40, 41, 40, 40), |
---|
30 | n/a | ('equal', 41, 81, 40, 80)]) |
---|
31 | n/a | |
---|
32 | n/a | def test_bjunk(self): |
---|
33 | n/a | sm = difflib.SequenceMatcher(isjunk=lambda x: x == ' ', |
---|
34 | n/a | a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40) |
---|
35 | n/a | self.assertEqual(sm.bjunk, set()) |
---|
36 | n/a | |
---|
37 | n/a | sm = difflib.SequenceMatcher(isjunk=lambda x: x == ' ', |
---|
38 | n/a | a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40 + ' ' * 20) |
---|
39 | n/a | self.assertEqual(sm.bjunk, {' '}) |
---|
40 | n/a | |
---|
41 | n/a | sm = difflib.SequenceMatcher(isjunk=lambda x: x in [' ', 'b'], |
---|
42 | n/a | a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40 + ' ' * 20) |
---|
43 | n/a | self.assertEqual(sm.bjunk, {' ', 'b'}) |
---|
44 | n/a | |
---|
45 | n/a | |
---|
46 | n/a | class TestAutojunk(unittest.TestCase): |
---|
47 | n/a | """Tests for the autojunk parameter added in 2.7""" |
---|
48 | n/a | def test_one_insert_homogenous_sequence(self): |
---|
49 | n/a | # By default autojunk=True and the heuristic kicks in for a sequence |
---|
50 | n/a | # of length 200+ |
---|
51 | n/a | seq1 = 'b' * 200 |
---|
52 | n/a | seq2 = 'a' + 'b' * 200 |
---|
53 | n/a | |
---|
54 | n/a | sm = difflib.SequenceMatcher(None, seq1, seq2) |
---|
55 | n/a | self.assertAlmostEqual(sm.ratio(), 0, places=3) |
---|
56 | n/a | self.assertEqual(sm.bpopular, {'b'}) |
---|
57 | n/a | |
---|
58 | n/a | # Now turn the heuristic off |
---|
59 | n/a | sm = difflib.SequenceMatcher(None, seq1, seq2, autojunk=False) |
---|
60 | n/a | self.assertAlmostEqual(sm.ratio(), 0.9975, places=3) |
---|
61 | n/a | self.assertEqual(sm.bpopular, set()) |
---|
62 | n/a | |
---|
63 | n/a | |
---|
64 | n/a | class TestSFbugs(unittest.TestCase): |
---|
65 | n/a | def test_ratio_for_null_seqn(self): |
---|
66 | n/a | # Check clearing of SF bug 763023 |
---|
67 | n/a | s = difflib.SequenceMatcher(None, [], []) |
---|
68 | n/a | self.assertEqual(s.ratio(), 1) |
---|
69 | n/a | self.assertEqual(s.quick_ratio(), 1) |
---|
70 | n/a | self.assertEqual(s.real_quick_ratio(), 1) |
---|
71 | n/a | |
---|
72 | n/a | def test_comparing_empty_lists(self): |
---|
73 | n/a | # Check fix for bug #979794 |
---|
74 | n/a | group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes() |
---|
75 | n/a | self.assertRaises(StopIteration, next, group_gen) |
---|
76 | n/a | diff_gen = difflib.unified_diff([], []) |
---|
77 | n/a | self.assertRaises(StopIteration, next, diff_gen) |
---|
78 | n/a | |
---|
79 | n/a | def test_matching_blocks_cache(self): |
---|
80 | n/a | # Issue #21635 |
---|
81 | n/a | s = difflib.SequenceMatcher(None, "abxcd", "abcd") |
---|
82 | n/a | first = s.get_matching_blocks() |
---|
83 | n/a | second = s.get_matching_blocks() |
---|
84 | n/a | self.assertEqual(second[0].size, 2) |
---|
85 | n/a | self.assertEqual(second[1].size, 2) |
---|
86 | n/a | self.assertEqual(second[2].size, 0) |
---|
87 | n/a | |
---|
88 | n/a | def test_added_tab_hint(self): |
---|
89 | n/a | # Check fix for bug #1488943 |
---|
90 | n/a | diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"])) |
---|
91 | n/a | self.assertEqual("- \tI am a buggy", diff[0]) |
---|
92 | n/a | self.assertEqual("? --\n", diff[1]) |
---|
93 | n/a | self.assertEqual("+ \t\tI am a bug", diff[2]) |
---|
94 | n/a | self.assertEqual("? +\n", diff[3]) |
---|
95 | n/a | |
---|
96 | n/a | patch914575_from1 = """ |
---|
97 | n/a | 1. Beautiful is beTTer than ugly. |
---|
98 | n/a | 2. Explicit is better than implicit. |
---|
99 | n/a | 3. Simple is better than complex. |
---|
100 | n/a | 4. Complex is better than complicated. |
---|
101 | n/a | """ |
---|
102 | n/a | |
---|
103 | n/a | patch914575_to1 = """ |
---|
104 | n/a | 1. Beautiful is better than ugly. |
---|
105 | n/a | 3. Simple is better than complex. |
---|
106 | n/a | 4. Complicated is better than complex. |
---|
107 | n/a | 5. Flat is better than nested. |
---|
108 | n/a | """ |
---|
109 | n/a | |
---|
110 | n/a | patch914575_nonascii_from1 = """ |
---|
111 | n/a | 1. Beautiful is beTTer than ugly. |
---|
112 | n/a | 2. Explicit is better than ımplıcıt. |
---|
113 | n/a | 3. Simple is better than complex. |
---|
114 | n/a | 4. Complex is better than complicated. |
---|
115 | n/a | """ |
---|
116 | n/a | |
---|
117 | n/a | patch914575_nonascii_to1 = """ |
---|
118 | n/a | 1. Beautiful is better than ügly. |
---|
119 | n/a | 3. Sımple is better than complex. |
---|
120 | n/a | 4. Complicated is better than cömplex. |
---|
121 | n/a | 5. Flat is better than nested. |
---|
122 | n/a | """ |
---|
123 | n/a | |
---|
124 | n/a | patch914575_from2 = """ |
---|
125 | n/a | \t\tLine 1: preceded by from:[tt] to:[ssss] |
---|
126 | n/a | \t\tLine 2: preceded by from:[sstt] to:[sssst] |
---|
127 | n/a | \t \tLine 3: preceded by from:[sstst] to:[ssssss] |
---|
128 | n/a | Line 4: \thas from:[sst] to:[sss] after : |
---|
129 | n/a | Line 5: has from:[t] to:[ss] at end\t |
---|
130 | n/a | """ |
---|
131 | n/a | |
---|
132 | n/a | patch914575_to2 = """ |
---|
133 | n/a | Line 1: preceded by from:[tt] to:[ssss] |
---|
134 | n/a | \tLine 2: preceded by from:[sstt] to:[sssst] |
---|
135 | n/a | Line 3: preceded by from:[sstst] to:[ssssss] |
---|
136 | n/a | Line 4: has from:[sst] to:[sss] after : |
---|
137 | n/a | Line 5: has from:[t] to:[ss] at end |
---|
138 | n/a | """ |
---|
139 | n/a | |
---|
140 | n/a | patch914575_from3 = """line 0 |
---|
141 | n/a | 1234567890123456789012345689012345 |
---|
142 | n/a | line 1 |
---|
143 | n/a | line 2 |
---|
144 | n/a | line 3 |
---|
145 | n/a | line 4 changed |
---|
146 | n/a | line 5 changed |
---|
147 | n/a | line 6 changed |
---|
148 | n/a | line 7 |
---|
149 | n/a | line 8 subtracted |
---|
150 | n/a | line 9 |
---|
151 | n/a | 1234567890123456789012345689012345 |
---|
152 | n/a | short line |
---|
153 | n/a | just fits in!! |
---|
154 | n/a | just fits in two lines yup!! |
---|
155 | n/a | the end""" |
---|
156 | n/a | |
---|
157 | n/a | patch914575_to3 = """line 0 |
---|
158 | n/a | 1234567890123456789012345689012345 |
---|
159 | n/a | line 1 |
---|
160 | n/a | line 2 added |
---|
161 | n/a | line 3 |
---|
162 | n/a | line 4 chanGEd |
---|
163 | n/a | line 5a chanGed |
---|
164 | n/a | line 6a changEd |
---|
165 | n/a | line 7 |
---|
166 | n/a | line 8 |
---|
167 | n/a | line 9 |
---|
168 | n/a | 1234567890 |
---|
169 | n/a | another long line that needs to be wrapped |
---|
170 | n/a | just fitS in!! |
---|
171 | n/a | just fits in two lineS yup!! |
---|
172 | n/a | the end""" |
---|
173 | n/a | |
---|
174 | n/a | class TestSFpatches(unittest.TestCase): |
---|
175 | n/a | |
---|
176 | n/a | def test_html_diff(self): |
---|
177 | n/a | # Check SF patch 914575 for generating HTML differences |
---|
178 | n/a | f1a = ((patch914575_from1 + '123\n'*10)*3) |
---|
179 | n/a | t1a = (patch914575_to1 + '123\n'*10)*3 |
---|
180 | n/a | f1b = '456\n'*10 + f1a |
---|
181 | n/a | t1b = '456\n'*10 + t1a |
---|
182 | n/a | f1a = f1a.splitlines() |
---|
183 | n/a | t1a = t1a.splitlines() |
---|
184 | n/a | f1b = f1b.splitlines() |
---|
185 | n/a | t1b = t1b.splitlines() |
---|
186 | n/a | f2 = patch914575_from2.splitlines() |
---|
187 | n/a | t2 = patch914575_to2.splitlines() |
---|
188 | n/a | f3 = patch914575_from3 |
---|
189 | n/a | t3 = patch914575_to3 |
---|
190 | n/a | i = difflib.HtmlDiff() |
---|
191 | n/a | j = difflib.HtmlDiff(tabsize=2) |
---|
192 | n/a | k = difflib.HtmlDiff(wrapcolumn=14) |
---|
193 | n/a | |
---|
194 | n/a | full = i.make_file(f1a,t1a,'from','to',context=False,numlines=5) |
---|
195 | n/a | tables = '\n'.join( |
---|
196 | n/a | [ |
---|
197 | n/a | '<h2>Context (first diff within numlines=5(default))</h2>', |
---|
198 | n/a | i.make_table(f1a,t1a,'from','to',context=True), |
---|
199 | n/a | '<h2>Context (first diff after numlines=5(default))</h2>', |
---|
200 | n/a | i.make_table(f1b,t1b,'from','to',context=True), |
---|
201 | n/a | '<h2>Context (numlines=6)</h2>', |
---|
202 | n/a | i.make_table(f1a,t1a,'from','to',context=True,numlines=6), |
---|
203 | n/a | '<h2>Context (numlines=0)</h2>', |
---|
204 | n/a | i.make_table(f1a,t1a,'from','to',context=True,numlines=0), |
---|
205 | n/a | '<h2>Same Context</h2>', |
---|
206 | n/a | i.make_table(f1a,f1a,'from','to',context=True), |
---|
207 | n/a | '<h2>Same Full</h2>', |
---|
208 | n/a | i.make_table(f1a,f1a,'from','to',context=False), |
---|
209 | n/a | '<h2>Empty Context</h2>', |
---|
210 | n/a | i.make_table([],[],'from','to',context=True), |
---|
211 | n/a | '<h2>Empty Full</h2>', |
---|
212 | n/a | i.make_table([],[],'from','to',context=False), |
---|
213 | n/a | '<h2>tabsize=2</h2>', |
---|
214 | n/a | j.make_table(f2,t2), |
---|
215 | n/a | '<h2>tabsize=default</h2>', |
---|
216 | n/a | i.make_table(f2,t2), |
---|
217 | n/a | '<h2>Context (wrapcolumn=14,numlines=0)</h2>', |
---|
218 | n/a | k.make_table(f3.splitlines(),t3.splitlines(),context=True,numlines=0), |
---|
219 | n/a | '<h2>wrapcolumn=14,splitlines()</h2>', |
---|
220 | n/a | k.make_table(f3.splitlines(),t3.splitlines()), |
---|
221 | n/a | '<h2>wrapcolumn=14,splitlines(True)</h2>', |
---|
222 | n/a | k.make_table(f3.splitlines(True),t3.splitlines(True)), |
---|
223 | n/a | ]) |
---|
224 | n/a | actual = full.replace('</body>','\n%s\n</body>' % tables) |
---|
225 | n/a | |
---|
226 | n/a | # temporarily uncomment next two lines to baseline this test |
---|
227 | n/a | #with open('test_difflib_expect.html','w') as fp: |
---|
228 | n/a | # fp.write(actual) |
---|
229 | n/a | |
---|
230 | n/a | with open(findfile('test_difflib_expect.html')) as fp: |
---|
231 | n/a | self.assertEqual(actual, fp.read()) |
---|
232 | n/a | |
---|
233 | n/a | def test_recursion_limit(self): |
---|
234 | n/a | # Check if the problem described in patch #1413711 exists. |
---|
235 | n/a | limit = sys.getrecursionlimit() |
---|
236 | n/a | old = [(i%2 and "K:%d" or "V:A:%d") % i for i in range(limit*2)] |
---|
237 | n/a | new = [(i%2 and "K:%d" or "V:B:%d") % i for i in range(limit*2)] |
---|
238 | n/a | difflib.SequenceMatcher(None, old, new).get_opcodes() |
---|
239 | n/a | |
---|
240 | n/a | def test_make_file_default_charset(self): |
---|
241 | n/a | html_diff = difflib.HtmlDiff() |
---|
242 | n/a | output = html_diff.make_file(patch914575_from1.splitlines(), |
---|
243 | n/a | patch914575_to1.splitlines()) |
---|
244 | n/a | self.assertIn('content="text/html; charset=utf-8"', output) |
---|
245 | n/a | |
---|
246 | n/a | def test_make_file_iso88591_charset(self): |
---|
247 | n/a | html_diff = difflib.HtmlDiff() |
---|
248 | n/a | output = html_diff.make_file(patch914575_from1.splitlines(), |
---|
249 | n/a | patch914575_to1.splitlines(), |
---|
250 | n/a | charset='iso-8859-1') |
---|
251 | n/a | self.assertIn('content="text/html; charset=iso-8859-1"', output) |
---|
252 | n/a | |
---|
253 | n/a | def test_make_file_usascii_charset_with_nonascii_input(self): |
---|
254 | n/a | html_diff = difflib.HtmlDiff() |
---|
255 | n/a | output = html_diff.make_file(patch914575_nonascii_from1.splitlines(), |
---|
256 | n/a | patch914575_nonascii_to1.splitlines(), |
---|
257 | n/a | charset='us-ascii') |
---|
258 | n/a | self.assertIn('content="text/html; charset=us-ascii"', output) |
---|
259 | n/a | self.assertIn('ımplıcıt', output) |
---|
260 | n/a | |
---|
261 | n/a | |
---|
262 | n/a | class TestOutputFormat(unittest.TestCase): |
---|
263 | n/a | def test_tab_delimiter(self): |
---|
264 | n/a | args = ['one', 'two', 'Original', 'Current', |
---|
265 | n/a | '2005-01-26 23:30:50', '2010-04-02 10:20:52'] |
---|
266 | n/a | ud = difflib.unified_diff(*args, lineterm='') |
---|
267 | n/a | self.assertEqual(list(ud)[0:2], [ |
---|
268 | n/a | "--- Original\t2005-01-26 23:30:50", |
---|
269 | n/a | "+++ Current\t2010-04-02 10:20:52"]) |
---|
270 | n/a | cd = difflib.context_diff(*args, lineterm='') |
---|
271 | n/a | self.assertEqual(list(cd)[0:2], [ |
---|
272 | n/a | "*** Original\t2005-01-26 23:30:50", |
---|
273 | n/a | "--- Current\t2010-04-02 10:20:52"]) |
---|
274 | n/a | |
---|
275 | n/a | def test_no_trailing_tab_on_empty_filedate(self): |
---|
276 | n/a | args = ['one', 'two', 'Original', 'Current'] |
---|
277 | n/a | ud = difflib.unified_diff(*args, lineterm='') |
---|
278 | n/a | self.assertEqual(list(ud)[0:2], ["--- Original", "+++ Current"]) |
---|
279 | n/a | |
---|
280 | n/a | cd = difflib.context_diff(*args, lineterm='') |
---|
281 | n/a | self.assertEqual(list(cd)[0:2], ["*** Original", "--- Current"]) |
---|
282 | n/a | |
---|
283 | n/a | def test_range_format_unified(self): |
---|
284 | n/a | # Per the diff spec at http://www.unix.org/single_unix_specification/ |
---|
285 | n/a | spec = '''\ |
---|
286 | n/a | Each <range> field shall be of the form: |
---|
287 | n/a | %1d", <beginning line number> if the range contains exactly one line, |
---|
288 | n/a | and: |
---|
289 | n/a | "%1d,%1d", <beginning line number>, <number of lines> otherwise. |
---|
290 | n/a | If a range is empty, its beginning line number shall be the number of |
---|
291 | n/a | the line just before the range, or 0 if the empty range starts the file. |
---|
292 | n/a | ''' |
---|
293 | n/a | fmt = difflib._format_range_unified |
---|
294 | n/a | self.assertEqual(fmt(3,3), '3,0') |
---|
295 | n/a | self.assertEqual(fmt(3,4), '4') |
---|
296 | n/a | self.assertEqual(fmt(3,5), '4,2') |
---|
297 | n/a | self.assertEqual(fmt(3,6), '4,3') |
---|
298 | n/a | self.assertEqual(fmt(0,0), '0,0') |
---|
299 | n/a | |
---|
300 | n/a | def test_range_format_context(self): |
---|
301 | n/a | # Per the diff spec at http://www.unix.org/single_unix_specification/ |
---|
302 | n/a | spec = '''\ |
---|
303 | n/a | The range of lines in file1 shall be written in the following format |
---|
304 | n/a | if the range contains two or more lines: |
---|
305 | n/a | "*** %d,%d ****\n", <beginning line number>, <ending line number> |
---|
306 | n/a | and the following format otherwise: |
---|
307 | n/a | "*** %d ****\n", <ending line number> |
---|
308 | n/a | The ending line number of an empty range shall be the number of the preceding line, |
---|
309 | n/a | or 0 if the range is at the start of the file. |
---|
310 | n/a | |
---|
311 | n/a | Next, the range of lines in file2 shall be written in the following format |
---|
312 | n/a | if the range contains two or more lines: |
---|
313 | n/a | "--- %d,%d ----\n", <beginning line number>, <ending line number> |
---|
314 | n/a | and the following format otherwise: |
---|
315 | n/a | "--- %d ----\n", <ending line number> |
---|
316 | n/a | ''' |
---|
317 | n/a | fmt = difflib._format_range_context |
---|
318 | n/a | self.assertEqual(fmt(3,3), '3') |
---|
319 | n/a | self.assertEqual(fmt(3,4), '4') |
---|
320 | n/a | self.assertEqual(fmt(3,5), '4,5') |
---|
321 | n/a | self.assertEqual(fmt(3,6), '4,6') |
---|
322 | n/a | self.assertEqual(fmt(0,0), '0') |
---|
323 | n/a | |
---|
324 | n/a | |
---|
325 | n/a | class TestBytes(unittest.TestCase): |
---|
326 | n/a | # don't really care about the content of the output, just the fact |
---|
327 | n/a | # that it's bytes and we don't crash |
---|
328 | n/a | def check(self, diff): |
---|
329 | n/a | diff = list(diff) # trigger exceptions first |
---|
330 | n/a | for line in diff: |
---|
331 | n/a | self.assertIsInstance( |
---|
332 | n/a | line, bytes, |
---|
333 | n/a | "all lines of diff should be bytes, but got: %r" % line) |
---|
334 | n/a | |
---|
335 | n/a | def test_byte_content(self): |
---|
336 | n/a | # if we receive byte strings, we return byte strings |
---|
337 | n/a | a = [b'hello', b'andr\xe9'] # iso-8859-1 bytes |
---|
338 | n/a | b = [b'hello', b'andr\xc3\xa9'] # utf-8 bytes |
---|
339 | n/a | |
---|
340 | n/a | unified = difflib.unified_diff |
---|
341 | n/a | context = difflib.context_diff |
---|
342 | n/a | |
---|
343 | n/a | check = self.check |
---|
344 | n/a | check(difflib.diff_bytes(unified, a, a)) |
---|
345 | n/a | check(difflib.diff_bytes(unified, a, b)) |
---|
346 | n/a | |
---|
347 | n/a | # now with filenames (content and filenames are all bytes!) |
---|
348 | n/a | check(difflib.diff_bytes(unified, a, a, b'a', b'a')) |
---|
349 | n/a | check(difflib.diff_bytes(unified, a, b, b'a', b'b')) |
---|
350 | n/a | |
---|
351 | n/a | # and with filenames and dates |
---|
352 | n/a | check(difflib.diff_bytes(unified, a, a, b'a', b'a', b'2005', b'2013')) |
---|
353 | n/a | check(difflib.diff_bytes(unified, a, b, b'a', b'b', b'2005', b'2013')) |
---|
354 | n/a | |
---|
355 | n/a | # same all over again, with context diff |
---|
356 | n/a | check(difflib.diff_bytes(context, a, a)) |
---|
357 | n/a | check(difflib.diff_bytes(context, a, b)) |
---|
358 | n/a | check(difflib.diff_bytes(context, a, a, b'a', b'a')) |
---|
359 | n/a | check(difflib.diff_bytes(context, a, b, b'a', b'b')) |
---|
360 | n/a | check(difflib.diff_bytes(context, a, a, b'a', b'a', b'2005', b'2013')) |
---|
361 | n/a | check(difflib.diff_bytes(context, a, b, b'a', b'b', b'2005', b'2013')) |
---|
362 | n/a | |
---|
363 | n/a | def test_byte_filenames(self): |
---|
364 | n/a | # somebody renamed a file from ISO-8859-2 to UTF-8 |
---|
365 | n/a | fna = b'\xb3odz.txt' # "Åodz.txt" |
---|
366 | n/a | fnb = b'\xc5\x82odz.txt' |
---|
367 | n/a | |
---|
368 | n/a | # they transcoded the content at the same time |
---|
369 | n/a | a = [b'\xa3odz is a city in Poland.'] |
---|
370 | n/a | b = [b'\xc5\x81odz is a city in Poland.'] |
---|
371 | n/a | |
---|
372 | n/a | check = self.check |
---|
373 | n/a | unified = difflib.unified_diff |
---|
374 | n/a | context = difflib.context_diff |
---|
375 | n/a | check(difflib.diff_bytes(unified, a, b, fna, fnb)) |
---|
376 | n/a | check(difflib.diff_bytes(context, a, b, fna, fnb)) |
---|
377 | n/a | |
---|
378 | n/a | def assertDiff(expect, actual): |
---|
379 | n/a | # do not compare expect and equal as lists, because unittest |
---|
380 | n/a | # uses difflib to report difference between lists |
---|
381 | n/a | actual = list(actual) |
---|
382 | n/a | self.assertEqual(len(expect), len(actual)) |
---|
383 | n/a | for e, a in zip(expect, actual): |
---|
384 | n/a | self.assertEqual(e, a) |
---|
385 | n/a | |
---|
386 | n/a | expect = [ |
---|
387 | n/a | b'--- \xb3odz.txt', |
---|
388 | n/a | b'+++ \xc5\x82odz.txt', |
---|
389 | n/a | b'@@ -1 +1 @@', |
---|
390 | n/a | b'-\xa3odz is a city in Poland.', |
---|
391 | n/a | b'+\xc5\x81odz is a city in Poland.', |
---|
392 | n/a | ] |
---|
393 | n/a | actual = difflib.diff_bytes(unified, a, b, fna, fnb, lineterm=b'') |
---|
394 | n/a | assertDiff(expect, actual) |
---|
395 | n/a | |
---|
396 | n/a | # with dates (plain ASCII) |
---|
397 | n/a | datea = b'2005-03-18' |
---|
398 | n/a | dateb = b'2005-03-19' |
---|
399 | n/a | check(difflib.diff_bytes(unified, a, b, fna, fnb, datea, dateb)) |
---|
400 | n/a | check(difflib.diff_bytes(context, a, b, fna, fnb, datea, dateb)) |
---|
401 | n/a | |
---|
402 | n/a | expect = [ |
---|
403 | n/a | # note the mixed encodings here: this is deeply wrong by every |
---|
404 | n/a | # tenet of Unicode, but it doesn't crash, it's parseable by |
---|
405 | n/a | # patch, and it's how UNIX(tm) diff behaves |
---|
406 | n/a | b'--- \xb3odz.txt\t2005-03-18', |
---|
407 | n/a | b'+++ \xc5\x82odz.txt\t2005-03-19', |
---|
408 | n/a | b'@@ -1 +1 @@', |
---|
409 | n/a | b'-\xa3odz is a city in Poland.', |
---|
410 | n/a | b'+\xc5\x81odz is a city in Poland.', |
---|
411 | n/a | ] |
---|
412 | n/a | actual = difflib.diff_bytes(unified, a, b, fna, fnb, datea, dateb, |
---|
413 | n/a | lineterm=b'') |
---|
414 | n/a | assertDiff(expect, actual) |
---|
415 | n/a | |
---|
416 | n/a | def test_mixed_types_content(self): |
---|
417 | n/a | # type of input content must be consistent: all str or all bytes |
---|
418 | n/a | a = [b'hello'] |
---|
419 | n/a | b = ['hello'] |
---|
420 | n/a | |
---|
421 | n/a | unified = difflib.unified_diff |
---|
422 | n/a | context = difflib.context_diff |
---|
423 | n/a | |
---|
424 | n/a | expect = "lines to compare must be str, not bytes (b'hello')" |
---|
425 | n/a | self._assert_type_error(expect, unified, a, b) |
---|
426 | n/a | self._assert_type_error(expect, unified, b, a) |
---|
427 | n/a | self._assert_type_error(expect, context, a, b) |
---|
428 | n/a | self._assert_type_error(expect, context, b, a) |
---|
429 | n/a | |
---|
430 | n/a | expect = "all arguments must be bytes, not str ('hello')" |
---|
431 | n/a | self._assert_type_error(expect, difflib.diff_bytes, unified, a, b) |
---|
432 | n/a | self._assert_type_error(expect, difflib.diff_bytes, unified, b, a) |
---|
433 | n/a | self._assert_type_error(expect, difflib.diff_bytes, context, a, b) |
---|
434 | n/a | self._assert_type_error(expect, difflib.diff_bytes, context, b, a) |
---|
435 | n/a | |
---|
436 | n/a | def test_mixed_types_filenames(self): |
---|
437 | n/a | # cannot pass filenames as bytes if content is str (this may not be |
---|
438 | n/a | # the right behaviour, but at least the test demonstrates how |
---|
439 | n/a | # things work) |
---|
440 | n/a | a = ['hello\n'] |
---|
441 | n/a | b = ['ohell\n'] |
---|
442 | n/a | fna = b'ol\xe9.txt' # filename transcoded from ISO-8859-1 |
---|
443 | n/a | fnb = b'ol\xc3a9.txt' # to UTF-8 |
---|
444 | n/a | self._assert_type_error( |
---|
445 | n/a | "all arguments must be str, not: b'ol\\xe9.txt'", |
---|
446 | n/a | difflib.unified_diff, a, b, fna, fnb) |
---|
447 | n/a | |
---|
448 | n/a | def test_mixed_types_dates(self): |
---|
449 | n/a | # type of dates must be consistent with type of contents |
---|
450 | n/a | a = [b'foo\n'] |
---|
451 | n/a | b = [b'bar\n'] |
---|
452 | n/a | datea = '1 fév' |
---|
453 | n/a | dateb = '3 fév' |
---|
454 | n/a | self._assert_type_error( |
---|
455 | n/a | "all arguments must be bytes, not str ('1 fév')", |
---|
456 | n/a | difflib.diff_bytes, difflib.unified_diff, |
---|
457 | n/a | a, b, b'a', b'b', datea, dateb) |
---|
458 | n/a | |
---|
459 | n/a | # if input is str, non-ASCII dates are fine |
---|
460 | n/a | a = ['foo\n'] |
---|
461 | n/a | b = ['bar\n'] |
---|
462 | n/a | list(difflib.unified_diff(a, b, 'a', 'b', datea, dateb)) |
---|
463 | n/a | |
---|
464 | n/a | def _assert_type_error(self, msg, generator, *args): |
---|
465 | n/a | with self.assertRaises(TypeError) as ctx: |
---|
466 | n/a | list(generator(*args)) |
---|
467 | n/a | self.assertEqual(msg, str(ctx.exception)) |
---|
468 | n/a | |
---|
469 | n/a | |
---|
470 | n/a | def test_main(): |
---|
471 | n/a | difflib.HtmlDiff._default_prefix = 0 |
---|
472 | n/a | Doctests = doctest.DocTestSuite(difflib) |
---|
473 | n/a | run_unittest( |
---|
474 | n/a | TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, |
---|
475 | n/a | TestOutputFormat, TestBytes, Doctests) |
---|
476 | n/a | |
---|
477 | n/a | if __name__ == '__main__': |
---|
478 | n/a | test_main() |
---|