forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_test.go
More file actions
85 lines (82 loc) · 1.68 KB
/
filter_test.go
File metadata and controls
85 lines (82 loc) · 1.68 KB
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
package export
import (
"bytes"
"io"
"strings"
"testing"
"github.com/MakeNowJust/heredoc"
)
func Test_filterJSON(t *testing.T) {
type args struct {
json io.Reader
query string
}
tests := []struct {
name string
args args
wantW string
wantErr bool
}{
{
name: "simple",
args: args{
json: strings.NewReader(`{"name":"Mona", "arms":8}`),
query: `.name`,
},
wantW: "Mona\n",
},
{
name: "multiple queries",
args: args{
json: strings.NewReader(`{"name":"Mona", "arms":8}`),
query: `.name,.arms`,
},
wantW: "Mona\n8\n",
},
{
name: "object as JSON",
args: args{
json: strings.NewReader(`{"user":{"login":"monalisa"}}`),
query: `.user`,
},
wantW: "{\"login\":\"monalisa\"}\n",
},
{
name: "complex",
args: args{
json: strings.NewReader(heredoc.Doc(`[
{
"title": "First title",
"labels": [{"name":"bug"}, {"name":"help wanted"}]
},
{
"title": "Second but not last",
"labels": []
},
{
"title": "Alas, tis' the end",
"labels": [{}, {"name":"feature"}]
}
]`)),
query: `.[] | [.title,(.labels | map(.name) | join(","))] | @tsv`,
},
wantW: heredoc.Doc(`
First title bug,help wanted
Second but not last
Alas, tis' the end ,feature
`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := FilterJSON(w, tt.args.json, tt.args.query); (err != nil) != tt.wantErr {
t.Errorf("filterJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("filterJSON() = %q, want %q", gotW, tt.wantW)
}
})
}
}