Class: Canon::Comparison::XmlComparatorHelpers::AttributeComparator

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/comparison/xml_comparator/attribute_comparator.rb

Overview

Attribute comparison logic Handles comparison of attribute sets with filtering and ordering

Class Method Summary collapse

Class Method Details

.add_attribute_difference(n1:, n2:, diff1:, diff2:, dimension:, differences:, opts: {}) ⇒ Object

Add an attribute difference

Parameters:

  • n1 (Object)

    First node

  • n2 (Object)

    Second node

  • diff1 (String)

    Difference type for node1

  • diff2 (String)

    Difference type for node2

  • dimension (Symbol)

    The match dimension

  • opts (Hash) (defaults to: {})

    Options

  • differences (Array)

    Array to append difference to



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 167

def self.add_attribute_difference(n1:, n2:, diff1:, diff2:,
                                  dimension:, differences:, opts: {})
  diff_node = Canon::Comparison::DiffNodeBuilder.build(
    node1: n1,
    node2: n2,
    diff1: diff1,
    diff2: diff2,
    dimension: dimension,
    verbose: opts[:verbose],
  )
  differences << diff_node if diff_node
end

.compare(node1, node2, opts, differences) ⇒ Symbol

Compare attribute sets between two nodes

Parameters:

  • node1 (Object)

    First node

  • node2 (Object)

    Second node

  • opts (Hash)

    Comparison options

  • differences (Array)

    Array to append differences to

Returns:

  • (Symbol)

    Comparison result



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 16

def self.compare(node1, node2, opts, differences)
  # Get attributes using the appropriate method for each node type
  raw_attrs1 = get_raw_attributes(node1)
  raw_attrs2 = get_raw_attributes(node2)

  attrs1 = XmlComparatorHelpers::AttributeFilter.filter(raw_attrs1,
                                                        opts)
  attrs2 = XmlComparatorHelpers::AttributeFilter.filter(raw_attrs2,
                                                        opts)

  match_opts = opts[:match_opts]
  attribute_order_behavior = match_opts[:attribute_order] || :strict

  # FAST PATH: identical filtered attributes in identical key
  # order — exact equality implies behavioral equality for every
  # value behavior, and no order difference exists. Most element
  # pairs hit this and skip the sort/hash/value work entirely.
  if attrs1 == attrs2 && attrs1.keys == attrs2.keys
    return Comparison::EQUIVALENT
  end

  # Check attribute order if not ignored
  keys1 = attrs1.keys.map(&:to_s)
  keys2 = attrs2.keys.map(&:to_s)

  if attribute_order_behavior == :strict
    compare_strict_order(node1, node2, attrs1, attrs2, keys1, keys2, opts,
                         differences)
  else
    compare_flexible_order(node1, node2, attrs1, attrs2, keys1, keys2, opts,
                           differences)
  end
end

.compare_attribute_values(node1, node2, attrs1, attrs2, opts, differences) ⇒ Symbol

Compare attribute values

Parameters:

  • node1 (Object)

    First node

  • node2 (Object)

    Second node

  • attrs1 (Hash)

    First node's attributes

  • attrs2 (Hash)

    Second node's attributes

  • opts (Hash)

    Comparison options

  • differences (Array)

    Array to append differences to

Returns:

  • (Symbol)

    Comparison result



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 141

def self.compare_attribute_values(node1, node2, attrs1, attrs2, opts,
differences)
  attrs1.each do |name, value|
    unless attrs2[name] == value
      add_attribute_difference(n1: node1, n2: node2,
                               diff1: Comparison::UNEQUAL_ATTRIBUTES,
                               diff2: Comparison::UNEQUAL_ATTRIBUTES,
                               dimension: :attribute_values,
                               opts: opts,
                               differences: differences)
      return Comparison::UNEQUAL_ATTRIBUTES
    end
  end

  Comparison::EQUIVALENT
end

.compare_flexible_order(node1, node2, attrs1, attrs2, keys1, keys2, opts, differences) ⇒ Symbol

Compare with flexible attribute ordering

