~speijnik/python-argvalidate/devel

« back to all changes in this revision

Viewing changes to argvalidate_tests.py

  • Committer: Stephan Peijnik
  • Date: 2010-04-23 18:10:11 UTC
  • Revision ID: debian@sp.or.at-20100423181011-u1uo3ui7do6qw7gm
Initial import from HG tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#   Copyright (C) 2009 Stephan Peijnik (stephan@peijnik.at)
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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/>.
 
15
 
 
16
import unittest
 
17
 
 
18
# Environment needs to be set-up before importing argvalidate
 
19
import os
 
20
os.environ['ARGVALIDATE_WARN'] = '0'
 
21
os.environ['ARGVALIDATE_WARN_KWARG_AS_ARG'] = '0'
 
22
os.environ['ARGVALIDATE_ENABLE'] = '1'
 
23
 
 
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
 
30
 
 
31
loader = unittest.TestLoader()
 
32
 
 
33
class TestHelperClass:
 
34
    def __init__(self, x):
 
35
        self.x = 5
 
36
 
 
37
class ArgvalidateLengthTestCase(unittest.TestCase):
 
38
    def test00_wrong_decorator_argument_count(self):
 
39
        try:
 
40
            @accepts(int)
 
41
            def test_func(x, y):
 
42
                pass
 
43
 
 
44
            self.fail('DecoratorNonKeyLengthException not raised on invalid '\
 
45
                'non keyword argument count in decorator.')
 
46
        except DecoratorNonKeyLengthException:
 
47
            pass
 
48
 
 
49
    def test01_wrong_decorator_nonkey_argument_count(self):
 
50
        try:
 
51
            @accepts(int, int)
 
52
            def test_func(x, y=5):
 
53
                pass
 
54
            self.fail('DecoratorNonKeyLengthException not raised on invalid '\
 
55
                'non keyword argument count in decorator.')
 
56
        except DecoratorNonKeyLengthException:
 
57
            pass
 
58
 
 
59
    def test02_unspecified_key_argument(self):
 
60
        try:
 
61
            @accepts(int, z=int)
 
62
            def test_func(x, y=5):
 
63
                pass
 
64
            self.fail('DecoratorKeyLengthException not raised on invalid '\
 
65
                'non keyword argument count in decorator.')
 
66
        except DecoratorKeyUnspecifiedException:
 
67
            pass
 
68
 
 
69
 
 
70
ArgvalidateLengthSuite =\
 
71
    loader.loadTestsFromTestCase(ArgvalidateLengthTestCase)
 
72
 
 
73
class ArgvalidateCommonMixin:
 
74
    __test__ = False
 
75
    test_func = None
 
76
    test_func2 = None
 
77
    test_func3 = None
 
78
    test_func4 = None
 
79
    test_func5 = None
 
80
    test_func6 = None
 
81
    test_func7 = None
 
82
    test_func8 = None
 
83
 
 
84
    def test00_builtin_args_correct(self):
 
85
        try:
 
86
            self.test_func(123)
 
87
        except ArgumentTypeException, e:
 
88
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
89
 
 
90
    def test01_builtin_args_incorrect(self):
 
91
        self.assertRaises(ArgumentTypeException,self.test_func, 'string')
 
92
        
 
93
    def test02_builtin_kwargs_correct(self):
 
94
        try:
 
95
            self.test_func2(test_arg=123)
 
96
        except ArgumentTypeException, e:
 
97
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
98
 
 
99
    def test03_builtin_kwargs_incorrect(self):
 
100
        self.assertRaises(ArgumentTypeException, self.test_func2,\
 
101
             test_arg='string')
 
102
 
 
103
    def test04_builtin_kwarg_as_arg_correct(self):
 
104
        try:
 
105
            self.test_func2(123)
 
106
        except ArgumentTypeException, e:
 
107
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
108
 
 
109
    def test05_builtin_kwarg_as_arg_incorrect(self):
 
110
        self.assertRaises(ArgumentTypeException, self.test_func2, 'string')
 
111
 
 
112
    def test06_builtin_kwarg_mixed_correct(self):
 
113
        try:
 
114
            self.test_func4(1, 2, test_arg='test_arg')
 
115
        except ArgumentTypeException, e:
 
116
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
117
 
 
118
    def test07_builtin_kwarg_mixed_incorrect(self):
 
119
        self.assertRaises(ArgumentTypeException, self.test_func4, 'str',\
 
120
             'str', test_arg=5)
 
121
        
 
122
    def test08_builtin_kwarg_mixed_incorrect2(self):
 
123
        self.assertRaises(ArgumentTypeException, self.test_func4, 1,\
 
124
             'test', test_arg='test_arg')
 
125
 
 
126
    def test09_builtin_kwarg_default(self):
 
127
        try:
 
128
            self.test_func3()
 
