equivalent-xml 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +6 -1
- data/lib/equivalent-xml.rb +15 -9
- data/spec/equvalent-xml_spec.rb +12 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -15,7 +15,7 @@ Testing XML output is difficult:
|
|
15
15
|
EquivalentXml for Nokogiri
|
16
16
|
|
17
17
|
=== Use
|
18
|
-
EquivalentXml.equivalent?(node_1, node_2, opts = { :element_order => false, :normalize_whitespace => true })
|
18
|
+
EquivalentXml.equivalent?(node_1, node_2, opts = { :element_order => false, :normalize_whitespace => true }) { |n1, n2, result| ... }
|
19
19
|
|
20
20
|
node_1 and node_2 can be any Nokogiri::XML::Node descendants (or any string
|
21
21
|
containing an XML document or document fragment). The most common use case is
|
@@ -30,6 +30,11 @@ node_1 is equivalent to node_2 if and only if:
|
|
30
30
|
* If called with :element_order => true, equivalent child elements must be
|
31
31
|
in the same relative position in order to be considered equal
|
32
32
|
|
33
|
+
If a block is given, the block will be called every time two nodes are compared. The parameters will be
|
34
|
+
the two nodes being compared as well as the result of the comparison. If the block explicitly returns
|
35
|
+
`true` or `false` (a real `TrueClass` or `FalseClass`, not just an expression that can be coerced to
|
36
|
+
true or false), the return value will override the result of the comparison.
|
37
|
+
|
33
38
|
+Element+ nodes are equivalent if they have the same name, and their
|
34
39
|
child nodesets are equal (as defined above)
|
35
40
|
|
data/lib/equivalent-xml.rb
CHANGED
@@ -12,26 +12,32 @@ require 'nokogiri'
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def compare_nodes(node_1, node_2, opts, &block)
|
15
|
-
|
16
|
-
|
15
|
+
result = nil
|
17
16
|
if (node_1.class != node_2.class) or self.same_namespace?(node_1,node_2) == false
|
18
|
-
false
|
17
|
+
result = false
|
19
18
|
else
|
20
19
|
case node_1.node_type
|
21
20
|
when Nokogiri::XML::Node::DOCUMENT_NODE
|
22
|
-
self.compare_documents(node_1,node_2,opts,&block)
|
21
|
+
result = self.compare_documents(node_1,node_2,opts,&block)
|
23
22
|
when Nokogiri::XML::Node::ELEMENT_NODE
|
24
|
-
self.compare_elements(node_1,node_2,opts,&block)
|
23
|
+
result = self.compare_elements(node_1,node_2,opts,&block)
|
25
24
|
when Nokogiri::XML::Node::ATTRIBUTE_NODE
|
26
|
-
self.compare_attributes(node_1,node_2,opts,&block)
|
25
|
+
result = self.compare_attributes(node_1,node_2,opts,&block)
|
27
26
|
when Nokogiri::XML::Node::CDATA_SECTION_NODE
|
28
|
-
self.compare_cdata(node_1,node_2,opts,&block)
|
27
|
+
result = self.compare_cdata(node_1,node_2,opts,&block)
|
29
28
|
when Nokogiri::XML::Node::TEXT_NODE
|
30
|
-
self.compare_text(node_1,node_2,opts,&block)
|
29
|
+
result = self.compare_text(node_1,node_2,opts,&block)
|
31
30
|
else
|
32
|
-
self.compare_children(node_1,node_2,opts,&block)
|
31
|
+
result = self.compare_children(node_1,node_2,opts,&block)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
if block_given?
|
35
|
+
block_result = yield(node_1, node_2, result)
|
36
|
+
if block_result.is_a?(TrueClass) or block_result.is_a?(FalseClass)
|
37
|
+
result = block_result
|
33
38
|
end
|
34
39
|
end
|
40
|
+
return result
|
35
41
|
end
|
36
42
|
|
37
43
|
def compare_documents(node_1, node_2, opts, &block)
|
data/spec/equvalent-xml_spec.rb
CHANGED
@@ -49,6 +49,18 @@ describe EquivalentXml do
|
|
49
49
|
EquivalentXml.equivalent?(doc1,doc2).should == false
|
50
50
|
end
|
51
51
|
|
52
|
+
it "should compare namespaces based on URI, not on prefix" do
|
53
|
+
doc1 = Nokogiri::XML("<doc xmlns:foo='foo:bar'><foo:first>foo bar baz</foo:first><foo:second>things</foo:second></doc>")
|
54
|
+
doc2 = Nokogiri::XML("<doc xmlns:baz='foo:bar'><baz:first>foo bar baz</baz:first><baz:second>things</baz:second></doc>")
|
55
|
+
EquivalentXml.equivalent?(doc1,doc2).should == true
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should ignore declared but unused namespaces" do
|
59
|
+
doc1 = Nokogiri::XML("<doc xmlns:foo='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
60
|
+
doc2 = Nokogiri::XML("<doc><first>foo bar baz</first><second>things</second></doc>")
|
61
|
+
EquivalentXml.equivalent?(doc1,doc2).should == true
|
62
|
+
end
|
63
|
+
|
52
64
|
it "should normalize simple whitespace by default" do
|
53
65
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
54
66
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
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:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 6
|
10
|
+
version: 0.1.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael B. Klein
|