From 3f4cd5f4e3aef36a189cd4dd4f083349879a9ef8 Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 11 Apr 2024 21:49:33 +0500 Subject: [PATCH] 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 --- spec/lua/test_expr_to_i_trailing.lua | 19 ++++++++++++++ .../testtranslator/specgenerators/LuaSG.scala | 25 +++++++++++++------ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 spec/lua/test_expr_to_i_trailing.lua diff --git a/spec/lua/test_expr_to_i_trailing.lua b/spec/lua/test_expr_to_i_trailing.lua new file mode 100644 index 000000000..0f954e06e --- /dev/null +++ b/spec/lua/test_expr_to_i_trailing.lua @@ -0,0 +1,19 @@ +-- Autogenerated from KST: please remove this line if doing any edits by hand! + +local luaunit = require("luaunit") + +require("expr_to_i_trailing") + +TestExprToITrailing = {} + +function TestExprToITrailing:test_expr_to_i_trailing() + local r = ExprToITrailing:from_file("src/term_strz.bin") + + luaunit.assertErrorMsgMatches(".+: ConversionError", function() + local _ = r.to_i_r10 + end) + luaunit.assertEquals(r.to_i_r16, 152517308) + luaunit.assertErrorMsgMatches(".+: ConversionError", function() + local _ = r.to_i_garbage + end) +end diff --git a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/LuaSG.scala b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/LuaSG.scala index 6e26760e4..d95340548 100644 --- a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/LuaSG.scala +++ b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/LuaSG.scala @@ -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 = { @@ -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 }