Permalink
Browse files

Removed explicit runtime dependency on nokogiri (though some implemen…

…tation may still be specific to nokogiri's interface)

Removed development dependency on Jeweler
Made whitespace normalization of text nodes optional
  • Loading branch information...
1 parent 53bb6cf commit 9473eaa8e441fc148e50cff476884e2f65d6edd5 @mbklein committed Feb 17, 2011
Showing with 75 additions and 91 deletions.
  1. +1 −0 .rvmrc
  2. +1 −4 Gemfile
  3. +0 −9 Gemfile.lock
  4. +3 −18 Rakefile
  5. +8 −36 equivalent-xml.gemspec
  6. +41 −22 lib/equivalent-xml.rb
  7. +21 −2 spec/equvalent-xml_spec.rb
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm gemset use equivalent-xml
View
@@ -1,11 +1,8 @@
source "http://rubygems.org"
-gem "nokogiri"
-
group :development do
- gem "shoulda", ">= 0"
+ gem "nokogiri"
gem "bundler", "~> 1.0.0"
- gem "jeweler", "~> 1.5.2"
gem "rcov", ">= 0"
gem "rspec"
end
View
@@ -2,13 +2,7 @@ GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
- git (1.2.5)
- jeweler (1.5.2)
- bundler (~> 1.0.0)
- git (>= 1.2.5)
- rake
nokogiri (1.4.4)
- rake (0.8.7)
rcov (0.9.9)
rspec (2.5.0)
rspec-core (~> 2.5.0)
@@ -18,15 +12,12 @@ GEM
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
- shoulda (2.11.3)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1.0.0)
- jeweler (~> 1.5.2)
nokogiri
rcov
rspec
- shoulda
View
@@ -1,5 +1,8 @@
require 'rubygems'
require 'bundler'
+require 'rake/tasklib'
+require 'rake/rdoctask'
+
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
@@ -9,31 +12,13 @@ rescue Bundler::BundlerError => e
end
require 'rake'
-require 'jeweler'
-Jeweler::Tasks.new do |gem|
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
- gem.name = "equivalent-xml"
- gem.homepage = "http://github.com/mbklein/equivalent-xml"
- gem.license = "MIT"
- gem.summary = %Q{Easy equivalency tests for Nokogiri::XML}
- gem.description = %Q{Compares two Nokogiri::XML::Nodes (Documents, etc.) for certain semantic equivalencies}
- gem.email = "mbklein@gmail.com"
- gem.authors = ["Michael B. Klein"]
- # Include your dependencies below. Runtime dependencies are required when using your gem,
- # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
- # gem.add_runtime_dependency 'jabber4r', '> 0.1'
- # gem.add_development_dependency 'rspec', '> 1.2.3'
-end
-Jeweler::RubygemsDotOrgTasks.new
-
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |t|
t.pattern = FileList['./spec/**/*_spec.rb']
end
task :default => :spec
-require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
View
@@ -1,64 +1,36 @@
-# Generated by jeweler
-# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
-# -*- encoding: utf-8 -*-
-
Gem::Specification.new do |s|
s.name = %q{equivalent-xml}
- s.version = "0.1.0"
+ s.version = "0.1.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Michael B. Klein"]
s.date = %q{2011-02-17}
- s.description = %q{Compares two Nokogiri::XML::Nodes (Documents, etc.) for certain semantic equivalencies}
+ s.description = %q{Compares two XML Nodes (Documents, etc.) for certain semantic equivalencies.
+ Currently written for Nokogiri, but with an eye toward supporting multiple XML libraries}
s.email = %q{mbklein@gmail.com}
s.extra_rdoc_files = [
"LICENSE.txt",
"README.rdoc"
]
s.files = [
- ".document",
"Gemfile",
"LICENSE.txt",
"README.rdoc",
"Rakefile",
- "VERSION",
"lib/equivalent-xml.rb"
]
s.homepage = %q{http://github.com/mbklein/equivalent-xml}
s.licenses = ["MIT"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.5.2}
- s.summary = %q{Easy equivalency tests for Nokogiri::XML}
+ s.summary = %q{Easy equivalency tests for Ruby XML}
s.test_files = [
"spec/equvalent-xml_spec.rb"
]
- if s.respond_to? :specification_version then
- s.specification_version = 3
-
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
- s.add_development_dependency(%q<shoulda>, [">= 0"])
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
- s.add_development_dependency(%q<rcov>, [">= 0"])
- s.add_development_dependency(%q<rspec>, [">= 0"])
- else
- s.add_dependency(%q<nokogiri>, [">= 0"])
- s.add_dependency(%q<shoulda>, [">= 0"])
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
- s.add_dependency(%q<rcov>, [">= 0"])
- s.add_dependency(%q<rspec>, [">= 0"])
- end
- else
- s.add_dependency(%q<nokogiri>, [">= 0"])
- s.add_dependency(%q<shoulda>, [">= 0"])
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
- s.add_dependency(%q<rcov>, [">= 0"])
- s.add_dependency(%q<rspec>, [">= 0"])
- end
+ s.add_development_dependency(%q<nokogiri>, [">= 0"])
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_development_dependency(%q<rcov>, [">= 0"])
+ s.add_development_dependency(%q<rspec>, [">= 0"])
end
View
@@ -1,30 +1,45 @@
-require 'nokogiri'
-
module EquivalentXml
class << self
- def equivalent?(node_1, node_2, opts = { :element_order => false })
+ ELEMENT_NODE = 1
+ ATTRIBUTE_NODE = 2
+ TEXT_NODE = 3
+ CDATA_SECTION_NODE = 4
+ ENTITY_REFERENCE_NODE = 5
+ ENTITY_NODE = 6
+ PROCESSING_INSTRUCTION_NODE = 7
+ COMMENT_NODE = 8
+ DOCUMENT_NODE = 9
+ DOCUMENT_TYPE_NODE = 10
+ DOCUMENT_FRAGMENT_NODE = 11
+ NOTATION_NODE = 12
+
+ DEFAULT_OPTS = { :element_order => false, :normalize_whitespace => true }
+
+ def equivalent?(node_1, node_2, opts = {})
+ opts = DEFAULT_OPTS.merge(opts)
self.compare_nodes(node_1, node_2, opts)
end
def compare_nodes(node_1, node_2, opts)
- if node_1.class != node_2.class
- return false
- elsif self.same_namespace?(node_1,node_2) == false
- return false
- elsif node_1.is_a?(Nokogiri::XML::Document)
- return self.compare_documents(node_1,node_2,opts)
- elsif node_1.is_a?(Nokogiri::XML::Element)
- return self.compare_elements(node_1,node_2,opts)
- elsif node_1.is_a?(Nokogiri::XML::Attr)
- return self.compare_attributes(node_1,node_2)
- elsif node_1.is_a?(Nokogiri::XML::CDATA)
- return self.compare_cdata(node_1,node_2)
- elsif node_1.is_a?(Nokogiri::XML::CharacterData)
- return self.compare_text(node_1,node_2)
+ if (node_1.class != node_2.class) or self.same_namespace?(node_1,node_2) == false
+ false
else
- return self.compare_children(node_1,node_2,opts)
+ case node_1.node_type
+ when DOCUMENT_NODE
+ self.compare_documents(node_1,node_2,opts)
+ when ELEMENT_NODE
+ self.compare_elements(node_1,node_2,opts)
+ when ATTRIBUTE_NODE
+ self.compare_attributes(node_1,node_2,opts)
+ when CDATA_SECTION_NODE
+ self.compare_cdata(node_1,node_2,opts)
+ when TEXT_NODE
+ self.compare_text(node_1,node_2,opts)
+ else
+ self.compare_children(node_1,node_2,opts)
+ end
end
end
@@ -36,15 +51,19 @@ def compare_elements(node_1, node_2, opts)
(node_1.name == node_2.name) && self.compare_children(node_1,node_2,opts)
end
- def compare_attributes(node_1, node_2)
+ def compare_attributes(node_1, node_2, opts)
(node_1.name == node_2.name) && (node_1.value == node_2.value)
end
- def compare_text(node_1, node_2)
- node_1.text.strip.gsub(/\s+/,' ') == node_2.text.strip.gsub(/\s+/,' ')
+ def compare_text(node_1, node_2, opts)
+ if opts[:normalize_whitespace]
+ node_1.text.strip.gsub(/\s+/,' ') == node_2.text.strip.gsub(/\s+/,' ')
+ else
+ node_1.text == node_2.text
+ end
end
- def compare_cdata(node_1, node_2)
+ def compare_cdata(node_1, node_2, opts)
node_1.text == node_2.text
end
View
@@ -1,4 +1,5 @@
$:.push(File.join(File.dirname(__FILE__),'..','lib'))
+require 'nokogiri'
require 'equivalent-xml'
describe EquivalentXml do
@@ -48,13 +49,19 @@
EquivalentXml.equivalent?(doc1,doc2).should == false
end
- it "should normalize simple whitespace" do
+ it "should normalize simple whitespace by default" do
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
EquivalentXml.equivalent?(doc1,doc2).should == true
end
- it "should normalize complex whitespace" do
+ it "shouldn't normalize simple whitespace if :normalize_whitespace => false is specified" do
+ doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
+ doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
+ EquivalentXml.equivalent?(doc1,doc2, :normalize_whitespace => false).should == false
+ end
+
+ it "should normalize complex whitespace by default" do
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
doc2 = Nokogiri::XML(%{<doc xmlns='foo:bar'>
<second>things</second>
@@ -66,6 +73,18 @@
EquivalentXml.equivalent?(doc1,doc2).should == true
end
+ it "shouldn't normalize complex whitespace if :normalize_whitespace => false is specified" do
+ doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
+ doc2 = Nokogiri::XML(%{<doc xmlns='foo:bar'>
+ <second>things</second>
+ <first>
+ foo
+ bar baz
+ </first>
+ </doc>})
+ EquivalentXml.equivalent?(doc1,doc2, :normalize_whitespace => false).should == false
+ end
+
it "should ignore comment nodes" do
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
doc2 = Nokogiri::XML(%{<doc xmlns='foo:bar'>

0 comments on commit 9473eaa

Please sign in to comment.