forked from starlight-go/starlight
-
Notifications
You must be signed in to change notification settings - Fork 4
/
skyhook_test.go
179 lines (159 loc) · 4.01 KB
/
skyhook_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package skyhook
import (
"reflect"
"testing"
)
func TestConversion(t *testing.T) {
data := []byte(`output = input + " world" + bang()`)
read := func(string) ([]byte, error) { return data, nil }
bang := func() string { return "!" }
s := New([]string{"bar"})
s.readFile = read
actual, err := s.Run("foo.sky", map[string]interface{}{
"input": "hello",
"bang": bang,
})
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"input": "hello",
"output": "hello world!",
}
if len(actual) != len(expected) {
t.Errorf("expected %d items, but got %d", len(expected), len(actual))
}
for k, v := range expected {
act, ok := actual[k]
if !ok {
t.Errorf("actual missing key %q", k)
continue
}
if !reflect.DeepEqual(act, v) {
t.Errorf("actual value for key %q expected to be %v but was %v", k, v, act)
}
}
}
func TestDirOrder(t *testing.T) {
s := New([]string{"testdata", "testdata/later"})
v, err := s.Run("foo.sky", map[string]interface{}{"input": "hello"})
if err != nil {
t.Fatal(err)
}
expected, actual := "hello world", v["output"]
if actual != expected {
t.Fatalf("expected %q but got %q", expected, actual)
}
}
func TestAllDirs(t *testing.T) {
s := New([]string{"testdata", "testdata/later"})
v, err := s.Run("bar.sky", map[string]interface{}{"input": "hello"})
if err != nil {
t.Fatal(err)
}
expected, actual := "hello from bar.sky", v["output"]
if actual != expected {
t.Fatalf("expected %q but got %q", expected, actual)
}
}
func TestFunc(t *testing.T) {
data := []byte(`
def foo():
return " world!"
output = input + foo()
`)
read := func(string) ([]byte, error) { return data, nil }
s := New([]string{"bar"})
s.readFile = read
actual, err := s.Run("foo.sky", map[string]interface{}{"input": "hello"})
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"input": "hello",
"output": "hello world!",
}
if len(actual) != len(expected) {
t.Errorf("expected %d items, but got %d", len(expected), len(actual))
}
for k, v := range expected {
act, ok := actual[k]
if !ok {
t.Errorf("actual missing key %q", k)
continue
}
if !reflect.DeepEqual(act, v) {
t.Errorf("actual value for key %q expected to be %v but was %v", k, v, act)
}
}
}
func TestRerun(t *testing.T) {
data := []byte(`output = input + " world!"`)
read := func(string) ([]byte, error) { return data, nil }
s := New([]string{"bar"})
s.readFile = read
actual, err := s.Run("foo.sky", map[string]interface{}{
"input": "hello",
})
if err != nil {
t.Fatal(err)
}
if actual["output"] != "hello world!" {
t.Fatalf(`expected "hello world!" but got %q`, actual["output"])
}
// change inputs but not script
actual, err = s.Run("foo.sky", map[string]interface{}{
"input": "goodbye",
})
if err != nil {
t.Fatal(err)
}
if actual["output"] != "goodbye world!" {
t.Fatalf(`expected "goodbye world!" but got %q`, actual["output"])
}
// change script, shouldn't change output sicne we cached it
data = []byte(`output = "hi!"`)
actual, err = s.Run("foo.sky", map[string]interface{}{
"input": "goodbye",
})
if err != nil {
t.Fatal(err)
}
if actual["output"] != "goodbye world!" {
t.Fatalf(`expected "goodbye world!" but got %q`, actual["output"])
}
// remove script, should change output
s.Forget("foo.sky")
actual, err = s.Run("foo.sky", map[string]interface{}{
"input": "goodbye",
})
if err != nil {
t.Fatal(err)
}
if actual["output"] != "hi!" {
t.Fatalf(`expected "hi!" but got %q`, actual["output"])
}
// reset all, should change output
s.Reset()
data = []byte(`output = "bye!"`)
actual, err = s.Run("foo.sky", map[string]interface{}{
"input": "goodbye",
})
if err != nil {
t.Fatal(err)
}
if actual["output"] != "bye!" {
t.Fatalf(`expected "bye!" but got %q`, actual["output"])
}
}
func TestEval(t *testing.T) {
v, err := Eval(`output = hi()`, map[string]interface{}{
"hi": func() string { return "hi!" },
})
if err != nil {
t.Fatal(err)
}
if v["output"] != "hi!" {
t.Fatalf(`expected "hi!" but got %q`, v["output"])
}
}