-
Notifications
You must be signed in to change notification settings - Fork 966
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow deprecated exception classes to rescue the current exception class
- Loading branch information
1 parent
f2e8694
commit ddab77c
Showing
3 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Faraday::DeprecatedClass do | ||
class SampleClass < StandardError | ||
attr_accessor :foo | ||
|
||
def initialize(foo = nil) | ||
@foo = foo || :foo | ||
end | ||
end | ||
|
||
SampleDeprecatedClass = Faraday::DeprecatedClass.proxy_class(SampleClass) | ||
|
||
it 'does not raise error for deprecated classes but prints an error message' do | ||
error_message, foobar = with_warn_squelching { SampleDeprecatedClass.new(:foo_bar) } | ||
expect(foobar).to be_a(SampleClass) | ||
expect(foobar.foo).to eq(:foo_bar) | ||
expect(error_message).to match( | ||
Regexp.new( | ||
'NOTE: SampleDeprecatedClass.new is deprecated; '\ | ||
'use SampleClass.new instead. It will be removed in or after version 1.0' | ||
) | ||
) | ||
end | ||
|
||
it 'does not raise an error for inherited error-namespaced classes but prints an error message' do | ||
error_message, = with_warn_squelching { Class.new(SampleDeprecatedClass) } | ||
|
||
expect(error_message).to match( | ||
Regexp.new( | ||
'NOTE: Inheriting SampleDeprecatedClass is deprecated; '\ | ||
'use SampleClass instead. It will be removed in or after version 1.0' | ||
) | ||
) | ||
end | ||
|
||
it 'allows backward-compatible class to be subclassed' do | ||
expect { | ||
with_warn_squelching { Class.new(SampleDeprecatedClass) } | ||
}.not_to raise_error | ||
end | ||
|
||
it 'allows rescuing of a current error with a deprecated error' do | ||
begin | ||
raise SampleClass, nil | ||
rescue SampleDeprecatedClass | ||
rescue StandardError => exc | ||
fail "rescued #{exc.class.name} instead" | ||
end | ||
end | ||
|
||
it 'allows rescuing of a current error with a current error' do | ||
begin | ||
raise SampleClass, nil | ||
rescue SampleClass | ||
rescue StandardError => exc | ||
fail "rescued #{exc.class.name} instead" | ||
end | ||
end | ||
|
||
it 'allows rescuing of a deprecated error with a deprecated error' do | ||
begin | ||
raise SampleDeprecatedClass, nil | ||
rescue SampleDeprecatedClass | ||
rescue StandardError => exc | ||
fail "rescued #{exc.class.name} instead" | ||
end | ||
end | ||
|
||
it 'allows rescuing of a deprecated error with a current error' do | ||
begin | ||
raise SampleDeprecatedClass, nil | ||
rescue SampleClass | ||
rescue StandardError => exc | ||
fail "rescued #{exc.class.name} instead" | ||
end | ||
end | ||
|
||
|
||
def with_warn_squelching | ||
stderr_catcher = StringIO.new | ||
original_stderr = $stderr | ||
$stderr = stderr_catcher | ||
result = yield if block_given? | ||
[stderr_catcher.tap(&:rewind).string, result] | ||
ensure | ||
$stderr = original_stderr | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters