test_xml 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +123 -0
- data/Rakefile +6 -0
- data/lib/test_xml.rb +3 -7
- data/lib/test_xml/spec.rb +4 -41
- data/lib/test_xml/spec/matchers.rb +4 -0
- data/lib/test_xml/spec/matchers/exactly_match_xml.rb +18 -0
- data/lib/test_xml/spec/matchers/exactly_match_xml_structure.rb +15 -0
- data/lib/test_xml/spec/matchers/match_xml.rb +18 -0
- data/lib/test_xml/spec/matchers/match_xml_structure.rb +16 -0
- data/lib/test_xml/test_unit.rb +2 -45
- data/lib/test_xml/test_unit/assertions.rb +53 -0
- data/lib/test_xml/version.rb +3 -0
- data/spec/lib/test_xml/spec/matchers/exactly_match_xml_spec.rb +56 -0
- data/spec/lib/test_xml/spec/matchers/exactly_match_xml_structure_spec.rb +56 -0
- data/spec/lib/test_xml/spec/matchers/match_xml_spec.rb +56 -0
- data/spec/lib/test_xml/spec/matchers/match_xml_structure_spec.rb +56 -0
- data/test/test_xml/test_unit/test_assertions.rb +159 -0
- metadata +41 -20
- data/lib/test_xml/matchers.rb +0 -21
- data/spec/test_xml/spec_spec.rb +0 -47
- data/test/test_xml/test_matchers.rb +0 -30
- data/test/test_xml/test_test_unit.rb +0 -55
data/README.rdoc
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
= TestXml
|
2
|
+
|
3
|
+
== DESCRIPTION:
|
4
|
+
|
5
|
+
TestXml is a small extension for RSpec and TestUnit for XML/HTML testing. I have found it very useful for API testing with our Cucumber tests.
|
6
|
+
|
7
|
+
== FEATURES:
|
8
|
+
|
9
|
+
* test XML with: RSpec, Test::Unit, Cucumber
|
10
|
+
* test XML structure (partially or fully matched)
|
11
|
+
* test the element values within XML structure
|
12
|
+
|
13
|
+
== EXAMPLES:
|
14
|
+
|
15
|
+
=== Cucumber feature
|
16
|
+
|
17
|
+
Scenario:
|
18
|
+
Given data
|
19
|
+
When I post the data
|
20
|
+
The I receive successful response
|
21
|
+
And response matches the following xml
|
22
|
+
"""
|
23
|
+
<transaction>
|
24
|
+
<status>success</status>
|
25
|
+
<id/>
|
26
|
+
<order_id/>
|
27
|
+
</transaction>
|
28
|
+
"""
|
29
|
+
|
30
|
+
The scenario will check:
|
31
|
+
|
32
|
+
* 'status' element and its value.
|
33
|
+
* 'id' and 'order_id' elements are present in XML
|
34
|
+
|
35
|
+
== USAGE:
|
36
|
+
|
37
|
+
=== RSpec
|
38
|
+
|
39
|
+
in spec_helper.rb
|
40
|
+
|
41
|
+
require 'test_xml'
|
42
|
+
require 'test_xml/spec'
|
43
|
+
|
44
|
+
in spec file:
|
45
|
+
|
46
|
+
it "should match_xml" do
|
47
|
+
xml = <<-XML
|
48
|
+
<root>
|
49
|
+
<one>1</one>
|
50
|
+
<two>2</two>
|
51
|
+
</root>
|
52
|
+
XML
|
53
|
+
|
54
|
+
xml.should match_xml(<<-XML)
|
55
|
+
<root>
|
56
|
+
<one>1</one>
|
57
|
+
<two>2</two>
|
58
|
+
</root>
|
59
|
+
XML
|
60
|
+
end
|
61
|
+
|
62
|
+
=== Implemented matchers
|
63
|
+
|
64
|
+
* match_xml
|
65
|
+
* exactly_match_xml
|
66
|
+
* match_xml_structure
|
67
|
+
* exactly_match_xml_structure
|
68
|
+
|
69
|
+
=== With Test::Unit
|
70
|
+
|
71
|
+
in test_helper.rb
|
72
|
+
|
73
|
+
require 'test_xml'
|
74
|
+
require 'test_xml/test_unit'
|
75
|
+
|
76
|
+
in test file:
|
77
|
+
|
78
|
+
def test_that_xml_matches
|
79
|
+
xml = <<-XML
|
80
|
+
<root>
|
81
|
+
<one>1</one>
|
82
|
+
</root>
|
83
|
+
XML
|
84
|
+
|
85
|
+
assert_match_xml(xml) do
|
86
|
+
<<-XML
|
87
|
+
<root>
|
88
|
+
<one>1</one>
|
89
|
+
</root>
|
90
|
+
XML
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
=== Implemented assertions
|
95
|
+
|
96
|
+
* assert_match_xml
|
97
|
+
* assert_exactly_match_xml
|
98
|
+
* assert_match_xml_structure
|
99
|
+
* assert_exactly_match_xml_structure
|
100
|
+
|
101
|
+
with assert_not_*
|
102
|
+
|
103
|
+
=== With Cucumber
|
104
|
+
|
105
|
+
Add to features/env.rb
|
106
|
+
|
107
|
+
require 'test_xml'
|
108
|
+
require 'test_xml/spec'
|
109
|
+
World(TestXml::Spec)
|
110
|
+
|
111
|
+
And you can add the following step:
|
112
|
+
|
113
|
+
Then /^response matches the following xml$/ do |string|
|
114
|
+
response.body.should match_xml(string)
|
115
|
+
end
|
116
|
+
|
117
|
+
== REQUIREMENTS
|
118
|
+
|
119
|
+
test_xml depends on Nokogiri
|
120
|
+
|
121
|
+
== INSTALL
|
122
|
+
|
123
|
+
[sudo] gem install test_xml
|
data/Rakefile
CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'rake/testtask'
|
4
4
|
require 'spec/rake/spectask'
|
5
|
+
require 'rdoc/task'
|
5
6
|
|
6
7
|
task :default => ['test:units', :spec]
|
7
8
|
|
@@ -18,3 +19,8 @@ Spec::Rake::SpecTask.new do |t|
|
|
18
19
|
t.libs << "spec"
|
19
20
|
t.verbose = true
|
20
21
|
end
|
22
|
+
|
23
|
+
RDoc::Task.new do |rd|
|
24
|
+
rd.main = "README.rdoc"
|
25
|
+
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
26
|
+
end
|
data/lib/test_xml.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'nokogiri'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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'
|
4
|
+
require 'test_xml/matchers'
|
5
|
+
require 'test_xml/nokogiri/node'
|
6
|
+
require 'test_xml/nokogiri'
|
data/lib/test_xml/spec.rb
CHANGED
@@ -1,42 +1,5 @@
|
|
1
|
-
|
2
|
-
module Spec
|
3
|
-
class MatchXml
|
4
|
-
include TestXml::Matchers
|
1
|
+
require 'test_xml/spec/matchers.rb'
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
3
|
+
# class Spec::Example::ExampleGroup
|
4
|
+
# include TestXml::Spec
|
5
|
+
# end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Spec::Matchers.define :exactly_match_xml do |expected|
|
2
|
+
match do |actual|
|
3
|
+
subject = Nokogiri::XML::Document.parse(actual)
|
4
|
+
pattern = Nokogiri::XML::Document.parse(expected)
|
5
|
+
|
6
|
+
compare_values = true
|
7
|
+
|
8
|
+
subject.match?(pattern, compare_values) && pattern.match?(subject, compare_values)
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message_for_should do |actual|
|
12
|
+
"the xml:\n#{actual}\nshould exactly match xml:\n#{expected}"
|
13
|
+
end
|
14
|
+
|
15
|
+
failure_message_for_should_not do |actual|
|
16
|
+
"the xml:\n#{actual}\nshould not exactly match xml:\n#{expected}\nbut it does"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Spec::Matchers.define :exactly_match_xml_structure do |expected|
|
2
|
+
match do |actual|
|
3
|
+
subject = Nokogiri::XML::Document.parse(actual)
|
4
|
+
pattern = Nokogiri::XML::Document.parse(expected)
|
5
|
+
subject.match?(pattern) && pattern.match?(subject)
|
6
|
+
end
|
7
|
+
|
8
|
+
failure_message_for_should do |actual|
|
9
|
+
"the xml:\n#{actual}\nshould exactly match xml structure:\n#{expected}"
|
10
|
+
end
|
11
|
+
|
12
|
+
failure_message_for_should_not do |actual|
|
13
|
+
"the xml:\n#{actual}\nshould not exactly match xml structure:\n#{expected}\nbut it does"
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Spec::Matchers.define :match_xml do |expected|
|
2
|
+
match do |actual|
|
3
|
+
subject = Nokogiri::XML::Document.parse(actual)
|
4
|
+
pattern = Nokogiri::XML::Document.parse(expected)
|
5
|
+
|
6
|
+
compare_values = true
|
7
|
+
|
8
|
+
subject.match?(pattern, compare_values)
|
9
|
+
end
|
10
|
+
|
11
|
+
failure_message_for_should do |actual|
|
12
|
+
"the xml:\n#{actual}\nshould match xml:\n#{expected}"
|
13
|
+
end
|
14
|
+
|
15
|
+
failure_message_for_should_not do |actual|
|
16
|
+
"the xml:\n#{actual}\nshould not match xml:\n#{expected}\nbut it does"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Spec::Matchers.define :match_xml_structure do |expected|
|
2
|
+
match do |actual|
|
3
|
+
subject = Nokogiri::XML::Document.parse(actual)
|
4
|
+
pattern = Nokogiri::XML::Document.parse(expected)
|
5
|
+
|
6
|
+
subject.match?(pattern)
|
7
|
+
end
|
8
|
+
|
9
|
+
failure_message_for_should do |actual|
|
10
|
+
"the xml:\n#{actual}\nshould match xml structure:\n#{expected}"
|
11
|
+
end
|
12
|
+
|
13
|
+
failure_message_for_should_not do |actual|
|
14
|
+
"the xml:\n#{actual}\nshould not match xml structure:\n#{expected}\nbut it does"
|
15
|
+
end
|
16
|
+
end
|
data/lib/test_xml/test_unit.rb
CHANGED
@@ -1,48 +1,5 @@
|
|
1
|
-
|
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
|
1
|
+
require 'test_xml/test_unit/assertions'
|
45
2
|
|
46
3
|
class Test::Unit::TestCase
|
47
|
-
include TestXml::TestUnit
|
4
|
+
include TestXml::TestUnit::Assertions
|
48
5
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module TestXml
|
2
|
+
module TestUnit
|
3
|
+
module Assertions
|
4
|
+
def self.assertions_for(name, options)
|
5
|
+
define_method("assert_#{name}") do |subject, &block|
|
6
|
+
pattern = block.call
|
7
|
+
|
8
|
+
actual = Nokogiri::XML.parse(subject)
|
9
|
+
expected = Nokogiri::XML.parse(pattern)
|
10
|
+
|
11
|
+
full_message = options[:message_for_should].gsub(/\<pattern\>/, pattern).gsub(/\<subject\>/, subject)
|
12
|
+
|
13
|
+
assert_block(full_message) do
|
14
|
+
options[:matcher].call(actual, expected)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
define_method("assert_not_#{name}") do |subject, &block|
|
19
|
+
pattern = block.call
|
20
|
+
|
21
|
+
actual = Nokogiri::XML.parse(subject)
|
22
|
+
expected = Nokogiri::XML.parse(pattern)
|
23
|
+
|
24
|
+
full_message = options[:message_for_should_not].gsub(/\<pattern\>/, pattern).gsub(/\<subject\>/, subject)
|
25
|
+
|
26
|
+
assert_block(full_message) do
|
27
|
+
!options[:matcher].call(actual, expected)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
assertions_for :match_xml,
|
33
|
+
:message_for_should => "the xml:\n<subject>\nshould match xml:\n<pattern>",
|
34
|
+
:message_for_should_not => "the xml:\n<subject>\nshould not match xml:\n<pattern> but it does",
|
35
|
+
:matcher => Proc.new {|actual, expected| actual.match?(expected, true)}
|
36
|
+
|
37
|
+
assertions_for :exactly_match_xml,
|
38
|
+
:message_for_should => "the xml:\n<subject>\nshould exactly match xml:\n<pattern>",
|
39
|
+
:message_for_should_not => "the xml:\n<subject>\nshould not exactly match xml:\n<pattern> but it does",
|
40
|
+
:matcher => Proc.new {|actual, expected| actual.match?(expected, true) && expected.match?(actual, true) }
|
41
|
+
|
42
|
+
assertions_for :match_xml_structure,
|
43
|
+
:message_for_should => "the xml:\n<subject>\nshould match xml structure:\n<pattern>",
|
44
|
+
:message_for_should_not => "the xml:\n<subject>\nshould not match xml structure:\n<pattern> but it does",
|
45
|
+
:matcher => Proc.new {|actual, expected| actual.match?(expected)}
|
46
|
+
|
47
|
+
assertions_for :exactly_match_xml_structure,
|
48
|
+
:message_for_should => "the xml:\n<subject>\nshould exactly match xml structure:\n<pattern>",
|
49
|
+
:message_for_should_not => "the xml:\n<subject>\nshould not exactly match xml structure:\n<pattern> but it does",
|
50
|
+
:matcher => Proc.new {|actual, expected| actual.match?(expected) && expected.match?(actual) }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "exactly_match_xml(xml)" do
|
4
|
+
subject {
|
5
|
+
<<-XML
|
6
|
+
<xml>
|
7
|
+
<one>1</one>
|
8
|
+
<two>2</two>
|
9
|
+
</xml>
|
10
|
+
XML
|
11
|
+
}
|
12
|
+
|
13
|
+
context "when xml is equal with subject" do
|
14
|
+
it "should pass" do
|
15
|
+
should exactly_match_xml(<<-XML)
|
16
|
+
<xml>
|
17
|
+
<one>1</one>
|
18
|
+
<two>2</two>
|
19
|
+
</xml>
|
20
|
+
XML
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when xml structure is equal with subject but elements have different content" do
|
25
|
+
it "should fail" do
|
26
|
+
should_not exactly_match_xml(<<-XML)
|
27
|
+
<xml>
|
28
|
+
<one>4</one>
|
29
|
+
<two>5</two>
|
30
|
+
</xml>
|
31
|
+
XML
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when xml has less elements" do
|
36
|
+
it "should fail" do
|
37
|
+
should_not exactly_match_xml(<<-XML)
|
38
|
+
<xml>
|
39
|
+
<one>1</one>
|
40
|
+
</xml>
|
41
|
+
XML
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when xml has more elements" do
|
46
|
+
it "should fail" do
|
47
|
+
should_not exactly_match_xml(<<-XML)
|
48
|
+
<xml>
|
49
|
+
<one>1</one>
|
50
|
+
<two>2</two>
|
51
|
+
<three>3</three>
|
52
|
+
</xml>
|
53
|
+
XML
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "exactly_match_xml_structure(xml)" do
|
4
|
+
subject {
|
5
|
+
<<-XML
|
6
|
+
<xml>
|
7
|
+
<one>1</one>
|
8
|
+
<two>2</two>
|
9
|
+
</xml>
|
10
|
+
XML
|
11
|
+
}
|
12
|
+
|
13
|
+
context "when xml structure is equal with subject" do
|
14
|
+
it "should pass" do
|
15
|
+
should exactly_match_xml_structure(<<-XML)
|
16
|
+
<xml>
|
17
|
+
<one/>
|
18
|
+
<two/>
|
19
|
+
</xml>
|
20
|
+
XML
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when xml structure is equal with subject and elements have different content" do
|
25
|
+
it "should pass" do
|
26
|
+
should exactly_match_xml_structure(<<-XML)
|
27
|
+
<xml>
|
28
|
+
<one>4</one>
|
29
|
+
<two>5</two>
|
30
|
+
</xml>
|
31
|
+
XML
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when xml has less elements" do
|
36
|
+
it "should fail" do
|
37
|
+
should_not exactly_match_xml_structure(<<-XML)
|
38
|
+
<xml>
|
39
|
+
<one/>
|
40
|
+
</xml>
|
41
|
+
XML
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when xml has more elements" do
|
46
|
+
it "should fail" do
|
47
|
+
should_not exactly_match_xml_structure(<<-XML)
|
48
|
+
<xml>
|
49
|
+
<one/>
|
50
|
+
<two/>
|
51
|
+
<three/>
|
52
|
+
</xml>
|
53
|
+
XML
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "match_xml(xml)" do
|
4
|
+
subject {
|
5
|
+
<<-XML
|
6
|
+
<xml>
|
7
|
+
<one>1</one>
|
8
|
+
<two>2</two>
|
9
|
+
</xml>
|
10
|
+
XML
|
11
|
+
}
|
12
|
+
|
13
|
+
context "when xml is equal" do
|
14
|
+
it "should pass" do
|
15
|
+
should match_xml(<<-XML)
|
16
|
+
<xml>
|
17
|
+
<one>1</one>
|
18
|
+
<two>2</two>
|
19
|
+
</xml>
|
20
|
+
XML
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when xml has less elements" do
|
25
|
+
it "should pass" do
|
26
|
+
should match_xml(<<-XML)
|
27
|
+
<xml>
|
28
|
+
<one>1</one>
|
29
|
+
</xml>
|
30
|
+
XML
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when xml structure is equal but elements have different content" do
|
35
|
+
it "should fail" do
|
36
|
+
should_not match_xml(<<-XML)
|
37
|
+
<xml>
|
38
|
+
<one>4</one>
|
39
|
+
<two>5</two>
|
40
|
+
</xml>
|
41
|
+
XML
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when xml has more elements" do
|
46
|
+
it "should fail" do
|
47
|
+
should_not match_xml(<<-XML)
|
48
|
+
<xml>
|
49
|
+
<one>1</one>
|
50
|
+
<two>2</two>
|
51
|
+
<three>3</three>
|
52
|
+
</xml>
|
53
|
+
XML
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "match_xml_structure(xml)" do
|
4
|
+
subject {
|
5
|
+
<<-XML
|
6
|
+
<xml>
|
7
|
+
<one>1</one>
|
8
|
+
<two>2</two>
|
9
|
+
</xml>
|
10
|
+
XML
|
11
|
+
}
|
12
|
+
|
13
|
+
context "when xml is equal" do
|
14
|
+
it "should pass" do
|
15
|
+
should match_xml_structure(<<-XML)
|
16
|
+
<xml>
|
17
|
+
<one>1</one>
|
18
|
+
<two>2</two>
|
19
|
+
</xml>
|
20
|
+
XML
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when xml structure is equal but elements have different content" do
|
25
|
+
it "should pass" do
|
26
|
+
should match_xml_structure(<<-XML)
|
27
|
+
<xml>
|
28
|
+
<one>4</one>
|
29
|
+
<two>5</two>
|
30
|
+
</xml>
|
31
|
+
XML
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when xml has less elements" do
|
36
|
+
it "should pass" do
|
37
|
+
should match_xml_structure(<<-XML)
|
38
|
+
<xml>
|
39
|
+
<one>1</one>
|
40
|
+
</xml>
|
41
|
+
XML
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when xml has more elements" do
|
46
|
+
it "should fail" do
|
47
|
+
should_not match_xml_structure(<<-XML)
|
48
|
+
<xml>
|
49
|
+
<one>1</one>
|
50
|
+
<two>2</two>
|
51
|
+
<three>3</three>
|
52
|
+
</xml>
|
53
|
+
XML
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestAssertions < Test::Unit::TestCase
|
4
|
+
def test_assert_match_xml
|
5
|
+
xml = <<-XML
|
6
|
+
<root>
|
7
|
+
<one>1</one>
|
8
|
+
<two>2</two>
|
9
|
+
</root>
|
10
|
+
XML
|
11
|
+
|
12
|
+
assert_match_xml(xml) do
|
13
|
+
<<-XML
|
14
|
+
<root>
|
15
|
+
<one>1</one>
|
16
|
+
</root>
|
17
|
+
XML
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_assert_not_match_xml
|
22
|
+
xml = <<-XML
|
23
|
+
<root>
|
24
|
+
<one>1</one>
|
25
|
+
</root>
|
26
|
+
XML
|
27
|
+
|
28
|
+
assert_not_match_xml(xml) do
|
29
|
+
<<-XML
|
30
|
+
<root>
|
31
|
+
<one>2</one>
|
32
|
+
</root>
|
33
|
+
XML
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_assert_match_xml_structure
|
38
|
+
xml = <<-XML
|
39
|
+
<root>
|
40
|
+
<one>1</one>
|
41
|
+
<two>
|
42
|
+
<three>3</three>
|
43
|
+
</two>
|
44
|
+
</root>
|
45
|
+
XML
|
46
|
+
|
47
|
+
assert_match_xml_structure(xml) do
|
48
|
+
<<-XML
|
49
|
+
<root>
|
50
|
+
<one>2</one>
|
51
|
+
<two>
|
52
|
+
<three>4</three>
|
53
|
+
</two>
|
54
|
+
</root>
|
55
|
+
XML
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_assert_not_match_xml_structure
|
60
|
+
xml = <<-XML
|
61
|
+
<root>
|
62
|
+
<one>1</one>
|
63
|
+
<two>
|
64
|
+
<three>3</three>
|
65
|
+
</two>
|
66
|
+
</root>
|
67
|
+
XML
|
68
|
+
|
69
|
+
assert_not_match_xml_structure(xml) do
|
70
|
+
<<-XML
|
71
|
+
<root>
|
72
|
+
<one>2</one>
|
73
|
+
<two>
|
74
|
+
<four/>
|
75
|
+
</two>
|
76
|
+
</root>
|
77
|
+
XML
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_assert_exactly_match_xml
|
82
|
+
xml = <<-XML
|
83
|
+
<root>
|
84
|
+
<one>1</one>
|
85
|
+
<two>2</two>
|
86
|
+
</root>
|
87
|
+
XML
|
88
|
+
|
89
|
+
assert_exactly_match_xml(xml) do
|
90
|
+
<<-XML
|
91
|
+
<root>
|
92
|
+
<one>1</one>
|
93
|
+
<two>2</two>
|
94
|
+
</root>
|
95
|
+
XML
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_assert_not_exactly_match_xml
|
100
|
+
xml = <<-XML
|
101
|
+
<root>
|
102
|
+
<one>1</one>
|
103
|
+
<two>2</two>
|
104
|
+
</root>
|
105
|
+
XML
|
106
|
+
|
107
|
+
assert_not_exactly_match_xml(xml) do
|
108
|
+
<<-XML
|
109
|
+
<root>
|
110
|
+
<one>1</one>
|
111
|
+
</root>
|
112
|
+
XML
|
113
|
+
end
|
114
|
+
end
|
115
|
+
def test_assert_exactly_match_xml_structure
|
116
|
+
xml = <<-XML
|
117
|
+
<root>
|
118
|
+
<one>1</one>
|
119
|
+
<two>
|
120
|
+
<three>3</three>
|
121
|
+
</two>
|
122
|
+
</root>
|
123
|
+
XML
|
124
|
+
|
125
|
+
assert_exactly_match_xml_structure(xml) do
|
126
|
+
<<-XML
|
127
|
+
<root>
|
128
|
+
<one>2</one>
|
129
|
+
<two>
|
130
|
+
<three>4</three>
|
131
|
+
</two>
|
132
|
+
</root>
|
133
|
+
XML
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_assert_not_exactly_match_xml_structure
|
138
|
+
xml = <<-XML
|
139
|
+
<root>
|
140
|
+
<one>1</one>
|
141
|
+
<two>
|
142
|
+
<three>3</three>
|
143
|
+
</two>
|
144
|
+
</root>
|
145
|
+
XML
|
146
|
+
|
147
|
+
assert_not_match_xml_structure(xml) do
|
148
|
+
<<-XML
|
149
|
+
<root>
|
150
|
+
<one>2</one>
|
151
|
+
<two>
|
152
|
+
<three>3</three>
|
153
|
+
<four/>
|
154
|
+
</two>
|
155
|
+
</root>
|
156
|
+
XML
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Pavel Gabriel
|
@@ -9,68 +14,84 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-06-07 00:00:00 +03:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: nokogiri
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
30
|
+
- 2
|
23
31
|
version: 1.3.2
|
24
|
-
|
25
|
-
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: test_xml allows you to test xml (match stucture and values) with RSpec, Test::Unit, Cucumber
|
26
35
|
email: alovak@gmail.com
|
27
36
|
executables: []
|
28
37
|
|
29
38
|
extensions: []
|
30
39
|
|
31
|
-
extra_rdoc_files:
|
32
|
-
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README.rdoc
|
33
42
|
files:
|
34
|
-
- lib/test_xml/matchers.rb
|
35
43
|
- lib/test_xml/nokogiri/node.rb
|
36
44
|
- lib/test_xml/nokogiri.rb
|
45
|
+
- lib/test_xml/spec/matchers/exactly_match_xml.rb
|
46
|
+
- lib/test_xml/spec/matchers/exactly_match_xml_structure.rb
|
47
|
+
- lib/test_xml/spec/matchers/match_xml.rb
|
48
|
+
- lib/test_xml/spec/matchers/match_xml_structure.rb
|
49
|
+
- lib/test_xml/spec/matchers.rb
|
37
50
|
- lib/test_xml/spec.rb
|
51
|
+
- lib/test_xml/test_unit/assertions.rb
|
38
52
|
- lib/test_xml/test_unit.rb
|
53
|
+
- lib/test_xml/version.rb
|
39
54
|
- lib/test_xml.rb
|
40
55
|
- test/test_helper.rb
|
41
56
|
- test/test_xml/nokogiri/test_node.rb
|
42
|
-
- test/test_xml/
|
43
|
-
-
|
57
|
+
- test/test_xml/test_unit/test_assertions.rb
|
58
|
+
- spec/lib/test_xml/spec/matchers/exactly_match_xml_spec.rb
|
59
|
+
- spec/lib/test_xml/spec/matchers/exactly_match_xml_structure_spec.rb
|
60
|
+
- spec/lib/test_xml/spec/matchers/match_xml_spec.rb
|
61
|
+
- spec/lib/test_xml/spec/matchers/match_xml_structure_spec.rb
|
44
62
|
- spec/spec_helper.rb
|
45
|
-
- spec/test_xml/spec_spec.rb
|
46
63
|
- Rakefile
|
64
|
+
- README.rdoc
|
47
65
|
has_rdoc: true
|
48
|
-
homepage: http://github.com/alovak/
|
66
|
+
homepage: http://github.com/alovak/test_xml
|
49
67
|
licenses: []
|
50
68
|
|
51
69
|
post_install_message:
|
52
|
-
rdoc_options:
|
53
|
-
|
70
|
+
rdoc_options:
|
71
|
+
- --main
|
72
|
+
- README.rdoc
|
54
73
|
require_paths:
|
55
74
|
- lib
|
56
75
|
required_ruby_version: !ruby/object:Gem::Requirement
|
57
76
|
requirements:
|
58
77
|
- - ">="
|
59
78
|
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
60
81
|
version: "0"
|
61
|
-
version:
|
62
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
83
|
requirements:
|
64
84
|
- - ">="
|
65
85
|
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
66
88
|
version: "0"
|
67
|
-
version:
|
68
89
|
requirements: []
|
69
90
|
|
70
91
|
rubyforge_project:
|
71
|
-
rubygems_version: 1.3.
|
92
|
+
rubygems_version: 1.3.6
|
72
93
|
signing_key:
|
73
94
|
specification_version: 3
|
74
|
-
summary: test_xml allows you to test xml with Test::Unit
|
95
|
+
summary: test_xml allows you to test xml with RSpec, Test::Unit, Cucumber
|
75
96
|
test_files: []
|
76
97
|
|
data/lib/test_xml/matchers.rb
DELETED
@@ -1,21 +0,0 @@
|
|
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
|
data/spec/test_xml/spec_spec.rb
DELETED
@@ -1,47 +0,0 @@
|
|
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
|
@@ -1,30 +0,0 @@
|
|
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
|
@@ -1,55 +0,0 @@
|
|
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
|