jgrep 1.4.1 → 1.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.markdown +18 -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 +239 -233
- data/spec/unit/parser_spec.rb +132 -127
- data/spec/unit/scanner_spec.rb +88 -86
- metadata +6 -19
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,249 @@
|
|
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
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
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
|
+
|
80
|
+
it 'should not format strings with a single [^\d\.] character' do
|
81
|
+
result1, result2 = JGrep.format("2012R2", "2008R2")
|
82
|
+
expect(result1).to be_a(String)
|
83
|
+
expect(result2).to be_a(String)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "#has_object?" do
|
88
|
+
it "should compare on a '=' operator" do
|
89
|
+
result = JGrep.has_object?({"foo" => 1}, "foo=1")
|
90
|
+
expect(result).to eq(true)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should compare on a '<=' operator" do
|
94
|
+
result = JGrep.has_object?({"foo" => 1}, "foo<=0")
|
95
|
+
expect(result).to eq(false)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should compare on a '>=' operator" do
|
99
|
+
result = JGrep.has_object?({"foo" => 1}, "foo>=0")
|
100
|
+
expect(result).to eq(true)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should compare on a '<' operator" do
|
104
|
+
result = JGrep.has_object?({"foo" => 1}, "foo<1")
|
105
|
+
expect(result).to eq(false)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should compare on a '>' operator" do
|
109
|
+
result = JGrep.has_object?({"foo" => 1}, "foo>0")
|
110
|
+
expect(result).to eq(true)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should compare based on regular expression" do
|
114
|
+
result = JGrep.has_object?({"foo" => "bar"}, "foo=/ba/")
|
115
|
+
expect(result).to eq(true)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should compare true booleans" do
|
119
|
+
result = JGrep.has_object?({"foo" => true}, "foo=true")
|
120
|
+
expect(result).to eq(true)
|
121
|
+
result = JGrep.has_object?({"foo" => false}, "foo=true")
|
122
|
+
expect(result).to eq(false)
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should compare true booleans" do
|
126
|
+
result = JGrep.has_object?({"foo" => false}, "foo=false")
|
127
|
+
expect(result).to eq(true)
|
128
|
+
result = JGrep.has_object?({"foo" => true}, "foo=false")
|
129
|
+
expect(result).to eq(false)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe "#is_object_in_array?" do
|
134
|
+
it "should return true if key=value is present in array" do
|
135
|
+
result = JGrep.is_object_in_array?([{"foo" => 1}, {"foo" => 0}], "foo=1")
|
136
|
+
expect(result).to eq(true)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should return false if key=value is not present in array" do
|
140
|
+
result = JGrep.is_object_in_array?([{"foo" => 1}, {"foo" => 0}], "foo=2")
|
141
|
+
expect(result).to eq(false)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "#has_complex?" do
|
146
|
+
it "should return true if complex statement is present in an array" do
|
147
|
+
result = JGrep.has_complex?({"foo" => ["bar" => 1]}, [["statement", "foo.bar=1"]])
|
148
|
+
expect(result).to eq(true)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should return false if complex statement is not present in an array" do
|
152
|
+
result = JGrep.has_complex?({"foo" => ["bar" => 1]}, [["statement", "foo.bar=0"]])
|
153
|
+
expect(result).to eq(false)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "#eval_statement" do
|
158
|
+
it "should return true if if document matches logical expression" do
|
159
|
+
result = JGrep.eval_statement({"foo" => 1, "bar" => 1}, [{"statement" => "foo=1"}, {"and" => "and"}, {"statement" => "bar=1"}])
|
160
|
+
expect(result).to eq(true)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should return true if if document matches logical expression array" do
|
164
|
+
result = JGrep.eval_statement({"foo" => ["bar" => 1]}, [{"statement" => [["statement", "foo.bar=1"]]}])
|
165
|
+
expect(result).to eq(true)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should return false if if document doesn't match logical expression" do
|
169
|
+
result = JGrep.eval_statement({"foo" => 1, "bar" => 1}, [{"statement" => "foo=0"}, {"and" => "and"}, {"statement" => "bar=1"}])
|
170
|
+
expect(result).to eq(false)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe "#filter_json" do
|
175
|
+
it "should return the correct values if there is a single filter" do
|
176
|
+
result = JGrep.filter_json([{"foo" => 1, "bar" => 1}], "foo")
|
177
|
+
expect(result).to eq([1])
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should return the correct values if there are multiple filters" do
|
181
|
+
result = JGrep.filter_json([{"foo" => 1, "foo1" => 1, "foo2" => 1}], %w[foo2 foo1])
|
182
|
+
expect(result).to eq([{"foo2" => 1, "foo1" => 1}])
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should return an empty set if the filter has not been found and there is only 1 filter" do
|
186
|
+
result = JGrep.filter_json([{"foo" => 1}], "bar")
|
187
|
+
expect(result).to eq([])
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should not return a structure containing a key if that key is not specified in the document" do
|
191
|
+
result = JGrep.filter_json([{"foo" => 1}], %w[foo bar])
|
192
|
+
expect(result).to eq([{"foo" => 1}])
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe "#validate_filters" do
|
197
|
+
it "should validate correct single filter" do
|
198
|
+
result = JGrep.validate_filters("foo")
|
199
|
+
expect(result).to be_nil
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should not validate if a single filter contains an invalid field" do
|
203
|
+
expect do
|
204
|
+
JGrep.validate_filters("and")
|
205
|
+
end.to raise_error "Invalid field for -s filter : 'and'"
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should correctly validate an array of filters" do
|
209
|
+
result = JGrep.validate_filters(%w[foo bar])
|
210
|
+
expect(result).to be_nil
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should not validate if an array of filters contain an illegal filter" do
|
214
|
+
expect do
|
215
|
+
JGrep.validate_filters(%w[foo or])
|
216
|
+
end.to raise_error "Invalid field for -s filter : 'or'"
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
describe "#dig_path" do
|
221
|
+
it "should return the correct key value for a hash" do
|
222
|
+
result = JGrep.dig_path({"foo" => 1}, "foo")
|
223
|
+
expect(result).to eq(1)
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should return the correct value for any value that is not a hash or an array" do
|
227
|
+
result = JGrep.dig_path(1, "foo")
|
228
|
+
expect(result).to eq(1)
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should return the correct value for a subvalue in an array" do
|
232
|
+
result = JGrep.dig_path([{"foo" => 1}, {"foo" => 2}], "foo")
|
233
|
+
expect(result).to eq([1, 2])
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should return the correct value if a wildcard is specified" do
|
237
|
+
result = JGrep.dig_path([{"foo" => {"bar" => 1}}], "foo.*")
|
238
|
+
expect(result).to eq([[{"bar" => 1}]])
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should return the correct value if the path contains a dot seperated key" do
|
242
|
+
result = JGrep.dig_path({"foo.bar" => 1}, "foo.bar")
|
243
|
+
expect(result).to eq(1)
|
244
|
+
result = JGrep.dig_path({"foo" => {"foo.bar" => 1}}, "foo.foo.bar")
|
245
|
+
expect(result).to eq(1)
|
246
|
+
end
|
242
247
|
end
|
248
|
+
end
|
243
249
|
end
|