Skip to content

Commit 99524ba

Browse files
committed
Add :js to some tests so we can check the output
1 parent 8437765 commit 99524ba

File tree

2 files changed

+144
-41
lines changed

2 files changed

+144
-41
lines changed

hkmc2/shared/src/test/mlscript/ucs/general/DualOptions.mls

Lines changed: 105 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:js
2+
13
// DualOptions.mls
24
// ===============
35
// Matching two options in any possible order.
@@ -8,7 +10,6 @@ class Some[T](value: T) extends Option[T]
810
module None extends Option[nothing]
911
class Pair[A, B](x: A, y: B)
1012

11-
// All `add_n` functions should be inferred to have the same type.
1213

1314
fun add_1(x, y) =
1415
if
@@ -18,9 +19,16 @@ fun add_1(x, y) =
1819
x is None and y is None then 0
1920

2021
add_1(None, None)
22+
//│ = 0
23+
2124
add_1(Some(5), None)
25+
//│ = 5
26+
2227
add_1(None, Some(9))
28+
//│ = 9
29+
2330
add_1(Some(5), Some(9))
31+
//│ = 14
2432

2533
fun add_2(x, y) =
2634
if x is
@@ -32,9 +40,16 @@ fun add_2(x, y) =
3240
None then 0
3341

3442
add_2(None, None)
43+
//│ = 0
44+
3545
add_2(Some(5), None)
46+
//│ = 5
47+
3648
add_2(None, Some(9))
49+
//│ = 9
50+
3751
add_2(Some(5), Some(9))
52+
//│ = 14
3853

3954

4055
fun add_3(x, y) =
@@ -45,9 +60,16 @@ fun add_3(x, y) =
4560
Pair(None, None) then 0
4661

4762
add_3(None, None)
63+
//│ = 0
64+
4865
add_3(Some(5), None)
66+
//│ = 5
67+
4968
add_3(None, Some(9))
69+
//│ = 9
70+
5071
add_3(Some(5), Some(9))
72+
//│ = 14
5173

5274

5375
:fixme
@@ -72,21 +94,27 @@ fun add_4(x, y) =
7294

7395
:fixme
7496
add_4(None, None)
75-
add_4(Some(5), None)
76-
add_4(None, Some(9))
77-
add_4(Some(5), Some(9))
7897
//│ ╔══[ERROR] Name not found: add_4
79-
//│ ║ l.74: add_4(None, None)
98+
//│ ║ l.96: add_4(None, None)
8099
//│ ╙── ^^^^^
100+
101+
:fixme
102+
add_4(Some(5), None)
81103
//│ ╔══[ERROR] Name not found: add_4
82-
//│ ║ l.75: add_4(Some(5), None)
83-
//│ ╙── ^^^^^
104+
//│ ║ l.102: add_4(Some(5), None)
105+
//│ ╙── ^^^^^
106+
107+
:fixme
108+
add_4(None, Some(9))
84109
//│ ╔══[ERROR] Name not found: add_4
85-
//│ ║ l.76: add_4(None, Some(9))
86-
//│ ╙── ^^^^^
110+
//│ ║ l.108: add_4(None, Some(9))
111+
//│ ╙── ^^^^^
112+
113+
:fixme
114+
add_4(Some(5), Some(9))
87115
//│ ╔══[ERROR] Name not found: add_4
88-
//│ ║ l.77: add_4(Some(5), Some(9))
89-
//│ ╙── ^^^^^
116+
//│ ║ l.114: add_4(Some(5), Some(9))
117+
//│ ╙── ^^^^^
90118

91119

92120
fun add_5(x, y) =
@@ -98,9 +126,16 @@ fun add_5(x, y) =
98126

99127

100128
add_5(None, None)
129+
//│ = 0
130+
101131
add_5(Some(5), None)
132+
//│ = 5
133+
102134
add_5(None, Some(9))
135+
//│ = 9
136+
103137
add_5(Some(5), Some(9))
138+
//│ = 14
104139

