class RSpec::JsonMatcher::AbstractMatcher

Attributes

expected[R]
parsed[R]
reasons[R]

Public Class Methods

new(expected = nil) click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 6
def initialize(expected = nil)
  @expected = expected
  @reasons = []
end

Public Instance Methods

===(json)
Alias for: matches?
compare(&reason) click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 27
def compare(&reason)
  raise NotImplementedError, "You must implement #{self.class}#compare"
end
description() click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 31
def description
  "be JSON"
end
failure_message() click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 35
def failure_message
  if has_parser_error?
    "expected value to be parsed as JSON, but failed"
  else
    inspection
  end
end
failure_message_when_negated()
matches?(json) click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 11
def matches?(json)
  @parsed = JSON.parse(json)
  if has_expectation?
    compare do |reason|
      @reasons << reason
    end
  else
    true
  end
rescue JSON::ParserError
  @parser_error = true
  false
end
Also aliased as: ===
negative_failure_message() click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 43
def negative_failure_message
  if has_parser_error?
    "expected value not to be parsed as JSON, but succeeded"
  else
    inspection("not ")
  end
end
Also aliased as: failure_message_when_negated

Private Instance Methods

has_expectation?() click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 58
def has_expectation?
  !!expected
end
has_parser_error?() click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 54
def has_parser_error?
  !!@parser_error
end
inspection(prefix = nil) click to toggle source
# File lib/rspec/json_matcher/abstract_matcher.rb, line 62
def inspection(prefix = nil)
  messages = ["expected #{prefix}to match:", expected.ai(indent: -2), "", "actual:", parsed.ai(indent: -2), ""]
  messages.push "reason: #{reasons.reverse.join(".")}" unless reasons.empty?
  messages.join("\n")
end