2 # Copyright (c) 2001, 2002 Steven Knight
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:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
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.
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
35 from SCons.Util import *
38 class UtilTestCase(unittest.TestCase):
40 """Test the subst function."""
42 loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
45 loc['TARGET'] = loc['TARGETS'][0]
46 loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah.cpp",
58 return string.replace(str, '/', os.sep)
61 newcom = scons_subst("test $TARGETS $SOURCES", loc, {})
62 assert newcom == cvt("test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp /bar/ack.cpp ../foo/ack.c")
64 newcom = scons_subst("test ${TARGETS[:]} ${SOURCES[0]}", loc, {})
65 assert newcom == cvt("test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp")
67 newcom = scons_subst("test ${TARGETS[1:]}v", loc, {})
68 assert newcom == cvt("test /bar/baz.obj ../foo/baz.objv")
70 newcom = scons_subst("test $TARGET", loc, {})
71 assert newcom == cvt("test foo/bar.exe")
73 newcom = scons_subst("test $TARGET$SOURCE[0]", loc, {})
74 assert newcom == cvt("test foo/bar.exe[0]")
76 newcom = scons_subst("test ${TARGET.file}", loc, {})
77 assert newcom == cvt("test bar.exe")
79 newcom = scons_subst("test ${TARGET.filebase}", loc, {})
80 assert newcom == cvt("test bar")
82 newcom = scons_subst("test ${TARGET.suffix}", loc, {})
83 assert newcom == cvt("test .exe")
85 newcom = scons_subst("test ${TARGET.base}", loc, {})
86 assert newcom == cvt("test foo/bar")
88 newcom = scons_subst("test ${TARGET.dir}", loc, {})
89 assert newcom == cvt("test foo")
91 newcom = scons_subst("test $xxx", loc, {})
92 assert newcom == cvt("test"), newcom
94 newcom = scons_subst("test $($xxx$)", loc, {})
95 assert newcom == cvt("test $($)"), newcom
97 newcom = scons_subst("test $( $xxx $)", loc, {})
98 assert newcom == cvt("test $( $)"), newcom
100 newcom = scons_subst("test $($xxx$)", loc, {}, re.compile('\$[()]'))
101 assert newcom == cvt("test"), newcom
103 newcom = scons_subst("test $( $xxx $)", loc, {}, re.compile('\$[()]'))
104 assert newcom == cvt("test"), newcom
106 newcom = scons_subst("test $zero", loc, {})
107 assert newcom == cvt("test 0"), newcom
109 newcom = scons_subst("test $one", loc, {})
110 assert newcom == cvt("test 1"), newcom
112 newcom = scons_subst("test aXbXcXd", loc, {}, re.compile('X'))
113 assert newcom == cvt("test abcd"), newcom
115 glob = { 'a' : 1, 'b' : 2 }
116 loc = {'a' : 3, 'c' : 4 }
117 newcom = scons_subst("test $a $b $c $d test", glob, loc)
118 assert newcom == "test 3 2 4 test", newcom
120 def test_subst_list(self):
121 """Testing the scons_subst_list() method..."""
123 loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
124 "/bar/baz with spaces.obj",
126 loc['TARGET'] = loc['TARGETS'][0]
127 loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah with spaces.cpp",
131 loc['NEWLINE'] = 'before\nafter'
138 return string.replace(str, '/', os.sep)
140 cmd_list = scons_subst_list("$TARGETS", loc, {})
141 assert cmd_list[0][1] == cvt("/bar/baz with spaces.obj"), cmd_list[0][1]
143 cmd_list = scons_subst_list("$SOURCES $NEWLINE $TARGETS", loc, {})
144 assert len(cmd_list) == 2, cmd_list
145 assert cmd_list[0][0] == cvt('foo/blah with spaces.cpp'), cmd_list[0][0]
146 assert cmd_list[1][2] == cvt("/bar/baz with spaces.obj"), cmd_list[1]
148 cmd_list = scons_subst_list("$SOURCES$NEWLINE", loc, {})
149 assert len(cmd_list) == 2, cmd_list
150 assert cmd_list[1][0] == 'after', cmd_list[1][0]
151 assert cmd_list[0][2] == cvt('../foo/ack.cbefore'), cmd_list[0][2]
153 glob = { 'a' : 1, 'b' : 2 }
154 loc = {'a' : 3, 'c' : 4 }
155 cmd_list = scons_subst_list("test $a $b $c $d test", glob, loc)
156 assert len(cmd_list) == 1, cmd_list
157 assert cmd_list[0] == ['test', '3', '2', '4', 'test'], cmd_list
159 def test_render_tree(self):
161 def __init__(self, name, children=[]):
162 self.children = children
167 def get_children(node):
170 windows_h = Node("windows.h")
171 stdlib_h = Node("stdlib.h")
172 stdio_h = Node("stdio.h")
173 bar_c = Node("bar.c", [stdlib_h, windows_h])
174 bar_o = Node("bar.o", [bar_c])
175 foo_c = Node("foo.c", [stdio_h])
176 foo_o = Node("foo.o", [foo_c])
177 foo = Node("foo", [foo_o, bar_o])
190 actual = render_tree(foo, get_children)
191 assert expect == actual, (expect, actual)
193 def test_is_Dict(self):
196 assert is_Dict(UserDict.UserDict())
197 assert not is_Dict([])
198 assert not is_Dict("")
199 if hasattr(types, 'UnicodeType'):
200 exec "assert not is_Dict(u'')"
202 def test_is_List(self):
205 assert is_List(UserList.UserList())
206 assert not is_List({})
207 assert not is_List("")
208 if hasattr(types, 'UnicodeType'):
209 exec "assert not is_List(u'')"
211 def test_is_String(self):
213 if hasattr(types, 'UnicodeType'):
214 exec "assert is_String(u'')"
220 assert is_String(UserString.UserString(''))
221 assert not is_String({})
222 assert not is_String([])
224 if __name__ == "__main__":
225 suite = unittest.makeSuite(UtilTestCase, 'test_')
226 if not unittest.TextTestRunner().run(suite).wasSuccessful():