Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 import difflib | 1 import difflib |
2 from test.support import run_unittest, findfile | 2 from test.support import run_unittest, findfile |
3 import unittest | 3 import unittest |
4 import doctest | 4 import doctest |
5 import sys | 5 import sys |
6 | 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_bjunk(self): | |
33 sm = difflib.SequenceMatcher(isjunk=lambda x: x == ' ', | |
34 a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40) | |
35 self.assertEqual(sm.bjunk, set()) | |
36 | |
37 sm = difflib.SequenceMatcher(isjunk=lambda x: x == ' ', | |
38 a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40 + ' ' * 20) | |
39 self.assertEqual(sm.bjunk, {' '}) | |
40 | |
41 sm = difflib.SequenceMatcher(isjunk=lambda x: x in [' ', 'b'], | |
42 a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40 + ' ' * 20) | |
43 self.assertEqual(sm.bjunk, {' ', 'b'}) | |
44 | |
45 | |
46 class TestAutojunk(unittest.TestCase): | |
47 """Tests for the autojunk parameter added in 2.7""" | |
48 def test_one_insert_homogenous_sequence(self): | |
49 # By default autojunk=True and the heuristic kicks in for a sequence | |
50 # of length 200+ | |
51 seq1 = 'b' * 200 | |
52 seq2 = 'a' + 'b' * 200 | |
53 | |
54 sm = difflib.SequenceMatcher(None, seq1, seq2) | |
55 self.assertAlmostEqual(sm.ratio(), 0, places=3) | |
56 self.assertEqual(sm.bpopular, {'b'}) | |
57 | |
58 # Now turn the heuristic off | |
59 sm = difflib.SequenceMatcher(None, seq1, seq2, autojunk=False) | |
60 self.assertAlmostEqual(sm.ratio(), 0.9975, places=3) | |
61 self.assertEqual(sm.bpopular, set()) | |
62 | |
63 | |
7 class TestSFbugs(unittest.TestCase): | 64 class TestSFbugs(unittest.TestCase): |
8 | |
9 def test_ratio_for_null_seqn(self): | 65 def test_ratio_for_null_seqn(self): |
10 # Check clearing of SF bug 763023 | 66 # Check clearing of SF bug 763023 |
11 s = difflib.SequenceMatcher(None, [], []) | 67 s = difflib.SequenceMatcher(None, [], []) |
12 self.assertEqual(s.ratio(), 1) | 68 self.assertEqual(s.ratio(), 1) |
13 self.assertEqual(s.quick_ratio(), 1) | 69 self.assertEqual(s.quick_ratio(), 1) |
14 self.assertEqual(s.real_quick_ratio(), 1) | 70 self.assertEqual(s.real_quick_ratio(), 1) |
15 | 71 |
16 def test_comparing_empty_lists(self): | 72 def test_comparing_empty_lists(self): |
17 # Check fix for bug #979794 | 73 # Check fix for bug #979794 |
18 group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes() | 74 group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes() |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 | 230 |
175 def test_no_trailing_tab_on_empty_filedate(self): | 231 def test_no_trailing_tab_on_empty_filedate(self): |
176 args = ['one', 'two', 'Original', 'Current'] | 232 args = ['one', 'two', 'Original', 'Current'] |
177 ud = difflib.unified_diff(*args, lineterm='') | 233 ud = difflib.unified_diff(*args, lineterm='') |
178 self.assertEqual(list(ud)[0:2], ["--- Original", "+++ Current"]) | 234 self.assertEqual(list(ud)[0:2], ["--- Original", "+++ Current"]) |
179 | 235 |
180 cd = difflib.context_diff(*args, lineterm='') | 236 cd = difflib.context_diff(*args, lineterm='') |
181 self.assertEqual(list(cd)[0:2], ["*** Original", "--- Current"]) | 237 self.assertEqual(list(cd)[0:2], ["*** Original", "--- Current"]) |
182 | 238 |
183 | 239 |
184 class TestDifflib(unittest.TestCase): | 240 class TestNoNewLineAtEnd(unittest.TestCase): |
185 def test_unified_no_eol(self): | 241 """ Test no newline at end of file, from issue2142 """ |
242 def test_unified_diff(self): | |
186 lines = list(difflib.unified_diff( | 243 lines = list(difflib.unified_diff( |
187 "one\ntwo\nthree".splitlines(1), | 244 "one\ntwo\nthree".splitlines(1), |
188 "one\ntwo\ntrois".splitlines(1), | 245 "one\ntwo\ntrois".splitlines(1), |
189 "Before", "After")) | 246 "Before", "After")) |
190 expected = [ | 247 expected = [ |
191 '--- Before\n', | 248 '--- Before\n', |
192 '+++ After\n', | 249 '+++ After\n', |
193 '@@ -1,3 +1,3 @@\n', | 250 '@@ -1,3 +1,3 @@\n', |
194 ' one\n', | 251 ' one\n', |
195 ' two\n', | 252 ' two\n', |
196 '-three\n', | 253 '-three\n', |
197 '\\ No newline at end of file\n', | 254 '\\ No newline at end of file\n', |
198 '+trois\n', | 255 '+trois\n', |
199 '\\ No newline at end of file\n' | 256 '\\ No newline at end of file\n' |
200 ] | 257 ] |
201 self.assertEqual(lines, expected, | 258 self.assertEqual(lines, expected) |
202 "unexpected diff output:\n-- expected this --\n%s\n" | 259 |
merwok
2010/12/02 23:48:47
Current version of unittest prints a diff for free
| |
203 "-- got this --\n%s\n" | 260 def test_context_diff(self): |
204 % (''.join(expected), ''.join(lines))) | |
205 | |
206 def test_context_no_eol(self): | |
207 lines = list(difflib.context_diff( | 261 lines = list(difflib.context_diff( |
208 "one\ntwo\nthree".splitlines(1), | 262 "one\ntwo\nthree".splitlines(1), |
209 "one\ntwo\ntrois".splitlines(1), | 263 "one\ntwo\ntrois".splitlines(1), |
210 "Before", "After")) | 264 "Before", "After")) |
211 expected = [ | 265 expected = [ |
212 '*** Before\n', | 266 '*** Before\n', |
213 '--- After\n', | 267 '--- After\n', |
214 '***************\n', | 268 '***************\n', |
215 '*** 1,3 ****\n', | 269 '*** 1,3 ****\n', |
216 ' one\n', | 270 ' one\n', |
217 ' two\n', | 271 ' two\n', |
218 '! three\n', | 272 '! three\n', |
219 '\\ No newline at end of file\n', | 273 '\\ No newline at end of file\n', |
220 '--- 1,3 ----\n', | 274 '--- 1,3 ----\n', |
221 ' one\n', | 275 ' one\n', |
222 ' two\n', | 276 ' two\n', |
223 '! trois\n', | 277 '! trois\n', |
224 '\\ No newline at end of file\n', | 278 '\\ No newline at end of file\n', |
225 ] | 279 ] |
226 self.assertEqual(lines, expected, | 280 self.assertEqual(lines, expected) |
227 "unexpected diff output!\n-- expected this --\n%s\n" | |
228 "-- got this --\n%s\n" | |
229 % (''.join(expected), ''.join(lines))) | |
230 | 281 |
231 | 282 |
232 def test_main(): | 283 def test_main(): |
233 difflib.HtmlDiff._default_prefix = 0 | 284 difflib.HtmlDiff._default_prefix = 0 |
234 Doctests = doctest.DocTestSuite(difflib) | 285 Doctests = doctest.DocTestSuite(difflib) |
235 run_unittest(TestSFpatches, TestSFbugs, TestOutputFormat, Doctests,\ | 286 run_unittest( |
merwok
2010/12/02 23:48:47
Please don’t escape newlines, it’s error-prone.
| |
236 TestDifflib) | 287 TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, |
288 TestOutputFormat, TestNoNewLineAtEnd, Doctests) | |
237 | 289 |
238 if __name__ == '__main__': | 290 if __name__ == '__main__': |
239 test_main() | 291 test_main() |
LEFT | RIGHT |