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 'rspec/matchers' require 'equivalent-xml' describe EquivalentXml do it "should consider a document equivalent to itself" do doc1 = Nokogiri::XML("foo bar bazthings") expect(doc1).to be_equivalent_to(doc1) end it "should compare non-XML content based on its string representation" do expect(nil).to be_equivalent_to(nil) expect('').to be_equivalent_to('') expect('').to be_equivalent_to(nil) expect('foo').to be_equivalent_to('foo') expect('foo').not_to be_equivalent_to('bar') doc1 = Nokogiri::XML("foo bar bazthings") expect(doc1).not_to be_equivalent_to(nil) end it "should ensure that attributes match" do doc1 = Nokogiri::XML("foo bar bazthings") doc2 = Nokogiri::XML("foo bar bazthings") expect(doc1).not_to be_equivalent_to(doc2) doc1 = Nokogiri::XML("foo bar bazthings") doc2 = Nokogiri::XML("foo bar bazthings") expect(doc1).to 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") expect(doc1).to 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") expect(doc1).to 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") expect(doc1).not_to 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") expect(doc1).not_to be_equivalent_to(doc2) end it "should ensure namespaces match" do doc1 = Nokogiri::XML("foo bar bazthings") doc2 = Nokogiri::XML("foo bar bazthings") expect(doc1).not_to 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") expect(doc1).to 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") expect(doc1).to 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") expect(doc1).to 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") expect(doc1).not_to 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 }) expect(doc1).to 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 }) expect(doc1).not_to 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 }) expect(doc1).to 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.") expect(doc1).not_to be_equivalent_to(doc2) end it "should properly handle documents passed in as strings" do doc1 = "foo bar bazthings" doc2 = "foo bar bazthings" expect(doc1).to be_equivalent_to(doc2) doc1 = "foo bar bazthings" doc2 = "foo bar baz quuxthings" expect(doc1).not_to be_equivalent_to(doc2) end it "should compare nodesets" do doc1 = Nokogiri::XML("foo bar bazthings") expect(doc1.root.children).to be_equivalent_to(doc1.root.children) end it "should compare nodeset with string" do doc1 = Nokogiri::XML("foo bar bazthings") expect(doc1.root.children).to 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") expect(EquivalentXml.equivalent?(doc1, doc2, :ignore_content => "SerialNumber")).to eq(true) expect(EquivalentXml.equivalent?(doc1, doc2, :ignore_content => "Devices>Device>SerialNumber")).to eq(true) expect(doc1).to be_equivalent_to(doc2).ignoring_content_of("SerialNumber") expect(doc1).to 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") expect(doc1).to be_equivalent_to(doc2).ignoring_content_of("SerialNumber") expect(doc1).to 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") expect(doc1).to 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") expect(doc1).not_to 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") expect(doc1).not_to 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") expect(doc1).to be_equivalent_to(doc2).ignoring_content_of("SerialNumber") end it "should properly compare a document to a string" do doc1 = '' doc2 = Nokogiri::XML doc1 expect(doc1).to be_equivalent_to(doc2) expect(doc2).to be_equivalent_to(doc1) 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") expect(doc1).to 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") expect(doc1).to be_equivalent_to(doc2).ignoring_attr_values end end context 'with :ignore_attr_values receiving specific attribute names' do it 'ignores the value of one specified attribute, but fails if other attributes are different, when comparing for equivalence' do doc1 = Nokogiri::XML("foo bar bazthings") doc2 = Nokogiri::XML("foo bar bazthings") expect(doc1).not_to be_equivalent_to(doc2).ignoring_attr_values( 'order' ) end it 'ignores the value of one specified attribute, but succeeds if other attributes match, when comparing for equivalence' do doc1 = Nokogiri::XML("foo bar bazthings") doc2 = Nokogiri::XML("foo bar bazthings") expect(doc1).to be_equivalent_to(doc2).ignoring_attr_values( 'order' ) end it 'ignores the values of multiple specified attributes when comparing for equivalence' do doc1 = Nokogiri::XML("foo bar bazthings") doc2 = Nokogiri::XML("foo bar bazthings") expect(doc1).to be_equivalent_to(doc2).ignoring_attr_values( 'order', 'status' ) end end context "(on fragments consisting of multiple nodes)" do it "should compare all nodes" do doc1 = "

Headline

Headline

" doc2 = "

Headline

Headline2

" expect(doc1).not_to be_equivalent_to(doc2) end end end