11package io .kaitai .struct .testtranslator .specgenerators
22
33import _root_ .io .kaitai .struct .datatype .{DataType , EndOfStreamError , KSError }
4+ import _root_ .io .kaitai .struct .datatype .DataType ._
45import _root_ .io .kaitai .struct .exprlang .Ast
56import _root_ .io .kaitai .struct .languages .GoCompiler
67import _root_ .io .kaitai .struct .testtranslator .{Main , TestAssert , TestEquals , TestSpec }
@@ -27,12 +28,16 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
2728 provider : TypeProvider ,
2829 importList : ImportList ,
2930 ) extends GoTranslator (out, provider, importList) {
31+ var doErrCheck = true
32+
3033 override def outAddErrCheck (): Unit = {
31- out.puts(" if err != nil {" )
32- out.inc
33- out.puts(" t.Fatal(err)" )
34- out.dec
35- out.puts(" }" )
34+ if (doErrCheck) {
35+ out.puts(" if err != nil {" )
36+ out.inc
37+ out.puts(" t.Fatal(err)" )
38+ out.dec
39+ out.puts(" }" )
40+ }
3641 }
3742 }
3843
@@ -75,17 +80,7 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
7580
7681 override def runParseExpectError (exception : KSError ): Unit = {
7782 out.puts(" err = r.Read(s, &r, &r)" )
78- importList.add(" \" github.com/stretchr/testify/assert\" " )
79- out.puts(" assert.Error(t, err)" )
80- exception match {
81- case EndOfStreamError =>
82- importList.add(" \" io\" " )
83- out.puts(" assert.ErrorIs(t, err, io.ErrUnexpectedEOF)" )
84- case _ =>
85- val errorName = GoCompiler .ksErrorName(exception)
86- out.puts(s " var wantErr ${errorName}" )
87- out.puts(" assert.ErrorAs(t, err, &wantErr)" )
88- }
83+ checkErr(exception)
8984 }
9085
9186 override def footer () = {
@@ -116,6 +111,33 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
116111 def trueArrayEquality (check : TestEquals , elType : DataType , elts : Seq [Ast .expr]): Unit =
117112 simpleEquality(check)
118113
114+ override def testException (actual : Ast .expr, exception : KSError ): Unit = {
115+ // We need a scope otherwise we got redeclaration error from Go in case of
116+ // several assertions, because we use the same name for expected exception
117+ out.puts(" {" )
118+ out.inc
119+
120+ // We do not want error check because we expect an error
121+ translator.doErrCheck = false
122+ val actStr = translateAct(actual)
123+ translator.doErrCheck = true
124+
125+ checkErr(exception)
126+
127+ // translateAct generates unused variable which not allowed in Go,
128+ // so we use it by checking its value
129+ translator.detectType(actual) match {
130+ case _ : FloatType => out.puts(s " assert.InDelta(t, 0, $actStr, $FLOAT_DELTA) " )
131+ case _ : NumericType => out.puts(s " assert.EqualValues(t, 0, $actStr) " )
132+ case _ : BooleanType => out.puts(s " assert.EqualValues(t, false, $actStr) " )
133+ case _ : StrType => out.puts(s " assert.EqualValues(t, \"\" , $actStr) " )
134+ case _ => out.puts(s " assert.Nil(t, $actStr) " )
135+ }
136+
137+ out.dec
138+ out.puts(" }" )
139+ }
140+
119141 override def indentStr : String = " \t "
120142
121143 override def results : String = {
@@ -138,4 +160,19 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
138160
139161 def translateAct (x : Ast .expr) =
140162 translator.translate(x).replace(REPLACER , " r." )
163+
164+ /** Generates code to check returned Go error to match of specified `exception`. */
165+ def checkErr (exception : KSError ): Unit = {
166+ importList.add(" \" github.com/stretchr/testify/assert\" " )
167+ out.puts(" assert.Error(t, err)" )
168+ exception match {
169+ case EndOfStreamError =>
170+ importList.add(" \" io\" " )
171+ out.puts(" assert.ErrorIs(t, err, io.ErrUnexpectedEOF)" )
172+ case _ =>
173+ val errorName = GoCompiler .ksErrorName(exception)
174+ out.puts(s " var wantErr ${errorName}" )
175+ out.puts(" assert.ErrorAs(t, err, &wantErr)" )
176+ }
177+ }
141178}
0 commit comments