Class: JsonSpec::Matchers::IncludeJson
- Inherits:
-
Object
- Object
- JsonSpec::Matchers::IncludeJson
show all
- Includes:
- Exclusion, Helpers
- Defined in:
- lib/json_spec/matchers.rb
Instance Method Summary
collapse
Methods included from Exclusion
#exclude_key?, #exclude_keys, #excluded_keys
Methods included from Helpers
#generate_normalized_json, #normalize_json, #parse_json
Constructor Details
#initialize(expected_json) ⇒ IncludeJson
Returns a new instance of IncludeJson.
62
63
64
|
# File 'lib/json_spec/matchers.rb', line 62
def initialize(expected_json)
@expected_json = expected_json
end
|
Instance Method Details
#at_path(path) ⇒ Object
76
77
78
79
|
# File 'lib/json_spec/matchers.rb', line 76
def at_path(path)
@path = path
self
end
|
#excluding(*keys) ⇒ Object
81
82
83
84
|
# File 'lib/json_spec/matchers.rb', line 81
def excluding(*keys)
excluded_keys.merge(keys.map{|k| k.to_s })
self
end
|
#failure_message_for_should ⇒ Object
91
92
93
94
95
|
# File 'lib/json_spec/matchers.rb', line 91
def failure_message_for_should
message = "Expected included JSON"
message << %( at path "#{@path}") if @path
message
end
|
#failure_message_for_should_not ⇒ Object
97
98
99
100
101
|
# File 'lib/json_spec/matchers.rb', line 97
def failure_message_for_should_not
message = "Expected excluded JSON"
message << %( at path "#{@path}") if @path
message
end
|
#including(*keys) ⇒ Object
86
87
88
89
|
# File 'lib/json_spec/matchers.rb', line 86
def including(*keys)
excluded_keys.subtract(keys.map{|k| k.to_s })
self
end
|
#matches?(actual_json) ⇒ Boolean
66
67
68
69
70
71
72
73
74
|
# File 'lib/json_spec/matchers.rb', line 66
def matches?(actual_json)
actual = parse_json(actual_json, @path)
expected = exclude_keys(parse_json(@expected_json))
case actual
when Hash then actual.values.map{|v| exclude_keys(v) }.include?(expected)
when Array then actual.map{|e| exclude_keys(e) }.include?(expected)
else false
end
end
|