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 |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 16 | |
| 17 | def ensure_const_removed(name, &block) |
| 18 | begin |
| 19 | block.call |
| 20 | ensure |
| 21 | Object.send :remove_const, name if Object.const_defined? name |
| 22 | end |
| 23 | end |
Kevin Clark | 96cc516 | 2008-06-18 00:56:08 +0000 | [diff] [blame] | 24 | end |
| 25 | |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 26 | describe 'deprecate!' do |
Kevin Clark | 96cc516 | 2008-06-18 00:56:08 +0000 | [diff] [blame] | 27 | it_should_behave_like "deprecation" |
| 28 | |
Kevin Clark | fc964ee | 2008-06-18 00:57:46 +0000 | [diff] [blame] | 29 | def stub_stderr(callstr, offset=1) |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 30 | STDERR.should_receive(:puts).with("Warning: calling deprecated method #{callstr}") |
Kevin Clark | fc964ee | 2008-06-18 00:57:46 +0000 | [diff] [blame] | 31 | line = caller.first[/\d+$/].to_i + offset |
| 32 | STDERR.should_receive(:puts).with(" from #{__FILE__}:#{line}") |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 33 | end |
| 34 | |
| 35 | it "should work for Module methods" do |
| 36 | mod = Module.new do |
| 37 | class << self |
| 38 | def new |
| 39 | "new" |
| 40 | end |
| 41 | deprecate! :old => :new |
| 42 | end |
| 43 | end |
| 44 | stub_stderr "#{mod.inspect}.old" |
| 45 | mod.old.should == "new" |
| 46 | end |
| 47 | |
| 48 | it "should work with Modules that extend themselves" do |
| 49 | mod = Module.new do |
| 50 | extend self |
| 51 | def new |
| 52 | "new" |
| 53 | end |
| 54 | deprecate! :old => :new |
| 55 | end |
| 56 | stub_stderr "#{mod.inspect}.old" |
| 57 | mod.old.should == "new" |
| 58 | end |
| 59 | |
| 60 | it "should work wtih Class methods" do |
| 61 | klass = Class.new do |
| 62 | class << self |
| 63 | def new |
| 64 | "new" |
| 65 | end |
| 66 | deprecate! :old => :new |
| 67 | end |
| 68 | end |
| 69 | stub_stderr "#{klass.inspect}.old" |
| 70 | klass.old.should == "new" |
| 71 | end |
| 72 | |
| 73 | it "should work with Classes that include Modules" do |
| 74 | mod = Module.new do |
| 75 | def new |
| 76 | "new" |
| 77 | end |
| 78 | deprecate! :old => :new |
| 79 | end |
| 80 | klass = Class.new do |
| 81 | include mod |
| 82 | end |
| 83 | stub_stderr "#{klass.inspect}#old" |
| 84 | klass.new.old.should == "new" |
| 85 | end |
| 86 | |
| 87 | it "should work with instance methods" do |
| 88 | klass = Class.new do |
| 89 | def new |
| 90 | "new" |
| 91 | end |
| 92 | deprecate! :old => :new |
| 93 | end |
| 94 | stub_stderr "#{klass.inspect}#old" |
| 95 | klass.new.old.should == "new" |
| 96 | end |
| 97 | |
| 98 | it "should work with multiple method names" do |
| 99 | klass = Class.new do |
| 100 | def new1 |
| 101 | "new 1" |
| 102 | end |
| 103 | def new2 |
| 104 | "new 2" |
| 105 | end |
| 106 | deprecate! :old1 => :new1, :old2 => :new2 |
| 107 | end |
Kevin Clark | fc964ee | 2008-06-18 00:57:46 +0000 | [diff] [blame] | 108 | stub_stderr("#{klass.inspect}#old1", 3).ordered |
| 109 | stub_stderr("#{klass.inspect}#old2", 3).ordered |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 110 | inst = klass.new |
| 111 | inst.old1.should == "new 1" |
| 112 | inst.old2.should == "new 2" |
| 113 | end |
| 114 | |
| 115 | it "should only log a message once, even across multiple instances" do |
| 116 | klass = Class.new do |
| 117 | def new |
| 118 | "new" |
| 119 | end |
| 120 | deprecate! :old => :new |
| 121 | end |
| 122 | stub_stderr("#{klass.inspect}#old").once |
| 123 | klass.new.old.should == "new" |
| 124 | klass.new.old.should == "new" |
| 125 | end |
| 126 | |
| 127 | it "should pass arguments" do |
| 128 | klass = Class.new do |
| 129 | def new(a, b) |
| 130 | "new: #{a + b}" |
| 131 | end |
| 132 | deprecate! :old => :new |
| 133 | end |
| 134 | stub_stderr("#{klass.inspect}#old") |
| 135 | klass.new.old(3, 5).should == "new: 8" |
| 136 | end |
| 137 | |
| 138 | it "should pass blocks" do |
| 139 | klass = Class.new do |
| 140 | def new |
| 141 | "new #{yield}" |
| 142 | end |
| 143 | deprecate! :old => :new |
| 144 | end |
| 145 | stub_stderr("#{klass.inspect}#old") |
| 146 | klass.new.old { "block" }.should == "new block" |
| 147 | end |
| 148 | |
| 149 | it "should not freeze the definition of the new method" do |
| 150 | klass = Class.new do |
| 151 | def new |
| 152 | "new" |
| 153 | end |
| 154 | deprecate! :old => :new |
| 155 | end |
| 156 | klass.send :define_method, :new do |
| 157 | "new 2" |
| 158 | end |
| 159 | stub_stderr("#{klass.inspect}#old") |
| 160 | klass.new.old.should == "new 2" |
| 161 | end |
Kevin Clark | da40e8d | 2008-06-18 00:58:04 +0000 | [diff] [blame] | 162 | |
| 163 | it "should call the forwarded method in the same context as the original" do |
| 164 | klass = Class.new do |
| 165 | def myself |
| 166 | self |
| 167 | end |
| 168 | deprecate! :me => :myself |
| 169 | end |
| 170 | inst = klass.new |
| 171 | stub_stderr("#{klass.inspect}#me") |
| 172 | inst.me.should eql(inst.myself) |
| 173 | end |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 174 | end |
| 175 | |
| 176 | describe "deprecate_class!" do |
Kevin Clark | 96cc516 | 2008-06-18 00:56:08 +0000 | [diff] [blame] | 177 | it_should_behave_like "deprecation" |
| 178 | |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 179 | def stub_stderr_jruby(klass) |
| 180 | return if RUBY_PLATFORM != "java" |
| 181 | stub_stderr(klass, nil, caller.first) |
| 182 | end |
| 183 | |
| 184 | def stub_stderr_mri(klass, offset=1) |
| 185 | return if RUBY_PLATFORM == "java" |
| 186 | stub_stderr(klass, offset, caller.first) |
| 187 | end |
| 188 | |
| 189 | def stub_stderr(klass, offset=1, called=nil) |
Kevin Clark | de7864e | 2008-06-18 01:02:14 +0000 | [diff] [blame] | 190 | STDERR.should_receive(:puts).with("Warning: class #{klass} is deprecated") |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 191 | if offset |
| 192 | line = (called || caller.first)[/\d+$/].to_i + offset |
| 193 | STDERR.should_receive(:puts).with(" from #{__FILE__}:#{line}") |
| 194 | else |
| 195 | STDERR.should_receive(:puts).with(/^ from #{Regexp.escape(__FILE__)}:/) |
| 196 | end |
Kevin Clark | 2a8a731 | 2008-06-18 01:01:07 +0000 | [diff] [blame] | 197 | end |
| 198 | |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 199 | it "should create a new global constant that points to the old one" do |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 200 | ensure_const_removed :DeprecationSpecOldClass do |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 201 | klass = Class.new do |
| 202 | def foo |
| 203 | "foo" |
| 204 | end |
| 205 | end |
| 206 | deprecate_class! :DeprecationSpecOldClass => klass |
Kevin Clark | eff97fc | 2008-06-18 01:01:40 +0000 | [diff] [blame] | 207 | stub_stderr(:DeprecationSpecOldClass) |
Kevin Clark | f5754ac | 2008-06-18 01:00:12 +0000 | [diff] [blame] | 208 | ::DeprecationSpecOldClass.should eql(klass) |
| 209 | ::DeprecationSpecOldClass.new.foo.should == "foo" |
Kevin Clark | f5754ac | 2008-06-18 01:00:12 +0000 | [diff] [blame] | 210 | end |
| 211 | end |
| 212 | |
| 213 | it "should create a global constant even from inside a module" do |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 214 | ensure_const_removed :DeprecationSpecOldClass do |
Kevin Clark | f5754ac | 2008-06-18 01:00:12 +0000 | [diff] [blame] | 215 | klass = nil #define scoping |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 216 | Module.new do |
Kevin Clark | f5754ac | 2008-06-18 01:00:12 +0000 | [diff] [blame] | 217 | klass = Class.new do |
| 218 | def foo |
| 219 | "foo" |
| 220 | end |
| 221 | end |
| 222 | deprecate_class! :DeprecationSpecOldClass => klass |
| 223 | end |
Kevin Clark | eff97fc | 2008-06-18 01:01:40 +0000 | [diff] [blame] | 224 | stub_stderr(:DeprecationSpecOldClass) |
Kevin Clark | f5754ac | 2008-06-18 01:00:12 +0000 | [diff] [blame] | 225 | ::DeprecationSpecOldClass.should eql(klass) |
| 226 | ::DeprecationSpecOldClass.new.foo.should == "foo" |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 227 | end |
| 228 | end |
Kevin Clark | 2a8a731 | 2008-06-18 01:01:07 +0000 | [diff] [blame] | 229 | |
| 230 | it "should not prevent the deprecated class from being a superclass" do |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 231 | ensure_const_removed :DeprecationSpecOldClass do |
Kevin Clark | 2a8a731 | 2008-06-18 01:01:07 +0000 | [diff] [blame] | 232 | klass = Class.new do |
| 233 | def foo |
| 234 | "foo" |
| 235 | end |
| 236 | end |
| 237 | deprecate_class! :DeprecationSpecOldClass => klass |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 238 | stub_stderr_jruby(:DeprecationSpecOldClass) |
Kevin Clark | 2a8a731 | 2008-06-18 01:01:07 +0000 | [diff] [blame] | 239 | subklass = Class.new(::DeprecationSpecOldClass) do |
| 240 | def foo |
| 241 | "subclass #{super}" |
| 242 | end |
| 243 | end |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 244 | stub_stderr_mri(:DeprecationSpecOldClass) |
Kevin Clark | 2a8a731 | 2008-06-18 01:01:07 +0000 | [diff] [blame] | 245 | subklass.superclass.should eql(klass) |
| 246 | subklass.new.foo.should == "subclass foo" |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 247 | end |
| 248 | end |
Kevin Clark | fc31164 | 2008-06-18 01:01:57 +0000 | [diff] [blame] | 249 | |
| 250 | it "should not bleed info between deprecations" do |
| 251 | ensure_const_removed :DeprecationSpecOldClass do |
| 252 | ensure_const_removed :DeprecationSpecOldClass2 do |
| 253 | klass = Class.new do |
| 254 | def foo |
| 255 | "foo" |
| 256 | end |
| 257 | end |
| 258 | deprecate_class! :DeprecationSpecOldClass => klass |
| 259 | klass2 = Class.new do |
| 260 | def bar |
| 261 | "bar" |
| 262 | end |
| 263 | end |
| 264 | deprecate_class! :DeprecationSpecOldClass2 => klass2 |
| 265 | stub_stderr(:DeprecationSpecOldClass) |
| 266 | ::DeprecationSpecOldClass.new.foo.should == "foo" |
| 267 | stub_stderr(:DeprecationSpecOldClass2) |
| 268 | ::DeprecationSpecOldClass2.new.bar.should == "bar" |
| 269 | end |
| 270 | end |
| 271 | end |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 272 | end |
| 273 | |
| 274 | describe "deprecate_module!" do |
| 275 | it_should_behave_like "deprecation" |
| 276 | |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 277 | def stub_stderr_jruby(mod) |
| 278 | return if RUBY_PLATFORM != "java" |
| 279 | stub_stderr(mod, nil, caller.first) |
| 280 | end |
| 281 | |
| 282 | def stub_stderr_mri(mod, offset=1) |
| 283 | return if RUBY_PLATFORM == "java" |
| 284 | stub_stderr(mod, offset, caller.first) |
| 285 | end |
| 286 | |
| 287 | def stub_stderr(mod, offset=1, called=nil) |
Kevin Clark | de7864e | 2008-06-18 01:02:14 +0000 | [diff] [blame] | 288 | STDERR.should_receive(:puts).with("Warning: module #{mod} is deprecated") |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 289 | if offset |
| 290 | line = (called || caller.first)[/\d+$/].to_i + offset |
| 291 | STDERR.should_receive(:puts).with(" from #{__FILE__}:#{line}") |
| 292 | else |
| 293 | STDERR.should_receive(:puts).with(/^ from #{Regexp.escape(__FILE__)}:/) |
| 294 | end |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 295 | end |
| 296 | |
| 297 | it "should create a new global constant that points to the old one" do |
| 298 | ensure_const_removed :DeprecationSpecOldModule do |
| 299 | mod = Module.new do |
| 300 | def self.foo |
| 301 | "foo" |
| 302 | end |
| 303 | end |
| 304 | deprecate_module! :DeprecationSpecOldModule => mod |
Kevin Clark | eff97fc | 2008-06-18 01:01:40 +0000 | [diff] [blame] | 305 | stub_stderr(:DeprecationSpecOldModule) |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 306 | ::DeprecationSpecOldModule.should eql(mod) |
| 307 | ::DeprecationSpecOldModule.foo.should == "foo" |
| 308 | end |
| 309 | end |
| 310 | |
| 311 | it "should create a global constant even from inside a module" do |
| 312 | ensure_const_removed :DeprecationSpecOldModule do |
| 313 | mod = nil # scoping |
| 314 | Module.new do |
| 315 | mod = Module.new do |
| 316 | def self.foo |
| 317 | "foo" |
| 318 | end |
| 319 | end |
| 320 | deprecate_module! :DeprecationSpecOldModule => mod |
| 321 | end |
Kevin Clark | eff97fc | 2008-06-18 01:01:40 +0000 | [diff] [blame] | 322 | stub_stderr(:DeprecationSpecOldModule) |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 323 | ::DeprecationSpecOldModule.should eql(mod) |
| 324 | ::DeprecationSpecOldModule.foo.should == "foo" |
| 325 | end |
| 326 | end |
| 327 | |
| 328 | it "should work for modules that extend themselves" do |
| 329 | ensure_const_removed :DeprecationSpecOldModule do |
| 330 | mod = Module.new do |
| 331 | extend self |
| 332 | def foo |
| 333 | "foo" |
| 334 | end |
| 335 | end |
| 336 | deprecate_module! :DeprecationSpecOldModule => mod |
Kevin Clark | eff97fc | 2008-06-18 01:01:40 +0000 | [diff] [blame] | 337 | stub_stderr(:DeprecationSpecOldModule) |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 338 | ::DeprecationSpecOldModule.should eql(mod) |
| 339 | ::DeprecationSpecOldModule.foo.should == "foo" |
| 340 | end |
| 341 | end |
| 342 | |
| 343 | it "should work for modules included in other modules" do |
| 344 | ensure_const_removed :DeprecationSpecOldModule do |
| 345 | mod = Module.new do |
| 346 | def foo |
| 347 | "foo" |
| 348 | end |
| 349 | end |
| 350 | deprecate_module! :DeprecationSpecOldModule => mod |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 351 | stub_stderr_jruby(:DeprecationSpecOldModule) |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 352 | mod2 = Module.new do |
| 353 | class << self |
| 354 | include ::DeprecationSpecOldModule |
| 355 | end |
| 356 | end |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 357 | stub_stderr_mri(:DeprecationSpecOldModule) |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 358 | mod2.foo.should == "foo" |
| 359 | end |
| 360 | end |
| 361 | |
| 362 | it "should work for modules included in classes" do |
| 363 | ensure_const_removed :DeprecationSpecOldModule do |
| 364 | mod = Module.new do |
| 365 | def foo |
| 366 | "foo" |
| 367 | end |
| 368 | end |
| 369 | deprecate_module! :DeprecationSpecOldModule => mod |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 370 | stub_stderr_jruby(:DeprecationSpecOldModule) |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 371 | klass = Class.new do |
| 372 | include ::DeprecationSpecOldModule |
| 373 | end |
Kevin Clark | 8c3e093 | 2008-06-18 01:19:54 +0000 | [diff] [blame] | 374 | stub_stderr_mri(:DeprecationSpecOldModule) |
Kevin Clark | 03a5fb1 | 2008-06-18 01:01:25 +0000 | [diff] [blame] | 375 | klass.new.foo.should == "foo" |
Kevin Clark | 2a8a731 | 2008-06-18 01:01:07 +0000 | [diff] [blame] | 376 | end |
| 377 | end |
Kevin Clark | de7864e | 2008-06-18 01:02:14 +0000 | [diff] [blame] | 378 | |
| 379 | it "should not bleed info between deprecations" do |
| 380 | ensure_const_removed :DeprecationSpecOldModule do |
| 381 | ensure_const_removed :DeprecationSpecOldModule2 do |
| 382 | mod = Module.new do |
| 383 | def self.foo |
| 384 | "foo" |
| 385 | end |
| 386 | end |
| 387 | deprecate_module! :DeprecationSpecOldModule => mod |
| 388 | mod2 = Module.new do |
| 389 | def self.bar |
| 390 | "bar" |
| 391 | end |
| 392 | end |
| 393 | deprecate_module! :DeprecationSpecOldModule2 => mod2 |
| 394 | stub_stderr(:DeprecationSpecOldModule) |
| 395 | ::DeprecationSpecOldModule.foo.should == "foo" |
| 396 | stub_stderr(:DeprecationSpecOldModule2) |
| 397 | ::DeprecationSpecOldModule2.bar.should == "bar" |
| 398 | end |
| 399 | end |
| 400 | end |
Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame] | 401 | end |