rspec-json_expectations 1.4.0 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 578ce36c168740a6cf33f28aa580b7eb9a3b2bda
4
- data.tar.gz: b06d98b0d63db3bafa6aeca999c7716de7c92913
3
+ metadata.gz: 2c297902e9eca947b87a20ed364ba4b89df4999d
4
+ data.tar.gz: cbcad8557c7b4e6b868952a9fbd7a095a502f412
5
5
  SHA512:
6
- metadata.gz: 4531142c7ea3c080f9cb3df97550346f7e9ff5e8bf009b1f1ee29d3ab2d5eb023619b51f69281dec5c9fa093e6b397596bc80e14e105ed7d9a8c5bdf8244d22b
7
- data.tar.gz: a9ec4e356c69bb1a5ef5c2d5898b21ba95df8d24eff9b41669ee6c2790b0a52b8d45afdf0a2941d9141d74280e427c1e927a4aa4d426b8b9b25f9e871989c76b
6
+ metadata.gz: df339e78a810a408a580c4a80cff1990fc03ddd5ecda91f7fd195750cf1a25bcb57bc5afc4f0435b2b1a9781617c408ba36da0f476b10cc7c059648226087191
7
+ data.tar.gz: 69ebdd7402a814145b81567ab4e5ddcfd6d45b36680105d578470ca090c2e7af07ba4d1a5e8c2b196c273c067e158bb5f604a8bf14d551a3934654ce1fe1a123
data/.travis.yml CHANGED
@@ -2,14 +2,11 @@ dist: trusty
2
2
  language: ruby
3
3
 
4
4
  rvm:
5
- - 1.9.3
6
- - 2.0.0
7
5
  - 2.1
8
6
  - 2.2
9
- - "ruby-2.3.0"
7
+ - 2.3
10
8
 
11
9
  gemfile:
12
- - gemfiles/rspec-2
13
10
  - gemfiles/rspec-3
14
11
 
15
12
  before_install:
