From ba05e7a644fc8d2614c9616974f9ec5af737fbdc Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 11 Apr 2024 22:31:50 +0500 Subject: [PATCH] Construct: support `exception` key top-level and in assertions 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 --- .../test_default_endian_expr_exception.py | 11 +++++++++++ spec/construct/test_eof_exception_bytes.py | 11 +++++++++++ spec/construct/test_eof_exception_sized.py | 11 +++++++++++ spec/construct/test_eof_exception_u4.py | 11 +++++++++++ spec/construct/test_eos_exception_bytes.py | 11 +++++++++++ spec/construct/test_eos_exception_sized.py | 11 +++++++++++ spec/construct/test_eos_exception_u4.py | 11 +++++++++++ spec/construct/test_expr_to_i_trailing.py | 16 ++++++++++++++++ spec/construct/test_valid_fail_anyof_int.py | 11 +++++++++++ spec/construct/test_valid_fail_contents.py | 11 +++++++++++ spec/construct/test_valid_fail_eq_bytes.py | 11 +++++++++++ spec/construct/test_valid_fail_eq_int.py | 11 +++++++++++ spec/construct/test_valid_fail_eq_str.py | 11 +++++++++++ spec/construct/test_valid_fail_expr.py | 11 +++++++++++ spec/construct/test_valid_fail_inst.py | 11 +++++++++++ spec/construct/test_valid_fail_max_int.py | 11 +++++++++++ spec/construct/test_valid_fail_min_int.py | 11 +++++++++++ spec/construct/test_valid_fail_range_bytes.py | 11 +++++++++++ spec/construct/test_valid_fail_range_float.py | 11 +++++++++++ spec/construct/test_valid_fail_range_int.py | 11 +++++++++++ spec/construct/test_valid_fail_range_str.py | 11 +++++++++++ .../specgenerators/ConstructSG.scala | 18 +++++++++++++++++- 22 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 spec/construct/test_default_endian_expr_exception.py create mode 100644 spec/construct/test_eof_exception_bytes.py create mode 100644 spec/construct/test_eof_exception_sized.py create mode 100644 spec/construct/test_eof_exception_u4.py create mode 100644 spec/construct/test_eos_exception_bytes.py create mode 100644 spec/construct/test_eos_exception_sized.py create mode 100644 spec/construct/test_eos_exception_u4.py create mode 100644 spec/construct/test_expr_to_i_trailing.py create mode 100644 spec/construct/test_valid_fail_anyof_int.py create mode 100644 spec/construct/test_valid_fail_contents.py create mode 100644 spec/construct/test_valid_fail_eq_bytes.py create mode 100644 spec/construct/test_valid_fail_eq_int.py create mode 100644 spec/construct/test_valid_fail_eq_str.py create mode 100644 spec/construct/test_valid_fail_expr.py create mode 100644 spec/construct/test_valid_fail_inst.py create mode 100644 spec/construct/test_valid_fail_max_int.py create mode 100644 spec/construct/test_valid_fail_min_int.py create mode 100644 spec/construct/test_valid_fail_range_bytes.py create mode 100644 spec/construct/test_valid_fail_range_float.py create mode 100644 spec/construct/test_valid_fail_range_int.py create mode 100644 spec/construct/test_valid_fail_range_str.py diff --git a/spec/construct/test_default_endian_expr_exception.py b/spec/construct/test_default_endian_expr_exception.py new file mode 100644 index 000000000..d76b6838d --- /dev/null +++ b/spec/construct/test_default_endian_expr_exception.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from default_endian_expr_exception import _schema + +class TestDefaultEndianExprException(unittest.TestCase): + def test_default_endian_expr_exception(self): + with self.assertRaises(kaitaistruct.UndecidedEndiannessError): + r = _schema.parse_file('src/endian_expr.bin') diff --git a/spec/construct/test_eof_exception_bytes.py b/spec/construct/test_eof_exception_bytes.py new file mode 100644 index 000000000..460fa2eeb --- /dev/null +++ b/spec/construct/test_eof_exception_bytes.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from eof_exception_bytes import _schema + +class TestEofExceptionBytes(unittest.TestCase): + def test_eof_exception_bytes(self): + with self.assertRaises(EOFError): + r = _schema.parse_file('src/term_strz.bin') diff --git a/spec/construct/test_eof_exception_sized.py b/spec/construct/test_eof_exception_sized.py new file mode 100644 index 000000000..c5aec8e29 --- /dev/null +++ b/spec/construct/test_eof_exception_sized.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from eof_exception_sized import _schema + +class TestEofExceptionSized(unittest.TestCase): + def test_eof_exception_sized(self): + with self.assertRaises(EOFError): + r = _schema.parse_file('src/term_strz.bin') diff --git a/spec/construct/test_eof_exception_u4.py b/spec/construct/test_eof_exception_u4.py new file mode 100644 index 000000000..cdfc15f6c --- /dev/null +++ b/spec/construct/test_eof_exception_u4.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from eof_exception_u4 import _schema + +class TestEofExceptionU4(unittest.TestCase): + def test_eof_exception_u4(self): + with self.assertRaises(EOFError): + r = _schema.parse_file('src/term_strz.bin') diff --git a/spec/construct/test_eos_exception_bytes.py b/spec/construct/test_eos_exception_bytes.py new file mode 100644 index 000000000..1f289b126 --- /dev/null +++ b/spec/construct/test_eos_exception_bytes.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from eos_exception_bytes import _schema + +class TestEosExceptionBytes(unittest.TestCase): + def test_eos_exception_bytes(self): + with self.assertRaises(EOFError): + r = _schema.parse_file('src/term_strz.bin') diff --git a/spec/construct/test_eos_exception_sized.py b/spec/construct/test_eos_exception_sized.py new file mode 100644 index 000000000..13b855a19 --- /dev/null +++ b/spec/construct/test_eos_exception_sized.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from eos_exception_sized import _schema + +class TestEosExceptionSized(unittest.TestCase): + def test_eos_exception_sized(self): + with self.assertRaises(EOFError): + r = _schema.parse_file('src/term_strz.bin') diff --git a/spec/construct/test_eos_exception_u4.py b/spec/construct/test_eos_exception_u4.py new file mode 100644 index 000000000..d5ccf882d --- /dev/null +++ b/spec/construct/test_eos_exception_u4.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from eos_exception_u4 import _schema + +class TestEosExceptionU4(unittest.TestCase): + def test_eos_exception_u4(self): + with self.assertRaises(EOFError): + r = _schema.parse_file('src/term_strz.bin') diff --git a/spec/construct/test_expr_to_i_trailing.py b/spec/construct/test_expr_to_i_trailing.py new file mode 100644 index 000000000..0574eb725 --- /dev/null +++ b/spec/construct/test_expr_to_i_trailing.py @@ -0,0 +1,16 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from expr_to_i_trailing import _schema + +class TestExprToITrailing(unittest.TestCase): + def test_expr_to_i_trailing(self): + r = _schema.parse_file('src/term_strz.bin') + + with self.assertRaises(ValueError): + r.to_i_r10 + self.assertEqual(r.to_i_r16, 152517308) + with self.assertRaises(ValueError): + r.to_i_garbage diff --git a/spec/construct/test_valid_fail_anyof_int.py b/spec/construct/test_valid_fail_anyof_int.py new file mode 100644 index 000000000..15a553d93 --- /dev/null +++ b/spec/construct/test_valid_fail_anyof_int.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_anyof_int import _schema + +class TestValidFailAnyofInt(unittest.TestCase): + def test_valid_fail_anyof_int(self): + with self.assertRaises(kaitaistruct.ValidationNotAnyOfError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_contents.py b/spec/construct/test_valid_fail_contents.py new file mode 100644 index 000000000..87642b2de --- /dev/null +++ b/spec/construct/test_valid_fail_contents.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_contents import _schema + +class TestValidFailContents(unittest.TestCase): + def test_valid_fail_contents(self): + with self.assertRaises(kaitaistruct.ValidationNotEqualError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_eq_bytes.py b/spec/construct/test_valid_fail_eq_bytes.py new file mode 100644 index 000000000..828361236 --- /dev/null +++ b/spec/construct/test_valid_fail_eq_bytes.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_eq_bytes import _schema + +class TestValidFailEqBytes(unittest.TestCase): + def test_valid_fail_eq_bytes(self): + with self.assertRaises(kaitaistruct.ValidationNotEqualError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_eq_int.py b/spec/construct/test_valid_fail_eq_int.py new file mode 100644 index 000000000..c670673ea --- /dev/null +++ b/spec/construct/test_valid_fail_eq_int.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_eq_int import _schema + +class TestValidFailEqInt(unittest.TestCase): + def test_valid_fail_eq_int(self): + with self.assertRaises(kaitaistruct.ValidationNotEqualError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_eq_str.py b/spec/construct/test_valid_fail_eq_str.py new file mode 100644 index 000000000..7ff680eb8 --- /dev/null +++ b/spec/construct/test_valid_fail_eq_str.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_eq_str import _schema + +class TestValidFailEqStr(unittest.TestCase): + def test_valid_fail_eq_str(self): + with self.assertRaises(kaitaistruct.ValidationNotEqualError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_expr.py b/spec/construct/test_valid_fail_expr.py new file mode 100644 index 000000000..d0ef5bccc --- /dev/null +++ b/spec/construct/test_valid_fail_expr.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_expr import _schema + +class TestValidFailExpr(unittest.TestCase): + def test_valid_fail_expr(self): + with self.assertRaises(kaitaistruct.ValidationExprError): + r = _schema.parse_file('src/nav_parent_switch.bin') diff --git a/spec/construct/test_valid_fail_inst.py b/spec/construct/test_valid_fail_inst.py new file mode 100644 index 000000000..f8875c88d --- /dev/null +++ b/spec/construct/test_valid_fail_inst.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_inst import _schema + +class TestValidFailInst(unittest.TestCase): + def test_valid_fail_inst(self): + with self.assertRaises(kaitaistruct.ValidationNotEqualError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_max_int.py b/spec/construct/test_valid_fail_max_int.py new file mode 100644 index 000000000..2882bccf7 --- /dev/null +++ b/spec/construct/test_valid_fail_max_int.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_max_int import _schema + +class TestValidFailMaxInt(unittest.TestCase): + def test_valid_fail_max_int(self): + with self.assertRaises(kaitaistruct.ValidationGreaterThanError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_min_int.py b/spec/construct/test_valid_fail_min_int.py new file mode 100644 index 000000000..b0bd166f8 --- /dev/null +++ b/spec/construct/test_valid_fail_min_int.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_min_int import _schema + +class TestValidFailMinInt(unittest.TestCase): + def test_valid_fail_min_int(self): + with self.assertRaises(kaitaistruct.ValidationLessThanError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_range_bytes.py b/spec/construct/test_valid_fail_range_bytes.py new file mode 100644 index 000000000..9b91b5447 --- /dev/null +++ b/spec/construct/test_valid_fail_range_bytes.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_range_bytes import _schema + +class TestValidFailRangeBytes(unittest.TestCase): + def test_valid_fail_range_bytes(self): + with self.assertRaises(kaitaistruct.ValidationGreaterThanError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_range_float.py b/spec/construct/test_valid_fail_range_float.py new file mode 100644 index 000000000..2959777d3 --- /dev/null +++ b/spec/construct/test_valid_fail_range_float.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_range_float import _schema + +class TestValidFailRangeFloat(unittest.TestCase): + def test_valid_fail_range_float(self): + with self.assertRaises(kaitaistruct.ValidationGreaterThanError): + r = _schema.parse_file('src/floating_points.bin') diff --git a/spec/construct/test_valid_fail_range_int.py b/spec/construct/test_valid_fail_range_int.py new file mode 100644 index 000000000..ca894ca54 --- /dev/null +++ b/spec/construct/test_valid_fail_range_int.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_range_int import _schema + +class TestValidFailRangeInt(unittest.TestCase): + def test_valid_fail_range_int(self): + with self.assertRaises(kaitaistruct.ValidationGreaterThanError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/spec/construct/test_valid_fail_range_str.py b/spec/construct/test_valid_fail_range_str.py new file mode 100644 index 000000000..0222aec45 --- /dev/null +++ b/spec/construct/test_valid_fail_range_str.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_range_str import _schema + +class TestValidFailRangeStr(unittest.TestCase): + def test_valid_fail_range_str(self): + with self.assertRaises(kaitaistruct.ValidationGreaterThanError): + r = _schema.parse_file('src/fixed_struct.bin') diff --git a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/ConstructSG.scala b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/ConstructSG.scala index 90d452400..e403b9f92 100644 --- a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/ConstructSG.scala +++ b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/ConstructSG.scala @@ -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 @@ -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 = { @@ -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")