$:.push(File.join(File.dirname(__FILE__),'..','lib')) require 'nokogiri' require 'equivalent-xml' #require 'equivalent-xml/rspec_matchers' 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 end