xml_spec 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/README +0 -0
- data/Rakefile +2 -0
- data/lib/xml_spec/cucumber.rb +7 -0
- data/lib/xml_spec/helpers.rb +14 -0
- data/lib/xml_spec/matchers.rb +68 -0
- data/lib/xml_spec/nokogiri/node.rb +60 -0
- data/lib/xml_spec/nokogiri.rb +5 -0
- data/lib/xml_spec/version.rb +3 -0
- data/lib/xml_spec.rb +7 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/xml_spec/matchers_spec.rb +33 -0
- data/xml_spec.gemspec +23 -0
- metadata +148 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
require "rspec"
|
3
|
+
|
4
|
+
module XmlSpec
|
5
|
+
module Matchers
|
6
|
+
class BeXmlEql
|
7
|
+
include XmlSpec::Helpers
|
8
|
+
|
9
|
+
attr_reader :expected, :actual
|
10
|
+
|
11
|
+
def diffable?
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(expected_xml)
|
16
|
+
@expected_xml = expected_xml
|
17
|
+
end
|
18
|
+
|
19
|
+
def matches?(actual_xml)
|
20
|
+
@actual, @expected = parse_xml(actual_xml, @expected_xml)
|
21
|
+
@actual.match?(@expected, true) && @expected.match?(@actual, true)
|
22
|
+
end
|
23
|
+
|
24
|
+
def failure_message_for_should
|
25
|
+
"the xml:\n#{@actual}\nshould exactly match xml:\n#{@expected}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def failure_message_for_should_not
|
29
|
+
"the xml:\n#{@actual}\nshould not exactly match xml:\n#{@expected}\nbut it does"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class IncludeXml
|
34
|
+
include XmlSpec::Helpers
|
35
|
+
|
36
|
+
attr_reader :expected, :actual
|
37
|
+
|
38
|
+
def initialize(expected_xml)
|
39
|
+
@expected_xml = expected_xml
|
40
|
+
end
|
41
|
+
|
42
|
+
def matches?(actual_xml)
|
43
|
+
@actual, @expected = parse_xml(actual_xml, @expected_xml)
|
44
|
+
@actual.match?(expected, true)
|
45
|
+
end
|
46
|
+
|
47
|
+
def failure_message_for_should
|
48
|
+
"the xml:\n#{@actual}\nshould match xml structure:\n#{@expected}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def failure_message_for_should_not
|
52
|
+
"the xml:\n#{@actual}\nshould not match xml structure:\n#{@expected}\nbut it does"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def be_xml_eql(xml)
|
57
|
+
XmlSpec::Matchers::BeXmlEql.new(xml)
|
58
|
+
end
|
59
|
+
|
60
|
+
def include_xml(xml)
|
61
|
+
XmlSpec::Matchers::IncludeXml.new(xml)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
RSpec.configure do |config|
|
67
|
+
config.include XmlSpec::Matchers
|
68
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module XmlSpec
|
2
|
+
module NokogiriExt
|
3
|
+
module Node
|
4
|
+
def match?(element, compare_value = false)
|
5
|
+
if compare_value && element.leaf?
|
6
|
+
comparable_attributes == element.comparable_attributes and equal_text?(element)
|
7
|
+
else
|
8
|
+
|
9
|
+
#TODO clean this part of the code
|
10
|
+
if compare_value
|
11
|
+
(comparable_attributes == element.comparable_attributes) &&
|
12
|
+
contains_elements_of?(element) &&
|
13
|
+
element.elements.all? {|el| matches_at_least_one?(el, compare_value) }
|
14
|
+
else
|
15
|
+
contains_elements_of?(element) &&
|
16
|
+
element.elements.all? {|el| matches_at_least_one?(el, compare_value) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def elements
|
22
|
+
children.collect {|node| node if node.element? }.delete_if {|node| node.nil?}
|
23
|
+
end
|
24
|
+
|
25
|
+
# Attributes of the current node.
|
26
|
+
def comparable_attributes
|
27
|
+
attributes.collect {|k,a| [k.downcase, a.value]}.sort
|
28
|
+
end
|
29
|
+
|
30
|
+
# Check if node is either childless, self-closing, or has content text.
|
31
|
+
def leaf?
|
32
|
+
children.size == 0 or (children.size == 1 && children.first.text?)
|
33
|
+
end
|
34
|
+
|
35
|
+
# def placeholder?
|
36
|
+
# TestXml.placeholders_enabled? and (content =~ /^`.*`$/)
|
37
|
+
# end
|
38
|
+
|
39
|
+
private
|
40
|
+
def equal_text?(element)
|
41
|
+
#element.content = content if element.placeholder?
|
42
|
+
|
43
|
+
content == element.content
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def contains_elements_of?(element)
|
48
|
+
element.elements.find {|el| not contains?(el)}.nil?
|
49
|
+
end
|
50
|
+
|
51
|
+
def contains?(element)
|
52
|
+
children.find {|node| node.element? && node.name == element.name }
|
53
|
+
end
|
54
|
+
|
55
|
+
def matches_at_least_one?(element, compare_value)
|
56
|
+
search(element.name).find { |el| el.match?(element, compare_value) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/xml_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Matchers:" do
|
4
|
+
context "be_xml_eql" do
|
5
|
+
it "matches the exact same XML" do
|
6
|
+
xml = %(<name complete="true">TAPUHI</name><count type="integer">1</count>)
|
7
|
+
xml.should be_xml_eql(%(<name complete="true">TAPUHI</name><count type="integer">1</count>))
|
8
|
+
end
|
9
|
+
|
10
|
+
it "doesn't match XML with different attributes" do
|
11
|
+
xml = %(<name complete="true">TAPUHI</name><count type="integer">1</count>)
|
12
|
+
xml.should_not be_xml_eql(%(<name complete="false">TAPUHI</name><count type="integer">1</count>))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "include_xml" do
|
17
|
+
it "matches a basic xml structure" do
|
18
|
+
xml = %(<values type="array">
|
19
|
+
<value>
|
20
|
+
<name>TAPUHI</name>
|
21
|
+
<count type="integer">1</count>
|
22
|
+
</value>
|
23
|
+
</values>)
|
24
|
+
|
25
|
+
xml.should include_xml(%(<values type="array"><value><name>TAPUHI</name></value></values>))
|
26
|
+
end
|
27
|
+
|
28
|
+
it "ignores other nodes" do
|
29
|
+
xml = %(<value><name>TAPUHI</name><count type="integer">1</count></value>)
|
30
|
+
xml.should include_xml(%(<value><name>TAPUHI</name></value>))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/xml_spec.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/xml_spec/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Federico Gonzalez"]
|
6
|
+
gem.email = ["fedegl@gmail.com"]
|
7
|
+
gem.description = %q{Test XML with RSpec and Cucumber}
|
8
|
+
gem.summary = %q{Test XML with RSpec and Cucumber}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "xml_spec"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = XmlSpec::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "nokogiri", ">= 1.3.2"
|
19
|
+
gem.add_dependency "rspec", "~> 2.0"
|
20
|
+
|
21
|
+
gem.add_development_dependency "rake", "~> 0.9"
|
22
|
+
gem.add_development_dependency "cucumber", "~> 1.1", ">= 1.1.1"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xml_spec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Federico Gonzalez
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-02-20 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: nokogiri
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 31
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 3
|
32
|
+
- 2
|
33
|
+
version: 1.3.2
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 2
|
47
|
+
- 0
|
48
|
+
version: "2.0"
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rake
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 25
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
- 9
|
63
|
+
version: "0.9"
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: cucumber
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 13
|
75
|
+
segments:
|
76
|
+
- 1
|
77
|
+
- 1
|
78
|
+
version: "1.1"
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 17
|
82
|
+
segments:
|
83
|
+
- 1
|
84
|
+
- 1
|
85
|
+
- 1
|
86
|
+
version: 1.1.1
|
87
|
+
type: :development
|
88
|
+
version_requirements: *id004
|
89
|
+
description: Test XML with RSpec and Cucumber
|
90
|
+
email:
|
91
|
+
- fedegl@gmail.com
|
92
|
+
executables: []
|
93
|
+
|
94
|
+
extensions: []
|
95
|
+
|
96
|
+
extra_rdoc_files: []
|
97
|
+
|
98
|
+
files:
|
99
|
+
- .gitignore
|
100
|
+
- Gemfile
|
101
|
+
- README
|
102
|
+
- Rakefile
|
103
|
+
- lib/xml_spec.rb
|
104
|
+
- lib/xml_spec/cucumber.rb
|
105
|
+
- lib/xml_spec/helpers.rb
|
106
|
+
- lib/xml_spec/matchers.rb
|
107
|
+
- lib/xml_spec/nokogiri.rb
|
108
|
+
- lib/xml_spec/nokogiri/node.rb
|
109
|
+
- lib/xml_spec/version.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/xml_spec/matchers_spec.rb
|
112
|
+
- xml_spec.gemspec
|
113
|
+
homepage: ""
|
114
|
+
licenses: []
|
115
|
+
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: 3
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
hash: 3
|
136
|
+
segments:
|
137
|
+
- 0
|
138
|
+
version: "0"
|
139
|
+
requirements: []
|
140
|
+
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 1.8.10
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: Test XML with RSpec and Cucumber
|
146
|
+
test_files:
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
- spec/xml_spec/matchers_spec.rb
|