Kevin Clark | 3eca078 | 2008-06-18 00:54:53 +0000 | [diff] [blame^] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | |
| 3 | describe 'deprecate!' do |
| 4 | def stub_stderr(callstr) |
| 5 | STDERR.should_receive(:puts).with("Warning: calling deprecated method #{callstr}") |
| 6 | end |
| 7 | |
| 8 | it "should work for Module methods" do |
| 9 | mod = Module.new do |
| 10 | class << self |
| 11 | def new |
| 12 | "new" |
| 13 | end |
| 14 | deprecate! :old => :new |
| 15 | end |
| 16 | end |
| 17 | stub_stderr "#{mod.inspect}.old" |
| 18 | mod.old.should == "new" |
| 19 | end |
| 20 | |
| 21 | it "should work with Modules that extend themselves" do |
| 22 | mod = Module.new do |
| 23 | extend self |
| 24 | def new |
| 25 | "new" |
| 26 | end |
| 27 | deprecate! :old => :new |
| 28 | end |
| 29 | stub_stderr "#{mod.inspect}.old" |
| 30 | mod.old.should == "new" |
| 31 | end |
| 32 | |
| 33 | it "should work wtih Class methods" do |
| 34 | klass = Class.new do |
| 35 | class << self |
| 36 | def new |
| 37 | "new" |
| 38 | end |
| 39 | deprecate! :old => :new |
| 40 | end |
| 41 | end |
| 42 | stub_stderr "#{klass.inspect}.old" |
| 43 | klass.old.should == "new" |
| 44 | end |
| 45 | |
| 46 | it "should work with Classes that include Modules" do |
| 47 | mod = Module.new do |
| 48 | def new |
| 49 | "new" |
| 50 | end |
| 51 | deprecate! :old => :new |
| 52 | end |
| 53 | klass = Class.new do |
| 54 | include mod |
| 55 | end |
| 56 | stub_stderr "#{klass.inspect}#old" |
| 57 | klass.new.old.should == "new" |
| 58 | end |
| 59 | |
| 60 | it "should work with instance methods" do |
| 61 | klass = Class.new do |
| 62 | def new |
| 63 | "new" |
| 64 | end |
| 65 | deprecate! :old => :new |
| 66 | end |
| 67 | stub_stderr "#{klass.inspect}#old" |
| 68 | klass.new.old.should == "new" |
| 69 | end |
| 70 | |
| 71 | it "should work with multiple method names" do |
| 72 | klass = Class.new do |
| 73 | def new1 |
| 74 | "new 1" |
| 75 | end |
| 76 | def new2 |
| 77 | "new 2" |
| 78 | end |
| 79 | deprecate! :old1 => :new1, :old2 => :new2 |
| 80 | end |
| 81 | stub_stderr("#{klass.inspect}#old1").ordered |
| 82 | stub_stderr("#{klass.inspect}#old2").ordered |
| 83 | inst = klass.new |
| 84 | inst.old1.should == "new 1" |
| 85 | inst.old2.should == "new 2" |
| 86 | end |
| 87 | |
| 88 | it "should only log a message once, even across multiple instances" do |
| 89 | klass = Class.new do |
| 90 | def new |
| 91 | "new" |
| 92 | end |
| 93 | deprecate! :old => :new |
| 94 | end |
| 95 | stub_stderr("#{klass.inspect}#old").once |
| 96 | klass.new.old.should == "new" |
| 97 | klass.new.old.should == "new" |
| 98 | end |
| 99 | |
| 100 | it "should pass arguments" do |
| 101 | klass = Class.new do |
| 102 | def new(a, b) |
| 103 | "new: #{a + b}" |
| 104 | end |
| 105 | deprecate! :old => :new |
| 106 | end |
| 107 | stub_stderr("#{klass.inspect}#old") |
| 108 | klass.new.old(3, 5).should == "new: 8" |
| 109 | end |
| 110 | |
| 111 | it "should pass blocks" do |
| 112 | klass = Class.new do |
| 113 | def new |
| 114 | "new #{yield}" |
| 115 | end |
| 116 | deprecate! :old => :new |
| 117 | end |
| 118 | stub_stderr("#{klass.inspect}#old") |
| 119 | klass.new.old { "block" }.should == "new block" |
| 120 | end |
| 121 | |
| 122 | it "should not freeze the definition of the new method" do |
| 123 | klass = Class.new do |
| 124 | def new |
| 125 | "new" |
| 126 | end |
| 127 | deprecate! :old => :new |
| 128 | end |
| 129 | klass.send :define_method, :new do |
| 130 | "new 2" |
| 131 | end |
| 132 | stub_stderr("#{klass.inspect}#old") |
| 133 | klass.new.old.should == "new 2" |
| 134 | end |
| 135 | end |
| 136 | |
| 137 | describe "deprecate_class!" do |
| 138 | it "should create a new global constant that points to the old one" do |
| 139 | begin |
| 140 | klass = Class.new do |
| 141 | def foo |
| 142 | "foo" |
| 143 | end |
| 144 | end |
| 145 | deprecate_class! :DeprecationSpecOldClass => klass |
| 146 | DeprecationSpecOldClass.should eql(klass) |
| 147 | DeprecationSpecOldClass.new.foo.should == "foo" |
| 148 | ensure |
| 149 | Object.send :remove_const, :DeprecationSpecOldClass |
| 150 | end |
| 151 | end |
| 152 | end |