Skip to content

Commit 3f4cd5f

Browse files
committed
Lua: support exception key in assertions => add expr_to_i_trailing test
Test will fail, because `tonumber()` (used under the hood) will return `nil` in case of non-number
1 parent deec84c commit 3f4cd5f

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

spec/lua/test_expr_to_i_trailing.lua

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/LuaSG.scala

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@ class LuaSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(s
3434
}
3535

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

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

65+
override def testException(actual: Ast.expr, exception: KSError): Unit = {
66+
out.puts(s"""luaunit.assertErrorMsgMatches(".+: ${toPattern(exception)}", function()""")
67+
out.inc
68+
out.puts(s"local _ = ${translateAct(actual)}")
69+
out.dec
70+
out.puts("end)")
71+
}
72+
7173
def translateAct(x: Ast.expr) =
7274
translator.translate(x).replace("self." + Main.INIT_OBJ_NAME, "r")
7375

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

79+
def toPattern(exception: KSError): String = {
80+
exception match {
81+
case _: ValidationNotEqualError => "not equal, expected .*, but got .*"
82+
case UndecidedEndiannessError => "unable to decide endianness"
83+
case EndOfStreamError => "requested %d+ bytes, but only %d+ bytes available"
84+
case _ => LuaCompiler.ksErrorName(exception)
85+
}
86+
}
87+
7788
override def results: String =
7889
"-- " + AUTOGEN_COMMENT + "\n\n" + super.results
7990
}

0 commit comments

Comments
 (0)