Add new Options flavors. (Hartmut Goebel)
[scons.git] / src / engine / SCons / Options / PackageOptionTests.py
1 #
2 # __COPYRIGHT__
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 sys
27 import unittest
28
29 import SCons.Errors
30 import SCons.Options
31
32 import TestCmd
33
34 class PackageOptionTestCase(unittest.TestCase):
35     def test_PackageOption(self):
36         """Test PackageOption creation"""
37         opts = SCons.Options.Options()
38         opts.Add(SCons.Options.PackageOption('test', 'test option help', '/default/path'))
39
40         o = opts.options[0]
41         assert o.key == 'test', o.key
42         assert o.help == 'test option help\n    ( yes | no | /path/to/test )', repr(o.help)
43         assert o.default == '/default/path', o.default
44         assert not o.validator is None, o.validator
45         assert not o.converter is None, o.converter
46
47     def test_converter(self):
48         """Test the PackageOption converter"""
49         opts = SCons.Options.Options()
50         opts.Add(SCons.Options.PackageOption('test', 'test option help', '/default/path'))
51
52         o = opts.options[0]
53
54         true_values = [
55                 'yes',    'YES',
56                 'true',   'TRUE',
57                 'on',     'ON',
58                 'enable', 'ENABLE',
59                 'search', 'SEARCH',
60         ]
61         false_values = [
62                 'no',      'NO',
63                 'false',   'FALSE',
64                 'off',     'OFF',
65                 'disable', 'DISABLE',
66         ]
67
68         for t in true_values:
69             x = o.converter(t)
70             assert x, "converter returned false for '%s'" % t
71
72         for f in false_values:
73             x = o.converter(f)
74             assert not x, "converter returned true for '%s'" % f
75
76         x = o.converter('/explicit/path')
77         assert x == '/explicit/path', x
78
79     def test_validator(self):
80         """Test the PackageOption validator"""
81         opts = SCons.Options.Options()
82         opts.Add(SCons.Options.PackageOption('test', 'test option help', '/default/path'))
83
84         test = TestCmd.TestCmd(workdir='')
85         test.write('exists', 'exists\n')
86
87         o = opts.options[0]
88
89         env = {'F':0, 'T':1, 'X':'x'}
90
91         exists = test.workpath('exists')
92         does_not_exist = test.workpath('does_not_exist')
93
94         o.validator('F', '/path', env)
95         o.validator('T', '/path', env)
96         o.validator('X', exists, env)
97
98         caught = None
99         try:
100             o.validator('X', does_not_exist, env)
101         except SCons.Errors.UserError:
102             caught = 1
103         assert caught, "did not catch expected UserError"
104
105
106 if __name__ == "__main__":
107     suite = unittest.makeSuite(PackageOptionTestCase, 'test_')
108     if not unittest.TextTestRunner().run(suite).wasSuccessful():
109         sys.exit(1)