129
        except ArgumentTypeException, e:
 
130
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
131
 
 
132
    def test10_builtin_kwarg_mixed_default(self):
 
133
        try:
 
134
            self.test_func5(1, y=2)
 
135
        except ArgumentTypeException, e:
 
136
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
137
 
 
138
    def test11_custom_instance_correct(self):
 
139
        try:
 
140
            self.test_func6(TestHelperClass(8))
 
141
        except ArgumentTypeException, e:
 
142
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
143
 
 
144
    def test12_custom_instance_incorrect(self):
 
145
        self.assertRaises(ArgumentTypeException, self.test_func6, int)
 
146
 
 
147
    def test13_custom_instance_incorrect2(self):
 
148
        self.assertRaises(ArgumentTypeException, self.test_func6, object())
 
149
 
 
150
    def test14_ignore_type(self):
 
151
        try:
 
152
            self.test_func7(5)
 
153
            self.test_func7('test')
 
154
        except ArgumentTypeException, e:
 
155
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
156
 
 
157
    def test15_mixed_type_correct(self):
 
158
        try:
 
159
            self.test_func8(5)
 
160
            self.test_func8('test')
 
161
        except ArgumentTypeException, e:
 
162
            self.fail('ArgvalidateTypeException raised: %s' % (e.message))
 
163
 
 
164
    def test16_mixed_type_incorrect(self):
 
165
        self.assertRaises(ArgumentTypeException, self.test_func8, 0.5)
 
166
 
 
167
class ArgvalidateFuncTestCase(ArgvalidateCommonMixin, unittest.TestCase):
 
168
    __test__ = True
 
169
    
 
170
    def __init__(self, *args, **kwargs):
 
171
        @accepts(int)
 
172
        def test_func(x):
 
173
            self.assertEquals(x, 123)
 
174
 
 
175
        @accepts(test_arg=int)
 
176
        def test_func2(test_arg=1):
 
177
            self.assertEquals(test_arg, 123)
 
178
 
 
179
        @accepts(test_arg=int)
 
180
        def test_func3(test_arg=1):
 
181
            self.assertEquals(test_arg, 1)
 
182
 
 
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')
 
188
 
 
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')
 
194
 
 
195
        @accepts(TestHelperClass)
 
196
        def test_func6(test_helper_instance):
 
197
            self.assertTrue(isinstance(test_helper_instance, TestHelperClass))
 
198
 
 
199
        @accepts(type_any)
 
200
        def test_func7(ignored_type):
 
201
            pass
 
202
 
 
203
        @accepts(one_of(str, int))
 
204
        def test_func8(str_or_int):
 
205
            self.assertTrue(isinstance(str_or_int, (str, int)))
 
206
 
 
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
 
215
 
 
216
        unittest.TestCase.__init__(self, *args, **kwargs)
 
217
 
 
218
ArgvalidateFuncSuite = loader.loadTestsFromTestCase(ArgvalidateFuncTestCase)
 
219
 
 
220
class ArgvalidateMethodTestCase(ArgvalidateCommonMixin, unittest.TestCase):
 
221
    __test__ = True
 
222
    
 
223
    def __init__(self, *args, **kwargs):
 
224
        assert_equals = self.assertEquals
 
225
        assert_true = self.assertTrue
 
226
 
 
227
        class TestClass(object):
 
228
            @accepts(int)
 
229
            def test_func(self, x):
 
230
                assert_equals(x, 123)
 
231
 
 
232
            @accepts(test_arg=int)
 
233
            def test_func2(self, test_arg=1):
 
234
                assert_equals(test_arg, 123)
 
235
 
 
236
            @accepts(test_arg=int)
 
237
            def test_func3(self, test_arg=1):
 
238
                assert_equals(test_arg, 1)
 
239
 
 
240
            @accepts(int, y=int, test_arg=str)
 
241
            def test_func4(self, x, y=1, test_arg='test'):
 
242
                assert_equals(x, 1)
 
243
                assert_equals(y, 2)
 
244
                assert_equals(test_arg, 'test_arg')
 
245
 
 
246
            @accepts(int, y=int, test_arg=str)
 
247
            def test_func5(self, x, y=1, test_arg='test_arg'):
 
248
                assert_equals(x, 1)
 
249
                assert_equals(y, 2)
 
250
                assert_equals(test_arg, 'test_arg')
 
251
 
 
252
            @accepts(TestHelperClass)
 
253
            def test_func6(self, test_helper_instance):
 
254
                assert_true(isinstance(test_helper_instance, TestHelperClass))
 
255
 
 
256
            @accepts(type_any)
 
257
            def test_func7(self,ignored_type):
 
258
                pass
 
259
 
 
260
            @accepts(one_of(str, int))
 
261
            def test_func8(self, str_or_int):
 
262
                assert_true(isinstance(str_or_int, (str, int)))
 
