Added XML::Attribute#==

Overwriting this method makes it easier to check if a given namespace
equals the default XML (and soon HTML) namespace.
This commit is contained in:
Yorick Peterse 2015-03-26 00:53:16 +01:00
parent f2d69af33b
commit 4ad502958d
2 changed files with 46 additions and 0 deletions

View File

@ -37,6 +37,14 @@ module Oga
def inspect def inspect
return "Namespace(name: #{name.inspect} uri: #{uri.inspect})" return "Namespace(name: #{name.inspect} uri: #{uri.inspect})"
end end
##
# @param [Oga::XML::Namespace] other
# @return [TrueClass|FalseClass]
#
def ==(other)
return other.is_a?(self.class) && name == other.name && uri == other.uri
end
end # Namespace end # Namespace
end # XML end # XML
end # Oga end # Oga

View File

@ -20,4 +20,42 @@ describe Oga::XML::Namespace do
ns.inspect.should == 'Namespace(name: "x" uri: "y")' ns.inspect.should == 'Namespace(name: "x" uri: "y")'
end end
end end
describe '#==' do
describe 'when comparing with an object that is not a Namespace' do
ns = described_class.new(:name => 'a')
ns.should_not == 10
end
describe 'when comparing two Namespace instances' do
it 'returns true if both namespace names are equal' do
ns1 = described_class.new(:name => 'a')
ns2 = described_class.new(:name => 'a')
ns1.should == ns2
end
it 'returns true if both namespace names and URIs are equal' do
ns1 = described_class.new(:name => 'a', :uri => 'foo')
ns2 = described_class.new(:name => 'a', :uri => 'foo')
ns1.should == ns2
end
it 'returns false if two namespace names are not equal' do
ns1 = described_class.new(:name => 'a')
ns2 = described_class.new(:name => 'b')
ns1.should_not == ns2
end
it 'retrns false if two namespace URIs are not equal' do
ns1 = described_class.new(:name => 'a', :uri => 'foo')
ns2 = described_class.new(:name => 'a', :uri => 'bar')
ns1.should_not == ns2
end
end
end
end end