1
# Copyright (C) 2009 Stephan Peijnik (stephan@peijnik.at)
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU Lesser General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Lesser General Public License for more details.
13
# You should have received a copy of the GNU Lesser General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
# Environment needs to be set-up before importing argvalidate
20
os.environ['ARGVALIDATE_WARN'] = '0'
21
os.environ['ARGVALIDATE_WARN_KWARG_AS_ARG'] = '0'
22
os.environ['ARGVALIDATE_ENABLE'] = '1'
24
from argvalidate import accepts, returns, one_of, type_any
25
from argvalidate import ArgumentTypeException
26
from argvalidate import ReturnValueTypeException
27
from argvalidate import DecoratorNonKeyLengthException
28
from argvalidate import DecoratorKeyUnspecifiedException
29
from argvalidate import DecoratorStackingException, raises_exceptions
31
loader = unittest.TestLoader()
33
class TestHelperClass:
34
def __init__(self, x):
37
class ArgvalidateLengthTestCase(unittest.TestCase):
38
def test00_wrong_decorator_argument_count(self):
44
self.fail('DecoratorNonKeyLengthException not raised on invalid '\
45
'non keyword argument count in decorator.')
46
except DecoratorNonKeyLengthException:
49
def test01_wrong_decorator_nonkey_argument_count(self):
52
def test_func(x, y=5):
54
self.fail('DecoratorNonKeyLengthException not raised on invalid '\
55
'non keyword argument count in decorator.')
56
except DecoratorNonKeyLengthException:
59
def test02_unspecified_key_argument(self):
62
def test_func(x, y=5):
64
self.fail('DecoratorKeyLengthException not raised on invalid '\
65
'non keyword argument count in decorator.')
66
except DecoratorKeyUnspecifiedException:
70
ArgvalidateLengthSuite =\
71
loader.loadTestsFromTestCase(ArgvalidateLengthTestCase)
73
class ArgvalidateCommonMixin:
84
def test00_builtin_args_correct(self):
87
except ArgumentTypeException, e:
88
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
90
def test01_builtin_args_incorrect(self):
91
self.assertRaises(ArgumentTypeException,self.test_func, 'string')
93
def test02_builtin_kwargs_correct(self):
95
self.test_func2(test_arg=123)
96
except ArgumentTypeException, e:
97
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
99
def test03_builtin_kwargs_incorrect(self):
100
self.assertRaises(ArgumentTypeException, self.test_func2,\
103
def test04_builtin_kwarg_as_arg_correct(self):
106
except ArgumentTypeException, e:
107
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
109
def test05_builtin_kwarg_as_arg_incorrect(self):
110
self.assertRaises(ArgumentTypeException, self.test_func2, 'string')
112
def test06_builtin_kwarg_mixed_correct(self):
114
self.test_func4(1, 2, test_arg='test_arg')
115
except ArgumentTypeException, e:
116
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
118
def test07_builtin_kwarg_mixed_incorrect(self):
119
self.assertRaises(ArgumentTypeException, self.test_func4, 'str',\
122
def test08_builtin_kwarg_mixed_incorrect2(self):
123
self.assertRaises(ArgumentTypeException, self.test_func4, 1,\
124
'test', test_arg='test_arg')
126
def test09_builtin_kwarg_default(self):
129
except ArgumentTypeException, e:
130
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
132
def test10_builtin_kwarg_mixed_default(self):
134
self.test_func5(1, y=2)
135
except ArgumentTypeException, e:
136
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
138
def test11_custom_instance_correct(self):
140
self.test_func6(TestHelperClass(8))
141
except ArgumentTypeException, e:
142
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
144
def test12_custom_instance_incorrect(self):
145
self.assertRaises(ArgumentTypeException, self.test_func6, int)
147
def test13_custom_instance_incorrect2(self):
148
self.assertRaises(ArgumentTypeException, self.test_func6, object())
150
def test14_ignore_type(self):
153
self.test_func7('test')
154
except ArgumentTypeException, e:
155
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
157
def test15_mixed_type_correct(self):
160
self.test_func8('test')
161
except ArgumentTypeException, e:
162
self.fail('ArgvalidateTypeException raised: %s' % (e.message))
164
def test16_mixed_type_incorrect(self):
165
self.assertRaises(ArgumentTypeException, self.test_func8, 0.5)
167
class ArgvalidateFuncTestCase(ArgvalidateCommonMixin, unittest.TestCase):
170
def __init__(self, *args, **kwargs):
173
self.assertEquals(x, 123)
175
@accepts(test_arg=int)
176
def test_func2(test_arg=1):
177
self.assertEquals(test_arg, 123)
179
@accepts(test_arg=int)
180
def test_func3(test_arg=1):
181
self.assertEquals(test_arg, 1)
183
@accepts(int, y=int, test_arg=str)
184
def test_func4(x, y=1, test_arg='test'):
185
self.assertEquals(x, 1)
186
self.assertEquals(y, 2)
187
self.assertEquals(test_arg, 'test_arg')
189
@accepts(int, y=int, test_arg=str)
190
def test_func5(x, y=1, test_arg='test_arg'):
191
self.assertEquals(x, 1)
192
self.assertEquals(y, 2)
193
self.assertEquals(test_arg, 'test_arg')
195
@accepts(TestHelperClass)
196
def test_func6(test_helper_instance):
197
self.assertTrue(isinstance(test_helper_instance, TestHelperClass))
200
def test_func7(ignored_type):
203
@accepts(one_of(str, int))
204
def test_func8(str_or_int):
205
self.assertTrue(isinstance(str_or_int, (str, int)))
207
self.test_func = test_func
208
self.test_func2 = test_func2
209
self.test_func3 = test_func3
210
self.test_func4 = test_func4
211
self.test_func5 = test_func5
212
self.test_func6 = test_func6
213
self.test_func7 = test_func7
214
self.test_func8 = test_func8
216
unittest.TestCase.__init__(self, *args, **kwargs)
218
ArgvalidateFuncSuite = loader.loadTestsFromTestCase(ArgvalidateFuncTestCase)
220
class ArgvalidateMethodTestCase(ArgvalidateCommonMixin, unittest.TestCase):
223
def __init__(self, *args, **kwargs):
224
assert_equals = self.assertEquals
225
assert_true = self.assertTrue
227
class TestClass(object):
229
def test_func(self, x):
230
assert_equals(x, 123)
232
@accepts(test_arg=int)
233
def test_func2(self, test_arg=1):
234
assert_equals(test_arg, 123)
236
@accepts(test_arg=int)
237
def test_func3(self, test_arg=1):
238
assert_equals(test_arg, 1)
240
@accepts(int, y=int, test_arg=str)
241
def test_func4(self, x, y=1, test_arg='test'):
244
assert_equals(test_arg, 'test_arg')
246
@accepts(int, y=int, test_arg=str)
247
def test_func5(self, x, y=1, test_arg='test_arg'):
250
assert_equals(test_arg, 'test_arg')
252
@accepts(TestHelperClass)
253
def test_func6(self, test_helper_instance):
254
assert_true(isinstance(test_helper_instance, TestHelperClass))
257
def test_func7(self,ignored_type):
260
@accepts(one_of(str, int))
261
def test_func8(self, str_or_int):
262
assert_true(isinstance(str_or_int, (str, int)))
264
instance = TestClass()
265
self.test_func = instance.test_func
266
self.test_func2 = instance.test_func2
267
self.test_func3 = instance.test_func3
268
self.test_func4 = instance.test_func4
269
self.test_func5 = instance.test_func5
270
self.test_func6 = instance.test_func6
271
self.test_func7 = instance.test_func7
272
self.test_func8 = instance.test_func8
274
unittest.TestCase.__init__(self, *args, **kwargs)
276
ArgvalidateMethodSuite = loader.loadTestsFromTestCase(ArgvalidateMethodTestCase)
278
class ArgvalidateReturnValueTestCase(unittest.TestCase):
279
def __init__(self, *args, **kwargs):
284
self.test_func = test_func
285
unittest.TestCase.__init__(self, *args, **kwargs)
287
def test00_return_value_correct(self):
290
except ReturnValueTypeException, e:
291
self.fail('ReturnValueTypeException raised: %s' % (e.message))
294
def test01_return_value_incorrect(self):
295
self.assertRaises(ReturnValueTypeException, self.test_func, 'test')
297
ArgvalidateReturnValueSuite =\
298
loader.loadTestsFromTestCase(ArgvalidateReturnValueTestCase)
300
class ArgvalidateStackingCase(unittest.TestCase):
301
def test00_stacking_return_value_first(self):
311
def test_func(self, data):
315
A().test_func('test')
316
except DecoratorStackingException, e:
317
self.fail('DecoratorStackingException raised: %s' % (e.message))
319
def test01_stacking_args_first(self):
329
def test_func(self, data):
333
A().test_func('test')
334
except DecoratorStackingException, e:
335
self.fail('DecoratorStackingException raised: %s' % (e.message))
337
def test02_stacking_args_invalid(self):
344
self.fail('DecoratorStackingException not raised for double'+\
345
' func_args decorator.')
346
except DecoratorStackingException:
353
def test_func(self, data):
356
self.fail('DecoratorStackingException not raised for double'+\
357
' method_args decorator.')
358
except DecoratorStackingException:
361
def test03_stacking_return_value_invalid(self):
367
self.fail('DecoratorStackingException not raised for double'+\
368
' returns decorator.')
369
except DecoratorStackingException:
372
ArgvalidateStackingSuite = loader.loadTestsFromTestCase(ArgvalidateStackingCase)
374
class ArgvalidateMiscTestCase(unittest.TestCase):
375
def test00_check_raises_exceptions(self):
376
if 'ARGVALIDATE_WARN' in os.environ:
379
argvalidate_warn = int(os.environ['ARGVALIDATE_WARN'])
383
if argvalidate_warn != 0:
384
self.assertFalse(raises_exceptions())
386
self.assertTrue(raises_exceptions())
389
ArgvalidateMiscSuite = loader.loadTestsFromTestCase(ArgvalidateMiscTestCase)
391
ArgvalidateTestSuite = unittest.TestSuite(
392
[ArgvalidateFuncSuite, ArgvalidateMethodSuite, ArgvalidateReturnValueSuite,
393
ArgvalidateLengthSuite, ArgvalidateStackingSuite, ArgvalidateMiscSuite])