/
jsonnet_test.go
311 lines (258 loc) · 7.24 KB
/
jsonnet_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package jsonnet
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
// Demo for the README.
func Test_Demo(t *testing.T) {
vm := Make()
vm.ExtVar("color", "purple")
x, err := vm.EvaluateSnippet(`Test_Demo`, `"dark " + std.extVar("color")`)
if err != nil {
panic(err)
}
if x != "\"dark purple\"\n" {
panic("fail: we got " + x)
}
vm.Destroy()
}
// importFunc returns a couple of hardwired responses.
func importFunc(base, rel string) (result string, path string, err error) {
if rel == "alien.conf" {
return `{ type: "alien", origin: "Ork", name: "Mork" }`, "alien.conf", nil
}
if rel == "human.conf" {
return `{ type: "human", origin: "Earth", name: "Mendy" }`, "human.conf", nil
}
return "", "", errors.New(fmt.Sprintf("Cannot import %q", rel))
}
// check there is no err, and a == b.
func check(t *testing.T, err error, a, b string) {
if err != nil {
t.Errorf("got error: %q", err.Error())
}
if a != b {
t.Errorf("got %q but wanted %q", a, b)
}
}
func Test_Simple(t *testing.T) {
// Each time there's a new version, this will force an update to this code.
check(t, nil, Version(), `v0.10.0`)
vm := Make()
defer vm.Destroy()
vm.TlaVar("color", "purple")
vm.TlaVar("size", "XXL")
vm.TlaCode("gooselevel", "1234 * 10 + 5")
vm.ExtVar("color", "purple")
vm.ExtVar("size", "XXL")
vm.ExtCode("gooselevel", "1234 * 10 + 5")
vm.ImportCallback(importFunc)
x, err := vm.EvaluateSnippet(`test1`, `20 + 22`)
check(t, err, x, `42`+"\n")
x, err = vm.EvaluateSnippet(`test2`, `function(color, size, gooselevel) color`)
check(t, err, x, `"purple"`+"\n")
x, err = vm.EvaluateSnippet(`test2`, `std.extVar("color")`)
check(t, err, x, `"purple"`+"\n")
vm.StringOutput(true)
x, err = vm.EvaluateSnippet(`test2`, `"whee"`)
check(t, err, x, `whee`+"\n")
vm.StringOutput(false)
x, err = vm.EvaluateSnippet(`test3`, `
local a = import "alien.conf";
local b = import "human.conf";
a.name + b.name
`)
check(t, err, x, `"MorkMendy"`+"\n")
x, err = vm.EvaluateSnippet(`test4`, `
local a = import "alien.conf";
local b = a { type: "fictitious" };
b.type + b.name
`)
check(t, err, x, `"fictitiousMork"`+"\n")
}
func Test_FileScript(t *testing.T) {
vm := Make()
defer vm.Destroy()
x, err := vm.EvaluateFile("test2.j")
check(t, err, x, `{
"awk": "/usr/bin/awk",
"shell": "/bin/csh"
}
`)
}
func Test_Misc(t *testing.T) {
vm := Make()
defer vm.Destroy()
vm.MaxStack(10)
vm.MaxTrace(10)
vm.GcMinObjects(10)
vm.GcGrowthTrigger(2.0)
x, err := vm.EvaluateSnippet("Misc", `
local a = import "test2.j";
a.awk + a.shell`)
check(t, err, x, `"/usr/bin/awk/bin/csh"`+"\n")
}
func Test_FormatFile(t *testing.T) {
f, err := ioutil.TempFile("", "jsonnet-fmt-test")
if err != nil {
t.Fatal(err)
}
filename := f.Name()
defer func() {
f.Close()
os.Remove(filename)
}()
data := `{
"quoted": "keys",
"notevaluated": 20 + 22,
"trailing": "comma",}
`
if err := ioutil.WriteFile(filename, []byte(data), 0644); err != nil {
t.Fatalf("WriteFile %s: %v", filename, err)
}
vm := Make()
defer vm.Destroy()
result, err := vm.FormatFile(filename)
check(t, err, result, "{\n quoted: 'keys',\n notevaluated: 20 + 22,\n trailing: 'comma',\n}\n")
}
func Test_FormatSnippet(t *testing.T) {
data := `{
"quoted": "keys",
"notevaluated": 20 + 22,
"trailing": "comma",}
`
vm := Make()
defer vm.Destroy()
result, err := vm.FormatSnippet("testfoo", data)
check(t, err, result, "{\n quoted: 'keys',\n notevaluated: 20 + 22,\n trailing: 'comma',\n}\n")
}
func Test_FormatIndent(t *testing.T) {
data := `{
"quoted": "keys",
"notevaluated": 20 + 22,
"trailing": "comma",}
`
vm := Make()
defer vm.Destroy()
vm.FormatIndent(1)
result, err := vm.FormatSnippet("testfoo", data)
check(t, err, result, "{\n quoted: 'keys',\n notevaluated: 20 + 22,\n trailing: 'comma',\n}\n")
}
func TestJsonString(t *testing.T) {
vm := Make()
defer vm.Destroy()
v := vm.NewString("foo")
if v2, ok := v.ExtractString(); !ok {
t.Errorf("ExtractString() returned !ok")
} else if v2 != "foo" {
t.Errorf("Incorrect extracted string: %s", v2)
}
if _, ok := v.ExtractNumber(); ok {
t.Errorf("ExtractNumber() returned ok")
}
if _, ok := v.ExtractBool(); ok {
t.Errorf("ExtractBool() returned ok")
}
if ok := v.ExtractNull(); ok {
t.Errorf("ExtractNull() returned ok")
}
}
func TestJsonNumber(t *testing.T) {
vm := Make()
defer vm.Destroy()
v := vm.NewNumber(42)
if _, ok := v.ExtractString(); ok {
t.Errorf("ExtractString() returned ok")
}
if v2, ok := v.ExtractNumber(); !ok {
t.Errorf("ExtractNumber() returned !ok")
} else if v2 != 42 {
t.Errorf("ExtractNumber() returned unexpected value: %v", v2)
}
if _, ok := v.ExtractBool(); ok {
t.Errorf("ExtractBool() returned ok")
}
if ok := v.ExtractNull(); ok {
t.Errorf("ExtractNull() returned ok")
}
}
func TestJsonArray(t *testing.T) {
vm := Make()
defer vm.Destroy()
a := vm.NewArray()
a.ArrayAppend(vm.NewString("foo"))
a.ArrayAppend(vm.NewNull())
a.ArrayAppend(vm.NewNumber(3.14))
// Can't actually inspect array elements with this version of
// jsonnet ...
}
func TestJsonObject(t *testing.T) {
vm := Make()
defer vm.Destroy()
a := vm.NewObject()
a.ObjectAppend("foo", vm.NewString("foo"))
a.ObjectAppend("bar", vm.NewNull())
a.ObjectAppend("baz", vm.NewNumber(3.14))
// Can't actually inspect array elements with this version of
// jsonnet ...
}
func TestNativeRaw(t *testing.T) {
vm := Make()
defer vm.Destroy()
vm.NativeCallbackRaw("myadd", []string{"a", "b"}, func(args ...*JsonValue) (*JsonValue, error) {
if len(args) != 2 {
return nil, errors.New("wrong number of args")
}
a, ok := args[0].ExtractNumber()
if !ok {
return nil, errors.New("a is not a number")
}
b, ok := args[1].ExtractNumber()
if !ok {
return nil, errors.New("b is not a number")
}
return vm.NewNumber(a + b), nil
})
x, err := vm.EvaluateSnippet("NativeRaw", `std.native("myadd")(3, 4)`)
check(t, err, x, "7\n")
x, err = vm.EvaluateSnippet("NativeRaw", `std.native("myadd")(42)`)
if err == nil {
t.Errorf("Wrong number of args failed to produce error")
}
x, err = vm.EvaluateSnippet("NativeRaw", `std.native("myadd")(3, "foo")`)
if err == nil {
t.Errorf("Go error was not translated into jsonnet error")
} else if !strings.Contains(err.Error(), "b is not a number") {
t.Errorf("Wrong jsonnet error: %v", err)
}
}
func TestNative(t *testing.T) {
vm := Make()
defer vm.Destroy()
vm.NativeCallback("myadd", []string{"a", "b"}, func(a, b float64) (float64, error) {
return a + b, nil
})
vm.NativeCallback("fail", []string{}, func() (interface{}, error) {
return nil, fmt.Errorf("this is an error")
})
x, err := vm.EvaluateSnippet("Native", `std.native("myadd")(3, 4)`)
check(t, err, x, "7\n")
x, err = vm.EvaluateSnippet("Native", `std.native("myadd")(42)`)
if err == nil {
t.Errorf("Wrong number of args failed to produce error")
}
x, err = vm.EvaluateSnippet("Native", `std.native("myadd")(3, "foo")`)
if err == nil {
t.Errorf("Wrong arg types failed to produce an error")
}
x, err = vm.EvaluateSnippet("Native", `std.native("fail")()`)
if err == nil {
t.Errorf("Go error was not propagated")
} else if !strings.Contains(err.Error(), "this is an error") {
t.Errorf("Go error text was not propagated")
}
}