From 08e0f610ec5ecc1677a2bbbd87035c524638da8b Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Thu, 4 Apr 2019 14:15:27 -0700 Subject: [PATCH] Prefer match? when match data not needed --- lib/capybara/node/simple.rb | 2 +- lib/capybara/queries/selector_query.rb | 8 ++++---- lib/capybara/queries/style_query.rb | 2 +- lib/capybara/rack_test/form.rb | 2 +- lib/capybara/selector.rb | 8 ++++---- lib/capybara/selector/css.rb | 2 +- lib/capybara/selector/filters/base.rb | 2 +- lib/capybara/selenium/driver.rb | 2 +- .../selenium/driver_specializations/chrome_driver.rb | 4 ++-- lib/capybara/server/animation_disabler.rb | 2 +- lib/capybara/session/config.rb | 4 ++-- 11 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/capybara/node/simple.rb b/lib/capybara/node/simple.rb index 76b33ec3..111419ec 100644 --- a/lib/capybara/node/simple.rb +++ b/lib/capybara/node/simple.rb @@ -107,7 +107,7 @@ module Capybara !find_xpath(VISIBILITY_XPATH) else # No need for an xpath if only checking the current element - !(native.key?('hidden') || (native[:style] =~ /display:\s?none/) || %w[script head].include?(tag_name)) + !(native.key?('hidden') || (/display:\s?none/.match? native[:style]) || %w[script head].include?(tag_name)) end end diff --git a/lib/capybara/queries/selector_query.rb b/lib/capybara/queries/selector_query.rb index 6da5aa7b..a1d436ab 100644 --- a/lib/capybara/queries/selector_query.rb +++ b/lib/capybara/queries/selector_query.rb @@ -368,13 +368,13 @@ module Capybara def matches_id_filter?(node) return true unless use_default_id_filter? && options[:id].is_a?(Regexp) - node[:id] =~ options[:id] + options[:id].match? node[:id] end def matches_class_filter?(node) return true unless use_default_class_filter? && options[:class].is_a?(Regexp) - node[:class] =~ options[:class] + options[:class].match? node[:class] end def matches_style_filter?(node) @@ -382,7 +382,7 @@ module Capybara when String, nil true when Regexp - node[:style] =~ options[:style] + options[:style].match? node[:style] when Hash matches_style?(node, options[:style]) end @@ -392,7 +392,7 @@ module Capybara @actual_styles = node.initial_cache[:style] || node.style(*styles.keys) styles.all? do |style, value| if value.is_a? Regexp - @actual_styles[style.to_s] =~ value + value.match? @actual_styles[style.to_s] else @actual_styles[style.to_s] == value end diff --git a/lib/capybara/queries/style_query.rb b/lib/capybara/queries/style_query.rb index ea4b9f0d..f45af212 100644 --- a/lib/capybara/queries/style_query.rb +++ b/lib/capybara/queries/style_query.rb @@ -19,7 +19,7 @@ module Capybara @actual_styles = node.style(*@expected_styles.keys) @expected_styles.all? do |style, value| if value.is_a? Regexp - @actual_styles[style] =~ value + value.match? @actual_styles[style] else @actual_styles[style] == value end diff --git a/lib/capybara/rack_test/form.rb b/lib/capybara/rack_test/form.rb index ad3d1102..827bc8f8 100644 --- a/lib/capybara/rack_test/form.rb +++ b/lib/capybara/rack_test/form.rb @@ -56,7 +56,7 @@ private end def request_method - self[:method].to_s.match?(/post/i) ? :post : :get + /post/i.match?(self[:method]) ? :post : :get end def merge_param!(params, key, value) diff --git a/lib/capybara/selector.rb b/lib/capybara/selector.rb index 5528b1d4..8e442183 100644 --- a/lib/capybara/selector.rb +++ b/lib/capybara/selector.rb @@ -45,7 +45,7 @@ end Capybara.add_selector(:id, locator_type: [String, Symbol, Regexp]) do xpath { |id| builder(XPath.descendant).add_attribute_conditions(id: id) } - locator_filter { |node, id| id.is_a?(Regexp) ? node[:id] =~ id : true } + locator_filter { |node, id| id.is_a?(Regexp) ? id.match?(node[:id]) : true } end Capybara.add_selector(:field, locator_type: [String, Symbol]) do @@ -71,7 +71,7 @@ Capybara.add_selector(:field, locator_type: [String, Symbol]) do node_filter(:readonly, :boolean) { |node, value| !(value ^ node.readonly?) } node_filter(:with) do |node, with| val = node.value - (with.is_a?(Regexp) ? val =~ with : val == with.to_s).tap do |res| + (with.is_a?(Regexp) ? with.match?(val) : val == with.to_s).tap do |res| add_error("Expected value to be #{with.inspect} but was #{val.inspect}") unless res end end @@ -219,7 +219,7 @@ Capybara.add_selector(:fillable_field, locator_type: [String, Symbol]) do node_filter(:with) do |node, with| val = node.value - (with.is_a?(Regexp) ? val =~ with : val == with.to_s).tap do |res| + (with.is_a?(Regexp) ? with.match?(val) : val == with.to_s).tap do |res| add_error("Expected value to be #{with.inspect} but was #{val.inspect}") unless res end end @@ -618,7 +618,7 @@ Capybara.add_selector(:element, locator_type: [String, Symbol]) do node_filter(:attributes, matcher: /.+/) do |node, name, val| next true unless val.is_a?(Regexp) - (node[name] =~ val).tap do |res| + (val.match? node[name]).tap do |res| add_error("Expected #{name} to match #{val.inspect} but it was #{node[name]}") unless res end end diff --git a/lib/capybara/selector/css.rb b/lib/capybara/selector/css.rb index 842893c0..821dd9e4 100644 --- a/lib/capybara/selector/css.rb +++ b/lib/capybara/selector/css.rb @@ -6,7 +6,7 @@ module Capybara def self.escape(str) value = str.dup out = +'' - out << value.slice!(0...1) if value =~ /^[-_]/ + out << value.slice!(0...1) if value.match?(/^[-_]/) out << (value[0].match?(NMSTART) ? value.slice!(0...1) : escape_char(value.slice!(0...1))) out << value.gsub(/[^a-zA-Z0-9_-]/) { |char| escape_char char } out diff --git a/lib/capybara/selector/filters/base.rb b/lib/capybara/selector/filters/base.rb index 5d2ad0cb..0532724b 100644 --- a/lib/capybara/selector/filters/base.rb +++ b/lib/capybara/selector/filters/base.rb @@ -34,7 +34,7 @@ module Capybara def handles_option?(option_name) if matcher? - option_name =~ @matcher + @matcher.match? option_name else @name == option_name end diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index f005e184..ae8d5c50 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -338,7 +338,7 @@ private end def silenced_unknown_error_message?(msg) - silenced_unknown_error_messages.any? { |regex| msg =~ regex } + silenced_unknown_error_messages.any? { |regex| msg.match? regex } end def silenced_unknown_error_messages diff --git a/lib/capybara/selenium/driver_specializations/chrome_driver.rb b/lib/capybara/selenium/driver_specializations/chrome_driver.rb index 6449cb3b..61954bd6 100644 --- a/lib/capybara/selenium/driver_specializations/chrome_driver.rb +++ b/lib/capybara/selenium/driver_specializations/chrome_driver.rb @@ -8,7 +8,7 @@ module Capybara::Selenium::Driver::ChromeDriver begin super rescue NoMethodError => e - raise unless e.message =~ /full_screen_window/ + raise unless e.message.match?(/full_screen_window/) result = bridge.http.call(:post, "session/#{bridge.session_id}/window/fullscreen", {}) result['value'] @@ -19,7 +19,7 @@ module Capybara::Selenium::Driver::ChromeDriver def resize_window_to(handle, width, height) super rescue Selenium::WebDriver::Error::UnknownError => err - raise unless err.message =~ /failed to change window state/ + raise unless err.message.match?(/failed to change window state/) # Chromedriver doesn't wait long enough for state to change when coming out of fullscreen # and raises unnecessary error. Wait a bit and try again. diff --git a/lib/capybara/server/animation_disabler.rb b/lib/capybara/server/animation_disabler.rb index 001f81f4..7d1501c4 100644 --- a/lib/capybara/server/animation_disabler.rb +++ b/lib/capybara/server/animation_disabler.rb @@ -36,7 +36,7 @@ module Capybara attr_reader :disable_markup def html_content? - !!(@headers['Content-Type'] =~ /html/) + /html/.match?(@headers['Content-Type']) end def insert_disable(html) diff --git a/lib/capybara/session/config.rb b/lib/capybara/session/config.rb index 7624b215..0c088230 100644 --- a/lib/capybara/session/config.rb +++ b/lib/capybara/session/config.rb @@ -77,14 +77,14 @@ module Capybara remove_method :app_host= def app_host=(url) - raise ArgumentError, "Capybara.app_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}." if url && url !~ URI::DEFAULT_PARSER.make_regexp + raise ArgumentError, "Capybara.app_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}." unless url.nil? || url.match?(URI::DEFAULT_PARSER.make_regexp) @app_host = url end remove_method :default_host= def default_host=(url) - raise ArgumentError, "Capybara.default_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}." if url && url !~ URI::DEFAULT_PARSER.make_regexp + raise ArgumentError, "Capybara.default_host should be set to a url (http://www.example.com). Attempted to set #{url.inspect}." unless url.nil? || url.match?(URI::DEFAULT_PARSER.make_regexp) @default_host = url end