rspec-xml 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem 'nokogiri'
3
+ # bundler doesn't require this from the .gemspec
4
+ # https://github.com/carlhuda/bundler/issues/1041
5
+ gem 'pry'
4
6
 
5
- # Specify your gem's dependencies in rspec-xml.gemspec
6
7
  gemspec
@@ -1,5 +1,3 @@
1
- module Rspec
2
- module Xml
3
- VERSION = "0.0.2"
4
- end
1
+ module RSpecXML
2
+ VERSION = "0.0.3"
5
3
  end
@@ -0,0 +1,84 @@
1
+ module RSpecXML
2
+ module XMLMatchers
3
+ class HaveXPath
4
+
5
+ def initialize(xpath)
6
+ self.xpath = xpath
7
+ @matcher = Matcher.new(:xpath => xpath)
8
+ end
9
+
10
+ def with_text(text)
11
+ @matcher = TextMatcher.new(:xpath => xpath, :text => text)
12
+ self
13
+ end
14
+
15
+ def matches?(xml)
16
+ @matcher.matches?(xml)
17
+ end
18
+
19
+ def failure_message_for_should
20
+ @matcher.failure_message_for_should
21
+ end
22
+
23
+ def failure_message_for_should_not
24
+ @matcher.failure_message_for_should_not
25
+ end
26
+
27
+ private
28
+
29
+ attr_accessor :xpath
30
+
31
+ class Matcher
32
+
33
+ def initialize(options={})
34
+ self.xpath = options[:xpath]
35
+ end
36
+
37
+ def matches?(xml)
38
+ ::Nokogiri::XML(xml).xpath(@xpath).count > 0
39
+ end
40
+
41
+ def failure_message_for_should
42
+ "expected #{xpath} to exist"
43
+ end
44
+
45
+ def failure_message_for_should_not
46
+ "expected #{xpath} to not exist"
47
+ end
48
+
49
+ private
50
+
51
+ attr_accessor :xpath
52
+
53
+ end
54
+
55
+ class TextMatcher
56
+
57
+ def initialize(options)
58
+ self.xpath = options[:xpath]
59
+ self.text = options[:text]
60
+ end
61
+
62
+ def matches?(xml)
63
+ ::Nokogiri::XML(xml).xpath(xpath).text == text
64
+ end
65
+
66
+ def failure_message_for_should
67
+ "expected #{xpath} to contain #{text}"
68
+ end
69
+
70
+ def failure_message_for_should_not
71
+ "expected #{xpath} to contain #{text}"
72
+ end
73
+
74
+ private
75
+
76
+ attr_accessor :text, :xpath
77
+ end
78
+ end
79
+
80
+ def have_xpath(xpath)
81
+ HaveXPath.new(xpath)
82
+ end
83
+ end
84
+ end
data/lib/rspec-xml.rb CHANGED
@@ -1,7 +1,12 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+
4
+ module RSpecXML
5
+ end
6
+
1
7
  require "rspec-xml/version"
8
+ require 'rspec-xml/xml_matchers'
2
9
 
3
- module Rspec
4
- module Xml
5
- # Your code goes here...
6
- end
10
+ RSpec.configure do |config|
11
+ config.include RSpecXML::XMLMatchers
7
12
  end
data/rspec-xml.gemspec CHANGED
@@ -4,7 +4,7 @@ require "rspec-xml/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "rspec-xml"
7
- s.version = Rspec::Xml::VERSION
7
+ s.version = RSpecXML::VERSION
8
8
  s.authors = ["Dan Carper"]
9
9
  s.email = ["dcarper@dreamagile.com"]
10
10
  s.homepage = ""
@@ -17,8 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.require_paths = ["lib"]
19
19
 
20
- # specify any dependencies here; for example:
21
- s.add_development_dependency 'bundler'
20
+ s.add_development_dependency 'turnip'
22
21
 
23
22
  s.add_runtime_dependency 'rspec'
24
23
  s.add_runtime_dependency 'nokogiri'
@@ -0,0 +1,15 @@
1
+ Feature: "some xml".should have_xpath('//something').with_text('Budweiser') ---
2
+
3
+ Background:
4
+ Given there is a string of XML
5
+
6
+ @wip
7
+ Scenario: the matcher should pass when the node contains the text
8
+ Given the best node contains the text: Budweiser
9
+ When I test for the text: Budweiser within the best node
10
+ Then the test should pass
11
+ @wop
12
+ Scenario: the matcher should fail when the node doesn't contain the text
13
+ Given the best node contains the text: Budweiser
14
+ When I test for the text: Miller within the best node
15
+ Then the test should fail
@@ -0,0 +1,22 @@
1
+ steps_for :have_path do
2
+ step 'there is a string of XML' do
3
+ @xml = lambda { |insides| "<xml>#{insides}</xml>" }
4
+ end
5
+
6
+ step 'the :node_name node contains the text: :text' do |node_name, text|
7
+ @xml = @xml.call "<#{node_name}>#{text}</#{node_name}>"
8
+ end
9
+
10
+ step 'I test for the text: :text within the :node_name node' do |text, node_name|
11
+ @test = lambda { @xml.should have_xpath("//#{node_name}").with_text(text) }
12
+ end
13
+
14
+ step 'the test should pass' do
15
+ @test.should_not raise_error(RSpec::Expectations::ExpectationNotMetError)
16
+ end
17
+
18
+ step 'the test should fail' do
19
+ @test.should raise_error(RSpec::Expectations::ExpectationNotMetError)
20
+ end
21
+
22
+ end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,2 @@
1
- require File.expand_path('../../lib/rspec', __FILE__)
1
+ require File.expand_path('../../lib/rspec-xml', __FILE__)
2
+ require 'pry'
@@ -1,19 +1,32 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe XMLMatchers do
3
+ describe RSpecXML::XMLMatchers do
4
4
  describe '#have_xpath' do
