Skip to content

Commit

Permalink
support :count, :minimum, :maximum options
Browse files Browse the repository at this point in the history
  • Loading branch information
pd committed Mar 4, 2008
1 parent 0e6ce1d commit 024d5c4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
24 changes: 20 additions & 4 deletions lib/rspec_hpricot_matchers/have_tag.rb
@@ -1,8 +1,14 @@
module RspecHpricotMatchers
class HaveTag
def initialize(selector, inner_text, &block)
def initialize(selector, inner_text_or_options, options, &block)
@selector = selector
@inner_text = inner_text
if Hash === inner_text_or_options
@inner_text = nil
@options = inner_text_or_options
else
@inner_text = inner_text_or_options
@options = options
end
end

def matches?(actual)
Expand Down Expand Up @@ -38,6 +44,16 @@ def matches?(actual)
end
end

if @options[:count]
return false unless matched_elements.length == @options[:count]
end
if @options[:minimum]
return false unless matched_elements.length >= @options[:minimum]
end
if @options[:maximum]
return false unless matched_elements.length <= @options[:maximum]
end

!matched_elements.empty?
end

Expand All @@ -52,7 +68,7 @@ def negative_failure_message
end
end

def have_tag(selector, inner_text = nil, &block)
HaveTag.new(selector, inner_text, &block)
def have_tag(selector, inner_text_or_options = nil, options = {}, &block)
HaveTag.new(selector, inner_text_or_options, options, &block)
end
end
55 changes: 48 additions & 7 deletions spec/rspec_hpricot_matchers/have_tag_spec.rb
Expand Up @@ -35,21 +35,62 @@
end

it "should support nested have_tag() calls" do
@html.should have_tag('ul') do |e|
e.should have_tag('li')
@html.should have_tag('ul') do |ul|
ul.should have_tag('li')
end
end

it "should support negated nested have_tag() calls" do
@html.should have_tag('ul') do |e|
e.should_not have_tag('dd')
@html.should have_tag('ul') do |ul|
ul.should_not have_tag('dd')
end
end

it "should treat multiple nested have_tag() expectations as a logical AND" do
@html.should have_tag('ul') do |e|
e.should have_tag('li')
e.should_not have_tag('dd')
@html.should have_tag('ul') do |ul|
ul.should have_tag('li')
ul.should_not have_tag('dd')
end
end
end

describe 'have_tag with counts' do
before(:each) do
@html = <<-EOHTML
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Foo again</li>
<li><a href="/baz">With inner elements</a></li>
</ul>
EOHTML
end

it "should treat :count as expecting exactly n matched elements" do
@html.should have_tag('li', :count => 4)
@html.should_not have_tag('li', :count => 3)
@html.should_not have_tag('li', :count => 5)
end

it "should treat :minimum as expecting at least n matched elements" do
(0..4).each { |n| @html.should have_tag('li', :minimum => n) }
@html.should_not have_tag('li', :minimum => 5)
end

it "should treat :maximum as expecting at most n matched elements" do
@html.should_not have_tag('li', :maximum => 3)
@html.should have_tag('li', :maximum => 4)
@html.should have_tag('li', :maximum => 5)
end

it "should support matching of content while specifying a count" do
@html.should have_tag('li', /foo/i, :count => 2)
end

it "should work when the have_tag is nested" do
@html.should have_tag('ul') do |ul|
ul.should have_tag('li', :minimum => 2)
ul.should have_tag('li', /foo/i, :count => 2)
end
end
end

0 comments on commit 024d5c4

Please sign in to comment.