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

Style updates

This commit is contained in:
Thomas Walpole 2020-09-05 12:24:43 -07:00
parent 239802d775
commit f59df7ed86
29 changed files with 68 additions and 56 deletions

1
.gitignore vendored
View file

@ -22,3 +22,4 @@ bin
gem-private_key.pem gem-private_key.pem
save_path_tmp save_path_tmp
vendor/bundle vendor/bundle
.byebug_history

View file

@ -3,6 +3,7 @@ require:
- rubocop-performance - rubocop-performance
AllCops: AllCops:
NewCops: enable
DisabledByDefault: false DisabledByDefault: false
TargetRubyVersion: 2.5 TargetRubyVersion: 2.5
Exclude: Exclude:

View file

@ -81,7 +81,7 @@ end
task :release do task :release do
version = Capybara::VERSION version = Capybara::VERSION
puts "Releasing #{version}, y/n?" puts "Releasing #{version}, y/n?"
exit(1) unless STDIN.gets.chomp == 'y' exit(1) unless $stdin.gets.chomp == 'y'
sh "git commit -am 'tagged #{version}' && " \ sh "git commit -am 'tagged #{version}' && " \
"git tag #{version} && " \ "git tag #{version} && " \
'gem build capybara.gemspec && ' \ 'gem build capybara.gemspec && ' \

View file

@ -81,7 +81,7 @@ module Capybara
@javascript_driver || :selenium @javascript_driver || :selenium
end end
def deprecate(method, alternate_method, once = false) def deprecate(method, alternate_method, once: false)
@deprecation_notified ||= {} @deprecation_notified ||= {}
unless once && @deprecation_notified[method] unless once && @deprecation_notified[method]
warn "DEPRECATED: ##{method} is deprecated, please use ##{alternate_method} instead: #{Capybara::Helpers.filter_backtrace(caller)}" warn "DEPRECATED: ##{method} is deprecated, please use ##{alternate_method} instead: #{Capybara::Helpers.filter_backtrace(caller)}"

View file

@ -100,7 +100,7 @@ module Capybara
# @param [Boolean] check_ancestors Whether to inherit visibility from ancestors # @param [Boolean] check_ancestors Whether to inherit visibility from ancestors
# @return [Boolean] Whether the element is visible # @return [Boolean] Whether the element is visible
# #
def visible?(check_ancestors = true) def visible?(check_ancestors = true) # rubocop:disable Style/OptionalBooleanParameter
return false if (tag_name == 'input') && (native[:type] == 'hidden') return false if (tag_name == 'input') && (native[:type] == 'hidden')
return false if tag_name == 'template' return false if tag_name == 'template'

View file

@ -16,7 +16,7 @@ module Capybara
end end
end end
def description(applied = false) def description(applied = false) # rubocop:disable Style/OptionalBooleanParameter
child_query = @child_node&.instance_variable_get(:@query) child_query = @child_node&.instance_variable_get(:@query)
desc = super desc = super
desc += " that is an ancestor of #{child_query.description}" if child_query desc += " that is an ancestor of #{child_query.description}" if child_query

View file

@ -55,7 +55,7 @@ module Capybara
def name; selector.name; end def name; selector.name; end
def label; selector.label || selector.name; end def label; selector.label || selector.name; end
def description(only_applied = false) def description(only_applied = false) # rubocop:disable Style/OptionalBooleanParameter
desc = +'' desc = +''
show_for = show_for_stage(only_applied) show_for = show_for_stage(only_applied)
@ -95,11 +95,9 @@ module Capybara
desc << ' that also matches the custom filter block' if @filter_block && show_for[:node] desc << ' that also matches the custom filter block' if @filter_block && show_for[:node]
desc << " within #{@resolved_node.inspect}" if describe_within? desc << " within #{@resolved_node.inspect}" if describe_within?
if locator.is_a?(String) && locator.start_with?('#', './/', '//') if locator.is_a?(String) && locator.start_with?('#', './/', '//') && !selector.raw_locator?
unless selector.raw_locator? desc << "\nNote: It appears you may be passing a CSS selector or XPath expression rather than a locator. " \
desc << "\nNote: It appears you may be passing a CSS selector or XPath expression rather than a locator. " \ "Please see the documentation for acceptable locator values.\n\n"
"Please see the documentation for acceptable locator values.\n\n"
end
end end
desc desc
end end
@ -653,7 +651,7 @@ module Capybara
d = u.dot w d = u.dot w
e = v.dot w e = v.dot w
cap_d = (a * c) - (b * b) cap_d = (a * c) - (b**2)
sD = tD = cap_d sD = tD = cap_d
# compute the line parameters of the two closest points # compute the line parameters of the two closest points

