1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/rspec/matchers.rb

162 lines
3.8 KiB
Ruby
Raw Normal View History

module Capybara
module RSpecMatchers
2013-02-16 04:02:32 -05:00
class Matcher
def wrap(actual)
if actual.respond_to?("has_selector?")
actual
else
Capybara.string(actual.to_s)
end
end
end
class HaveSelector < Matcher
def initialize(*args)
@args = args
end
def matches?(actual)
2012-07-09 07:39:57 -04:00
wrap(actual).assert_selector(*@args)
end
def does_not_match?(actual)
2012-07-09 07:39:57 -04:00
wrap(actual).assert_no_selector(*@args)
2011-02-06 14:07:12 -05:00
end
def description
"have #{query.description}"
end
def query
@query ||= Capybara::Query.new(*@args)
end
end
2013-02-16 04:02:32 -05:00
class HaveText < Matcher
2013-03-03 18:04:23 -05:00
attr_reader :type, :content, :options
2013-03-03 18:04:23 -05:00
def initialize(*args)
@type = args.shift if args.first.is_a?(Symbol)
@content = args.shift
@options = (args.first.is_a?(Hash))? args.first : {}
end
def matches?(actual)
@actual = wrap(actual)
2013-03-03 18:04:23 -05:00
@actual.has_text?(type, content, options)
end
def does_not_match?(actual)
@actual = wrap(actual)
2013-03-03 18:04:23 -05:00
@actual.has_no_text?(type, content, options)
end
def failure_message
2013-03-17 10:48:04 -04:00
message = Capybara::Helpers.failure_message(description, options)
2013-03-03 18:04:23 -05:00
message << " in #{format(@actual.text(type))}"
message
end
def failure_message_when_negated
failure_message.sub(/(to find)/, 'not \1')
end
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
def description
2013-03-03 18:04:23 -05:00
"text #{format(content)}"
end
2013-03-03 18:04:23 -05:00
def format(content)
content = Capybara::Helpers.normalize_whitespace(content) unless content.is_a? Regexp
content.inspect
end
end
2013-02-16 04:02:32 -05:00
class HaveTitle < Matcher
attr_reader :title
def initialize(title)
@title = title
end
def matches?(actual)
@actual = wrap(actual)
@actual.has_title?(title)
end
def does_not_match?(actual)
@actual = wrap(actual)
@actual.has_no_title?(title)
end
def failure_message
2013-02-16 04:02:32 -05:00
"expected there to be title #{title.inspect} in #{@actual.title.inspect}"
end
def failure_message_when_negated
2013-02-16 04:02:32 -05:00
"expected there not to be title #{title.inspect} in #{@actual.title.inspect}"
end
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
2013-02-16 04:02:32 -05:00
def description
"have title #{title.inspect}"
end
end
def have_selector(*args)
HaveSelector.new(*args)
end
def have_xpath(xpath, options={})
HaveSelector.new(:xpath, xpath, options)
end
2013-03-03 18:04:23 -05:00
def have_css(css, options={})
HaveSelector.new(:css, css, options)
end
2011-02-10 11:15:28 -05:00
2013-03-03 18:04:23 -05:00
def have_text(*args)
HaveText.new(*args)
end
2013-03-03 18:04:23 -05:00
alias_method :have_content, :have_text
2011-02-13 11:04:13 -05:00
2013-02-16 04:02:32 -05:00
def have_title(title)
HaveTitle.new(title)
end
def have_link(locator, options={})
HaveSelector.new(:link, locator, options)
2011-02-13 11:04:13 -05:00
end
2011-03-25 05:54:52 -04:00
def have_button(locator, options={})
HaveSelector.new(:button, locator, options)
end
def have_field(locator, options={})
HaveSelector.new(:field, locator, options)
end
def have_checked_field(locator, options={})
HaveSelector.new(:field, locator, options.merge(:checked => true))
end
def have_unchecked_field(locator, options={})
HaveSelector.new(:field, locator, options.merge(:unchecked => true))
2011-03-25 05:54:52 -04:00
end
def have_select(locator, options={})
HaveSelector.new(:select, locator, options)
end
def have_table(locator, options={})
HaveSelector.new(:table, locator, options)
end
end
end