Skip to content

Commit

Permalink
C#: generate casts of expected numerical constants to enum types
Browse files Browse the repository at this point in the history
Tests regenerated using this command:
./spec_kst_to_all -t csharp --all-specs -f
  • Loading branch information
Mingun committed Apr 6, 2024
1 parent ec0fafa commit cc673b9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.

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.

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 @@ -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}
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit cc673b9

Please sign in to comment.