ef6815082e6e9ad0d722d2d934fd06653b830c42
[scons.git] / src / engine / SCons / Variables / BoolVariableTests.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.Variables
31
32 class BoolVariableTestCase(unittest.TestCase):
33     def test_BoolVariable(self):
34         """Test BoolVariable creation"""
35         opts = SCons.Variables.Variables()
36         opts.Add(SCons.Variables.BoolVariable('test', 'test option help', 0))
37
38         o = opts.options[0]
39         assert o.key == 'test', o.key
40         assert o.help == 'test option help (yes|no)', o.help
41         assert o.default == 0, o.default
42         assert not o.validator is None, o.validator
43         assert not o.converter is None, o.converter
44
45     def test_converter(self):
46         """Test the BoolVariable converter"""
47         opts = SCons.Variables.Variables()
48         opts.Add(SCons.Variables.BoolVariable('test', 'test option help', 0))
49
50         o = opts.options[0]
51
52         true_values = [
53                 'y',    'Y',
54                 'yes',  'YES',
55                 't',    'T',
56                 'true', 'TRUE',
57                 'on',   'ON',
58                 'all',  'ALL',
59                 '1',
60         ]
61         false_values = [
62                 'n',    'N',
63                 'no',   'NO',
64                 'f',    'F',
65                 'false', 'FALSE',
66                 'off',  'OFF',
67                 'none', 'NONE',
68                 '0',
69         ]
70
71         for t in true_values:
72             x = o.converter(t)
73             assert x, "converter returned false for '%s'" % t
74
75         for f in false_values:
76             x = o.converter(f)
77             assert not x, "converter returned true for '%s'" % f
78
79         caught = None
80         try:
81             o.converter('x')
82         except ValueError:
83             caught = 1
84         assert caught, "did not catch expected ValueError"
85
86     def test_validator(self):
87         """Test the BoolVariable validator"""
88         opts = SCons.Variables.Variables()
89         opts.Add(SCons.Variables.BoolVariable('test', 'test option help', 0))
90
91         o = opts.options[0]
92
93         env = {
94             'T' : True,
95             'F' : False,
96             'N' : 'xyzzy',
97         }
98
99         o.validator('T', 0, env)
100
101         o.validator('F', 0, env)
102
103         caught = None
104         try:
105             o.validator('N', 0, env)
106         except SCons.Errors.UserError:
107             caught = 1
108         assert caught, "did not catch expected UserError for N"
109
110         caught = None
111         try:
112             o.validator('NOSUCHKEY', 0, env)
113         except KeyError:
114             caught = 1
115         assert caught, "did not catch expected KeyError for NOSUCHKEY"
116
117
118 if __name__ == "__main__":
119     suite = unittest.makeSuite(BoolVariableTestCase, 'test_')
120     if not unittest.TextTestRunner().run(suite).wasSuccessful():
121         sys.exit(1)