Skip to content

Commit

Permalink
Lua: support exception key in assertions => add `expr_to_i_trailing…
Browse files Browse the repository at this point in the history
…` test

Test will fail, because `tonumber()` (used under the hood) will return `nil` in case of non-number
  • Loading branch information
Mingun committed Jul 16, 2024
1 parent deec84c commit 3f4cd5f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
19 changes: 19 additions & 0 deletions spec/lua/test_expr_to_i_trailing.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ class LuaSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(s
}

override def runParseExpectError(exception: KSError): Unit = {
val msg = exception match {
case _: ValidationNotEqualError => "not equal, expected .*, but got .*"
case UndecidedEndiannessError => "unable to decide endianness"
case EndOfStreamError => "requested %d+ bytes, but only %d+ bytes available"
case _ => LuaCompiler.ksErrorName(exception)
}
out.puts(s"""luaunit.assertErrorMsgMatches(".+: $msg", $className.from_file, $className, "src/${spec.data}")""")
out.puts(s"""luaunit.assertErrorMsgMatches(".+: ${toPattern(exception)}", $className.from_file, $className, "src/${spec.data}")""")
}

override def footer(): Unit = {
Expand Down Expand Up @@ -68,12 +62,29 @@ class LuaSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(s
override def trueArrayEquality(check: TestEquals, elType: DataType, elts: Seq[Ast.expr]): Unit =
simpleEquality(check)

override def testException(actual: Ast.expr, exception: KSError): Unit = {
out.puts(s"""luaunit.assertErrorMsgMatches(".+: ${toPattern(exception)}", function()""")
out.inc
out.puts(s"local _ = ${translateAct(actual)}")
out.dec
out.puts("end)")
}

def translateAct(x: Ast.expr) =
translator.translate(x).replace("self." + Main.INIT_OBJ_NAME, "r")

def translateExp(x: Ast.expr) =
translator.translate(x).replace("self._root", className)

def toPattern(exception: KSError): String = {
exception match {
case _: ValidationNotEqualError => "not equal, expected .*, but got .*"
case UndecidedEndiannessError => "unable to decide endianness"
case EndOfStreamError => "requested %d+ bytes, but only %d+ bytes available"
case _ => LuaCompiler.ksErrorName(exception)
}
}

override def results: String =
"-- " + AUTOGEN_COMMENT + "\n\n" + super.results
}

0 comments on commit 3f4cd5f

Please sign in to comment.