Issue 2401: Fix usage of comparison with None, patch from Jared Grubb
[scons.git] / src / engine / SCons / Variables / EnumVariableTests.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 EnumVariableTestCase(unittest.TestCase):
33     def test_EnumVariable(self):
34         """Test EnumVariable creation"""
35         opts = SCons.Variables.Variables()
36         opts.Add(SCons.Variables.EnumVariable('test', 'test option help', 0,
37                                           ['one', 'two', 'three'],
38                                           {}))
39
40         o = opts.options[0]
41         assert o.key == 'test', o.key
42         assert o.help == 'test option help (one|two|three)', o.help
43         assert o.default == 0, o.default
44         assert o.validator is not None, o.validator
45         assert o.converter is not None, o.converter
46
47     def test_converter(self):
48         """Test the EnumVariable converter"""
49         opts = SCons.Variables.Variables()
50         opts.Add(SCons.Variables.EnumVariable('test', 'test option help', 0,
51                                           ['one', 'two', 'three']))
52
53         o = opts.options[0]
54
55         for a in ['one', 'two', 'three', 'no_match']:
56             x = o.converter(a)
57             assert x == a, x
58
59         opts = SCons.Variables.Variables()
60         opts.Add(SCons.Variables.EnumVariable('test', 'test option help', 0,
61                                           ['one', 'two', 'three'],
62                                           {'1' : 'one',
63                                            '2' : 'two',
64                                            '3' : 'three'}))
65
66         o = opts.options[0]
67
68         x = o.converter('one')
69         assert x == 'one', x
70         x = o.converter('1')
71         assert x == 'one', x
72
73         x = o.converter('two')
74         assert x == 'two', x
75         x = o.converter('2')
76         assert x == 'two', x
77
78         x = o.converter('three')
79         assert x == 'three', x
80         x = o.converter('3')
81         assert x == 'three', x
82
83         opts = SCons.Variables.Variables()
84         opts.Add(SCons.Variables.EnumVariable('test0', 'test option help', 0,
85                                           ['one', 'two', 'three'],
86                                           {'a' : 'one',
87                                            'b' : 'two',
88                                            'c' : 'three'},
89                                           ignorecase=0))
90         opts.Add(SCons.Variables.EnumVariable('test1', 'test option help', 0,
91                                           ['one', 'two', 'three'],
92                                           {'a' : 'one',
93                                            'b' : 'two',
94                                            'c' : 'three'},
95                                           ignorecase=1))
96         opts.Add(SCons.Variables.EnumVariable('test2', 'test option help', 0,
97                                           ['one', 'two', 'three'],
98                                           {'a' : 'one',
99                                            'b' : 'two',
100                                            'c' : 'three'},
101                                           ignorecase=2))
102
103         o0 = opts.options[0]
104         o1 = opts.options[1]
105         o2 = opts.options[2]
106
107         table = {
108             'one'   : ['one',   'one',   'one'],
109             'One'   : ['One',   'One',   'one'],
110             'ONE'   : ['ONE',   'ONE',   'one'],
111             'two'   : ['two',   'two',   'two'],
112             'twO'   : ['twO',   'twO',   'two'],
113             'TWO'   : ['TWO',   'TWO',   'two'],
114             'three' : ['three', 'three', 'three'],
115             'thRee' : ['thRee', 'thRee', 'three'],
116             'THREE' : ['THREE', 'THREE', 'three'],
117             'a'     : ['one',   'one',   'one'],
118             'A'     : ['A',     'one',   'one'],
119             'b'     : ['two',   'two',   'two'],
120             'B'     : ['B',     'two',   'two'],
121             'c'     : ['three', 'three', 'three'],
122             'C'     : ['C',     'three', 'three'],
123         }
124
125         for k, l in table.items():
126             x = o0.converter(k)
127             assert x == l[0], "o0 got %s, expected %s" % (x, l[0])
128             x = o1.converter(k)
129             assert x == l[1], "o1 got %s, expected %s" % (x, l[1])
130             x = o2.converter(k)
131             assert x == l[2], "o2 got %s, expected %s" % (x, l[2])
132
133     def test_validator(self):
134         """Test the EnumVariable validator"""
135         opts = SCons.Variables.Variables()
136         opts.Add(SCons.Variables.EnumVariable('test0', 'test option help', 0,
137                                           ['one', 'two', 'three'],
138                                           {'a' : 'one',
139                                            'b' : 'two',
140                                            'c' : 'three'},
141                                           ignorecase=0))
142         opts.Add(SCons.Variables.EnumVariable('test1', 'test option help', 0,
143                                           ['one', 'two', 'three'],
144                                           {'a' : 'one',
145                                            'b' : 'two',
146                                            'c' : 'three'},
147                                           ignorecase=1))
148         opts.Add(SCons.Variables.EnumVariable('test2', 'test option help', 0,
149                                           ['one', 'two', 'three'],
150                                           {'a' : 'one',
151                                            'b' : 'two',
152                                            'c' : 'three'},
153                                           ignorecase=2))
154
155         o0 = opts.options[0]
156         o1 = opts.options[1]
157         o2 = opts.options[2]
158
159         def valid(o, v):
160             o.validator('X', v, {})
161
162         def invalid(o, v):
163             caught = None
164             try:
165                 o.validator('X', v, {})
166             except SCons.Errors.UserError:
167                 caught = 1
168             assert caught, "did not catch expected UserError for o = %s, v = %s" % (o.key, v)
169
170         table = {
171             'one'   : [  valid,   valid,   valid],
172             'One'   : [invalid,   valid,   valid],
173             'ONE'   : [invalid,   valid,   valid],
174             'two'   : [  valid,   valid,   valid],
175             'twO'   : [invalid,   valid,   valid],
176             'TWO'   : [invalid,   valid,   valid],
177             'three' : [  valid,   valid,   valid],
178             'thRee' : [invalid,   valid,   valid],
179             'THREE' : [invalid,   valid,   valid],
180             'a'     : [invalid, invalid, invalid],
181             'A'     : [invalid, invalid, invalid],
182             'b'     : [invalid, invalid, invalid],
183             'B'     : [invalid, invalid, invalid],
184             'c'     : [invalid, invalid, invalid],
185             'C'     : [invalid, invalid, invalid],
186             'no_v'  : [invalid, invalid, invalid],
187         }
188
189         for v, l in table.items():
190             l[0](o0, v)
191             l[1](o1, v)
192             l[2](o2, v)
193
194
195 if __name__ == "__main__":
196     suite = unittest.makeSuite(EnumVariableTestCase, 'test_')
197     if not unittest.TextTestRunner().run(suite).wasSuccessful():
198         sys.exit(1)
199
200 # Local Variables:
201 # tab-width:4
202 # indent-tabs-mode:nil
203 # End:
204 # vim: set expandtab tabstop=4 shiftwidth=4: