class JsonSpec::Matchers::IncludeJson

Public Class Methods

new(expected_json = nil) click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 8
def initialize(expected_json = nil)
  @expected_json = expected_json
end

Public Instance Methods

at_path(path) click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 25
def at_path(path)
  @path = path
  self
end
description() click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 55
def description
  message_with_path("include JSON")
end
excluding(*keys) click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 35
def excluding(*keys)
  excluded_keys.merge(keys.map(&:to_s))
  self
end
failure_message() click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 45
def failure_message
  message_with_path("Expected included JSON")
end
Also aliased as: failure_message_for_should
failure_message_for_should()
Alias for: failure_message
failure_message_for_should_not()
failure_message_when_negated() click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 50
def failure_message_when_negated
  message_with_path("Expected excluded JSON")
end
from_file(path) click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 30
def from_file(path)
  @expected_json = load_json(path)
  self
end
including(*keys) click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 40
def including(*keys)
  excluded_keys.subtract(keys.map(&:to_s))
  self
end
matches?(actual_json) click to toggle source
# File lib/json_spec/matchers/include_json.rb, line 12
def matches?(actual_json)
  raise "Expected included JSON not provided" if @expected_json.nil?

  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)
  when String then actual.include?(expected)
  else false
  end
end