Skip to content

Commit

Permalink
KST: autogenerate tests with debug reads
Browse files Browse the repository at this point in the history
Add new parameter `debug` to a test to generate parsers and tests in debug mode
  • Loading branch information
Mingun committed Jul 16, 2024
1 parent deec84c commit 00fc3c8
Show file tree
Hide file tree
Showing 28 changed files with 92 additions and 32 deletions.
1 change: 1 addition & 0 deletions spec/construct/test_debug_switch_user.py

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

4 changes: 1 addition & 3 deletions spec/cpp_stl_11/test_debug_switch_user.cpp

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

4 changes: 1 addition & 3 deletions spec/cpp_stl_98/test_debug_switch_user.cpp

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

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

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

2 changes: 1 addition & 1 deletion spec/javascript/test_debug_switch_user.js

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

1 change: 1 addition & 0 deletions spec/ks/debug_switch_user.kst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
id: debug_switch_user
data: nav_parent_switch.bin
debug: true
asserts:
- actual: code
expected: 1
Expand Down
2 changes: 1 addition & 1 deletion spec/lua/test_debug_switch_user.lua

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

1 change: 1 addition & 0 deletions spec/nim/tdebug_switch_user.nim

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

1 change: 1 addition & 0 deletions spec/perl/TestDebugSwitchUser.t

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

2 changes: 1 addition & 1 deletion spec/php/DebugSwitchUserTest.php

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

3 changes: 1 addition & 2 deletions spec/python/test_debug_switch_user.py

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

2 changes: 1 addition & 1 deletion spec/ruby/debug_switch_user_spec.rb

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

