2016-03-07 16:52:19 -08:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 12:23:54 -08:00
|
|
|
|
2014-02-16 20:13:58 +03:00
|
|
|
module Capybara
|
|
|
|
# @api private
|
|
|
|
module Queries
|
|
|
|
class BaseQuery
|
2018-01-08 12:23:54 -08:00
|
|
|
COUNT_KEYS = %i[count minimum maximum between].freeze
|
2014-02-16 20:13:58 +03:00
|
|
|
|
|
|
|
attr_reader :options
|
2016-12-15 09:04:01 -08:00
|
|
|
attr_writer :session_options
|
|
|
|
|
2017-05-28 08:54:55 -07:00
|
|
|
def initialize(options)
|
|
|
|
@session_options = options.delete(:session_options)
|
|
|
|
end
|
|
|
|
|
2016-12-15 09:04:01 -08:00
|
|
|
def session_options
|
|
|
|
@session_options || Capybara.session_options
|
|
|
|
end
|
2014-02-16 20:13:58 +03:00
|
|
|
|
|
|
|
def wait
|
2016-12-15 09:04:01 -08:00
|
|
|
self.class.wait(options, session_options.default_max_wait_time)
|
2016-08-30 10:18:06 -07:00
|
|
|
end
|
|
|
|
|
2018-01-09 14:05:50 -08:00
|
|
|
def self.wait(options, default = Capybara.default_max_wait_time)
|
2018-03-14 13:16:49 -07:00
|
|
|
# if no value or nil for the :wait option is passed it should default to the default
|
2018-08-20 16:46:31 -07:00
|
|
|
wait = options.fetch(:wait, nil)
|
|
|
|
wait = default if wait.nil?
|
|
|
|
wait || 0
|
2014-02-16 20:13:58 +03:00
|
|
|
end
|
|
|
|
|
2016-08-30 11:37:54 -07: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 13:06:03 -08:00
|
|
|
count_specified? ? matches_count?(0) : false
|
2016-08-30 11:37:54 -07: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 14:05:50 -08:00
|
|
|
return (Integer(options[:count]) == count) if options[:count]
|
2016-08-30 11:37:54 -07:00
|
|
|
return false if options[:maximum] && (Integer(options[:maximum]) < count)
|
|
|
|
return false if options[:minimum] && (Integer(options[:minimum]) > count)
|
2018-01-09 14:05:50 -08:00
|
|
|
return false if options[:between] && !options[:between].include?(count)
|
2018-01-13 13:06:03 -08:00
|
|
|
true
|
2016-08-30 11:37:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Generates a failure message from the query description and count options.
|
|
|
|
#
|
|
|
|
def failure_message
|
2018-05-10 13:20:23 -07:00
|
|
|
+"expected to find #{description}" << count_message
|
2016-08-30 11:37:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
2018-05-10 13:20:23 -07:00
|
|
|
+"expected not to find #{description}" << count_message
|
2016-08-30 11:37:54 -07:00
|
|
|
end
|
|
|
|
|
2018-01-09 14:05:50 -08:00
|
|
|
private
|
2014-02-16 20:13:58 +03:00
|
|
|
|
2018-01-13 13:06:03 -08:00
|
|
|
def count_specified?
|
2018-08-20 16:46:31 -07:00
|
|
|
COUNT_KEYS.any? { |key| options.key? key }
|
2018-01-13 13:06:03 -08:00
|
|
|
end
|
|
|
|
|
2016-08-30 11:37:54 -07:00
|
|
|
def count_message
|
2018-07-10 14:18:39 -07:00
|
|
|
message = +''
|
|
|
|
count, between, maximum, minimum = options.values_at(:count, :between, :maximum, :minimum)
|
|
|
|
if count
|
|
|
|
message << " #{count} #{Capybara::Helpers.declension('time', 'times', count)}"
|
|
|
|
elsif between
|
|
|
|
message << " between #{between.first} and #{between.last} times"
|
|
|
|
elsif maximum
|
|
|
|
message << " at most #{maximum} #{Capybara::Helpers.declension('time', 'times', maximum)}"
|
|
|
|
elsif minimum
|
|
|
|
message << " at least #{minimum} #{Capybara::Helpers.declension('time', 'times', minimum)}"
|
2016-08-30 11:37:54 -07:00
|
|
|
end
|
|
|
|
message
|
|
|
|
end
|
|
|
|
|
2014-02-16 20:13:58 +03:00
|
|
|
def assert_valid_keys
|
2018-05-26 10:26:44 -07:00
|
|
|
invalid_keys = @options.keys - valid_keys
|
2018-01-09 14:05:50 -08:00
|
|
|
return if invalid_keys.empty?
|
|
|
|
|
2018-07-10 14:18:39 -07:00
|
|
|
invalid_names = invalid_keys.map(&:inspect).join(', ')
|
|
|
|
valid_names = valid_keys.map(&:inspect).join(', ')
|
2018-01-09 14:05:50 -08:00
|
|
|
raise ArgumentError, "invalid keys #{invalid_names}, should be one of #{valid_names}"
|
2014-02-16 20:13:58 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|