View file

@ -15,7 +15,7 @@ module Capybara
end end
end end
def description(applied = false) def description(applied = false) # rubocop:disable Style/OptionalBooleanParameter
desc = super desc = super
sibling_query = @sibling_node&.instance_variable_get(:@query) sibling_query = @sibling_node&.instance_variable_get(:@query)
desc += " that is a sibling of #{sibling_query.description}" if sibling_query desc += " that is a sibling of #{sibling_query.description}" if sibling_query

View file

@ -17,6 +17,7 @@ class Capybara::RackTest::Driver < Capybara::Driver::Base
def initialize(app, **options) def initialize(app, **options)
raise ArgumentError, 'rack-test requires a rack application, but none was given' unless app raise ArgumentError, 'rack-test requires a rack application, but none was given' unless app
super()
@app = app @app = app
@options = DEFAULT_OPTIONS.merge(options) @options = DEFAULT_OPTIONS.merge(options)
end end

View file

@ -6,7 +6,7 @@ class Capybara::RackTest::Form < Capybara::RackTest::Node
# That check should be based solely on the form element's 'enctype' attribute value, # That check should be based solely on the form element's 'enctype' attribute value,
# which should probably be provided to Rack::Test in its non-GET request methods. # which should probably be provided to Rack::Test in its non-GET request methods.
class NilUploadedFile < Rack::Test::UploadedFile class NilUploadedFile < Rack::Test::UploadedFile
def initialize def initialize # rubocop:disable Lint/MissingSuper
@empty_file = Tempfile.new('nil_uploaded_file') @empty_file = Tempfile.new('nil_uploaded_file')
@empty_file.close @empty_file.close
end end

View file

@ -27,7 +27,7 @@ module Capybara
super super
end end
def respond_to_missing?(method_name, include_private = false) def respond_to_missing?(method_name, include_all)
@registered.respond_to?(method_name) || super @registered.respond_to?(method_name) || super
end end

View file

@ -4,12 +4,14 @@ module Puma
module MiniSSL module MiniSSL
class Socket class Socket
def read_nonblock(size, *_) def read_nonblock(size, *_)
wait_states = %i[wait_readable wait_writable]
loop do loop do
output = engine_read_all output = engine_read_all
return output if output return output if output
data = @socket.read_nonblock(size, exception: false) data = @socket.read_nonblock(size, exception: false)
raise IO::EAGAINWaitReadable if %i[wait_readable wait_writable].include? data raise IO::EAGAINWaitReadable if wait_states.include? data
return nil if data.nil? return nil if data.nil?
@engine.inject(data) @engine.inject(data)

View file

@ -91,13 +91,9 @@ module Capybara
return load_up_to(count + 1) <=> count return load_up_to(count + 1) <=> count
end end
if min && (min = Integer(min)) return -1 if min && (min = Integer(min)) && (load_up_to(min) < min)
return -1 if load_up_to(min) < min
end
if max && (max = Integer(max)) return 1 if max && (max = Integer(max)) && (load_up_to(max + 1) > max)
return 1 if load_up_to(max + 1) > max
end
if between if between
min, max = (between.begin && between.min) || 1, between.end min, max = (between.begin && between.min) || 1, between.end

View file

@ -259,7 +259,8 @@ module Capybara
end end
def parameter_names(block) def parameter_names(block)
block.parameters.select { |(type, _name)| %i[key keyreq].include? type }.map { |(_type, name)| name } key_types = %i[key keyreq]
block.parameters.select { |(type, _name)| key_types.include? type }.map { |(_type, name)| name }
end end
def expression(type, allowed_filters, &block) def expression(type, allowed_filters, &block)

View file