Parameters:

  • node1 (Object)

    First node

  • node2 (Object)

    Second node

  • attrs1 (Hash)

    First node's attributes

  • attrs2 (Hash)

    Second node's attributes

  • keys1 (Array<String>)

    First node's attribute keys

  • keys2 (Array<String>)

    Second node's attribute keys

  • opts (Hash)

    Comparison options

  • differences (Array)

    Array to append differences to

Returns:

  • (Symbol)

    Comparison result



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 102

def self.compare_flexible_order(node1, node2, attrs1, attrs2, keys1, keys2, opts,
differences)
  # Check if order differs (but keys are the same) - track as informative
  if keys1 != keys2 && keys1.sort == keys2.sort && opts[:verbose]
    add_attribute_difference(n1: node1, n2: node2,
                             diff1: Comparison::UNEQUAL_ATTRIBUTES,
                             diff2: Comparison::UNEQUAL_ATTRIBUTES,
                             dimension: :attribute_order,
                             opts: opts,
                             differences: differences)
  end

  # Sort attributes so order doesn't matter for comparison
  attrs1 = attrs1.sort_by { |k, _v| k.to_s }.to_h
  attrs2 = attrs2.sort_by { |k, _v| k.to_s }.to_h

  unless attrs1.keys.map(&:to_s).sort == attrs2.keys.map(&:to_s).sort
    add_attribute_difference(n1: node1, n2: node2,
                             diff1: Comparison::MISSING_ATTRIBUTE,
                             diff2: Comparison::MISSING_ATTRIBUTE,
                             dimension: :attribute_presence,
                             opts: opts,
                             differences: differences)
    return Comparison::MISSING_ATTRIBUTE
  end

  compare_attribute_values(node1, node2, attrs1, attrs2, opts,
                           differences)
end

.compare_strict_order(node1, node2, attrs1, attrs2, keys1, keys2, opts, differences) ⇒ Symbol

Compare with strict attribute ordering

Parameters:

  • node1 (Object)

    First node

  • node2 (Object)

    Second node

  • attrs1 (Hash)

    First node's attributes

  • attrs2 (Hash)

    Second node's attributes

  • keys1 (Array<String>)

    First node's attribute keys

  • keys2 (Array<String>)

    Second node's attribute keys

  • opts (Hash)

    Comparison options

  • differences (Array)

    Array to append differences to

Returns:

  • (Symbol)

    Comparison result



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 61

def self.compare_strict_order(node1, node2, attrs1, attrs2, keys1, keys2, opts,
differences)
  if keys1 != keys2
    # Keys are different or in different order
    if keys1.sort == keys2.sort
      # Same keys, different order - attribute_order difference
      add_attribute_difference(n1: node1, n2: node2,
                               diff1: Comparison::UNEQUAL_ATTRIBUTES,
                               diff2: Comparison::UNEQUAL_ATTRIBUTES,
                               dimension: :attribute_order,
                               opts: opts,
                               differences: differences)
      return Comparison::UNEQUAL_ATTRIBUTES
    else
      # Different keys - attribute_presence difference
      add_attribute_difference(n1: node1, n2: node2,
                               diff1: Comparison::MISSING_ATTRIBUTE,
                               diff2: Comparison::MISSING_ATTRIBUTE,
                               dimension: :attribute_presence,
                               opts: opts,
                               differences: differences)
      return Comparison::MISSING_ATTRIBUTE
    end
  end

  # Order matches, check values
  compare_attribute_values(node1, node2, attrs1, attrs2, opts,
                           differences)
end

.get_raw_attributes(node) ⇒ Object

Get raw attributes from a node, dispatching by type. Nokogiri::XML::Element has attribute_nodes (NodeSet), Canon::Xml::Nodes::ElementNode has attribute_nodes (Array), Moxml::Element has attributes (Hash-like).



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 184

def self.get_raw_attributes(node)
  case node
  when Canon::Xml::Nodes::ElementNode
    node.attribute_nodes
  else
    if defined?(Nokogiri) && node.is_a?(Nokogiri::XML::Element)
      node.attribute_nodes
    else
      node.attributes
    end
  end
end