blob: 03a7427d2634344ea6d97c1393c330224c04b6a8 [file] [log] [blame]
Kevin Clark3eca0782008-06-18 00:54:53 +00001require File.dirname(__FILE__) + '/spec_helper'
2
Kevin Clark96cc5162008-06-18 00:56:08 +00003shared_examples_for "deprecation" do
4 before(:all) do
5 # ensure deprecation is turned on
6 Thrift.send :remove_const, :DEPRECATION
7 Thrift.const_set :DEPRECATION, true
8 end
9
10 after(:all) do
11 # now turn it off again
12 # no other specs should want it
13 Thrift.send :remove_const, :DEPRECATION
14 Thrift.const_set :DEPRECATION, false
15 end
16end
17
Kevin Clark3eca0782008-06-18 00:54:53 +000018describe 'deprecate!' do
Kevin Clark96cc5162008-06-18 00:56:08 +000019 it_should_behave_like "deprecation"
20
Kevin Clark3eca0782008-06-18 00:54:53 +000021 def stub_stderr(callstr)
22 STDERR.should_receive(:puts).with("Warning: calling deprecated method #{callstr}")
23 end
24
25 it "should work for Module methods" do
26 mod = Module.new do
27 class << self
28 def new
29 "new"
30 end
31 deprecate! :old => :new
32 end
33 end
34 stub_stderr "#{mod.inspect}.old"
35 mod.old.should == "new"
36 end
37
38 it "should work with Modules that extend themselves" do
39 mod = Module.new do
40 extend self
41 def new
42 "new"
43 end
44 deprecate! :old => :new
45 end
46 stub_stderr "#{mod.inspect}.old"
47 mod.old.should == "new"
48 end
49
50 it "should work wtih Class methods" do
51 klass = Class.new do
52 class << self
53 def new
54 "new"
55 end
56 deprecate! :old => :new
57 end
58 end
59 stub_stderr "#{klass.inspect}.old"
60 klass.old.should == "new"
61 end
62
63 it "should work with Classes that include Modules" do
64 mod = Module.new do
65 def new
66 "new"
67 end
68 deprecate! :old => :new
69 end
70 klass = Class.new do
71 include mod
72 end
73 stub_stderr "#{klass.inspect}#old"
74 klass.new.old.should == "new"
75 end
76
77 it "should work with instance methods" do
78 klass = Class.new do
79 def new
80 "new"
81 end
82 deprecate! :old => :new
83 end
84 stub_stderr "#{klass.inspect}#old"
85 klass.new.old.should == "new"
86 end
87
88 it "should work with multiple method names" do
89 klass = Class.new do
90 def new1
91 "new 1"
92 end
93 def new2
94 "new 2"
95 end
96 deprecate! :old1 => :new1, :old2 => :new2
97 end
98 stub_stderr("#{klass.inspect}#old1").ordered
99 stub_stderr("#{klass.inspect}#old2").ordered
100 inst = klass.new
101 inst.old1.should == "new 1"
102 inst.old2.should == "new 2"
103 end
104
105 it "should only log a message once, even across multiple instances" do
106 klass = Class.new do
107 def new
108 "new"
109 end
110 deprecate! :old => :new
111 end
112 stub_stderr("#{klass.inspect}#old").once
113 klass.new.old.should == "new"
114 klass.new.old.should == "new"
115 end
116
117 it "should pass arguments" do
118 klass = Class.new do
119 def new(a, b)
120 "new: #{a + b}"
121 end
122 deprecate! :old => :new
123 end
124 stub_stderr("#{klass.inspect}#old")
125 klass.new.old(3, 5).should == "new: 8"
126 end
127
128 it "should pass blocks" do
129 klass = Class.new do
130 def new
131 "new #{yield}"
132 end
133 deprecate! :old => :new
134 end
135 stub_stderr("#{klass.inspect}#old")
136 klass.new.old { "block" }.should == "new block"
137 end
138
139 it "should not freeze the definition of the new method" do
140 klass = Class.new do
141 def new
142 "new"
143 end
144 deprecate! :old => :new
145 end
146 klass.send :define_method, :new do
147 "new 2"
148 end
149 stub_stderr("#{klass.inspect}#old")
150 klass.new.old.should == "new 2"
151 end
152end
153
154describe "deprecate_class!" do
Kevin Clark96cc5162008-06-18 00:56:08 +0000155 it_should_behave_like "deprecation"
156
Kevin Clark3eca0782008-06-18 00:54:53 +0000157 it "should create a new global constant that points to the old one" do
158 begin
159 klass = Class.new do
160 def foo
161 "foo"
162 end
163 end
164 deprecate_class! :DeprecationSpecOldClass => klass
165 DeprecationSpecOldClass.should eql(klass)
166 DeprecationSpecOldClass.new.foo.should == "foo"
167 ensure
Kevin Clark96cc5162008-06-18 00:56:08 +0000168 Object.send :remove_const, :DeprecationSpecOldClass if Object.const_defined? :DeprecationSpecOldClass
Kevin Clark3eca0782008-06-18 00:54:53 +0000169 end
170 end
171end