105140

106141
fun add_6(x, y) =
@@ -111,24 +146,43 @@ fun add_6(x, y) =
111146
[None, None] then 0
112147

113148
add_6(None, None)
149+
//│ = 0
150+
114151
add_6(Some(5), None)
152+
//│ = 5
153+
115154
add_6(None, Some(9))
155+
//│ = 9
156+
116157
add_6(Some(5), Some(9))
158+
//│ = 14
117159

118160

119-
fun p(x) = true
120161

121162

122-
// FIXME Remove `case p(x) of true -> 0; _ -> 0`
123-
fun add_6(x, y) =
163+
fun add_6(p, x, y) =
124164
if
125165
x is Some(xv) and y is Some(yv) then xv + yv
126166
y is None and p(x) and x is Some(xv) then 42
127167
y is None and x is Some(xv) then xv
128168
x is None and y is Some(yv) then yv
129169
y is None and x is None then 0
130170

131-
fun add_7(x, y) =
171+
172+
add_6((x) => true, None, None)
173+
//│ = 0
174+
175+
add_6((x) => true, Some(5), None)
176+
//│ = 42
177+
178+
add_6((x) => true, None, Some(9))
179+
//│ = 9
180+
181+
add_6((x) => true, Some(5), Some(9))
182+
//│ = 14
183+
184+
185+
fun add_7(p, x, y) =
132186
if
133187
x is Some(xv) and y is Some(yv) then xv + yv
134188
// y is None and p(x) and x is Some(xv) then 42
@@ -137,7 +191,24 @@ fun add_7(x, y) =
137191
y is Some(yv) and x is None then yv
138192
y is None and x is None then 0
139193

140-
fun add_8(x, y) =
194+
195+
add_7((x) => x > 0, None, None)
196+
//│ = 0
197+
198+
add_7((x) => x > 0, Some(5), None)
199+
//│ = 5
200+
201+
add_7((x) => x > 0, None, Some(9))
202+
//│ = 36
203+
204+
add_7((x) => x > 0, None, Some(-9))
205+
//│ = -9
206+
207+
add_7((x) => x > 0, Some(5), Some(9))
208+
//│ = 14
209+
210+
211+
fun add_8(p, x, y) =
141212
if
142213
x is Some(xv) and y is Some(yv) then xv + yv
143214
y is None and p(x) and x is Some(xv) then 42
@@ -147,3 +218,21 @@ fun add_8(x, y) =
147218
y is None and x is None then 0
148219

149220

221+
add_8((x) => x > 0, None, None)
222+
//│ = 0
223+
224+
add_8((x) => true, Some(9), None)
225+
//│ = 42
226+
227+
add_8((x) => x > 0, Some(5), None)
228+
//│ = 5
229+
230+
add_8((x) => x > 0, None, Some(9))
231+
//│ = 36
232+
233+
add_8((x) => x > 0, None, Some(-9))
234+
//│ = -9
235+
236+
add_8((x) => x > 0, Some(5), Some(9))
237+
//│ = 14
238+
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1+
:js
2+
13
// We test the support for simple tuple patterns in this file.
24
// Splice tuple patterns will be implement in the future.
35

46
fun sum(x, y) = x + y
57
sum(1, 2)
8+
//│ = 3
69

710
:fixme
811
fun sum([x, y]) = x + y
912
sum([1, 2])
1013
//│ /!!!\ Uncaught error: scala.MatchError: Tup(List(Ident(x), Ident(y))) (of class hkmc2.syntax.Tree$Tup)
1114

1215
:ucs desugared
13-
fun sum''(pair) =
16+
fun sum(pair) =
1417
if pair is [x, y] then x + y
15-
sum''([1, 2])
18+
sum([1, 2])
1619
//│ Desugared:
17-
//│ > if pair@31 is [$elem0@32, $elem1@33] and
18-
//│ > let x@34 = $elem0@32#0
19-
//│ > let y@35 = $elem1@33#0
20-
//│ > else .+#1(x@34#0, y@35#0)
20+
//│ > if pair@33 is [$elem0@34, $elem1@35] and
21+
//│ > let x@36 = $elem0@34#0
22+
//│ > let y@37 = $elem1@35#0
23+
//│ > else .+#1(x@36#0, y@37#0)
24+
//│ = 3
2125

