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.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/canon/rspec_matchers.rb', line 46

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



136
137
138
# File 'lib/canon/rspec_matchers.rb', line 136

def actual
  @target
end

#diffableObject

rubocop:disable Naming/PredicateMethod



140
141
142
# File 'lib/canon/rspec_matchers.rb', line 140

def diffable # rubocop:disable Naming/PredicateMethod
  false
end

#expectedObject



132
133
134
# File 'lib/canon/rspec_matchers.rb', line 132

def expected
  @expected
end

#failure_messageObject



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

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

#failure_message_when_negatedObject



128
129
130
# File 'lib/canon/rspec_matchers.rb', line 128

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

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/canon/rspec_matchers.rb', line 90

def matches?(target)
  @target = target

  # Build comparison options from config and matcher params
  opts = build_comparison_options

  # Add format hint if explicitly provided
  opts[:format] = @format if @format

  # Delegate to Canon::Comparison.equivalent? - the SINGLE source of truth
  # Comparison handles format detection, HTML parsing, and all business logic
  @comparison_result = Canon::Comparison.equivalent?(
    @expected,
    @target,
    opts,
  )

  # When verbose: true, result is a ComparisonResult object
  # Use the equivalent? method to check for normative differences
  case @comparison_result
  when Canon::Comparison::ComparisonResult
    @comparison_result.equivalent?
  when Hash
    # Legacy format - Hash with :differences array and :preprocessed strings
    @comparison_result[:differences].empty?
  when Array
    # Legacy format - XML/JSON/YAML returns []
    @comparison_result.empty?
  else
    # Boolean result
    @comparison_result
  end
end

#show_diffs(value) ⇒ SerializationMatcher

Chain method for controlling diff display

Parameters:

  • value (Symbol, String)

    :all, :normative, or :informative

Returns:



61
62
63
64
# File 'lib/canon/rspec_matchers.rb', line 61

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:



69
70
71
72
73
# File 'lib/canon/rspec_matchers.rb', line 69

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:



86
87
88
# File 'lib/canon/rspec_matchers.rb', line 86

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:



78
79
80
81
# File 'lib/canon/rspec_matchers.rb', line 78

def with_profile(profile_name)
  @match_profile = profile_name
  self
end