@ -18,7 +18,8 @@ Capybara.add_selector(:element, locator_type: [String, Symbol]) do
end end
describe_expression_filters do |**options| describe_expression_filters do |**options|
booleans, values = options.partition { |_k, v| [true, false].include? v }.map(&:to_h) boolean_values = [true, false]
booleans, values = options.partition { |_k, v| boolean_values.include? v }.map(&:to_h)
desc = describe_all_expression_filters(**values) desc = describe_all_expression_filters(**values)
desc + booleans.map do |k, v| desc + booleans.map do |k, v|
v ? " with #{k} attribute" : "without #{k} attribute" v ? " with #{k} attribute" : "without #{k} attribute"

View file

@ -75,6 +75,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
end end
def initialize(app, **options) def initialize(app, **options)
super()
self.class.load_selenium self.class.load_selenium
@app = app @app = app
@browser = nil @browser = nil

View file

@ -464,8 +464,8 @@ private
end end
end end
def each_key(keys) def each_key(keys, &block)
normalize_keys(keys).each { |key| yield(key) } normalize_keys(keys).each(&block)
end end
def find_context def find_context

View file

@ -110,7 +110,7 @@ private
end end
end end
def browser_version(to_float = true) def browser_version(to_float: true)
caps = capabilities caps = capabilities
ver = (caps[:browser_version] || caps[:version]) ver = (caps[:browser_version] || caps[:version])
ver = ver.to_f if to_float ver = ver.to_f if to_float

View file

@ -40,7 +40,7 @@ module Capybara
end end
def insert_disable(html) def insert_disable(html)
html.sub(%r{(</head>)}, disable_markup + '\\1') html.sub(%r{(</head>)}, "#{disable_markup}\\1")
end end
DISABLE_MARKUP_TEMPLATE = <<~HTML DISABLE_MARKUP_TEMPLATE = <<~HTML

View file

@ -355,8 +355,8 @@ module Capybara
# #
# @param [String] locator Id or legend of the fieldset # @param [String] locator Id or legend of the fieldset
# #
def within_fieldset(locator) def within_fieldset(locator, &block)
within(:fieldset, locator) { yield } within(:fieldset, locator, &block)
end end
## ##
@ -365,8 +365,8 @@ module Capybara
# #
# @param [String] locator Id or caption of the table # @param [String] locator Id or caption of the table
# #
def within_table(locator) def within_table(locator, &block)
within(:table, locator) { yield } within(:table, locator, &block)
end end
## ##
@ -398,8 +398,9 @@ module Capybara
driver.switch_to_frame(:parent) driver.switch_to_frame(:parent)
when :top when :top
idx = scopes.index(:frame) idx = scopes.index(:frame)
top_level_scopes = [:frame, nil]
if idx if idx
if scopes.slice(idx..-1).any? { |scope| ![:frame, nil].include?(scope) } if scopes.slice(idx..-1).any? { |scope| !top_level_scopes.include?(scope) }
raise Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's "\ raise Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's "\
'`within` block.' '`within` block.'
end end
@ -788,7 +789,7 @@ module Capybara
# #
# Yield a block using a specific maximum wait time. # Yield a block using a specific maximum wait time.
# #
def using_wait_time(seconds) def using_wait_time(seconds, &block)
if Capybara.threadsafe if Capybara.threadsafe
begin begin
previous_wait_time = config.default_max_wait_time previous_wait_time = config.default_max_wait_time
@ -798,7 +799,7 @@ module Capybara
config.default_max_wait_time = previous_wait_time config.default_max_wait_time = previous_wait_time
end end
else else
Capybara.using_wait_time(seconds) { yield } Capybara.using_wait_time(seconds, &block)
end end
end end

View file

@ -60,7 +60,7 @@ Capybara::SpecHelper.spec '#find' do
end end
end end
context 'with frozen time', requires: [:js] do context 'with frozen time', requires: [:js] do # rubocop:disable RSpec/EmptyExampleGroup
if defined?(Process::CLOCK_MONOTONIC) if defined?(Process::CLOCK_MONOTONIC)
it 'will time out even if time is frozen' do it 'will time out even if time is frozen' do
@session.visit('/with_js') @session.visit('/with_js')

View file

