-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_hypertype.py
117 lines (98 loc) · 2.67 KB
/
test_hypertype.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
from hypertype import *
def test_simple_types():
assert String.valid("(↑t)")
assert not String.valid(5)
assert Integer.valid(1)
assert not Integer.valid("(↑t)")
assert Float.valid(3.14)
assert not Float.valid("(↑t)")
assert Boolean.valid(True)
assert Boolean.valid(False)
assert not Boolean.valid("(↑t)")
assert not Boolean.valid(3.14)
assert Nothing.valid(None)
assert not Nothing.valid(5)
assert not Nothing.valid("")
assert Any.valid(None)
assert Any.valid("(↑t)")
assert Any.valid(1)
assert Any.valid(3.14)
assert Any.valid(False)
def test_list():
Names = List(String)
assert Names.valid([])
assert Names.valid(["a", "b"])
assert not Names.valid(["a", "b", 3])
def test_tuple():
Point = Tuple(Integer, Integer)
assert Point.valid([1, 2]) is True
assert Point.valid([1, 2, 3]) is False
assert Point.valid([1, "a"]) is False
def test_record():
Person = Record({
"name": String,
"age": Integer,
"phone_numbers": List(String)
})
assert Person.valid({
"name": "Alice",
"age": 42,
"phone_numbers": ["123-456-7890", "123-456-7891"]
})
assert not Person.valid("Alice")
assert not Person.valid({
"name": "Alice",
"age": 42
})
assert not Person.valid({
"name": "Alice",
"age": 42,
"phone_numbers": "123-456-7890"
})
def type_dict():
PriceList = Dict(String, Float)
assert PriceList.valid({})
assert PriceList.valid({
"apple": 10.0,
"mango": 10.0,
})
assert not PriceList.valid({"apple": "x"})
def test_one_of():
Value = Integer | List(Integer)
assert Value.valid(1)
assert Value.valid([1, 2])
assert not Value.valid("foo")
assert not Value.valid(["a", "b"])
def test_literal():
Plus = Literal("+")
assert Plus.valid("+")
assert not Plus.valid("foo")
def test_reference():
BinOp = Literal("+") | Literal("*")
Expr = Reference()
Expr.set(
Integer
| Record({"left": Expr, "op": BinOp, "right": Expr})
)
assert Expr.valid(1)
assert Expr.valid({"left": 1, "op": "+", "right": 2})
assert Expr.valid({
"left": 1,
"op": "+",
"right": {
"left": 2,
"op": "*",
"right": 3
}})
def test_method():
Value = Reference()
Value.set(Integer | List(Value))
@method
def total(value: Integer):
return value
@method
def total(value: List(Value)):
return sum(total(v) for v in value)
assert total(1) == 1
assert total([1, 2, 3, 4]) == 10
assert total([1, [2, 3], 4]) == 10