Skip to content

Commit

Permalink
has_content? and has_text? can handle regexp as well
Browse files Browse the repository at this point in the history
closes #740
  • Loading branch information
nashby authored and jnicklas committed Jul 13, 2012
1 parent 522a6ef commit 2e4eb7d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/capybara/node/matchers.rb
Expand Up @@ -167,7 +167,7 @@ def has_text?(content)
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
Expand All @@ -191,7 +191,7 @@ def has_no_text?(content)
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
Expand Down Expand Up @@ -436,7 +436,19 @@ def ==(other)
# @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
Expand Down
30 changes: 30 additions & 0 deletions lib/capybara/spec/session/has_text_spec.rb
Expand Up @@ -68,6 +68,21 @@
@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
Expand Down Expand Up @@ -134,5 +149,20 @@
@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
24 changes: 24 additions & 0 deletions spec/rspec/matchers_spec.rb
Expand Up @@ -231,6 +231,10 @@
"<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')
Expand All @@ -243,6 +247,10 @@
"<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')
Expand All @@ -261,6 +269,10 @@
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')
Expand Down Expand Up @@ -303,6 +315,10 @@
"<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')
Expand All @@ -315,6 +331,10 @@
"<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')
Expand All @@ -333,6 +353,10 @@
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')
Expand Down

0 comments on commit 2e4eb7d

Please sign in to comment.