forked from fiatjaf/jiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jq_test.go
56 lines (42 loc) · 1.13 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
package jiq
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJqQueries(t *testing.T) {
var assert = assert.New(t)
res, _ := jqrun(".a", `{"a": 23}`, nil)
assert.Equal("23", res)
res, _ = jqrun(`.a | {pli: ., plu: [12, "I have \(.) horses"]}`, `{"a": 23}`, nil)
assert.Equal(`{
"pli": 23,
"plu": [
12,
"I have 23 horses"
]
}`, res)
res, _ = jqrun(".a[].w", `{"a": [{"w": 18}, {"w": false}]}`, nil)
assert.Equal("18\nfalse", res)
res, _ = jqrun(".a", `{"a": [{"w": 18}, {"w": false}]}`, []string{"-c"})
assert.Equal(`[{"w":18},{"w":false}]`, res)
}
func TestJqModules(t *testing.T) {
var assert = assert.New(t)
dir, err := ioutil.TempDir("", "")
if !assert.NoError(err, "error creating tempdir") {
return
}
defer os.RemoveAll(dir)
content := []byte(`def hello(f): f ;`)
err = ioutil.WriteFile(filepath.Join(dir, ".jq"), content, 0666)
if !assert.NoError(err, "error creating tempfile") {
return
}
defer os.Setenv("HOME", os.Getenv("HOME"))
os.Setenv("HOME", dir)
res, _ := jqrun("hello(.hi)", `{"hi": "world"}`, nil)
assert.Equal(`"world"`, res)
}