1 change: 1 addition & 0 deletions spec/rust/test_debug_switch_user.rs

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 @@ -14,6 +14,7 @@ case class TestException(actual: Ast.expr, exception: KSError) extends TestAsser
case class TestSpec(
id: String,
data: String,
debug: Boolean,
asserts: List[TestAssert],
exception: Option[KSError],
extraImports: List[String]
Expand Down Expand Up @@ -48,11 +49,12 @@ object TestSpec {

val id = ParseUtils.getValueStr(srcMap, "id", List())
val data = ParseUtils.getValueStr(srcMap, "data", List())
val debug = ParseUtils.getOptValueBool(srcMap, "debug", List()).getOrElse(false)
val asserts = ParseUtils.getList[TestAssert](srcMap, "asserts", testAssertFromYaml, List())
val exception = ParseUtils.getOptValueStr(srcMap, "exception", List()).map(KSError.fromName)
val extraImports = ParseUtils.getListStr(srcMap, "imports", List())

TestSpec(id, data, asserts, exception, extraImports)
TestSpec(id, data, debug, asserts, exception, extraImports)
}

def fromFile(fileName: String): TestSpec = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestTranslator(options: CLIOptions) {
Console.println(s"Translating: $testName")

val testSpec = loadTestSpec(testName)
val classSpecs = loadClassSpecs(testName)
val classSpecs = loadClassSpecs(testName, testSpec.debug)
val initObj = classSpecs(INIT_OBJ_TYPE)
val provider = new ClassTypeProvider(classSpecs, initObj)

Expand Down Expand Up @@ -57,7 +57,7 @@ class TestTranslator(options: CLIOptions) {
def loadTestSpec(testName: String): TestSpec =
TestSpec.fromFile(s"$specKsDir/$testName.kst")

def loadClassSpecs(testName: String): ClassSpecs = {
def loadClassSpecs(testName: String, debug: Boolean): ClassSpecs = {
val cliConfig = CLIConfig(importPaths = importDirs)
val (origSpecsOpt, errors) = JavaKSYParser.localFileToSpecs(s"$formatsDir/$testName.ksy", cliConfig)

Expand All @@ -82,7 +82,7 @@ class TestTranslator(options: CLIOptions) {
endian = None,
bitEndian = None,
encoding = None,
forceDebug = false,
forceDebug = debug,
opaqueTypes = None,
zeroCopySubstream = None,
imports = List()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class CSharpSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerato

override def runParse(): Unit = {
out.puts(s"var r = $className.FromFile(SourceFile(" + "\"" + spec.data + "\"));")
if (spec.debug) {
out.puts("r._read();")
}
}

override def runParseExpectError(exception: KSError): Unit = {
Expand All @@ -40,7 +43,7 @@ class CSharpSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerato
out.puts("delegate")
out.puts("{")
out.inc
out.puts(s"""$className.FromFile(SourceFile("${spec.data}"));""")
runParse()
out.dec
out.puts("}")
out.dec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ class ConstructSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGener
out.inc
}

override def runParse(): Unit =
override def runParse(): Unit = {
out.puts(s"r = _schema.parse_file('src/${spec.data}')")
if (spec.debug) {
out.puts("r._read()")
}
}

override def footer(): Unit = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,32 @@ class CppStlSG(spec: TestSpec, provider: ClassTypeProvider, cppConfig: CppRuntim
override def runParse(): Unit = {
runParseCommon1()
out.puts(s"$className* r = new $className(&ks);")
if (spec.debug) {
out.puts("r->_read();")
}
}

override def runParseExpectError(exception: KSError): Unit = {
cppImportList.addKaitai("kaitai/exceptions.h")

runParseCommon1()
out.puts(s"$className* r = ${compiler.nullPtr};")
out.puts("BOOST_CHECK_THROW(")
out.inc
out.puts(s"r = new $className(&ks),")
out.puts(compiler.ksErrorName(exception))
out.dec
out.puts(");")
if (spec.debug) {
out.puts(s"$className* r = new $className(&ks);")
out.puts("BOOST_CHECK_THROW(")
out.inc
out.puts("r->_read(),")
out.puts(compiler.ksErrorName(exception))
out.dec
out.puts(");")
} else {
out.puts(s"$className* r = ${compiler.nullPtr};")
out.puts("BOOST_CHECK_THROW(")
out.inc
out.puts(s"r = new $className(&ks),")
out.puts(compiler.ksErrorName(exception))
out.dec
out.puts(");")
}
}

def runParseCommon1(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class JavaSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(
val needsFinal = spec.asserts.exists(assert => assert.isInstanceOf[TestException])
val finalKeyword = if (needsFinal) "final " else ""
out.puts(s"${finalKeyword}$className r = $className.fromFile(SRC_DIR + " + "\"" + spec.data + "\");")
if (spec.debug) {
out.puts("r._read();")
}
}

override def footer(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ class JavaScriptSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGene
val entryClass = JavaScriptCompiler.type2class(entry)
out.puts(s"var $entryClass = require('$entryClass').$entryClass;")
}
if (spec.debug) {
out.puts("r._read();")
}
}

override def runParseExpectError(exception: KSError): Unit = {
importList.add("testHelperThrows")
importList.add("KaitaiStream")

out.puts(s"testHelperThrows('$className', 'src/${spec.data}', ${JavaScriptCompiler.ksErrorName(exception)});")
if (spec.debug) {
out.puts("r._read();")
}
}

override def footer(): Unit = if (spec.exception.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class LuaSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(s

override def runParse(): Unit = {
out.puts(s"""local r = $className:from_file("src/${spec.data}")""")
if (spec.debug) {
out.puts("r:_read()")
}
}

override def runParseExpectError(exception: KSError): Unit = {
Expand All @@ -40,7 +43,12 @@ class LuaSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(s
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}")""")
if (spec.debug) {
out.puts(s"""local r = $className:from_file("src/${spec.data}")""")
out.puts(s"""luaunit.assertErrorMsgMatches(".+: $msg", r.read, r")""")
} else {
out.puts(s"""luaunit.assertErrorMsgMatches(".+: $msg", $className.from_file, $className, "src/${spec.data}")""")
}
}

override def footer(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class NimSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(s
// Members declared in io.kaitai.struct.testtranslator.specgenerators.BaseGenerator
override def fileName(name: String): String = s"t${spec.id}.nim"
override def header(): Unit = {
out.puts(s"let r = ${className}.fromFile" + "(\"../../src/" + spec.data + "\")")
out.puts(s"let r = ${className}.fromFile(\"../../src/${spec.data}\")")
if (spec.debug) {
out.puts("r._read()")
}
}
override def footer(): Unit = { }
override def nullAssert(actual: expr): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class PHPSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(s

override def runParse(): Unit = {
out.puts(s"$$r = $className::fromFile(self::SRC_DIR_PATH . '/${spec.data}');")
if (spec.debug) {
out.puts("$r->_read();")
}
}

override def runParseExpectError(exception: KSError): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ class PerlSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(

override def runParse(): Unit = {
out.puts(s"my $$r = $className->from_file('src/${spec.data}');")
if (spec.debug) {
out.puts("$r->_read();")
}
}

override def runParseExpectError(exception: KSError): Unit = {
val msg = exception match {
case UndecidedEndiannessError => "Unable to decide on endianness"
case EndOfStreamError => "Requested \\d+ bytes, but only \\d+ bytes available"
}
out.puts(s"""throws_ok { $className->from_file('src/${spec.data}') } '/^$msg/';""")
out.puts("throws_ok {")
out.inc
runParse()
out.dec
out.puts(s"""} '/^$msg/';""")
}

override def footer(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class PythonSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerato
override def runParse(): Unit = {
out.puts(s"with $className.from_file('src/${spec.data}') as r:")
out.inc
if (spec.debug) {
out.puts("r._read()")
}
}

override def runParseExpectError(exception: KSError): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class RubySG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(

override def runParse(): Unit = {
out.puts(s"r = $className.from_file('src/${spec.data}')")
if (spec.debug) {
out.puts("r._read")
}
}

override def runParseExpectError(exception: KSError): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RustSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(
out.puts("extern crate kaitai_struct;")
out.puts(s"extern crate rust;")
out.puts

out.puts("use kaitai_struct::KaitaiStruct;")
out.puts(s"use rust::$className;")
out.puts
Expand All @@ -29,6 +29,9 @@ class RustSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(

out.puts("if let Ok(r) = " + className + "::from_file(\"src/" + spec.data + "\") {")
out.inc
if (spec.debug) {
out.puts("r.read().expect(\"cannot read test file\");")
}
}

override def footer(): Unit = {
Expand Down

0 comments on commit 00fc3c8

Please sign in to comment.