1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/registration_container.rb
Koichi ITO 676a21e13a
Add uplelvel to deprecation warning of Capybara (#2379)
Follow https://github.com/teamcapybara/capybara/issues/2371.

This PR adds `uplelvel` to deprecation warning of Capybara.
The `uplevel` option has been introduced from Ruby 2.6.

> If the `uplevel` keyword argument is given, the string will be prepended
with information for the given caller frame in the same format used by
the `rb_warn` C function.

https://ruby-doc.org/core-2.6.0/Kernel.html#method-i-warn

On the other hand, Capybara supports Ruby 2.5.
Therefore, This PR adds `Capybara::Helpers.warn` internal method
for emulating `warn 'message', uplevel: 1` in Ruby 2.5 or lower.

This option will clarify where deprecation warnings should be fixed.

## Before

```console
DEPRECATED: Calling 'has_key?' on the drivers/servers container is
deprecated without replacement
```

## After

```console
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/turnip-4.1.0/
lib/turnip/capybara.rb:10: warning:
DEPRECATED: Calling 'has_key?' on the drivers/servers container is
deprecated without replacement
```
2020-09-06 12:21:53 -07:00

44 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Capybara
# @api private
class RegistrationContainer
def names
@registered.keys
end
def [](name)
@registered[name]
end
def []=(name, value)
Capybara::Helpers.warn 'DEPRECATED: Directly setting drivers/servers is deprecated, please use Capybara.register_driver/register_server instead'
@registered[name] = value
end
def method_missing(method_name, *args, **options, &block)
if @registered.respond_to?(method_name)
Capybara::Helpers.warn "DEPRECATED: Calling '#{method_name}' on the drivers/servers container is deprecated without replacement"
# RUBY 2.6 will send an empty hash rather than nothing with **options so fix that
return @registered.public_send(method_name, *args, &block) if options.empty?
return @registered.public_send(method_name, *args, **options, &block)
end
super
end
def respond_to_missing?(method_name, include_all)
@registered.respond_to?(method_name) || super
end
private
def initialize
@registered = {}
end
def register(name, block)
@registered[name] = block
end
end
end