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