equivalent-xml 0.1.6 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -1
- data/lib/equivalent-xml/rspec_matchers.rb +52 -0
- data/spec/{equvalent-xml_spec.rb → equivalent-xml_spec.rb} +19 -17
- metadata +7 -6
data/README.rdoc
CHANGED
@@ -32,7 +32,7 @@ node_1 is equivalent to node_2 if and only if:
|
|
32
32
|
|
33
33
|
If a block is given, the block will be called every time two nodes are compared. The parameters will be
|
34
34
|
the two nodes being compared as well as the result of the comparison. If the block explicitly returns
|
35
|
-
|
35
|
+
+true+ or +false+ (a real +TrueClass+ or +FalseClass+, not just an expression that can be coerced to
|
36
36
|
true or false), the return value will override the result of the comparison.
|
37
37
|
|
38
38
|
+Element+ nodes are equivalent if they have the same name, and their
|
@@ -62,6 +62,20 @@ considered equivalent.
|
|
62
62
|
Don't normalize whitespace within text nodes; require text nodes to
|
63
63
|
match exactly.
|
64
64
|
|
65
|
+
=== Using with RSpec
|
66
|
+
|
67
|
+
EquivalentXml includes a custom matcher for RSpec (version >=1.2.4) that makes including XML
|
68
|
+
equivalencies in your spec tests a cinch!
|
69
|
+
|
70
|
+
Equivalency:
|
71
|
+
node_1.should be_equivalent_to(node_2)
|
72
|
+
node_1.should_not be_equivalent_to(node_2)
|
73
|
+
|
74
|
+
Chained modifiers:
|
75
|
+
node_1.should be_equivalent_to(node_2).respecting_element_order
|
76
|
+
node_1.should be_equivalent_to(node_2).with_whitespace_intact
|
77
|
+
node_1.should be_equivalent_to(node_2).respecting_element_order.with_whitespace_intact
|
78
|
+
|
65
79
|
== Contributing to equivalent-xml
|
66
80
|
|
67
81
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'equivalent-xml'
|
2
|
+
|
3
|
+
class RSpecNotFound < Exception ; end
|
4
|
+
|
5
|
+
if defined?(RSpec)
|
6
|
+
rspec_namespace = RSpec::Matchers
|
7
|
+
elsif defined?(Spec)
|
8
|
+
rspec_namespace = Spec::Matchers
|
9
|
+
else
|
10
|
+
raise RSpecNotFound, "Cannot find Spec (rspec 1.x) or RSpec (rspec 2.x)"
|
11
|
+
end
|
12
|
+
|
13
|
+
rspec_namespace.define :be_equivalent_to do |expected, opts|
|
14
|
+
@opts = opts || {}
|
15
|
+
|
16
|
+
match do |actual|
|
17
|
+
@expected = expected
|
18
|
+
@actual = actual
|
19
|
+
EquivalentXml.equivalent?(@actual,@expected,@opts) { |n1,n2,result|
|
20
|
+
if result == false and @failure_nodes.nil?
|
21
|
+
@failure_nodes = { :expected => n2, :actual => n1 }
|
22
|
+
end
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
chain :respecting_element_order do
|
27
|
+
@opts[:element_order] = true
|
28
|
+
end
|
29
|
+
|
30
|
+
chain :with_whitespace_intact do
|
31
|
+
@opts[:normalize_whitespace] = false
|
32
|
+
end
|
33
|
+
|
34
|
+
failure_message_for_should do
|
35
|
+
<<-MESSAGE
|
36
|
+
expected:
|
37
|
+
#{@failure_nodes[:expected].to_xml}
|
38
|
+
got:
|
39
|
+
#{@failure_nodes[:actual].to_xml}
|
40
|
+
MESSAGE
|
41
|
+
end
|
42
|
+
|
43
|
+
failure_message_for_should_not do
|
44
|
+
<<-MESSAGE
|
45
|
+
expected:
|
46
|
+
#{@actual.to_xml}
|
47
|
+
not to be equivalent to:
|
48
|
+
#{@expected.to_xml}
|
49
|
+
MESSAGE
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -1,76 +1,77 @@
|
|
1
1
|
$:.push(File.join(File.dirname(__FILE__),'..','lib'))
|
2
2
|
require 'nokogiri'
|
3
3
|
require 'equivalent-xml'
|
4
|
+
require 'equivalent-xml/rspec_matchers'
|
4
5
|
|
5
6
|
describe EquivalentXml do
|
6
7
|
|
7
8
|
it "should consider a document equivalent to itself" do
|
8
9
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
9
|
-
|
10
|
+
doc1.should be_equivalent_to(doc1)
|
10
11
|
end
|
11
12
|
|
12
13
|
it "should ensure that attributes match" do
|
13
14
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>")
|
14
15
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first order='2'>foo bar baz</first><second>things</second></doc>")
|
15
|
-
|
16
|
+
doc1.should_not be_equivalent_to(doc2)
|
16
17
|
|
17
18
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>")
|
18
19
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>")
|
19
|
-
|
20
|
+
doc1.should be_equivalent_to(doc2)
|
20
21
|
end
|
21
22
|
|
22
23
|
it "shouldn't care about attribute order" do
|
23
24
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first order='1' value='quux'>foo bar baz</first><second>things</second></doc>")
|
24
25
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first value='quux' order='1'>foo bar baz</first><second>things</second></doc>")
|
25
|
-
|
26
|
+
doc1.should be_equivalent_to(doc2)
|
26
27
|
end
|
27
28
|
|
28
29
|
it "shouldn't care about element order by default" do
|
29
30
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
30
31
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><second>things</second><first>foo bar baz</first></doc>")
|
31
|
-
|
32
|
+
doc1.should be_equivalent_to(doc2)
|
32
33
|
end
|
33
34
|
|
34
35
|
it "should care about element order if :element_order => true is specified" do
|
35
36
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
36
37
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><second>things</second><first>foo bar baz</first></doc>")
|
37
|
-
|
38
|
+
doc1.should_not be_equivalent_to(doc2).respecting_element_order
|
38
39
|
end
|
39
40
|
|
40
41
|
it "should ensure nodesets have the same number of elements" do
|
41
42
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
42
43
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><second>things</second><first>foo bar baz</first><third/></doc>")
|
43
|
-
|
44
|
+
doc1.should_not be_equivalent_to(doc2)
|
44
45
|
end
|
45
46
|
|
46
47
|
it "should ensure namespaces match" do
|
47
48
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
48
49
|
doc2 = Nokogiri::XML("<doc xmlns='foo:baz'><first>foo bar baz</first><second>things</second></doc>")
|
49
|
-
|
50
|
+
doc1.should_not be_equivalent_to(doc2)
|
50
51
|
end
|
51
52
|
|
52
53
|
it "should compare namespaces based on URI, not on prefix" do
|
53
54
|
doc1 = Nokogiri::XML("<doc xmlns:foo='foo:bar'><foo:first>foo bar baz</foo:first><foo:second>things</foo:second></doc>")
|
54
55
|
doc2 = Nokogiri::XML("<doc xmlns:baz='foo:bar'><baz:first>foo bar baz</baz:first><baz:second>things</baz:second></doc>")
|
55
|
-
|
56
|
+
doc1.should be_equivalent_to(doc2)
|
56
57
|
end
|
57
58
|
|
58
59
|
it "should ignore declared but unused namespaces" do
|
59
60
|
doc1 = Nokogiri::XML("<doc xmlns:foo='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
60
61
|
doc2 = Nokogiri::XML("<doc><first>foo bar baz</first><second>things</second></doc>")
|
61
|
-
|
62
|
+
doc1.should be_equivalent_to(doc2)
|
62
63
|
end
|
63
64
|
|
64
65
|
it "should normalize simple whitespace by default" do
|
65
66
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
66
67
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
67
|
-
|
68
|
+
doc1.should be_equivalent_to(doc2)
|
68
69
|
end
|
69
70
|
|
70
71
|
it "shouldn't normalize simple whitespace if :normalize_whitespace => false is specified" do
|
71
72
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
72
73
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first>foo bar baz</first><second>things</second></doc>")
|
73
|
-
|
74
|
+
doc1.should_not be_equivalent_to(doc2).with_whitespace_intact
|
74
75
|
end
|
75
76
|
|
76
77
|
it "should normalize complex whitespace by default" do
|
@@ -82,7 +83,7 @@ describe EquivalentXml do
|
|
82
83
|
bar baz
|
83
84
|
</first>
|
84
85
|
</doc>})
|
85
|
-
|
86
|
+
doc1.should be_equivalent_to(doc2)
|
86
87
|
end
|
87
88
|
|
88
89
|
it "shouldn't normalize complex whitespace if :normalize_whitespace => false is specified" do
|
@@ -94,7 +95,7 @@ describe EquivalentXml do
|
|
94
95
|
bar baz
|
95
96
|
</first>
|
96
97
|
</doc>})
|
97
|
-
|
98
|
+
doc1.should_not be_equivalent_to(doc2).with_whitespace_intact
|
98
99
|
end
|
99
100
|
|
100
101
|
it "should ignore comment nodes" do
|
@@ -107,22 +108,23 @@ describe EquivalentXml do
|
|
107
108
|
bar baz
|
108
109
|
</first>
|
109
110
|
</doc>})
|
110
|
-
|
111
|
+
doc1.should be_equivalent_to(doc2)
|
111
112
|
end
|
112
113
|
|
113
114
|
it "should properly handle a mixture of text and element nodes" do
|
114
115
|
doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><phrase>This phrase <b>has bold text</b> in it.</phrase></doc>")
|
115
116
|
doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><phrase>This phrase in <b>has bold text</b> it.</phrase></doc>")
|
116
|
-
|
117
|
+
doc1.should_not be_equivalent_to(doc2)
|
117
118
|
end
|
118
119
|
|
119
120
|
it "should properly handle documents passed in as strings" do
|
120
121
|
doc1 = "<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>"
|
121
122
|
doc2 = "<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>"
|
123
|
+
doc1.should be_equivalent_to(doc2)
|
122
124
|
|
123
125
|
doc1 = "<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>"
|
124
126
|
doc2 = "<doc xmlns='foo:bar'><first order='1'>foo bar baz quux</first><second>things</second></doc>"
|
125
|
-
|
127
|
+
doc1.should_not be_equivalent_to(doc2)
|
126
128
|
end
|
127
129
|
|
128
130
|
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:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 2
|
8
9
|
- 1
|
9
|
-
|
10
|
-
version: 0.1.6
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael B. Klein
|
@@ -93,7 +93,8 @@ files:
|
|
93
93
|
- README.rdoc
|
94
94
|
- Rakefile
|
95
95
|
- lib/equivalent-xml.rb
|
96
|
-
-
|
96
|
+
- lib/equivalent-xml/rspec_matchers.rb
|
97
|
+
- spec/equivalent-xml_spec.rb
|
97
98
|
has_rdoc: true
|
98
99
|
homepage: http://github.com/mbklein/equivalent-xml
|
99
100
|
licenses:
|
@@ -124,9 +125,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
125
|
requirements: []
|
125
126
|
|
126
127
|
rubyforge_project:
|
127
|
-
rubygems_version: 1.
|
128
|
+
rubygems_version: 1.6.2
|
128
129
|
signing_key:
|
129
130
|
specification_version: 3
|
130
131
|
summary: Easy equivalency tests for Ruby XML
|
131
132
|
test_files:
|
132
|
-
- spec/
|
133
|
+
- spec/equivalent-xml_spec.rb
|