Skip to content

Commit

Permalink
Construct: support exception key top-level and in assertions
Browse files Browse the repository at this point in the history
List of tests that depends on
`asserts[i].exception` key:
  expr_to_i_trailing

`exception` key:
  default_endian_expr_exception
  eof_exception_bytes
  eof_exception_sized
  eof_exception_u4
  eos_exception_bytes
  eos_exception_sized
  eos_exception_u4
  valid_fail_anyof_int
  valid_fail_contents
  valid_fail_eq_bytes
  valid_fail_eq_int
  valid_fail_eq_str
  valid_fail_expr
  valid_fail_inst
  valid_fail_max_int
  valid_fail_min_int
  valid_fail_range_bytes
  valid_fail_range_float
  valid_fail_range_int
  valid_fail_range_str
  • Loading branch information
Mingun committed Jul 16, 2024
1 parent deec84c commit ba05e7a
Show file tree
Hide file tree
Showing 22 changed files with 253 additions and 1 deletion.
11 changes: 11 additions & 0 deletions spec/construct/test_default_endian_expr_exception.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_eof_exception_bytes.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_eof_exception_sized.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_eof_exception_u4.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_eos_exception_bytes.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_eos_exception_sized.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_eos_exception_u4.py

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

16 changes: 16 additions & 0 deletions spec/construct/test_expr_to_i_trailing.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_anyof_int.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_contents.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_eq_bytes.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_eq_int.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_eq_str.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_expr.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_inst.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_max_int.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_min_int.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_range_bytes.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_range_float.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_range_int.py

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

11 changes: 11 additions & 0 deletions spec/construct/test_valid_fail_range_str.py

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
@@ -1,8 +1,9 @@
package io.kaitai.struct.testtranslator.specgenerators

import _root_.io.kaitai.struct.{ClassTypeProvider, Utils}
import _root_.io.kaitai.struct.datatype.DataType
import _root_.io.kaitai.struct.datatype.{DataType, KSError}
import _root_.io.kaitai.struct.exprlang.Ast
import _root_.io.kaitai.struct.languages.PythonCompiler
import _root_.io.kaitai.struct.testtranslator.{Main, TestAssert, TestEquals, TestSpec}
import _root_.io.kaitai.struct.translators.ConstructTranslator

Expand All @@ -28,6 +29,13 @@ class ConstructSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGener
override def runParse(): Unit =
out.puts(s"r = _schema.parse_file('src/${spec.data}')")

override def runParseExpectError(exception: KSError): Unit = {
importList.add("import kaitaistruct")
out.puts(s"with self.assertRaises(${PythonCompiler.ksErrorName(exception)}):")
out.inc
runParse()
}

override def footer(): Unit = {}

override def simpleEquality(check: TestEquals): Unit = {
Expand All @@ -50,6 +58,14 @@ class ConstructSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGener
override def trueArrayEquality(check: TestEquals, elType: DataType, elts: Seq[Ast.expr]): Unit =
simpleEquality(check)

override def testException(actual: Ast.expr, exception: KSError): Unit = {
importList.add("import kaitaistruct")
out.puts(s"with self.assertRaises(${PythonCompiler.ksErrorName(exception)}):")
out.inc
out.puts(translateAct(actual))
out.dec
}

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

Expand Down

0 comments on commit ba05e7a

Please sign in to comment.