From 85049abbf10130958822b6f9a3dd8a432b6855dd Mon Sep 17 00:00:00 2001 From: Mingun Date: Tue, 20 Aug 2024 21:51:17 +0500 Subject: [PATCH] Change test generator to correctly handle unknown enum values and regenerate tests --- .../kaitai/struct/spec/TestEnumInvalid.java | 10 +++++---- .../struct/spec/TestEnumToIInvalid.java | 3 ++- .../spec/TestSwitchManualEnumInvalid.java | 21 +++++++++---------- .../spec/TestSwitchManualEnumInvalidElse.java | 21 +++++++++---------- .../specgenerators/JavaSG.scala | 17 +++++++++++++-- 5 files changed, 43 insertions(+), 29 deletions(-) diff --git a/spec/java/src/io/kaitai/struct/spec/TestEnumInvalid.java b/spec/java/src/io/kaitai/struct/spec/TestEnumInvalid.java index a90501a5f..365efd344 100644 --- a/spec/java/src/io/kaitai/struct/spec/TestEnumInvalid.java +++ b/spec/java/src/io/kaitai/struct/spec/TestEnumInvalid.java @@ -1,16 +1,18 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + package io.kaitai.struct.spec; import io.kaitai.struct.testformats.EnumInvalid; import org.testng.annotations.Test; - -import static org.testng.Assert.assertEquals; - +import static org.testng.Assert.*; public class TestEnumInvalid extends CommonSpec { + @Test public void testEnumInvalid() throws Exception { EnumInvalid r = EnumInvalid.fromFile(SRC_DIR + "term_strz.bin"); assertEquals(r.pet1(), EnumInvalid.Animal.DOG); - assertEquals(r.pet2(), null); + assertEquals(r.pet2(), EnumInvalid.Animal.byId(111)); + assertTrue(EnumInvalid.Animal.byId(111) instanceof EnumInvalid.Animal.Unknown); } } diff --git a/spec/java/src/io/kaitai/struct/spec/TestEnumToIInvalid.java b/spec/java/src/io/kaitai/struct/spec/TestEnumToIInvalid.java index 6b78337c6..1c0787be4 100644 --- a/spec/java/src/io/kaitai/struct/spec/TestEnumToIInvalid.java +++ b/spec/java/src/io/kaitai/struct/spec/TestEnumToIInvalid.java @@ -12,7 +12,8 @@ public void testEnumToIInvalid() throws Exception { EnumToIInvalid r = EnumToIInvalid.fromFile(SRC_DIR + "term_strz.bin"); assertEquals(r.pet1(), EnumToIInvalid.Animal.DOG); - assertEquals(r.pet2(), 111); + assertEquals(r.pet2(), EnumToIInvalid.Animal.byId(111)); + assertTrue(EnumToIInvalid.Animal.byId(111) instanceof EnumToIInvalid.Animal.Unknown); assertIntEquals(r.pet2I(), 111); assertEquals(r.pet2IToS(), "111"); assertIntEquals(r.pet2Mod(), 32879); diff --git a/spec/java/src/io/kaitai/struct/spec/TestSwitchManualEnumInvalid.java b/spec/java/src/io/kaitai/struct/spec/TestSwitchManualEnumInvalid.java index 924325eb4..d3527e4e6 100644 --- a/spec/java/src/io/kaitai/struct/spec/TestSwitchManualEnumInvalid.java +++ b/spec/java/src/io/kaitai/struct/spec/TestSwitchManualEnumInvalid.java @@ -1,23 +1,22 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + package io.kaitai.struct.spec; import io.kaitai.struct.testformats.SwitchManualEnumInvalid; -import io.kaitai.struct.testformats.SwitchManualEnumInvalid.Opcode.*; - import org.testng.annotations.Test; - import static org.testng.Assert.*; - public class TestSwitchManualEnumInvalid extends CommonSpec { + @Test public void testSwitchManualEnumInvalid() throws Exception { SwitchManualEnumInvalid r = SwitchManualEnumInvalid.fromFile(SRC_DIR + "enum_negative.bin"); - assertEquals(r.opcodes().size(), 2); - - assertNull(r.opcodes().get(0).code()); - assertNull(r.opcodes().get(0).body()); - - assertNull(r.opcodes().get(1).code()); - assertNull(r.opcodes().get(1).body()); + assertIntEquals(r.opcodes().size(), 2); + assertEquals(r.opcodes().get((int) 0).code(), SwitchManualEnumInvalid.Opcode.CodeEnum.byId(255)); + assertTrue(SwitchManualEnumInvalid.Opcode.CodeEnum.byId(255) instanceof SwitchManualEnumInvalid.Opcode.CodeEnum.Unknown); + assertNull(r.opcodes().get((int) 0).body()); + assertEquals(r.opcodes().get((int) 1).code(), SwitchManualEnumInvalid.Opcode.CodeEnum.byId(1)); + assertTrue(SwitchManualEnumInvalid.Opcode.CodeEnum.byId(1) instanceof SwitchManualEnumInvalid.Opcode.CodeEnum.Unknown); + assertNull(r.opcodes().get((int) 1).body()); } } diff --git a/spec/java/src/io/kaitai/struct/spec/TestSwitchManualEnumInvalidElse.java b/spec/java/src/io/kaitai/struct/spec/TestSwitchManualEnumInvalidElse.java index 7bf215702..1529d1b55 100644 --- a/spec/java/src/io/kaitai/struct/spec/TestSwitchManualEnumInvalidElse.java +++ b/spec/java/src/io/kaitai/struct/spec/TestSwitchManualEnumInvalidElse.java @@ -1,23 +1,22 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + package io.kaitai.struct.spec; import io.kaitai.struct.testformats.SwitchManualEnumInvalidElse; -import io.kaitai.struct.testformats.SwitchManualEnumInvalidElse.Opcode.*; - import org.testng.annotations.Test; - import static org.testng.Assert.*; - public class TestSwitchManualEnumInvalidElse extends CommonSpec { + @Test public void testSwitchManualEnumInvalidElse() throws Exception { SwitchManualEnumInvalidElse r = SwitchManualEnumInvalidElse.fromFile(SRC_DIR + "enum_negative.bin"); - assertEquals(r.opcodes().size(), 2); - - assertNull(r.opcodes().get(0).code()); - assertEquals(((Defval) r.opcodes().get(0).body()).value().intValue(), 123); - - assertNull(r.opcodes().get(1).code()); - assertEquals(((Defval) r.opcodes().get(1).body()).value().intValue(), 123); + assertIntEquals(r.opcodes().size(), 2); + assertEquals(r.opcodes().get((int) 0).code(), SwitchManualEnumInvalidElse.Opcode.CodeEnum.byId(255)); + assertTrue(SwitchManualEnumInvalidElse.Opcode.CodeEnum.byId(255) instanceof SwitchManualEnumInvalidElse.Opcode.CodeEnum.Unknown); + assertIntEquals(((SwitchManualEnumInvalidElse.Opcode.Defval) (r.opcodes().get((int) 0).body())).value(), 123); + assertEquals(r.opcodes().get((int) 1).code(), SwitchManualEnumInvalidElse.Opcode.CodeEnum.byId(1)); + assertTrue(SwitchManualEnumInvalidElse.Opcode.CodeEnum.byId(1) instanceof SwitchManualEnumInvalidElse.Opcode.CodeEnum.Unknown); + assertIntEquals(((SwitchManualEnumInvalidElse.Opcode.Defval) (r.opcodes().get((int) 1).body())).value(), 123); } } diff --git a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/JavaSG.scala b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/JavaSG.scala index fe56d3e65..d26284600 100644 --- a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/JavaSG.scala +++ b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/JavaSG.scala @@ -65,11 +65,24 @@ class JavaSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator( override def simpleEquality(check: TestEquals): Unit = { val actType = translator.detectType(check.actual) + val expType = translator.detectType(check.expected) + val actStr = translateAct(check.actual) val expStr = translator.translate(check.expected) - actType match { - case _: IntType | _: BooleanType => + + (actType, expType) match { + case (_: IntType | _: BooleanType, _) => out.puts(s"assertIntEquals($actStr, $expStr);") + case (et: EnumType, _: IntType) => { + // When we expect unknown value of enumeration, in KST we uses just it integer value + // That expression would be translated to number. Wh should wrap it to enum + val enumSpec = et.enumSpec.get + val expEnum = translator.doEnumById(enumSpec, expStr) + val enumName = translator.enumClass(enumSpec.name) + + out.puts(s"assertEquals($actStr, $expEnum);") + out.puts(s"assertTrue($expEnum instanceof $enumName.Unknown);") + } case _ => out.puts(s"assertEquals($actStr, $expStr);") }