Class: Hexp::Nokogiri::Equality

Inherits:
Object
  • Object
show all
Defined in:
lib/hexp/nokogiri/equality.rb

Overview

Used in test to see if two Nokogiri objects have the same content, i.e. are equivalent as far as we are concerned

Constant Summary collapse

CLASSES =
[
  ::Nokogiri::HTML::Document,
  ::Nokogiri::HTML::DocumentFragment,
  ::Nokogiri::XML::Document,
  ::Nokogiri::XML::Node,
  ::Nokogiri::XML::Text,
  ::Nokogiri::XML::Element,
  ::Nokogiri::XML::DocumentFragment,
  ::Nokogiri::XML::DTD,
]

Instance Method Summary collapse

Constructor Details

#initialize(this, that) ⇒ Equality

Create a new equality tester for two Nokogiri objects

(see Hexp::Nokogiri::Equality::CLASSES for all possible objects that can be passed in)

Examples:

doc = Nokogiri::HTML::Document.new
this_node = Nokogiri::XML::Element.new("div", doc)
that_node = Nokogiri::XML::Element.new("span", doc)
Hexp::Nokogiri::Equality.new(this_node, that_node).call #=> false

Parameters:

  • this (Object)

    The first object to compare

  • that (Object)

    The second object to compare



34
35
36
37
38
39
# File 'lib/hexp/nokogiri/equality.rb', line 34

def initialize(this, that)
  @this, @that = this, that
  [this, that].each do |input|
    raise "#{input.class} is not a Nokogiri element." unless CLASSES.include?(input.class)
  end
end

Instance Method Details

#callBoolean

Perform the comparison

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/hexp/nokogiri/equality.rb', line 47

def call
  [ equal_class?,
    equal_name?,
    equal_children?,
    equal_attributes?,
    equal_text? ].all?
end

#compare_childrenBoolean

Compare the child elements, assuming both elements respond_to? :children

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/hexp/nokogiri/equality.rb', line 93

def compare_children
  @this.children.map.with_index do |child, idx|
    self.class.new(child, @that.children[idx]).call
  end
end

#equal_attributes?Boolean

Do the elements under comparison have the same attributes

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/hexp/nokogiri/equality.rb', line 105

def equal_attributes?
  return true unless @this.respond_to? :attributes
  @this.attributes.keys.all? do |key|
    @this[key] == @that[key]
  end
end

#equal_children?Boolean

Do the elements under comparison have the same child elements

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/hexp/nokogiri/equality.rb', line 81

def equal_children?
  return true unless @this.respond_to? :children
  @this.children.count == @that.children.count &&
    compare_children.all?
end

#equal_class?Boolean

Are the two elements instances of the same class

Returns:

  • (Boolean)


61
62
63
# File 'lib/hexp/nokogiri/equality.rb', line 61

def equal_class?
  @this.class == @that.class
end

#equal_name?Boolean

Do both elements have the same tag name

Returns:

  • (Boolean)


71
72
73
# File 'lib/hexp/nokogiri/equality.rb', line 71

def equal_name?
  @this.name == @that.name
end

#equal_text?Boolean

Compare the text of text elements

If the elements are not of type Nokogiri::XML::Text, return true

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/hexp/nokogiri/equality.rb', line 120

def equal_text?
  return true unless @this.instance_of?(::Nokogiri::XML::Text)
  @this.text == @that.text
end