+ assert p.get() == s, p.get()
+
+ def test_display(self):
+ old_stdout = sys.stdout
+ sys.stdout = OutBuffer()
+ display("line1")
+ display.set_mode(0)
+ display("line2")
+ display.set_mode(1)
+ display("line3")
+ display("line4\n", append_newline=0)
+ display.set_mode(0)
+ display("dont print1")
+ display("dont print2\n", append_newline=0)
+ display.set_mode(1)
+ assert sys.stdout.buffer == "line1\nline3\nline4\n"
+ sys.stdout = old_stdout
+
+ def test_get_native_path(self):
+ """Test the get_native_path() function."""
+ import tempfile
+ filename = tempfile.mktemp()
+ str = '1234567890 ' + filename
+ try:
+ open(filename, 'w').write(str)
+ assert open(get_native_path(filename)).read() == str
+ finally:
+ try:
+ os.unlink(filename)
+ except OSError:
+ pass
+
+ def test_PrependPath(self):
+ """Test prepending to a path"""
+ p1 = r'C:\dir\num\one;C:\dir\num\two'
+ p2 = r'C:\mydir\num\one;C:\mydir\num\two'
+ # have to include the pathsep here so that the test will work on UNIX too.
+ p1 = PrependPath(p1,r'C:\dir\num\two',sep = ';')
+ p1 = PrependPath(p1,r'C:\dir\num\three',sep = ';')
+ p2 = PrependPath(p2,r'C:\mydir\num\three',sep = ';')
+ p2 = PrependPath(p2,r'C:\mydir\num\one',sep = ';')
+ assert(p1 == r'C:\dir\num\three;C:\dir\num\two;C:\dir\num\one')
+ assert(p2 == r'C:\mydir\num\one;C:\mydir\num\three;C:\mydir\num\two')
+
+ def test_AppendPath(self):
+ """Test appending to a path."""
+ p1 = r'C:\dir\num\one;C:\dir\num\two'
+ p2 = r'C:\mydir\num\one;C:\mydir\num\two'
+ # have to include the pathsep here so that the test will work on UNIX too.
+ p1 = AppendPath(p1,r'C:\dir\num\two',sep = ';')
+ p1 = AppendPath(p1,r'C:\dir\num\three',sep = ';')
+ p2 = AppendPath(p2,r'C:\mydir\num\three',sep = ';')
+ p2 = AppendPath(p2,r'C:\mydir\num\one',sep = ';')
+ assert(p1 == r'C:\dir\num\one;C:\dir\num\two;C:\dir\num\three')
+ assert(p2 == r'C:\mydir\num\two;C:\mydir\num\three;C:\mydir\num\one')
+
+ def test_PrependPathPreserveOld(self):
+ """Test prepending to a path while preserving old paths"""
+ p1 = r'C:\dir\num\one;C:\dir\num\two'
+ # have to include the pathsep here so that the test will work on UNIX too.
+ p1 = PrependPath(p1,r'C:\dir\num\two',sep = ';', delete_existing=0)
+ p1 = PrependPath(p1,r'C:\dir\num\three',sep = ';')
+ assert(p1 == r'C:\dir\num\three;C:\dir\num\one;C:\dir\num\two')
+
+ def test_AppendPathPreserveOld(self):
+ """Test appending to a path while preserving old paths"""
+ p1 = r'C:\dir\num\one;C:\dir\num\two'
+ # have to include the pathsep here so that the test will work on UNIX too.
+ p1 = AppendPath(p1,r'C:\dir\num\one',sep = ';', delete_existing=0)
+ 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_CLVar(self):
+ """Test the command-line construction variable class"""
+ f = SCons.Util.CLVar('a b')
+
+ r = f + 'c d'
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ' c d'
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ['c d']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', 'c d'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + [' c d']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', ' c d'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ['c', 'd']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + [' c', 'd']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', ' c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ f = SCons.Util.CLVar(['a b'])
+
+ r = f + 'c d'
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ' c d'
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ['c d']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a b', 'c d'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + [' c d']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a b', ' c d'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ['c', 'd']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + [' c', 'd']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a b', ' c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ f = SCons.Util.CLVar(['a', 'b'])
+
+ r = f + 'c d'
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ' c d'
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ['c d']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', 'c d'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + [' c d']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', ' c d'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + ['c', 'd']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', 'c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ r = f + [' c', 'd']
+ assert isinstance(r, SCons.Util.CLVar), type(r)
+ assert r.data == ['a', 'b', ' c', 'd'], r.data
+ assert str(r) == 'a b c d', str(r)
+
+ def test_Selector(self):
+ """Test the Selector class"""
+
+ class MyNode:
+ def __init__(self, name):
+ self.name = name
+ self.suffix = os.path.splitext(name)[1]
+
+ def __str__(self):
+ return self.name
+
+ s = Selector({'a' : 'AAA', 'b' : 'BBB'})
+ assert s['a'] == 'AAA', s['a']
+ assert s['b'] == 'BBB', s['b']
+ exc_caught = None
+ try:
+ x = s['c']
+ except KeyError:
+ exc_caught = 1
+ assert exc_caught, "should have caught a KeyError"
+ s['c'] = 'CCC'
+ assert s['c'] == 'CCC', s['c']
+
+ class DummyEnv(collections.UserDict):
+ def subst(self, key):
+ if key[0] == '$':
+ return self[key[1:]]
+ return key
+
+ env = DummyEnv()
+
+ s = Selector({'.d' : 'DDD', '.e' : 'EEE'})
+ ret = s(env, [])
+ assert ret is None, ret
+ ret = s(env, [MyNode('foo.d')])
+ assert ret == 'DDD', ret
+ ret = s(env, [MyNode('bar.e')])
+ assert ret == 'EEE', ret
+ ret = s(env, [MyNode('bar.x')])
+ assert ret is None, ret
+ s[None] = 'XXX'
+ ret = s(env, [MyNode('bar.x')])
+ assert ret == 'XXX', ret
+
+ env = DummyEnv({'FSUFF' : '.f', 'GSUFF' : '.g'})
+
+ s = Selector({'$FSUFF' : 'FFF', '$GSUFF' : 'GGG'})
+ ret = s(env, [MyNode('foo.f')])
+ assert ret == 'FFF', ret
+ ret = s(env, [MyNode('bar.g')])
+ assert ret == 'GGG', ret
+
+ def test_adjustixes(self):
+ """Test the adjustixes() function"""
+ r = adjustixes('file', 'pre-', '-suf')
+ assert r == 'pre-file-suf', r
+ r = adjustixes('pre-file', 'pre-', '-suf')
+ assert r == 'pre-file-suf', r
+ r = adjustixes('file-suf', 'pre-', '-suf')
+ assert r == 'pre-file-suf', r
+ r = adjustixes('pre-file-suf', 'pre-', '-suf')
+ assert r == 'pre-file-suf', r
+ r = adjustixes('pre-file.xxx', 'pre-', '-suf')
+ assert r == 'pre-file.xxx', r
+ r = adjustixes('dir/file', 'pre-', '-suf')
+ assert r == os.path.join('dir', 'pre-file-suf'), r
+
+ def test_containsAny(self):
+ """Test the containsAny() function"""
+ assert containsAny('*.py', '*?[]')
+ assert not containsAny('file.txt', '*?[]')
+
+ def test_containsAll(self):
+ """Test the containsAll() function"""
+ assert containsAll('43221', '123')
+ assert not containsAll('134', '123')
+
+ def test_containsOnly(self):
+ """Test the containsOnly() function"""
+ assert containsOnly('.83', '0123456789.')
+ assert not containsOnly('43221', '123')
+
+ def test_LogicalLines(self):
+ """Test the LogicalLines class"""
+ fobj = io.StringIO(r"""
+foo \
+bar \
+baz
+foo
+bling \
+bling \ bling
+bling
+""")
+
+ lines = LogicalLines(fobj).readlines()
+ assert lines == [
+ '\n',
+ 'foo bar baz\n',
+ 'foo\n',
+ 'bling bling \\ bling\n',
+ 'bling\n',
+ ], lines
+
+ def test_intern(self):
+ s1 = silent_intern("spam")
+ # Python 1.5 and 3.x do not have a unicode() built-in
+ if sys.version[0] == '2':
+ s2 = silent_intern(unicode("unicode spam"))
+ s3 = silent_intern(42)
+ s4 = silent_intern("spam")
+ assert id(s1) == id(s4)
+
+
+class MD5TestCase(unittest.TestCase):
+
+ def test_collect(self):
+ """Test collecting a list of signatures into a new signature value
+ """
+ s = list(map(MD5signature, ('111', '222', '333')))
+
+ assert '698d51a19d8a121ce581499d7b701668' == MD5collect(s[0:1])
+ assert '8980c988edc2c78cc43ccb718c06efd5' == MD5collect(s[0:2])
+ assert '53fd88c84ff8a285eb6e0a687e55b8c7' == MD5collect(s)
+
+ def test_MD5signature(self):
+ """Test generating a signature"""
+ s = MD5signature('111')
+ assert '698d51a19d8a121ce581499d7b701668' == s, s
+
+ s = MD5signature('222')
+ assert 'bcbe3365e6ac95ea2c0343a2395834dd' == s, s
+
+class NodeListTestCase(unittest.TestCase):
+ def test_simple_attributes(self):
+ """Test simple attributes of a NodeList class"""
+ class TestClass:
+ def __init__(self, name, child=None):
+ self.child = child
+ self.bar = name
+
+ t1 = TestClass('t1', TestClass('t1child'))
+ t2 = TestClass('t2', TestClass('t2child'))
+ t3 = TestClass('t3')
+
+ nl = NodeList([t1, t2, t3])
+ assert nl.bar == [ 't1', 't2', 't3' ], nl.bar
+ assert nl[0:2].child.bar == [ 't1child', 't2child' ], \
+ nl[0:2].child.bar
+
+ def test_callable_attributes(self):
+ """Test callable attributes of a NodeList class"""
+ class TestClass:
+ def __init__(self, name, child=None):
+ self.child = child
+ self.bar = name
+ def foo(self):
+ return self.bar + "foo"
+ def getself(self):
+ return self
+
+ t1 = TestClass('t1', TestClass('t1child'))
+ t2 = TestClass('t2', TestClass('t2child'))
+ t3 = TestClass('t3')
+
+ nl = NodeList([t1, t2, t3])
+ assert nl.foo() == [ 't1foo', 't2foo', 't3foo' ], nl.foo()
+ assert nl.bar == [ 't1', 't2', 't3' ], nl.bar
+ assert nl.getself().bar == [ 't1', 't2', 't3' ], nl.getself().bar
+ assert nl[0:2].child.foo() == [ 't1childfoo', 't2childfoo' ], \
+ nl[0:2].child.foo()
+ assert nl[0:2].child.bar == [ 't1child', 't2child' ], \
+ nl[0:2].child.bar
+
+ def test_null(self):
+ """Test a null NodeList"""
+ nl = NodeList([])
+ r = str(nl)
+ assert r == '', r
+ for node in nl:
+ raise Exception("should not enter this loop")
+
+
+class flattenTestCase(unittest.TestCase):
+
+ def test_scalar(self):
+ """Test flattening a scalar"""
+ result = flatten('xyz')
+ assert result == ['xyz'], result