|
1 import pymake.data, pymake.functions, pymake.util |
|
2 import unittest |
|
3 import re |
|
4 from cStringIO import StringIO |
|
5 |
|
6 def multitest(cls): |
|
7 for name in cls.testdata.iterkeys(): |
|
8 def m(self, name=name): |
|
9 return self.runSingle(*self.testdata[name]) |
|
10 |
|
11 setattr(cls, 'test_%s' % name, m) |
|
12 return cls |
|
13 |
|
14 class SplitWordsTest(unittest.TestCase): |
|
15 testdata = ( |
|
16 (' test test.c test.o ', ['test', 'test.c', 'test.o']), |
|
17 ('\ttest\t test.c \ntest.o', ['test', 'test.c', 'test.o']), |
|
18 ) |
|
19 |
|
20 def runTest(self): |
|
21 for s, e in self.testdata: |
|
22 w = s.split() |
|
23 self.assertEqual(w, e, 'splitwords(%r)' % (s,)) |
|
24 |
|
25 class GetPatSubstTest(unittest.TestCase): |
|
26 testdata = ( |
|
27 ('%.c', '%.o', ' test test.c test.o ', 'test test.o test.o'), |
|
28 ('%', '%.o', ' test.c test.o ', 'test.c.o test.o.o'), |
|
29 ('foo', 'bar', 'test foo bar', 'test bar bar'), |
|
30 ('foo', '%bar', 'test foo bar', 'test %bar bar'), |
|
31 ('%', 'perc_%', 'path', 'perc_path'), |
|
32 ('\\%', 'sub%', 'p %', 'p sub%'), |
|
33 ('%.c', '\\%%.o', 'foo.c bar.o baz.cpp', '%foo.o bar.o baz.cpp'), |
|
34 ) |
|
35 |
|
36 def runTest(self): |
|
37 for s, r, d, e in self.testdata: |
|
38 words = d.split() |
|
39 p = pymake.data.Pattern(s) |
|
40 a = ' '.join((p.subst(r, word, False) |
|
41 for word in words)) |
|
42 self.assertEqual(a, e, 'Pattern(%r).subst(%r, %r)' % (s, r, d)) |
|
43 |
|
44 class LRUTest(unittest.TestCase): |
|
45 # getkey, expected, funccount, debugitems |
|
46 expected = ( |
|
47 (0, '', 1, (0,)), |
|
48 (0, '', 2, (0,)), |
|
49 (1, ' ', 3, (1, 0)), |
|
50 (1, ' ', 3, (1, 0)), |
|
51 (0, '', 4, (0, 1)), |
|
52 (2, ' ', 5, (2, 0, 1)), |
|
53 (1, ' ', 5, (1, 2, 0)), |
|
54 (3, ' ', 6, (3, 1, 2)), |
|
55 ) |
|
56 |
|
57 def spaceFunc(self, l): |
|
58 self.funccount += 1 |
|
59 return ''.ljust(l) |
|
60 |
|
61 def runTest(self): |
|
62 self.funccount = 0 |
|
63 c = pymake.util.LRUCache(3, self.spaceFunc, lambda k, v: k % 2) |
|
64 self.assertEqual(tuple(c.debugitems()), ()) |
|
65 |
|
66 for i in xrange(0, len(self.expected)): |
|
67 k, e, fc, di = self.expected[i] |
|
68 |
|
69 v = c.get(k) |
|
70 self.assertEqual(v, e) |
|
71 self.assertEqual(self.funccount, fc, |
|
72 "funccount, iteration %i, got %i expected %i" % (i, self.funccount, fc)) |
|
73 goti = tuple(c.debugitems()) |
|
74 self.assertEqual(goti, di, |
|
75 "debugitems, iteration %i, got %r expected %r" % (i, goti, di)) |
|
76 |
|
77 class EqualityTest(unittest.TestCase): |
|
78 def test_string_expansion(self): |
|
79 s1 = pymake.data.StringExpansion('foo bar', None) |
|
80 s2 = pymake.data.StringExpansion('foo bar', None) |
|
81 |
|
82 self.assertEqual(s1, s2) |
|
83 |
|
84 def test_expansion_simple(self): |
|
85 s1 = pymake.data.Expansion(None) |
|
86 s2 = pymake.data.Expansion(None) |
|
87 |
|
88 self.assertEqual(s1, s2) |
|
89 |
|
90 s1.appendstr('foo') |
|
91 s2.appendstr('foo') |
|
92 self.assertEqual(s1, s2) |
|
93 |
|
94 def test_expansion_string_finish(self): |
|
95 """Adjacent strings should normalize to same value.""" |
|
96 s1 = pymake.data.Expansion(None) |
|
97 s2 = pymake.data.Expansion(None) |
|
98 |
|
99 s1.appendstr('foo') |
|
100 s2.appendstr('foo') |
|
101 |
|
102 s1.appendstr(' bar') |
|
103 s1.appendstr(' baz') |
|
104 s2.appendstr(' bar baz') |
|
105 |
|
106 self.assertEqual(s1, s2) |
|
107 |
|
108 def test_function(self): |
|
109 s1 = pymake.data.Expansion(None) |
|
110 s2 = pymake.data.Expansion(None) |
|
111 |
|
112 n1 = pymake.data.StringExpansion('FOO', None) |
|
113 n2 = pymake.data.StringExpansion('FOO', None) |
|
114 |
|
115 v1 = pymake.functions.VariableRef(None, n1) |
|
116 v2 = pymake.functions.VariableRef(None, n2) |
|
117 |
|
118 s1.appendfunc(v1) |
|
119 s2.appendfunc(v2) |
|
120 |
|
121 self.assertEqual(s1, s2) |
|
122 |
|
123 |
|
124 class StringExpansionTest(unittest.TestCase): |
|
125 def test_base_expansion_interface(self): |
|
126 s1 = pymake.data.StringExpansion('FOO', None) |
|
127 |
|
128 self.assertTrue(s1.is_static_string) |
|
129 |
|
130 funcs = list(s1.functions()) |
|
131 self.assertEqual(len(funcs), 0) |
|
132 |
|
133 funcs = list(s1.functions(True)) |
|
134 self.assertEqual(len(funcs), 0) |
|
135 |
|
136 refs = list(s1.variable_references()) |
|
137 self.assertEqual(len(refs), 0) |
|
138 |
|
139 |
|
140 class ExpansionTest(unittest.TestCase): |
|
141 def test_is_static_string(self): |
|
142 e1 = pymake.data.Expansion() |
|
143 e1.appendstr('foo') |
|
144 |
|
145 self.assertTrue(e1.is_static_string) |
|
146 |
|
147 e1.appendstr('bar') |
|
148 self.assertTrue(e1.is_static_string) |
|
149 |
|
150 vname = pymake.data.StringExpansion('FOO', None) |
|
151 func = pymake.functions.VariableRef(None, vname) |
|
152 |
|
153 e1.appendfunc(func) |
|
154 |
|
155 self.assertFalse(e1.is_static_string) |
|
156 |
|
157 def test_get_functions(self): |
|
158 e1 = pymake.data.Expansion() |
|
159 e1.appendstr('foo') |
|
160 |
|
161 vname1 = pymake.data.StringExpansion('FOO', None) |
|
162 vname2 = pymake.data.StringExpansion('BAR', None) |
|
163 |
|
164 func1 = pymake.functions.VariableRef(None, vname1) |
|
165 func2 = pymake.functions.VariableRef(None, vname2) |
|
166 |
|
167 e1.appendfunc(func1) |
|
168 e1.appendfunc(func2) |
|
169 |
|
170 funcs = list(e1.functions()) |
|
171 self.assertEqual(len(funcs), 2) |
|
172 |
|
173 func3 = pymake.functions.SortFunction(None) |
|
174 func3.append(vname1) |
|
175 |
|
176 e1.appendfunc(func3) |
|
177 |
|
178 funcs = list(e1.functions()) |
|
179 self.assertEqual(len(funcs), 3) |
|
180 |
|
181 refs = list(e1.variable_references()) |
|
182 self.assertEqual(len(refs), 2) |
|
183 |
|
184 def test_get_functions_descend(self): |
|
185 e1 = pymake.data.Expansion() |
|
186 vname1 = pymake.data.StringExpansion('FOO', None) |
|
187 func1 = pymake.functions.VariableRef(None, vname1) |
|
188 e2 = pymake.data.Expansion() |
|
189 e2.appendfunc(func1) |
|
190 |
|
191 func2 = pymake.functions.SortFunction(None) |
|
192 func2.append(e2) |
|
193 |
|
194 e1.appendfunc(func2) |
|
195 |
|
196 funcs = list(e1.functions()) |
|
197 self.assertEqual(len(funcs), 1) |
|
198 |
|
199 funcs = list(e1.functions(True)) |
|
200 self.assertEqual(len(funcs), 2) |
|
201 |
|
202 self.assertTrue(isinstance(funcs[0], pymake.functions.SortFunction)) |
|
203 |
|
204 def test_is_filesystem_dependent(self): |
|
205 e = pymake.data.Expansion() |
|
206 vname1 = pymake.data.StringExpansion('FOO', None) |
|
207 func1 = pymake.functions.VariableRef(None, vname1) |
|
208 e.appendfunc(func1) |
|
209 |
|
210 self.assertFalse(e.is_filesystem_dependent) |
|
211 |
|
212 func2 = pymake.functions.WildcardFunction(None) |
|
213 func2.append(vname1) |
|
214 e.appendfunc(func2) |
|
215 |
|
216 self.assertTrue(e.is_filesystem_dependent) |
|
217 |
|
218 def test_is_filesystem_dependent_descend(self): |
|
219 sort = pymake.functions.SortFunction(None) |
|
220 wildcard = pymake.functions.WildcardFunction(None) |
|
221 |
|
222 e = pymake.data.StringExpansion('foo/*', None) |
|
223 wildcard.append(e) |
|
224 |
|
225 e = pymake.data.Expansion(None) |
|
226 e.appendfunc(wildcard) |
|
227 |
|
228 sort.append(e) |
|
229 |
|
230 e = pymake.data.Expansion(None) |
|
231 e.appendfunc(sort) |
|
232 |
|
233 self.assertTrue(e.is_filesystem_dependent) |
|
234 |
|
235 |
|
236 if __name__ == '__main__': |
|
237 unittest.main() |