-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_spec.rb
322 lines (258 loc) · 8.43 KB
/
json_spec.rb
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
describe 'JSON filter' do
context 'with -t option' do
describe 'Output string' do
let(:input) { load_fixture('json/string.json') }
let(:output) { '"test"' }
before { run_rf('-t json _', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end
context 'with -r option' do
describe 'Output string' do
let(:input) { load_fixture('json/string.json') }
let(:args) { '-j -r _' }
let(:expect_output) { "test\n" }
it_behaves_like 'a successful exec'
end
# When using pipes, `--no-color` is implicitly applied internally, so we explicitly test for it.
context 'with --color option' do
let(:input) { load_fixture('json/string.json') }
let(:args) { '-j -r --color _' }
let(:expect_output) { "test\n" }
it_behaves_like 'a successful exec'
end
end
context 'with --disable-boolean-mode option' do
let(:input) { '"foobar"' }
where do
{
'TrueClass' => {
command: 'true',
output: 'true'
},
'FalseClass' => {
command: 'false',
output: 'false'
},
'NilClass' => {
command: 'nil',
output: 'null'
}
}
end
with_them do
before { run_rf("-j --disable-boolean-mode '#{command}'", input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end
context 'when use -H option' do
let(:input) { '"foobar"' }
let(:args) { '-j -H --no-color true testfile' }
let(:expect_output) do
out = input.split("\n").map { |line| "testfile:#{line}" }.join("\n")
"#{out}\n"
end
before do
write_file 'testfile', input
end
it_behaves_like 'a successful exec'
end
context 'with -R option' do
let(:output) do
<<~OUTPUT
"foobarbaz"
"foo"
OUTPUT
end
before do
write_file('foo.json', '"foo"')
write_file('notmatch.txt', '"not match"')
write_file('notmatch.yml', '"not match"')
FileUtils.mkdir_p(expand_path('a/b'))
write_file('foo/bar/baz.json', '"foobarbaz"')
write_file('foo/bar/notmatch.txt', '"not match"')
write_file('foo/bar/notmatch.yml', '"not match"')
run_rf('-j -R _ .')
end
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
context 'with -g option' do
let(:input) { '"foo"' }
let(:output) { '"foo"' }
where(:command) do
%w[-g --grep]
end
with_them do
before { run_rf("-j #{command} .", input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output_on_stdout output_string_eq output }
end
end
context 'when multiple files' do
let(:input) { '"foobar"' }
where do
{
'without -H option' => {
option: '',
expect_output: <<~OUTPUT
"foobar"
"foobar"
OUTPUT
},
'with -H option' => {
option: '-H',
expect_output: <<~OUTPUT
testfile1:"foobar"
testfile2:"foobar"
OUTPUT
}
}
end
with_them do
let(:args) { "-j #{option} --no-color true testfile1 testfile2" }
before do
write_file 'testfile1', input
write_file 'testfile2', input
end
it_behaves_like 'a successful exec'
end
end
context 'when input from stdin' do
describe 'Output string' do
let(:input) { load_fixture('json/string.json') }
let(:output) { '"test"' }
before { run_rf('-j _', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
describe 'Output each object of the array one by one' do
let(:input) { load_fixture('json/array.json') }
let(:output) do
<<~OUTPUT
"foo"
"bar"
"baz"
OUTPUT
end
before { run_rf('-j _', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
describe 'Output only the filtered objects' do
let(:input) { load_fixture('json/array.json') }
let(:output) { '"foo"' }
before { run_rf('-j /foo/', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
describe 'Output the value of the selected Hash key' do
let(:input) { load_fixture('json/hash.json') }
let(:output) do
<<~OUTPUT
[
"a",
"b",
"c"
]
OUTPUT
end
before { run_rf('-j "_.bar.baz"', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
describe 'Output the value of the selected Hash space included key' do
let(:input) { load_fixture('json/hash.json') }
let(:output) { '"foo bar"' }
before { run_rf(%q(-j '_["foo bar"]'), input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
describe 'Read all at once' do
let(:input) { load_fixture('json/hash.json') }
let(:output) do
'[{"foo"=>1, "bar"=>{"baz"=>["a", "b", "c"]}, "foo bar"=>"foo bar"}]'
end
before { run_rf("-j -s -q 'p _'", input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end
context 'when input from file' do
describe 'Output string' do
let(:file) { 'test.json' }
let(:input) { load_fixture('json/string.json') }
let(:output) { '"test"' }
before do
write_file file, input
run_rf("-j _ #{file}")
end
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end
context 'when suppress automatic printing' do
describe 'Output string' do
let(:input) { load_fixture('json/string.json') }
let(:output) { '' }
before { run_rf('-j -q _', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end
context 'when use regexp' do
describe 'Input as String' do
let(:input) { load_fixture('json/string.json') }
let(:output) { '"test"' }
before { run_rf('-j /test/', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
describe 'Input as Number' do
let(:input) { load_fixture('json/number.json') }
let(:output) { '123456789' }
before { run_rf('-j /123456789/', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
describe 'Input as Hash' do
let(:input) { '{"foo": "bar"}' }
let(:output) do
<<~OUTPUT
{
"foo": "bar"
}
OUTPUT
end
before { run_rf('-j /foo/', input) }
it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end
context 'when inptut is invalid JSON' do
describe 'Error message' do
let(:input) { '{"foo": "bar"' }
let(:args) { '-j _' }
let(:expect_output) do
"Error: failed to parse JSON: unexpected end of data position: 14\n"
end
it_behaves_like 'a failed exec'
end
end
context 'with duplicate keys in the input object' do
describe 'Output string' do
let(:input) { '{"foo": "bar", "foo": "baz"}' }
let(:args) { '-j _' }
let(:expect_output) do
<<~JSON
{
"foo": "baz"
}
JSON
end
it_behaves_like 'a successful exec'
end
end
end