Skip to content

Commit

Permalink
[#25 state:open] Added tests to create matches_id? function in link
Browse files Browse the repository at this point in the history
  • Loading branch information
gaffo committed Oct 29, 2008
1 parent 8ce99cc commit 275829d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/webrat/core/link.rb
Expand Up @@ -33,11 +33,22 @@ def matches_text?(link_text)
html =~ matcher || title =~ matcher
end

def matches_id?(id_or_regexp)
if id_or_regexp.is_a?(Regexp)
(id =~ id_or_regexp) ? true : false
else
(id == id_or_regexp) ? true : false
end
end

def text
@element.innerHTML
end

protected
def id
@element['id']
end

def data
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
Expand Down
50 changes: 50 additions & 0 deletions spec/webrat/core/link_spec.rb
Expand Up @@ -20,4 +20,54 @@
link.click
end

it "should matches_text? on regexp" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.matches_text?(/link/i).should == 0
end

it "should matches_text? on link_text" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.matches_text?("Link Text").should == 0
end

it "should not matches_text? on link_text case insensitive" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end

it "should match text including  " do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.matches_text?("Link Text").should == 0
end

it "should not matches_text? on wrong text" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Some Other Link")
link.should_receive(:title).and_return(nil)
link.matches_text?("Link Text").should == false
end

it "should matches_id? on exact matching id" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:id).and_return("some_id")
link.matches_id?("some_id").should == true
end

it "should not matches_id? on incorrect id" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:id).and_return("other_id")
link.matches_id?("some_id").should == false
end

it "should matches_id? on matching id by regexp" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:id).and_return("some_id")
link.matches_id?(/some/).should == true
end

end

0 comments on commit 275829d

Please sign in to comment.