@@ -0,0 +1,145 @@
1
+ Feature: match_unordered_json matcher
2
+
3
+ As a developer extensively testing my APIs with RSpec
4
+ I want to have a suitable tool to test my API responses
5
+ And I want to use simple ruby hashes to describe the parts of response
6
+ For that I need a custom matcher
7
+
8
+ Scenario: Expecting json array to match expected array
9
+ Given a file "spec/simple_example_spec.rb" with:
10
+ """ruby
11
+ require "spec_helper"
12
+
13
+ RSpec.describe "A json response" do
14
+ subject { '[1, 2, 3]' }
15
+
16
+ it "matches when the order is equal and the size is equal" do
17
+ expect(subject).to match_unordered_json([1, 2, 3])
18
+ end
19
+ end
20
+ """
21
+ When I run "rspec spec/simple_example_spec.rb"
22
+ Then I see:
23
+ """
24
+ 1 example, 0 failures
25
+ """
26
+
27
+ Scenario: Expecting json array to match expected array with different order
28
+ Given a file "spec/simple_example_spec.rb" with:
29
+ """ruby
30
+ require "spec_helper"
31
+
32
+ RSpec.describe "A json response" do
33
+ subject { '[1, 2, 3]' }
34
+
35
+ it "matches when the order is different but the size is equal" do
36
+ expect(subject).to match_unordered_json([2, 3, 1])
37
+ end
38
+ end
39
+ """
40
+ When I run "rspec spec/simple_example_spec.rb"
41
+ Then I see:
42
+ """
43
+ 1 example, 0 failures
44
+ """
45
+
46
+ Scenario: Expecting json array to not match a subcollection
47
+ Given a file "spec/simple_example_spec.rb" with:
48
+ """ruby
49
+ require "spec_helper"
50
+
51
+ RSpec.describe "A json response" do
52
+ subject { '[1, 2, 3]' }
53
+
54
+ it "doesn't match if the size is different" do
55
+ expect(subject).to match_unordered_json([1, 2])
56
+ end
57
+ end
58
+ """
59
+ When I run "rspec spec/simple_example_spec.rb"
60
+ Then I see:
61
+ """
62
+ json atom at path "" does not match the expected size:
63
+ """
64
+ And I see:
65
+ """
66
+ expected collection contained: [1, 2]
67
+ """
68
+ And I see:
69
+ """
70
+ actual collection contained: [1, 2, 3]
71
+ """
72
+ And I see:
73
+ """
74
+ the extra elements were: [3]
75
+ """
76
+
77
+
78
+ Scenario: Expecting json array to not match a subcollection
79
+ Given a file "spec/simple_example_spec.rb" with:
80
+ """ruby
81
+ require "spec_helper"
82
+
83
+ RSpec.describe "A json response" do
84
+ subject { '[1, 2, 3]' }
85
+
86
+ it "doesn't match if the order is different and size is different" do
87
+ expect(subject).to match_unordered_json([3, 1])
88
+ end
89
+ end
90
+ """
91
+ When I run "rspec spec/simple_example_spec.rb"
92
+ Then I see:
93
+ """
94
+ json atom at path "" does not match the expected size:
95
+ """
96
+ And I see:
97
+ """
98
+ expected collection contained: [3, 1]
99
+ """
100
+ And I see:
101
+ """
102
+ actual collection contained: [1, 2, 3]
103
+ """
104
+ And I see:
105
+ """
106
+ the extra elements were: [2]
107
+ """
108
+
109
+ Scenario: Expecting json array to successfully not match when the arrays do not match
110
+ Given a file "spec/simple_example_spec.rb" with:
111
+ """ruby
112
+ require "spec_helper"
113
+
114
+ RSpec.describe "A json response" do
115
+ subject { '[1, 2]' }
116
+
117
+ it "successfully does not match when the the size is unequal" do
118
+ expect(subject).to_not match_unordered_json([1, 2, 3])
119
+ end
120
+ end
121
+ """
122
+ When I run "rspec spec/simple_example_spec.rb"
123
+ Then I see:
124
+ """
125
+ 1 example, 0 failures
126
+ """
127
+
128
+ Scenario: Expecting json array to fail to not match when the arrays do match
129
+ Given a file "spec/simple_example_spec.rb" with:
130
+ """ruby
131
+ require "spec_helper"
132
+
133
+ RSpec.describe "A json response" do
134
+ subject { '[1, 2, 3]' }
135
+
136
+ it "fails to not match when the arrays do match" do
137
+ expect(subject).to_not match_unordered_json([1, 2, 3])
138
+ end
139
+ end
140
+ """
141
+ When I run "rspec spec/simple_example_spec.rb"
142
+ Then I see:
143
+ """
144
+ 1 example, 1 failure
145
+ """
@@ -12,6 +12,7 @@ module RSpec
12
12
 
13
13
  def render_error(path, error, negate=false)
14
14
  [
15
+ render_size_mismatch(path, error, negate),
15
16
  render_no_key(path, error, negate),
16
17
  render_not_eq(path, error, negate),
17
18
  render_not_match(path, error, negate),
@@ -19,6 +20,17 @@ module RSpec
19
20
  ].select { |e| e }.first
20
21
  end
21
22
 
23
+ def render_size_mismatch(path, error, negate=false)
24
+ %{
25
+ json atom at path "#{path}" does not match the expected size:
26
+
27
+ expected collection contained: #{error[:expected].inspect}
28
+ actual collection contained: #{error[:actual].inspect}
29
+ the extra elements were: #{error[:extra_elements].inspect}
30
+
31
+ } if error_is_size_mismatch?(error)
32
+ end
33
+
22
34
  def render_no_key(path, error, negate=false)
