Class: Canon::RSpecMatchers::SerializationMatcher

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

Overview

Base matcher class for serialization equivalence This is a THIN WRAPPER around Canon::Comparison API

Instance Method Summary collapse

Constructor Details

#initialize(expected, format = nil, match_profile: nil, match: nil, preprocessing: nil, diff_algorithm: nil, show_diffs: nil) ⇒ SerializationMatcher

Returns a new instance of SerializationMatcher.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/canon/rspec_matchers.rb', line 49

def initialize(expected, format = nil, match_profile: nil,
               match: nil, preprocessing: nil, diff_algorithm: nil,
               show_diffs: nil)
  @expected = expected
  @format = format&.to_sym
  @match_profile = match_profile
  @match = match
  @preprocessing = preprocessing
  @diff_algorithm = diff_algorithm
  @show_diffs = show_diffs
end

Instance Method Details

#actualObject



121
122
123
# File 'lib/canon/rspec_matchers.rb', line 121

def actual
  @target
end

#diffableObject

rubocop:disable Naming/PredicateMethod



125
126
127
# File 'lib/canon/rspec_matchers.rb', line 125

def diffable # rubocop:disable Naming/PredicateMethod
  false
end

#does_not_match?(target) ⇒ Boolean

Skip the rebaseliner path for .not_to. RSpec invokes does_not_match? if defined, instead of negating matches?.

Returns:

  • (Boolean)


105
106
107
# File 'lib/canon/rspec_matchers.rb', line 105

def does_not_match?(target)
  !compute_equivalent(target)
end

#expectedObject



117
118
119
# File 'lib/canon/rspec_matchers.rb', line 117

def expected
  @expected
end

#failure_messageObject



109
110
111
# File 'lib/canon/rspec_matchers.rb', line 109

def failure_message
  "expected #{format_name} to be equivalent\n\n#{diff_output}"
end

#failure_message_when_negatedObject



113
114
115
# File 'lib/canon/rspec_matchers.rb', line 113

def failure_message_when_negated
  "expected #{format_name} not to be equivalent"
end

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
# File 'lib/canon/rspec_matchers.rb', line 93

def matches?(target)
  # Capture caller_locations only when rebaseliner is enabled so that
  # passing assertions don't pay for stack walking.
  @rebaseliner_caller = caller_locations(1, 12) if Canon::Rebaseliner.enabled?
  equivalent = compute_equivalent(target)
  return true if equivalent

  attempt_rebaseline
end

#show_diffs(value) ⇒ SerializationMatcher

Chain method for controlling diff display

Parameters:

  • value (Symbol, String)

    :all, :normative, or :informative

Returns:



64
65
66
67
# File 'lib/canon/rspec_matchers.rb', line 64

def show_diffs(value)
  @show_diffs = value.to_sym
  self
end

#with_match(**match_opts) ⇒ SerializationMatcher

Chain method for setting match options

Parameters:

  • match_opts (Hash)

    match options

Returns:



72
73
74
75
76
# File 'lib/canon/rspec_matchers.rb', line 72

def with_match(**match_opts)
  @match ||= {}
  @match = @match.merge(match_opts)
  self
end

#with_options(**options) ⇒ SerializationMatcher

Chain method for setting match options (alias for with_match)

Parameters:

  • options (Hash)

    Match dimension options

Returns:



89
90
91
# File 'lib/canon/rspec_matchers.rb', line 89

def with_options(**options)
  with_match(**options)
end

#with_profile(profile_name) ⇒ SerializationMatcher

Chain method for setting match profile

Parameters:

  • profile_name (Symbol)

    Profile name (:strict, :spec_friendly, etc.)

Returns:



81
82
83
84
# File 'lib/canon/rspec_matchers.rb', line 81

def with_profile(profile_name)
  @match_profile = profile_name
  self
end