-
Notifications
You must be signed in to change notification settings - Fork 8
/
jq_test.go
87 lines (75 loc) · 1.93 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
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
package libjq_go
import (
"fmt"
"strings"
"testing"
)
func Test_OneProgram_OneInput(t *testing.T) {
res, err := Jq().Program(".foo").Run(`{"foo":"bar"}`)
if err != nil {
t.Fatalf("expect program not fail: %s", err)
}
if res != `"bar"` {
t.Fatalf("expect '\"bar\"', got '%s'", res)
}
res, err = Jq().Program(".foo").RunRaw(`{"foo":"bar"}`)
if err != nil {
t.Fatalf("expect program not fail: %s", err)
}
if res != `bar` {
t.Fatalf("expect 'bar', got '%s'", res)
}
}
func Benchmark_HasKey(b *testing.B) {
p, err := Jq().Program(`has("foo")`).Precompile()
if err != nil {
b.Fatalf("precompile program: %s", err)
}
for i := 0; i < b.N; i++ {
_, err := p.Run(`{"bar":"baz"}`)
if err != nil {
b.Fatalf("run %d: %s", i, err)
}
}
}
func Benchmark_PreCompile(b *testing.B) {
for i := 0; i < b.N; i++ {
prg := fmt.Sprintf(`has("foo%d")`, i)
p, err := Jq().Program(prg).Precompile()
if err != nil {
b.Fatalf("precompile program %d: %s", i, err)
}
_, err = p.Run(`{"bar":"baz"}`)
if err != nil {
b.Fatalf("run %d: %s", i, err)
}
}
}
func Test_CompileError(t *testing.T) {
_, err := Jq().Program(`{"message": .message"}`).Run(`{"message":"bar"}`)
if err == nil {
t.Fatal("expect program should fail")
}
expect := "jq: error: syntax error"
if !strings.Contains(err.Error(), expect) {
t.Fatalf("expect '%s' in err: %s", expect, err)
}
expect = "compile error"
if !strings.Contains(err.Error(), expect) {
t.Fatalf("expect '%s' in err: %s", expect, err)
}
expect = "0 0 0" // {0 0 0 0 [0 0 0 0 0 0 0 0]} problem
if strings.Contains(err.Error(), expect) {
t.Fatalf("not expect '%s' in err: %s", expect, err)
}
}
func Test_RunError(t *testing.T) {
_, err := Jq().Program(".foo[] | keys").Run(`{"foo":"bar"}`)
if err == nil {
t.Fatal("expect program should fail")
}
expect := "Cannot iterate over string"
if !strings.Contains(err.Error(), expect) {
t.Fatalf("expect '%s' in err: %s", expect, err)
}
}