-
Notifications
You must be signed in to change notification settings - Fork 5
/
jq_test.go
59 lines (53 loc) · 1.25 KB
/
jq_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
package jq_test
import (
"."
"encoding/json"
"testing"
)
func TestApply(t *testing.T) {
expectReturn(t, mustApply(t, `.`, 1), 1)
expectReturn(t, mustApply(t, `.`, 1), 1)
expectReturn(t, mustApply(t, `.`, 1, 2, 3), 1, 2, 3)
expectReturn(t, mustApply(t, `. | select(. != 0) | 1 / .`, 0, 1, 2, 3), 1.0/1, 1.0/2, 1.0/3)
}
func expectReturn(t *testing.T, seq []string, expect ...interface{}) {
if len(seq) != len(expect) {
t.Fatalf("expected: %v, got: %v", formatJson(expect), formatJson(seq))
}
for i, sj := range seq {
sx := formatJson(expect[i])
if sj != sx {
t.Errorf("item %v expected: %v, got: %v", i, sx, sj)
}
}
}
func mustApply(t *testing.T, proc string, input ...interface{}) []string {
seq, err := jq.Apply(proc, input...)
if err != nil {
t.Logf("while applying %#v to:", proc)
for i, inp := range input {
t.Logf("- %v: %#v", i, inp)
}
t.Fatal(err)
}
ret := make([]string, len(seq))
for i, s := range seq {
ret[i] = string(s)
}
return ret
}
func mustCompile(t *testing.T, proc string) {
vm, err := jq.Compile(proc)
defer vm.Close()
if err != nil {
t.Logf(`while compiling: %#v`, proc)
t.Fatal(err)
}
}
func formatJson(v interface{}) string {
p, err := json.Marshal(v)
if err != nil {
panic(err)
}
return string(p)
}