-
Notifications
You must be signed in to change notification settings - Fork 1
/
deprecations_spec.rb
233 lines (200 loc) · 5.31 KB
/
deprecations_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# frozen_string_literal: true
require 'spec_helper'
module Examples
module FooMod
def self.bar(*args, **kw_args, &block)
{ __method__ => { args: args, kw_args: kw_args, block: block } }
end
def self.alt_bar
nil
end
def self.baz(arg)
yield(arg) if arg
end
deprecated :bar, :alt_bar, 'next version'
deprecated :baz
end
class Foo < BasicObject
def self.bar(*args, **kw_args, &block)
{ __method__ => { args: args, kw_args: kw_args, block: block } }
end
def self.alt_bar
nil
end
def self.baz(arg)
yield(arg) if arg
end
def foo(*args, **kw_args, &block)
{ foo: { args: args, kw_args: kw_args, block: block } }
end
def alt_foo
nil
end
def foo_baz(arg)
yield(arg) if arg
end
deprecated :bar, :alt_bar, 'next version'
deprecated :baz
deprecated :foo, :alt_foo, 'next version'
deprecated :foo_baz
end
class FooChild < Foo
end
class Bar < BasicObject
deprecated!
attr_reader :args
def initialize(*args, **kw_args, &block)
@args = { initialize: { args: args, kw_args: kw_args, block: block } }
end
end
end
RSpec.describe Deprecations do
let(:block) { proc { 666 } }
before { Deprecations.behavior = :silence }
context 'parameter forwarding' do
it 'forwards parameters of class methods of modules' do
result = Examples::FooMod.bar(:arg1, 42, half: 21, &block)
expect(result).to eq(
bar: {
args: [:arg1, 42],
kw_args: {
half: 21
},
block: block
}
)
end
it 'forwards parameters of class methods' do
result = Examples::Foo.bar(:arg1, 42, half: 21, &block)
expect(result).to eq(
bar: {
args: [:arg1, 42],
kw_args: {
half: 21
},
block: block
}
)
end
it 'forwards parameters of instance methods' do
result = Examples::Foo.new.foo(:arg1, 42, half: 21, &block)
expect(result).to eq(
foo: {
args: [:arg1, 42],
kw_args: {
half: 21
},
block: block
}
)
end
it 'forwards parameters of initialize' do
result = Examples::Bar.new(:arg1, 42, half: 21, &block)
expect(result.args).to eq(
initialize: {
args: [:arg1, 42],
kw_args: {
half: 21
},
block: block
}
)
end
end
context 'reporting' do
it 'reports when class methods of modules are called' do
expect(Deprecations).to receive(:call).with(
'Examples::FooMod.bar',
'Examples::FooMod.alt_bar',
'next version'
)
expect(Deprecations).to receive(:call).with(
'Examples::FooMod.baz',
nil,
nil
)
Examples::FooMod.bar
Examples::FooMod.baz(false)
end
it 'reports when class methods are called' do
expect(Deprecations).to receive(:call).with(
'Examples::Foo.bar',
'Examples::Foo.alt_bar',
'next version'
)
expect(Deprecations).to receive(:call).with('Examples::Foo.baz', nil, nil)
Examples::Foo.bar
Examples::Foo.baz(false)
end
it 'reports when instance methods are called' do
expect(Deprecations).to receive(:call).with(
'Examples::Foo#foo',
'Examples::Foo#alt_foo',
'next version'
)
expect(Deprecations).to receive(:call).with(
'Examples::Foo#foo_baz',
nil,
nil
)
Examples::Foo.new.foo
Examples::Foo.new.foo_baz(false)
end
it 'reports when class methods of a child class are called' do
expect(Deprecations).to receive(:call).with(
'Examples::FooChild.bar',
'Examples::FooChild.alt_bar',
'next version'
)
expect(Deprecations).to receive(:call).with(
'Examples::FooChild.baz',
nil,
nil
)
Examples::FooChild.bar
Examples::FooChild.baz(false)
end
it 'reports when instance methods of a child class are called' do
expect(Deprecations).to receive(:call).with(
'Examples::FooChild#foo',
'Examples::FooChild#alt_foo',
'next version'
)
expect(Deprecations).to receive(:call).with(
'Examples::FooChild#foo_baz',
nil,
nil
)
Examples::FooChild.new.foo
Examples::FooChild.new.foo_baz(false)
end
it 'reports when initialize is called' do
expect(Deprecations).to receive(:call).with('Examples::Bar', nil, nil)
Examples::Bar.new
end
end
context 'error handling' do
it 'raises a NameError for invalid deprecated method name' do
expect { Class.new { deprecated :some } }.to raise_error(
NameError,
/some/
)
end
it 'raises a NameError for invalid alternative method name' do
expect do
Class.new do
def foo
nil
end
deprecated :foo, :some
end
end.to raise_error(NameError, /some/)
end
it 'does not deprecate already deprecated class method' do
expect(Examples::FooMod.__send__(:deprecated, :bar)).to be_nil
end
it 'does not deprecate already deprecated method' do
expect(Examples::Foo.__send__(:deprecated, :foo)).to be_nil
end
end
end