|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Faraday::DeprecatedClass do |
| 4 | + class SampleClass < StandardError |
| 5 | + attr_accessor :foo |
| 6 | + |
| 7 | + def initialize(foo = nil) |
| 8 | + @foo = foo || :foo |
| 9 | + end |
| 10 | + end |
| 11 | + |
| 12 | + SampleDeprecatedClass = Faraday::DeprecatedClass.proxy_class(SampleClass) |
| 13 | + |
| 14 | + it 'does not raise error for deprecated classes but prints an error message' do |
| 15 | + error_message, foobar = with_warn_squelching { SampleDeprecatedClass.new(:foo_bar) } |
| 16 | + expect(foobar).to be_a(SampleClass) |
| 17 | + expect(foobar.foo).to eq(:foo_bar) |
| 18 | + expect(error_message).to match( |
| 19 | + Regexp.new( |
| 20 | + 'NOTE: SampleDeprecatedClass.new is deprecated; '\ |
| 21 | + 'use SampleClass.new instead. It will be removed in or after version 1.0' |
| 22 | + ) |
| 23 | + ) |
| 24 | + end |
| 25 | + |
| 26 | + it 'does not raise an error for inherited error-namespaced classes but prints an error message' do |
| 27 | + error_message, = with_warn_squelching { Class.new(SampleDeprecatedClass) } |
| 28 | + |
| 29 | + expect(error_message).to match( |
| 30 | + Regexp.new( |
| 31 | + 'NOTE: Inheriting SampleDeprecatedClass is deprecated; '\ |
| 32 | + 'use SampleClass instead. It will be removed in or after version 1.0' |
| 33 | + ) |
| 34 | + ) |
| 35 | + end |
| 36 | + |
| 37 | + it 'allows backward-compatible class to be subclassed' do |
| 38 | + expect do |
| 39 | + with_warn_squelching { Class.new(SampleDeprecatedClass) } |
| 40 | + end.not_to raise_error |
| 41 | + end |
| 42 | + |
| 43 | + it 'allows rescuing of a current error with a deprecated error' do |
| 44 | + expect { raise SampleClass, nil }.to raise_error(SampleDeprecatedClass) |
| 45 | + end |
| 46 | + |
| 47 | + it 'allows rescuing of a current error with a current error' do |
| 48 | + expect { raise SampleClass, nil }.to raise_error(SampleClass) |
| 49 | + end |
| 50 | + |
| 51 | + it 'allows rescuing of a deprecated error with a deprecated error' do |
| 52 | + expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleDeprecatedClass) |
| 53 | + end |
| 54 | + |
| 55 | + it 'allows rescuing of a deprecated error with a current error' do |
| 56 | + expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleClass) |
| 57 | + end |
| 58 | + |
| 59 | + describe 'match behavior' do |
| 60 | + class SampleDeprecatedClassA < SampleDeprecatedClass; end |
| 61 | + class SampleDeprecatedClassB < SampleDeprecatedClass; end |
| 62 | + |
| 63 | + class SampleDeprecatedClassAX < SampleDeprecatedClassA; end |
| 64 | + |
| 65 | + class SampleClassA < SampleClass; end |
| 66 | + |
| 67 | + describe 'undeprecated class' do |
| 68 | + it 'is === to instance of deprecated class' do |
| 69 | + expect(SampleDeprecatedClass.new.is_a?(SampleClass)).to be true |
| 70 | + end |
| 71 | + |
| 72 | + it 'is === to instance of subclass of deprecated class' do |
| 73 | + expect(SampleDeprecatedClassA.new.is_a?(SampleClass)).to be true |
| 74 | + end |
| 75 | + |
| 76 | + it 'is === to instance of subclass of subclass of deprecated class' do |
| 77 | + expect(SampleDeprecatedClassAX.new.is_a?(SampleClass)).to be true |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + describe 'subclass of undeprecated class' do |
| 82 | + it 'is not === to instance of undeprecated class' do |
| 83 | + expect(SampleClass.new.is_a?(SampleClassA)).to be false |
| 84 | + end |
| 85 | + |
| 86 | + it 'is not === to instance of deprecated class' do |
| 87 | + expect(SampleDeprecatedClass.new.is_a?(SampleClassA)).to be false |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + describe 'deprecated class' do |
| 92 | + it 'is === to instance of undeprecated class' do |
| 93 | + expect(SampleDeprecatedClass.new.is_a?(SampleClass)).to be true |
| 94 | + end |
| 95 | + |
| 96 | + it 'is === to instance of subclass of undeprecated class' do |
| 97 | + expect(SampleClassA.superclass == SampleDeprecatedClass.superclass).to be true |
| 98 | + end |
| 99 | + |
| 100 | + it 'is === to instance of subclass of deprecated class' do |
| 101 | + expect(SampleDeprecatedClassA.new.is_a?(SampleDeprecatedClass)).to be true |
| 102 | + end |
| 103 | + |
| 104 | + it 'is === to instance of subclass of subclass of deprecated class' do |
| 105 | + expect(SampleDeprecatedClassAX.new.is_a?(SampleDeprecatedClass)).to be true |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + describe 'subclass of deprecated class' do |
| 110 | + it 'is not === to instance of subclass of undeprecated class' do |
| 111 | + expect(SampleClass.new.is_a?(SampleDeprecatedClassA)).to be false |
| 112 | + end |
| 113 | + |
| 114 | + it 'is not === to instance of another subclass of deprecated class' do |
| 115 | + expect(SampleDeprecatedClassB.new.is_a?(SampleDeprecatedClassA)).to be false |
| 116 | + end |
| 117 | + |
| 118 | + it 'is === to instance of its subclass' do |
| 119 | + expect(SampleDeprecatedClassAX.new.is_a?(SampleDeprecatedClassA)).to be true |
| 120 | + end |
| 121 | + |
| 122 | + it 'is === to instance of deprecated class' do |
| 123 | + expect(SampleDeprecatedClassB.new.is_a?(SampleDeprecatedClass)).to be true |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + describe 'subclass of subclass of deprecated class' do |
| 128 | + it 'is not === to instance of subclass of another subclass of deprecated class' do |
| 129 | + expect(SampleDeprecatedClassB.new.is_a?(SampleDeprecatedClassAX)).to be false |
| 130 | + end |
| 131 | + |
| 132 | + it 'is not === to instance of its superclass' do |
| 133 | + expect(SampleDeprecatedClass.new.is_a?(SampleDeprecatedClassA)).to be false |
| 134 | + end |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + def with_warn_squelching |
| 139 | + stderr_catcher = StringIO.new |
| 140 | + original_stderr = $stderr |
| 141 | + $stderr = stderr_catcher |
| 142 | + result = yield if block_given? |
| 143 | + [stderr_catcher.tap(&:rewind).string, result] |
| 144 | + ensure |
| 145 | + $stderr = original_stderr |
| 146 | + end |
| 147 | +end |
0 commit comments