jgrep 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.markdown +5 -0
- data/README.markdown +13 -12
- data/Rakefile +6 -1
- data/bin/jgrep +115 -129
- data/lib/jgrep.rb +286 -377
- data/lib/parser/parser.rb +109 -125
- data/lib/parser/scanner.rb +148 -149
- data/spec/Rakefile +3 -3
- data/spec/spec_helper.rb +1 -2
- data/spec/unit/jgrep_spec.rb +233 -233
- data/spec/unit/parser_spec.rb +132 -127
- data/spec/unit/scanner_spec.rb +82 -86
- metadata +6 -18
data/spec/Rakefile
CHANGED
@@ -4,8 +4,8 @@ require "rspec/core/rake_task"
|
|
4
4
|
|
5
5
|
desc "Run JGrep tests"
|
6
6
|
RSpec::Core::RakeTask.new(:test) do |t|
|
7
|
-
|
8
|
-
|
7
|
+
t.pattern = "unit/*_spec.rb"
|
8
|
+
t.rspec_opts = "--format s --color --backtrace"
|
9
9
|
end
|
10
10
|
|
11
|
-
task :
|
11
|
+
task default: :test
|
data/spec/spec_helper.rb
CHANGED
data/spec/unit/jgrep_spec.rb
CHANGED
@@ -1,243 +1,243 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
1
|
require File.dirname(__FILE__) + "/../spec_helper"
|
4
2
|
|
5
3
|
module JGrep
|
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
|
-
|
4
|
+
describe JGrep do
|
5
|
+
describe "#validate_expression" do
|
6
|
+
it "should be true for valid expressions" do
|
7
|
+
expect(JGrep.validate_expression("bob=true")).to be(true)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return errors for invalid ones" do
|
11
|
+
expect(JGrep.validate_expression("something that is invalid")).to start_with("Error")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#jgrep" do
|
16
|
+
it "should return a valid json document" do
|
17
|
+
result = JGrep.jgrep("[{\"foo\":1}]", "foo=1")
|
18
|
+
expect(result).to eq([{"foo" => 1}])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should fail on an invalid json document" do
|
22
|
+
STDERR.expects(:puts).with("Error. Invalid JSON given")
|
23
|
+
JGrep.jgrep("[foo:]", "foo=1")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return '[]' if value is not present in document" do
|
27
|
+
result = JGrep.jgrep("[{\"bar\":1}]", "foo=1")
|
28
|
+
expect(result).to eq([])
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should correctly return 'null' if a null value is present in the document" do
|
32
|
+
result = JGrep.jgrep("[{\"foo\":null}]", "foo=null")
|
33
|
+
expect(result).to eq([{"foo" => nil}])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return the origional json document if no expression is given" do
|
37
|
+
result = JGrep.jgrep("[{\"foo\":\"bar\"}]", "")
|
38
|
+
expect(result).to eq([{"foo" => "bar"}])
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should filter on the origional json document if not expression is given and a filter is given" do
|
42
|
+
result = JGrep.jgrep("[{\"foo\":\"bar\"}]", "", "foo")
|
43
|
+
expect(result).to eq(["bar"])
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should support starting from a subdocument" do
|
47
|
+
doc = '
|
41
48
|
{"results": [
|
42
49
|
{"foo":"bar"},
|
43
50
|
{"foo":"baz"}
|
44
51
|
]
|
45
52
|
}
|
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
|
-
it "should return the correct value if the path contains a dot seperated key" do
|
236
|
-
result = JGrep::dig_path({"foo.bar" => 1}, "foo.bar")
|
237
|
-
result.should == 1
|
238
|
-
result = JGrep::dig_path({"foo" => {"foo.bar" =>1}}, "foo.foo.bar")
|
239
|
-
result.should == 1
|
240
|
-
end
|
241
|
-
end
|
53
|
+
'
|
54
|
+
|
55
|
+
JGrep.verbose_on
|
56
|
+
results = JGrep.jgrep(doc, "foo=bar", nil, "results")
|
57
|
+
expect(results).to eq([{"foo" => "bar"}])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#format" do
|
62
|
+
it "should correctly format integers" do
|
63
|
+
result1, result2 = JGrep.format("1", 1)
|
64
|
+
expect(result1.is_a?(Integer)).to eq(true)
|
65
|
+
expect(result2.is_a?(Integer)).to eq(true)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should correctly format floating point numbers" do
|
69
|
+
result1, result2 = JGrep.format("1.1", 1.1)
|
70
|
+
expect(result1.is_a?(Float)).to eq(true)
|
71
|
+
expect(result2.is_a?(Float)).to eq(true)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should not format strings" do
|
75
|
+
result1, result2 = JGrep.format("foo", "bar")
|
76
|
+
expect(result1.is_a?(String)).to eq(true)
|
77
|
+
expect(result2.is_a?(String)).to eq(true)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#has_object?" do
|
82
|
+
it "should compare on a '=' operator" do
|
83
|
+
result = JGrep.has_object?({"foo" => 1}, "foo=1")
|
84
|
+
expect(result).to eq(true)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should compare on a '<=' operator" do
|
88
|
+
result = JGrep.has_object?({"foo" => 1}, "foo<=0")
|
89
|
+
expect(result).to eq(false)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should compare on a '>=' operator" do
|
93
|
+
result = JGrep.has_object?({"foo" => 1}, "foo>=0")
|
94
|
+
expect(result).to eq(true)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should compare on a '<' operator" do
|
98
|
+
result = JGrep.has_object?({"foo" => 1}, "foo<1")
|
99
|
+
expect(result).to eq(false)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should compare on a '>' operator" do
|
103
|
+
result = JGrep.has_object?({"foo" => 1}, "foo>0")
|
104
|
+
expect(result).to eq(true)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should compare based on regular expression" do
|
108
|
+
result = JGrep.has_object?({"foo" => "bar"}, "foo=/ba/")
|
109
|
+
expect(result).to eq(true)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should compare true booleans" do
|
113
|
+
result = JGrep.has_object?({"foo" => true}, "foo=true")
|
114
|
+
expect(result).to eq(true)
|
115
|
+
result = JGrep.has_object?({"foo" => false}, "foo=true")
|
116
|
+
expect(result).to eq(false)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should compare true booleans" do
|
120
|
+
result = JGrep.has_object?({"foo" => false}, "foo=false")
|
121
|
+
expect(result).to eq(true)
|
122
|
+
result = JGrep.has_object?({"foo" => true}, "foo=false")
|
123
|
+
expect(result).to eq(false)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "#is_object_in_array?" do
|
128
|
+
it "should return true if key=value is present in array" do
|
129
|
+
result = JGrep.is_object_in_array?([{"foo" => 1}, {"foo" => 0}], "foo=1")
|
130
|
+
expect(result).to eq(true)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should return false if key=value is not present in array" do
|
134
|
+
result = JGrep.is_object_in_array?([{"foo" => 1}, {"foo" => 0}], "foo=2")
|
135
|
+
expect(result).to eq(false)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe "#has_complex?" do
|
140
|
+
it "should return true if complex statement is present in an array" do
|
141
|
+
result = JGrep.has_complex?({"foo" => ["bar" => 1]}, [["statement", "foo.bar=1"]])
|
142
|
+
expect(result).to eq(true)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should return false if complex statement is not present in an array" do
|
146
|
+
result = JGrep.has_complex?({"foo" => ["bar" => 1]}, [["statement", "foo.bar=0"]])
|
147
|
+
expect(result).to eq(false)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe "#eval_statement" do
|
152
|
+
it "should return true if if document matches logical expression" do
|
153
|
+
result = JGrep.eval_statement({"foo" => 1, "bar" => 1}, [{"statement" => "foo=1"}, {"and" => "and"}, {"statement" => "bar=1"}])
|
154
|
+
expect(result).to eq(true)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should return true if if document matches logical expression array" do
|
158
|
+
result = JGrep.eval_statement({"foo" => ["bar" => 1]}, [{"statement" => [["statement", "foo.bar=1"]]}])
|
159
|
+
expect(result).to eq(true)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should return false if if document doesn't match logical expression" do
|
163
|
+
result = JGrep.eval_statement({"foo" => 1, "bar" => 1}, [{"statement" => "foo=0"}, {"and" => "and"}, {"statement" => "bar=1"}])
|
164
|
+
expect(result).to eq(false)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe "#filter_json" do
|
169
|
+
it "should return the correct values if there is a single filter" do
|
170
|
+
result = JGrep.filter_json([{"foo" => 1, "bar" => 1}], "foo")
|
171
|
+
expect(result).to eq([1])
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should return the correct values if there are multiple filters" do
|
175
|
+
result = JGrep.filter_json([{"foo" => 1, "foo1" => 1, "foo2" => 1}], %w[foo2 foo1])
|
176
|
+
expect(result).to eq([{"foo2" => 1, "foo1" => 1}])
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should return an empty set if the filter has not been found and there is only 1 filter" do
|
180
|
+
result = JGrep.filter_json([{"foo" => 1}], "bar")
|
181
|
+
expect(result).to eq([])
|
182
|
+
end
|
183
|
+
|
184
|
+
it "should not return a structure containing a key if that key is not specified in the document" do
|
185
|
+
result = JGrep.filter_json([{"foo" => 1}], %w[foo bar])
|
186
|
+
expect(result).to eq([{"foo" => 1}])
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
describe "#validate_filters" do
|
191
|
+
it "should validate correct single filter" do
|
192
|
+
result = JGrep.validate_filters("foo")
|
193
|
+
expect(result).to be_nil
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should not validate if a single filter contains an invalid field" do
|
197
|
+
expect do
|
198
|
+
JGrep.validate_filters("and")
|
199
|
+
end.to raise_error "Invalid field for -s filter : 'and'"
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should correctly validate an array of filters" do
|
203
|
+
result = JGrep.validate_filters(%w[foo bar])
|
204
|
+
expect(result).to be_nil
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should not validate if an array of filters contain an illegal filter" do
|
208
|
+
expect do
|
209
|
+
JGrep.validate_filters(%w[foo or])
|
210
|
+
end.to raise_error "Invalid field for -s filter : 'or'"
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
describe "#dig_path" do
|
215
|
+
it "should return the correct key value for a hash" do
|
216
|
+
result = JGrep.dig_path({"foo" => 1}, "foo")
|
217
|
+
expect(result).to eq(1)
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should return the correct value for any value that is not a hash or an array" do
|
221
|
+
result = JGrep.dig_path(1, "foo")
|
222
|
+
expect(result).to eq(1)
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should return the correct value for a subvalue in an array" do
|
226
|
+
result = JGrep.dig_path([{"foo" => 1}, {"foo" => 2}], "foo")
|
227
|
+
expect(result).to eq([1, 2])
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should return the correct value if a wildcard is specified" do
|
231
|
+
result = JGrep.dig_path([{"foo" => {"bar" => 1}}], "foo.*")
|
232
|
+
expect(result).to eq([[{"bar" => 1}]])
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should return the correct value if the path contains a dot seperated key" do
|
236
|
+
result = JGrep.dig_path({"foo.bar" => 1}, "foo.bar")
|
237
|
+
expect(result).to eq(1)
|
238
|
+
result = JGrep.dig_path({"foo" => {"foo.bar" => 1}}, "foo.foo.bar")
|
239
|
+
expect(result).to eq(1)
|
240
|
+
end
|
242
241
|
end
|
242
|
+
end
|
243
243
|
end
|