Module: Canon::Comparison::MatchOptions

Defined in:
lib/canon/comparison/match_options.rb,
lib/canon/comparison/match_options/xml_resolver.rb,
lib/canon/comparison/match_options/base_resolver.rb,
lib/canon/comparison/match_options/json_resolver.rb,
lib/canon/comparison/match_options/yaml_resolver.rb

Overview

Module containing match option utilities and format-specific modules

Defined Under Namespace

Modules: Json, Xml, Yaml Classes: BaseResolver, JsonResolver, XmlResolver, YamlResolver

Constant Summary collapse

PREPROCESSING_OPTIONS =

Preprocessing options - what to do before comparison

%i[none c14n normalize format rendered].freeze
MATCH_BEHAVIORS =

Matching behaviors (deprecated - use per-dimension validation instead) This universal constant is kept for backward compatibility but should not be used for validation. Use BaseResolver.dimension_behaviors instead. Note: :strip and :compact are only valid for attribute_values dimension.

%i[strict strip compact normalize ignore].freeze

Class Method Summary collapse

Class Method Details

.match_text?(text1, text2, behavior, whitespace_type: :strict) ⇒ Boolean

Apply match behavior to text comparison

Parameters:

  • text1 (String)

    First text

  • text2 (String)

    Second text

  • behavior (Symbol)

    Match behavior (:strict, :normalize, :ignore)

  • whitespace_type (Symbol) (defaults to: :strict)

    Whitespace type handling (:strict, :normalize)

Returns:

  • (Boolean)

    true if texts match according to behavior



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/canon/comparison/match_options.rb', line 68

def match_text?(text1, text2, behavior, whitespace_type: :strict)
  case behavior
  when :strict
    text1 == text2
  when :normalize
    if whitespace_type == :normalize
      normalize_text(text1) == normalize_text(text2)
    else
      normalize_text_preserving_type(text1) == normalize_text_preserving_type(text2)
    end
  when :ignore
    true
  else
    raise Canon::Error, "Unknown match behavior: #{behavior}"
  end
end

.normalize_text(text) ⇒ Object

Normalize text by collapsing whitespace and trimming Mimics HTML whitespace collapsing



87
88
89
90
91
92
93
# File 'lib/canon/comparison/match_options.rb', line 87

def normalize_text(text)
  return "" if text.nil?

  text.to_s
    .gsub(/[\p{Space} ]+/, " ") # Collapse all whitespace to single space
    .strip # Remove leading/trailing whitespace
end

.normalize_text_preserving_type(text) ⇒ Object

Normalize text preserving Unicode whitespace type distinctions.



96
97
98
99
100
101
102
# File 'lib/canon/comparison/match_options.rb', line 96

def normalize_text_preserving_type(text)
  return "" if text.nil?

  text.to_s
    .gsub(/[ \t\r\n\f\v]+/, " ") # Collapse only ASCII whitespace
    .strip
end

.process_attribute_value(value, behavior) ⇒ Object

Process attribute value according to match behavior



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/canon/comparison/match_options.rb', line 105

def process_attribute_value(value, behavior)
  case behavior
  when :strict
    value.to_s
  when :strip
    value.to_s.strip
  when :compact
    value.to_s.gsub(/[\p{Space} ]+/, " ")
  when :normalize
    normalize_text(value)
  when :ignore
    ""
  else
    raise Canon::Error, "Unknown attribute value behavior: #{behavior}"
  end
end