Refactor construction variable expansion.
[scons.git] / src / engine / SCons / UtilTests.py
1 #
2 # Copyright (c) 2001 Steven Knight
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #
23
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
25
26 import sys
27 import os.path
28 import unittest
29 import SCons.Node
30 import SCons.Node.FS
31 from SCons.Util import scons_str2nodes, scons_subst, PathList
32
33
34 class UtilTestCase(unittest.TestCase):
35     def test_str2nodes(self):
36         """Test the str2nodes function."""
37         nodes = scons_str2nodes("Util.py UtilTests.py")
38         assert len(nodes) == 2
39         assert isinstance(nodes[0], SCons.Node.FS.File)
40         assert isinstance(nodes[1], SCons.Node.FS.File)
41         assert nodes[0].path == "Util.py"
42         assert nodes[1].path == "UtilTests.py"
43
44         nodes = scons_str2nodes("Util.py UtilTests.py", SCons.Node.FS.FS().File)
45         assert len(nodes) == 2
46         assert isinstance(nodes[0], SCons.Node.FS.File)
47         assert isinstance(nodes[1], SCons.Node.FS.File)
48         assert nodes[0].path == "Util.py"
49         assert nodes[1].path == "UtilTests.py"
50
51         nodes = scons_str2nodes(["Util.py", "UtilTests.py"])
52         assert len(nodes) == 2
53         assert isinstance(nodes[0], SCons.Node.FS.File)
54         assert isinstance(nodes[1], SCons.Node.FS.File)
55         assert nodes[0].path == "Util.py"
56         assert nodes[1].path == "UtilTests.py"
57
58         n1 = SCons.Node.FS.default_fs.File("Util.py")
59         nodes = scons_str2nodes([n1, "UtilTests.py"])
60         assert len(nodes) == 2
61         assert isinstance(nodes[0], SCons.Node.FS.File)
62         assert isinstance(nodes[1], SCons.Node.FS.File)
63         assert nodes[0].path == "Util.py"
64         assert nodes[1].path == "UtilTests.py"
65
66         class SConsNode(SCons.Node.Node):
67             pass
68         node = scons_str2nodes(SConsNode())
69
70         class OtherNode:
71             pass
72         node = scons_str2nodes(OtherNode())
73
74
75     def test_subst(self):
76         """Test the subst function."""
77         loc = {}
78         loc['targets'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
79                                                           "/bar/baz.obj",
80                                                           "../foo/baz.obj" ]))
81         loc['target'] = loc['targets'][0]
82         loc['sources'] = PathList(map(os.path.normpath, [ "./foo/blah.cpp",
83                                                           "/bar/ack.cpp",
84                                                           "../foo/ack.c" ]))
85
86         newcom = scons_subst("test $targets $sources", loc, {})
87         assert newcom == "test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp /bar/ack.cpp ../foo/ack.c"
88
89         newcom = scons_subst("test ${targets[:]} ${sources[0]}", loc, {})
90         assert newcom == "test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp"
91
92         newcom = scons_subst("test ${targets[1:]}v", loc, {})
93         assert newcom == "test /bar/baz.obj ../foo/baz.objv"
94
95         newcom = scons_subst("test $target", loc, {})
96         assert newcom == "test foo/bar.exe"
97
98         newcom = scons_subst("test $target$source[0]", loc, {})
99         assert newcom == "test foo/bar.exe[0]"
100
101         newcom = scons_subst("test ${target.file}", loc, {})
102         assert newcom == "test bar.exe"
103
104         newcom = scons_subst("test ${target.filebase}", loc, {})
105         assert newcom == "test bar"
106
107         newcom = scons_subst("test ${target.suffix}", loc, {})
108         assert newcom == "test .exe"
109
110         newcom = scons_subst("test ${target.base}", loc, {})
111         assert newcom == "test foo/bar"
112
113         newcom = scons_subst("test ${target.dir}", loc, {})
114         assert newcom == "test foo"
115
116
117
118 if __name__ == "__main__":
119     suite = unittest.makeSuite(UtilTestCase, 'test_')
120     if not unittest.TextTestRunner().run(suite).wasSuccessful():
121         sys.exit(1)