-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_returns.py
62 lines (45 loc) · 1.62 KB
/
test_returns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import unittest
from pyvalid import InvalidReturnTypeError, returns
from pyvalid.validators import is_validator
class ReturnsDecoratorTestCase(unittest.TestCase):
def setUp(self):
@returns(float, int)
def func1(arg):
return arg
self.func1 = func1
@returns(str, None, -1)
def func2(arg):
return arg
self.func2 = func2
@is_validator
def func3_checker(val):
return val == 'val1'
@returns(func3_checker, None, bool)
def func3(arg):
return arg
self.func3 = func3
@returns()
def func_with_doc():
"""TEST_DOCSTRING"""
pass
self.func_with_doc = func_with_doc
def test_valid_returns(self):
# func1
self.assertEqual(self.func1(int()), int())
self.assertEqual(self.func1(float()), float())
self.assertEqual(self.func2(str()), str())
self.assertRaises(InvalidReturnTypeError, self.func1, str())
self.assertRaises(InvalidReturnTypeError, self.func1, None)
# func2
self.assertIsNone(self.func2(None))
self.assertEqual(self.func2(-1), -1)
self.assertRaises(InvalidReturnTypeError, self.func2, int())
# func3
self.assertEqual(self.func3('val1'), 'val1')
self.assertIsNone(self.func3(None), 'val1')
self.assertEqual(self.func3(True), True)
self.assertRaises(InvalidReturnTypeError, self.func3, int())
def test_docstring(self):
self.assertEqual(self.func_with_doc.__doc__, 'TEST_DOCSTRING')
if __name__ == '__main__':
unittest.main()