summaryrefslogtreecommitdiff
path: root/spec/ruby/core/method/original_name_spec.rb
blob: 8fec0e7c33cee5c1a0161719bc3e518bc5126081 (plain)
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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Method#original_name" do
  it "returns the name of the method" do
    "abc".method(:upcase).original_name.should == :upcase
  end

  it "returns the original name when aliased" do
    obj = MethodSpecs::Methods.new
    obj.method(:foo).original_name.should == :foo
    obj.method(:bar).original_name.should == :foo
    obj.method(:bar).unbind.bind(obj).original_name.should == :foo
  end

  it "returns the original name even when aliased twice" do
    obj = MethodSpecs::Methods.new
    obj.method(:foo).original_name.should == :foo
    obj.method(:baz).original_name.should == :foo
    obj.method(:baz).unbind.bind(obj).original_name.should == :foo
  end

  it "returns the original name even when aliased thrice" do
    obj = MethodSpecs::Methods.new
    obj.method(:qux).original_name.should == :foo
    obj.method(:qux).unbind.bind(obj).original_name.should == :foo
  end

  it "returns the source UnboundMethod's name (not the name given to define_method)" do
    klass = Class.new { define_method(:my_inspect, ::Kernel.instance_method(:inspect)) }
    klass.new.method(:my_inspect).original_name.should == :inspect
  end

  it "preserves the source method's name through define_method and alias" do
    source = Class.new { def my_method; end }
    klass = Class.new(source) do
      define_method(:renamed, source.instance_method(:my_method))
      alias aliased renamed
    end
    klass.new.method(:renamed).original_name.should == :my_method
    klass.new.method(:aliased).original_name.should == :my_method
  end
end