Skip to content

Commit 00faff0

Browse files
committed
Use meaningful function names in tuple pattern tests
1 parent 63885f9 commit 00faff0

File tree

2 files changed

+96
-93
lines changed

2 files changed

+96
-93
lines changed

hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tupleGet([1, 2, 3, 4], -1)
1717
//│ = 4
1818

1919
:sjs
20-
fun stupid(xs) = if xs is
20+
fun nonsense(xs) = if xs is
2121
[..ys] then ys
2222
[] then "empty"
2323
//│ Desugared:
@@ -28,7 +28,7 @@ fun stupid(xs) = if xs is
2828
//│ > else ys@61#666
2929
//│ > xs@59 is []=0 then "empty"
3030
//│ JS:
31-
//│ function stupid(xs) {
31+
//│ function nonsense(xs) {
3232
//│ let rest, ys;
3333
//│ if (Array.isArray(xs) && xs.length >= 0) {
3434
//│ rest = globalThis.tupleSlice(xs, 0, 0);
@@ -40,14 +40,14 @@ fun stupid(xs) = if xs is
4040
//│ };
4141
//│ undefined
4242

43-
stupid([])
43+
nonsense([])
4444
//│ = []
4545

46-
stupid([1, 2, 3, 4])
46+
nonsense([1, 2, 3, 4])
4747
//│ = [ 1, 2, 3, 4 ]
4848

4949
:sjs
50-
fun foo(xs) = if xs is
50+
fun lead_and_last(xs) = if xs is
5151
[x, ..ys, y] then x + y
5252
[] then 0
5353
//│ Desugared:
@@ -62,7 +62,7 @@ fun foo(xs) = if xs is
6262
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@75#666, y@77#666)
6363
//│ > xs@71 is []=0 then 0
6464
//│ JS:
65-
//│ function foo(xs) {
65+
//│ function lead_and_last(xs) {
6666
//│ let last0, rest, first0, x, ys, y;
6767
//│ if (Array.isArray(xs) && xs.length >= 2) {
6868
//│ first0 = xs[0];
@@ -82,21 +82,20 @@ fun foo(xs) = if xs is
8282
//│ };
8383
//│ undefined
8484

85-
foo(["foo", "bar"])
85+
lead_and_last(["foo", "bar"])
8686
//│ = 'foobar'
8787

88-
foo([1, 2, 3, 4])
88+
lead_and_last([1, 2, 3, 4])
8989
//│ = 5
9090

91-
foo([])
91+
lead_and_last([])
9292
//│ = 0
9393

9494
:re
95-
foo(["boom"])
95+
lead_and_last(["boom"])
9696
//│ ═══[RUNTIME ERROR] Error: match error
9797

9898
:sjs
99-
:todo // Better using ... instead of .. for the rest pattern.
10099
fun nested_tuple_patterns(xs) = if xs is
101100
[x, ..[y, z], w] then x + y + z + w
102101
[] then 0

hkmc2/shared/src/test/mlscript/ucs/patterns/SimpleTuple.mls

Lines changed: 86 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -39,139 +39,143 @@ test("")
3939
test(12)
4040
//│ ═══[RUNTIME ERROR] Error: match error
4141

