equivalent-xml 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -88,6 +88,7 @@ Chained modifiers:
88
88
 
89
89
  == History
90
90
 
91
+ - <b>0.2.8</b> - Allow comparison against nodesets (contrib. by gkellogg)
91
92
  - <b>0.2.7</b> - Auto-require RSpec matchers if RSpec is loaded
92
93
  - <b>0.2.6</b> - Added documentation for RSpec matchers
93
94
  - <b>0.2.5</b> - Added YARD documentation
@@ -8,8 +8,8 @@ module EquivalentXml
8
8
 
9
9
  # Determine if two XML documents or nodes are equivalent
10
10
  #
11
- # @param [Nokogiri::XML::Node] node_1 The first top-level XML node to compare
12
- # @param [Nokogiri::XML::Node] node_2 The secton top-level XML node to compare
11
+ # @param [Nokogiri::XML::Node, Nokogiri::XML::NodeSet] node_1 The first top-level XML node to compare
12
+ # @param [Nokogiri::XML::Node, Nokogiri::XML::NodeSet] node_2 The secton top-level XML node to compare
13
13
  # @param [Hash] opts Options that determine how certain comparisons are evaluated
14
14
  # @option opts [Boolean] :element_order (false) Child elements must occur in the same order to be considered equivalent
15
15
  # @option opts [Boolean] :normalize_whitespace (true) Collapse whitespace within Text nodes before comparing
@@ -17,7 +17,11 @@ module EquivalentXml
17
17
  # @return [Boolean] true or false
18
18
  def equivalent?(node_1, node_2, opts = {}, &block)
19
19
  opts = DEFAULT_OPTS.merge(opts)
20
- self.compare_nodes(as_node(node_1), as_node(node_2), opts, &block)
20
+ if [node_1, node_2].any? { |node| node.is_a?(Nokogiri::XML::NodeSet)}
21
+ self.compare_nodesets(as_nodeset(node_1, opts), as_nodeset(node_2, opts), opts, &block)
22
+ else
23
+ self.compare_nodes(as_node(node_1), as_node(node_2), opts, &block)
24
+ end
21
25
  end
22
26
 
23
27
  def compare_nodes(node_1, node_2, opts, &block)
@@ -76,14 +80,8 @@ module EquivalentXml
76
80
  end
77
81
 
78
82
  def compare_children(node_1, node_2, opts, &block)
79
- ignore_proc = lambda do |child|
80
- child.node_type == Nokogiri::XML::Node::COMMENT_NODE ||
81
- child.node_type == Nokogiri::XML::Node::PI_NODE ||
82
- (opts[:normalize_whitespace] && child.node_type == Nokogiri::XML::Node::TEXT_NODE && child.text.strip.empty?)
83
- end
84
-
85
- nodeset_1 = node_1.children.reject { |child| ignore_proc.call(child) }
86
- nodeset_2 = node_2.children.reject { |child| ignore_proc.call(child) }
83
+ nodeset_1 = as_nodeset(node_1.children, opts)
84
+ nodeset_2 = as_nodeset(node_2.children, opts)
87
85
  result = self.compare_nodesets(nodeset_1,nodeset_2,opts,&block)
88
86
 
89
87
  if node_1.respond_to?(:attribute_nodes)
@@ -104,6 +102,7 @@ module EquivalentXml
104
102
 
105
103
  local_set_1.each do |search_node|
106
104
  found_node = local_set_2.find { |test_node| self.equivalent?(search_node,test_node,opts,&block) }
105
+
107
106
  if found_node.nil?
108
107
  return false
109
108
  else
@@ -153,6 +152,21 @@ module EquivalentXml
153
152
  end
154
153
  end
155
154
 
155
+ def as_nodeset(data, opts = {})
156
+ ignore_proc = lambda do |child|
157
+ child.node_type == Nokogiri::XML::Node::COMMENT_NODE ||
158
+ child.node_type == Nokogiri::XML::Node::PI_NODE ||
159
+ (opts[:normalize_whitespace] && child.node_type == Nokogiri::XML::Node::TEXT_NODE && child.text.strip.empty?)
160
+ end
161
+
162
+ if data.is_a?(Nokogiri::XML::NodeSet)
163
+ data.reject { |child| ignore_proc.call(child) }
164
+ else
165
+ result = Nokogiri::XML("<root>#{data}</root>")
166
+ result.root.nil? ? data : result.root.children.reject { |child| ignore_proc.call(child) }
167
+ end
168
+ end
169
+
156
170
  end
157
171
 
158
172
  end
@@ -137,4 +137,13 @@ describe EquivalentXml do
137
137
  doc1.should_not be_equivalent_to(doc2)
138
138
  end
139
139
 
140
+ it "should compare nodesets" do
141
+ doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
142
+ doc1.root.children.should be_equivalent_to(doc1.root.children)
143
+ end
144
+
145
+ it "should compare nodeset with string" do
146
+ doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
147
+ doc1.root.children.should be_equivalent_to("<first xmlns='foo:bar'>foo bar baz</first><second xmlns='foo:bar'>things</second>")
148
+ end
140
149
  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: 25
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 7
10
- version: 0.2.7
9
+ - 8
10
+ version: 0.2.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael B. Klein
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-05 00:00:00 -07:00
19
- default_executable:
18
+ date: 2011-10-07 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: nokogiri
@@ -101,7 +100,6 @@ files:
101
100
  - lib/equivalent-xml.rb
102
101
  - lib/equivalent-xml/rspec_matchers.rb
103
102
  - spec/equivalent-xml_spec.rb
104
- has_rdoc: true
105
103
  homepage: http://github.com/mbklein/equivalent-xml
106
104
  licenses:
107
105
  - MIT
@@ -131,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
129
  requirements: []
132
130
 
133
131
  rubyforge_project:
134
- rubygems_version: 1.6.2
132
+ rubygems_version: 1.8.6
135
133
  signing_key:
136
134
  specification_version: 3
137
135
  summary: Easy equivalency tests for Ruby XML