Skip to content

Commit 9f0ccc9

Browse files
committed
Add :expect command and improve a test with it
1 parent 99524ba commit 9f0ccc9

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
1313
val sjs = NullaryCommand("sjs")
1414
val showRepl = NullaryCommand("showRepl")
1515
val silent = NullaryCommand("silent")
16+
val expect = Command("expect"): ln =>
17+
ln.trim
1618

1719
private val baseScp: codegen.js.Scope =
1820
codegen.js.Scope.empty
@@ -61,6 +63,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
6163
output(s"JS:")
6264
output(jsStr)
6365
def mkQuery(prefix: Str, jsStr: Str) =
66+
import hkmc2.Message.MessageContext
6467
val queryStr = jsStr.replaceAll("\n", " ")
6568
val (reply, stderr) = host.query(queryStr, expectRuntimeErrors.isUnset && fixme.isUnset && todo.isUnset)
6669
reply match
@@ -73,11 +76,15 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
7376
output(s"> ${line}")
7477
content match
7578
case "undefined" =>
76-
case _ => output(s"$prefix= ${content}")
79+
case _ =>
80+
expect.get match
81+
case S(expected) if content != expected => raise:
82+
ErrorReport(msg"Expected: ${expected}, got: ${content}" -> N :: Nil,
83+
source = Diagnostic.Source.Runtime)
84+
case _ => output(s"$prefix= ${content}")
7785
case ReplHost.Empty =>
7886
case ReplHost.Unexecuted(message) => ???
7987
case ReplHost.Error(isSyntaxError, message) =>
80-
import hkmc2.Message.MessageContext
8188
if (isSyntaxError) then
8289
// If there is a syntax error in the generated code,
8390
// it should be a code generation error.

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Some[T](value: T) extends Option[T]
1010
module None extends Option[nothing]
1111
class Pair[A, B](x: A, y: B)
1212

13+
// All `add_n` functions should be inferred to have the same type.
1314

1415
fun add_1(x, y) =
1516
if
@@ -95,25 +96,25 @@ fun add_4(x, y) =
9596
:fixme
9697
add_4(None, None)
9798
//│ ╔══[ERROR] Name not found: add_4
98-
//│ ║ l.96: add_4(None, None)
99+
//│ ║ l.97: add_4(None, None)
99100
//│ ╙── ^^^^^
100101

101102
:fixme
102103
add_4(Some(5), None)
103104
//│ ╔══[ERROR] Name not found: add_4
104-
//│ ║ l.102: add_4(Some(5), None)
105+
//│ ║ l.103: add_4(Some(5), None)
105106
//│ ╙── ^^^^^
106107

107108
:fixme
108109
add_4(None, Some(9))
109110
//│ ╔══[ERROR] Name not found: add_4
110-
//│ ║ l.108: add_4(None, Some(9))
111+
//│ ║ l.109: add_4(None, Some(9))
111112
//│ ╙── ^^^^^
112113

113114
:fixme
114115
add_4(Some(5), Some(9))
115116
//│ ╔══[ERROR] Name not found: add_4
116-
//│ ║ l.114: add_4(Some(5), Some(9))
117+
//│ ║ l.115: add_4(Some(5), Some(9))
117118
//│ ╙── ^^^^^
118119

119120

@@ -158,6 +159,7 @@ add_6(Some(5), Some(9))
158159
//│ = 14
159160

160161

162+
// Functions from now on have a predicate `p` that can be used to add some preconditions.
161163

162164

163165
fun add_6(p, x, y) =
@@ -169,15 +171,23 @@ fun add_6(p, x, y) =
169171
y is None and x is None then 0
170172

171173

174+
:expect 0
172175
add_6((x) => true, None, None)
173176
//│ = 0
174177

178+
:expect 42
175179
add_6((x) => true, Some(5), None)
176180
//│ = 42
177181

182+
:expect 5
183+
add_6((x) => false, Some(5), None)
184+
//│ = 5
185+
186+
:expect 9
178187
add_6((x) => true, None, Some(9))
179188
//│ = 9
180189

190+
:expect 14
181191
add_6((x) => true, Some(5), Some(9))
182192
//│ = 14
183193

@@ -192,18 +202,23 @@ fun add_7(p, x, y) =
192202
y is None and x is None then 0
193203

194204

205+
:expect 0
195206
add_7((x) => x > 0, None, None)
196207
//│ = 0
197208

209+
:expect 5
198210
add_7((x) => x > 0, Some(5), None)
199211
//│ = 5
200212

213+
:expect 36
201214
add_7((x) => x > 0, None, Some(9))
202215
//│ = 36
203216

217+
:expect -9
204218
add_7((x) => x > 0, None, Some(-9))
205219
//│ = -9
206220

221+
:expect 14
207222
add_7((x) => x > 0, Some(5), Some(9))
208223
//│ = 14
209224

@@ -218,21 +233,27 @@ fun add_8(p, x, y) =
218233
y is None and x is None then 0
219234

220235

236+
:expect 0
221237
add_8((x) => x > 0, None, None)
222238
//│ = 0
223239

240+
:expect 42
224241
add_8((x) => true, Some(9), None)
225242
//│ = 42
226243

244+
:expect 5
227245
add_8((x) => x > 0, Some(5), None)
228246
//│ = 5
229247

248+
:expect 36
230249
add_8((x) => x > 0, None, Some(9))
231250
//│ = 36
232251

252+
:expect -9
233253
add_8((x) => x > 0, None, Some(-9))
234254
//│ = -9
235255

256+
:expect 14
236257
add_8((x) => x > 0, Some(5), Some(9))
237258
//│ = 14
238259

0 commit comments

Comments
 (0)