michael@0: import pymake.data, pymake.functions, pymake.util michael@0: import unittest michael@0: import re michael@0: from cStringIO import StringIO michael@0: michael@0: def multitest(cls): michael@0: for name in cls.testdata.iterkeys(): michael@0: def m(self, name=name): michael@0: return self.runSingle(*self.testdata[name]) michael@0: michael@0: setattr(cls, 'test_%s' % name, m) michael@0: return cls michael@0: michael@0: class SplitWordsTest(unittest.TestCase): michael@0: testdata = ( michael@0: (' test test.c test.o ', ['test', 'test.c', 'test.o']), michael@0: ('\ttest\t test.c \ntest.o', ['test', 'test.c', 'test.o']), michael@0: ) michael@0: michael@0: def runTest(self): michael@0: for s, e in self.testdata: michael@0: w = s.split() michael@0: self.assertEqual(w, e, 'splitwords(%r)' % (s,)) michael@0: michael@0: class GetPatSubstTest(unittest.TestCase): michael@0: testdata = ( michael@0: ('%.c', '%.o', ' test test.c test.o ', 'test test.o test.o'), michael@0: ('%', '%.o', ' test.c test.o ', 'test.c.o test.o.o'), michael@0: ('foo', 'bar', 'test foo bar', 'test bar bar'), michael@0: ('foo', '%bar', 'test foo bar', 'test %bar bar'), michael@0: ('%', 'perc_%', 'path', 'perc_path'), michael@0: ('\\%', 'sub%', 'p %', 'p sub%'), michael@0: ('%.c', '\\%%.o', 'foo.c bar.o baz.cpp', '%foo.o bar.o baz.cpp'), michael@0: ) michael@0: michael@0: def runTest(self): michael@0: for s, r, d, e in self.testdata: michael@0: words = d.split() michael@0: p = pymake.data.Pattern(s) michael@0: a = ' '.join((p.subst(r, word, False) michael@0: for word in words)) michael@0: self.assertEqual(a, e, 'Pattern(%r).subst(%r, %r)' % (s, r, d)) michael@0: michael@0: class LRUTest(unittest.TestCase): michael@0: # getkey, expected, funccount, debugitems michael@0: expected = ( michael@0: (0, '', 1, (0,)), michael@0: (0, '', 2, (0,)), michael@0: (1, ' ', 3, (1, 0)), michael@0: (1, ' ', 3, (1, 0)), michael@0: (0, '', 4, (0, 1)), michael@0: (2, ' ', 5, (2, 0, 1)), michael@0: (1, ' ', 5, (1, 2, 0)), michael@0: (3, ' ', 6, (3, 1, 2)), michael@0: ) michael@0: michael@0: def spaceFunc(self, l): michael@0: self.funccount += 1 michael@0: return ''.ljust(l) michael@0: michael@0: def runTest(self): michael@0: self.funccount = 0 michael@0: c = pymake.util.LRUCache(3, self.spaceFunc, lambda k, v: k % 2) michael@0: self.assertEqual(tuple(c.debugitems()), ()) michael@0: michael@0: for i in xrange(0, len(self.expected)): michael@0: k, e, fc, di = self.expected[i] michael@0: michael@0: v = c.get(k) michael@0: self.assertEqual(v, e) michael@0: self.assertEqual(self.funccount, fc, michael@0: "funccount, iteration %i, got %i expected %i" % (i, self.funccount, fc)) michael@0: goti = tuple(c.debugitems()) michael@0: self.assertEqual(goti, di, michael@0: "debugitems, iteration %i, got %r expected %r" % (i, goti, di)) michael@0: michael@0: class EqualityTest(unittest.TestCase): michael@0: def test_string_expansion(self): michael@0: s1 = pymake.data.StringExpansion('foo bar', None) michael@0: s2 = pymake.data.StringExpansion('foo bar', None) michael@0: michael@0: self.assertEqual(s1, s2) michael@0: michael@0: def test_expansion_simple(self): michael@0: s1 = pymake.data.Expansion(None) michael@0: s2 = pymake.data.Expansion(None) michael@0: michael@0: self.assertEqual(s1, s2) michael@0: michael@0: s1.appendstr('foo') michael@0: s2.appendstr('foo') michael@0: self.assertEqual(s1, s2) michael@0: michael@0: def test_expansion_string_finish(self): michael@0: """Adjacent strings should normalize to same value.""" michael@0: s1 = pymake.data.Expansion(None) michael@0: s2 = pymake.data.Expansion(None) michael@0: michael@0: s1.appendstr('foo') michael@0: s2.appendstr('foo') michael@0: michael@0: s1.appendstr(' bar') michael@0: s1.appendstr(' baz') michael@0: s2.appendstr(' bar baz') michael@0: michael@0: self.assertEqual(s1, s2) michael@0: michael@0: def test_function(self): michael@0: s1 = pymake.data.Expansion(None) michael@0: s2 = pymake.data.Expansion(None) michael@0: michael@0: n1 = pymake.data.StringExpansion('FOO', None) michael@0: n2 = pymake.data.StringExpansion('FOO', None) michael@0: michael@0: v1 = pymake.functions.VariableRef(None, n1) michael@0: v2 = pymake.functions.VariableRef(None, n2) michael@0: michael@0: s1.appendfunc(v1) michael@0: s2.appendfunc(v2) michael@0: michael@0: self.assertEqual(s1, s2) michael@0: michael@0: michael@0: class StringExpansionTest(unittest.TestCase): michael@0: def test_base_expansion_interface(self): michael@0: s1 = pymake.data.StringExpansion('FOO', None) michael@0: michael@0: self.assertTrue(s1.is_static_string) michael@0: michael@0: funcs = list(s1.functions()) michael@0: self.assertEqual(len(funcs), 0) michael@0: michael@0: funcs = list(s1.functions(True)) michael@0: self.assertEqual(len(funcs), 0) michael@0: michael@0: refs = list(s1.variable_references()) michael@0: self.assertEqual(len(refs), 0) michael@0: michael@0: michael@0: class ExpansionTest(unittest.TestCase): michael@0: def test_is_static_string(self): michael@0: e1 = pymake.data.Expansion() michael@0: e1.appendstr('foo') michael@0: michael@0: self.assertTrue(e1.is_static_string) michael@0: michael@0: e1.appendstr('bar') michael@0: self.assertTrue(e1.is_static_string) michael@0: michael@0: vname = pymake.data.StringExpansion('FOO', None) michael@0: func = pymake.functions.VariableRef(None, vname) michael@0: michael@0: e1.appendfunc(func) michael@0: michael@0: self.assertFalse(e1.is_static_string) michael@0: michael@0: def test_get_functions(self): michael@0: e1 = pymake.data.Expansion() michael@0: e1.appendstr('foo') michael@0: michael@0: vname1 = pymake.data.StringExpansion('FOO', None) michael@0: vname2 = pymake.data.StringExpansion('BAR', None) michael@0: michael@0: func1 = pymake.functions.VariableRef(None, vname1) michael@0: func2 = pymake.functions.VariableRef(None, vname2) michael@0: michael@0: e1.appendfunc(func1) michael@0: e1.appendfunc(func2) michael@0: michael@0: funcs = list(e1.functions()) michael@0: self.assertEqual(len(funcs), 2) michael@0: michael@0: func3 = pymake.functions.SortFunction(None) michael@0: func3.append(vname1) michael@0: michael@0: e1.appendfunc(func3) michael@0: michael@0: funcs = list(e1.functions()) michael@0: self.assertEqual(len(funcs), 3) michael@0: michael@0: refs = list(e1.variable_references()) michael@0: self.assertEqual(len(refs), 2) michael@0: michael@0: def test_get_functions_descend(self): michael@0: e1 = pymake.data.Expansion() michael@0: vname1 = pymake.data.StringExpansion('FOO', None) michael@0: func1 = pymake.functions.VariableRef(None, vname1) michael@0: e2 = pymake.data.Expansion() michael@0: e2.appendfunc(func1) michael@0: michael@0: func2 = pymake.functions.SortFunction(None) michael@0: func2.append(e2) michael@0: michael@0: e1.appendfunc(func2) michael@0: michael@0: funcs = list(e1.functions()) michael@0: self.assertEqual(len(funcs), 1) michael@0: michael@0: funcs = list(e1.functions(True)) michael@0: self.assertEqual(len(funcs), 2) michael@0: michael@0: self.assertTrue(isinstance(funcs[0], pymake.functions.SortFunction)) michael@0: michael@0: def test_is_filesystem_dependent(self): michael@0: e = pymake.data.Expansion() michael@0: vname1 = pymake.data.StringExpansion('FOO', None) michael@0: func1 = pymake.functions.VariableRef(None, vname1) michael@0: e.appendfunc(func1) michael@0: michael@0: self.assertFalse(e.is_filesystem_dependent) michael@0: michael@0: func2 = pymake.functions.WildcardFunction(None) michael@0: func2.append(vname1) michael@0: e.appendfunc(func2) michael@0: michael@0: self.assertTrue(e.is_filesystem_dependent) michael@0: michael@0: def test_is_filesystem_dependent_descend(self): michael@0: sort = pymake.functions.SortFunction(None) michael@0: wildcard = pymake.functions.WildcardFunction(None) michael@0: michael@0: e = pymake.data.StringExpansion('foo/*', None) michael@0: wildcard.append(e) michael@0: michael@0: e = pymake.data.Expansion(None) michael@0: e.appendfunc(wildcard) michael@0: michael@0: sort.append(e) michael@0: michael@0: e = pymake.data.Expansion(None) michael@0: e.appendfunc(sort) michael@0: michael@0: self.assertTrue(e.is_filesystem_dependent) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()