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 Apr 13, 2024
1 parent 5422af5 commit 82a7fb1
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 @@ -13,15 +14,19 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
* to access "this.INIT_OBJ_NAME" and replaces it with "r."
*/
class GoTestOutputWriter(indentStr: String) extends GoOutputWriter(indentStr) {
var doErrCheck = true

override def puts(s: String): Unit = {
super.puts(s.replace(REPLACER, "r."))
}
override def putsErrCheck(result: Option[String]): Unit = {
puts("if err != nil {")
inc
puts("t.Fatal(err)")
dec
puts("}")
if (doErrCheck) {
puts("if err != nil {")
inc
puts("t.Fatal(err)")
dec
puts("}")
}
}
}

Expand Down Expand Up @@ -64,17 +69,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 @@ -105,6 +100,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
out.doErrCheck = false
val actStr = translateAct(actual)
out.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 @@ -127,4 +149,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 82a7fb1

Please sign in to comment.