diff options
Diffstat (limited to 'src/engine/SCons/UtilTests.py')
| -rw-r--r-- | src/engine/SCons/UtilTests.py | 98 |
1 files changed, 74 insertions, 24 deletions
diff --git a/src/engine/SCons/UtilTests.py b/src/engine/SCons/UtilTests.py index c52c910..1e56dc0 100644 --- a/src/engine/SCons/UtilTests.py +++ b/src/engine/SCons/UtilTests.py @@ -125,7 +125,9 @@ class UtilTestCase(unittest.TestCase): def tree_case_2(self, prune=1): """Fixture for the render_tree() and print_tree() tests.""" - stdlib_h = self.Node("stdlib.h") + types_h = self.Node('types.h') + malloc_h = self.Node('malloc.h') + stdlib_h = self.Node('stdlib.h', [types_h, malloc_h]) bar_h = self.Node('bar.h', [stdlib_h]) blat_h = self.Node('blat.h', [stdlib_h]) blat_c = self.Node('blat.c', [blat_h, bar_h]) @@ -136,13 +138,18 @@ class UtilTestCase(unittest.TestCase): +-blat.c +-blat.h | +-stdlib.h + | +-types.h + | +-malloc.h +-bar.h - +-[stdlib.h] """ - - if not prune: - expect = expect.replace('[', '') - expect = expect.replace(']', '') + if prune: + expect += """ +-[stdlib.h] +""" + else: + expect += """ +-stdlib.h + +-types.h + +-malloc.h +""" lines = expect.split('\n')[:-1] lines = ['[E BSPACN ]'+l for l in lines] @@ -163,6 +170,13 @@ class UtilTestCase(unittest.TestCase): actual = render_tree(node, get_children, 1) assert expect == actual, (expect, actual) + # Ensure that we can call render_tree on the same Node + # again. This wasn't possible in version 2.4.1 and earlier + # due to a bug in render_tree (visited was set to {} as default + # parameter) + actual = render_tree(node, get_children, 1) + assert expect == actual, (expect, actual) + def test_print_tree(self): """Test the print_tree() function""" def get_children(node): @@ -183,24 +197,39 @@ class UtilTestCase(unittest.TestCase): actual = sys.stdout.getvalue() assert withtags == actual, (withtags, actual) + # Test that explicitly setting prune to zero works + # the same as the default (see above) node, expect, withtags = self.tree_case_2(prune=0) sys.stdout = io.StringIO() + print_tree(node, get_children, 0) + actual = sys.stdout.getvalue() + assert expect == actual, (expect, actual) + + sys.stdout = io.StringIO() + print_tree(node, get_children, 0, showtags=1) + actual = sys.stdout.getvalue() + assert withtags == actual, (withtags, actual) + + # Test output with prune=1 + node, expect, withtags = self.tree_case_2(prune=1) + + sys.stdout = io.StringIO() print_tree(node, get_children, 1) actual = sys.stdout.getvalue() assert expect == actual, (expect, actual) + # Ensure that we can call print_tree on the same Node + # again. This wasn't possible in version 2.4.1 and earlier + # due to a bug in print_tree (visited was set to {} as default + # parameter) sys.stdout = io.StringIO() - # The following call should work here: - # print_tree(node, get_children, 1, showtags=1) - # For some reason I don't understand, though, *this* - # time that we call print_tree, the visited dictionary - # is still populated with the values from the last call! - # I can't see why this would be, short of a bug in Python, - # and rather than continue banging my head against the - # brick wall for a *test*, we're going to going with - # the cheap, easy workaround: - print_tree(node, get_children, 1, showtags=1, visited={}) + print_tree(node, get_children, 1) + actual = sys.stdout.getvalue() + assert expect == actual, (expect, actual) + + sys.stdout = io.StringIO() + print_tree(node, get_children, 1, showtags=1) actual = sys.stdout.getvalue() assert withtags == actual, (withtags, actual) finally: @@ -488,6 +517,30 @@ class UtilTestCase(unittest.TestCase): p1 = AppendPath(p1,r'C:\dir\num\three',sep = ';') assert(p1 == r'C:\dir\num\one;C:\dir\num\two;C:\dir\num\three') + def test_addPathIfNotExists(self): + """Test the AddPathIfNotExists() function""" + env_dict = { 'FOO' : os.path.normpath('/foo/bar') + os.pathsep + \ + os.path.normpath('/baz/blat'), + 'BAR' : os.path.normpath('/foo/bar') + os.pathsep + \ + os.path.normpath('/baz/blat'), + 'BLAT' : [ os.path.normpath('/foo/bar'), + os.path.normpath('/baz/blat') ] } + AddPathIfNotExists(env_dict, 'FOO', os.path.normpath('/foo/bar')) + AddPathIfNotExists(env_dict, 'BAR', os.path.normpath('/bar/foo')) + AddPathIfNotExists(env_dict, 'BAZ', os.path.normpath('/foo/baz')) + AddPathIfNotExists(env_dict, 'BLAT', os.path.normpath('/baz/blat')) + AddPathIfNotExists(env_dict, 'BLAT', os.path.normpath('/baz/foo')) + + assert env_dict['FOO'] == os.path.normpath('/foo/bar') + os.pathsep + \ + os.path.normpath('/baz/blat'), env_dict['FOO'] + assert env_dict['BAR'] == os.path.normpath('/bar/foo') + os.pathsep + \ + os.path.normpath('/foo/bar') + os.pathsep + \ + os.path.normpath('/baz/blat'), env_dict['BAR'] + assert env_dict['BAZ'] == os.path.normpath('/foo/baz'), env_dict['BAZ'] + assert env_dict['BLAT'] == [ os.path.normpath('/baz/foo'), + os.path.normpath('/foo/bar'), + os.path.normpath('/baz/blat') ], env_dict['BLAT' ] + def test_CLVar(self): """Test the command-line construction variable class""" f = SCons.Util.CLVar('a b') @@ -592,10 +645,11 @@ class UtilTestCase(unittest.TestCase): class MyNode(object): def __init__(self, name): self.name = name - self.suffix = os.path.splitext(name)[1] def __str__(self): return self.name + def get_suffix(self): + return os.path.splitext(self.name)[1] s = Selector({'a' : 'AAA', 'b' : 'BBB'}) assert s['a'] == 'AAA', s['a'] @@ -679,12 +733,7 @@ bling \ bling \ bling bling """ - try: - fobj = io.StringIO(content) - except TypeError: - # Python 2.7 and beyond require unicode strings. - fobj = io.StringIO(u(content)) - + fobj = io.StringIO(unicode(content)) lines = LogicalLines(fobj).readlines() assert lines == [ '\n', @@ -696,7 +745,7 @@ bling def test_intern(self): s1 = silent_intern("spam") - # Python 3.x does not have a unicode() global function + # TODO: Python 3.x does not have a unicode() global function if sys.version[0] == '2': s2 = silent_intern(unicode("unicode spam")) s3 = silent_intern(42) @@ -780,6 +829,7 @@ class flattenTestCase(unittest.TestCase): result = flatten('xyz') assert result == ['xyz'], result + if __name__ == "__main__": suite = unittest.TestSuite() tclasses = [ dictifyTestCase, |
