Skip to content

Commit

Permalink
Go: support asserts[i].exception key and generate expr_to_i_trailin…
Browse files Browse the repository at this point in the history
…g test
  • Loading branch information
Mingun committed Jul 19, 2024
1 parent 5dbf318 commit 491e889
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 16 deletions.
51 changes: 51 additions & 0 deletions spec/go/expr_to_i_trailing_test.go

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

import _root_.io.kaitai.struct.datatype.{DataType, EndOfStreamError, KSError}
import _root_.io.kaitai.struct.datatype.DataType._
import _root_.io.kaitai.struct.exprlang.Ast
import _root_.io.kaitai.struct.languages.GoCompiler
import _root_.io.kaitai.struct.testtranslator.{Main, TestAssert, TestEquals, TestSpec}
Expand All @@ -27,12 +28,16 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
provider: TypeProvider,
importList: ImportList,
) extends GoTranslator(out, provider, importList) {
var doErrCheck = true

override def outAddErrCheck(): Unit = {
out.puts("if err != nil {")
out.inc
out.puts("t.Fatal(err)")
out.dec
out.puts("}")
if (doErrCheck) {
out.puts("if err != nil {")
out.inc
out.puts("t.Fatal(err)")
out.dec
out.puts("}")
}
}
}

Expand Down Expand Up @@ -75,17 +80,7 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp

override def runParseExpectError(exception: KSError): Unit = {
out.puts("err = r.Read(s, &r, &r)")
importList.add("\"github.com/stretchr/testify/assert\"")
out.puts("assert.Error(t, err)")
exception match {
case EndOfStreamError =>
importList.add("\"io\"")
out.puts("assert.ErrorIs(t, err, io.ErrUnexpectedEOF)")
case _ =>
val errorName = GoCompiler.ksErrorName(exception)
out.puts(s"var wantErr ${errorName}")
out.puts("assert.ErrorAs(t, err, &wantErr)")
}
checkErr(exception)
}

override def footer() = {
Expand Down Expand Up @@ -116,6 +111,33 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
def trueArrayEquality(check: TestEquals, elType: DataType, elts: Seq[Ast.expr]): Unit =
simpleEquality(check)

override def testException(actual: Ast.expr, exception: KSError): Unit = {
// We need a scope otherwise we got redeclaration error from Go in case of
// several assertions, because we use the same name for expected exception
out.puts("{")
out.inc

// We do not want error check because we expect an error
translator.doErrCheck = false
val actStr = translateAct(actual)
translator.doErrCheck = true

checkErr(exception)

// translateAct generates unused variable which not allowed in Go,
// so we use it by checking its value
translator.detectType(actual) match {
case _: FloatType => out.puts(s"assert.InDelta(t, 0, $actStr, $FLOAT_DELTA)")
case _: NumericType => out.puts(s"assert.EqualValues(t, 0, $actStr)")
case _: BooleanType => out.puts(s"assert.EqualValues(t, false, $actStr)")
case _: StrType => out.puts(s"assert.EqualValues(t, \"\", $actStr)")
case _ => out.puts(s"assert.Nil(t, $actStr)")
}

out.dec
out.puts("}")
}

override def indentStr: String = "\t"

override def results: String = {
Expand All @@ -138,4 +160,19 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp

def translateAct(x: Ast.expr) =
translator.translate(x).replace(REPLACER, "r.")

/** Generates code to check returned Go error to match of specified `exception`. */
def checkErr(exception: KSError): Unit = {
importList.add("\"github.com/stretchr/testify/assert\"")
out.puts("assert.Error(t, err)")
exception match {
case EndOfStreamError =>
importList.add("\"io\"")
out.puts("assert.ErrorIs(t, err, io.ErrUnexpectedEOF)")
case _ =>
val errorName = GoCompiler.ksErrorName(exception)
out.puts(s"var wantErr ${errorName}")
out.puts("assert.ErrorAs(t, err, &wantErr)")
}
}
}

0 comments on commit 491e889

Please sign in to comment.