42-
:ucs desugared
4342
class Point(x: Int, y: Int)
44-
fun discarded_cases(thing) =
43+
44+
:ucs desugared
45+
fun with_other_constructors(thing) =
4546
if thing is
46-
[x, y] then x + y
47+
[x, y] then x * y
4748
Point(x, y) then x + y
4849
//│ Desugared:
4950
//│ > if
50-
//│ > thing@58 is []=2 and
51-
//│ > let $first0@65 = thing@58#2.0
52-
//│ > let $first1@64 = thing@58#3.1
53-
//│ > let x@66 = $first0@65#0
54-
//│ > let y@67 = $first1@64#0
55-
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@66#666, y@67#666)
56-
//│ > thing@58 is Point($param0@59, $param1@60) and
57-
//│ > let x@61 = $param0@59#0
58-
//│ > let y@62 = $param1@60#0
59-
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@61#666, y@62#666)
60-
61-
:e
62-
:todo
63-
discarded_cases(Point(0, 0))
64-
//│ = 0
51+
//│ > thing@59 is []=2 and
52+
//│ > let $first0@66 = thing@59#2.0
53+
//│ > let $first1@65 = thing@59#3.1
54+
//│ > let x@67 = $first0@66#0
55+
//│ > let y@68 = $first1@65#0
56+
//│ > else globalThis:import#Prelude#666.*‹member:*›(x@67#666, y@68#666)
57+
//│ > thing@59 is Point($param0@60, $param1@61) and
58+
//│ > let x@62 = $param0@60#0
59+
//│ > let y@63 = $param1@61#0
60+
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@62#666, y@63#666)
61+
62+
:expect 7
63+
with_other_constructors(Point(3, 4))
64+
//│ = 7
65+
66+
:expect 12
67+
with_other_constructors([3, 4])
68+
//│ = 12
6569

6670
// A workaround is to move the tuple pattern to the last case.
6771
:ucs desugared
68-
fun working_cases(thing) =
72+
fun with_other_constructors(thing) =
6973
if thing is
7074
Point(x, y) then x + y
71-
[x, y] then x + y
75+
[x, y] then x * y
7276
//│ Desugared:
7377
//│ > if
74-
//│ > thing@79 is Point($param0@85, $param1@86) and
75-
//│ > let x@87 = $param0@85#0
76-
//│ > let y@88 = $param1@86#0
77-
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@87#666, y@88#666)
78-
//│ > thing@79 is []=2 and
79-
//│ > let $first0@81 = thing@79#1.0
80-
//│ > let $first1@80 = thing@79#2.1
81-
//│ > let x@82 = $first0@81#0
82-
//│ > let y@83 = $first1@80#0
83-
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@82#666, y@83#666)
84-
85-
working_cases(Point(0, 0))
86-
//│ = 0
87-
88-
// However, the `Object` type forbids tuples to be used.
89-
:todo
90-
working_cases([0, 0])
91-
//│ = 0
92-
93-
94-
fun not_working(x) =
78+
//│ > thing@83 is Point($param0@89, $param1@90) and
79+
//│ > let x@91 = $param0@89#0
80+
//│ > let y@92 = $param1@90#0
81+
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@91#666, y@92#666)
82+
//│ > thing@83 is []=2 and
83+
//│ > let $first0@85 = thing@83#1.0
84+
//│ > let $first1@84 = thing@83#2.1
85+
//│ > let x@86 = $first0@85#0
86+
//│ > let y@87 = $first1@84#0
87+
//│ > else globalThis:import#Prelude#666.*‹member:*›(x@86#666, y@87#666)
88+
89+
:expect 7
90+
with_other_constructors(Point(3, 4))
91+
//│ = 7
92+
93+
:expect 12
94+
with_other_constructors([3, 4])
95+
//│ = 12
96+
97+
98+
fun with_else(x) =
9599
if x is
96100
[a, b, c] then
97101
a + b + c
98102
else
99103
0
100104

101-
not_working([1, 2, 3])
105+
with_else([1, 2, 3])
102106
//│ = 6
103107

104-
not_working([1, 2])
108+
with_else([1, 2])
105109
//│ = 0
106110

