5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
29 _python_ = TestSCons._python_
31 test = TestSCons.TestSCons()
34 # Test that the signature of function action includes all the
38 test.write('SConstruct', r"""
46 ('header', 'Header string (default cell argument)', 'Head:'),
47 ('trailer', 'Trailer string (default cell argument)', 'Tail'),
48 ('NbDeps', 'Number of dependencies', '2'),
49 ('separator', 'Separator for the dependencies (function constant)', ':'),
50 ('closure_cell_value', 'Value of a closure cell', '25'),
51 ('b', 'Value of b (value default argument', '7'),
52 ('regexp', 'Regexp (object as a default argument', 'a(a*)'),
53 ('docstring', 'Docstring', 'docstring'),
54 ('extracode', 'Extra code for the builder function', ''),
55 ('extraarg', 'Extra arg builder function', ''),
56 ('nestedfuncexp', 'Expression for the nested function', 'xxx - b'),
59 optEnv = Environment(options=options, tools=[])
61 r = re.compile(optEnv['regexp'])
65 def toto(header='%(header)s', trailer='%(trailer)s'):
66 xxx = %(closure_cell_value)s
67 def writeDeps(target, source, env, b=%(b)s, r=r %(extraarg)s ,
68 header=header, trailer=trailer):
69 """+'"""%(docstring)s"""'+"""
71 return %(nestedfuncexp)s
72 f = open(str(target[0]),'wb')
74 for d in env['ENVDEPS']:
75 f.write(d+'%(separator)s')
76 f.write(trailer+'\\n')
77 f.write(str(foo())+'\\n')
78 f.write(r.match('aaaa').group(1)+'\\n')
81 f.write(str(xarg)+'\\n')
91 def toto(header='%(header)s', trailer='%(trailer)s'):
92 xxx = %(closure_cell_value)s
93 def writeDeps(target, source, env, b=%(b)s, r=r %(extraarg)s ,
94 header=header, trailer=trailer, xxx=xxx):
95 """+'"""%(docstring)s"""'+"""
96 def foo(b=b, xxx=xxx):
97 return %(nestedfuncexp)s
98 f = open(str(target[0]),'wb')
100 for d in env['ENVDEPS']:
101 f.write(d+'%(separator)s')
102 f.write(trailer+'\\n')
103 f.write(str(foo())+'\\n')
104 f.write(r.match('aaaa').group(1)+'\\n')
107 f.write(str(xarg)+'\\n')
116 # Check that lexical closure are supported
122 a().func_closure[0].cell_contents
123 exec( withClosure % optEnv )
124 except (AttributeError, TypeError):
125 exec( NoClosure % optEnv )
127 genHeaderBld = SCons.Builder.Builder(
128 action = SCons.Action.Action(
130 'Generating $TARGET',
137 env.Append(BUILDERS = {'GenHeader' : genHeaderBld})
139 envdeps = list(map(str, range(int(optEnv['NbDeps']))))
141 env.GenHeader('Out', None, ENVDEPS=envdeps)
146 scons: Reading SConscript files ...
147 scons: done reading SConscript files.
148 scons: Building targets ...
150 scons: done building targets.
154 scons: Reading SConscript files ...
155 scons: done reading SConscript files.
156 scons: Building targets ...
157 scons: `.' is up to date.
158 scons: done building targets.
162 if sys.version[:3] == '2.1':
163 expectedStderr = """\
164 %s:79: SyntaxWarning: local name 'x' in 'a' shadows use of 'x' as global in nested scope 'b'
166 """ % test.workpath('SConstruct')
170 def runtest(arguments, expectedOutFile, expectedRebuild=True, stderr=expectedStderr):
171 test.run(arguments=arguments,
172 stdout=expectedRebuild and rebuildstr or nobuildstr,
173 stderr=expectedStderr)
174 test.must_match('Out.gen.h', expectedOutFile)
176 # Should not be rebuild when ran a second time with the same
179 test.run(arguments = arguments, stdout=nobuildstr, stderr=expectedStderr)
180 test.must_match('Out.gen.h', expectedOutFile)
184 runtest('', """Head:0:1:Tail\n18\naaa\n""")
186 # Changing a docstring should not cause a rebuild
187 runtest('docstring=ThisBuilderDoesXAndY', """Head:0:1:Tail\n18\naaa\n""", False)
188 runtest('docstring=SuperBuilder', """Head:0:1:Tail\n18\naaa\n""", False)
189 runtest('docstring=', """Head:0:1:Tail\n18\naaa\n""", False)
191 # Changing a variable listed in the varlist should cause a rebuild
192 runtest('NbDeps=3', """Head:0:1:2:Tail\n18\naaa\n""")
193 runtest('NbDeps=4', """Head:0:1:2:3:Tail\n18\naaa\n""")
194 runtest('', """Head:0:1:Tail\n18\naaa\n""")
196 # Changing the function code should cause a rebuild
197 runtest('extracode=f.write("XX\\n")', """Head:0:1:Tail\n18\naaa\nXX\n""")
198 runtest('extracode=a=2', """Head:0:1:Tail\n18\naaa\n""")
199 runtest('', """Head:0:1:Tail\n18\naaa\n""")
201 # Changing a constant used in the function code should cause a rebuild
202 runtest('separator=,', """Head:0,1,Tail\n18\naaa\n""")
203 runtest('separator=;', """Head:0;1;Tail\n18\naaa\n""")
204 runtest('', """Head:0:1:Tail\n18\naaa\n""")
206 # Changing the code of a nested function should cause a rebuild
207 runtest('nestedfuncexp=b-xxx', """Head:0:1:Tail\n-18\naaa\n""")
208 runtest('nestedfuncexp=b+xxx', """Head:0:1:Tail\n32\naaa\n""")
209 runtest('', """Head:0:1:Tail\n18\naaa\n""")
211 # Adding an extra argument should cause a rebuild.
212 runtest('extraarg=,xarg=2', """Head:0:1:Tail\n18\naaa\n2\n""")
213 runtest('extraarg=,xarg=5', """Head:0:1:Tail\n18\naaa\n5\n""")
214 runtest('', """Head:0:1:Tail\n18\naaa\n""")
216 # Changing the value of a default argument should cause a rebuild
218 runtest('b=0', """Head:0:1:Tail\n25\naaa\n""")
219 runtest('b=9', """Head:0:1:Tail\n16\naaa\n""")
220 runtest('', """Head:0:1:Tail\n18\naaa\n""")
223 runtest('regexp=(aaaa)', """Head:0:1:Tail\n18\naaaa\n""")
224 runtest('regexp=aa(a+)', """Head:0:1:Tail\n18\naa\n""")
225 runtest('', """Head:0:1:Tail\n18\naaa\n""")
227 # Changing the value of a closure cell value should cause a rebuild
229 runtest('closure_cell_value=32', """Head:0:1:Tail\n25\naaa\n""")
230 runtest('closure_cell_value=7', """Head:0:1:Tail\n0\naaa\n""")
231 runtest('', """Head:0:1:Tail\n18\naaa\n""")
233 # case 2: a default argument
234 runtest('header=MyHeader:', """MyHeader:0:1:Tail\n18\naaa\n""")
235 runtest('trailer=MyTrailer', """Head:0:1:MyTrailer\n18\naaa\n""")
236 runtest('', """Head:0:1:Tail\n18\naaa\n""")
242 # indent-tabs-mode:nil
244 # vim: set expandtab tabstop=4 shiftwidth=4: