Move SCons.Util.scons_str2nodes() to SCons.Node/__init__.py and shorten its name.
[scons.git] / src / engine / SCons / UtilTests.py
1 #
2 # Copyright (c) 2001, 2002 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 re
29 import string
30 import sys
31 import types
32 import unittest
33 import SCons.Node
34 import SCons.Node.FS
35 from SCons.Util import *
36 import TestCmd
37
38 class UtilTestCase(unittest.TestCase):
39     def test_subst(self):
40         """Test the subst function."""
41         loc = {}
42         loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
43                                                           "/bar/baz.obj",
44                                                           "../foo/baz.obj" ]))
45         loc['TARGET'] = loc['TARGETS'][0]
46         loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah.cpp",
47                                                           "/bar/ack.cpp",
48                                                           "../foo/ack.c" ]))
49         loc['xxx'] = None
50         loc['zero'] = 0
51         loc['one'] = 1
52
53         if os.sep == '/':
54             def cvt(str):
55                 return str
56         else:
57             def cvt(str):
58                 return string.replace(str, '/', os.sep)
59
60
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")
63
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")
66
67         newcom = scons_subst("test ${TARGETS[1:]}v", loc, {})
68         assert newcom == cvt("test /bar/baz.obj ../foo/baz.objv")
69
70         newcom = scons_subst("test $TARGET", loc, {})
71         assert newcom == cvt("test foo/bar.exe")
72
73         newcom = scons_subst("test $TARGET$SOURCE[0]", loc, {})
74         assert newcom == cvt("test foo/bar.exe[0]")
75
76         newcom = scons_subst("test ${TARGET.file}", loc, {})
77         assert newcom == cvt("test bar.exe")
78
79         newcom = scons_subst("test ${TARGET.filebase}", loc, {})
80         assert newcom == cvt("test bar")
81
82         newcom = scons_subst("test ${TARGET.suffix}", loc, {})
83         assert newcom == cvt("test .exe")
84
85         newcom = scons_subst("test ${TARGET.base}", loc, {})
86         assert newcom == cvt("test foo/bar")
87
88         newcom = scons_subst("test ${TARGET.dir}", loc, {})
89         assert newcom == cvt("test foo")
90
91         newcom = scons_subst("test $xxx", loc, {})
92         assert newcom == cvt("test"), newcom
93
94         newcom = scons_subst("test $($xxx$)", loc, {})
95         assert newcom == cvt("test $($)"), newcom
96
97         newcom = scons_subst("test $( $xxx $)", loc, {})
98         assert newcom == cvt("test $( $)"), newcom
99
100         newcom = scons_subst("test $($xxx$)", loc, {}, re.compile('\$[()]'))
101         assert newcom == cvt("test"), newcom
102
103         newcom = scons_subst("test $( $xxx $)", loc, {}, re.compile('\$[()]'))
104         assert newcom == cvt("test"), newcom
105
106         newcom = scons_subst("test $zero", loc, {})
107         assert newcom == cvt("test 0"), newcom
108
109         newcom = scons_subst("test $one", loc, {})
110         assert newcom == cvt("test 1"), newcom
111
112         newcom = scons_subst("test aXbXcXd", loc, {}, re.compile('X'))
113         assert newcom == cvt("test abcd"), newcom
114
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
119
120     def test_subst_list(self):
121         """Testing the scons_subst_list() method..."""
122         loc = {}
123         loc['TARGETS'] = PathList(map(os.path.normpath, [ "./foo/bar.exe",
124                                                           "/bar/baz with spaces.obj",
125                                                           "../foo/baz.obj" ]))
126         loc['TARGET'] = loc['TARGETS'][0]
127         loc['SOURCES'] = PathList(map(os.path.normpath, [ "./foo/blah with spaces.cpp",
128                                                           "/bar/ack.cpp",
129                                                           "../foo/ack.c" ]))
130         loc['xxx'] = None
131         loc['NEWLINE'] = 'before\nafter'
132
133         if os.sep == '/':
134             def cvt(str):
135                 return str
136         else:
137             def cvt(str):
138                 return string.replace(str, '/', os.sep)
139
140         cmd_list = scons_subst_list("$TARGETS", loc, {})
141         assert cmd_list[0][1] == cvt("/bar/baz with spaces.obj"), cmd_list[0][1]
142
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]
147
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]
152
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
158
159     def test_render_tree(self):
160         class Node:
161             def __init__(self, name, children=[]):
162                 self.children = children
163                 self.name = name
164             def __str__(self):
165                 return self.name
166
167         def get_children(node):
168             return node.children
169
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])
178
179         expect = """\
180 +-foo
181   +-foo.o
182   | +-foo.c
183   |   +-stdio.h
184   +-bar.o
185     +-bar.c
186       +-stdlib.h
187       +-windows.h
188 """
189
190         actual = render_tree(foo, get_children)
191         assert expect == actual, (expect, actual)
192
193     def test_is_Dict(self):
194         assert is_Dict({})
195         import UserDict
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'')"
201
202     def test_is_List(self):
203         assert is_List([])
204         import UserList
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'')"
210
211     def test_is_String(self):
212         assert is_String("")
213         if hasattr(types, 'UnicodeType'):
214             exec "assert is_String(u'')"
215         try:
216             import UserString
217         except:
218             pass
219         else:
220             assert is_String(UserString.UserString(''))
221         assert not is_String({})
222         assert not is_String([])
223
224 if __name__ == "__main__":
225     suite = unittest.makeSuite(UtilTestCase, 'test_')
226     if not unittest.TextTestRunner().run(suite).wasSuccessful():
227         sys.exit(1)