1
1
package io .kaitai .struct .testtranslator .specgenerators
2
2
3
3
import _root_ .io .kaitai .struct .datatype .{DataType , EndOfStreamError , KSError }
4
+ import _root_ .io .kaitai .struct .datatype .DataType ._
4
5
import _root_ .io .kaitai .struct .exprlang .Ast
5
6
import _root_ .io .kaitai .struct .languages .GoCompiler
6
7
import _root_ .io .kaitai .struct .testtranslator .{Main , TestAssert , TestEquals , TestSpec }
@@ -27,12 +28,16 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
27
28
provider : TypeProvider ,
28
29
importList : ImportList ,
29
30
) extends GoTranslator (out, provider, importList) {
31
+ var doErrCheck = true
32
+
30
33
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
+ }
36
41
}
37
42
}
38
43
@@ -75,17 +80,7 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
75
80
76
81
override def runParseExpectError (exception : KSError ): Unit = {
77
82
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)
89
84
}
90
85
91
86
override def footer () = {
@@ -116,6 +111,33 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
116
111
def trueArrayEquality (check : TestEquals , elType : DataType , elts : Seq [Ast .expr]): Unit =
117
112
simpleEquality(check)
118
113
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
+
119
141
override def indentStr : String = " \t "
120
142
121
143
override def results : String = {
@@ -138,4 +160,19 @@ class GoSG(spec: TestSpec, provider: ClassTypeProvider) extends BaseGenerator(sp
138
160
139
161
def translateAct (x : Ast .expr) =
140
162
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
+ }
141
178
}
0 commit comments