Class: Hexp::Nokogiri::Equality
- Inherits:
-
Object
- Object
- Hexp::Nokogiri::Equality
- 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
-
#call ⇒ Boolean
Perform the comparison.
-
#compare_children ⇒ Boolean
Compare the child elements, assuming both elements respond_to? :children.
-
#equal_attributes? ⇒ Boolean
Do the elements under comparison have the same attributes.
-
#equal_children? ⇒ Boolean
Do the elements under comparison have the same child elements.
-
#equal_class? ⇒ Boolean
Are the two elements instances of the same class.
-
#equal_name? ⇒ Boolean
Do both elements have the same tag name.
-
#equal_text? ⇒ Boolean
Compare the text of text elements.
-
#initialize(this, that) ⇒ Equality
constructor
Create a new equality tester for two Nokogiri objects.
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)
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
#call ⇒ Boolean
Perform the comparison
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_children ⇒ Boolean
Compare the child elements, assuming both elements respond_to? :children
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
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
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
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
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
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 |