test_xml 0.0.1
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/Rakefile +20 -0
- data/lib/test_xml.rb +10 -0
- data/lib/test_xml/matchers.rb +21 -0
- data/lib/test_xml/nokogiri.rb +3 -0
- data/lib/test_xml/nokogiri/node.rb +35 -0
- data/lib/test_xml/spec.rb +42 -0
- data/lib/test_xml/test_unit.rb +48 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/test_xml/spec_spec.rb +47 -0
- data/test/test_helper.rb +4 -0
- data/test/test_xml/nokogiri/test_node.rb +116 -0
- data/test/test_xml/test_matchers.rb +30 -0
- data/test/test_xml/test_test_unit.rb +55 -0
- metadata +76 -0
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
task :default => ['test:units', :spec]
|
7
|
+
|
8
|
+
namespace :test do
|
9
|
+
Rake::TestTask.new(:units) do |t|
|
10
|
+
t.test_files = FileList['test/**/test_*.rb']
|
11
|
+
t.libs << "test"
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
Spec::Rake::SpecTask.new do |t|
|
18
|
+
t.libs << "spec"
|
19
|
+
t.verbose = true
|
20
|
+
end
|
data/lib/test_xml.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
module TestXml
|
5
|
+
VERSION = '0.0.1'
|
6
|
+
end
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/../lib/test_xml/matchers'
|
9
|
+
require File.dirname(__FILE__) + '/../lib/test_xml/nokogiri/node'
|
10
|
+
require File.dirname(__FILE__) + '/../lib/test_xml/nokogiri'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TestXml
|
2
|
+
module Matchers
|
3
|
+
class XmlMatcher
|
4
|
+
attr_reader :subject, :pattern, :compare_value
|
5
|
+
def initialize(subject, pattern, compare_value)
|
6
|
+
@subject = Nokogiri::XML::Document.parse(subject)
|
7
|
+
@pattern = Nokogiri::XML::Document.parse(pattern)
|
8
|
+
@compare_value = compare_value
|
9
|
+
end
|
10
|
+
|
11
|
+
def match?
|
12
|
+
subject.match?(pattern, compare_value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def match_xml?(subject, pattern, compare_value = false)
|
17
|
+
matcher = XmlMatcher.new(subject, pattern, compare_value)
|
18
|
+
matcher.match?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module TestXml
|
2
|
+
module NokogiriExt
|
3
|
+
module Node
|
4
|
+
def match?(element, compare_value = false)
|
5
|
+
if compare_value && element.text_element?
|
6
|
+
equal_text?(element)
|
7
|
+
else
|
8
|
+
contains_elements_of?(element) &&
|
9
|
+
!element.elements.find {|el| not at(el.name).match?(el, compare_value) }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def elements
|
14
|
+
children.collect {|node| node if node.element? }.delete_if {|node| node.nil?}
|
15
|
+
end
|
16
|
+
|
17
|
+
def text_element?
|
18
|
+
children.size == 1 && children.first.text?
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def equal_text?(element)
|
23
|
+
content == element.content
|
24
|
+
end
|
25
|
+
|
26
|
+
def contains_elements_of?(element)
|
27
|
+
element.elements.find {|el| not contains?(el)}.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
def contains?(element)
|
31
|
+
children.find {|node| node.element? && node.name == element.name }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module TestXml
|
2
|
+
module Spec
|
3
|
+
class MatchXml
|
4
|
+
include TestXml::Matchers
|
5
|
+
|
6
|
+
def initialize(expected, compare_values = false)
|
7
|
+
@expected = expected
|
8
|
+
@compare_values = compare_values
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(actual)
|
12
|
+
@actual = actual
|
13
|
+
match_xml?(@actual, @expected, @compare_values)
|
14
|
+
end
|
15
|
+
|
16
|
+
def failure_message_for_should
|
17
|
+
"expected\n#{@actual} to match#{structure_message}\n#{@expected}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def failure_message_for_should_not
|
21
|
+
"expected\n#{@actual} not to match#{structure_message}\n#{@expected}"
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def structure_message
|
26
|
+
" structure" unless @compare_values
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def match_xml(expected)
|
31
|
+
MatchXml.new(expected, true)
|
32
|
+
end
|
33
|
+
|
34
|
+
def match_xml_structure(expected)
|
35
|
+
MatchXml.new(expected, false)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Spec::Example::ExampleGroup
|
41
|
+
include TestXml::Spec
|
42
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module TestXml
|
2
|
+
module TestUnit
|
3
|
+
include TestXml::Matchers
|
4
|
+
|
5
|
+
def assert_match_xml(xml, message = "")
|
6
|
+
pattern = yield
|
7
|
+
full_message = build_message(message, <<-EOT, xml, pattern)
|
8
|
+
The following xml:
|
9
|
+
?
|
10
|
+
does not match with:
|
11
|
+
?
|
12
|
+
EOT
|
13
|
+
assert_block(full_message) do
|
14
|
+
match_xml?(xml, yield, true)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def assert_match_xml_structure(xml, message = "")
|
19
|
+
pattern = yield
|
20
|
+
full_message = build_message(message, <<-EOT, xml, pattern)
|
21
|
+
The following xml:
|
22
|
+
?
|
23
|
+
does not match with structure of:
|
24
|
+
?
|
25
|
+
EOT
|
26
|
+
assert_block(full_message) do
|
27
|
+
match_xml?(xml, yield)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def assert_not_match_xml(xml, message = "")
|
32
|
+
pattern = yield
|
33
|
+
full_message = build_message(message, <<-EOT, xml, pattern)
|
34
|
+
The following xml:
|
35
|
+
?
|
36
|
+
matches with:
|
37
|
+
?
|
38
|
+
EOT
|
39
|
+
assert_block(full_message) do
|
40
|
+
!match_xml?(xml, yield, true)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Test::Unit::TestCase
|
47
|
+
include TestXml::TestUnit
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "xml" do
|
4
|
+
before(:each) do
|
5
|
+
@xml = <<-XML
|
6
|
+
<root>
|
7
|
+
<one>1</one>
|
8
|
+
<two>2</two>
|
9
|
+
</root>
|
10
|
+
XML
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should match_xml" do
|
14
|
+
@xml.should match_xml(<<-XML)
|
15
|
+
<root>
|
16
|
+
<one>1</one>
|
17
|
+
<two>2</two>
|
18
|
+
</root>
|
19
|
+
XML
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should not match_xml" do
|
23
|
+
@xml.should_not match_xml(<<-XML)
|
24
|
+
<root>
|
25
|
+
<one>2</one>
|
26
|
+
</root>
|
27
|
+
XML
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should match_xml_structure" do
|
31
|
+
@xml.should match_xml_structure(<<-XML)
|
32
|
+
<root>
|
33
|
+
<one>2</one>
|
34
|
+
<two>1</two>
|
35
|
+
</root>
|
36
|
+
XML
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not match_xml_structure" do
|
40
|
+
@xml.should_not match_xml_structure(<<-XML)
|
41
|
+
<root>
|
42
|
+
<one>2</one>
|
43
|
+
<three>1</three>
|
44
|
+
</root>
|
45
|
+
XML
|
46
|
+
end
|
47
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestNode < Test::Unit::TestCase
|
4
|
+
def test_elements
|
5
|
+
assert doc.elements.size == 1
|
6
|
+
assert doc.at('root').elements.size == 2
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_text_element
|
10
|
+
assert doc(<<-XML).at('root').text_element?
|
11
|
+
<root>
|
12
|
+
hello
|
13
|
+
</root>
|
14
|
+
XML
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_match_of_elements_without_comparing_values
|
18
|
+
subject = doc(<<-XML)
|
19
|
+
<root>
|
20
|
+
<one>1</one>
|
21
|
+
<two>
|
22
|
+
<three>3</three>
|
23
|
+
</two>
|
24
|
+
</root>
|
25
|
+
XML
|
26
|
+
|
27
|
+
pattern = doc(<<-XML)
|
28
|
+
<root>
|
29
|
+
<two><three/></two>
|
30
|
+
<one>2</one>
|
31
|
+
</root>
|
32
|
+
XML
|
33
|
+
|
34
|
+
assert subject.match?(pattern)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_no_match_of_elements_without_comparing_values
|
38
|
+
subject = doc(<<-XML)
|
39
|
+
<root>
|
40
|
+
<one>1</one>
|
41
|
+
<two/>
|
42
|
+
</root>
|
43
|
+
XML
|
44
|
+
|
45
|
+
pattern = doc(<<-XML)
|
46
|
+
<root>
|
47
|
+
<four/>
|
48
|
+
<five>5</five>
|
49
|
+
</root>
|
50
|
+
XML
|
51
|
+
|
52
|
+
assert !subject.match?(pattern)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_match_with_values
|
56
|
+
subject = doc(<<-XML)
|
57
|
+
<root>
|
58
|
+
<one>1</one>
|
59
|
+
<two><three>3</three></two>
|
60
|
+
</root>
|
61
|
+
XML
|
62
|
+
|
63
|
+
pattern = doc(<<-XML)
|
64
|
+
<root>
|
65
|
+
<two><three>3</three></two>
|
66
|
+
<one>1</one>
|
67
|
+
</root>
|
68
|
+
XML
|
69
|
+
|
70
|
+
assert subject.match?(pattern, true)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_no_match_with_values
|
74
|
+
subject = doc(<<-XML)
|
75
|
+
<root>
|
76
|
+
<one>1</one>
|
77
|
+
</root>
|
78
|
+
XML
|
79
|
+
|
80
|
+
not_matched_pattern = doc(<<-XML)
|
81
|
+
<root>
|
82
|
+
<one>2</one>
|
83
|
+
</root>
|
84
|
+
XML
|
85
|
+
|
86
|
+
assert !subject.match?(not_matched_pattern, true)
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_element_contains_another_element
|
90
|
+
element = create_element('<one/>')
|
91
|
+
|
92
|
+
assert !doc.send(:contains?,element)
|
93
|
+
assert doc.at('root').send(:contains?,element)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_element_contains_elements
|
97
|
+
assert doc.root.send(:contains_elements_of?, doc.root)
|
98
|
+
assert !doc.root.send(:contains_elements_of?, doc('<root><test_element/></root>'))
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def create_element(xml)
|
104
|
+
Nokogiri::XML::Document.parse(xml).root
|
105
|
+
end
|
106
|
+
|
107
|
+
def doc(xml = nil)
|
108
|
+
xml ||= <<-XML
|
109
|
+
<root>
|
110
|
+
<one>1</one>
|
111
|
+
<two>2</two>
|
112
|
+
</root>
|
113
|
+
XML
|
114
|
+
Nokogiri::XML::Document.parse(xml)
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestMatchers < Test::Unit::TestCase
|
4
|
+
include TestXml::Matchers
|
5
|
+
|
6
|
+
def test_match
|
7
|
+
actual = <<-XML
|
8
|
+
<root>
|
9
|
+
<one>
|
10
|
+
<two>2</two>
|
11
|
+
</one>
|
12
|
+
</root>
|
13
|
+
XML
|
14
|
+
|
15
|
+
assert match_xml?(actual, '<root><one><two/></one></root>')
|
16
|
+
assert !match_xml?(actual, '<root><two/></root>')
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_match_with_values
|
20
|
+
actual = <<-XML
|
21
|
+
<root>
|
22
|
+
<one>1</one>
|
23
|
+
<two>2</two>
|
24
|
+
</root>
|
25
|
+
XML
|
26
|
+
|
27
|
+
assert match_xml?(actual, '<root><one>1</one></root>', true)
|
28
|
+
assert !match_xml?(actual, '<root><one>2</one></root>', true)
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestTestUnit < Test::Unit::TestCase
|
4
|
+
def test_assert_match_xml
|
5
|
+
xml = <<-XML
|
6
|
+
<root>
|
7
|
+
<one>1</one>
|
8
|
+
</root>
|
9
|
+
XML
|
10
|
+
|
11
|
+
assert_match_xml(xml) do
|
12
|
+
<<-XML
|
13
|
+
<root>
|
14
|
+
<one>1</one>
|
15
|
+
</root>
|
16
|
+
XML
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_assert_match_xml_structure
|
21
|
+
xml = <<-XML
|
22
|
+
<root>
|
23
|
+
<one>1</one>
|
24
|
+
<two>
|
25
|
+
<three>3</three>
|
26
|
+
</two>
|
27
|
+
</root>
|
28
|
+
XML
|
29
|
+
|
30
|
+
assert_match_xml_structure(xml) do
|
31
|
+
<<-XML
|
32
|
+
<root>
|
33
|
+
<one>2</one>
|
34
|
+
<two><three/></two>
|
35
|
+
</root>
|
36
|
+
XML
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_assert_not_match_xml
|
41
|
+
xml = <<-XML
|
42
|
+
<root>
|
43
|
+
<one>1</one>
|
44
|
+
</root>
|
45
|
+
XML
|
46
|
+
|
47
|
+
assert_not_match_xml(xml) do
|
48
|
+
<<-XML
|
49
|
+
<root>
|
50
|
+
<one>2</one>
|
51
|
+
</root>
|
52
|
+
XML
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: test_xml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavel Gabriel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-04 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: nokogiri
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.3.2
|
24
|
+
version:
|
25
|
+
description: test_xml allows you to test xml (match stucture and values) with Test::Unit or RSpec
|
26
|
+
email: alovak@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/test_xml/matchers.rb
|
35
|
+
- lib/test_xml/nokogiri/node.rb
|
36
|
+
- lib/test_xml/nokogiri.rb
|
37
|
+
- lib/test_xml/spec.rb
|
38
|
+
- lib/test_xml/test_unit.rb
|
39
|
+
- lib/test_xml.rb
|
40
|
+
- test/test_helper.rb
|
41
|
+
- test/test_xml/nokogiri/test_node.rb
|
42
|
+
- test/test_xml/test_matchers.rb
|
43
|
+
- test/test_xml/test_test_unit.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
- spec/test_xml/spec_spec.rb
|
46
|
+
- Rakefile
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://github.com/alovak/
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.5
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: test_xml allows you to test xml with Test::Unit or RSpec
|
75
|
+
test_files: []
|
76
|
+
|