@ -388,8 +388,8 @@ Capybara::SpecHelper.spec 'node' do
describe '#==' do describe '#==' do
it 'preserve object identity' do it 'preserve object identity' do
expect(@session.find('//h1') == @session.find('//h1')).to be true # rubocop:disable Lint/UselessComparison expect(@session.find('//h1') == @session.find('//h1')).to be true # rubocop:disable Lint/BinaryOperatorWithIdenticalOperands
expect(@session.find('//h1') === @session.find('//h1')).to be true # rubocop:disable Style/CaseEquality, Lint/UselessComparison expect(@session.find('//h1') === @session.find('//h1')).to be true # rubocop:disable Style/CaseEquality, Lint/BinaryOperatorWithIdenticalOperands
expect(@session.find('//h1').eql?(@session.find('//h1'))).to be false expect(@session.find('//h1').eql?(@session.find('//h1'))).to be false
end end

View file

@ -31,7 +31,7 @@ Capybara::SpecHelper.spec '#save_page' do
it 'can store files in a specified directory' do it 'can store files in a specified directory' do
Capybara.save_path = alternative_path Capybara.save_path = alternative_path
@session.save_page @session.save_page
path = Dir.glob(alternative_path + '/capybara-*.html').first path = Dir.glob("#{alternative_path}/capybara-*.html").first
expect(File.read(path)).to include('Another World') expect(File.read(path)).to include('Another World')
end end
@ -43,14 +43,14 @@ Capybara::SpecHelper.spec '#save_page' do
it 'can store files in a specified directory with a given filename' do it 'can store files in a specified directory with a given filename' do
Capybara.save_path = alternative_path Capybara.save_path = alternative_path
@session.save_page('capybara-001133.html') @session.save_page('capybara-001133.html')
path = alternative_path + '/capybara-001133.html' path = "#{alternative_path}/capybara-001133.html"
expect(File.read(path)).to include('Another World') expect(File.read(path)).to include('Another World')
end end
it 'can store files in a specified directory with a given relative filename' do it 'can store files in a specified directory with a given relative filename' do
Capybara.save_path = alternative_path Capybara.save_path = alternative_path
@session.save_page('tmp/capybara-001144.html') @session.save_page('tmp/capybara-001144.html')
path = alternative_path + '/tmp/capybara-001144.html' path = "#{alternative_path}/tmp/capybara-001144.html"
expect(File.read(path)).to include('Another World') expect(File.read(path)).to include('Another World')
end end
@ -63,7 +63,7 @@ Capybara::SpecHelper.spec '#save_page' do
it 'returns an absolute path in given directory' do it 'returns an absolute path in given directory' do
Capybara.save_path = alternative_path Capybara.save_path = alternative_path
result = @session.save_page result = @session.save_page
path = File.expand_path(Dir.glob(alternative_path + '/capybara-*.html').first, alternative_path) path = File.expand_path(Dir.glob("#{alternative_path}/capybara-*.html").first, alternative_path)
expect(result).to eq(path) expect(result).to eq(path)
end end

View file

@ -58,7 +58,7 @@ module Capybara
def run_specs(session, name, **options, &filter_block) def run_specs(session, name, **options, &filter_block)
specs = @specs specs = @specs
RSpec.describe Capybara::Session, name, options do # rubocop:disable RSpec/EmptyExampleGroup RSpec.describe Capybara::Session, name, options do
include Capybara::SpecHelper include Capybara::SpecHelper
include Capybara::RSpecMatchers include Capybara::RSpecMatchers
@ -72,11 +72,11 @@ module Capybara
end end
before :each, psc: true do before :each, psc: true do
SpecHelper.reset_threadsafe(true, session) SpecHelper.reset_threadsafe(bool: true, session: session)
end end
after psc: true do after psc: true do
SpecHelper.reset_threadsafe(false, session) SpecHelper.reset_threadsafe(session: session)
end end
before :each, :exact_false do before :each, :exact_false do
@ -91,7 +91,7 @@ module Capybara
end end
end end
def reset_threadsafe(bool = false, session = nil) def reset_threadsafe(bool: false, session: nil)
# Work around limit on when threadsafe can be changed # Work around limit on when threadsafe can be changed
Capybara::Session.class_variable_set(:@@instance_created, false) # rubocop:disable Style/ClassVars Capybara::Session.class_variable_set(:@@instance_created, false) # rubocop:disable Style/ClassVars
Capybara.threadsafe = bool Capybara.threadsafe = bool
@ -109,11 +109,9 @@ module Capybara
stream.reopen(old_stream) stream.reopen(old_stream)
end end
def quietly def quietly(&block)
silence_stream(STDOUT) do silence_stream($stdout) do
silence_stream(STDERR) do silence_stream($stderr, &block)
yield
end
end end
end end
@ -133,4 +131,4 @@ module Capybara
end end
end end
Dir[File.dirname(__FILE__) + '/session/**/*.rb'].each { |file| require_relative file } Dir["#{File.dirname(__FILE__)}/session/**/*.rb"].each { |file| require_relative file }

