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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
require File.dirname(__FILE__) + '/spec_helper'
shared_examples_for "deprecation" do
before(:all) do
# ensure deprecation is turned on
Thrift.send :remove_const, :DEPRECATION
Thrift.const_set :DEPRECATION, true
end
after(:all) do
# now turn it off again
# no other specs should want it
Thrift.send :remove_const, :DEPRECATION
Thrift.const_set :DEPRECATION, false
end
before(:each) do
Thrift::DeprecationProxy.reset_deprecation_warnings
end
def ensure_const_removed(name, &block)
begin
block.call
ensure
Object.send :remove_const, name if Object.const_defined? name
end
end
end
describe 'deprecate!' do
it_should_behave_like "deprecation"
def stub_stderr(callstr, offset=1)
STDERR.should_receive(:puts).with("Warning: calling deprecated method #{callstr}")
line = caller.first[/\d+$/].to_i + offset
STDERR.should_receive(:puts).with(" from #{__FILE__}:#{line}")
end
it "should work for Module methods" do
mod = Module.new do
class << self
def new
"new"
end
deprecate! :old => :new
end
end
stub_stderr "#{mod.inspect}.old"
mod.old.should == "new"
end
it "should work with Modules that extend themselves" do
mod = Module.new do
extend self
def new
"new"
end
deprecate! :old => :new
end
stub_stderr "#{mod.inspect}.old"
mod.old.should == "new"
end
it "should work wtih Class methods" do
klass = Class.new do
class << self
def new
"new"
end
deprecate! :old => :new
end
end
stub_stderr "#{klass.inspect}.old"
klass.old.should == "new"
end
it "should work with Classes that include Modules" do
mod = Module.new do
def new
"new"
end
deprecate! :old => :new
end
klass = Class.new do
include mod
end
stub_stderr "#{klass.inspect}#old"
klass.new.old.should == "new"
end
it "should work with instance methods" do
klass = Class.new do
def new
"new"
end
deprecate! :old => :new
end
stub_stderr "#{klass.inspect}#old"
klass.new.old.should == "new"
end
it "should work with multiple method names" do
klass = Class.new do
def new1
"new 1"
end
def new2
"new 2"
end
deprecate! :old1 => :new1, :old2 => :new2
end
stub_stderr("#{klass.inspect}#old1", 3).ordered
stub_stderr("#{klass.inspect}#old2", 3).ordered
inst = klass.new
inst.old1.should == "new 1"
inst.old2.should == "new 2"
end
it "should only log a message once, even across multiple instances" do
klass = Class.new do
def new
"new"
end
deprecate! :old => :new
end
stub_stderr("#{klass.inspect}#old").once
klass.new.old.should == "new"
klass.new.old.should == "new"
end
it "should pass arguments" do
klass = Class.new do
def new(a, b)
"new: #{a + b}"
end
deprecate! :old => :new
end
stub_stderr("#{klass.inspect}#old")
klass.new.old(3, 5).should == "new: 8"
end
it "should pass blocks" do
klass = Class.new do
def new
"new #{yield}"
end
deprecate! :old => :new
end
stub_stderr("#{klass.inspect}#old")
klass.new.old { "block" }.should == "new block"
end
it "should not freeze the definition of the new method" do
klass = Class.new do
def new
"new"
end
deprecate! :old => :new
end
klass.send :define_method, :new do
"new 2"
end
stub_stderr("#{klass.inspect}#old")
klass.new.old.should == "new 2"
end
it "should call the forwarded method in the same context as the original" do
klass = Class.new do
def myself
self
end
deprecate! :me => :myself
end
inst = klass.new
stub_stderr("#{klass.inspect}#me")
inst.me.should eql(inst.myself)
end
end
describe "deprecate_class!" do
it_should_behave_like "deprecation"
def stub_stderr_jruby(klass)
return unless defined? JRUBY_VERSION
stub_stderr(klass, nil, caller.first)
end
def stub_stderr_mri(klass, offset=1)
return if defined? JRUBY_VERSION
stub_stderr(klass, offset, caller.first)
end
def stub_stderr(klass, offset=1, called=nil)
STDERR.should_receive(:puts).with("Warning: class #{klass} is deprecated")
if offset
line = (called || caller.first)[/\d+$/].to_i + offset
STDERR.should_receive(:puts).with(" from #{__FILE__}:#{line}")
else
STDERR.should_receive(:puts).with(/^ from #{Regexp.escape(__FILE__)}:/)
end
end
it "should create a new global constant that points to the old one" do
ensure_const_removed :DeprecationSpecOldClass do
klass = Class.new do
def foo
"foo"
end
end
deprecate_class! :DeprecationSpecOldClass => klass
stub_stderr(:DeprecationSpecOldClass)
::DeprecationSpecOldClass.should eql(klass)
::DeprecationSpecOldClass.new.foo.should == "foo"
end
end
it "should create a global constant even from inside a module" do
ensure_const_removed :DeprecationSpecOldClass do
klass = nil #define scoping
Module.new do
klass = Class.new do
def foo
"foo"
end
end
deprecate_class! :DeprecationSpecOldClass => klass
end
stub_stderr(:DeprecationSpecOldClass)
::DeprecationSpecOldClass.should eql(klass)
::DeprecationSpecOldClass.new.foo.should == "foo"
end
end
it "should not prevent the deprecated class from being a superclass" do
ensure_const_removed :DeprecationSpecOldClass do
klass = Class.new do
def foo
"foo"
end
end
deprecate_class! :DeprecationSpecOldClass => klass
stub_stderr_jruby(:DeprecationSpecOldClass)
subklass = Class.new(::DeprecationSpecOldClass) do
def foo
"subclass #{super}"
end
end
stub_stderr_mri(:DeprecationSpecOldClass)
subklass.superclass.should eql(klass)
subklass.new.foo.should == "subclass foo"
end
end
it "should not bleed info between deprecations" do
ensure_const_removed :DeprecationSpecOldClass do
ensure_const_removed :DeprecationSpecOldClass2 do
klass = Class.new do
def foo
"foo"
end
end
deprecate_class! :DeprecationSpecOldClass => klass
klass2 = Class.new do
def bar
"bar"
end
end
deprecate_class! :DeprecationSpecOldClass2 => klass2
stub_stderr(:DeprecationSpecOldClass)
::DeprecationSpecOldClass.new.foo.should == "foo"
stub_stderr(:DeprecationSpecOldClass2)
::DeprecationSpecOldClass2.new.bar.should == "bar"
end
end
end
it "should work when Object.inherited calls a method on self" do
ensure_const_removed :DeprecationSpecOldClass do
old_inherited = Object.method(:inherited)
begin
(class << Object;self;end).class_eval do
define_method :inherited do |cls|
cls.inspect
old_inherited.call(cls)
end
end
klass = Class.new do
def foo
"foo"
end
end
STDERR.should_receive(:puts).exactly(0).times
lambda { deprecate_class! :DeprecationSpecOldClass => klass }.should_not raise_error
ensure
(class << Object;self;end).class_eval do
define_method :inherited, old_inherited
end
end
end
end
end
describe "deprecate_module!" do
it_should_behave_like "deprecation"
def stub_stderr_jruby(mod)
return unless defined? JRUBY_VERSION
stub_stderr(mod, nil, caller.first)
end
def stub_stderr_mri(mod, offset=1)
return if defined? JRUBY_VERSION
stub_stderr(mod, offset, caller.first)
end
def stub_stderr(mod, offset=1, called=nil)
STDERR.should_receive(:puts).with("Warning: module #{mod} is deprecated")
source = Regexp.escape(__FILE__) + ":"
if offset
line = (called || caller.first)[/\d+$/].to_i + offset
source += Regexp.escape(line.to_s)
else
source += "\d+"
end
STDERR.should_receive(:puts).with(/^ from #{source}(?::in `[^']+')?$/)
end
it "should create a new global constant that points to the old one" do
ensure_const_removed :DeprecationSpecOldModule do
mod = Module.new do
def self.foo
"foo"
end
end
deprecate_module! :DeprecationSpecOldModule => mod
stub_stderr(:DeprecationSpecOldModule)
::DeprecationSpecOldModule.should eql(mod)
::DeprecationSpecOldModule.foo.should == "foo"
end
end
it "should create a global constant even from inside a module" do
ensure_const_removed :DeprecationSpecOldModule do
mod = nil # scoping
Module.new do
mod = Module.new do
def self.foo
"foo"
end
end
deprecate_module! :DeprecationSpecOldModule => mod
end
stub_stderr(:DeprecationSpecOldModule)
::DeprecationSpecOldModule.should eql(mod)
::DeprecationSpecOldModule.foo.should == "foo"
end
end
it "should work for modules that extend themselves" do
ensure_const_removed :DeprecationSpecOldModule do
mod = Module.new do
extend self
def foo
"foo"
end
end
deprecate_module! :DeprecationSpecOldModule => mod
stub_stderr(:DeprecationSpecOldModule)
::DeprecationSpecOldModule.should eql(mod)
::DeprecationSpecOldModule.foo.should == "foo"
end
end
it "should work for modules included in other modules" do
ensure_const_removed :DeprecationSpecOldModule do
mod = Module.new do
def foo
"foo"
end
end
deprecate_module! :DeprecationSpecOldModule => mod
stub_stderr_jruby(:DeprecationSpecOldModule)
mod2 = Module.new do
class << self
include ::DeprecationSpecOldModule
end
end
stub_stderr_mri(:DeprecationSpecOldModule)
mod2.foo.should == "foo"
end
end
it "should work for modules included in classes" do
ensure_const_removed :DeprecationSpecOldModule do
mod = Module.new do
def foo
"foo"
end
end
deprecate_module! :DeprecationSpecOldModule => mod
stub_stderr_jruby(:DeprecationSpecOldModule)
klass = Class.new do
include ::DeprecationSpecOldModule
end
stub_stderr_mri(:DeprecationSpecOldModule)
klass.new.foo.should == "foo"
end
end
it "should not bleed info between deprecations" do
ensure_const_removed :DeprecationSpecOldModule do
ensure_const_removed :DeprecationSpecOldModule2 do
mod = Module.new do
def self.foo
"foo"
end
end
deprecate_module! :DeprecationSpecOldModule => mod
mod2 = Module.new do
def self.bar
"bar"
end
end
deprecate_module! :DeprecationSpecOldModule2 => mod2
stub_stderr(:DeprecationSpecOldModule)
::DeprecationSpecOldModule.foo.should == "foo"
stub_stderr(:DeprecationSpecOldModule2)
::DeprecationSpecOldModule2.bar.should == "bar"
end
end
end
it "should skip thrift library code when printing caller" do
klass = Class.new do
include ThriftStruct
FIELDS = {
1 => {:name => "foo", :type => Thrift::Types::STRING}
}
def struct_fields
FIELDS
end
end
stub_stderr('ThriftStruct')
klass.new(:foo => "foo")
end
end
|