Skip to content

Commit

Permalink
Pulling features up from have_tag into have_selector
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Feb 24, 2009
1 parent d96899b commit 2296cad
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 231 deletions.
85 changes: 70 additions & 15 deletions lib/webrat/core/matchers/have_selector.rb
@@ -1,24 +1,79 @@
require "webrat/core/matchers/have_xpath"

module Webrat
module Matchers

class HaveSelector < HaveXpath #:nodoc:

# ==== Returns
# String:: The failure message.
def failure_message
"expected following text to match selector #{@expected}:\n#{@document}"
"expected following output to contain a #{tag_inspect} tag:\n#{@document}"
end

# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected following text to not match selector #{@expected}:\n#{@document}"
"expected following output to omit a #{tag_inspect}:\n#{@document}"
end


def matches?(stringlike, &block)
@block ||= block
matched = matches(stringlike)

options = @expected.last.dup

if options[:count]
matched.size == options[:count] && (!@block || @block.call(matched))
else
matched.any? && (!@block || @block.call(matched))
end
end

def tag_inspect
options = @expected.last.dup
content = options.delete(:content)

html = "<#{@expected.first}"
options.each do |k,v|
html << " #{k}='#{v}'"
end

if content
html << ">#{content}</#{@expected.first}>"
else
html << "/>"
end

html
end

def query
Nokogiri::CSS::Parser.parse(*super).map { |ast| ast.to_xpath }
options = @expected.last.dup
selector = @expected.first.to_s

options.each do |key, value|
next if [:content, :count].include?(key)
selector << "[#{key}='#{value}']"
end

q = Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }.first

if options[:content] && options[:content].include?("'") && options[:content].include?('"')
parts = options[:content].split("'").map do |part|
"'#{part}'"
end

string = "concat(" + parts.join(", \"'\", ") + ")"
q << "[contains(., #{string})]"
elsif options[:content] && options[:content].include?("'")
q << "[contains(., \"#{options[:content]}\")]"
elsif options[:content]
q << "[contains(., '#{options[:content]}')]"
end

q
end

end

# Matches HTML content against a CSS 3 selector.
Expand All @@ -28,24 +83,24 @@ def query
#
# ==== Returns
# HaveSelector:: A new have selector matcher.
def have_selector(expected, &block)
HaveSelector.new(expected, &block)
def have_selector(name, attributes = {}, &block)
HaveSelector.new([name, attributes], &block)
end
alias_method :match_selector, :have_selector


# Asserts that the body of the response contains
# the supplied selector
def assert_have_selector(expected)
hs = HaveSelector.new(expected)
assert hs.matches?(response_body), hs.failure_message
def assert_have_selector(name, attributes = {}, &block)
matcher = HaveSelector.new([name, attributes], &block)
assert matcher.matches?(response_body), matcher.failure_message
end

# Asserts that the body of the response
# does not contain the supplied string or regepx
def assert_have_no_selector(expected)
hs = HaveSelector.new(expected)
assert !hs.matches?(response_body), hs.negative_failure_message
def assert_have_no_selector(name, attributes = {}, &block)
matcher = HaveSelector.new([name, attributes], &block)
assert !matcher.matches?(response_body), matcher.negative_failure_message
end

end
Expand Down
93 changes: 8 additions & 85 deletions lib/webrat/core/matchers/have_tag.rb
@@ -1,97 +1,20 @@
require "webrat/core/matchers/have_selector"

module Webrat

module HaveTagMatcher

class HaveTag < ::Webrat::Matchers::HaveSelector #:nodoc:
# ==== Returns
# String:: The failure message.
def failure_message
"expected following output to contain a #{tag_inspect} tag:\n#{@document}"
end

# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected following output to omit a #{tag_inspect}:\n#{@document}"
end

def matches?(stringlike, &block)
@block ||= block
matched = matches(stringlike)

options = @expected.last.dup

if options[:count]
matched.size == options[:count] && (!@block || @block.call(matched))
else
matched.any? && (!@block || @block.call(matched))
end
end

def tag_inspect
options = @expected.last.dup
content = options.delete(:content)

html = "<#{@expected.first}"
options.each do |k,v|
html << " #{k}='#{v}'"
end

if content
html << ">#{content}</#{@expected.first}>"
else
html << "/>"
end

html
end

def query
options = @expected.last.dup
selector = @expected.first.to_s

options.each do |key, value|
next if [:content, :count].include?(key)
selector << "[#{key}='#{value}']"
end

q = Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }.first

if options[:content] && options[:content].include?("'") && options[:content].include?('"')
parts = options[:content].split("'").map do |part|
"'#{part}'"
end

string = "concat(" + parts.join(", \"'\", ") + ")"
q << "[contains(., #{string})]"
elsif options[:content] && options[:content].include?("'")
q << "[contains(., \"#{options[:content]}\")]"
elsif options[:content]
q << "[contains(., '#{options[:content]}')]"
end

q
end
end

def have_tag(name, attributes = {}, &block)
HaveTag.new([name, attributes], &block)
def have_tag(*args, &block)
have_selector(*args, &block)
end

alias_method :match_tag, :have_tag

# Asserts that the body of the response contains
# the supplied tag with the associated selectors
def assert_have_tag(name, attributes = {})
ht = HaveTag.new([name, attributes])
assert ht.matches?(response_body), ht.failure_message
def assert_have_tag(*args, &block)
assert_have_selector(*args, &block)
end

# Asserts that the body of the response
# does not contain the supplied string or regepx
def assert_have_no_tag(name, attributes = {})
ht = HaveTag.new([name, attributes])
assert !ht.matches?(response_body), ht.negative_failure_message
def assert_have_no_tag(*args, &block)
assert_have_no_selector(*args, &block)
end

end
Expand Down
83 changes: 64 additions & 19 deletions spec/public/matchers/have_selector_spec.rb
Expand Up @@ -26,40 +26,85 @@
@body.should_not have_selector("p")
end

it "should be able to loop over all the matched elements" do
@body.should have_selector("div") do |node|
node.first.name.should == "div"
describe "specifying attributes" do
it "should be able to specify the attributes of the tag" do
@body.should have_selector("div", :class => "inner")
end
end

it "should not match of any of the matchers in the block fail" do
lambda {
@body.should have_selector("div") do |node|
node.first.name.should == "p"
end
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
describe "specifying content" do
it "should be able to specify the content of the tag" do
@body.should have_selector("div", :content => "hello, world!")
end

it "should be able to specify the content of the tag with double quotes in it" do
@body.should have_selector("h2", :content => 'Welcome "Bryan"')
end

it "should be able to specify the content of the tag with single quotes in it" do
@body.should have_selector("h3", :content => "Welcome 'Bryan'")
end

it "should be able to specify the content of the tag with both kinds of quotes" do
@body.should have_selector("h4", :content => "Welcome 'Bryan\"")
end
end

it "should be able to use #have_selector in the block" do
@body.should have_selector("#main") do |node|
node.should have_selector(".inner")
describe "specifying counts" do
it "should be able to specify the number of occurences of the tag" do
@body.should have_selector("li", :count => 2)
end

it "should not match if the count is wrong" do
lambda {
@body.should have_selector("li", :count => 3)
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
end

it "should not match any parent tags in the block" do
lambda {
@body.should have_selector(".inner") do |node|
node.should have_selector("#main")
describe "specifying nested elements" do
it "should be able to loop over all the matched elements" do
@body.should have_selector("div") do |node|
node.first.name.should == "div"
end
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end

it "should not match of any of the matchers in the block fail" do
lambda {
@body.should have_selector("div") do |node|
node.first.name.should == "p"
end
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end

it "should be able to use #have_selector in the block" do
@body.should have_selector("#main") do |node|
node.should have_selector(".inner")
end
end

it "should not match any parent tags in the block" do
lambda {
@body.should have_selector(".inner") do |node|
node.should have_selector("#main")
end
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end

it "should work with items that have multiple child nodes" do
@body.should have_selector("ul") do |n|
n.should have_selector("li", :content => "First")
n.should have_selector("li", :content => "Second")
end
end
end

describe "asserts for selector," do
describe "Test::Unit assertions" do
include Test::Unit::Assertions

before(:each) do
should_receive(:response_body).and_return @body
require 'test/unit'
should_receive(:response_body).and_return @body
end

describe "assert_have_selector" do
Expand Down

0 comments on commit 2296cad

Please sign in to comment.