View file

@ -9,6 +9,7 @@ class TestApp < Sinatra::Base
class TestAppError < Exception; end # rubocop:disable Lint/InheritException class TestAppError < Exception; end # rubocop:disable Lint/InheritException
class TestAppOtherError < Exception # rubocop:disable Lint/InheritException class TestAppOtherError < Exception # rubocop:disable Lint/InheritException
def initialize(string1, msg) def initialize(string1, msg)
super()
@something = string1 @something = string1
@message = msg @message = msg
end end
@ -89,11 +90,11 @@ class TestApp < Sinatra::Base
end end
get '/form/get' do get '/form/get' do
'<pre id="results">' + params[:form].to_yaml + '</pre>' %(<pre id="results">#{params[:form].to_yaml}</pre>)
end end
post '/relative' do post '/relative' do
'<pre id="results">' + params[:form].to_yaml + '</pre>' %(<pre id="results">#{params[:form].to_yaml}</pre>)
end end
get '/favicon.ico' do get '/favicon.ico' do
@ -180,7 +181,7 @@ class TestApp < Sinatra::Base
post '/form' do post '/form' do
self.class.form_post_count += 1 self.class.form_post_count += 1
'<pre id="results">' + params[:form].merge('post_count' => self.class.form_post_count).to_yaml + '</pre>' %(<pre id="results">#{params[:form].merge('post_count' => self.class.form_post_count).to_yaml}</pre>)
end end
post '/upload_empty' do post '/upload_empty' do

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
# rubocop:disable RSpec/MultipleDescribes
require 'spec_helper' require 'spec_helper'
require 'capybara/rspec' require 'capybara/rspec'
@ -96,4 +98,4 @@ ffeature 'if ffeature aliases focused tag then' do # rubocop:disable RSpec/Focus
expect(example.metadata[:focus]).to eq true expect(example.metadata[:focus]).to eq true
end end
end end
# rubocop:enable RSpec/RepeatedExample # rubocop:enable RSpec/RepeatedExample, RSpec/MultipleDescribes

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
# rubocop:disable RSpec/MultipleDescribes
require 'spec_helper' require 'spec_helper'
require 'capybara/rspec' require 'capybara/rspec'
@ -17,3 +19,5 @@ feature 'if xscenario aliases to pending then' do
xscenario "this test should be 'temporarily disabled with xscenario'" do xscenario "this test should be 'temporarily disabled with xscenario'" do
end end
end end
# rubocop:enable RSpec/MultipleDescribes

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
# rubocop:disable RSpec/MultipleDescribes
require 'spec_helper' require 'spec_helper'
RSpec.describe 'capybara/rspec' do RSpec.describe 'capybara/rspec' do
@ -143,3 +145,5 @@ feature 'Feature DSL' do
expect(page.body).to include('Another World') expect(page.body).to include('Another World')
end end
end end
# rubocop:enable RSpec/MultipleDescribes

View file

@ -167,7 +167,7 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
long_string = (0...60).map { |i| ((i % 26) + 65).chr }.join long_string = (0...60).map { |i| ((i % 26) + 65).chr }.join
session.visit('/form') session.visit('/form')
session.fill_in('form_first_name', with: long_string, fill_options: { clear: :none }) session.fill_in('form_first_name', with: long_string, fill_options: { clear: :none })
expect(session.find(:fillable_field, 'form_first_name').value).to eq('John' + long_string) expect(session.find(:fillable_field, 'form_first_name').value).to eq("John#{long_string}")
end end
end end