5
- it 'should pass if the xpath exists containing the supplied text' do
6
- "<node>HAI</node>".should have_xpath('/node').with_text('HAI')
7
- end
5
+ context '#with_text' do
6
+ it 'should pass if the xpath exists containing the supplied text' do
7
+ "<node>HAI</node>".should have_xpath('/node').with_text('HAI')
8
+ end
8
9
 
9
- it "should fail if the xpath does not exist" do
10
- lambda { "<node>HAI</node>".should have_xpath('/ne').with_text('HAI')}.
11
- should raise_error(RSpec::Expectations::ExpectationNotMetError)
12
- end
10
+ it "should fail if the xpath does not exist" do
11
+ lambda { "<node>HAI</node>".should have_xpath('/ne').with_text('HAI')}.
12
+ should raise_error(RSpec::Expectations::ExpectationNotMetError)
13
+ end
13
14
 
14
- it 'should fail if the xpath does not contain the supplied text' do
15
- lambda { "<node>HAI</node>".should have_xpath('/ne').with_text('HI')}.
16
- should raise_error(RSpec::Expectations::ExpectationNotMetError)
17
- end
15
+ it 'should fail if the xpath does not contain the supplied text' do
16
+ lambda { "<node>HAI</node>".should have_xpath('/ne').with_text('HI')}.
17
+ should raise_error(RSpec::Expectations::ExpectationNotMetError)
18
+ end
19
+ end
20
+
21
+ context 'without specifying text' do
22
+ it 'should pass if the node exists' do
23
+ '<node>something</node>'.should have_xpath('/node')
24
+ end
25
+
26
+ it 'should fail if the node does not exist' do
27
+ lambda { "<node>HAI</node>".should have_xpath('/ne')}.
28
+ should raise_error(RSpec::Expectations::ExpectationNotMetError)
29
+ end
30
+ end
18
31
  end
19
32
  end
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-06 00:00:00.000000000 Z
12
+ date: 2012-08-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: bundler
15
+ name: turnip
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
@@ -71,10 +71,11 @@ files:
71
71
  - Rakefile
72
72
  - lib/rspec-xml.rb
73
73
  - lib/rspec-xml/version.rb
74
- - lib/rspec.rb
75
- - lib/rspec/xml_matchers.rb
74
+ - lib/rspec-xml/xml_matchers.rb
76
75
  - readme.md
77
76
  - rspec-xml.gemspec
77
+ - spec/features/have_path.feature
78
+ - spec/features/steps/have_path_steps.rb
78
79
  - spec/spec_helper.rb
79
80
  - spec/xml_matchers_spec.rb
80
81
  homepage: ''
@@ -102,5 +103,7 @@ signing_key:
102
103
  specification_version: 3
103
104
  summary: Spec your XML
104
105
  test_files:
106
+ - spec/features/have_path.feature
107
+ - spec/features/steps/have_path_steps.rb
105
108
  - spec/spec_helper.rb
106
109
  - spec/xml_matchers_spec.rb
@@ -1,30 +0,0 @@
1
- module XMLMatchers
2
- class HaveXPath
3
-
4
- def initialize(xpath)
5
- @xpath = xpath
6
- end
7
-
8
- def with_text(text)
9
- @text = text
10
- self
11
- end
12
-
13
- def matches?(xml)
14
- ::Nokogiri::XML(xml).xpath(@xpath).text == @text
15
- end
16
-
17
- def failure_message_for_should
18
- "expected #{@xpath} to exist"
19
- end
20
-
21
- def failure_message_for_should_not
22
- "expected #{@xpath} to not exist"
23
- end
24
- end
25
-
26
- def have_xpath(xpath)
27
- HaveXPath.new(xpath)
28
- end
29
- end
30
-
data/lib/rspec.rb DELETED
@@ -1,7 +0,0 @@
1
- Bundler.require
2
- require 'nokogiri'
3
- require 'rspec/xml_matchers'
4
-
5
- RSpec.configure do |config|
6
- config.include XMLMatchers
7
- end