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

More Ruby keyword arguments changes support

This commit is contained in:
Thomas Walpole 2020-07-04 14:05:56 -07:00
parent 6d18eea5ab
commit de542cc2d5
4 changed files with 28 additions and 17 deletions

View file

@ -209,13 +209,15 @@ module Capybara
# @!method wont_have_xpath # @!method wont_have_xpath
# See {Capybara::Node::Matchers#has_no_xpath?} # See {Capybara::Node::Matchers#has_no_xpath?}
%w[text content title current_path].each do |assertion| # This currently doesn't work for Ruby 2.8 due to Minitest not forwarding keyword args separately
infect_an_assertion "assert_#{assertion}", "must_have_#{assertion}", :reverse # %w[text content title current_path].each do |assertion|
infect_an_assertion "refute_#{assertion}", "wont_have_#{assertion}", :reverse # infect_an_assertion "assert_#{assertion}", "must_have_#{assertion}", :reverse
end # infect_an_assertion "refute_#{assertion}", "wont_have_#{assertion}", :reverse
# end
# rubocop:disable Style/MultilineBlockChain # rubocop:disable Style/MultilineBlockChain
(%w[selector xpath css link button field select table checked_field unchecked_field (%w[text content title current_path
selector xpath css link button field select table checked_field unchecked_field
ancestor sibling].flat_map do |assertion| ancestor sibling].flat_map do |assertion|
[%W[assert_#{assertion} must_have_#{assertion}], [%W[assert_#{assertion} must_have_#{assertion}],
%W[refute_#{assertion} wont_have_#{assertion}]] %W[refute_#{assertion} wont_have_#{assertion}]]
@ -228,14 +230,15 @@ module Capybara
%W[refute_matches_#{assertion} wont_match_#{assertion}]] %W[refute_matches_#{assertion} wont_match_#{assertion}]]
end).each do |(meth, new_name)| end).each do |(meth, new_name)|
class_eval <<-ASSERTION, __FILE__, __LINE__ + 1 class_eval <<-ASSERTION, __FILE__, __LINE__ + 1
def #{new_name} *args, &block def #{new_name} *args, **kw_args, &block
::Minitest::Expectation.new(self, ::Minitest::Spec.current).#{new_name}(*args, &block) ::Minitest::Expectation.new(self, ::Minitest::Spec.current).#{new_name}(*args, **kw_args, &block)
end end
ASSERTION ASSERTION
::Minitest::Expectation.class_eval <<-ASSERTION, __FILE__, __LINE__ + 1 ::Minitest::Expectation.class_eval <<-ASSERTION, __FILE__, __LINE__ + 1
def #{new_name} *args, &block def #{new_name} *args, **kw_args, &block
ctx.#{meth}(target, *args, &block) raise "Calling ##{new_name} outside of test." unless ctx
ctx.#{meth}(target, *args, **kw_args, &block)
end end
ASSERTION ASSERTION
end end
@ -243,9 +246,9 @@ module Capybara
## ##
# @deprecated # @deprecated
def must_have_style(*args, &block) def must_have_style(*args, **kw_args, &block)
warn 'must_have_style is deprecated, please use must_match_style' warn 'must_have_style is deprecated, please use must_match_style'
must_match_style(*args, &block) must_match_style(*args, **kw_args, &block)
end end
end end
end end

View file

@ -60,14 +60,15 @@ module Capybara
# @param styles [Hash] # @param styles [Hash]
# @return [Boolean] If the styles match # @return [Boolean] If the styles match
# #
def matches_style?(styles, **options) def matches_style?(styles = nil, **options)
styles, options = options, {} if styles.nil?
make_predicate(options) { assert_matches_style(styles, **options) } make_predicate(options) { assert_matches_style(styles, **options) }
end end
## ##
# @deprecated Use {#matches_style?} instead. # @deprecated Use {#matches_style?} instead.
# #
def has_style?(styles, **options) def has_style?(styles = nil, **options)
warn "DEPRECATED: has_style? is deprecated, please use matches_style? : #{Capybara::Helpers.filter_backtrace(caller)}" warn "DEPRECATED: has_style? is deprecated, please use matches_style? : #{Capybara::Helpers.filter_backtrace(caller)}"
matches_style?(styles, **options) matches_style?(styles, **options)
end end
@ -122,7 +123,8 @@ module Capybara
# @param styles [Hash] # @param styles [Hash]
# @raise [Capybara::ExpectationNotMet] If the element doesn't have the specified styles # @raise [Capybara::ExpectationNotMet] If the element doesn't have the specified styles
# #
def assert_matches_style(styles, **options) def assert_matches_style(styles = nil, **options)
styles, options = options, {} if styles.nil?
query_args, query_opts = _set_query_session_options(styles, options) query_args, query_opts = _set_query_session_options(styles, options)
query = Capybara::Queries::StyleQuery.new(*query_args, **query_opts) query = Capybara::Queries::StyleQuery.new(*query_args, **query_opts)
synchronize(query.wait) do synchronize(query.wait) do
@ -134,7 +136,7 @@ module Capybara
## ##
# @deprecated Use {#assert_matches_style} instead. # @deprecated Use {#assert_matches_style} instead.
# #
def assert_style(styles, **options) def assert_style(styles = nil, **options)
warn 'assert_style is deprecated, please use assert_matches_style instead' warn 'assert_style is deprecated, please use assert_matches_style instead'
assert_matches_style(styles, **options) assert_matches_style(styles, **options)
end end

View file

@ -146,14 +146,15 @@ module Capybara
# RSpec matcher for element style. # RSpec matcher for element style.
# #
# @see Capybara::Node::Matchers#matches_style? # @see Capybara::Node::Matchers#matches_style?
def match_style(styles, **options) def match_style(styles = nil, **options)
styles, options = options, {} if styles.nil?
Matchers::MatchStyle.new(styles, **options) Matchers::MatchStyle.new(styles, **options)
end end
## ##
# @deprecated # @deprecated
# #
def have_style(styles, **options) def have_style(styles = nil, **options)
warn "DEPRECATED: have_style is deprecated, please use match_style : #{Capybara::Helpers.filter_backtrace(caller)}" warn "DEPRECATED: have_style is deprecated, please use match_style : #{Capybara::Helpers.filter_backtrace(caller)}"
match_style(styles, **options) match_style(styles, **options)
end end

View file

@ -6,6 +6,11 @@ module Capybara
module RSpecMatchers module RSpecMatchers
module Matchers module Matchers
class MatchStyle < WrappedElementMatcher class MatchStyle < WrappedElementMatcher
def initialize(styles = nil, **kw_args, &filter_block)
styles, kw_args = kw_args, {} if styles.nil?
super(styles, **kw_args, &filter_block)
end
def element_matches?(el) def element_matches?(el)
el.assert_matches_style(*@args, **@kw_args) el.assert_matches_style(*@args, **@kw_args)
end end