jgrep 1.4.1 → 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,249 @@
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
+
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