This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
checker_union_test.py
executable file
·170 lines (130 loc) · 6.1 KB
/
checker_union_test.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
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
# -*- 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 unittest
from pytypedecl import checker
from pytypedecl import pytd
from tests import simple
from tests import union
class TestCheckerUnion(unittest.TestCase):
def testSimpleArgNoneAble(self):
"""Type checking of function with none-able argument."""
# should work with no exceptions
self.assertEquals(0, union.StrToInt(None))
self.assertEquals(10, union.StrToInt("10"))
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
union.StrToInt(10) # can only pass str? so this should be an error
expected = checker.ParamTypeErrorMsg("StrToInt",
"s",
int,
pytd.UnionType([str, type(None)]))
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testNoneAbleAdd(self):
"""Type checking of function with None-able args, return and overloading.
"""
self.assertEquals(None, union.Add(None, 4))
self.assertEquals(None, union.Add(10.0, None))
self.assertEquals(10, union.Add(5, 5))
self.assertEquals(10.0, union.Add(5.0, 5.0))
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
union.Add([1], None) # list not in signature
expected = checker.OverloadingTypeErrorMsg("Add")
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testUnionSimple(self):
"""Type checking of function with union args.
"""
self.assertEquals(42.0, union.IntOrFloat(1, 2.0))
self.assertEquals(42.0, union.IntOrFloat(1.0, 2))
def testUnionError(self):
"""Type checking of function with union args (error).
"""
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
union.IntOrFloat("1", 2)
expected = checker.ParamTypeErrorMsg("IntOrFloat",
"a",
str,
pytd.UnionType([int, float]))
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testUnionNone(self):
"""Type checking of function with None union.
"""
self.assertEquals(3, union.IntOrNone(3))
self.assertEquals(None, union.IntOrNone(None))
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
union.IntOrNone("error")
expected_param = checker.ParamTypeErrorMsg("IntOrNone",
"a",
str,
pytd.UnionType([int,
type(None)]))
expected_ret = checker.ReturnTypeErrorMsg("IntOrNone",
str,
pytd.UnionType([int,
type(None)]))
[actual_param, actual_ret] = context.exception.args[0]
self.assertEquals(expected_param, actual_param)
self.assertEquals(expected_ret, actual_ret)
def testUnionWithClassTypes(self):
"""Type checking of function with union and class types.
"""
self.assertEquals(None, union.AppleOrBananaOrOrange(simple.Apple()))
self.assertEquals(None, union.AppleOrBananaOrOrange(simple.Banana()))
self.assertEquals(None, union.AppleOrBananaOrOrange(simple.Orange()))
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
union.AppleOrBananaOrOrange(42)
expected = checker.ParamTypeErrorMsg("AppleOrBananaOrOrange",
"f",
int,
pytd.UnionType([simple.Apple,
simple.Banana,
simple.Orange]))
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testUnionInReturnOK(self):
"""Typechecking fct with union in return type.
"""
self.assertEquals([42], union.UnionReturn())
def testUnionInReturnError(self):
"""Typechecking fct with union in return type (error).
"""
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
union.UnionReturnError()
expected = checker.ReturnTypeErrorMsg("UnionReturnError",
tuple,
pytd.UnionType([int, list]))
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
def testIntersectionSimple(self):
"""Typechecking fct with intersection types.
"""
self.assertEquals("cool", union.DoSomeIOStuff(union.File()))
def testIntersectionError(self):
"""Typechecking fct with intersection types (error).
"""
with self.assertRaises(checker.CheckTypeAnnotationError) as context:
union.DoSomeIOStuff(union.Readable()) # we want Readable & Writable
expected = checker.ParamTypeErrorMsg("DoSomeIOStuff",
"f",
union.Readable,
pytd.IntersectionType(
[union.Readable,
union.Writable]))
[actual] = context.exception.args[0]
self.assertEquals(expected, actual)
# TODO(raoulDoc): more tests! mixing overloading etc
if __name__ == "__main__":
unittest.main()