rspec-xml 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -0
- data/lib/rspec-xml/version.rb +1 -1
- data/lib/rspec-xml/xml_matchers/have_xpath/matcher.rb +10 -4
- data/lib/rspec-xml/xml_matchers/have_xpath.rb +6 -3
- data/lib/rspec-xml/xml_matchers.rb +12 -1
- data/lib/rspec-xml.rb +1 -0
- data/spec/features/have_path.feature +1 -0
- data/spec/features/steps/within_steps.rb +31 -0
- data/spec/features/within.feature +24 -0
- data/spec/rspec-xml/xml_matchers/have_xpath_matcher_spec.rb +7 -2
- data/spec/rspec-xml/xml_matchers_spec.rb +32 -0
- data/spec/spec_helper.rb +2 -0
- metadata +11 -3
data/CHANGELOG.md
ADDED
data/lib/rspec-xml/version.rb
CHANGED
@@ -6,21 +6,27 @@ module RSpecXML
|
|
6
6
|
class Matcher
|
7
7
|
def initialize(options={})
|
8
8
|
self.xpath = options[:xpath]
|
9
|
+
self.example_group = options[:example_group]
|
9
10
|
end
|
10
11
|
|
11
12
|
def matches?(xml)
|
12
|
-
::Nokogiri::XML(xml).xpath(
|
13
|
+
::Nokogiri::XML(xml).xpath(full_xpath).count > 0
|
13
14
|
end
|
14
15
|
|
15
16
|
def failure_message_for_should
|
16
|
-
"expected #{
|
17
|
+
"expected #{full_xpath} to exist"
|
17
18
|
end
|
18
19
|
|
19
20
|
def failure_message_for_should_not
|
20
|
-
"expected #{
|
21
|
+
"expected #{full_xpath} to not exist"
|
21
22
|
end
|
22
23
|
|
23
|
-
|
24
|
+
def full_xpath
|
25
|
+
xpath_stack = example_group.instance_variable_get(:@xpath_stack) || []
|
26
|
+
xpath_stack.join.concat(xpath)
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_accessor :xpath, :example_group
|
24
30
|
end
|
25
31
|
end
|
26
32
|
end
|
@@ -2,13 +2,16 @@ module RSpecXML
|
|
2
2
|
module XMLMatchers
|
3
3
|
class HaveXPath
|
4
4
|
|
5
|
-
def initialize(xpath)
|
6
|
-
self.matcher = Matcher.new(
|
5
|
+
def initialize(xpath, example_group)
|
6
|
+
self.matcher = Matcher.new(
|
7
|
+
:xpath => xpath,
|
8
|
+
:example_group => example_group
|
9
|
+
)
|
7
10
|
end
|
8
11
|
|
9
12
|
def with_text(text)
|
10
13
|
self.matcher = TextMatcher.new(
|
11
|
-
:xpath => matcher.
|
14
|
+
:xpath => matcher.full_xpath,
|
12
15
|
:text => text.to_s )
|
13
16
|
self
|
14
17
|
end
|
@@ -7,7 +7,18 @@ module RSpecXML
|
|
7
7
|
module XMLMatchers
|
8
8
|
|
9
9
|
def have_xpath(xpath)
|
10
|
-
HaveXPath.new(xpath)
|
10
|
+
HaveXPath.new(xpath, self)
|
11
|
+
end
|
12
|
+
|
13
|
+
def within(xpath, &example_group_block)
|
14
|
+
return unless block_given?
|
15
|
+
|
16
|
+
instance_exec(example_group_block) do
|
17
|
+
@xpath_stack ||= []
|
18
|
+
@xpath_stack.push xpath
|
19
|
+
yield
|
20
|
+
@xpath_stack.pop
|
21
|
+
end
|
11
22
|
end
|
12
23
|
|
13
24
|
end
|
data/lib/rspec-xml.rb
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
steps_for :within do
|
2
|
+
step 'there is a string of XML' do
|
3
|
+
@xml = lambda { |insides| "<xml><elem>#{insides}</elem></xml>" }
|
4
|
+
end
|
5
|
+
|
6
|
+
step 'there are the following nodes:' do |table|
|
7
|
+
text = table.hashes.map { |h| "<#{h["node"]}>#{h["value"]}</#{h["node"]}>" }
|
8
|
+
@xml = @xml[text.join]
|
9
|
+
end
|
10
|
+
|
11
|
+
step 'I test for the following text within the corresponding nodes:' do |table|
|
12
|
+
@test = lambda do
|
13
|
+
within('xml') do
|
14
|
+
within('/elem') do
|
15
|
+
table.hashes.each do |hash|
|
16
|
+
@xml.should have_xpath("/#{hash["node"]}").with_text(hash["text"])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
step 'the test should pass' do
|
24
|
+
@test.should_not raise_error(RSpec::Expectations::ExpectationNotMetError)
|
25
|
+
end
|
26
|
+
|
27
|
+
step 'the test should fail' do
|
28
|
+
@test.should raise_error(RSpec::Expectations::ExpectationNotMetError)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
@within
|
2
|
+
Feature: within should be able to accept block as arugment
|
3
|
+
|
4
|
+
Background:
|
5
|
+
Given there is a string of XML
|
6
|
+
Given there are the following nodes:
|
7
|
+
| node | value |
|
8
|
+
| property1 | value1 |
|
9
|
+
| property2 | value2 |
|
10
|
+
|
11
|
+
Scenario: the matcher should pass when the nodes contains the text
|
12
|
+
When I test for the following text within the corresponding nodes:
|
13
|
+
| node | text |
|
14
|
+
| property1 | value1 |
|
15
|
+
| property2 | value2 |
|
16
|
+
Then the test should pass
|
17
|
+
|
18
|
+
Scenario: the matcher should fail when the node doesn't contain the text
|
19
|
+
When I test for the following text within the corresponding nodes:
|
20
|
+
| node | text |
|
21
|
+
| property1 | value1 |
|
22
|
+
| property2 | value9 |
|
23
|
+
Then the test should fail
|
24
|
+
|
@@ -1,18 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module Factory
|
4
|
+
def self.example_group
|
5
|
+
@example_group ||= Class.new
|
6
|
+
end
|
4
7
|
def self.xpath_matcher(xpath)
|
5
|
-
RSpecXML::XMLMatchers::HaveXPath.new(xpath)
|
8
|
+
RSpecXML::XMLMatchers::HaveXPath.new(xpath, example_group)
|
6
9
|
end
|
7
10
|
end
|
8
11
|
|
9
12
|
describe RSpecXML::XMLMatchers::HaveXPath do
|
13
|
+
let(:example_group) { Factory.example_group }
|
14
|
+
|
10
15
|
describe '#intialize' do
|
11
16
|
it 'should build and save a matcher containing the supplied xpath' do
|
12
17
|
|
13
18
|
RSpecXML::XMLMatchers::HaveXPath::Matcher.
|
14
19
|
expects(:new).
|
15
|
-
with(:xpath => 'fake xpath').
|
20
|
+
with(:xpath => 'fake xpath', :example_group => example_group).
|
16
21
|
returns(:flag)
|
17
22
|
|
18
23
|
xpath_matcher = Factory.xpath_matcher('fake xpath')
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RSpecXML::XMLMatchers do
|
4
|
+
describe '#within' do
|
5
|
+
|
6
|
+
it 'should create instance variable @xpath_stack inside within block' do
|
7
|
+
@xpath_stack.should be_nil
|
8
|
+
within("foo/bar") do
|
9
|
+
@xpath_stack.should == ["foo/bar"]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should increase @xpath_stack properly when blocks are nested' do
|
14
|
+
within("foo") do
|
15
|
+
within("/bar") do
|
16
|
+
@xpath_stack.should == ["foo", "/bar"]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should reduce @xpath_stack on exit from block' do
|
22
|
+
within("foo") do
|
23
|
+
within("/bar") do
|
24
|
+
end
|
25
|
+
@xpath_stack.should == ["foo"]
|
26
|
+
end
|
27
|
+
@xpath_stack.should == []
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08
|
12
|
+
date: 2012-09-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: turnip
|
@@ -83,6 +83,7 @@ extensions: []
|
|
83
83
|
extra_rdoc_files: []
|
84
84
|
files:
|
85
85
|
- .gitignore
|
86
|
+
- CHANGELOG.md
|
86
87
|
- Gemfile
|
87
88
|
- Rakefile
|
88
89
|
- lib/rspec-xml.rb
|
@@ -96,9 +97,12 @@ files:
|
|
96
97
|
- rspec-xml.gemspec
|
97
98
|
- spec/features/have_path.feature
|
98
99
|
- spec/features/steps/have_path_steps.rb
|
100
|
+
- spec/features/steps/within_steps.rb
|
101
|
+
- spec/features/within.feature
|
99
102
|
- spec/rspec-xml/xml_matchers/have_xpath/matcher_spec.rb
|
100
103
|
- spec/rspec-xml/xml_matchers/have_xpath/text_matcher_spec.rb
|
101
104
|
- spec/rspec-xml/xml_matchers/have_xpath_matcher_spec.rb
|
105
|
+
- spec/rspec-xml/xml_matchers_spec.rb
|
102
106
|
- spec/spec_helper.rb
|
103
107
|
homepage: ''
|
104
108
|
licenses: []
|
@@ -120,14 +124,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
124
|
version: '0'
|
121
125
|
requirements: []
|
122
126
|
rubyforge_project: rspec-xml
|
123
|
-
rubygems_version: 1.8.
|
127
|
+
rubygems_version: 1.8.19
|
124
128
|
signing_key:
|
125
129
|
specification_version: 3
|
126
130
|
summary: Spec your XML
|
127
131
|
test_files:
|
128
132
|
- spec/features/have_path.feature
|
129
133
|
- spec/features/steps/have_path_steps.rb
|
134
|
+
- spec/features/steps/within_steps.rb
|
135
|
+
- spec/features/within.feature
|
130
136
|
- spec/rspec-xml/xml_matchers/have_xpath/matcher_spec.rb
|
131
137
|
- spec/rspec-xml/xml_matchers/have_xpath/text_matcher_spec.rb
|
132
138
|
- spec/rspec-xml/xml_matchers/have_xpath_matcher_spec.rb
|
139
|
+
- spec/rspec-xml/xml_matchers_spec.rb
|
133
140
|
- spec/spec_helper.rb
|
141
|
+
has_rdoc:
|