From 3f7c35856471875e4faaaf94b4086ee6dc7f7cf5 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 9 May 2022 14:37:08 -0700 Subject: [PATCH] minor code cleanup --- lib/capybara/helpers.rb | 2 +- lib/capybara/rack_test/form.rb | 2 +- lib/capybara/selector/filter_set.rb | 9 ++++----- lib/capybara/selector/regexp_disassembler.rb | 7 ++----- lib/capybara/selenium/extensions/html5_drag.rb | 6 ++---- lib/capybara/selenium/node.rb | 4 +--- lib/capybara/spec/session/find_spec.rb | 2 +- 7 files changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/capybara/helpers.rb b/lib/capybara/helpers.rb index c25f79a3d..e2a9d5b8a 100644 --- a/lib/capybara/helpers.rb +++ b/lib/capybara/helpers.rb @@ -84,7 +84,7 @@ def filter_backtrace(trace) def warn(message, uplevel: 1) Kernel.warn(message, uplevel: uplevel) end - + if defined?(Process::CLOCK_MONOTONIC_RAW) def monotonic_time; Process.clock_gettime Process::CLOCK_MONOTONIC_RAW; end elsif defined?(Process::CLOCK_MONOTONIC_PRECISE) diff --git a/lib/capybara/rack_test/form.rb b/lib/capybara/rack_test/form.rb index 0c12a5087..863610b9c 100644 --- a/lib/capybara/rack_test/form.rb +++ b/lib/capybara/rack_test/form.rb @@ -17,7 +17,7 @@ def path; @empty_file.path; end def size; 0; end def read; ''; end def append_to(_); end - def set_encoding(_); end + def set_encoding(_); end # rubocop:disable Naming/AccessorMethodName end def params(button) diff --git a/lib/capybara/selector/filter_set.rb b/lib/capybara/selector/filter_set.rb index cd6a04ec4..d65b2548e 100644 --- a/lib/capybara/selector/filter_set.rb +++ b/lib/capybara/selector/filter_set.rb @@ -101,11 +101,10 @@ def expression_filter_descriptions private def options_with_defaults(options) - expression_filters.chain(node_filters) - .select { |_n, filter| filter.default? } - .each_with_object(options.dup) do |(name, filter), opts| - opts[name] = filter.default unless opts.key?(name) - end + expression_filters + .chain(node_filters) + .filter_map { |name, filter| [name, filter.default] if filter.default? } + .to_h.merge!(options) end def add_filter(name, filter_class, *types, matcher: nil, **options, &block) diff --git a/lib/capybara/selector/regexp_disassembler.rb b/lib/capybara/selector/regexp_disassembler.rb index 3c00e603b..e86f379d9 100644 --- a/lib/capybara/selector/regexp_disassembler.rb +++ b/lib/capybara/selector/regexp_disassembler.rb @@ -69,11 +69,8 @@ def combine(strs) suffixes = [[]] strs.reverse_each do |str| if str.is_a? Set - prefixes = str.each_with_object([]) { |s, memo| memo.concat combine(s) } - - result = [] - prefixes.product(suffixes) { |pair| result << pair.flatten(1) } - suffixes = result + prefixes = str.flat_map { |s| combine(s) } + suffixes = prefixes.product(suffixes).map { |pair| pair.flatten(1) } else suffixes.each { |arr| arr.unshift str } end diff --git a/lib/capybara/selenium/extensions/html5_drag.rb b/lib/capybara/selenium/extensions/html5_drag.rb index 7aeade4be..adee84e95 100644 --- a/lib/capybara/selenium/extensions/html5_drag.rb +++ b/lib/capybara/selenium/extensions/html5_drag.rb @@ -39,10 +39,8 @@ def html5_drop(*args) input.set_file(args) driver.execute_script DROP_FILE, self, input else - items = args.each_with_object([]) do |arg, arr| - arg.each_with_object(arr) do |(type, data), arr_| - arr_ << { type: type, data: data } - end + items = args.flat_map do |arg| + arg.map { |(type, data)| { type: type, data: data } } end driver.execute_script DROP_STRING, items, self end diff --git a/lib/capybara/selenium/node.rb b/lib/capybara/selenium/node.rb index 38428fb7d..6d27ff3c7 100644 --- a/lib/capybara/selenium/node.rb +++ b/lib/capybara/selenium/node.rb @@ -37,9 +37,7 @@ def value end def style(styles) - styles.each_with_object({}) do |style, result| - result[style] = native.css_value(style) - end + styles.to_h { |style| [style, native.css_value(style)] } end ## diff --git a/lib/capybara/spec/session/find_spec.rb b/lib/capybara/spec/session/find_spec.rb index 7131616f0..99fe6722d 100644 --- a/lib/capybara/spec/session/find_spec.rb +++ b/lib/capybara/spec/session/find_spec.rb @@ -528,7 +528,7 @@ expect(@session.find(:link, 'test-foo')[:id]).to eq 'foo' end end - + it 'should warn if passed count options' do allow(Capybara::Helpers).to receive(:warn) @session.find('//h1', count: 44)