if defined?(RUBY_ENGINE) and (RUBY_ENGINE == 'ruby') and (RUBY_VERSION >= '1.9')
require 'simplecov'
SimpleCov.start
end
$:.push(File.join(File.dirname(__FILE__),'..','lib'))
require 'nokogiri'
require 'equivalent-xml'
describe EquivalentXml do
it "should consider a document equivalent to itself" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc1.should be_equivalent_to(doc1)
end
it "should compare non-XML content based on its string representation" do
nil.should be_equivalent_to(nil)
''.should be_equivalent_to('')
''.should be_equivalent_to(nil)
'foo'.should be_equivalent_to('foo')
'foo'.should_not be_equivalent_to('bar')
doc1 = Nokogiri::XML("foo bar bazthings")
doc1.should_not be_equivalent_to(nil)
end
it "should ensure that attributes match" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should_not be_equivalent_to(doc2)
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should be_equivalent_to(doc2)
end
it "shouldn't care about attribute order" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should be_equivalent_to(doc2)
end
it "shouldn't care about element order by default" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("thingsfoo bar baz")
doc1.should be_equivalent_to(doc2)
end
it "should care about element order if :element_order => true is specified" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("thingsfoo bar baz")
doc1.should_not be_equivalent_to(doc2).respecting_element_order
end
it "should ensure nodesets have the same number of elements" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("thingsfoo bar baz")
doc1.should_not be_equivalent_to(doc2)
end
it "should ensure namespaces match" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should_not be_equivalent_to(doc2)
end
it "should compare namespaces based on URI, not on prefix" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should be_equivalent_to(doc2)
end
it "should ignore declared but unused namespaces" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should be_equivalent_to(doc2)
end
it "should normalize simple whitespace by default" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should be_equivalent_to(doc2)
end
it "shouldn't normalize simple whitespace if :normalize_whitespace => false is specified" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should_not be_equivalent_to(doc2).with_whitespace_intact
end
it "should normalize complex whitespace by default" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML(%{
things
foo
bar baz
})
doc1.should be_equivalent_to(doc2)
end
it "shouldn't normalize complex whitespace if :normalize_whitespace => false is specified" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML(%{
things
foo
bar baz
})
doc1.should_not be_equivalent_to(doc2).with_whitespace_intact
end
it "should ignore comment nodes" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML(%{
things
foo
bar baz
})
doc1.should be_equivalent_to(doc2)
end
it "should properly handle a mixture of text and element nodes" do
doc1 = Nokogiri::XML("This phrase has bold text in it.")
doc2 = Nokogiri::XML("This phrase in has bold text it.")
doc1.should_not be_equivalent_to(doc2)
end
it "should properly handle documents passed in as strings" do
doc1 = "foo bar bazthings"
doc2 = "foo bar bazthings"
doc1.should be_equivalent_to(doc2)
doc1 = "foo bar bazthings"
doc2 = "foo bar baz quuxthings"
doc1.should_not be_equivalent_to(doc2)
end
it "should compare nodesets" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc1.root.children.should be_equivalent_to(doc1.root.children)
end
it "should compare nodeset with string" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc1.root.children.should be_equivalent_to("foo bar bazthings")
end
context "with the :ignore_content_paths option set to a CSS selector" do
it "ignores the text content of a node that matches the given CSS selector when comparing with #equivalent?" do
doc1 = Nokogiri::XML("iPhone1234")
doc2 = Nokogiri::XML("iPhone5678")
EquivalentXml.equivalent?(doc1, doc2, :ignore_content => "SerialNumber").should be_true
EquivalentXml.equivalent?(doc1, doc2, :ignore_content => "Devices>Device>SerialNumber").should be_true
doc1.should be_equivalent_to(doc2).ignoring_content_of("SerialNumber")
doc1.should be_equivalent_to(doc2).ignoring_content_of("Devices>Device>SerialNumber")
end
it "ignores the text content of a node that matches the given CSS selector when comparing with a matcher" do
doc1 = Nokogiri::XML("iPhone1234")
doc2 = Nokogiri::XML("iPhone5678")
doc1.should be_equivalent_to(doc2).ignoring_content_of("SerialNumber")
doc1.should be_equivalent_to(doc2).ignoring_content_of("Devices>Device>SerialNumber")
end
it "ignores all children of a node that matches the given selector when comparing for equivalence" do
doc1 = Nokogiri::XML("iPhone1234")
doc2 = Nokogiri::XML("iPad5678")
doc1.should be_equivalent_to(doc2).ignoring_content_of("Device")
end
it "still considers the number of elements even if they match the given CSS selector" do
doc1 = Nokogiri::XML("iPhone1234")
doc2 = Nokogiri::XML("iPhone1234iPad5678")
doc1.should_not be_equivalent_to(doc2).ignoring_content_of("Device")
end
it "still considers attributes on the matched path when comparing for equivalence" do
doc1 = Nokogiri::XML("iPhone1234")
doc2 = Nokogiri::XML("iPhone1234")
doc1.should_not be_equivalent_to(doc2).ignoring_content_of("Device")
end
it "ignores all matches of the CSS selector" do
doc1 = Nokogiri::XML("iPhone1001iPad2001")
doc2 = Nokogiri::XML("iPhone1002iPad2002")
doc1.should be_equivalent_to(doc2).ignoring_content_of("SerialNumber")
end
end
context "with the :ignore_content_paths option set to an array of CSS selectors" do
it "ignores the content of all nodes that match any of the given CSS selectors when comparing for equivalence" do
doc1 = Nokogiri::XML("iPhone1234AAAA")
doc2 = Nokogiri::XML("iPhone5678BBBB")
doc1.should be_equivalent_to(doc2).ignoring_content_of(["SerialNumber", "ICCID"])
end
end
context "with :ignore_attr_values set to true" do
it "ignores the values of attributes when comparing for equivalence" do
doc1 = Nokogiri::XML("foo bar bazthings")
doc2 = Nokogiri::XML("foo bar bazthings")
doc1.should be_equivalent_to(doc2).ignoring_attr_values
end
end
end