equivalent-xml 0.2.2 → 0.2.4

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.
@@ -13,7 +13,9 @@ require 'nokogiri'
13
13
 
14
14
  def compare_nodes(node_1, node_2, opts, &block)
15
15
  result = nil
16
- if (node_1.class != node_2.class) or self.same_namespace?(node_1,node_2) == false
16
+ if [node_1, node_2].any? { |node| not node.respond_to?(:node_type) }
17
+ result = node_1.to_s == node_2.to_s
18
+ elsif (node_1.class != node_2.class) or self.same_namespace?(node_1,node_2) == false
17
19
  result = false
18
20
  else
19
21
  case node_1.node_type
@@ -108,14 +110,17 @@ require 'nokogiri'
108
110
  end
109
111
 
110
112
  def same_namespace?(node_1, node_2)
111
- unless node_1.respond_to?(:namespace) and node_2.respond_to?(:namespace)
112
- return true
113
+ args = [node_1,node_2]
114
+
115
+ # CharacterData nodes shouldn't have namespaces. But in Nokogiri,
116
+ # they do. And they're invisible. And they get corrupted easily.
117
+ # So let's wilfully ignore them. And while we're at it, let's
118
+ # ignore any class that doesn't know it has a namespace.
119
+ if args.all? { |node| not node.respond_to?(:namespace) } or
120
+ args.any? { |node| node.is_a?(Nokogiri::XML::CharacterData) }
121
+ return true
113
122
  end
114
123
 
115
- if node_1.namespace.nil? and node_2.namespace.nil?
116
- return true
117
- end
118
-
119
124
  href1 = node_1.namespace.nil? ? '' : node_1.namespace.href
120
125
  href2 = node_2.namespace.nil? ? '' : node_2.namespace.href
121
126
  return href1 == href2
@@ -125,7 +130,12 @@ require 'nokogiri'
125
130
  if data.respond_to?(:node_type)
126
131
  return data
127
132
  else
128
- return Nokogiri::XML(data)
133
+ result = Nokogiri::XML(data)
134
+ if result.root.nil?
135
+ return data
136
+ else
137
+ return result
138
+ end
129
139
  end
130
140
  end
131
141
 
@@ -16,11 +16,7 @@ rspec_namespace.define :be_equivalent_to do |expected, opts|
16
16
  match do |actual|
17
17
  @expected = expected
18
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
- }
19
+ EquivalentXml.equivalent?(@actual,@expected,@opts)
24
20
  end
25
21
 
26
22
  chain :respecting_element_order do
@@ -34,18 +30,18 @@ rspec_namespace.define :be_equivalent_to do |expected, opts|
34
30
  failure_message_for_should do
35
31
  <<-MESSAGE
36
32
  expected:
37
- #{@failure_nodes[:expected].to_xml}
33
+ #{@expected.to_s}
38
34
  got:
39
- #{@failure_nodes[:actual].to_xml}
35
+ #{@actual.to_s}
40
36
  MESSAGE
41
37
  end
42
38
 
43
39
  failure_message_for_should_not do
44
40
  <<-MESSAGE
45
41
  expected:
46
- #{@actual.to_xml}
42
+ #{@actual.to_s}
47
43
  not to be equivalent to:
48
- #{@expected.to_xml}
44
+ #{@expected.to_s}
49
45
  MESSAGE
50
46
  end
51
47
 
@@ -10,6 +10,16 @@ describe EquivalentXml do
10
10
  doc1.should be_equivalent_to(doc1)
11
11
  end
12
12
 
13
+ it "should compare non-XML content based on its string representation" do
14
+ nil.should be_equivalent_to(nil)
15
+ ''.should be_equivalent_to('')
16
+ ''.should be_equivalent_to(nil)
17
+ 'foo'.should be_equivalent_to('foo')
18
+ 'foo'.should_not be_equivalent_to('bar')
19
+ doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>")
20
+ doc1.should_not be_equivalent_to(nil)
21
+ end
22
+
13
23
  it "should ensure that attributes match" do
14
24
  doc1 = Nokogiri::XML("<doc xmlns='foo:bar'><first order='1'>foo bar baz</first><second>things</second></doc>")
15
25
  doc2 = Nokogiri::XML("<doc xmlns='foo:bar'><first order='2'>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: 19
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 4
10
+ version: 0.2.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael B. Klein