2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 15:23:54 -05:00
|
|
|
|
2014-02-16 12:13:58 -05:00
|
|
|
module Capybara
|
|
|
|
# @api private
|
|
|
|
module Queries
|
|
|
|
class BaseQuery
|
2018-01-08 15:23:54 -05:00
|
|
|
COUNT_KEYS = %i[count minimum maximum between].freeze
|
2014-02-16 12:13:58 -05:00
|
|
|
|
|
|
|
attr_reader :options
|
2016-12-15 12:04:01 -05:00
|
|
|
attr_writer :session_options
|
|
|
|
|
2017-05-28 11:54:55 -04:00
|
|
|
def initialize(options)
|
|
|
|
@session_options = options.delete(:session_options)
|
|
|
|
end
|
|
|
|
|
2016-12-15 12:04:01 -05:00
|
|
|
def session_options
|
|
|
|
@session_options || Capybara.session_options
|
|
|
|
end
|
2014-02-16 12:13:58 -05:00
|
|
|
|
|
|
|
def wait
|
2016-12-15 12:04:01 -05:00
|
|
|
self.class.wait(options, session_options.default_max_wait_time)
|
2016-08-30 13:18:06 -04:00
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
def self.wait(options, default = Capybara.default_max_wait_time)
|
2018-03-14 16:16:49 -04:00
|
|
|
# if no value or nil for the :wait option is passed it should default to the default
|
|
|
|
w = options.fetch(:wait, nil)
|
|
|
|
w = default if w.nil?
|
|
|
|
w || 0
|
2014-02-16 12:13:58 -05:00
|
|
|
end
|
|
|
|
|
2016-08-30 14:37:54 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Checks if a count of 0 is valid for the query
|
|
|
|
# Returns false if query does not have any count options specified.
|
|
|
|
#
|
|
|
|
def expects_none?
|
2018-01-13 16:06:03 -05:00
|
|
|
count_specified? ? matches_count?(0) : false
|
2016-08-30 14:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Checks if the given count matches the query count options.
|
|
|
|
# Defaults to true if no count options are specified. If multiple
|
|
|
|
# count options exist, it tests that all conditions are met;
|
|
|
|
# however, if :count is specified, all other options are ignored.
|
|
|
|
#
|
|
|
|
# @param [Integer] count The actual number. Should be coercible via Integer()
|
|
|
|
#
|
|
|
|
def matches_count?(count)
|
2018-01-09 17:05:50 -05:00
|
|
|
return (Integer(options[:count]) == count) if options[:count]
|
2016-08-30 14:37:54 -04:00
|
|
|
return false if options[:maximum] && (Integer(options[:maximum]) < count)
|
|
|
|
return false if options[:minimum] && (Integer(options[:minimum]) > count)
|
2018-01-09 17:05:50 -05:00
|
|
|
return false if options[:between] && !options[:between].include?(count)
|
2018-01-13 16:06:03 -05:00
|
|
|
true
|
2016-08-30 14:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Generates a failure message from the query description and count options.
|
|
|
|
#
|
|
|
|
def failure_message
|
|
|
|
String.new("expected to find #{description}") << count_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
|
|
|
String.new("expected not to find #{description}") << count_message
|
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
private
|
2014-02-16 12:13:58 -05:00
|
|
|
|
2018-01-13 16:06:03 -05:00
|
|
|
def count_specified?
|
|
|
|
COUNT_KEYS.any? { |k| options.key? k }
|
|
|
|
end
|
|
|
|
|
2016-08-30 14:37:54 -04:00
|
|
|
def count_message
|
2018-01-09 17:05:50 -05:00
|
|
|
message = "".dup
|
2016-08-30 14:37:54 -04:00
|
|
|
if options[:count]
|
|
|
|
message << " #{options[:count]} #{Capybara::Helpers.declension('time', 'times', options[:count])}"
|
|
|
|
elsif options[:between]
|
|
|
|
message << " between #{options[:between].first} and #{options[:between].last} times"
|
|
|
|
elsif options[:maximum]
|
|
|
|
message << " at most #{options[:maximum]} #{Capybara::Helpers.declension('time', 'times', options[:maximum])}"
|
|
|
|
elsif options[:minimum]
|
|
|
|
message << " at least #{options[:minimum]} #{Capybara::Helpers.declension('time', 'times', options[:minimum])}"
|
|
|
|
end
|
|
|
|
message
|
|
|
|
end
|
|
|
|
|
2014-02-16 12:13:58 -05:00
|
|
|
def assert_valid_keys
|
|
|
|
invalid_keys = @options.keys - valid_keys
|
2018-01-09 17:05:50 -05:00
|
|
|
return if invalid_keys.empty?
|
|
|
|
|
|
|
|
invalid_names = invalid_keys.map(&:inspect).join(", ")
|
|
|
|
valid_names = valid_keys.map(&:inspect).join(", ")
|
|
|
|
raise ArgumentError, "invalid keys #{invalid_names}, should be one of #{valid_names}"
|
2014-02-16 12:13:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|