Skip to content

Commit

Permalink
Allow assertions to take non-string error messages (#170)
Browse files Browse the repository at this point in the history
These are meant to get materialized, similar to `error` which we already
handle correctly.

Added a unit test.

Fixes #146
  • Loading branch information
lihaoyi-databricks authored Jun 1, 2023
1 parent 7504e1a commit b1582e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 7 additions & 8 deletions sjsonnet/src/sjsonnet/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,12 @@ class Evaluator(resolver: CachedResolver,
}

def visitError(e: Expr.Error)(implicit scope: ValScope): Nothing = {
Error.fail(
visitExpr(e.value) match {
case Val.Str(_, s) => s
case r => Materializer.stringify(r)
},
e.pos
)
Error.fail(materializeError(visitExpr(e.value)), e.pos)
}

private def materializeError(value: Val) = value match {
case Val.Str(_, s) => s
case r => Materializer.stringify(r)
}

def visitUnaryOp(e: UnaryOp)(implicit scope: ValScope): Val = {
Expand Down Expand Up @@ -242,7 +241,7 @@ class Evaluator(resolver: CachedResolver,
e.asserted.msg match {
case null => Error.fail("Assertion failed", e)
case msg =>
Error.fail("Assertion failed: " + visitExpr(msg).cast[Val.Str].value, e)
Error.fail("Assertion failed: " + materializeError(visitExpr(msg)), e)
}
}
visitExpr(e.returned)
Expand Down
6 changes: 6 additions & 0 deletions sjsonnet/test/src/sjsonnet/EvaluatorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,11 @@ object EvaluatorTests extends TestSuite{
eval("""local f(x)=null; f == null""") ==> ujson.False
eval("""local f=null; f == null""") ==> ujson.True
}


test("errorNonString") {
assert(evalErr("""error {a: "b"}""").contains("""{"a": "b"}"""))
assert(evalErr("""assert 1 == 2 : { a: "b"}; 1""").contains("""{"a": "b"}"""))
}
}
}

0 comments on commit b1582e2

Please sign in to comment.