This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
forked from starlight-go/starlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starlight_test.go
170 lines (150 loc) · 3.75 KB
/
starlight_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
package starlight
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestConversion(t *testing.T) {
dir, cleanup := makeScript(t, "out.star",
`output = input + " world" + bang()`)
defer cleanup()
bang := func() string { return "!" }
s := New(dir)
actual, err := s.Run("out.star", map[string]interface{}{
"input": "hello",
"bang": bang,
})
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"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("testdata", "testdata/later")
v, err := s.Run("foo.star", 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("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 TestRerun(t *testing.T) {
dir, cleanup := makeScript(t, "foo.star",
`output = input + " world!"`)
defer cleanup()
s := New(dir)
actual, err := s.Run("foo.star", 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.star", 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
err = ioutil.WriteFile(filepath.Join(dir, "foo.star"), []byte(`output = "hi!"`), 0600)
if err != nil {
t.Fatal(err)
}
actual, err = s.Run("foo.star", 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.star")
actual, err = s.Run("foo.star", 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()
err = ioutil.WriteFile(filepath.Join(dir, "foo.star"), []byte(`output = "bye!"`), 0600)
if err != nil {
t.Fatal(err)
}
actual, err = s.Run("foo.star", 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([]byte(`output = hi()`), map[string]interface{}{
"hi": func() string { return "hi!" },
}, nil)
if err != nil {
t.Fatal(err)
}
if v["output"] != "hi!" {
t.Fatalf(`expected "hi!" but got %q`, v["output"])
}
}
func makeScript(t *testing.T, name, data string) (dir string, cleanup func()) {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
filename := filepath.Join(dir, name)
t.Logf("creating new script at %v", filename)
err = ioutil.WriteFile(filename, []byte(data), 0600)
if err != nil {
os.RemoveAll(dir)
t.Fatal(err)
}
return dir, func() { os.RemoveAll(dir) }
}