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)

    Options

  • differences (Array)

    Array to append difference to



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 159

def self.add_attribute_difference(n1:, n2:, diff1:, diff2:,
dimension:, differences:, **opts)
  # Import DiffNodeBuilder to avoid circular dependency
  require_relative "diff_node_builder"

  diff_node = Canon::Comparison::DiffNodeBuilder.build(
    node1: n1,
    node2: n2,
    diff1: diff1,
    diff2: diff2,
    dimension: dimension,
    **opts,
  )
  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
# 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 = node1.respond_to?(:attribute_nodes) ? node1.attribute_nodes : node1.attributes
  raw_attrs2 = node2.respond_to?(:attribute_nodes) ? node2.attribute_nodes : node2.attributes

  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

  # 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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 133

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



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/comparison/xml_comparator/attribute_comparator.rb', line 94

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/canon/comparison/xml_comparator/attribute_comparator.rb', line 53

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