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
|
import {
Bind,
BindTo,
Do,
Effect,
Label,
Noop,
Pure,
Seq,
} from '../stdlib/effect'
import { Throw, Try } from '../stdlib/exception'
import { PutStringLn } from '../stdlib/stdio'
import { DefineEffect, JsExpr, SetEvalEnvironment } from '../stdlib/sys'
import { Test, AssertEqualsK, AssertEquals } from '../stdlib/test'
interface MyEffect<_a extends number, _b extends string, _c extends unknown>
extends Effect {}
export type main = [
SetEvalEnvironment<'test.node'>,
PutStringLn<'====================='>,
PutStringLn<'=== Running tests ==='>,
PutStringLn<'=====================\n\n'>,
testDo,
testSeq,
testBind,
testException,
testFFI,
PutStringLn<''>
]
type testSeq = Do<
[
PutStringLn<'Seq'>,
Test<
'should run a sequence of effects and all results',
[
BindTo<'result', Seq<[Pure<1>, Pure<2>, Pure<3>, Pure<4>]>>,
Bind<Label<'result'>, AssertEqualsK<[1, 2, 3, 4]>>
]
>,
Test<
'should produce undefined for effects with no result',
[
BindTo<'result', Seq<[Pure<1>, Noop, Pure<3>, Noop]>>,
Bind<Label<'result'>, AssertEqualsK<[1, undefined, 3, undefined]>>
]
>
]
>
type testDo = Do<
[
PutStringLn<'Do'>,
Test<
'should run a sequence of effects and return the last effect',
[
BindTo<'result', Do<[Pure<1>, Pure<2>, Pure<3>, Pure<4>]>>,
Bind<Label<'result'>, AssertEqualsK<4>>
]
>,
Test<
'should return undefined if last effect doesnt produce a result',
[
BindTo<'result', Do<[Pure<1>, Noop, Pure<3>, Noop]>>,
Bind<Label<'result'>, AssertEqualsK<undefined>>
]
>
]
>
type testBind = Do<
[
PutStringLn<'Bind & BindTo'>,
Test<
'should bind value to function and kind',
[
Bind<
Pure<[1, 2, 3]>,
<val extends number[]>() => AssertEquals<val, [1, 2, 3]>
>,
Bind<Pure<[1, 2, 3]>, AssertEqualsK<[1, 2, 3]>>
]
>,
Test<
'should bind label correctly',
[
BindTo<'value', Pure<{ a: 'b'; c: { d: 1 } }>>,
Bind<Label<'value'>, AssertEqualsK<{ a: 'b'; c: { d: 1 } }>>
]
>,
Test<
'should bind only inside Do scope',
[
Do<
[
BindTo<'value', Pure<'some value'>>,
Bind<Label<'value'>, AssertEqualsK<'some value'>>
]
>,
Bind<Try<Label<'value'>, <_>() => Pure<'none'>>, AssertEqualsK<'none'>>
]
>,
Test<
'should bind only inside Bind scope',
[
Bind<
BindTo<'value', Pure<'some value'>>,
<_>() => Bind<Label<'value'>, AssertEqualsK<'some value'>>
>,
Bind<Try<Label<'value'>, <_>() => Pure<'none'>>, AssertEqualsK<'none'>>
]
>
]
>
type testException = Do<
[
PutStringLn<'Try/Throw'>,
Test<
'should return the result of effect',
[Bind<Try<Pure<20>, <_>() => Pure<0>>, AssertEqualsK<20>>]
>,
Test<
'should return the result of error handler',
[
Bind<
Try<Do<[Throw<'wow'>, Pure<20>]>, <_>() => Pure<0>>,
AssertEqualsK<0>
>,
Bind<
Try<Do<[Throw<'wow'>, Pure<20>]>, <e>() => Pure<e>>,
AssertEqualsK<'wow'>
>
]
>
]
>
type testFFI = Do<
[
PutStringLn<'FFI'>,
Test<
'should return the result of js expression',
[Bind<JsExpr<'69 * 420'>, AssertEqualsK<28980>>]
>,
Test<
'should test custom effect',
[
DefineEffect<
'MyEffect',
`([a, b, c], ctx) => {
return [
ctx.getTypeValue(a) * 2,
ctx.getTypeValue(b) + '!',
[...ctx.getTypeValue(c), 'wow'],
]
}`
>,
Bind<
MyEffect<4, 'hey', [1, 2]>,
AssertEqualsK<[8, 'hey!', [1, 2, 'wow']]>
>
]
>
]
>
|