From a0f913be7d7589b5a744e0ba6087b9ef5c118ac6 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Tue, 27 Feb 2018 15:46:13 -0800 Subject: [PATCH] Capybara::Helpers keyword args --- lib/capybara/helpers.rb | 23 +++++++++++------------ lib/capybara/queries/text_query.rb | 4 ++-- lib/capybara/queries/title_query.rb | 2 +- lib/capybara/session.rb | 2 +- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/capybara/helpers.rb b/lib/capybara/helpers.rb index 423f8c56..3dc08e40 100644 --- a/lib/capybara/helpers.rb +++ b/lib/capybara/helpers.rb @@ -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(//) - return html.clone.insert match.end(0), "" if match + return html.clone.insert match.end(0), "" 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 diff --git a/lib/capybara/queries/text_query.rb b/lib/capybara/queries/text_query.rb index 02d60b40..2b29235e 100644 --- a/lib/capybara/queries/text_query.rb +++ b/lib/capybara/queries/text_query.rb @@ -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" diff --git a/lib/capybara/queries/title_query.rb b/lib/capybara/queries/title_query.rb index fbfd198d..6013c020 100644 --- a/lib/capybara/queries/title_query.rb +++ b/lib/capybara/queries/title_query.rb @@ -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 diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index a5b99467..86eb9bbf 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -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