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.
@@ -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
- t.pattern = "unit/*_spec.rb"
8
- t.rspec_opts = "--format s --color --backtrace"
7
+ t.pattern = "unit/*_spec.rb"
8
+ t.rspec_opts = "--format s --color --backtrace"
9
9
  end
10
10
 
11
- task :default => :test
11
+ task default: :test
@@ -5,6 +5,5 @@ require "mocha"
5
5
  require File.dirname(__FILE__) + "/../lib/jgrep"
6
6
 
7
7
  RSpec.configure do |config|
8
- config.mock_with :mocha
8
+ config.mock_with :mocha
9
9
  end
10
-
@@ -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
- describe JGrep do
7
- describe "#jgrep" do
8
-
9
- it "should return a valid json document" do
10
- result = JGrep::jgrep("[{\"foo\":1}]", "foo=1")
11
- result.should == [{"foo"=>1}]
12
- end
13
-
14
- it "should fail on an invalid json document" do
15
- STDERR.expects(:puts).with("Error. Invalid JSON given")
16
- result = JGrep::jgrep("[foo:]", "foo=1")
17
- end
18
-
19
- it "should return '[]' if value is not present in document" do
20
- result = JGrep::jgrep("[{\"bar\":1}]", "foo=1")
21
- result.should == []
22
- end
23
-
24
- it "should correctly return 'null' if a null value is present in the document" do
25
- result = JGrep::jgrep("[{\"foo\":null}]", "foo=null")
26
- result.should == [{"foo" => nil}]
27
- end
28
-
29
- it "should return the origional json document if no expression is given" do
30
- result = JGrep::jgrep("[{\"foo\":\"bar\"}]", "")
31
- result.should == [{"foo" => "bar"}]
32
- end
33
-
34
- it "should filter on the origional json document if not expression is given and a filter is given" do
35
- result = JGrep::jgrep("[{\"foo\":\"bar\"}]", "", "foo")
36
- result.should == ["bar"]
37
- end
38
-
39
- it "should support starting from a subdocument" do
40
- doc = %q(
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
- JGrep.verbose_on
49
- results = JGrep::jgrep(doc, "foo=bar", nil, "results")
50
- results.should == [{"foo"=>"bar"}]
51
- end
52
- end
53
-
54
- describe "#format" do
55
-
56
- it "should correctly format integers" do
57
- result1, result2 = JGrep::format("1",1)
58
- result1.is_a?(Fixnum).should == true
59
- result2.is_a?(Fixnum).should == true
60
- end
61
-
62
- it "should correctly format floating point numbers" do
63
- result1, result2 = JGrep::format("1.1", 1.1)
64
- result1.is_a?(Float).should == true
65
- result2.is_a?(Float).should == true
66
- end
67
-
68
- it "should not format strings" do
69
- result1, result2 = JGrep::format("foo", "bar")
70
- result1.is_a?(String).should == true
71
- result2.is_a?(String).should == true
72
- end
73
- end
74
-
75
- describe "#has_object?" do
76
-
77
- it "should compare on a '=' operator" do
78
- result = JGrep::has_object?({"foo"=> 1}, "foo=1")
79
- result.should == true
80
- end
81
-
82
- it "should compare on a '<=' operator" do
83
- result = JGrep::has_object?({"foo"=> 1}, "foo<=0")
84
- result.should == false
85
- end
86
-
87
- it "should compare on a '>=' operator" do
88
- result = JGrep::has_object?({"foo"=> 1}, "foo>=0")
89
- result.should == true
90
- end
91
-
92
- it "should compare on a '<' operator" do
93
- result = JGrep::has_object?({"foo"=> 1}, "foo<1")
94
- result.should == false
95
- end
96
-
97
- it "should compare on a '>' operator" do
98
- result = JGrep::has_object?({"foo"=> 1}, "foo>0")
99
- result.should == true
100
- end
101
-
102
- it "should compare based on regular expression" do
103
- result = JGrep::has_object?({"foo"=> "bar"}, "foo=/ba/")
104
- result.should == true
105
- end
106
-
107
- it "should compare true booleans" do
108
- result = JGrep::has_object?({"foo"=> true}, "foo=true")
109
- result.should == true
110
- result = JGrep::has_object?({"foo"=> false}, "foo=true")
111
- result.should == false
112
- end
113
-
114
- it "should compare true booleans" do
115
- result = JGrep::has_object?({"foo"=> false}, "foo=false")
116
- result.should == true
117
- result = JGrep::has_object?({"foo"=> true}, "foo=false")
118
- result.should == false
119
- end
120
- end
121
-
122
- describe "#is_object_in_array?" do
123
-
124
- it "should return true if key=value is present in array" do
125
- result = JGrep::is_object_in_array?([{"foo" => 1},{"foo" => 0}], "foo=1")
126
- result.should == true
127
- end
128
-
129
- it "should return false if key=value is not present in array" do
130
- result = JGrep::is_object_in_array?([{"foo" => 1},{"foo" => 0}], "foo=2")
131
- result.should == false
132
- end
133
- end
134
-
135
- describe "#has_complex?" do
136
-
137
- it "should return true if complex statement is present in an array" do
138
- result = JGrep::has_complex?({"foo" => ["bar" => 1]}, [["statement","foo.bar=1"]])
139
- result.should == true
140
- end
141
-
142
- it "should return false if complex statement is not present in an array" do
143
- result = JGrep::has_complex?({"foo" => ["bar" => 1]}, [["statement","foo.bar=0"]])
144
- result.should == false
145
- end
146
- end
147
-
148
- describe "#eval_statement" do
149
-
150
- it "should return true if if document matches logical expression" do
151
- result = JGrep::eval_statement({"foo" => 1, "bar" => 1}, [{"statement" => "foo=1"}, {"and" => "and"}, {"statement" => "bar=1"}])
152
- result.should == true
153
- end
154
-
155
- it "should return true if if document matches logical expression array" do
156
- result = JGrep::eval_statement({"foo" => ["bar" => 1]}, [{"statement" => [["statement", "foo.bar=1"]]}] )
157
- result.should == true
158
- end
159
-
160
- it "should return false if if document doesn't match logical expression" do
161
- result = JGrep::eval_statement({"foo" => 1, "bar" => 1}, [{"statement" => "foo=0"}, {"and" => "and"}, {"statement" => "bar=1"}])
162
- result.should == false
163
- end
164
- end
165
-
166
- describe "#filter_json" do
167
- it "should return the correct values if there is a single filter" do
168
- result = JGrep::filter_json([{"foo" => 1, "bar" => 1}], "foo")
169
- result.should == [1]
170
- end
171
-
172
- it "should return the correct values if there are multiple filters" do
173
- result = JGrep::filter_json([{"foo" => 1, "foo1" => 1, "foo2" => 1}], ["foo2", "foo1"])
174
- result.should == [{"foo2"=>1, "foo1"=>1}]
175
- end
176
-
177
- it "should return an empty set if the filter has not been found and there is only 1 filter" do
178
- result = JGrep::filter_json([{"foo" => 1}], "bar")
179
- result.should == []
180
- end
181
-
182
- it "should not return a structure containing a key if that key is not specified in the document" do
183
- result = JGrep::filter_json([{"foo" => 1}], ["foo", "bar"])
184
- result.should == [{"foo" => 1}]
185
- end
186
- end
187
-
188
- describe "#validate_filters" do
189
-
190
- it "should validate correct single filter" do
191
- result = JGrep::validate_filters("foo")
192
- result.should be_nil
193
- end
194
-
195
- it "should not validate if a single filter contains an invalid field" do
196
- expect{
197
- result = JGrep::validate_filters("and")
198
- }.to raise_error "Invalid field for -s filter : 'and'"
199
- end
200
-
201
- it "should correctly validate an array of filters" do
202
- result = JGrep::validate_filters(["foo", "bar"])
203
- result.should be_nil
204
- end
205
-
206
- it "should not validate if an array of filters contain an illegal filter" do
207
- expect{
208
- result = JGrep::validate_filters(["foo", "or"])
209
- }.to raise_error "Invalid field for -s filter : 'or'"
210
- end
211
- end
212
-
213
- describe "#dig_path" do
214
-
215
- it "should return the correct key value for a hash" do
216
- result = JGrep::dig_path({"foo" => 1}, "foo")
217
- result.should == 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
- result.should == 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
- result.should == [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
- result.should == [[{"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
- 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