Class: RSpec::ResemblesJsonMatchers::JsonMatcher

Inherits:
Object
  • Object
show all
Includes:
Matchers::Composable, Helpers
Defined in:
lib/rspec/resembles_json_matchers/json_matcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#matcher?, #matcherize, #sentencize

Constructor Details

#initialize(expected_json) ⇒ JsonMatcher

Returns a new instance of JsonMatcher.



20
21
22
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 20

def initialize(expected_json)
  @expected = expected_json.try(:deep_stringify_keys)
end

Instance Attribute Details

#expectedObject (readonly)

Returns the value of attribute expected.



18
19
20
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 18

def expected
  @expected
end

Class Method Details

.can_match?(hash) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 14

def self.can_match?(hash)
  hash.is_a? Hash
end

Instance Method Details

#actualObject



70
71
72
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 70

def actual
  @actual ||= {}
end

#descriptionObject



35
36
37
38
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 35

def description
  # TODO Figure out how to discover the right indent level
  "have json that looks like\n#{expected_formatted.indent(2)}"
end

#expected_formattedObject



62
63
64
65
66
67
68
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 62

def expected_formatted
  out = +"{\n"
  out << expected_matchers.map do |k, v|
    %{"#{k}": #{RSpec::Support::ObjectFormatter.format(v.expected_value)}}.indent(2)
  end.join(",\n")
  out << "\n}"
end

#expected_matchersObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 48

def expected_matchers
  @expected_matchers ||= {}.tap do |hsh|
    (expected.keys + actual.keys).uniq.each do |key|
      expected_value = matcherize(expected[key])
      hsh[key.to_s] =
        if !expected.key?(key) then ExtraAttributeMatcher.new(key, expected_value)
        elsif !actual.key?(key) then MissingAttributeMatcher.new(key, expected_value)
        else
          AttributeMatcher.new(key, expected_value)
        end
    end
  end
end

#failure_messageObject



40
41
42
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 40

def failure_message
  AttributeDiffer.new(self).to_s
end

#matches?(actual_json) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 24

def matches?(actual_json)
  @actual = actual_json.try(:deep_stringify_keys)
  # Can't use #all? because it stops on the first false
  all_passed = true
  expected_matchers.each do |_key, matcher|
    result = matcher.matches?(actual)
    all_passed &&= result
  end
  all_passed
end

#to_json(*_args) ⇒ Object



44
45
46
# File 'lib/rspec/resembles_json_matchers/json_matcher.rb', line 44

def to_json(*_args)
  failure_message
end