Class: Canon::RSpecMatchers::SerializationMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/rspec_matchers.rb

Overview

Base matcher class for serialization equivalence

Instance Method Summary collapse

Constructor Details

#initialize(expected, format = :xml) ⇒ SerializationMatcher

Returns a new instance of SerializationMatcher.



16
17
18
19
20
# File 'lib/canon/rspec_matchers.rb', line 16

def initialize(expected, format = :xml)
  @expected = expected
  @format = format.to_sym
  @result = nil
end

Instance Method Details

#match_jsonObject



49
50
51
# File 'lib/canon/rspec_matchers.rb', line 49

def match_json
  canonicalize_and_compare(:json)
end

#match_xmlObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/canon/rspec_matchers.rb', line 30

def match_xml
  @result = CompareXML.equivalent?(
    Nokogiri::XML(@target),
    Nokogiri::XML(@expected),
    {
      collapse_whitespace: true,
      ignore_attr_order: true,
      verbose: true,
    },
  )

  @result.empty?
end

#match_yamlObject

Canonicalize and check string equivalence for YAML/JSON



45
46
47
# File 'lib/canon/rspec_matchers.rb', line 45

def match_yaml
  canonicalize_and_compare(:yaml)
end

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/canon/rspec_matchers.rb', line 22

def matches?(target)
  @target = target
  send("match_#{@format}") ||
    raise(Canon::Error, "Unsupported format: #{@format}")
rescue NoMethodError
  raise Canon::Error, "Unsupported format: #{@format}"
end