-
Notifications
You must be signed in to change notification settings - Fork 2
/
module_test.go
60 lines (49 loc) · 1 KB
/
module_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
package nethttp
import (
"bytes"
"fmt"
"log"
"strings"
"testing"
"go.starlark.net/starlark"
)
func TestModule(t *testing.T) {
tests := []testCase{
{
name: "module should identify itself",
input: `print(http)`,
output: `<module http>`,
},
}
for i, tc := range tests {
runTestCase(t, i, &tc)
}
}
type testCase struct {
name string
input string
output string
}
func runTestCase(t *testing.T, i int, tc *testCase) {
t.Run(tc.name, func(t *testing.T) {
var out bytes.Buffer
thread := &starlark.Thread{
Print: func(_ *starlark.Thread, msg string) { fmt.Fprintln(&out, msg) },
}
globals := starlark.StringDict{
"http": NewModule(),
}
_, err := starlark.ExecFile(thread, tc.name, tc.input, globals)
if err != nil {
if evalErr, ok := err.(*starlark.EvalError); ok {
log.Fatal(evalErr.Backtrace())
}
log.Fatal(err)
}
got := strings.TrimSpace(out.String())
want := tc.output
if got != want {
t.Errorf("#%d: got %q, want %q", i, got, want)
}
})
}