Debian Codegraph @

Lib/test/test_difflib.py

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