Skip to content

Commit d94104b

Browse files
Add specs for original_name with define_method
Verify that Method#original_name and UnboundMethod#original_name return the source UnboundMethod's name when a method is created via define_method, and that the original name is preserved through define_method and aliasing.
1 parent 9b3f5ff commit d94104b

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

core/method/original_name_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,19 @@
1919
obj.method(:baz).original_name.should == :foo
2020
obj.method(:baz).unbind.bind(obj).original_name.should == :foo
2121
end
22+
23+
it "returns the source UnboundMethod's name (not the name given to define_method)" do
24+
klass = Class.new { define_method(:my_inspect, ::Kernel.instance_method(:inspect)) }
25+
klass.new.method(:my_inspect).original_name.should == :inspect
26+
end
27+
28+
it "preserves the source method's name through define_method and alias" do
29+
source = Class.new { def my_method; end }
30+
klass = Class.new(source) do
31+
define_method(:renamed, source.instance_method(:my_method))
32+
alias aliased renamed
33+
end
34+
klass.new.method(:renamed).original_name.should == :my_method
35+
klass.new.method(:aliased).original_name.should == :my_method
36+
end
2237
end

core/unboundmethod/original_name_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,19 @@
1919
obj.method(:baz).unbind.original_name.should == :foo
2020
UnboundMethodSpecs::Methods.instance_method(:baz).original_name.should == :foo
2121
end
22+
23+
it "returns the source UnboundMethod's name (not the name given to define_method)" do
24+
klass = Class.new { define_method(:my_inspect, ::Kernel.instance_method(:inspect)) }
25+
klass.instance_method(:my_inspect).original_name.should == :inspect
26+
end
27+
28+
it "preserves the source method's name through define_method and alias" do
29+
source = Class.new { def my_method; end }
30+
klass = Class.new(source) do
31+
define_method(:renamed, source.instance_method(:my_method))
32+
alias aliased renamed
33+
end
34+
klass.instance_method(:renamed).original_name.should == :my_method
35+
klass.instance_method(:aliased).original_name.should == :my_method
36+
end
2237
end

0 commit comments

Comments
 (0)