263
 
 
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
 
273
        
 
274
        unittest.TestCase.__init__(self, *args, **kwargs)
 
275
 
 
276
ArgvalidateMethodSuite = loader.loadTestsFromTestCase(ArgvalidateMethodTestCase)
 
277
 
 
278
class ArgvalidateReturnValueTestCase(unittest.TestCase):
 
279
    def __init__(self, *args, **kwargs):
 
280
        @returns(int)
 
281
        def test_func(var):
 
282
            return var
 
283
 
 
284
        self.test_func = test_func
 
285
        unittest.TestCase.__init__(self, *args, **kwargs)
 
286
 
 
287
    def test00_return_value_correct(self):
 
288
        try:
 
289
            self.test_func(5)
 
290
        except ReturnValueTypeException, e:
 
291
            self.fail('ReturnValueTypeException raised: %s' % (e.message))
 
292
 
 
293
 
 
294
    def test01_return_value_incorrect(self):
 
295
        self.assertRaises(ReturnValueTypeException, self.test_func, 'test')
 
296
 
 
297
ArgvalidateReturnValueSuite =\
 
298
     loader.loadTestsFromTestCase(ArgvalidateReturnValueTestCase)
 
299
 
 
300
class ArgvalidateStackingCase(unittest.TestCase):
 
301
    def test00_stacking_return_value_first(self):
 
302
        try:
 
303
            @returns(int)
 
304
            @accepts(str)
 
305
            def test_func(data):
 
306
                return 1
 
307
 
 
308
            class A(object):
 
309
                @returns(int)
 
310
                @accepts(str)
 
311
                def test_func(self, data):
 
312
                    return 1
 
313
 
 
314
            test_func('test')
 
315
            A().test_func('test')
 
316
        except DecoratorStackingException, e:
 
317
            self.fail('DecoratorStackingException raised: %s' % (e.message))
 
318
 
 
319
    def test01_stacking_args_first(self):
 
320
        try:
 
321
            @accepts(str)
 
322
            @returns(int)
 
323
            def test_func(data):
 
324
                return 1
 
325
 
 
326
            class A(object):
 
327
                @accepts(str)
 
328
                @returns(int)
 
329
                def test_func(self, data):
 
330
                    return 1
 
331
 
 
332
            test_func('test')
 
333
            A().test_func('test')
 
334
        except DecoratorStackingException, e:
 
335
            self.fail('DecoratorStackingException raised: %s' % (e.message))
 
336
 
 
337
    def test02_stacking_args_invalid(self):
 
338
        try:
 
339
            @accepts(str)
 
340
            @accepts(str)
 
341
            def test_func(data):
 
342
                return data
 
343
 
 
344
            self.fail('DecoratorStackingException not raised for double'+\
 
345
                 ' func_args decorator.')
 
346
        except DecoratorStackingException:
 
347
            pass
 
348
 
 
349
        try:
 
350
            class A(object):
 
351
                @accepts(str)
 
352
                @accepts(str)
 
353
                def test_func(self, data):
 
354
                    return data
 
355
 
 
356
            self.fail('DecoratorStackingException not raised for double'+\
 
357
                ' method_args decorator.')
 
358
        except DecoratorStackingException:
 
359
            pass
 
360
 
 
361
    def test03_stacking_return_value_invalid(self):
 
362
        try:
 
363
            @returns(int)
 
364
            @returns(int)
 
365
            def test_func():
 
366
                return 5
 
367
            self.fail('DecoratorStackingException not raised for double'+\
 
368
                ' returns decorator.')
 
369
        except DecoratorStackingException:
 
370
            pass
 
371
 
 
372
ArgvalidateStackingSuite = loader.loadTestsFromTestCase(ArgvalidateStackingCase)
 
373
 
 
374
class ArgvalidateMiscTestCase(unittest.TestCase):
 
375
    def test00_check_raises_exceptions(self):
 
376
        if 'ARGVALIDATE_WARN' in os.environ:
 
377
            argvalidate_warn = 0
 
378
            try:
 
379
                argvalidate_warn = int(os.environ['ARGVALIDATE_WARN'])
 
380
            except ValueError:
 
381
                pass
 
382
 
 
383
        if argvalidate_warn != 0:
 
384
            self.assertFalse(raises_exceptions())
 
385
        else:
 
386
            self.assertTrue(raises_exceptions())
 
387
        
 
388
 
 
389
ArgvalidateMiscSuite = loader.loadTestsFromTestCase(ArgvalidateMiscTestCase)
 
390
 
 
391
ArgvalidateTestSuite = unittest.TestSuite(
 
392
  [ArgvalidateFuncSuite, ArgvalidateMethodSuite, ArgvalidateReturnValueSuite,
 
393
  ArgvalidateLengthSuite, ArgvalidateStackingSuite, ArgvalidateMiscSuite])