rubyspec / rubyspec

Executable specification for the Ruby programming language using RSpec syntax.

This URL has Read+Write access

rubyspec / language / alias_spec.rb
100644 147 lines (129 sloc) 3.452 kb
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
require File.dirname(__FILE__) + '/../spec_helper'
 
class AliasObject
  attr :foo
  attr_reader :bar
  attr_accessor :baz
  
  def prep; @foo = 3; @bar = 4; end
  def value; 5; end
  def false_value; 6; end
end
 
describe "The alias keyword" do
  before(:each) do
    @obj = AliasObject.new
    @meta = class << @obj;self;end
  end
 
  it "creates a new name for an existing method" do
    @meta.class_eval do
      alias __value value
    end
    @obj.__value.should == 5
  end
 
  it "adds the new method to the list of methods" do
    original_methods = @obj.methods
    @meta.class_eval do
      alias __value value
    end
    (@obj.methods - original_methods).map {|m| m.to_s }.should == ["__value"]
  end
 
  it "adds the new method to the list of public methods" do
    original_methods = @obj.public_methods
    @meta.class_eval do
      alias __value value
    end
    (@obj.public_methods - original_methods).map {|m| m.to_s }.should == ["__value"]
  end
 
  it "overwrites an existing method with the target name" do
    @meta.class_eval do
      alias false_value value
    end
    @obj.false_value.should == 5
  end
 
  it "is reversible" do
    @meta.class_eval do
      alias __value value
      alias value false_value
    end
    @obj.value.should == 6
 
    @meta.class_eval do
      alias value __value
    end
    @obj.value.should == 5
  end
 
  it "operates on the object's metaclass when used in instance_eval" do
    @obj.instance_eval do
      alias __value value
    end
 
    @obj.__value.should == 5
    lambda { AliasObject.new.__value }.should raise_error(NoMethodError)
  end
 
  it "operates on methods defined via attr, attr_reader, and attr_accessor" do
    @obj.prep
    @obj.instance_eval do
      alias afoo foo
      alias abar bar
      alias abaz baz
    end
 
    @obj.afoo.should == 3
    @obj.abar.should == 4
    @obj.baz = 5
    @obj.abaz.should == 5
  end
  
  it "operates on methods with splat arguments" do
    class AliasObject2;end
    AliasObject2.class_eval do
      def test(*args)
        4
      end
      def test_with_check(*args)
        test_without_check(*args)
      end
      alias test_without_check test
      alias test test_with_check
    end
    AliasObject2.new.test(1,2,3,4,5).should == 4
  end
  
  it "operates on methods with splat arguments on eigenclasses" do
    @meta.class_eval do
      def test(*args)
        4
      end
      def test_with_check(*args)
        test_without_check(*args)
      end
      alias test_without_check test
      alias test test_with_check
    end
    @obj.test(1,2,3,4,5).should == 4
  end
 
  it "operates on methods with splat arguments defined in a superclass" do
    class AliasObject3;end
    class Sub3 < AliasObject3;end
    AliasObject3.class_eval do
      def test(*args)
        4
      end
      def test_with_check(*args)
        test_without_check(*args)
      end
    end
    Sub3.class_eval do
      alias test_without_check test
      alias test test_with_check
    end
    Sub3.new.test(1,2,3,4,5).should == 4
  end
 
  it "operates on methods with splat arguments defined in a superclass using text block for class eval" do
    class Sub < AliasObject;end
    AliasObject.class_eval <<-code
def test(*args)
4
end
def test_with_check(*args)
test_without_check(*args)
end
alias test_without_check test
alias test test_with_check
code
    Sub.new.test("testing").should == 4
  end
end