rspec_tag_matchers 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.textile +105 -0
- data/Rakefile +74 -0
- data/lib/rspec_tag_matchers.rb +9 -0
- data/lib/rspec_tag_matchers/have_tag.rb +114 -0
- data/spec/rspec_tag_matchers/have_tag_spec.rb +241 -0
- data/spec/spec_helper.rb +11 -0
- metadata +83 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Kyle Hargraves
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
h1. RSPEC_TAG_MATCHER
|
2
|
+
|
3
|
+
_Implementation of *have_tag()* RSpec matcher using *Nokogiri*._
|
4
|
+
|
5
|
+
h2. Description
|
6
|
+
|
7
|
+
An implementation of rspec_on_rails' @have_tag()@ matcher which does not depend on Rails' @assert_select()@. Using *Nokogiri* instead, the matcher is available to non-Rails projects, and enjoys the full flexibility of Nokogiri's advanced and blazing fast CSS and XPath selector support.
|
8
|
+
|
9
|
+
h2. Installation
|
10
|
+
|
11
|
+
*Gem:*
|
12
|
+
|
13
|
+
<pre>sudo gem install rspec_tag_matchers --source http://gemcutter.org</pre>
|
14
|
+
|
15
|
+
*Dependencies:*
|
16
|
+
|
17
|
+
* "rspec":http://github.com/dchelimsky/rspec (stating the obvious)
|
18
|
+
|
19
|
+
*Configuration*
|
20
|
+
|
21
|
+
In @spec_helper.rb@:
|
22
|
+
|
23
|
+
<pre>
|
24
|
+
require 'rspec_tag_matchers'
|
25
|
+
|
26
|
+
Spec::Runner.configure do |config|
|
27
|
+
config.include(RspecTagMatchers)
|
28
|
+
end
|
29
|
+
</pre>
|
30
|
+
|
31
|
+
h2. Usage
|
32
|
+
|
33
|
+
As its first argument, @have_tag()@ accepts any CSS or XPath selectors which are supported by *Nokogiri*.
|
34
|
+
|
35
|
+
<pre>
|
36
|
+
body.should have_tag('form[@action*=session]')
|
37
|
+
body.should have_tag('ul > li + li')
|
38
|
+
</pre>
|
39
|
+
|
40
|
+
Expectations can be placed upon the inner text of the matched element by providing another argument, which should be either a @String@ or a @Regexp@:
|
41
|
+
|
42
|
+
<pre>
|
43
|
+
body.should have_tag('h1', 'Welcome')
|
44
|
+
body.should have_tag('p', /a very important blurb/i)
|
45
|
+
</pre>
|
46
|
+
|
47
|
+
Expectations can be placed upon the number of matched elements by passing an options hash:
|
48
|
+
|
49
|
+
<pre>
|
50
|
+
body.should have_tag('abbr', :count => 1) # exactly one
|
51
|
+
body.should have_tag('dt', :minimum => 4) # at least 4
|
52
|
+
body.should have_tag('dd', :maximum => 4) # at most 4
|
53
|
+
body.should have_tag('a.outgoing', /rspec/i, :count => 2)
|
54
|
+
</pre>
|
55
|
+
|
56
|
+
The @:count@ key also accepts a @Range@, making the following equivalent:
|
57
|
+
|
58
|
+
<pre>
|
59
|
+
body.should have_tag('tr', :count => 3..5)
|
60
|
+
body.should have_tag('tr', :minimum => 3, :maximum => 5)
|
61
|
+
</pre>
|
62
|
+
|
63
|
+
The usage of @with_tag()@, however, is no longer supported. Instead, a block passed to @have_tag()@ will have each matched element successively yielded to it. If none of the blocks return without raising an @ExpectationNotMetError@, the outer @have_tag()@ is treated as having failed:
|
64
|
+
|
65
|
+
<pre>
|
66
|
+
body.should have_tag('thead') do |thead|
|
67
|
+
thead.should have_tag('th', :count => 5)
|
68
|
+
end
|
69
|
+
</pre>
|
70
|
+
|
71
|
+
This also allows arbitrary expectations to be applied from within the block, such as:
|
72
|
+
|
73
|
+
<pre>
|
74
|
+
body.should have_tag('dl dd.sha1') do |dd|
|
75
|
+
dd.inner_text.length.should == 40
|
76
|
+
end
|
77
|
+
</pre>
|
78
|
+
|
79
|
+
h2. Notes
|
80
|
+
|
81
|
+
Currently, this implementation does not support substitution values as assert_select did (by way of @HTML::Selector@):
|
82
|
+
|
83
|
+
*Note supported:*
|
84
|
+
|
85
|
+
<pre>
|
86
|
+
body.should have_tag('li[class=?]', dom_class)
|
87
|
+
body.should have_tag('tr.person#?', /^person-\d+$/)
|
88
|
+
</pre>
|
89
|
+
|
90
|
+
I (Kyle Hargraves, original author) rarely use these, and Hpricot's advanced selectors make them mostly useless, as far as I can tell, so I am unlikely to implement them myself.
|
91
|
+
|
92
|
+
This @have_tag()@ further differs from the @assert_select@-based implementation in that the nested @have_tag()@ calls must *all* pass on a single selected element in order to be true. This was a source of confusion in RSpec ticket #316. There is a spec covering this case if you need an example.
|
93
|
+
|
94
|
+
h2. Origin
|
95
|
+
|
96
|
+
This project was originally forked from:
|
97
|
+
|
98
|
+
"http://github.com/cjohansen/validatious-on-rails":http://github.com/pd/rspec_hpricot_matchers
|
99
|
+
|
100
|
+
h2. License
|
101
|
+
|
102
|
+
*Original work:*
|
103
|
+
|
104
|
+
Released under the MIT license.<br />
|
105
|
+
Copyright (c) 2008 "Kyle Hargraves":http://github.com/pd
|
data/Rakefile
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'spec/rake/spectask'
|
8
|
+
rescue LoadError
|
9
|
+
begin
|
10
|
+
gem 'rspec-rails', '>= 1.0.0'
|
11
|
+
require 'spec/rake/spectask'
|
12
|
+
rescue LoadError
|
13
|
+
puts "RSpec - or one of it's dependencies - is not available. Install it with: sudo gem install rspec-rails"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
NAME = "rspec_tag_matchers"
|
18
|
+
SUMMARY = %Q{Implementation of have_tag() RSpec matcher using Nokogiri.}
|
19
|
+
DESCRIPTION = <<-END
|
20
|
+
rspec_tag_matchers provides an implementation of rspec_on_rails'
|
21
|
+
have_tag() matcher which does not depend on Rails' assert_select().
|
22
|
+
Using Nokogiri instead, the matcher is available to non-Rails projects,
|
23
|
+
and enjoys the full flexibility of Nokogiri's advanced and blazing fast
|
24
|
+
CSS and XPath selector support. Forked from rspec_hpricot_matchers.
|
25
|
+
END
|
26
|
+
HOMEPAGE = "http://github.com/grimen/#{NAME}"
|
27
|
+
AUTHOR = "Kyle Hargraves"
|
28
|
+
EMAIL = "pd@krh.me"
|
29
|
+
SUPPORT_FILES = %w(README.textile)
|
30
|
+
|
31
|
+
begin
|
32
|
+
gem 'jeweler', '>= 1.0.0'
|
33
|
+
require 'jeweler'
|
34
|
+
|
35
|
+
Jeweler::Tasks.new do |gemspec|
|
36
|
+
gemspec.name = NAME
|
37
|
+
gemspec.summary = SUMMARY
|
38
|
+
gemspec.description = DESCRIPTION
|
39
|
+
gemspec.homepage = HOMEPAGE
|
40
|
+
gemspec.author = AUTHOR
|
41
|
+
gemspec.email = EMAIL
|
42
|
+
|
43
|
+
gemspec.require_paths = %w{lib}
|
44
|
+
gemspec.files = SUPPORT_FILES << %w(MIT-LICENSE Rakefile) << Dir.glob(File.join(*%w[{generators,lib,test} ** *]).to_s)
|
45
|
+
gemspec.extra_rdoc_files = SUPPORT_FILES
|
46
|
+
|
47
|
+
gemspec.add_dependency 'nokogiri', '>= 1.4.0'
|
48
|
+
gemspec.add_dependency 'rspec-rails', '>= 1.2.6'
|
49
|
+
end
|
50
|
+
|
51
|
+
Jeweler::GemcutterTasks.new
|
52
|
+
rescue LoadError
|
53
|
+
puts "Jeweler - or one of it's dependencies - is not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
|
54
|
+
end
|
55
|
+
|
56
|
+
desc 'Default: Run specs.'
|
57
|
+
task :default => :spec
|
58
|
+
|
59
|
+
desc %Q{Generate documentation.}
|
60
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
61
|
+
rdoc.rdoc_dir = 'rdoc'
|
62
|
+
rdoc.title = NAME
|
63
|
+
rdoc.options << '--line-numbers' << '--inline-source' << '--charset=UTF-8'
|
64
|
+
rdoc.rdoc_files.include(SUPPORT_FILES)
|
65
|
+
rdoc.rdoc_files.include(File.join(*%w[lib ** *.rb]))
|
66
|
+
end
|
67
|
+
|
68
|
+
#if defined?(Spec)
|
69
|
+
desc 'Run specs.'
|
70
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
71
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
72
|
+
t.spec_opts = ['-c']
|
73
|
+
end
|
74
|
+
#end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module RspecTagMatchers
|
4
|
+
class HaveTag
|
5
|
+
def initialize(selector, inner_text_or_options, options, &block)
|
6
|
+
@selector = selector
|
7
|
+
if Hash === inner_text_or_options
|
8
|
+
@inner_text = nil
|
9
|
+
@options = inner_text_or_options
|
10
|
+
else
|
11
|
+
@inner_text = inner_text_or_options
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def matches?(actual, &block)
|
17
|
+
@actual = actual
|
18
|
+
@hdoc = hdoc_for(@actual)
|
19
|
+
|
20
|
+
matched_elements = @hdoc.search(@selector)
|
21
|
+
if matched_elements.empty?
|
22
|
+
return @options[:count] == 0
|
23
|
+
end
|
24
|
+
|
25
|
+
if @inner_text
|
26
|
+
matched_elements = filter_on_inner_text(matched_elements)
|
27
|
+
end
|
28
|
+
|
29
|
+
if block
|
30
|
+
matched_elements = filter_on_nested_expectations(matched_elements, block)
|
31
|
+
end
|
32
|
+
|
33
|
+
@actual_count = matched_elements.length
|
34
|
+
return false if not acceptable_count?(@actual_count)
|
35
|
+
|
36
|
+
!matched_elements.empty?
|
37
|
+
end
|
38
|
+
|
39
|
+
def failure_message
|
40
|
+
explanation = @actual_count ? "but found #{@actual_count}" : "but did not"
|
41
|
+
"expected\n#{@hdoc.to_s}\nto have #{failure_count_phrase} #{failure_selector_phrase}, #{explanation}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def negative_failure_message
|
45
|
+
explanation = @actual_count ? "but found #{@actual_count}" : "but did"
|
46
|
+
"expected\n#{@hdoc.to_s}\nnot to have #{failure_count_phrase} #{failure_selector_phrase}, #{explanation}"
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def hdoc_for(input)
|
51
|
+
if Nokogiri::XML::Document === input
|
52
|
+
input
|
53
|
+
elsif input.respond_to?(:body)
|
54
|
+
Nokogiri::HTML(input.body)
|
55
|
+
else
|
56
|
+
Nokogiri::HTML(input.to_s)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def filter_on_inner_text(elements)
|
61
|
+
elements.select do |el|
|
62
|
+
next(el.inner_text =~ @inner_text) if @inner_text.is_a?(Regexp)
|
63
|
+
el.inner_text == @inner_text
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def filter_on_nested_expectations(elements, block)
|
68
|
+
elements.select do |el|
|
69
|
+
begin
|
70
|
+
block.call(el)
|
71
|
+
rescue Spec::Expectations::ExpectationNotMetError
|
72
|
+
false
|
73
|
+
else
|
74
|
+
true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def acceptable_count?(actual_count)
|
80
|
+
if @options[:count]
|
81
|
+
return false unless @options[:count] === actual_count
|
82
|
+
end
|
83
|
+
if @options[:minimum]
|
84
|
+
return false unless actual_count >= @options[:minimum]
|
85
|
+
end
|
86
|
+
if @options[:maximum]
|
87
|
+
return false unless actual_count <= @options[:maximum]
|
88
|
+
end
|
89
|
+
true
|
90
|
+
end
|
91
|
+
|
92
|
+
def failure_count_phrase
|
93
|
+
if @options[:count]
|
94
|
+
"#{@options[:count]} elements matching"
|
95
|
+
elsif @options[:minimum] || @options[:maximum]
|
96
|
+
count_explanations = []
|
97
|
+
count_explanations << "at least #{@options[:minimum]}" if @options[:minimum]
|
98
|
+
count_explanations << "at most #{@options[:maximum]}" if @options[:maximum]
|
99
|
+
"#{count_explanations.join(' and ')} elements matching"
|
100
|
+
else
|
101
|
+
"an element matching"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def failure_selector_phrase
|
106
|
+
phrase = @selector.inspect
|
107
|
+
phrase << (@inner_text ? " with inner text #{@inner_text.inspect}" : "")
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def have_tag(selector, inner_text_or_options = nil, options = {}, &block)
|
112
|
+
HaveTag.new(selector, inner_text_or_options, options, &block)
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,241 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), *%w[.. spec_helper])
|
3
|
+
|
4
|
+
describe 'have_tag' do
|
5
|
+
before(:each) do
|
6
|
+
@html = "<ul><li>An egregiously long string</li></ul>"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should match against strings" do
|
10
|
+
@html.should have_tag('li')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should match against nokogiri documents" do
|
14
|
+
hdoc = Nokogiri::HTML(@html)
|
15
|
+
hdoc.should have_tag('li')
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should use the response of #body if the target responds to it" do
|
19
|
+
response = Object.new
|
20
|
+
class << response
|
21
|
+
def body
|
22
|
+
"<ul><li>An egregiously long string</li></ul>"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
response.should have_tag('li')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should not match when the target does not have the selected element" do
|
29
|
+
@html.should_not have_tag('dd')
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should match against the inner text of the selected element" do
|
33
|
+
@html.should have_tag('li', 'An egregiously long string')
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should match negatively against the inner text" do
|
37
|
+
@html.should_not have_tag('li', 'Some other string entirely')
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should match against a Regexp describing the inner text" do
|
41
|
+
@html.should have_tag('li', /GREG/i)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should match negatively against a Regexp describing the inner text" do
|
45
|
+
@html.should_not have_tag('li', /GREG/)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should include the body in the failure message" do
|
49
|
+
lambda {
|
50
|
+
@html.should have_tag('abbr')
|
51
|
+
}.should raise_error(SpecFailed, /#{Regexp.escape(@html)}/)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should include the selector in the failure message" do
|
55
|
+
lambda {
|
56
|
+
@html.should have_tag('abbr')
|
57
|
+
}.should raise_error(SpecFailed, /"abbr"/)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should include the expected inner text if provided" do
|
61
|
+
lambda {
|
62
|
+
@html.should have_tag('li', /something else/)
|
63
|
+
}.should raise_error(SpecFailed, %r{/something else/})
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'have_tag inner expectations' do
|
68
|
+
before(:each) do
|
69
|
+
@html = "<ul><li>An egregiously long string</li></ul>"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should fail when the outer selector fails" do
|
73
|
+
lambda {
|
74
|
+
@html.should have_tag('dl') do |dl|
|
75
|
+
dl.should have_tag('li')
|
76
|
+
end
|
77
|
+
}.should raise_error(SpecFailed)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should match the inner expectations against the elements matched by the outer selector" do
|
81
|
+
@html.should have_tag('ul') do |ul|
|
82
|
+
ul.should have_tag('li')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should support negated inner expectations" do
|
87
|
+
@html.should have_tag('ul') do |ul|
|
88
|
+
ul.should_not have_tag('dd')
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should treat multiple nested have_tag() expectations as a logical AND" do
|
93
|
+
@html.should have_tag('ul') do |ul|
|
94
|
+
ul.should have_tag('li')
|
95
|
+
ul.should_not have_tag('dd')
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should only match against a single element at a time when nesting expectations (see RSpec LH#316)" do
|
100
|
+
html = <<-EOHTML
|
101
|
+
<ul>
|
102
|
+
<li>
|
103
|
+
<a href="1">1</a>
|
104
|
+
</li>
|
105
|
+
<li>
|
106
|
+
<a href="2">2</a>
|
107
|
+
<span>Hello</span>
|
108
|
+
</li>
|
109
|
+
</ul>
|
110
|
+
EOHTML
|
111
|
+
|
112
|
+
html.should have_tag('li', :count => 1) do |li|
|
113
|
+
li.should have_tag('a')
|
114
|
+
li.should have_tag('span')
|
115
|
+
end
|
116
|
+
|
117
|
+
html.should have_tag('li', :count => 1) do |li|
|
118
|
+
li.should have_tag('a')
|
119
|
+
li.should_not have_tag('span')
|
120
|
+
end
|
121
|
+
|
122
|
+
html.should have_tag('li', :count => 2) do |li|
|
123
|
+
li.should have_tag('a')
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should yield elements which respond to #body" do
|
128
|
+
@html.should have_tag('ul') do |ul|
|
129
|
+
ul.should respond_to(:body)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should supports arbitrary expectations within the block" do
|
134
|
+
html = %q{<span class="sha1">cbc0bd52f99fe19304bccad383694e92b8ee2c71</span>}
|
135
|
+
html.should have_tag('span.sha1') do |span|
|
136
|
+
span.inner_text.length.should == 40
|
137
|
+
end
|
138
|
+
html.should_not have_tag('span.sha1') do |span|
|
139
|
+
span.inner_text.length.should == 41
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should include a description of the inner expectation in the failure message" do
|
144
|
+
pending "No idea how to implement this."
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe 'have_tag with counts' do
|
149
|
+
before(:each) do
|
150
|
+
@html = <<-EOHTML
|
151
|
+
<ul>
|
152
|
+
<li>Foo</li>
|
153
|
+
<li>Bar</li>
|
154
|
+
<li>Foo again</li>
|
155
|
+
<li><a href="/baz">With inner elements</a></li>
|
156
|
+
</ul>
|
157
|
+
EOHTML
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should treat an integer :count as expecting exactly n matched elements" do
|
161
|
+
@html.should have_tag('li', :count => 4)
|
162
|
+
@html.should_not have_tag('li', :count => 3)
|
163
|
+
@html.should_not have_tag('li', :count => 5)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should treat a range :count as expecting between x and y matched elements" do
|
167
|
+
@html.should have_tag('li', :count => 1..5)
|
168
|
+
@html.should_not have_tag('li', :count => 2..3)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should treat a :count of zero as if a negative match were expected" do
|
172
|
+
@html.should have_tag('dd', :count => 0)
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should treat :minimum as expecting at least n matched elements" do
|
176
|
+
(0..4).each { |n| @html.should have_tag('li', :minimum => n) }
|
177
|
+
@html.should_not have_tag('li', :minimum => 5)
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should treat :maximum as expecting at most n matched elements" do
|
181
|
+
@html.should_not have_tag('li', :maximum => 3)
|
182
|
+
@html.should have_tag('li', :maximum => 4)
|
183
|
+
@html.should have_tag('li', :maximum => 5)
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should support matching of content while specifying a count" do
|
187
|
+
@html.should have_tag('li', /foo/i, :count => 2)
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should work when the have_tag is nested" do
|
191
|
+
@html.should have_tag('ul') do |ul|
|
192
|
+
ul.should have_tag('li', :minimum => 2)
|
193
|
+
ul.should have_tag('li', /foo/i, :count => 2)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should include the actual number of elements matched in the failure message" do
|
198
|
+
lambda {
|
199
|
+
@html.should have_tag('li', :count => 3)
|
200
|
+
}.should raise_error(SpecFailed, /found 4/)
|
201
|
+
lambda {
|
202
|
+
@html.should have_tag('li', :count => 5)
|
203
|
+
}.should raise_error(SpecFailed, /found 4/)
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should include the actual number of elements matched in the negative failure message" do
|
207
|
+
lambda {
|
208
|
+
@html.should_not have_tag('li', :count => 4)
|
209
|
+
}.should raise_error(SpecFailed, /found 4/)
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should include the expected number of elements in the failure message" do
|
213
|
+
lambda {
|
214
|
+
@html.should have_tag('li', :count => 2)
|
215
|
+
}.should raise_error(SpecFailed, /to have 2 elements matching/)
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should include the expected number of elements in the negative failure message" do
|
219
|
+
lambda {
|
220
|
+
@html.should_not have_tag('li', :count => 4)
|
221
|
+
}.should raise_error(SpecFailed, /to have 4 elements matching/)
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should describe the :minimum case using 'at least ...'" do
|
225
|
+
lambda {
|
226
|
+
@html.should have_tag('li', :minimum => 80)
|
227
|
+
}.should raise_error(SpecFailed, /to have at least 80 elements matching/)
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should describe the :maximum case using 'at most ...'" do
|
231
|
+
lambda {
|
232
|
+
@html.should have_tag('li', :maximum => 2)
|
233
|
+
}.should raise_error(SpecFailed, /to have at most 2 elements matching/)
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should describe the :minimum and :maximum case using 'at least ... and at most ...'" do
|
237
|
+
lambda {
|
238
|
+
@html.should have_tag('li', :minimum => 8, :maximum => 30)
|
239
|
+
}.should raise_error(SpecFailed, /to have at least 8 and at most 30 elements matching/)
|
240
|
+
end
|
241
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$: << File.join(File.dirname(__FILE__), *%w[.. lib])
|
3
|
+
require 'rspec_tag_matchers'
|
4
|
+
|
5
|
+
Spec::Runner.configure do |config|
|
6
|
+
config.include(RspecTagMatchers)
|
7
|
+
end
|
8
|
+
|
9
|
+
unless defined?(SpecFailed)
|
10
|
+
SpecFailed = Spec::Expectations::ExpectationNotMetError
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec_tag_matchers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Hargraves
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-22 00:00:00 +01: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.4.0
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec-rails
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.6
|
34
|
+
version:
|
35
|
+
description: " rspec_tag_matchers provides an implementation of rspec_on_rails'\n have_tag() matcher which does not depend on Rails' assert_select().\n Using Nokogiri instead, the matcher is available to non-Rails projects,\n and enjoys the full flexibility of Nokogiri's advanced and blazing fast \n CSS and XPath selector support. Forked from rspec_hpricot_matchers.\n"
|
36
|
+
email: pd@krh.me
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- MIT-LICENSE
|
43
|
+
- README.textile
|
44
|
+
- Rakefile
|
45
|
+
- lib/rspec_tag_matchers.rb
|
46
|
+
- lib/rspec_tag_matchers/have_tag.rb
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.textile
|
50
|
+
- Rakefile
|
51
|
+
- lib/rspec_tag_matchers.rb
|
52
|
+
- lib/rspec_tag_matchers/have_tag.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/grimen/rspec_tag_matchers
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --charset=UTF-8
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.5
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Implementation of have_tag() RSpec matcher using Nokogiri.
|
81
|
+
test_files:
|
82
|
+
- spec/rspec_tag_matchers/have_tag_spec.rb
|
83
|
+
- spec/spec_helper.rb
|