-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.ergo
196 lines (186 loc) · 5.08 KB
/
test.ergo
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
type = {
enum = {
MapResult = std:Type:enum MapResult {
Ok = std:Map
Error = std:String
}
ok = MapResult:Ok {}
err = MapResult:Error "oh no"
MapResult _ = $ok
MapResult _ = $err
MapResult:@:Ok {} = $ok
MapResult:@:Error "oh no" = $err
std:Bool:true
}
struct = {
StringPair = std:Type:struct StringPair {
first = std:String
second = std:String
}
pair = StringPair:new { first = hello, second = world }
StringPair _ = $pair
StringPair:@ {first = hello, second = world} = $pair
std:Bool:true
}
pass = {
std:Type:pass _ = a
std:Type:pass _ = std:Error:@ a
std:Bool:true
}
get = {
check = fn ~:allow-error :T :v -> {
std:Bool:true = std:equal (std:Type:basic $T) (std:Type:get ^{allow-error} $v)
()
}
check std:String str
check std:Array [a,b]
check std:Unit ()
check std:Unset $unset
std:Error _ = std:Type:get <| std:Error:new err
check ~allow-error std:Error <| std:Error:new err
$unset = std:Type:get ~no-eval {[a,b]}
std:Type :MyType = std:Type:new my_type
i = MyType:new a b c
std:match (std:Type:get $i) [$MyType -> ()]
m = { :(std:Type:basic $MyType) = () }
std:Type :t = std:Type:get $i
() = m:$t
std:Bool:true
}
}
required = {
exists = {
std:required _ = hi
std:Bool:true
}
missing = {
std:Error _ = { std:required _ = $unset }
std:Bool:true
}
}
if = {
:"one case" = {
true = {
std:String "yep" = std:if std:Bool:true yep
std:Bool:true
}
false = {
std:Unset _ = std:if std:Bool:false yep
std:Bool:true
}
}
else = {
std:String "nope" = std:if std:Bool:false yep else nope
std:Bool:true
}
:"else if" = {
std:String "second" = std:if std:Bool:false first else if std:Bool:true second
std:Bool:true
}
:"many else if" = {
std:String "third" = std:if std:Bool:false first else if std:Bool:false second else if std:Bool:true third
std:Bool:true
}
:"else if unset" = {
std:Unset _ = std:if std:Bool:false first else if std:Bool:false second else if std:Bool:false third
std:Bool:true
}
:"else if else" = {
std:String "nope" = std:if std:Bool:false first else if std:Bool:false second else if std:Bool:false third else nope
std:Bool:true
}
invalid = {
std:Error _ = std:if std:Bool:false a elif b
std:Error _ = std:if std:Bool:false a else if a
std:Error _ = std:if std:Bool:false a b
std:Bool:true
}
}
f = fn :a ^(std:optargs :b :c) -> {b,c}
optargs = {
none = {
{} = f 1
std:Bool:true
}
one = {
{b=2} = f 1 2
std:Bool:true
}
all = {
{b=2,c=3} = f 1 2 3
std:Bool:true
}
:"too many" = {
std:Error _ = f 1 2 3 4
std:Bool:true
}
:"explicit unset" = {
f = fn :a ^(std:optargs (std:default :b as ())) -> $b
() = f 1
std:Bool:true
}
}
dynamic = {
bind = {
:"same as eval" = {
v = std:dynamic:get key
"foo" = std:dynamic:eval { key = foo } $v
"bar" = std:dynamic:bind { key = bar } $v
std:Bool:true
}
:"affects nested" = {
m = {
x = {
y = std:dynamic:get key
}
}
$unset = std:dynamic:eval { key = foo } $m |>:x:y
"foo" = std:dynamic:bind { key = foo } $m |>:x:y
std:Bool:true
}
}
}
Unset = {
or = {
none = {
$unset = std:Unset:or ^[]
std:Bool:true
}
one = {
$unset = std:Unset:or $unset
"a" = std:Unset:or a
std:Bool:true
}
many = {
"1" = std:Unset:or $unset { $unset } 1 2 3
$unset = std:Unset:or $unset { $unset } $unset
std:Bool:true
}
}
index = {
none = {
() = std:Unset:index ()
std:Bool:true
}
one = {
1 = std:Unset:index {a=1} a
std:Bool:true
}
many = {
123 = std:Unset:index { a = { b = { c = 123 }, d = 4 } } a b c
std:Bool:true
}
missing = {
$unset = std:Unset:index { a = { b = { c = 123 }, d = 4 } } x
$unset = std:Unset:index { a = { b = { c = 123 }, d = 4 } } a x
$unset = std:Unset:index { a = { b = { c = 123 }, d = 4 } } a b x
std:Error _ = std:Unset:index { a = { b = { c = 123 }, d = 4 } } a b c x
std:Bool:true
}
bad-index = {
std:Error _ = std:Unset:index { a = { b = { c = 123 }, d = 4 } } a b c x
std:Bool:true
}
}
}
std:test:run { if, type, required, optargs, dynamic, Unset }