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

Capybara::Helpers keyword args

This commit is contained in:
Thomas Walpole 2018-02-27 15:46:13 -08:00
parent 4a7c4d2314
commit a0f913be7d
4 changed files with 15 additions and 16 deletions

View file

@ -24,14 +24,16 @@ module Capybara
# if text is not a regexp
#
# @param [String] text Text to escape
# @return [String] Escaped text
# @param [Boolean] exact (false) Whether or not this should be an exact text match
# @param [Fixnum, Boolean, nil] options Options passed to Regexp.new when creating the Regexp
# @return [Regexp] Regexp to match the passed in text and options
#
def to_regexp(text, regexp_options = nil, exact = false)
def to_regexp(text, exact: false, options: nil)
return text if text.is_a?(Regexp)
escaped = Regexp.escape(normalize_whitespace(text))
escaped = "\\A#{escaped}\\z" if exact
Regexp.new(escaped, regexp_options)
Regexp.new(escaped, options)
end
##
@ -40,12 +42,13 @@ module Capybara
# `Capybara.asset_host`.
#
# @param [String] html HTML code to inject into
# @param [URL] host (Capybara.asset_host) The host from which assets should be loaded
# @return [String] The modified HTML code
#
def inject_asset_host(html, asset_host = Capybara.asset_host)
if asset_host && Nokogiri::HTML(html).css("base").empty?
def inject_asset_host(html, host: Capybara.asset_host)
if host && Nokogiri::HTML(html).css("base").empty?
match = html.match(/<head[^<]*?>/)
return html.clone.insert match.end(0), "<base href='#{asset_host}' />" if match
return html.clone.insert match.end(0), "<base href='#{host}' />" if match
end
html
end
@ -65,13 +68,9 @@ module Capybara
end
if defined?(Process::CLOCK_MONOTONIC)
def monotonic_time
Process.clock_gettime Process::CLOCK_MONOTONIC
end
def monotonic_time; Process.clock_gettime Process::CLOCK_MONOTONIC; end
else
def monotonic_time
Time.now.to_f
end
def monotonic_time; Time.now.to_f; end
end
end
end

View file

@ -19,7 +19,7 @@ module Capybara
super(@options)
self.session_options = session_options
@search_regexp = Capybara::Helpers.to_regexp(@expected_text, nil, exact?)
@search_regexp = Capybara::Helpers.to_regexp(@expected_text, exact: exact?)
assert_valid_keys
end
@ -69,7 +69,7 @@ module Capybara
end
def case_insensitive_message
insensitive_regexp = Capybara::Helpers.to_regexp(@expected_text, Regexp::IGNORECASE)
insensitive_regexp = Capybara::Helpers.to_regexp(@expected_text, options: Regexp::IGNORECASE)
insensitive_count = @actual_text.scan(insensitive_regexp).size
if insensitive_count != @count
"it was found #{insensitive_count} #{Capybara::Helpers.declension("time", "times", insensitive_count)} using a case insensitive search"

View file

@ -8,7 +8,7 @@ module Capybara
@expected_title = expected_title.is_a?(Regexp) ? expected_title : Capybara::Helpers.normalize_whitespace(expected_title)
@options = options
super(@options)
@search_regexp = Capybara::Helpers.to_regexp(@expected_title, nil, options.fetch(:exact, false))
@search_regexp = Capybara::Helpers.to_regexp(@expected_title, exact: options.fetch(:exact, false))
assert_valid_keys
end

View file

@ -680,7 +680,7 @@ module Capybara
#
def save_page(path = nil)
prepare_path(path, 'html').tap do |p|
File.write(p, Capybara::Helpers.inject_asset_host(body, config.asset_host), mode: 'wb')
File.write(p, Capybara::Helpers.inject_asset_host(body, host: config.asset_host), mode: 'wb')
end
end