diff --git a/lib/oga/xml/node_set.rb b/lib/oga/xml/node_set.rb index cead831..bcf44a8 100644 --- a/lib/oga/xml/node_set.rb +++ b/lib/oga/xml/node_set.rb @@ -197,6 +197,28 @@ module Oga return self.class.new(to_a | other.to_a) end + ## + # Returns `true` if the current node set and the one given in `other` are + # equal to each other. + # + # @param [Oga::XML::NodeSet] other + # + def ==(other) + return other.is_a?(NodeSet) && other.equal_nodes?(@nodes) + end + + ## + # Returns `true` if the nodes given in `nodes` are equal to those + # specified in the current `@nodes` variable. This method allows two + # NodeSet instances to compare each other without the need of exposing + # `@nodes` to the public. + # + # @param [Array] nodes + # + def equal_nodes?(nodes) + return @nodes == nodes + end + ## # Adds the nodes of the given node set to the current node set. # diff --git a/spec/oga/xml/node_set_spec.rb b/spec/oga/xml/node_set_spec.rb index bc2de1e..0197db1 100644 --- a/spec/oga/xml/node_set_spec.rb +++ b/spec/oga/xml/node_set_spec.rb @@ -278,6 +278,23 @@ describe Oga::XML::NodeSet do end end + context '#==' do + before do + node = Oga::XML::Node.new + @set1 = described_class.new([node]) + @set2 = described_class.new([node]) + @set3 = described_class.new + end + + example 'return true if two node sets are equal' do + @set1.should == @set2 + end + + example 'return false if two node sets are not equal' do + @set1.should_not == @set3 + end + end + context '#concat' do before do n1 = Oga::XML::Element.new(:name => 'a')