Class: RSpec::ResemblesJsonMatchers::JsonMatcher
- Inherits:
-
Object
- Object
- RSpec::ResemblesJsonMatchers::JsonMatcher
show all
- Includes:
- Matchers::Composable, Helpers
- Defined in:
- lib/rspec/resembles_json_matchers/json_matcher.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helpers
#matcher?, #matcherize, #sentencize
Constructor Details
#initialize(expected_json) ⇒ JsonMatcher
Returns a new instance of JsonMatcher.
20
21
22
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 20
def initialize(expected_json)
@expected = expected_json.try(:deep_stringify_keys)
end
|
Instance Attribute Details
#expected ⇒ Object
Returns the value of attribute expected.
18
19
20
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 18
def expected
@expected
end
|
Class Method Details
.can_match?(hash) ⇒ Boolean
14
15
16
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 14
def self.can_match?(hash)
hash.is_a? Hash
end
|
Instance Method Details
#actual ⇒ Object
70
71
72
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 70
def actual
@actual ||= {}
end
|
#description ⇒ Object
35
36
37
38
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 35
def description
"have json that looks like\n#{expected_formatted.indent(2)}"
end
|
62
63
64
65
66
67
68
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 62
def expected_formatted
out = +"{\n"
out << expected_matchers.map do |k, v|
%{"#{k}": #{RSpec::Support::ObjectFormatter.format(v.expected_value)}}.indent(2)
end.join(",\n")
out << "\n}"
end
|
#expected_matchers ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 48
def expected_matchers
@expected_matchers ||= {}.tap do |hsh|
(expected.keys + actual.keys).uniq.each do |key|
expected_value = matcherize(expected[key])
hsh[key.to_s] =
if !expected.key?(key) then ExtraAttributeMatcher.new(key, expected_value)
elsif !actual.key?(key) then MissingAttributeMatcher.new(key, expected_value)
else
AttributeMatcher.new(key, expected_value)
end
end
end
end
|
#failure_message ⇒ Object
40
41
42
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 40
def failure_message
AttributeDiffer.new(self).to_s
end
|
#matches?(actual_json) ⇒ Boolean
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 24
def matches?(actual_json)
@actual = actual_json.try(:deep_stringify_keys)
all_passed = true
expected_matchers.each do |_key, matcher|
result = matcher.matches?(actual)
all_passed &&= result
end
all_passed
end
|
#to_json(*_args) ⇒ Object
44
45
46
|
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 44
def to_json(*_args)
failure_message
end
|