has_content? and has_text? can handle regexp as well

closes #740
This commit is contained in:
Vasiliy Ermolovich 2012-07-09 00:13:25 +03:00 committed by Jonas Nicklas
parent 522a6ef7a2
commit 2e4eb7d17a
3 changed files with 69 additions and 3 deletions

View File

@ -167,7 +167,7 @@ module Capybara
normalized_content = normalize_whitespace(content)
synchronize do
normalize_whitespace(text).include?(normalized_content) or
normalize_whitespace(text).match(escape_regexp(normalized_content)) or
raise ExpectationNotMet
end
return true
@ -191,7 +191,7 @@ module Capybara
normalized_content = normalize_whitespace(content)
synchronize do
!normalize_whitespace(text).include?(normalized_content) or
!normalize_whitespace(text).match(escape_regexp(normalized_content)) or
raise ExpectationNotMet
end
return true
@ -436,7 +436,19 @@ module Capybara
# @return [String] Normalized text
#
def normalize_whitespace(text)
text.gsub(/\s+/, ' ').strip
text.is_a?(Regexp) ? text : text.gsub(/\s+/, ' ').strip
end
##
#
# Escapes any characters that would have special meaning in a regexp
# if text is not a regexp
#
# @param [String] text Text to escape
# @return [String] Escaped text
#
def escape_regexp(text)
text.is_a?(Regexp) ? text : Regexp.escape(text)
end
end
end

View File

@ -68,6 +68,21 @@ shared_examples_for 'has_text' do
@session.visit('/with_html')
@session.should_not have_text('Inside element with hidden ancestor')
end
it "should be true if the text in the page matches given regexp" do
@session.visit('/with_html')
@session.should have_text(/Lorem/)
end
it "should be false if the text in the page doesn't match given regexp" do
@session.visit('/with_html')
@session.should_not have_text(/xxxxyzzz/)
end
it "should escape any characters that would have special meaning in a regexp" do
@session.visit('/with_html')
@session.should_not have_text('.orem')
end
end
describe '#has_no_text?' do
@ -134,5 +149,20 @@ shared_examples_for 'has_text' do
@session.visit('/with_html')
@session.should have_no_text('Inside element with hidden ancestor')
end
it "should be true if the text in the page doesn't match given regexp" do
@session.visit('/with_html')
@session.should have_no_text(/xxxxyzzz/)
end
it "should be false if the text in the page matches given regexp" do
@session.visit('/with_html')
@session.should_not have_no_text(/Lorem/)
end
it "should escape any characters that would have special meaning in a regexp" do
@session.visit('/with_html')
@session.should have_no_text('.orem')
end
end
end

View File

@ -231,6 +231,10 @@ describe Capybara::RSpecMatchers do
"<h1>Text</h1>".should have_content('Text')
end
it "passes if has_content? returns true using regexp" do
"<h1>Text</h1>".should have_content(/ext/)
end
it "fails if has_content? returns false" do
expect do
"<h1>Text</h1>".should have_content('No such Text')
@ -243,6 +247,10 @@ describe Capybara::RSpecMatchers do
"<h1>Text</h1>".should_not have_content('No such Text')
end
it "passes because escapes any characters that would have special meaning in a regexp" do
"<h1>Text</h1>".should_not have_content('.')
end
it "fails if has_no_content? returns false" do
expect do
"<h1>Text</h1>".should_not have_content('Text')
@ -261,6 +269,10 @@ describe Capybara::RSpecMatchers do
page.should have_content('This is a test')
end
it "passes if has_content? returns true using regexp" do
page.should have_content(/test/)
end
it "fails if has_content? returns false" do
expect do
page.should have_content('No such Text')
@ -303,6 +315,10 @@ describe Capybara::RSpecMatchers do
"<h1>Text</h1>".should have_text('Text')
end
it "passes if has_text? returns true using regexp" do
"<h1>Text</h1>".should have_text(/ext/)
end
it "fails if has_text? returns false" do
expect do
"<h1>Text</h1>".should have_text('No such Text')
@ -315,6 +331,10 @@ describe Capybara::RSpecMatchers do
"<h1>Text</h1>".should_not have_text('No such Text')
end
it "passes because escapes any characters that would have special meaning in a regexp" do
"<h1>Text</h1>".should_not have_text('.')
end
it "fails if has_no_text? returns false" do
expect do
"<h1>Text</h1>".should_not have_text('Text')
@ -333,6 +353,10 @@ describe Capybara::RSpecMatchers do
page.should have_text('This is a test')
end
it "passes if has_text? returns true using regexp" do
page.should have_text(/test/)
end
it "fails if has_text? returns false" do
expect do
page.should have_text('No such Text')