Collect common type-checking logic in SCons.Util.
[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 os
27 import os.path
28 import string
29 import sys
30 import unittest
31 import SCons.Node
32 import SCons.Node.FS
33 from SCons.Util import *
34 import TestCmd
35
36 class UtilTestCase(unittest.TestCase):
37     def test_str2nodes(self):
38         """Test the str2nodes function."""
39         nodes = scons_str2nodes("Util.py UtilTests.py")
40         assert len(nodes) == 2
41         assert isinstance(nodes[0], SCons.Node.FS.File)
42         assert isinstance(nodes[1], SCons.Node.FS.File)
43         assert nodes[0].path == "Util.py"
44         assert nodes[1].path == "UtilTests.py"
45
46         nodes = scons_str2nodes("Util.py UtilTests.py", SCons.Node.FS.FS().File)
47         assert len(nodes) == 2
48         assert isinstance(nodes[0], SCons.Node.FS.File)
49         assert isinstance(nodes[1], SCons.Node.FS.File)
50         assert nodes[0].path == "Util.py"
51         assert nodes[1].path == "UtilTests.py"
52
53         nodes = scons_str2nodes(["Util.py", "UtilTests.py"])
54         assert len(nodes) == 2
55         assert isinstance(nodes[0], SCons.Node.FS.File)
56         assert isinstance(nodes[1], SCons.Node.FS.File)
57         assert nodes[0].path == "Util.py"
58         assert nodes[1].path == "UtilTests.py"
59
60         n1 = SCons.Node.FS.default_fs.File("Util.py")
61         nodes = scons_str2nodes([n1, "UtilTests.py"])
62         assert len(nodes) == 2
63         assert isinstance(nodes[0], SCons.Node.FS.File)
64         assert isinstance(nodes[1], SCons.Node.FS.File)
65         assert nodes[0].path == "Util.py"
66         assert nodes[1].path == "UtilTests.py"
67
68         class SConsNode(SCons.Node.Node):
69             pass
70         node = scons_str2nodes(SConsNode())
71
72         class OtherNode:
73             pass
74         node = scons_str2nodes(OtherNode())
75
76
77     def test_subst(self):
78         """Test the subst function."""
79         loc = {}
80         loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
81                                                           "/bar/baz.obj",
82                                                           "../foo/baz.obj" ]))
83         loc['TARGET'] = loc['TARGETS'][0]
84         loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah.cpp",
85                                                           "/bar/ack.cpp",
86                                                           "../foo/ack.c" ]))
87         loc['xxx'] = None
88
89         if os.sep == '/':
90             def cvt(str):
91                 return str
92         else:
93             def cvt(str):
94                 return string.replace(str, '/', os.sep)
95
96
97         newcom = scons_subst("test $TARGETS $SOURCES", loc, {})
98         assert newcom == cvt("test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp /bar/ack.cpp ../foo/ack.c")
99
100         newcom = scons_subst("test ${TARGETS[:]} ${SOURCES[0]}", loc, {})
101         assert newcom == cvt("test foo/bar.exe /bar/baz.obj ../foo/baz.obj foo/blah.cpp")
102
103         newcom = scons_subst("test ${TARGETS[1:]}v", loc, {})
104         assert newcom == cvt("test /bar/baz.obj ../foo/baz.objv")
105
106         newcom = scons_subst("test $TARGET", loc, {})
107         assert newcom == cvt("test foo/bar.exe")
108
109         newcom = scons_subst("test $TARGET$SOURCE[0]", loc, {})
110         assert newcom == cvt("test foo/bar.exe[0]")
111
112         newcom = scons_subst("test ${TARGET.file}", loc, {})
113         assert newcom == cvt("test bar.exe")
114
115         newcom = scons_subst("test ${TARGET.filebase}", loc, {})
116         assert newcom == cvt("test bar")
117
118         newcom = scons_subst("test ${TARGET.suffix}", loc, {})
119         assert newcom == cvt("test .exe")
120
121         newcom = scons_subst("test ${TARGET.base}", loc, {})
122         assert newcom == cvt("test foo/bar")
123
124         newcom = scons_subst("test ${TARGET.dir}", loc, {})
125         assert newcom == cvt("test foo")
126
127         newcom = scons_subst("test $xxx", loc, {})
128         assert newcom == cvt("test"), newcom
129
130     def test_subst_list(self):
131         """Testing the scons_subst_list() method..."""
132         loc = {}
133         loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
134                                                           "/bar/baz with spaces.obj",
135                                                           "../foo/baz.obj" ]))
136         loc['TARGET'] = loc['TARGETS'][0]
137         loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah with spaces.cpp",
138                                                           "/bar/ack.cpp",
139                                                           "../foo/ack.c" ]))
140         loc['xxx'] = None
141         loc['NEWLINE'] = 'before\nafter'
142
143         if os.sep == '/':
144             def cvt(str):
145                 return str
146         else:
147             def cvt(str):
148                 return string.replace(str, '/', os.sep)
149
150         cmd_list = scons_subst_list("$TARGETS", loc, {})
151         assert cmd_list[0][1] == cvt("/bar/baz with spaces.obj"), cmd_list[0][1]
152
153         cmd_list = scons_subst_list("$SOURCES $NEWLINE $TARGETS", loc, {})
154         assert len(cmd_list) == 2, cmd_list
155         assert cmd_list[0][0] == cvt('foo/blah with spaces.cpp'), cmd_list[0][0]
156         assert cmd_list[1][2] == cvt("/bar/baz with spaces.obj"), cmd_list[1]
157
158         cmd_list = scons_subst_list("$SOURCES$NEWLINE", loc, {})
159         assert len(cmd_list) == 2, cmd_list
160         assert cmd_list[1][0] == 'after', cmd_list[1][0]
161         assert cmd_list[0][2] == cvt('../foo/ack.cbefore'), cmd_list[0][2]
162
163     def test_find_file(self):
164         """Testing find_file function."""
165         test = TestCmd.TestCmd(workdir = '')
166         test.write('./foo', 'Some file\n')
167         fs = SCons.Node.FS.FS(test.workpath(""))
168         os.chdir(test.workpath("")) # FS doesn't like the cwd to be something other than it's root
169         node_derived = fs.File(test.workpath('./bar/baz'))
170         node_derived.builder_set(1) # Any non-zero value.
171         paths = map(fs.Dir, ['.', './bar'])
172         nodes = [find_file('foo', paths, fs.File), 
173                  find_file('baz', paths, fs.File)] 
174         file_names = map(str, nodes)
175         file_names = map(os.path.normpath, file_names)
176         assert os.path.normpath('./foo') in file_names, file_names
177         assert os.path.normpath('./bar/baz') in file_names, file_names
178
179     def test_autogenerate(dict):
180         """Test autogenerating variables in a dictionary."""
181         dict = {'LIBS'          : [ 'foo', 'bar', 'baz' ],
182                 'LIBLINKPREFIX' : 'foo',
183                 'LIBLINKSUFFIX' : 'bar'}
184         autogenerate(dict, dir = SCons.Node.FS.default_fs.Dir('/xx'))
185         assert len(dict['_LIBFLAGS']) == 3, dict('_LIBFLAGS')
186         assert dict['_LIBFLAGS'][0] == 'foofoobar', \
187                dict['_LIBFLAGS'][0]
188         assert dict['_LIBFLAGS'][1] == 'foobarbar', \
189                dict['_LIBFLAGS'][1]
190         assert dict['_LIBFLAGS'][2] == 'foobazbar', \
191                dict['_LIBFLAGS'][2]
192
193         blat = SCons.Node.FS.default_fs.File('blat')
194         dict = {'CPPPATH'   : [ 'foo', 'bar', 'baz', '$FOO/bar', blat],
195                 'INCPREFIX' : 'foo',
196                 'INCSUFFIX' : 'bar',
197                 'FOO'       : 'baz' }
198         autogenerate(dict, dir = SCons.Node.FS.default_fs.Dir('/xx'))
199         assert len(dict['_INCFLAGS']) == 5, dict['_INCFLAGS']
200         assert dict['_INCFLAGS'][0] == os.path.normpath('foo/xx/foobar'), \
201                dict['_INCFLAGS'][0]
202         assert dict['_INCFLAGS'][1] == os.path.normpath('foo/xx/barbar'), \
203                dict['_INCFLAGS'][1]
204         assert dict['_INCFLAGS'][2] == os.path.normpath('foo/xx/bazbar'), \
205                dict['_INCFLAGS'][2]
206         assert dict['_INCFLAGS'][3] == os.path.normpath('foo/xx/baz/barbar'), \
207                dict['_INCFLAGS'][3]
208         
209         assert dict['_INCFLAGS'][4] == os.path.normpath('fooblatbar'), \
210                dict['_INCFLAGS'][4]
211
212     def test_render_tree(self):
213         class Node:
214             def __init__(self, name, children=[]):
215                 self.children = children
216                 self.name = name
217             def __str__(self):
218                 return self.name
219
220         def get_children(node):
221             return node.children
222
223         windows_h = Node("windows.h")
224         stdlib_h = Node("stdlib.h")
225         stdio_h = Node("stdio.h")
226         bar_c = Node("bar.c", [stdlib_h, windows_h])
227         bar_o = Node("bar.o", [bar_c])
228         foo_c = Node("foo.c", [stdio_h])
229         foo_o = Node("foo.o", [foo_c])
230         foo = Node("foo", [foo_o, bar_o])
231
232         expect = """\
233 +-foo
234   +-foo.o
235   | +-foo.c
236   |   +-stdio.h
237   +-bar.o
238     +-bar.c
239       +-stdlib.h
240       +-windows.h
241 """
242
243         actual = render_tree(foo, get_children)
244         assert expect == actual, (expect, actual)
245
246     def test_is_Dict(self):
247         assert is_Dict({})
248         import UserDict
249         assert is_Dict(UserDict.UserDict())
250         assert not is_Dict([])
251         assert not is_Dict("")
252
253     def test_is_List(self):
254         assert is_List([])
255         import UserList
256         assert is_List(UserList.UserList())
257         assert not is_List({})
258         assert not is_List("")
259
260     def test_is_String(self):
261         assert is_String("")
262         try:
263             import UserString
264         except:
265             pass
266         else:
267             assert is_String(UserString.UserString())
268         assert not is_String({})
269         assert not is_String([])
270
271 if __name__ == "__main__":
272     suite = unittest.makeSuite(UtilTestCase, 'test_')
273     if not unittest.TextTestRunner().run(suite).wasSuccessful():
274         sys.exit(1)