rspec-resembles_json_matchers 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +117 -58
- data/.travis.yml +3 -2
- data/Appraisals +7 -5
- data/Gemfile +3 -1
- data/Guardfile +6 -5
- data/LICENSE.txt +13 -0
- data/README.md +5 -0
- data/Rakefile +2 -0
- data/examples/example_spec.rb +67 -63
- data/gemfiles/.bundle/config +3 -0
- data/gemfiles/rails_5.gemfile +1 -1
- data/gemfiles/rails_5.gemfile.lock +100 -67
- data/gemfiles/{rails_42.gemfile → rails_6.gemfile} +2 -2
- data/gemfiles/rails_6.gemfile.lock +236 -0
- data/lib/rspec/resembles_json_matchers.rb +4 -3
- data/lib/rspec/resembles_json_matchers/attribute_differ.rb +38 -43
- data/lib/rspec/resembles_json_matchers/attribute_matcher.rb +2 -5
- data/lib/rspec/resembles_json_matchers/helpers.rb +4 -3
- data/lib/rspec/resembles_json_matchers/json_matcher.rb +7 -6
- data/lib/rspec/resembles_json_matchers/matcherizer.rb +2 -3
- data/lib/rspec/resembles_json_matchers/resembles_any_of_matcher.rb +9 -13
- data/lib/rspec/resembles_json_matchers/resembles_array_matcher.rb +2 -4
- data/lib/rspec/resembles_json_matchers/resembles_boolean_matcher.rb +1 -0
- data/lib/rspec/resembles_json_matchers/resembles_class_matcher.rb +2 -1
- data/lib/rspec/resembles_json_matchers/resembles_date_matcher.rb +2 -0
- data/lib/rspec/resembles_json_matchers/resembles_hash_matcher.rb +11 -11
- data/lib/rspec/resembles_json_matchers/resembles_matcher.rb +8 -16
- data/lib/rspec/resembles_json_matchers/resembles_nil_matcher.rb +1 -0
- data/lib/rspec/resembles_json_matchers/resembles_numeric_matcher.rb +2 -1
- data/lib/rspec/resembles_json_matchers/resembles_route_matcher.rb +1 -0
- data/lib/rspec/resembles_json_matchers/resembles_string_matcher.rb +1 -0
- data/lib/rspec/resembles_json_matchers/string_indent.rb +4 -2
- data/lib/rspec/resembles_json_matchers/version.rb +3 -1
- data/rspec-resembles_json_matchers.gemspec +14 -9
- metadata +66 -24
- data/gemfiles/rails_42.gemfile.lock +0 -183
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "rspec/resembles_json_matchers/version"
|
2
4
|
require "rspec/resembles_json_matchers/string_indent"
|
3
5
|
|
@@ -32,7 +34,7 @@ module RSpec
|
|
32
34
|
|
33
35
|
def self.resembles_matcher_candidates
|
34
36
|
# Order matters
|
35
|
-
@
|
37
|
+
@resembles_matcher_candidates ||= [
|
36
38
|
JsonMatcher,
|
37
39
|
ResemblesAnyOfMatcher,
|
38
40
|
ResemblesRouteMatcher,
|
@@ -45,9 +47,8 @@ module RSpec
|
|
45
47
|
].freeze
|
46
48
|
end
|
47
49
|
|
48
|
-
def self.resembles_matcher_for(expected, **
|
50
|
+
def self.resembles_matcher_for(expected, **_a)
|
49
51
|
resembles_matcher_candidates.detect { |candidate| candidate.can_match?(expected) } || RSpec::Matchers::BuiltIn::Eq
|
50
52
|
end
|
51
|
-
|
52
53
|
end
|
53
54
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/inflector"
|
2
4
|
|
3
5
|
module RSpec::ResemblesJsonMatchers
|
@@ -47,40 +49,34 @@ module RSpec::ResemblesJsonMatchers
|
|
47
49
|
@buffer.print NORMAL_COLOR
|
48
50
|
@buffer.print prefix + " " + "#{matcher.attribute_name.to_json}: "
|
49
51
|
render(matcher.value_matcher, prefix: prefix + " ")
|
50
|
-
|
51
|
-
@buffer.
|
52
|
+
elsif nested_matcher?(matcher.value_matcher)
|
53
|
+
@buffer.print NORMAL_COLOR
|
54
|
+
@buffer.print prefix + " " + "#{matcher.attribute_name.to_json}: "
|
55
|
+
render(matcher.value_matcher, prefix: prefix + " ")
|
52
56
|
else
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
@buffer.print(",") unless last
|
58
|
-
@buffer.puts
|
57
|
+
@buffer.print REMOVED_COLOR
|
58
|
+
@buffer.print prefix
|
59
|
+
if prefix.include? "-"
|
60
|
+
@buffer.print " "
|
59
61
|
else
|
60
|
-
@buffer.print
|
61
|
-
@buffer.print prefix
|
62
|
-
if prefix.include? "-"
|
63
|
-
@buffer.print " "
|
64
|
-
else
|
65
|
-
@buffer.print "- "
|
66
|
-
end
|
67
|
-
@buffer.print "#{matcher.attribute_name.to_json}: "
|
68
|
-
render(matcher.value_matcher, prefix: prefix + " ")
|
69
|
-
@buffer.print NORMAL_COLOR
|
70
|
-
@buffer.print(",") unless last
|
71
|
-
@buffer.puts
|
72
|
-
@buffer.print ADDED_COLOR
|
73
|
-
@buffer.print prefix + "+ #{matcher.attribute_name.to_json}: "
|
74
|
-
render(matcher.actual_value, prefix: prefix + " ")
|
75
|
-
@buffer.print NORMAL_COLOR
|
76
|
-
@buffer.print(",") unless last
|
77
|
-
@buffer.puts
|
62
|
+
@buffer.print "- "
|
78
63
|
end
|
64
|
+
@buffer.print "#{matcher.attribute_name.to_json}: "
|
65
|
+
render(matcher.value_matcher, prefix: prefix + " ")
|
66
|
+
@buffer.print NORMAL_COLOR
|
67
|
+
@buffer.print(",") unless last
|
68
|
+
@buffer.puts
|
69
|
+
@buffer.print ADDED_COLOR
|
70
|
+
@buffer.print prefix + "+ #{matcher.attribute_name.to_json}: "
|
71
|
+
render(matcher.actual_value, prefix: prefix + " ")
|
72
|
+
@buffer.print NORMAL_COLOR
|
79
73
|
end
|
74
|
+
@buffer.print(",") unless last
|
75
|
+
@buffer.puts
|
80
76
|
end
|
81
77
|
|
82
78
|
def render_MissingAttributeMatcher(matcher, prefix: "", last: false)
|
83
|
-
prefix
|
79
|
+
prefix += (prefix.include?("-") ? " " : "- ")
|
84
80
|
@buffer.print REMOVED_COLOR
|
85
81
|
@buffer.print prefix + "#{matcher.attribute_name.to_json}: "
|
86
82
|
render(matcher.value_matcher, prefix: prefix)
|
@@ -89,7 +85,7 @@ module RSpec::ResemblesJsonMatchers
|
|
89
85
|
end
|
90
86
|
|
91
87
|
def render_ExtraAttributeMatcher(matcher, prefix: "", last: false)
|
92
|
-
prefix
|
88
|
+
prefix += "+ "
|
93
89
|
@buffer.print ADDED_COLOR
|
94
90
|
@buffer.print prefix + matcher.attribute_name.to_json + ": "
|
95
91
|
render(matcher.actual_value, prefix: prefix)
|
@@ -97,7 +93,7 @@ module RSpec::ResemblesJsonMatchers
|
|
97
93
|
@buffer.puts
|
98
94
|
end
|
99
95
|
|
100
|
-
def render_ResemblesAnyOfMatcher(matcher, prefix: "", **
|
96
|
+
def render_ResemblesAnyOfMatcher(matcher, prefix: "", **_opts)
|
101
97
|
@buffer.puts "["
|
102
98
|
if matcher.matched?
|
103
99
|
matcher.original_expected.each do |item|
|
@@ -124,44 +120,45 @@ module RSpec::ResemblesJsonMatchers
|
|
124
120
|
@buffer.print prefix + "]"
|
125
121
|
end
|
126
122
|
|
127
|
-
def render_ResemblesBooleanMatcher(matcher, **
|
123
|
+
def render_ResemblesBooleanMatcher(matcher, **_opts)
|
128
124
|
@buffer.print matcher.expected.to_json
|
129
125
|
end
|
130
126
|
|
131
|
-
def render_ResemblesStringMatcher(matcher, **
|
127
|
+
def render_ResemblesStringMatcher(matcher, **_opts)
|
132
128
|
@buffer.print matcher.expected.to_json
|
133
129
|
end
|
134
130
|
|
135
|
-
def render_ResemblesDateMatcher(matcher, **
|
131
|
+
def render_ResemblesDateMatcher(matcher, **_opts)
|
136
132
|
@buffer.print matcher.expected.to_json
|
137
133
|
end
|
138
134
|
|
139
|
-
def render_ResemblesNumericMatcher(matcher, **
|
135
|
+
def render_ResemblesNumericMatcher(matcher, **_opts)
|
140
136
|
@buffer.print matcher.expected.to_json
|
141
137
|
end
|
142
138
|
|
143
|
-
def render_ResemblesClassMatcher(matcher, **
|
139
|
+
def render_ResemblesClassMatcher(matcher, **_opts)
|
144
140
|
@buffer.print matcher.expected.inspect
|
145
141
|
end
|
146
142
|
|
147
|
-
def render_ResemblesNilMatcher(
|
143
|
+
def render_ResemblesNilMatcher(_matcher, **_opts)
|
148
144
|
@buffer.print "null"
|
149
145
|
end
|
150
146
|
|
151
|
-
def render_ResemblesRouteMatcher(matcher, **
|
147
|
+
def render_ResemblesRouteMatcher(matcher, **_opts)
|
152
148
|
@buffer.print matcher.expected.inspect
|
153
149
|
end
|
154
150
|
|
155
151
|
def method_missing(method_name, *args, &block)
|
156
152
|
if method_name.to_s.start_with?("render_")
|
157
153
|
raise NoMethodError, method_name if method_name.to_s.end_with?("Matcher")
|
154
|
+
|
158
155
|
@buffer.print RSpec::Support::ObjectFormatter.format(args.first)
|
159
156
|
else
|
160
157
|
super
|
161
158
|
end
|
162
159
|
end
|
163
160
|
|
164
|
-
def respond_to_missing?(method_name,
|
161
|
+
def respond_to_missing?(method_name, _include_private = false)
|
165
162
|
method_name.to_s.start_with?("render_")
|
166
163
|
end
|
167
164
|
|
@@ -169,11 +166,9 @@ module RSpec::ResemblesJsonMatchers
|
|
169
166
|
matcher.is_a?(JsonMatcher) || matcher.is_a?(ResemblesAnyOfMatcher)
|
170
167
|
end
|
171
168
|
|
172
|
-
NORMAL_COLOR = "\e[0m"
|
173
|
-
REMOVED_COLOR = "\e[31m"
|
174
|
-
ADDED_COLOR = "\e[32m"
|
175
|
-
NEUTRAL_COLOR = "\e[34m"
|
176
|
-
|
169
|
+
NORMAL_COLOR = "\e[0m"
|
170
|
+
REMOVED_COLOR = "\e[31m" # Red
|
171
|
+
ADDED_COLOR = "\e[32m" # Green
|
172
|
+
NEUTRAL_COLOR = "\e[34m" # Blue
|
177
173
|
end
|
178
174
|
end
|
179
|
-
|
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/core_ext/hash/indifferent_access"
|
2
4
|
require "active_support/core_ext/object/try"
|
3
5
|
|
4
6
|
module RSpec::ResemblesJsonMatchers
|
5
|
-
|
6
7
|
class AttributeMatcher
|
7
8
|
include RSpec::ResemblesJsonMatchers::Helpers
|
8
9
|
Undefined = Object.new # TODO use Dry::Core::Constants::Undefined
|
@@ -61,7 +62,6 @@ module RSpec::ResemblesJsonMatchers
|
|
61
62
|
true
|
62
63
|
end
|
63
64
|
end
|
64
|
-
|
65
65
|
end
|
66
66
|
|
67
67
|
class MissingAttributeMatcher < AttributeMatcher
|
@@ -80,7 +80,6 @@ module RSpec::ResemblesJsonMatchers
|
|
80
80
|
end
|
81
81
|
|
82
82
|
class ExtraAttributeMatcher < AttributeMatcher
|
83
|
-
|
84
83
|
def matches?(document)
|
85
84
|
@document = document.try(:with_indifferent_access)
|
86
85
|
false
|
@@ -93,7 +92,5 @@ module RSpec::ResemblesJsonMatchers
|
|
93
92
|
def description
|
94
93
|
"not be present"
|
95
94
|
end
|
96
|
-
|
97
95
|
end
|
98
|
-
|
99
96
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "json"
|
2
4
|
|
3
5
|
module RSpec::ResemblesJsonMatchers
|
@@ -14,16 +16,15 @@ module RSpec::ResemblesJsonMatchers
|
|
14
16
|
end
|
15
17
|
|
16
18
|
def matcherize(expected)
|
17
|
-
if
|
19
|
+
if matcher?(expected)
|
18
20
|
expected
|
19
21
|
else
|
20
22
|
RSpec::ResemblesJsonMatchers.resembles_matcher_for(expected).new(expected)
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
def
|
26
|
+
def matcher?(obj)
|
25
27
|
obj.respond_to?(:matches?) && obj.respond_to?(:description)
|
26
28
|
end
|
27
|
-
|
28
29
|
end
|
29
30
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/core_ext/hash/keys" # stringify_keys
|
2
4
|
require "json"
|
3
5
|
|
@@ -13,7 +15,7 @@ module RSpec::ResemblesJsonMatchers
|
|
13
15
|
hash.is_a? Hash
|
14
16
|
end
|
15
17
|
|
16
|
-
attr_reader :expected
|
18
|
+
attr_reader :expected
|
17
19
|
|
18
20
|
def initialize(expected_json)
|
19
21
|
@expected = expected_json.try(:deep_stringify_keys)
|
@@ -23,7 +25,7 @@ module RSpec::ResemblesJsonMatchers
|
|
23
25
|
@actual = actual_json.try(:deep_stringify_keys)
|
24
26
|
# Can't use #all? because it stops on the first false
|
25
27
|
all_passed = true
|
26
|
-
expected_matchers.each do |
|
28
|
+
expected_matchers.each do |_key, matcher|
|
27
29
|
result = matcher.matches?(actual)
|
28
30
|
all_passed &&= result
|
29
31
|
end
|
@@ -39,7 +41,7 @@ module RSpec::ResemblesJsonMatchers
|
|
39
41
|
AttributeDiffer.new(self).to_s
|
40
42
|
end
|
41
43
|
|
42
|
-
def to_json
|
44
|
+
def to_json(*_args)
|
43
45
|
failure_message
|
44
46
|
end
|
45
47
|
|
@@ -58,8 +60,8 @@ module RSpec::ResemblesJsonMatchers
|
|
58
60
|
end
|
59
61
|
|
60
62
|
def expected_formatted
|
61
|
-
out = "{\n"
|
62
|
-
out << expected_matchers.map do |k,v|
|
63
|
+
out = +"{\n"
|
64
|
+
out << expected_matchers.map do |k, v|
|
63
65
|
%{"#{k}": #{RSpec::Support::ObjectFormatter.format(v.expected_value)}}.indent(2)
|
64
66
|
end.join(",\n")
|
65
67
|
out << "\n}"
|
@@ -68,6 +70,5 @@ module RSpec::ResemblesJsonMatchers
|
|
68
70
|
def actual
|
69
71
|
@actual ||= {}
|
70
72
|
end
|
71
|
-
|
72
73
|
end
|
73
74
|
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module RSpec::ResemblesJsonMatchers
|
3
4
|
module Matcherizer
|
4
5
|
def matcherize(expected)
|
5
6
|
if matcher? expected
|
@@ -20,6 +21,4 @@ module RSpec::ResemblesJsonMatchers
|
|
20
21
|
obj.respond_to?(:matches?) && obj.respond_to?(:description)
|
21
22
|
end
|
22
23
|
end
|
23
|
-
|
24
24
|
end
|
25
|
-
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/core_ext/array/wrap"
|
2
4
|
|
3
5
|
module RSpec::ResemblesJsonMatchers
|
@@ -34,9 +36,7 @@ module RSpec::ResemblesJsonMatchers
|
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
37
|
-
def failure_message
|
38
|
-
|
39
|
-
end
|
39
|
+
def failure_message; end
|
40
40
|
|
41
41
|
def expected_matchers
|
42
42
|
@expected
|
@@ -47,16 +47,12 @@ module RSpec::ResemblesJsonMatchers
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def expected_formatted
|
50
|
-
""
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
"should #{v.description}".indent(2)
|
57
|
-
end
|
58
|
-
end.join("\n")
|
59
|
-
end << "\n"
|
50
|
+
out = +""
|
51
|
+
out << expected_matchers.map do |v|
|
52
|
+
"should #{v.description}".indent(2)
|
53
|
+
end.join("\n")
|
54
|
+
out << "\n"
|
55
|
+
out
|
60
56
|
end
|
61
57
|
end
|
62
58
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
module RSpec::ResemblesJsonMatchers
|
3
4
|
class ResemblesArrayMatcher
|
@@ -27,9 +28,6 @@ module RSpec::ResemblesJsonMatchers
|
|
27
28
|
@expected.map { |e| matcherize(e) }
|
28
29
|
end
|
29
30
|
|
30
|
-
def failure_message
|
31
|
-
end
|
31
|
+
def failure_message; end
|
32
32
|
end
|
33
|
-
|
34
|
-
|
35
33
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
module RSpec::ResemblesJsonMatchers
|
3
4
|
class ResemblesClassMatcher
|
@@ -17,7 +18,7 @@ module RSpec::ResemblesJsonMatchers
|
|
17
18
|
|
18
19
|
def matches?(actual)
|
19
20
|
@actual = actual
|
20
|
-
actual.
|
21
|
+
actual.is_a? @expected
|
21
22
|
end
|
22
23
|
|
23
24
|
def expected_formatted
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RSpec::ResemblesJsonMatchers
|
2
4
|
class ResemblesHashMatcher
|
3
5
|
include Helpers
|
@@ -30,11 +32,11 @@ module RSpec::ResemblesJsonMatchers
|
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
|
-
actual.
|
35
|
+
actual.each_key { |k| unmatched_matchers.delete(k) }
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
37
|
+
failed_matches.none? and
|
38
|
+
unmatched_keys.none? and
|
39
|
+
unmatched_matchers.none?
|
38
40
|
end
|
39
41
|
|
40
42
|
def description
|
@@ -59,9 +61,7 @@ module RSpec::ResemblesJsonMatchers
|
|
59
61
|
end
|
60
62
|
end
|
61
63
|
|
62
|
-
|
63
|
-
@failed_matches
|
64
|
-
end
|
64
|
+
attr_reader :failed_matches
|
65
65
|
|
66
66
|
def unmatched_keys
|
67
67
|
@actual.keys - @matched_keys
|
@@ -73,15 +73,16 @@ module RSpec::ResemblesJsonMatchers
|
|
73
73
|
|
74
74
|
def expected_formatted
|
75
75
|
out = "{\n"
|
76
|
-
out << expected_matchers.map do |k,v|
|
76
|
+
out << expected_matchers.map do |k, v|
|
77
77
|
%{ "%s": %s} % [k, v.expected_formatted]
|
78
78
|
end.join(",\n")
|
79
79
|
out << "\n}\n"
|
80
80
|
end
|
81
81
|
|
82
82
|
def pretty_failed_matches
|
83
|
-
failed_matches.map do |k,matcher|
|
83
|
+
failed_matches.map do |k, matcher|
|
84
84
|
next unless @actual.key?(k) # Covered by the unmatched matchers messages
|
85
|
+
|
85
86
|
matcher_failure_message =
|
86
87
|
matcher.failure_message
|
87
88
|
.gsub("(compared using ==)", "") # From the equality matcher, ugly in this context
|
@@ -99,10 +100,9 @@ module RSpec::ResemblesJsonMatchers
|
|
99
100
|
end
|
100
101
|
|
101
102
|
def pretty_unmatched_matchers
|
102
|
-
unmatched_matchers.map do |key,
|
103
|
+
unmatched_matchers.map do |key, _matcher|
|
103
104
|
"attribute #{key.to_s.inspect}:\n has a matcher defined, but that attribute was not provided"
|
104
105
|
end.join("\n") + "\n"
|
105
106
|
end
|
106
|
-
|
107
107
|
end
|
108
108
|
end
|