rspec-json_matchers 0.1.0.alpha.3 → 0.1.0.alpha.4
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 +4 -4
- data/.codeclimate.yml +16 -0
- data/.rubocop.yml +477 -22
- data/.travis.yml +12 -3
- data/Appraisals +24 -4
- data/Gemfile +1 -0
- data/README.md +37 -20
- data/Rakefile +1 -0
- data/gemfiles/rspec_3_0.gemfile +2 -1
- data/gemfiles/rspec_3_1.gemfile +2 -1
- data/gemfiles/rspec_3_2.gemfile +2 -1
- data/gemfiles/rspec_3_3.gemfile +2 -1
- data/gemfiles/rspec_3_4.gemfile +7 -0
- data/gemfiles/rspec_3_5.gemfile +7 -0
- data/gemfiles/rspec_3_6.gemfile +7 -0
- data/lib/rspec/json_matchers/expectation.rb +6 -2
- data/lib/rspec/json_matchers/expectations/mixins/built_in.rb +84 -5
- data/lib/rspec/json_matchers/expectations/private.rb +4 -2
- data/lib/rspec/json_matchers/matchers.rb +0 -1
- data/lib/rspec/json_matchers/matchers/be_json_matcher.rb +0 -14
- data/lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb +144 -5
- data/lib/rspec/json_matchers/utils/key_path/extraction.rb +8 -8
- data/lib/rspec/json_matchers/utils/key_path/path.rb +1 -1
- data/lib/rspec/json_matchers/version.rb +1 -1
- data/rspec-json_matchers.gemspec +3 -2
- metadata +16 -13
- data/lib/rspec/json_matchers/comparers.rb +0 -13
- data/lib/rspec/json_matchers/comparers/abstract_comparer.rb +0 -323
- data/lib/rspec/json_matchers/comparers/comparison_result.rb +0 -22
- data/lib/rspec/json_matchers/comparers/exact_keys_comparer.rb +0 -27
- data/lib/rspec/json_matchers/comparers/include_keys_comparer.rb +0 -26
- data/lib/rspec/json_matchers/matchers/be_json_with_sizes_matcher.rb +0 -24
- data/lib/rspec/json_matchers/matchers/be_json_with_something_matcher.rb +0 -188
@@ -1,22 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
module JsonMatchers
|
3
|
-
module Comparers
|
4
|
-
# @api private
|
5
|
-
#
|
6
|
-
# A value object returned by comparers
|
7
|
-
# Instead of just Boolean
|
8
|
-
class ComparisonResult
|
9
|
-
attr_reader :reasons
|
10
|
-
|
11
|
-
def initialize(matched, reasons)
|
12
|
-
@matched = matched
|
13
|
-
@reasons = reasons
|
14
|
-
end
|
15
|
-
|
16
|
-
def matched?
|
17
|
-
@matched
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require_relative "abstract_comparer"
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module JsonMatchers
|
5
|
-
module Comparers
|
6
|
-
# @api private
|
7
|
-
#
|
8
|
-
# The comparer class that disallow actual collection
|
9
|
-
# to have more properties/elements than expected collection
|
10
|
-
class ExactKeysComparer < AbstractComparer
|
11
|
-
private
|
12
|
-
|
13
|
-
# @note with side effect on `#reasons`
|
14
|
-
def has_matched_keys?
|
15
|
-
(actual_keys == expected_keys).tap do |success|
|
16
|
-
reasons.push(diff_keys.awesome_inspect) unless success
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def diff_keys
|
21
|
-
(actual_keys - expected_keys) +
|
22
|
-
(expected_keys - actual_keys)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require_relative "abstract_comparer"
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module JsonMatchers
|
5
|
-
module Comparers
|
6
|
-
# @api private
|
7
|
-
#
|
8
|
-
# The comparer class that allow actual collection
|
9
|
-
# to have more properties/elements than expected collection
|
10
|
-
class IncludeKeysComparer < AbstractComparer
|
11
|
-
private
|
12
|
-
|
13
|
-
# @note with side effect on `#reasons`
|
14
|
-
def has_matched_keys?
|
15
|
-
(expected_keys.subset?(actual_keys)).tap do |success|
|
16
|
-
reasons.push(diff_keys.awesome_inspect) unless success
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def diff_keys
|
21
|
-
expected_keys - actual_keys
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require "json"
|
2
|
-
require "awesome_print"
|
3
|
-
|
4
|
-
require_relative "be_json_with_something_matcher"
|
5
|
-
require_relative "../comparers"
|
6
|
-
require_relative "../utils"
|
7
|
-
|
8
|
-
module RSpec
|
9
|
-
module JsonMatchers
|
10
|
-
module Matchers
|
11
|
-
# @api private
|
12
|
-
class BeJsonWithSizesMatcher < BeJsonWithSomethingMatcher
|
13
|
-
private
|
14
|
-
|
15
|
-
def value_matching_proc
|
16
|
-
lambda do |expected, actual|
|
17
|
-
Expectations::Private::ArrayWithSize[expected].
|
18
|
-
expect?(actual)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,188 +0,0 @@
|
|
1
|
-
require "json"
|
2
|
-
require "awesome_print"
|
3
|
-
require "abstract_class"
|
4
|
-
|
5
|
-
require_relative "be_json_matcher"
|
6
|
-
require_relative "../utils"
|
7
|
-
|
8
|
-
module RSpec
|
9
|
-
module JsonMatchers
|
10
|
-
module Matchers
|
11
|
-
# @api private
|
12
|
-
# @abstract
|
13
|
-
#
|
14
|
-
# Parent of matcher classes that requires {#at_path} & {#with_exact_keys}
|
15
|
-
# This is not merged with {BeJsonMatcher}
|
16
|
-
# since it should be able to be used alone
|
17
|
-
class BeJsonWithSomethingMatcher < BeJsonMatcher
|
18
|
-
extend AbstractClass
|
19
|
-
|
20
|
-
attr_reader(*[:expected, :path, :with_exact_keys])
|
21
|
-
alias_method :with_exact_keys?, :with_exact_keys
|
22
|
-
|
23
|
-
def initialize(expected)
|
24
|
-
@expected = expected
|
25
|
-
@path = JsonMatchers::Utils::KeyPath::Path.new("")
|
26
|
-
@exact_match = false
|
27
|
-
end
|
28
|
-
|
29
|
-
def matches?(*_args)
|
30
|
-
super && has_valid_path? && expected_and_actual_matched?
|
31
|
-
end
|
32
|
-
|
33
|
-
def does_not_match?(*args)
|
34
|
-
!matches?(*args) && has_valid_path?
|
35
|
-
end
|
36
|
-
|
37
|
-
# Override {BeJsonMatcher#actual}
|
38
|
-
# It return actual object extracted by {#path}
|
39
|
-
# And also detect & set state for path error
|
40
|
-
# (either it's invalid or fails to extract)
|
41
|
-
#
|
42
|
-
# @return [Object]
|
43
|
-
# extracted object but could be object in the middle
|
44
|
-
# when extraction failed
|
45
|
-
def actual
|
46
|
-
result = path.extract(super)
|
47
|
-
has_path_error! if result.failed?
|
48
|
-
result.object
|
49
|
-
end
|
50
|
-
|
51
|
-
# Sets the path to be used for object,
|
52
|
-
# to avoid passing a deep nested
|
53
|
-
# {Hash} or {Array} as expectation
|
54
|
-
# Defaults to "" (if this is not called)
|
55
|
-
#
|
56
|
-
# The path uses period (".") as separator for parts
|
57
|
-
# Also period cannot be used as path name as a side-effect
|
58
|
-
#
|
59
|
-
# This does NOT raise error if the path is invalid
|
60
|
-
# (like having 2 periods, 1 period at the start/end of string)
|
61
|
-
# But it will fail the example with both `should` & `should_not`
|
62
|
-
#
|
63
|
-
# @param path [String] the "path" to be used
|
64
|
-
#
|
65
|
-
# @return [BeJsonWithSomethingMatcher] the match itself
|
66
|
-
#
|
67
|
-
# @throw [TypeError] when input is not a string
|
68
|
-
def at_path(path)
|
69
|
-
@path = JsonMatchers::Utils::KeyPath::Path.new(path)
|
70
|
-
self
|
71
|
-
end
|
72
|
-
|
73
|
-
# When `exactly` is `true`,
|
74
|
-
# makes the matcher to fail the example
|
75
|
-
# when actual has more elements than expected even expectation passes
|
76
|
-
#
|
77
|
-
# When `exactly` is `true`,
|
78
|
-
# makes the matcher to pass the example
|
79
|
-
# when actual has more elements than expected and expectation passes
|
80
|
-
#
|
81
|
-
# @param exactly [Boolean]
|
82
|
-
# whether the matcher should match keys in actual & expected exactly
|
83
|
-
#
|
84
|
-
# @return (see #at_path)
|
85
|
-
def with_exact_keys(exactly = true)
|
86
|
-
@with_exact_keys = exactly
|
87
|
-
self
|
88
|
-
end
|
89
|
-
|
90
|
-
# Overrides {BeJsonMatcher#failure_message}
|
91
|
-
def failure_message
|
92
|
-
return super if has_parser_error?
|
93
|
-
failure_message_for(true)
|
94
|
-
end
|
95
|
-
|
96
|
-
# Overrides {BeJsonMatcher#failure_message_when_negated}
|
97
|
-
def failure_message_when_negated
|
98
|
-
return super if has_parser_error?
|
99
|
-
failure_message_for(false)
|
100
|
-
end
|
101
|
-
|
102
|
-
private
|
103
|
-
|
104
|
-
def failure_message_for(should_match)
|
105
|
-
return invalid_path_message unless has_valid_path?
|
106
|
-
return path_error_message if has_path_error?
|
107
|
-
|
108
|
-
inspection_messages(should_match)
|
109
|
-
end
|
110
|
-
|
111
|
-
# @return [Bool] Whether `expected` & `parsed` are "equal"
|
112
|
-
def expected_and_actual_matched?
|
113
|
-
extracted_actual = actual
|
114
|
-
return false if has_path_error?
|
115
|
-
result = comparer_klass.
|
116
|
-
new(extracted_actual, expected, reasons, value_matching_proc).
|
117
|
-
compare
|
118
|
-
|
119
|
-
result.matched?.tap do |matched|
|
120
|
-
@reasons = result.reasons unless matched
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
def reasons
|
125
|
-
@reasons ||= []
|
126
|
-
end
|
127
|
-
|
128
|
-
def inspection_messages(should_match)
|
129
|
-
[
|
130
|
-
["expected", inspection_messages_prefix(should_match), "to match:"].
|
131
|
-
compact.map(&:strip).join(" "),
|
132
|
-
expected.awesome_inspect(indent: -2),
|
133
|
-
"",
|
134
|
-
"actual:",
|
135
|
-
actual.awesome_inspect(indent: -2),
|
136
|
-
"",
|
137
|
-
inspection_message_for_reason,
|
138
|
-
].join("\n")
|
139
|
-
end
|
140
|
-
|
141
|
-
def inspection_messages_prefix(should_match)
|
142
|
-
should_match ? nil : "not"
|
143
|
-
end
|
144
|
-
|
145
|
-
def inspection_message_for_reason
|
146
|
-
reasons.any? ? "reason/path: #{reasons.reverse.join('.')}" : nil
|
147
|
-
end
|
148
|
-
|
149
|
-
def original_actual
|
150
|
-
@actual
|
151
|
-
end
|
152
|
-
|
153
|
-
def has_path_error?
|
154
|
-
@has_path_error
|
155
|
-
end
|
156
|
-
|
157
|
-
def has_path_error!
|
158
|
-
@has_path_error = true
|
159
|
-
end
|
160
|
-
|
161
|
-
# For both positive and negative
|
162
|
-
def path_error_message
|
163
|
-
[
|
164
|
-
%(path "#{path}" does not exists in actual: ),
|
165
|
-
original_actual.awesome_inspect(indent: -2),
|
166
|
-
].join("\n")
|
167
|
-
end
|
168
|
-
|
169
|
-
def has_valid_path?
|
170
|
-
(path.nil? || path.valid?)
|
171
|
-
end
|
172
|
-
|
173
|
-
# For both positive and negative
|
174
|
-
def invalid_path_message
|
175
|
-
%(path "#{path}" is invalid)
|
176
|
-
end
|
177
|
-
|
178
|
-
def comparer_klass
|
179
|
-
if with_exact_keys?
|
180
|
-
Comparers::ExactKeysComparer
|
181
|
-
else
|
182
|
-
Comparers::IncludeKeysComparer
|
183
|
-
end
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|