-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_test.go
148 lines (122 loc) · 4.05 KB
/
program_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package jqx
import (
"bytes"
"fmt"
"io/fs"
"slices"
"strings"
"testing"
"testing/fstest"
)
func testRun(t *testing.T, stdin, want string, p *Program) {
t.Helper()
var got bytes.Buffer
p.Stdin = bytes.NewBufferString(strings.ReplaceAll(stdin, " ", "\n"))
p.Println = func(s string) { fmt.Fprintln(&got, s) }
err := p.Main()
if err != nil {
t.Log(err)
if want != "error" {
t.Fail()
}
return
}
want = strings.ReplaceAll(want, " ", "\n") + "\n"
assertEqual(t, got.String(), want)
}
func TestProgram(t *testing.T) {
testRun(t, "[] []", "[] []", &Program{})
testRun(t, "[] [}", "error", &Program{})
testRun(t, "[] []", "{}", &Program{StdinIsTerminal: true})
testRun(t, "[10]", "[10]", &Program{})
testRun(t, "[10]", "[10]", &Program{Args: []string{"-j"}})
testRun(t, "[10]", "[10]", &Program{Args: []string{"-j"}, StdoutIsTerminal: true})
testRun(t, "[10]", "[\n\t10\n]", &Program{StdoutIsTerminal: true})
testRun(t, "[10]", "[\n\t10\n]", &Program{Args: []string{"-t"}})
testRun(t, "[10]", "[\n\t10\n]", &Program{Args: []string{"-t", "-j"}})
testRun(t, `"a"`, `a`, &Program{})
testRun(t, `"a"`, `a`, &Program{StdoutIsTerminal: true})
testRun(t, `"a"`, `"a"`, &Program{Args: []string{"-j"}})
testRun(t, "[10]", "[10]", &Program{})
testRun(t, "[10]", "[10]", &Program{Args: []string{"."}})
testRun(t, "[10]", "10", &Program{Args: []string{".[]"}})
testRun(t, "[10]", "error", &Program{Args: []string{"|"}})
testRun(t, `] })`, `] })`, &Program{Args: []string{"-r"}})
testRun(t, `] })`, `] [1] }) [2]`, &Program{Args: []string{"-r", "., [length]"}})
testRun(t, `null`, "env files", &Program{Args: []string{"-e", "$vars | keys[]"}})
testRun(t, `null`, "files", &Program{Args: []string{"$vars | keys[]"}})
t.Setenv("XYZ", "_____")
testRun(t, `"XYZ"`, "_____", &Program{Args: []string{"-e", "$env[.]"}})
testRun(t, `"XYZ"`, "_____", &Program{Args: []string{"-e", "$vars.env[.]"}})
testRun(t, `"XYZ"`, "error", &Program{Args: []string{"$env[.]"}})
}
func TestOpen(t *testing.T) {
testFiles := map[string]any{
"a.json": "[1][2][3]",
"b.json": "[1,2,3]",
"c.notjson": "[}",
"d.txt": "foo",
"e.txt": "q\nw\ne\nr\nt\ny",
}
p := Program{StdinIsTerminal: true}
testRun(t, "", "{}", &p)
p.Open = toFS(testFiles, nil).Open
testRun(t, "", "{}", &p)
p.Args = []string{`"a.json" | $files[.][][]`, "a.json"}
testRun(t, "", "1 2 3", &p)
p.Args = []string{`.[][][]`, "a.json"}
testRun(t, "", "1 2 3", &p)
p.Args = []string{`.[][]`, "b.json"}
testRun(t, "", "1 2 3", &p)
for _, file := range []string{"c.json", "c.notjson", "d.txt", "e.txt"} {
p.Args = []string{".", file}
testRun(t, "", "error", &p)
}
p.Args = []string{"-r", ".[][] | .+.", "d.txt", "e.txt"}
testRun(t, "", "foofoo qq ww ee rr tt yy", &p)
}
func TestFS(t *testing.T) {
testfs := fstest.MapFS{
"x/d": &fstest.MapFile{Mode: fs.ModeDir},
"x/dd/a/d": &fstest.MapFile{Mode: fs.ModeDir},
"x/f": &fstest.MapFile{Data: []byte("file")},
"x/ff/a/f": &fstest.MapFile{Data: []byte("file")},
}
p := Program{StdinIsTerminal: true}
p.Find = func(s string) fs.FS {
return must(fs.Sub(testfs, s))
}
p.Args = []string{`$find | keys[]`}
testRun(t, "", "error", &p)
p.Args = slices.Insert(p.Args, 0, "-find", "x")
testRun(t, "", "d/ dd/ dd/a/ dd/a/d/ f ff/ ff/a/ ff/a/f", &p)
}
func TestDry(t *testing.T) {
const q = `snapshot("\(.).json"; [.])`
p := Program{}
ls := func() string {
rt := must(fs.Glob(p.OutFS, "*"))
return strings.Join(rt, " ")
}
cat := func(filename string) string {
data, err := fs.ReadFile(p.OutFS, filename)
if err != nil {
return "error"
}
return string(data)
}
for range 3 {
p.Args = []string{"--dry-run", q}
testRun(t, `false`, `false false.json`, &p)
assertEqual(t, ls(), "")
assertEqual(t, cat("false.json"), "error")
p.Args = []string{q}
testRun(t, `false`, `false`, &p)
assertEqual(t, ls(), "false.json")
assertEqual(t, cat("false.json"), "[false]")
p.Args = []string{"-t", q}
testRun(t, `false`, `false`, &p)
assertEqual(t, ls(), "false.json")
assertEqual(t, cat("false.json"), "[\n\tfalse\n]")
}
}