This repository was archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathchecker_test.py
More file actions
executable file
·171 lines (128 loc) · 5.29 KB
/
checker_test.py
File metadata and controls
executable file
·171 lines (128 loc) · 5.29 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding:utf-8; python-indent:2; indent-tabs-mode:nil -*-
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import types
import unittest
from pytypedecl import checker
from tests import simple
class TestChecker(unittest.TestCase):
def testSimpleArgTypeNoError(self):
"""Type checking of function with single argument."""
# there should be no exceptions thrown
result = simple.IntToInt(2)
self.assertEquals(42, result)
def testSimpleArgTypeError(self):
"""Type checking of function with single argument.
"""
# there should be a type error exception
expected = checker.ParamTypeErrorMsg("IntToInt", "i", str, int)
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
simple.IntToInt("test")
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testMultiArgTypeError(self):
"""Type checking of function with multiple argument.
"""
# there should be a type error exception
expected_a = checker.ParamTypeErrorMsg("MultiArgs", "a", str, int)
expected_c = checker.ParamTypeErrorMsg("MultiArgs", "c", int, dict)
expected_d = checker.ParamTypeErrorMsg("MultiArgs", "d", dict, str)
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
simple.MultiArgs("test", 1, 2, {})
a, c, d = context.exception.args[0]
self.assertEquals(expected_a, a)
self.assertEquals(expected_c, c)
self.assertEquals(expected_d, d)
def testReturnTypeNoError(self):
"""Type checking of return type of function.
"""
res = simple.GoodRet()
self.assertEquals(type(res), int)
def testReturnTypeError(self):
"""Type checking of return type of function.
"""
expected = checker.ReturnTypeErrorMsg("BadRet", str, int)
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
simple.BadRet()
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testReturnNone(self):
"""Type checking of return type of function with None.
"""
expected = checker.ReturnTypeErrorMsg("NoneRet", list, types.NoneType)
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
simple.NoneRet()
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testReturnAClassType(self):
"""Type checking of return type of a function returning a class type.
"""
expected = checker.ReturnTypeErrorMsg("AppleRet",
simple.Banana,
simple.Apple)
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
simple.AppleRet()
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testExceptionCorrect(self):
"""Type checking of exception: the correct exception is thrown.
"""
with self.assertRaises(simple.FooException):
simple.FooFail()
def testExceptionListCorrect(self):
"""Type checking of exception: one correct exception from a list.
"""
with self.assertRaises(simple.WrongException):
simple.WrongFail()
def testExceptionIncorrect(self):
"""Type checking of exception: incorrect exception thrown.
"""
expected = checker.ExceptionTypeErrorMsg(
"BadFail",
simple.BadException,
(simple.FooException, simple.WrongException))
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
simple.BadFail()
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testMultiTypeErrors(self):
"""Type checking of params and exceptions.
"""
expected_a = checker.ParamTypeErrorMsg("MultiFail",
"a",
simple.Banana,
simple.Apple)
expected_e = checker.ExceptionTypeErrorMsg(
"MultiFail",
simple.BadException,
(simple.FooException,))
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
simple.MultiFail(simple.Banana())
a, e = context.exception.args[0]
self.assertEquals(expected_a, a)
self.assertEquals(expected_e, e)
def testOptionalTypeParam(self):
"""Type checking with params without type.
"""
self.assertEquals(1, simple.MultiArgsNoType(1, 2, 3, "4", []))
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
simple.MultiArgsNoType(1, 2, 3, 4, 5)
expected_p = checker.ParamTypeErrorMsg("MultiArgsNoType",
"d",
int,
str)
[actual] = context.exception.args[0]
self.assertEquals(expected_p, actual)
if __name__ == "__main__":
unittest.main()