2011-02-04 08:47:15 -05:00
|
|
|
module Capybara
|
|
|
|
module RSpecMatchers
|
2011-02-06 08:36:42 -05:00
|
|
|
class HaveSelector
|
|
|
|
def initialize(*args)
|
|
|
|
@args = args
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def matches?(actual)
|
|
|
|
@actual = wrap(actual)
|
|
|
|
@actual.has_selector?(*@args)
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def does_not_match?(actual)
|
|
|
|
@actual = wrap(actual)
|
|
|
|
@actual.has_no_selector?(*@args)
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def failure_message_for_should
|
|
|
|
if normalized[:selector].failure_message
|
|
|
|
normalized[:selector].failure_message.call(@actual)
|
|
|
|
else
|
2011-02-06 14:07:12 -05:00
|
|
|
"expected #{selector_name} to return something"
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def failure_message_for_should_not
|
2011-02-06 14:07:12 -05:00
|
|
|
"expected #{selector_name} not to return anything"
|
|
|
|
end
|
|
|
|
|
|
|
|
def selector_name
|
|
|
|
name = "#{normalized[:selector].name} #{normalized[:locator].inspect}"
|
|
|
|
name << " with text #{normalized[:options][:text].inspect}" if normalized[:options][:text]
|
|
|
|
name
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def wrap(actual)
|
|
|
|
if actual.respond_to?("has_selector?")
|
|
|
|
actual
|
|
|
|
else
|
|
|
|
Capybara.string(actual.to_s)
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def normalized
|
|
|
|
@normalized ||= Capybara::Selector.normalize(*@args)
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
|
|
|
end
|
2011-02-06 08:36:42 -05:00
|
|
|
|
|
|
|
def have_selector(*args)
|
|
|
|
HaveSelector.new(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def have_xpath(*args)
|
|
|
|
HaveSelector.new(:xpath, *args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def have_css(*args)
|
|
|
|
HaveSelector.new(:css, *args)
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
|
|
|
end
|