23
35
  %{
24
36
  json atom at path "#{path}" is missing
@@ -58,6 +70,10 @@ module RSpec
58
70
  end.join("\n")
59
71
  end
60
72
 
73
+ def error_is_size_mismatch?(error)
74
+ error.is_a?(Hash) && error.has_key?(:_size_mismatch_error)
75
+ end
76
+
61
77
  def error_is_not_eq?(error)
62
78
  error.is_a?(Hash) && error.has_key?(:expected) && !error[:expected].is_a?(Regexp)
63
79
  end
@@ -9,16 +9,16 @@ module RSpec
9
9
  # json atom paths.
10
10
  class JsonTraverser
11
11
  HANDLED_BY_SIMPLE_VALUE_HANDLER = [String, Numeric, FalseClass, TrueClass, NilClass]
12
- RSPECMATCHERS = defined?(RSpec::Matchers) ? [RSpec::Matchers::BuiltIn::BaseMatcher] : []
12
+ RSPECMATCHERS = [RSpec::Matchers::BuiltIn::BaseMatcher]
13
13
  SUPPORTED_VALUES = [Hash, Regexp, Array, Matchers::UnorderedArrayMatcher] +
14
14
  HANDLED_BY_SIMPLE_VALUE_HANDLER + RSPECMATCHERS
15
15
 
16
16
  class << self
17
- def traverse(errors, expected, actual, negate=false, prefix=[])
17
+ def traverse(errors, expected, actual, negate=false, prefix=[], options={})
18
18
  [
19
19
  handle_hash(errors, expected, actual, negate, prefix),
20
20
  handle_array(errors, expected, actual, negate, prefix),
21
- handle_unordered(errors, expected, actual, negate, prefix),
21
+ handle_unordered(errors, expected, actual, negate, prefix, options),
22
22
  handle_value(errors, expected, actual, negate, prefix),
23
23
  handle_regex(errors, expected, actual, negate, prefix),
24
24
  handle_rspec_matcher(errors, expected, actual, negate, prefix),
@@ -53,13 +53,31 @@ module RSpec
53
53
  handle_keyvalue(errors, transformed_expected, actual, negate, prefix)
54
54
  end
55
55
 
56
- def handle_unordered(errors, expected, actual, negate=false, prefix=[])
56
+ def handle_unordered(errors, expected, actual, negate=false, prefix=[], options={})
57
57
  return nil unless expected.is_a?(Matchers::UnorderedArrayMatcher)
58
58
 
59
- conditionally_negate(
60
- expected.match(errors, actual, prefix),
61
- negate
62
- )
59
+ match_size_assertion_result = \
60
+ match_size_of_collection(errors, expected, actual, prefix, options)
61
+
62
+ if match_size_assertion_result == false
63
+ conditionally_negate(false, negate)
64
+ else
65
+ conditionally_negate(expected.match(errors, actual, prefix), negate)
66
+ end
67
+ end
68
+
69
+ def match_size_of_collection(errors, expected, actual, prefix, options)
70
+ match_size = options[:match_size]
71
+ return nil unless match_size
72
+ return nil if expected.size == actual.size
73
+
74
+ errors[prefix.join("/")] = {
75
+ _size_mismatch_error: true,
76
+ expected: expected.unwrap_array,
77
+ actual: actual,
78
+ extra_elements: actual - expected.unwrap_array,
79
+ }
80
+ false
63
81
  end
64
82
 
65
83
  def handle_value(errors, expected, actual, negate=false, prefix=[])
@@ -41,11 +41,40 @@ RSpec::JsonExpectations::MatcherFactory.new(:include_unordered_json).define_matc
41
41
  end
42
42
  end
43
43
 
44
+ RSpec::JsonExpectations::MatcherFactory.new(:match_unordered_json).define_matcher do
45
+ def traverse(expected, actual, negate=false)
46
+ unless expected.is_a?(Array)
47
+ raise ArgumentError,
48
+ "Expected value must be a array for match_unordered_json matcher"
49
+ end
50
+
51
+ actual_json = actual
52
+ actual_json = JSON.parse(actual) if String === actual
53
+
54
+ expected_wrapped_in_unordered_array = \
55
+ RSpec::JsonExpectations::Matchers::UnorderedArrayMatcher.new(expected)
56
+
57
+ RSpec::JsonExpectations::JsonTraverser.traverse(
58
+ @include_json_errors = { _negate: negate },
59
+ expected_wrapped_in_unordered_array,
60
+ actual_json,
61
+ negate,
62
+ [],
63
+ { match_size: true }
64
+ )
65
+ end
66
+ end
67
+
44
68
  module RSpec
45
69
  module JsonExpectations
46
70
  module Matchers
47
71
  class UnorderedArrayMatcher
72
+ extend Forwardable
73
+
48
74
  attr_reader :array
75
+
76
+ def_delegators :array, :size
77
+
49
78
  def initialize(array)
50
79
  @array = array
51
80
  end
@@ -75,6 +104,10 @@ module RSpec
75
104
  def all?(&blk)
76
105
  array.each_with_index.all?(&blk)
77
106
  end
107
+
108
+ def unwrap_array
109
+ array
110
+ end
78
111
  end
79
112
 
80
113
  def UnorderedArray(*array)
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module JsonExpectations
3
- VERSION = "1.4.0"
3
+ VERSION = "2.0.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.4.0
4
+ version: 2.0.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-07-05 00:00:00.000000000 Z
11
+ date: 2017-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -43,7 +43,6 @@ email:
43
43
  - waterlink000@gmail.com
44
44
  executables:
45
45
  - build
46
- - with-rspec-2
47
46
  - with-rspec-3
48
47
  extensions: []
49
48
  extra_rdoc_files: []
@@ -56,10 +55,10 @@ files:
56
55
  - README.md
57
56
  - Rakefile
58
57
  - bin/build
59
- - bin/with-rspec-2
60
58
  - bin/with-rspec-3
61
59
  - features/rspec/json_expectations/array_support.feature
62
60
  - features/rspec/json_expectations/include_json_matcher.feature
61
+ - features/rspec/json_expectations/match_unordered_json_matcher.feature
63
62
  - features/rspec/json_expectations/negation_matching.feature
64
63
  - features/rspec/json_expectations/nested_json_support.feature
65
64
  - features/rspec/json_expectations/regular_expressions_support.feature
@@ -68,7 +67,6 @@ files:
68
67
  - features/rspec/json_expectations/unordered_array_support.feature
69
68
  - features/step_definitions/steps.rb
70
69
  - features/support/env.rb
71
- - gemfiles/rspec-2
72
70
  - gemfiles/rspec-3
73
71
  - lib/rspec/json_expectations.rb
74
72
  - lib/rspec/json_expectations/failure_presenter.rb
@@ -106,6 +104,7 @@ summary: Set of matchers and helpers to allow you test your APIs responses like
106
104
  test_files:
107
105
  - features/rspec/json_expectations/array_support.feature
108
106
  - features/rspec/json_expectations/include_json_matcher.feature
107
+ - features/rspec/json_expectations/match_unordered_json_matcher.feature
109
108
  - features/rspec/json_expectations/negation_matching.feature
110
109
  - features/rspec/json_expectations/nested_json_support.feature
111
110
  - features/rspec/json_expectations/regular_expressions_support.feature
data/bin/with-rspec-2 DELETED
@@ -1,3 +0,0 @@
1
- #!/bin/bash
2
-
3
- BUNDLE_GEMFILE=gemfiles/rspec-2 bundle $*
data/gemfiles/rspec-2 DELETED
@@ -1,10 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in rspec-json_expectations.gemspec
4
- gemspec path: '..'
5
-
6
- group :test do
7
- gem 'cucumber'
8
- gem 'rspec', '~> 2.14.0'
9
- end
10
-