rspec-json_expectations 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 635c0cad90f8b318d89952ee50c859dc7bc4110b
4
- data.tar.gz: 430f157ca89a149f6f0c99e655ae6ebe40180ae1
3
+ metadata.gz: 578ce36c168740a6cf33f28aa580b7eb9a3b2bda
4
+ data.tar.gz: b06d98b0d63db3bafa6aeca999c7716de7c92913
5
5
  SHA512:
6
- metadata.gz: 07b925d6f73e3478dae9b094e7bcf8a624c3b814e9d22b6c2b877bc628c9676d05bc628e5a0a344b48d16746184f93a2f578c1555502c9a39be4d79dc39048e1
7
- data.tar.gz: 20ea442c480d27d1af256ea657d206e86f527b38171709aafa3710135565ff0f664a87c8ac981b924ad64d95287f33d2d83c937f9d63ef5d1cf7772c1789dd8e
6
+ metadata.gz: 4531142c7ea3c080f9cb3df97550346f7e9ff5e8bf009b1f1ee29d3ab2d5eb023619b51f69281dec5c9fa093e6b397596bc80e14e105ed7d9a8c5bdf8244d22b
7
+ data.tar.gz: a9ec4e356c69bb1a5ef5c2d5898b21ba95df8d24eff9b41669ee6c2790b0a52b8d45afdf0a2941d9141d74280e427c1e927a4aa4d426b8b9b25f9e871989c76b
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -152,6 +152,25 @@ Feature: Unordered array matching support for include_json matcher
152
152
  1 example, 0 failures
153
153
  """
154
154
 
155
+ Scenario: Expecting json string with array at root to fully include json with arrays using alternative syntax
156
+ Given a file "spec/nested_example_spec.rb" with:
157
+ """ruby
158
+ require "spec_helper"
159
+
160
+ RSpec.describe "A json response" do
161
+ subject { '%{JSON_WITH_ROOT_ARRAY}' }
162
+
163
+ it "has basic info about user" do
164
+ expect(subject).to include_unordered_json ["first flight", "day & night"]
165
+ end
166
+ end
167
+ """
168
+ When I run "rspec spec/nested_example_spec.rb"
169
+ Then I see:
170
+ """
171
+ 1 example, 0 failures
172
+ """
173
+
155
174
  Scenario: Expecting json string with array at root to fully include json with arrays with different order
156
175
  Given a file "spec/nested_example_spec.rb" with:
157
176
  """ruby
@@ -173,6 +192,25 @@ Feature: Unordered array matching support for include_json matcher
173
192
  1 example, 0 failures
174
193
  """
175
194
 
195
+ Scenario: Expecting json string with array at root to fully include json with arrays with different order using alternative syntax
196
+ Given a file "spec/nested_example_spec.rb" with:
197
+ """ruby
198
+ require "spec_helper"
199
+
200
+ RSpec.describe "A json response" do
201
+ subject { '%{JSON_WITH_ROOT_ARRAY}' }
202
+
203
+ it "has basic info about user" do
204
+ expect(subject).to include_unordered_json ["day & night", "first flight"]
205
+ end
206
+ end
207
+ """
208
+ When I run "rspec spec/nested_example_spec.rb"
209
+ Then I see:
210
+ """
211
+ 1 example, 0 failures
212
+ """
213
+
176
214
  Scenario: Expecting json string with array at root to fully include json with wrong values
177
215
  Given a file "spec/nested_example_spec.rb" with:
178
216
  """ruby
@@ -198,3 +236,27 @@ Feature: Unordered array matching support for include_json matcher
198
236
  expected: "unknown"
199
237
  got: nil
