Skip to content

Commit

Permalink
Add :expect command and improve a test with it
Browse files Browse the repository at this point in the history
  • Loading branch information
chengluyu committed Nov 10, 2024
1 parent dbc9322 commit eaf60b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
11 changes: 9 additions & 2 deletions hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
val sjs = NullaryCommand("sjs")
val showRepl = NullaryCommand("showRepl")
val silent = NullaryCommand("silent")
val expect = Command("expect"): ln =>
ln.trim

private val baseScp: codegen.js.Scope =
codegen.js.Scope.empty
Expand Down Expand Up @@ -61,6 +63,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
output(s"JS:")
output(jsStr)
def mkQuery(prefix: Str, jsStr: Str) =
import hkmc2.Message.MessageContext
val queryStr = jsStr.replaceAll("\n", " ")
val (reply, stderr) = host.query(queryStr, expectRuntimeErrors.isUnset && fixme.isUnset && todo.isUnset)
reply match
Expand All @@ -73,11 +76,15 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
output(s"> ${line}")
content match
case "undefined" =>
case _ => output(s"$prefix= ${content}")
case _ =>
expect.get match
case S(expected) if content != expected => raise:
ErrorReport(msg"Expected: ${expected}, got: ${content}" -> N :: Nil,
source = Diagnostic.Source.Runtime)
case _ => output(s"$prefix= ${content}")
case ReplHost.Empty =>
case ReplHost.Unexecuted(message) => ???
case ReplHost.Error(isSyntaxError, message) =>
import hkmc2.Message.MessageContext
if (isSyntaxError) then
// If there is a syntax error in the generated code,
// it should be a code generation error.
Expand Down
29 changes: 25 additions & 4 deletions hkmc2/shared/src/test/mlscript/ucs/general/DualOptions.mls
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Some[T](value: T) extends Option[T]
module None extends Option[nothing]
class Pair[A, B](x: A, y: B)

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

fun add_1(x, y) =
if
Expand Down Expand Up @@ -95,25 +96,25 @@ fun add_4(x, y) =
:fixme
add_4(None, None)
//│ ╔══[ERROR] Name not found: add_4
//│ ║ l.96: add_4(None, None)
//│ ║ l.97: add_4(None, None)
//│ ╙── ^^^^^

:fixme
add_4(Some(5), None)
//│ ╔══[ERROR] Name not found: add_4
//│ ║ l.102: add_4(Some(5), None)
//│ ║ l.103: add_4(Some(5), None)
//│ ╙── ^^^^^

:fixme
add_4(None, Some(9))
//│ ╔══[ERROR] Name not found: add_4
//│ ║ l.108: add_4(None, Some(9))
//│ ║ l.109: add_4(None, Some(9))
//│ ╙── ^^^^^

:fixme
add_4(Some(5), Some(9))
//│ ╔══[ERROR] Name not found: add_4
//│ ║ l.114: add_4(Some(5), Some(9))
//│ ║ l.115: add_4(Some(5), Some(9))
//│ ╙── ^^^^^


Expand Down Expand Up @@ -158,6 +159,7 @@ add_6(Some(5), Some(9))
//│ = 14


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


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


:expect 0
add_6((x) => true, None, None)
//│ = 0

:expect 42
add_6((x) => true, Some(5), None)
//│ = 42

:expect 5
add_6((x) => false, Some(5), None)
//│ = 5

:expect 9
add_6((x) => true, None, Some(9))
//│ = 9

:expect 14
add_6((x) => true, Some(5), Some(9))
//│ = 14

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


:expect 0
add_7((x) => x > 0, None, None)
//│ = 0

:expect 5
add_7((x) => x > 0, Some(5), None)
//│ = 5

:expect 36
add_7((x) => x > 0, None, Some(9))
//│ = 36

:expect -9
add_7((x) => x > 0, None, Some(-9))
//│ = -9

:expect 14
add_7((x) => x > 0, Some(5), Some(9))
//│ = 14

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


:expect 0
add_8((x) => x > 0, None, None)
//│ = 0

:expect 42
add_8((x) => true, Some(9), None)
//│ = 42

:expect 5
add_8((x) => x > 0, Some(5), None)
//│ = 5

:expect 36
add_8((x) => x > 0, None, Some(9))
//│ = 36

:expect -9
add_8((x) => x > 0, None, Some(-9))
//│ = -9

:expect 14
add_8((x) => x > 0, Some(5), Some(9))
//│ = 14

0 comments on commit eaf60b3

Please sign in to comment.