Skip to content

Commit a5f78a3

Browse files
aykevldeadprogram
authored andcommitted
main: add interp tests
This one needed more advanced checking of the error messages, because we don't want to hardcode things like `!dbg !10` in the output.
1 parent e409950 commit a5f78a3

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

errors_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
func TestErrors(t *testing.T) {
1818
for _, name := range []string{
1919
"cgo",
20+
"interp",
2021
"loader-importcycle",
2122
"loader-invaliddep",
2223
"loader-invalidpackage",

interp/interpreter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
417417
} else {
418418
locals[inst.localIndex] = literalValue{uint8(0)}
419419
}
420+
case callFn.name == "__tinygo_interp_raise_test_error":
421+
// Special function that will trigger an error.
422+
// This is used to test error reporting.
423+
return nil, mem, r.errorAt(inst, errors.New("test error"))
420424
case strings.HasSuffix(callFn.name, ".$typeassert"):
421425
if r.debug {
422426
fmt.Fprintln(os.Stderr, indent+"interface assert:", operands[1:])

testdata/errors/interp.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import _ "unsafe"
4+
5+
func init() {
6+
foo()
7+
}
8+
9+
func foo() {
10+
interp_test_error()
11+
}
12+
13+
// This is a function that always causes an error in interp, for testing.
14+
//
15+
//go:linkname interp_test_error __tinygo_interp_raise_test_error
16+
func interp_test_error()
17+
18+
func main() {
19+
}
20+
21+
// ERROR: # main
22+
// ERROR: {{.*testdata[\\/]errors[\\/]interp\.go}}:10:19: test error
23+
// ERROR: call void @__tinygo_interp_raise_test_error{{.*}}
24+
// ERROR: {{}}
25+
// ERROR: traceback:
26+
// ERROR: {{.*testdata[\\/]errors[\\/]interp\.go}}:10:19:
27+
// ERROR: call void @__tinygo_interp_raise_test_error{{.*}}
28+
// ERROR: {{.*testdata[\\/]errors[\\/]interp\.go}}:6:5:
29+
// ERROR: call void @main.foo{{.*}}
30+
// ERROR: {{.*testdata[\\/]errors}}:
31+
// ERROR: call void @"main.init#1"{{.*}}

0 commit comments

Comments
 (0)