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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/canon/comparison/match_options.rb', line 74

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) ⇒ String

Normalize text by collapsing whitespace and trimming Mimics HTML whitespace collapsing

Handles both ASCII and Unicode whitespace characters including:

  • Regular space (U+0020)

  • Non-breaking space (U+00A0)

  • Other Unicode whitespace per pSpace

Parameters:

  • text (String)

    Text to normalize

Returns:

  • (String)

    Normalized text



101
102
103
104
105
106
107
# File 'lib/canon/comparison/match_options.rb', line 101

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

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

.normalize_text_preserving_type(text) ⇒ String

Normalize text preserving Unicode whitespace type distinctions.

Only ASCII whitespace (space, tab, newline, etc.) is collapsed. Unicode whitespace (NBSP, ideographic space, etc.) is preserved, so different whitespace types remain distinguishable.

Parameters:

  • text (String)

    Text to normalize

Returns:

  • (String)

    Normalized text with preserved whitespace types



117
118
119
120
121
122
123
# File 'lib/canon/comparison/match_options.rb', line 117

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) ⇒ String

Process attribute value according to match behavior

Parameters:

  • value (String)

    Attribute value to process

  • behavior (Symbol)

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

Returns:

  • (String)

    Processed value



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/canon/comparison/match_options.rb', line 130

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}\u00a0]+/, " ")
  when :normalize
    normalize_text(value)
  when :ignore
    ""
  else
    raise Canon::Error, "Unknown attribute value behavior: #{behavior}"
  end
end