Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / lib / python2.4 / test / test_difflib.py
CommitLineData
920dae64
AT
1import difflib
2from test.test_support import run_unittest, findfile
3import unittest
4import doctest
5
6class TestSFbugs(unittest.TestCase):
7
8 def test_ratio_for_null_seqn(self):
9 # Check clearing of SF bug 763023
10 s = difflib.SequenceMatcher(None, [], [])
11 self.assertEqual(s.ratio(), 1)
12 self.assertEqual(s.quick_ratio(), 1)
13 self.assertEqual(s.real_quick_ratio(), 1)
14
15 def test_comparing_empty_lists(self):
16 # Check fix for bug #979794
17 group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes()
18 self.assertRaises(StopIteration, group_gen.next)
19 diff_gen = difflib.unified_diff([], [])
20 self.assertRaises(StopIteration, diff_gen.next)
21
22patch914575_from1 = """
23 1. Beautiful is beTTer than ugly.
24 2. Explicit is better than implicit.
25 3. Simple is better than complex.
26 4. Complex is better than complicated.
27"""
28
29patch914575_to1 = """
30 1. Beautiful is better than ugly.
31 3. Simple is better than complex.
32 4. Complicated is better than complex.
33 5. Flat is better than nested.
34"""
35
36patch914575_from2 = """
37\t\tLine 1: preceeded by from:[tt] to:[ssss]
38 \t\tLine 2: preceeded by from:[sstt] to:[sssst]
39 \t \tLine 3: preceeded by from:[sstst] to:[ssssss]
40Line 4: \thas from:[sst] to:[sss] after :
41Line 5: has from:[t] to:[ss] at end\t
42"""
43
44patch914575_to2 = """
45 Line 1: preceeded by from:[tt] to:[ssss]
46 \tLine 2: preceeded by from:[sstt] to:[sssst]
47 Line 3: preceeded by from:[sstst] to:[ssssss]
48Line 4: has from:[sst] to:[sss] after :
49Line 5: has from:[t] to:[ss] at end
50"""
51
52patch914575_from3 = """line 0
531234567890123456789012345689012345
54line 1
55line 2
56line 3
57line 4 changed
58line 5 changed
59line 6 changed
60line 7
61line 8 subtracted
62line 9
631234567890123456789012345689012345
64short line
65just fits in!!
66just fits in two lines yup!!
67the end"""
68
69patch914575_to3 = """line 0
701234567890123456789012345689012345
71line 1
72line 2 added
73line 3
74line 4 chanGEd
75line 5a chanGed
76line 6a changEd
77line 7
78line 8
79line 9
801234567890
81another long line that needs to be wrapped
82just fitS in!!
83just fits in two lineS yup!!
84the end"""
85
86class TestSFpatches(unittest.TestCase):
87
88 def test_html_diff(self):
89 # Check SF patch 914575 for generating HTML differences
90 f1a = ((patch914575_from1 + '123\n'*10)*3)
91 t1a = (patch914575_to1 + '123\n'*10)*3
92 f1b = '456\n'*10 + f1a
93 t1b = '456\n'*10 + t1a
94 f1a = f1a.splitlines()
95 t1a = t1a.splitlines()
96 f1b = f1b.splitlines()
97 t1b = t1b.splitlines()
98 f2 = patch914575_from2.splitlines()
99 t2 = patch914575_to2.splitlines()
100 f3 = patch914575_from3
101 t3 = patch914575_to3
102 i = difflib.HtmlDiff()
103 j = difflib.HtmlDiff(tabsize=2)
104 k = difflib.HtmlDiff(wrapcolumn=14)
105
106 full = i.make_file(f1a,t1a,'from','to',context=False,numlines=5)
107 tables = '\n'.join(
108 [
109 '<h2>Context (first diff within numlines=5(default))</h2>',
110 i.make_table(f1a,t1a,'from','to',context=True),
111 '<h2>Context (first diff after numlines=5(default))</h2>',
112 i.make_table(f1b,t1b,'from','to',context=True),
113 '<h2>Context (numlines=6)</h2>',
114 i.make_table(f1a,t1a,'from','to',context=True,numlines=6),
115 '<h2>Context (numlines=0)</h2>',
116 i.make_table(f1a,t1a,'from','to',context=True,numlines=0),
117 '<h2>Same Context</h2>',
118 i.make_table(f1a,f1a,'from','to',context=True),
119 '<h2>Same Full</h2>',
120 i.make_table(f1a,f1a,'from','to',context=False),
121 '<h2>Empty Context</h2>',
122 i.make_table([],[],'from','to',context=True),
123 '<h2>Empty Full</h2>',
124 i.make_table([],[],'from','to',context=False),
125 '<h2>tabsize=2</h2>',
126 j.make_table(f2,t2),
127 '<h2>tabsize=default</h2>',
128 i.make_table(f2,t2),
129 '<h2>Context (wrapcolumn=14,numlines=0)</h2>',
130 k.make_table(f3.splitlines(),t3.splitlines(),context=True,numlines=0),
131 '<h2>wrapcolumn=14,splitlines()</h2>',
132 k.make_table(f3.splitlines(),t3.splitlines()),
133 '<h2>wrapcolumn=14,splitlines(True)</h2>',
134 k.make_table(f3.splitlines(True),t3.splitlines(True)),
135 ])
136 actual = full.replace('</body>','\n%s\n</body>' % tables)
137 # temporarily uncomment next three lines to baseline this test
138 #f = open('test_difflib_expect.html','w')
139 #f.write(actual)
140 #f.close()
141 expect = open(findfile('test_difflib_expect.html')).read()
142
143
144 self.assertEqual(actual,expect)
145
146Doctests = doctest.DocTestSuite(difflib)
147
148run_unittest(TestSFpatches, TestSFbugs, Doctests)