equivalent-xml 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -17,8 +17,9 @@ EquivalentXml for Nokogiri
17
17
  === Use
18
18
  EquivalentXml.equivalent?(node_1, node_2, opts = { :element_order => false, :normalize_whitespace => true })
19
19
 
20
- node_1 and node_2 can be any Nokogiri::XML::Node descendants. The most common
21
- use is to compare two Nokogiri::XML::Document instances.
20
+ node_1 and node_2 can be any Nokogiri::XML::Node descendants (or any string
21
+ containing an XML document or document fragment). The most common use case is
22
+ to compare two Nokogiri::XML::Document instances.
22
23
 
23
24
  node_1 is equivalent to node_2 if and only if:
24
25
  * node_1 and node_2 are of the same class
@@ -1,25 +1,14 @@
1
1
  module EquivalentXml
2
-
3
- ELEMENT_NODE = 1
4
- ATTRIBUTE_NODE = 2
5
- TEXT_NODE = 3
6
- CDATA_SECTION_NODE = 4
7
- ENTITY_REFERENCE_NODE = 5
8
- ENTITY_NODE = 6
9
- PROCESSING_INSTRUCTION_NODE = 7
10
- COMMENT_NODE = 8
11
- DOCUMENT_NODE = 9
12
- DOCUMENT_TYPE_NODE = 10
13
- DOCUMENT_FRAGMENT_NODE = 11
14
- NOTATION_NODE = 12
2
+
3
+ require 'nokogiri'
15
4
 
16
5
  class << self
17
6
 
18
- DEFAULT_OPTS = { :element_order => false, :normalize_whitespace => true }
7
+ DEFAULT_OPTS = { :element_order => false, :normalize_whitespace => true }
19
8
 
20
9
  def equivalent?(node_1, node_2, opts = {}, &block)
21
10
  opts = DEFAULT_OPTS.merge(opts)
22
- self.compare_nodes(node_1, node_2, opts, &block)
11
+ self.compare_nodes(self.as_node(node_1), self.as_node(node_2), opts, &block)
23
12
  end
24
13
 
25
14
  def compare_nodes(node_1, node_2, opts, &block)
@@ -29,15 +18,15 @@ module EquivalentXml
29
18
  false
30
19
  else
31
20
  case node_1.node_type
32
- when DOCUMENT_NODE
21
+ when Nokogiri::XML::Node::DOCUMENT_NODE
33
22
  self.compare_documents(node_1,node_2,opts,&block)
34
- when ELEMENT_NODE
23
+ when Nokogiri::XML::Node::ELEMENT_NODE
35
24
  self.compare_elements(node_1,node_2,opts,&block)
36
- when ATTRIBUTE_NODE
25
+ when Nokogiri::XML::Node::ATTRIBUTE_NODE
37
26
  self.compare_attributes(node_1,node_2,opts,&block)
38
- when CDATA_SECTION_NODE
27
+ when Nokogiri::XML::Node::CDATA_SECTION_NODE
39
28
  self.compare_cdata(node_1,node_2,opts,&block)
40
- when TEXT_NODE
29
+ when Nokogiri::XML::Node::TEXT_NODE
41
30
  self.compare_text(node_1,node_2,opts,&block)
42
31
  else
43
32
  self.compare_children(node_1,node_2,opts,&block)
@@ -71,9 +60,9 @@ module EquivalentXml
71
60
 
72
61
  def compare_children(node_1, node_2, opts, &block)
73
62
  ignore_proc = lambda do |child|
74
- child.is_a?(Nokogiri::XML::Comment) ||
75
- child.is_a?(Nokogiri::XML::ProcessingInstruction) ||
76
- (child.class == Nokogiri::XML::Text && child.text.strip.empty?)
63
+ child.node_type == Nokogiri::XML::Node::COMMENT_NODE ||
64
+ child.node_type == Nokogiri::XML::Node::PI_NODE ||
65
+ (opts[:normalize_whitespace] && child.node_type == Nokogiri::XML::Node::TEXT_NODE && child.text.strip.empty?)
77
66
  end
78
67
 
79
68
  nodeset_1 = node_1.children.reject { |child| ignore_proc.call(child) }
@@ -125,7 +114,15 @@ module EquivalentXml
125
114
  href2 = node_2.namespace.nil? ? '' : node_2.namespace.href
126
115
  return href1 == href2
127
116
  end
128
-
117
+
118
+ def as_node(data)
119
+ if data.respond_to?(:node_type)
120
+ return data
121
+ else
122
+ return Nokogiri::XML(data)
123
+ end
124
+ end
125
+
129
126
  end
130
127
 
131
128
  end
@@ -104,4 +104,13 @@ describe EquivalentXml do
104
104
  EquivalentXml.equivalent?(doc1,doc2).should == false
105
105
  end
106
106
 
107
+ it "should properly handle documents passed in as strings" do
108
+ doc1 = "<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>"
109
+ doc2 = "<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>"
110
+
111
+ doc1 = "<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>"
112
+ doc2 = "<doc xmlns='foo:bar'><first order='1'>foo bar baz quux</first><second>things</second></doc>"
113
+ EquivalentXml.equivalent?(doc1,doc2).should == false
114
+ end
115
+
107
116
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: equivalent-xml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
4
+ hash: 17
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael B. Klein
@@ -30,7 +30,7 @@ dependencies:
30
30
  segments:
31
31
  - 0
32
32
  version: "0"
33
- type: :development
33
+ type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: bundler
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  requirements: []
125
125
 
126
126
  rubyforge_project:
127
- rubygems_version: 1.3.7
127
+ rubygems_version: 1.5.2
128
128
  signing_key:
129
129
  specification_version: 3
130
130
  summary: Easy equivalency tests for Ruby XML