Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | |
Kevin Clark | 96cc516 | 2008-06-18 00:56:08 +0000 | [diff] [blame] | 3 | shared_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 |
| 16 | end |
| 17 | |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 18 | describe 'deprecate!' do |
Kevin Clark | 96cc516 | 2008-06-18 00:56:08 +0000 | [diff] [blame] | 19 | it_should_behave_like "deprecation" |
| 20 | |
Kevin Clark | fc964ee | 2008-06-18 00:57:46 +0000 | [diff] [blame] | 21 | def stub_stderr(callstr, offset=1) |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 22 | STDERR.should_receive(:puts).with("Warning: calling deprecated method #{callstr}") |
Kevin Clark | fc964ee | 2008-06-18 00:57:46 +0000 | [diff] [blame] | 23 | line = caller.first[/\d+$/].to_i + offset |
| 24 | STDERR.should_receive(:puts).with(" from #{__FILE__}:#{line}") |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 25 | end |
| 26 | |
| 27 | it "should work for Module methods" do |
| 28 | mod = Module.new do |
| 29 | class << self |
| 30 | def new |
| 31 | "new" |
| 32 | end |
| 33 | deprecate! :old => :new |
| 34 | end |
| 35 | end |
| 36 | stub_stderr "#{mod.inspect}.old" |
| 37 | mod.old.should == "new" |
| 38 | end |
| 39 | |
| 40 | it "should work with Modules that extend themselves" do |
| 41 | mod = Module.new do |
| 42 | extend self |
| 43 | def new |
| 44 | "new" |
| 45 | end |
| 46 | deprecate! :old => :new |
| 47 | end |
| 48 | stub_stderr "#{mod.inspect}.old" |
| 49 | mod.old.should == "new" |
| 50 | end |
| 51 | |
| 52 | it "should work wtih Class methods" do |
| 53 | klass = Class.new do |
| 54 | class << self |
| 55 | def new |
| 56 | "new" |
| 57 | end |
| 58 | deprecate! :old => :new |
| 59 | end |
| 60 | end |
| 61 | stub_stderr "#{klass.inspect}.old" |
| 62 | klass.old.should == "new" |
| 63 | end |
| 64 | |
| 65 | it "should work with Classes that include Modules" do |
| 66 | mod = Module.new do |
| 67 | def new |
| 68 | "new" |
| 69 | end |
| 70 | deprecate! :old => :new |
| 71 | end |
| 72 | klass = Class.new do |
| 73 | include mod |
| 74 | end |
| 75 | stub_stderr "#{klass.inspect}#old" |
| 76 | klass.new.old.should == "new" |
| 77 | end |
| 78 | |
| 79 | it "should work with instance methods" do |
| 80 | klass = Class.new do |
| 81 | def new |
| 82 | "new" |
| 83 | end |
| 84 | deprecate! :old => :new |
| 85 | end |
| 86 | stub_stderr "#{klass.inspect}#old" |
| 87 | klass.new.old.should == "new" |
| 88 | end |
| 89 | |
| 90 | it "should work with multiple method names" do |
| 91 | klass = Class.new do |
| 92 | def new1 |
| 93 | "new 1" |
| 94 | end |
| 95 | def new2 |
| 96 | "new 2" |
| 97 | end |
| 98 | deprecate! :old1 => :new1, :old2 => :new2 |
| 99 | end |
Kevin Clark | fc964ee | 2008-06-18 00:57:46 +0000 | [diff] [blame] | 100 | stub_stderr("#{klass.inspect}#old1", 3).ordered |
| 101 | stub_stderr("#{klass.inspect}#old2", 3).ordered |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 102 | inst = klass.new |
| 103 | inst.old1.should == "new 1" |
| 104 | inst.old2.should == "new 2" |
| 105 | end |
| 106 | |
| 107 | it "should only log a message once, even across multiple instances" do |
| 108 | klass = Class.new do |
| 109 | def new |
| 110 | "new" |
| 111 | end |
| 112 | deprecate! :old => :new |
| 113 | end |
| 114 | stub_stderr("#{klass.inspect}#old").once |
| 115 | klass.new.old.should == "new" |
| 116 | klass.new.old.should == "new" |
| 117 | end |
| 118 | |
| 119 | it "should pass arguments" do |
| 120 | klass = Class.new do |
| 121 | def new(a, b) |
| 122 | "new: #{a + b}" |
| 123 | end |
| 124 | deprecate! :old => :new |
| 125 | end |
| 126 | stub_stderr("#{klass.inspect}#old") |
| 127 | klass.new.old(3, 5).should == "new: 8" |
| 128 | end |
| 129 | |
| 130 | it "should pass blocks" do |
| 131 | klass = Class.new do |
| 132 | def new |
| 133 | "new #{yield}" |
| 134 | end |
| 135 | deprecate! :old => :new |
| 136 | end |
| 137 | stub_stderr("#{klass.inspect}#old") |
| 138 | klass.new.old { "block" }.should == "new block" |
| 139 | end |
| 140 | |
| 141 | it "should not freeze the definition of the new method" do |
| 142 | klass = Class.new do |
| 143 | def new |
| 144 | "new" |
| 145 | end |
| 146 | deprecate! :old => :new |
| 147 | end |
| 148 | klass.send :define_method, :new do |
| 149 | "new 2" |
| 150 | end |
| 151 | stub_stderr("#{klass.inspect}#old") |
| 152 | klass.new.old.should == "new 2" |
| 153 | end |
Kevin Clark | da40e8d | 2008-06-18 00:58:04 +0000 | [diff] [blame] | 154 | |
| 155 | it "should call the forwarded method in the same context as the original" do |
| 156 | klass = Class.new do |
| 157 | def myself |
| 158 | self |
| 159 | end |
| 160 | deprecate! :me => :myself |
| 161 | end |
| 162 | inst = klass.new |
| 163 | stub_stderr("#{klass.inspect}#me") |
| 164 | inst.me.should eql(inst.myself) |
| 165 | end |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 166 | end |
| 167 | |
| 168 | describe "deprecate_class!" do |
Kevin Clark | 96cc516 | 2008-06-18 00:56:08 +0000 | [diff] [blame] | 169 | it_should_behave_like "deprecation" |
| 170 | |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 171 | it "should create a new global constant that points to the old one" do |
| 172 | begin |
| 173 | klass = Class.new do |
| 174 | def foo |
| 175 | "foo" |
| 176 | end |
| 177 | end |
| 178 | deprecate_class! :DeprecationSpecOldClass => klass |
Kevin Clark | f5754ac | 2008-06-18 01:00:12 +0000 | [diff] [blame^] | 179 | ::DeprecationSpecOldClass.should eql(klass) |
| 180 | ::DeprecationSpecOldClass.new.foo.should == "foo" |
| 181 | ensure |
| 182 | Object.send :remove_const, :DeprecationSpecOldClass if Object.const_defined? :DeprecationSpecOldClass |
| 183 | end |
| 184 | end |
| 185 | |
| 186 | it "should create a global constant even from inside a module" do |
| 187 | begin |
| 188 | klass = nil #define scoping |
| 189 | mod = Module.new do |
| 190 | klass = Class.new do |
| 191 | def foo |
| 192 | "foo" |
| 193 | end |
| 194 | end |
| 195 | deprecate_class! :DeprecationSpecOldClass => klass |
| 196 | end |
| 197 | ::DeprecationSpecOldClass.should eql(klass) |
| 198 | ::DeprecationSpecOldClass.new.foo.should == "foo" |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 199 | ensure |
Kevin Clark | 96cc516 | 2008-06-18 00:56:08 +0000 | [diff] [blame] | 200 | Object.send :remove_const, :DeprecationSpecOldClass if Object.const_defined? :DeprecationSpecOldClass |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 201 | end |
| 202 | end |
| 203 | end |