1 | #!/usr/bin/env python |
---|
2 | import unittest |
---|
3 | |
---|
4 | from Cheetah.Utils import VerifyType |
---|
5 | from Cheetah import _verifytype |
---|
6 | |
---|
7 | class VerifyType_Test(unittest.TestCase): |
---|
8 | def test_Verified(self): |
---|
9 | arg = 'foo' |
---|
10 | legalTypes = [str, unicode] |
---|
11 | try: |
---|
12 | rc = VerifyType.VerifyType(arg, 'arg', legalTypes, 'string') |
---|
13 | assert rc |
---|
14 | except TypeError: |
---|
15 | self.fail('Should not have raised a TypeError here') |
---|
16 | |
---|
17 | try: |
---|
18 | rc = _verifytype.verifyType(arg, 'arg', legalTypes, 'string') |
---|
19 | assert rc |
---|
20 | except TypeError: |
---|
21 | self.fail('Should not have raised a TypeError here') |
---|
22 | |
---|
23 | def test_Unverified(self): |
---|
24 | arg = 'foo' |
---|
25 | legalTypes = [list, dict] |
---|
26 | self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg, |
---|
27 | 'arg', legalTypes, 'list or dict') |
---|
28 | self.failUnlessRaises(TypeError, _verifytype.verifyType, arg, |
---|
29 | 'arg', legalTypes, 'list or dict') |
---|
30 | |
---|
31 | def test_IncorrectNumberOfArgs(self): |
---|
32 | arg = 'foo' |
---|
33 | legalTypes = [str, unicode] |
---|
34 | |
---|
35 | self.failUnlessRaises(TypeError, VerifyType.VerifyType) |
---|
36 | self.failUnlessRaises(TypeError, _verifytype.verifyType) |
---|
37 | |
---|
38 | self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg) |
---|
39 | self.failUnlessRaises(TypeError, _verifytype.verifyType, arg) |
---|
40 | |
---|
41 | self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg, |
---|
42 | 'arg') |
---|
43 | self.failUnlessRaises(TypeError, _verifytype.verifyType, arg, |
---|
44 | 'arg') |
---|
45 | |
---|
46 | self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg, |
---|
47 | 'arg', legalTypes) |
---|
48 | self.failUnlessRaises(TypeError, _verifytype.verifyType, arg, |
---|
49 | 'arg', legalTypes) |
---|
50 | |
---|
51 | self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg, |
---|
52 | 'arg', legalTypes, 'string', 'errmsgExtra', 'one more') |
---|
53 | self.failUnlessRaises(TypeError, _verifytype.verifyType, arg, |
---|
54 | 'arg', legalTypes, 'string', 'errmsgExtra', 'one more') |
---|
55 | |
---|
56 | def test_LegalTypesNotIterable(self): |
---|
57 | arg = 'foo' |
---|
58 | legalTypes = 1 |
---|
59 | |
---|
60 | self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg, |
---|
61 | 'arg', legalTypes, 'string') |
---|
62 | self.failUnlessRaises(TypeError, _verifytype.verifyType, arg, |
---|
63 | 'arg', legalTypes, 'string') |
---|
64 | |
---|
65 | class FakeClass(dict): |
---|
66 | pass |
---|
67 | |
---|
68 | class VerifyTypeClass_Test(unittest.TestCase): |
---|
69 | def test_VerifiedClass(self): |
---|
70 | arg = FakeClass |
---|
71 | legalTypes = [type] |
---|
72 | try: |
---|
73 | rc = VerifyType.VerifyTypeClass(arg, 'arg', legalTypes, '', dict) |
---|
74 | assert rc |
---|
75 | except TypeError: |
---|
76 | self.fail('Should not have raised a TypeError here') |
---|
77 | |
---|
78 | try: |
---|
79 | rc = _verifytype.verifyTypeClass(arg, 'arg', legalTypes, 'foo', dict) |
---|
80 | assert rc |
---|
81 | except TypeError: |
---|
82 | self.fail('Should not have raised a TypeError here') |
---|
83 | |
---|
84 | def test_UnverifiedClass(self): |
---|
85 | arg = FakeClass |
---|
86 | legalTypes = [type] |
---|
87 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg, |
---|
88 | legalTypes, 'subclass of list', list) |
---|
89 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg, |
---|
90 | legalTypes, 'subclass of list', list) |
---|
91 | |
---|
92 | def test_Verified(self): |
---|
93 | arg = 'foo' |
---|
94 | legalTypes = [str, unicode] |
---|
95 | try: |
---|
96 | rc = VerifyType.VerifyTypeClass(arg, 'arg', legalTypes, 'string', int) |
---|
97 | assert rc |
---|
98 | except TypeError: |
---|
99 | self.fail('Should not have raised a TypeError here') |
---|
100 | |
---|
101 | try: |
---|
102 | rc = _verifytype.verifyTypeClass(arg, 'arg', legalTypes, 'string', int) |
---|
103 | assert rc |
---|
104 | except TypeError: |
---|
105 | self.fail('Should not have raised a TypeError here') |
---|
106 | |
---|
107 | def test_Unverified(self): |
---|
108 | arg = 'foo' |
---|
109 | legalTypes = [list, dict] |
---|
110 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg, |
---|
111 | 'arg', legalTypes, 'list or dict', int) |
---|
112 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg, |
---|
113 | 'arg', legalTypes, 'list or dict', int) |
---|
114 | |
---|
115 | def test_IncorrectNumberOfArgs(self): |
---|
116 | arg = 'foo' |
---|
117 | legalTypes = [str, unicode] |
---|
118 | |
---|
119 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass) |
---|
120 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass) |
---|
121 | |
---|
122 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg) |
---|
123 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg) |
---|
124 | |
---|
125 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg, |
---|
126 | 'arg') |
---|
127 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg, |
---|
128 | 'arg') |
---|
129 | |
---|
130 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg, |
---|
131 | 'arg', legalTypes) |
---|
132 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg, |
---|
133 | 'arg', legalTypes) |
---|
134 | |
---|
135 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg, |
---|
136 | 'arg', legalTypes, 'string') |
---|
137 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg, |
---|
138 | 'arg', legalTypes, 'string') |
---|
139 | |
---|
140 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg, |
---|
141 | 'arg', legalTypes, 'string', int, 'errmsgExtra', 'one more') |
---|
142 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg, |
---|
143 | 'arg', legalTypes, 'string', int, 'errmsgExtra', 'one more') |
---|
144 | |
---|
145 | def test_LegalTypesNotIterable(self): |
---|
146 | arg = 'foo' |
---|
147 | legalTypes = 1 |
---|
148 | |
---|
149 | self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg, |
---|
150 | 'arg', legalTypes, 'string', int) |
---|
151 | self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg, |
---|
152 | 'arg', legalTypes, 'string', int) |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | if __name__ == '__main__': |
---|
158 | unittest.main() |
---|