This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ssainterp_test.go
137 lines (117 loc) · 2.44 KB
/
ssainterp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ssainterp_test
import (
"bytes"
"testing"
"github.com/go-interpreter/ssainterp"
"github.com/go-interpreter/ssainterp/interp"
)
func TestOutputCapture(t *testing.T) {
var out bytes.Buffer
_, _, err := ssainterp.Run(`
package main
func main() {
println("TEST-OUTPUT")
}
`, nil, nil, &out)
if err != nil {
t.Error(err)
}
s := string(out.Bytes())
if s != "TEST-OUTPUT\n" {
t.Error("output not as expected:" + s)
}
}
func TestCallInOut(t *testing.T) {
ctxt, exitCode, err := ssainterp.Run(`
package main
// function to call into proper Go
func Foo(a,b int) int
// function to be called from proper Go
func fact(n int) int {
//println("fact",n)
if n == 0 {
return 1
}
return n * fact(n-1)
}
func main() {
// call a func defined externally
if Foo(2,2) != 4 {
panic("2+2 not equal 4!")
}
}
`, []ssainterp.ExtFunc{
ssainterp.ExtFunc{
Nam: "main.Foo",
Fun: func(args []interp.Ivalue) interp.Ivalue {
return args[0].(int) + args[1].(int)
},
},
}, nil, nil)
if err != nil || exitCode != 0 {
t.Error(exitCode, err)
return
}
// call a function within the interpreter context
ival, err := ctxt.Call("main.fact", []interp.Ivalue{int(5)})
if err != nil {
t.Error(err)
return
}
if ival != 120 {
t.Error("context call fact(5) != 120, value:", ival)
}
}
func TestSSAinterpBad(t *testing.T) {
_, _, err := ssainterp.Run(`this is not go!`, nil, nil, nil)
if err == nil {
t.Error("bad code did not error")
}
//t.Log(err)
_, _, err = ssainterp.Run(`
package main
func notmain() {
}
`, nil, nil, nil)
if err == nil {
t.Error("no main function did not error")
}
//t.Log(err)
ctx, _, err := ssainterp.Run(`
package main
func main() {
}
`, nil, nil, nil)
if err != nil {
t.Error(err)
return
}
_, err = ctx.Call("very.unknown", nil)
if err == nil {
t.Error("unknown Interpreter.Call did not error")
}
}
func TestUnknownInclude(t *testing.T) {
_, _, err := ssainterp.Run(`
package main
import "there/is/no/package/with/this/name"
func main() {
}
`, nil, nil, nil)
if err == nil {
t.Error("bad import did not error")
}
t.Log("NOTE: there should be a file not found error above.")
}
func TestPanic(t *testing.T) {
_, _, err := ssainterp.Run(`
package main
func main() {
panic("HELP!")
}
`, nil, nil, nil)
if err == nil {
t.Error("panic did not error")
}
t.Log("NOTE: there should be a panic 'HELP!' above with a stack dump.")
}