2226
:ucs desugared
2327
fun test(thing) =
2428
if thing is [] then 0
29+
//│ Desugared:
30+
//│ > if thing@44 is [] then 0
31+
32+
:re
2533
test("")
34+
//│ ═══[RUNTIME ERROR] Error: match error
35+
36+
:re
2637
test(12)
27-
//│ Desugared:
28-
//│ > if thing@40 is [] then 0
38+
//│ ═══[RUNTIME ERROR] Error: match error
2939

3040
:ucs desugared
3141
class Point(x: Int, y: Int)
@@ -35,18 +45,19 @@ fun discarded_cases(thing) =
3545
Point(x, y) then x + y
3646
//│ Desugared:
3747
//│ > if
38-
//│ > thing@45 is [$elem0@51, $elem1@52] and
39-
//│ > let x@53 = $elem0@51#0
40-
//│ > let y@54 = $elem1@52#0
41-
//│ > else .+#3(x@53#0, y@54#0)
42-
//│ > thing@45 is Point($param0@46, $param1@47) and
43-
//│ > let x@48 = $param0@46#0
44-
//│ > let y@49 = $param1@47#0
45-
//│ > else .+#2(x@48#0, y@49#0)
48+
//│ > thing@53 is [$elem0@59, $elem1@60] and
49+
//│ > let x@61 = $elem0@59#0
50+
//│ > let y@62 = $elem1@60#0
51+
//│ > else .+#3(x@61#0, y@62#0)
52+
//│ > thing@53 is Point($param0@54, $param1@55) and
53+
//│ > let x@56 = $param0@54#0
54+
//│ > let y@57 = $param1@55#0
55+
//│ > else .+#2(x@56#0, y@57#0)
4656

4757
:e
4858
:todo
4959
discarded_cases(Point(0, 0))
60+
//│ = 0
5061

5162
// A workaround is to move the tuple pattern to the last case.
5263
:ucs desugared
@@ -56,20 +67,22 @@ fun working_cases(thing) =
5667
[x, y] then x + y
5768
//│ Desugared:
5869
//│ > if
59-
//│ > thing@61 is Point($param0@67, $param1@68) and
60-
//│ > let x@69 = $param0@67#0
61-
//│ > let y@70 = $param1@68#0
62-
//│ > else .+#5(x@69#0, y@70#0)
63-
//│ > thing@61 is [$elem0@62, $elem1@63] and
64-
//│ > let x@64 = $elem0@62#0
65-
//│ > let y@65 = $elem1@63#0
66-
//│ > else .+#4(x@64#0, y@65#0)
70+
//│ > thing@73 is Point($param0@79, $param1@80) and
71+
//│ > let x@81 = $param0@79#0
72+
//│ > let y@82 = $param1@80#0
73+
//│ > else .+#5(x@81#0, y@82#0)
74+
//│ > thing@73 is [$elem0@74, $elem1@75] and
75+
//│ > let x@76 = $elem0@74#0
76+
//│ > let y@77 = $elem1@75#0
77+
//│ > else .+#4(x@76#0, y@77#0)
6778

6879
working_cases(Point(0, 0))
80+
//│ = 0
6981

7082
// However, the `Object` type forbids tuples to be used.
7183
:todo
7284
working_cases([0, 0])
85+
//│ = 0
7386

7487

7588
fun not_working(x) =
@@ -80,6 +93,7 @@ fun not_working(x) =
8093
0
8194

8295
not_working([1, 2, 3])
96+
//│ = 6
8397

84-
:todo
8598
not_working([1, 2])
99+
//│ = 0

0 commit comments

Comments
 (0)