Class: JsonSpec::Matchers::IncludeJson

Inherits:
Object
  • Object
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.



68
69
70
# File 'lib/json_spec/matchers.rb', line 68

def initialize(expected_json)
  @expected_json = expected_json
end

Instance Method Details

#at_path(path) ⇒ Object



82
83
84
85
# File 'lib/json_spec/matchers.rb', line 82

def at_path(path)
  @path = path
  self
end

#descriptionObject



109
110
111
112
113
# File 'lib/json_spec/matchers.rb', line 109

def description
  message = "include JSON"
  message << %( at path "#{@path}") if @path
  message
end

#excluding(*keys) ⇒ Object



87
88
89
90
# File 'lib/json_spec/matchers.rb', line 87

def excluding(*keys)
  excluded_keys.merge(keys.map{|k| k.to_s })
  self
end

#failure_message_for_shouldObject



97
98
99
100
101
# File 'lib/json_spec/matchers.rb', line 97

def failure_message_for_should
  message = "Expected included JSON"
  message << %( at path "#{@path}") if @path
  message
end

#failure_message_for_should_notObject



103
104
105
106
107
# File 'lib/json_spec/matchers.rb', line 103

def failure_message_for_should_not
  message = "Expected excluded JSON"
  message << %( at path "#{@path}") if @path
  message
end

#including(*keys) ⇒ Object



92
93
94
95
# File 'lib/json_spec/matchers.rb', line 92

def including(*keys)
  excluded_keys.subtract(keys.map{|k| k.to_s })
  self
end

#matches?(actual_json) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'lib/json_spec/matchers.rb', line 72

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