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__"
40 return SCons.Util.scons_subst(x, self)
41 def __setitem__(self, key, value):
42 self.dict[key] = value
43 def __getitem__(self, key):
45 def has_key(self, key):
46 return self.dict.has_key(key)
51 def check(key, value, env):
52 assert int(value) == 6 * 9, "key %s = %s" % (key, repr(value))
54 # Check saved option file by executing and comparing against
55 # the expected dictionary
56 def checkSave(file, expected):
59 execfile(file, gdict, ldict)
60 assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
62 class OptionsTestCase(unittest.TestCase):
65 """Test adding to an Options object"""
66 opts = SCons.Options.Options()
70 'THE answer to THE question',
73 lambda x: int(x) + 12)
78 assert o.default == None
79 assert o.validator == None
80 assert o.converter == None
83 assert o.key == 'ANSWER'
84 assert o.help == 'THE answer to THE question'
85 assert o.default == "42"
86 o.validator(o.key, o.converter(o.default), {})
88 def test_it(var, opts=opts):
92 except SCons.Errors.UserError:
94 assert exc_caught, "did not catch UserError for '%s'" % var
99 def test_AddOptions(self):
100 """Test adding a list of options to an Options object"""
101 opts = SCons.Options.Options()
103 opts.AddOptions(('VAR2',),
105 'THE answer to THE question',
108 lambda x: int(x) + 12))
111 assert o.key == 'VAR2', o.key
112 assert o.help == '', o.help
113 assert o.default == None, o.default
114 assert o.validator == None, o.validator
115 assert o.converter == None, o.converter
118 assert o.key == 'ANSWER2', o.key
119 assert o.help == 'THE answer to THE question', o.help
120 assert o.default == "42", o.default
121 o.validator(o.key, o.converter(o.default), {})
123 def test_Update(self):
124 """Test updating an Environment"""
126 # Test that a default value is validated correctly.
127 test = TestSCons.TestSCons()
128 file = test.workpath('custom.py')
129 opts = SCons.Options.Options(file)
132 'THE answer to THE question',
135 lambda x: int(x) + 12)
139 assert env['ANSWER'] == 54
143 assert env['ANSWER'] == 54
145 # Test that a bad value from the file is used and
146 # validation fails correctly.
147 test = TestSCons.TestSCons()
148 file = test.workpath('custom.py')
149 test.write('custom.py', 'ANSWER=54')
150 opts = SCons.Options.Options(file)
153 'THE answer to THE question',
156 lambda x: int(x) + 12)
162 except AssertionError:
164 assert exc_caught, "did not catch expected assertion"
170 except AssertionError:
172 assert exc_caught, "did not catch expected assertion"
174 # Test that a good value from the file is used and validated.
175 test = TestSCons.TestSCons()
176 file = test.workpath('custom.py')
177 test.write('custom.py', 'ANSWER=42')
178 opts = SCons.Options.Options(file)
181 'THE answer to THE question',
184 lambda x: int(x) + 12)
188 assert env['ANSWER'] == 54
192 assert env['ANSWER'] == 54
194 # Test that a bad value from an args dictionary passed to
195 # Update() is used and validation fails correctly.
196 test = TestSCons.TestSCons()
197 file = test.workpath('custom.py')
198 test.write('custom.py', 'ANSWER=10')
199 opts = SCons.Options.Options(file)
202 'THE answer to THE question',
205 lambda x: int(x) + 12)
210 opts.Update(env, {'ANSWER':'54'})
211 except AssertionError:
213 assert exc_caught, "did not catch expected assertion"
215 # Test that a good value from an args dictionary
216 # passed to Update() is used and validated.
217 test = TestSCons.TestSCons()
218 file = test.workpath('custom.py')
219 test.write('custom.py', 'ANSWER=10')
220 opts = SCons.Options.Options(file)
223 'THE answer to THE question',
226 lambda x: int(x) + 12)
229 opts.Update(env, {'ANSWER':'42'})
230 assert env['ANSWER'] == 54
232 # Test against a former bug. If we supply a converter,
233 # but no default, the value should *not* appear in the
234 # Environment if no value is specified in the options file
236 test = TestSCons.TestSCons()
237 file = test.workpath('custom.py')
238 opts = SCons.Options.Options(file)
241 help='THE answer to THE question',
246 assert not env.has_key('ANSWER')
249 """Test updating an Environment with arguments overridden"""
251 # Test that a bad (command-line) argument is used
252 # and the validation fails correctly.
253 test = TestSCons.TestSCons()
254 file = test.workpath('custom.py')
255 test.write('custom.py', 'ANSWER=42')
256 opts = SCons.Options.Options(file, {'ANSWER':54})
259 'THE answer to THE question',
262 lambda x: int(x) + 12)
268 except AssertionError:
270 assert exc_caught, "did not catch expected assertion"
272 # Test that a good (command-line) argument is used and validated.
273 test = TestSCons.TestSCons()
274 file = test.workpath('custom.py')
275 test.write('custom.py', 'ANSWER=54')
276 opts = SCons.Options.Options(file, {'ANSWER':42})
279 'THE answer to THE question',
282 lambda x: int(x) + 12)
286 assert env['ANSWER'] == 54
288 # Test that a (command-line) argument is overridden by a dictionary
289 # supplied to Update() and the dictionary value is validated correctly.
290 test = TestSCons.TestSCons()
291 file = test.workpath('custom.py')
292 test.write('custom.py', 'ANSWER=54')
293 opts = SCons.Options.Options(file, {'ANSWER':54})
296 'THE answer to THE question',
299 lambda x: int(x) + 12)
302 opts.Update(env, {'ANSWER':42})
303 assert env['ANSWER'] == 54
306 """Testing saving Options"""
308 test = TestSCons.TestSCons()
309 cache_file = test.workpath('cached.options')
310 opts = SCons.Options.Options()
312 # test saving out empty file
318 opts.Add('OPT_VAL_2',
320 opts.Add('OPT_VAL_3',
324 opts.Update(env, {'OPT_VAL_3' : 2})
325 assert env['OPT_VAL'] == 21
326 assert env['OPT_VAL_2'] == 'foo'
327 assert env['OPT_VAL_3'] == 2
328 env['OPT_VAL_2'] = 'bar'
329 opts.Save(cache_file, env)
330 checkSave(cache_file, { 'OPT_VAL_2' : 'bar',
333 # Test against some old bugs
335 def __init__(self, x):
340 test = TestSCons.TestSCons()
341 cache_file = test.workpath('cached.options')
342 opts = SCons.Options.Options()
344 opts.Add('THIS_USED_TO_BREAK',
348 opts.Add('THIS_ALSO_BROKE',
352 opts.Add('THIS_SHOULD_WORK',
357 opts.Update(env, { 'THIS_USED_TO_BREAK' : "Single'Quotes'In'String",
358 'THIS_ALSO_BROKE' : "\\Escape\nSequences\t",
359 'THIS_SHOULD_WORK' : Foo('baz') })
360 opts.Save(cache_file, env)
361 checkSave(cache_file, { 'THIS_USED_TO_BREAK' : "Single'Quotes'In'String",
362 'THIS_ALSO_BROKE' : "\\Escape\nSequences\t",
363 'THIS_SHOULD_WORK' : 'baz' })
365 def test_GenerateHelpText(self):
366 opts = SCons.Options.Options()
369 'THE answer to THE question',
372 lambda x: int(x) + 12)
378 lambda x: int(x) + 12)
384 lambda x: int(x) + 12)
390 ANSWER: THE answer to THE question
403 text = opts.GenerateHelpText(env)
404 assert text == expect, text
411 ANSWER: THE answer to THE question
419 text = opts.GenerateHelpText(env, sort=cmp)
420 assert text == expectAlpha, text
422 if __name__ == "__main__":
423 suite = unittest.makeSuite(OptionsTestCase, 'test_')
424 if not unittest.TextTestRunner().run(suite).wasSuccessful():