From cc673b9a4457fda266b6c4b03f030ad5b6fc4271 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 6 Apr 2024 22:30:22 +0500 Subject: [PATCH] C#: generate casts of expected numerical constants to enum types Tests regenerated using this command: ./spec_kst_to_all -t csharp --all-specs -f --- .../tests/SpecEnumInvalid.cs | 2 ++ .../tests/SpecEnumToIInvalid.cs | 2 ++ .../tests/SpecSwitchManualEnumInvalid.cs | 3 ++- .../tests/SpecSwitchManualEnumInvalidElse.cs | 3 ++- .../specgenerators/CSharpSG.scala | 23 ++++++++++++++----- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/spec/csharp/kaitai_struct_csharp_tests/tests/SpecEnumInvalid.cs b/spec/csharp/kaitai_struct_csharp_tests/tests/SpecEnumInvalid.cs index 19cce2d4b..f9c3140cd 100644 --- a/spec/csharp/kaitai_struct_csharp_tests/tests/SpecEnumInvalid.cs +++ b/spec/csharp/kaitai_struct_csharp_tests/tests/SpecEnumInvalid.cs @@ -1,3 +1,5 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + using NUnit.Framework; namespace Kaitai diff --git a/spec/csharp/kaitai_struct_csharp_tests/tests/SpecEnumToIInvalid.cs b/spec/csharp/kaitai_struct_csharp_tests/tests/SpecEnumToIInvalid.cs index 896647684..4295d21e4 100644 --- a/spec/csharp/kaitai_struct_csharp_tests/tests/SpecEnumToIInvalid.cs +++ b/spec/csharp/kaitai_struct_csharp_tests/tests/SpecEnumToIInvalid.cs @@ -1,3 +1,5 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + using NUnit.Framework; namespace Kaitai diff --git a/spec/csharp/kaitai_struct_csharp_tests/tests/SpecSwitchManualEnumInvalid.cs b/spec/csharp/kaitai_struct_csharp_tests/tests/SpecSwitchManualEnumInvalid.cs index 5a7c81c55..1190d9b0c 100644 --- a/spec/csharp/kaitai_struct_csharp_tests/tests/SpecSwitchManualEnumInvalid.cs +++ b/spec/csharp/kaitai_struct_csharp_tests/tests/SpecSwitchManualEnumInvalid.cs @@ -1,3 +1,5 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + using NUnit.Framework; namespace Kaitai @@ -10,7 +12,6 @@ public void TestSwitchManualEnumInvalid() { var r = SwitchManualEnumInvalid.FromFile(SourceFile("enum_negative.bin")); - Assert.AreEqual(r.Opcodes.Count, 2); Assert.AreEqual(r.Opcodes[0].Code, (SwitchManualEnumInvalid.Opcode.CodeEnum) 255); Assert.IsNull(r.Opcodes[0].Body); diff --git a/spec/csharp/kaitai_struct_csharp_tests/tests/SpecSwitchManualEnumInvalidElse.cs b/spec/csharp/kaitai_struct_csharp_tests/tests/SpecSwitchManualEnumInvalidElse.cs index 7611bb40f..92d4af9cc 100644 --- a/spec/csharp/kaitai_struct_csharp_tests/tests/SpecSwitchManualEnumInvalidElse.cs +++ b/spec/csharp/kaitai_struct_csharp_tests/tests/SpecSwitchManualEnumInvalidElse.cs @@ -1,3 +1,5 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + using NUnit.Framework; namespace Kaitai @@ -10,7 +12,6 @@ public void TestSwitchManualEnumInvalidElse() { var r = SwitchManualEnumInvalidElse.FromFile(SourceFile("enum_negative.bin")); - Assert.AreEqual(r.Opcodes.Count, 2); Assert.AreEqual(r.Opcodes[0].Code, (SwitchManualEnumInvalidElse.Opcode.CodeEnum) 255); Assert.AreEqual(((SwitchManualEnumInvalidElse.Opcode.Defval) (r.Opcodes[0].Body)).Value, 123); diff --git a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CSharpSG.scala b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CSharpSG.scala index 6714c8b52..f506a28b1 100644 --- a/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CSharpSG.scala +++ b/translator/src/main/scala/io/kaitai/struct/testtranslator/specgenerators/CSharpSG.scala @@ -2,6 +2,7 @@ package io.kaitai.struct.testtranslator.specgenerators import _root_.io.kaitai.struct.{ClassTypeProvider, Utils} import _root_.io.kaitai.struct.datatype.{DataType, EndOfStreamError, KSError} +import _root_.io.kaitai.struct.datatype.DataType.{EnumType, IntType} import _root_.io.kaitai.struct.exprlang.Ast import _root_.io.kaitai.struct.languages.CSharpCompiler import _root_.io.kaitai.struct.testtranslator.{Main, TestAssert, TestEquals, TestSpec} @@ -57,14 +58,24 @@ class CSharpSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerato } override def simpleEquality(check: TestEquals): Unit = { - val actType = translator.detectType(check.actual) val actStr = translateAct(check.actual) - val expStr = translator.translate(check.expected) - actType match { - case _ => - // TODO: fix order - actually it is (expected, actual) - out.puts(s"Assert.AreEqual($actStr, $expStr);") + var expStr = translator.translate(check.expected) + + // Specially for enums with is tested for unknown values + val actType = translator.detectType(check.actual) + val expType = translator.detectType(check.expected) + + // If type of value is enum but we check it against numeric value, + // do the cast of expected value to enum + expStr = (actType, expType) match { + case (act: EnumType, _: IntType) => { + val casted = CSharpCompiler.kaitaiType2NativeType(act, true) + s"($casted) $expStr" + } + case _ => expStr } + // TODO: fix order - actually it is (expected, actual) + out.puts(s"Assert.AreEqual($actStr, $expStr);") } override def floatEquality(check: TestEquals): Unit = {