200
238
  """
239
+
240
+ Scenario: Expecting json string with array at root to fully include json with wrong values using alternative syntax
241
+ Given a file "spec/nested_example_spec.rb" with:
242
+ """ruby
243
+ require "spec_helper"
244
+
245
+ RSpec.describe "A json response" do
246
+ subject { '%{JSON_WITH_ROOT_ARRAY}' }
247
+
248
+ it "has basic info about user" do
249
+ expect(subject).to include_unordered_json ["day & night", "unknown", "first flight"]
250
+ end
251
+ end
252
+ """
253
+ When I run "rspec spec/nested_example_spec.rb"
254
+ Then I see:
255
+ """
256
+ json atom at path "1" is missing
257
+ """
258
+ And I see:
259
+ """
260
+ expected: "unknown"
261
+ got: nil
262
+ """
@@ -1,4 +1,5 @@
1
1
  require "rspec/core"
2
+ require "rspec/json_expectations/matcher_factory"
2
3
  require "rspec/json_expectations/version"
3
4
  require "rspec/json_expectations/json_traverser"
4
5
  require "rspec/json_expectations/failure_presenter"
@@ -0,0 +1,50 @@
1
+ module RSpec
2
+ module JsonExpectations
3
+ class MatcherFactory
4
+ def initialize(matcher_name)
5
+ @matcher_name = matcher_name
6
+ end
7
+
8
+ def define_matcher(&block)
9
+ RSpec::Matchers.define(@matcher_name) do |expected|
10
+ yield
11
+
12
+ # RSpec 2 vs 3
13
+ if respond_to?(:failure_message)
14
+ match do |actual|
15
+ traverse(expected, actual, false)
16
+ end
17
+
18
+ match_when_negated do |actual|
19
+ traverse(expected, actual, true)
20
+ end
21
+
22
+ failure_message do |actual|
23
+ RSpec::JsonExpectations::FailurePresenter.render(@include_json_errors)
24
+ end
25
+
26
+ failure_message_when_negated do |actual|
27
+ RSpec::JsonExpectations::FailurePresenter.render(@include_json_errors)
28
+ end
29
+ else
30
+ match_for_should do |actual|
31
+ traverse(expected, actual, false)
32
+ end
33
+
34
+ match_for_should_not do |actual|
35
+ traverse(expected, actual, true)
36
+ end
37
+
38
+ failure_message_for_should do |actual|
39
+ RSpec::JsonExpectations::FailurePresenter.render(@include_json_errors)
40
+ end
41
+
42
+ failure_message_for_should_not do |actual|
43
+ RSpec::JsonExpectations::FailurePresenter.render(@include_json_errors)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,40 +1,4 @@
1
- RSpec::Matchers.define :include_json do |expected|
2
-
3
- # RSpec 2 vs 3
4
- if respond_to?(:failure_message)
5
- match do |actual|
6
- traverse(expected, actual, false)
7
- end
8
-
9
- match_when_negated do |actual|
10
- traverse(expected, actual, true)
11
- end
12
-
13
- failure_message do |actual|
14
- RSpec::JsonExpectations::FailurePresenter.render(@include_json_errors)
15
- end
16
-
17
- failure_message_when_negated do |actual|
18
- RSpec::JsonExpectations::FailurePresenter.render(@include_json_errors)
19
- end
20
- else
21
- match_for_should do |actual|
22
- traverse(expected, actual, false)
23
- end
24
-
25
- match_for_should_not do |actual|
26
- traverse(expected, actual, true)
27
- end
28
-
29
- failure_message_for_should do |actual|
30
- RSpec::JsonExpectations::FailurePresenter.render(@include_json_errors)
31
- end
32
-
33
- failure_message_for_should_not do |actual|
34
- RSpec::JsonExpectations::FailurePresenter.render(@include_json_errors)
35
- end
36
- end
37
-
1
+ RSpec::JsonExpectations::MatcherFactory.new(:include_json).define_matcher do
38
2
  def traverse(expected, actual, negate=false)
39
3
  unless expected.is_a?(Hash) ||
40
4
  expected.is_a?(Array) ||
@@ -55,6 +19,28 @@ RSpec::Matchers.define :include_json do |expected|
55
19
  end
56
20
  end
57
21
 
22
+ RSpec::JsonExpectations::MatcherFactory.new(:include_unordered_json).define_matcher do
23
+ def traverse(expected, actual, negate=false)
24
+ unless expected.is_a?(Array)
25
+ raise ArgumentError,
26
+ "Expected value must be a array for include_unordered_json matcher"
27
+ end
28
+
29
+ actual_json = actual
30
+ actual_json = JSON.parse(actual) if String === actual
31
+
32
+ expected_wrapped_in_unordered_array = \
33
+ RSpec::JsonExpectations::Matchers::UnorderedArrayMatcher.new(expected)
34
+
35
+ RSpec::JsonExpectations::JsonTraverser.traverse(
36
+ @include_json_errors = { _negate: negate },
37
+ expected_wrapped_in_unordered_array,
38
+ actual_json,
39
+ negate
40
+ )
41
+ end
42
+ end
43
+
58
44
  module RSpec
59
45
  module JsonExpectations
60
46
  module Matchers
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module JsonExpectations
3
- VERSION = "1.3.0"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-json_expectations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksii Fedorov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-18 00:00:00.000000000 Z
11
+ date: 2016-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -51,6 +51,7 @@ files:
51
51
  - ".gitignore"
52
52
  - ".travis.yml"
53
53
  - CODE_OF_CONDUCT.md
54
+ - Gemfile
54
55
  - LICENSE.txt
55
56
  - README.md
56
57
  - Rakefile
@@ -72,6 +73,7 @@ files:
72
73
  - lib/rspec/json_expectations.rb
73
74
  - lib/rspec/json_expectations/failure_presenter.rb
74
75
  - lib/rspec/json_expectations/json_traverser.rb
76
+ - lib/rspec/json_expectations/matcher_factory.rb
75
77
  - lib/rspec/json_expectations/matchers.rb
76
78
  - lib/rspec/json_expectations/version.rb
77
79
  - local_env.rb
@@ -112,4 +114,3 @@ test_files:
112
114
  - features/rspec/json_expectations/unordered_array_support.feature
113
115
  - features/step_definitions/steps.rb
114
116
  - features/support/env.rb
115
- has_rdoc: