Skip to content

Commit

Permalink
Make "should contain" matcher work with inner text instead of HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 7, 2008
1 parent b23dcfb commit 7b3e6fa
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
78 changes: 72 additions & 6 deletions lib/webrat/core/matchers/have_content.rb
Expand Up @@ -3,12 +3,31 @@ module Matchers

class HasContent
def initialize(content)
# Require nokogiri and fall back on rexml
begin
require "nokogiri"
require "webrat/nokogiri"
rescue LoadError => e
if require "rexml/document"
require "webrat/vendor/nokogiri/css"
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
end
end

@content = content
end

def matches?(element)
element = element.body.to_s if element.respond_to?(:body)
@element = element

def matches?(stringlike)
if defined?(Nokogiri::XML)
matches_nokogiri?(stringlike)
else
matches_rexml?(stringlike)
end
end

def matches_rexml?(stringlike)
@document = rexml_document(stringlike)
@element = @document.inner_text

case @content
when String
Expand All @@ -18,16 +37,63 @@ def matches?(element)
end
end

def matches_nokogiri?(stringlike)
@document = nokogiri_document(stringlike)
@element = @document.inner_text

case @content
when String
@element.include?(@content)
when Regexp
@element.match(@content)
end
end

def rexml_document(stringlike)
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)

case stringlike
when REXML::Document
stringlike.root
when REXML::Node
stringlike
when StringIO, String
begin
REXML::Document.new(stringlike.to_s).root
rescue REXML::ParseException => e
if e.message.include?("second root element")
REXML::Document.new("<fake-root-element>#{stringlike}</fake-root-element>").root
else
raise e
end
end
end
end

def nokogiri_document(stringlike)
return stringlike.dom if stringlike.respond_to?(:dom)
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)

case stringlike
when Nokogiri::HTML::Document, Nokogiri::XML::NodeSet
stringlike
when StringIO
Nokogiri::HTML(stringlike.string)
else
Nokogiri::HTML(stringlike.to_s)
end
end

# ==== Returns
# String:: The failure message.
def failure_message
"expected the following element's content to #{content_message}:\n#{@element.inner_text}"
"expected the following element's content to #{content_message}:\n#{@element}"
end

# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected the following element's content to not #{content_message}:\n#{@element.inner_text}"
"expected the following element's content to not #{content_message}:\n#{@element}"
end

def content_message
Expand Down
20 changes: 6 additions & 14 deletions spec/api/matchers_spec.rb
Expand Up @@ -58,31 +58,23 @@
include Webrat::Matchers

before(:each) do
@element = stub(:element)
@element.stub!(:inner_text).and_return <<-EOF
@body = <<-EOF
<div id='main'>
<div class='inner'>hello, world!</div>
</div>
EOF

@element.stub!(:include?)
@element.stub!(:match)
end

describe "#matches?" do
it "should call element#include? when the argument is a string" do
@element.should_receive(:include?)

Webrat::Matchers::HasContent.new("hello, world!").matches?(@element)
@body.should contain("hello, world!")
end

it "should call element#match when the argument is a regular expression" do
@element.should_receive(:match)

Webrat::Matchers::HasContent.new(/hello, world/).matches?(@element)
@body.should contain(/hello, world/)
end
end

describe "#failure_message" do
it "should include the content string" do
hc = Webrat::Matchers::HasContent.new("hello, world!")
Expand All @@ -100,9 +92,9 @@

it "should include the element's inner content" do
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
hc.matches?(@element)
hc.matches?(@body)

hc.failure_message.should include(@element.inner_text)
hc.failure_message.should include("hello, world!")
end
end
end
Expand Down

0 comments on commit 7b3e6fa

Please sign in to comment.