Class: RSpec::JsonMatcher::AbstractMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/json_matcher/abstract_matcher.rb

Direct Known Subclasses

ExactMatcher, FuzzyMatcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected = nil) ⇒ AbstractMatcher

Returns a new instance of AbstractMatcher.



6
7
8
9
# File 'lib/rspec/json_matcher/abstract_matcher.rb', line 6

def initialize(expected = nil)
  @expected = expected
  @reasons = []
end

Instance Attribute Details

#expectedObject (readonly)

Returns the value of attribute expected.



4
5
6
# File 'lib/rspec/json_matcher/abstract_matcher.rb', line 4

def expected
  @expected
end

#parsedObject (readonly)

Returns the value of attribute parsed.



4
5
6
# File 'lib/rspec/json_matcher/abstract_matcher.rb', line 4

def parsed
  @parsed
end

#reasonsObject (readonly)

Returns the value of attribute reasons.



4
5
6
# File 'lib/rspec/json_matcher/abstract_matcher.rb', line 4

def reasons
  @reasons
end

Instance Method Details

#compare(&reason) ⇒ Object

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/rspec/json_matcher/abstract_matcher.rb', line 27

def compare(&reason)
  raise NotImplementedError, "You must implement #{self.class}#compare"
end

#descriptionObject



31
32
33
# File 'lib/rspec/json_matcher/abstract_matcher.rb', line 31

def description
  "be JSON"
end

#failure_messageObject



35
36
37
38
39
40
41
# 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

#matches?(json) ⇒ Boolean Also known as: ===

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

#negative_failure_messageObject Also known as: failure_message_when_negated



43
44
45
46
47
48
49
# 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