107111
:ucs desugared
108-
fun multiple_checks(xs) =
112+
fun match_against_different_length(xs) =
109113
if xs is
110114
[] then 0
111115
[x] then x + 1
112116
[x, y] then x + y + 2
113117
[x, y, z] then x + y + z + 3
114118
//│ Desugared:
115119
//│ > if
116-
//│ > xs@123 is []=0 then 0
117-
//│ > xs@123 is []=1 and
118-
//│ > let $first0@126 = xs@123#8.0
119-
//│ > let x@137 = $first0@126#2
120-
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@137#666, 1)
121-
//│ > xs@123 is []=2 and
122-
//│ > let $first0@126 = xs@123#5.0
123-
//│ > let $first1@125 = xs@123#6.1
124-
//│ > let x@133 = $first0@126#1
125-
//│ > let y@134 = $first1@125#1
126-
//│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@133#666, y@134#666), 2)
127-
//│ > xs@123 is []=3 and
128-
//│ > let $first0@126 = xs@123#1.0
129-
//│ > let $first1@125 = xs@123#2.1
130-
//│ > let $first2@124 = xs@123#3.2
131-
//│ > let x@127 = $first0@126#0
132-
//│ > let y@128 = $first1@125#0
133-
//│ > let z@129 = $first2@124#0
134-
//│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@127#666, y@128#666), z@129#666), 3)
120+
//│ > xs@127 is []=0 then 0
121+
//│ > xs@127 is []=1 and
122+
//│ > let $first0@130 = xs@127#8.0
123+
//│ > let x@141 = $first0@130#2
124+
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@141#666, 1)
125+
//│ > xs@127 is []=2 and
126+
//│ > let $first0@130 = xs@127#5.0
127+
//│ > let $first1@129 = xs@127#6.1
128+
//│ > let x@137 = $first0@130#1
129+
//│ > let y@138 = $first1@129#1
130+
//│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@137#666, y@138#666), 2)
131+
//│ > xs@127 is []=3 and
132+
//│ > let $first0@130 = xs@127#1.0
133+
//│ > let $first1@129 = xs@127#2.1
134+
//│ > let $first2@128 = xs@127#3.2
135+
//│ > let x@131 = $first0@130#0
136+
//│ > let y@132 = $first1@129#0
137+
//│ > let z@133 = $first2@128#0
138+
//│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@131#666, y@132#666), z@133#666), 3)
135139

136140
:expect 0
137-
multiple_checks([])
141+
match_against_different_length([])
138142
//│ = 0
139143

140144
:expect 18
141-
multiple_checks([17])
145+
match_against_different_length([17])
142146
//│ = 18
143147

144148
:expect 22
145-
multiple_checks([9, 11])
149+
match_against_different_length([9, 11])
146150
//│ = 22
147151

148152
:expect 42
149-
multiple_checks([13, 13, 13])
153+
match_against_different_length([13, 13, 13])
150154
//│ = 42
151155

152156
:import ../Prelude/Option.mls
153157
//│ Imported 3 member(s)
154158

155159
:ucs desugared normalized
156160
:todo // Sub-scrutinees are not memorized thus the normalization is not correct.
157-
fun multiple_checks(xs) =
161+
fun with_the_common_prefix(xs) =
158162
if xs is
159163
[Some(x)] then x + 1
160164
[None] then 0
161165
//│ Desugared:
162166
//│ > if
163-
//│ > xs@165 is []=1 and
164-
//│ > let $first0@166 = xs@165#3.0
165-
//│ > $first0@166 is Some($param0@167) and
166-
//│ > let x@168 = $param0@167#0
167-
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@168#666, 1)
168-
//│ > xs@165 is []=1 and
169-
//│ > let $first0@166 = xs@165#1.0
170-
//│ > $first0@166 is None then 0
167+
//│ > xs@169 is []=1 and
168+
//│ > let $first0@170 = xs@169#3.0
169+
//│ > $first0@170 is Some($param0@171) and
170+
//│ > let x@172 = $param0@171#0
171+
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@172#666, 1)
172+
//│ > xs@169 is []=1 and
173+
//│ > let $first0@170 = xs@169#1.0
174+
//│ > $first0@170 is None then 0
171175
//│ Normalized:
172-
//│ > if xs@165 is []=1 and
173-
//│ > let $first0@166 = xs@165#3.0
174-
//│ > $first0@166 is Some($param0@167) and
175-
//│ > let x@168 = $param0@167#0
176-
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@168#666, 1)
177-
//│ > $first0@166 is None then 0
176+
//│ > if xs@169 is []=1 and
177+
//│ > let $first0@170 = xs@169#3.0
178+
//│ > $first0@170 is Some($param0@171) and
179+
//│ > let x@172 = $param0@171#0
180+
//│ > else globalThis:import#Prelude#666.+‹member:+›(x@172#666, 1)
181+
//│ > $first0@170 is None then 0

0 commit comments

Comments
 (0)