-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_jq_test.go
101 lines (76 loc) · 2.75 KB
/
go_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"io"
"os"
"strings"
"testing"
"golang.org/x/exp/slices"
)
func captureParseJsonAndFindConsoleOutput(input io.Reader, path []string) string {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
parseJsonAndFind(input, path)
w.Close()
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout
return string(out)
}
func TestParseJsonAndFind(t *testing.T) {
t.Run("for valid json input and path set as '.repo' it returns propers data", func(t *testing.T) {
path := argsToPath([]string{"./go-jq", ".repo"})
input := strings.NewReader("{\"id\":\"123\",\"type\":\"event\",\"repo\":{\"id\":\"2222\",\"type\":\"private\"},\"events\":[{\"id\":\"1\"},{\"id\":\"2\"}],\"test\":[[1,2,3],[4,5,6]]}")
got := captureParseJsonAndFindConsoleOutput(input, path)
want := "{\n \"id\": \"2222\",\n \"type\": \"private\"\n}\n"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("for valid json input and path set as '.repo.type' it returns propers data", func(t *testing.T) {
path := argsToPath([]string{"./go-jq", ".repo.type"})
input := strings.NewReader("{\"id\":\"123\",\"type\":\"event\",\"repo\":{\"id\":\"2222\",\"type\":\"private\"},\"events\":[{\"id\":\"1\"},{\"id\":\"2\"}],\"test\":[[1,2,3],[4,5,6]]}")
got := captureParseJsonAndFindConsoleOutput(input, path)
want := "\"private\"\n"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("for valid json input and path set as '.test[0]' it returns propers data", func(t *testing.T) {
path := argsToPath([]string{"./go-jq", ".test[0]"})
input := strings.NewReader("{\"id\":\"123\",\"type\":\"event\",\"repo\":{\"id\":\"2222\",\"type\":\"private\"},\"events\":[{\"id\":\"1\"},{\"id\":\"2\"}],\"test\":[[1,2,3],[4,5,6]]}")
got := captureParseJsonAndFindConsoleOutput(input, path)
want := "[\n 1,\n 2,\n 3\n]\n"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("for invalid json input it returns error message", func(t *testing.T) {
path := argsToPath([]string{"./go-jq"})
input := strings.NewReader("{ invalid: }")
got := captureParseJsonAndFindConsoleOutput(input, path)
want := "invalid json\n"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
}
func TestArgsToPath(t *testing.T) {
args := []string{"./go-jq", ".repo"}
got := argsToPath(args)
want := []string{"repo"}
if !slices.Equal(got, want) {
t.Errorf("got %q, want %q", got, want)
}
args = []string{"/go-jq", ".repo.type"}
got = argsToPath(args)
want = []string{"repo", "type"}
if !slices.Equal(got, want) {
t.Errorf("got %q, want %q", got, want)
}
args = []string{"/go-jq", ".test[0]"}
got = argsToPath(args)
want = []string{"test", "0"}
if !slices.Equal(got, want) {
t.Errorf("got %q, want %q", got, want)
}
}