Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions core/method/original_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@
klass.new.method(:renamed).original_name.should == :my_method
klass.new.method(:aliased).original_name.should == :my_method
end

it "returns the source UnboundMethod's name for Kernel#is_a? and Kernel#kind_of?" do
klass = Class.new { define_method(:is_a?, ::Kernel.instance_method(:is_a?)) }
klass.new.method(:is_a?).original_name.should == :is_a?
Comment on lines +45 to +46
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give the new method another name otherwise this doesn't test anything, same below


klass = Class.new { define_method(:kind_of?, ::Kernel.instance_method(:kind_of?)) }
klass.new.method(:kind_of?).original_name.should == :kind_of?
end

it "preserves the source name when aliasing a define_method'd Kernel method" do
klass = Class.new do
define_method(:is_a?, ::Kernel.instance_method(:is_a?))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, this should have another name

alias_method :renamed_is_a?, :is_a?
end
klass.new.method(:renamed_is_a?).original_name.should == :is_a?
end
end
26 changes: 26 additions & 0 deletions core/method/shared/to_s.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,30 @@ def obj.bar; end
it "shows the metaclass and the owner for a Module instance method retrieved from a class" do
String.method(:include).inspect.should.start_with?("#<Method: #<Class:String>(Module)#include")
end

it "shows the original name in parentheses for an aliased method" do
klass = Class.new do
def original_method; end
alias_method :renamed_method, :original_method
end
klass.new.method(:renamed_method).send(@method).should =~ /#renamed_method\(original_method\)/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
klass.new.method(:renamed_method).send(@method).should =~ /#renamed_method\(original_method\)/
klass.new.method(:renamed_method).send(@method).should.include? '#renamed_method(original_method)'

end

it "shows the source UnboundMethod's name in parentheses for a define_method'd method" do
klass = Class.new { define_method(:renamed_is_a?, ::Kernel.instance_method(:is_a?)) }
klass.new.method(:renamed_is_a?).send(@method).should =~ /#renamed_is_a\?\(is_a\?\)/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, .should.include? is much easier to read by avoiding escaping.
Same below

end

it "does not annotate a directly looked-up Kernel method with a shared internal name" do
Object.new.method(:is_a?).send(@method).should_not =~ /\(kind_of\?\)/
Object.new.method(:kind_of?).send(@method).should_not =~ /\(is_a\?\)/
Comment on lines +96 to +97
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.should_not.include?

end

it "shows the source name when aliasing a define_method'd Kernel method" do
klass = Class.new do
define_method(:is_a?, ::Kernel.instance_method(:is_a?))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be good to give this one another name for clarity and to make it more interesting

alias_method :renamed_is_a?, :is_a?
end
klass.new.method(:renamed_is_a?).send(@method).should =~ /#renamed_is_a\?\(is_a\?\)/
end
end
16 changes: 16 additions & 0 deletions core/unboundmethod/original_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@
klass.instance_method(:renamed).original_name.should == :my_method
klass.instance_method(:aliased).original_name.should == :my_method
end

it "returns the source UnboundMethod's name for Kernel#is_a? and Kernel#kind_of?" do
klass = Class.new { define_method(:is_a?, ::Kernel.instance_method(:is_a?)) }
klass.instance_method(:is_a?).original_name.should == :is_a?

klass = Class.new { define_method(:kind_of?, ::Kernel.instance_method(:kind_of?)) }
klass.instance_method(:kind_of?).original_name.should == :kind_of?
end

it "preserves the source name when aliasing a define_method'd Kernel method" do
klass = Class.new do
define_method(:is_a?, ::Kernel.instance_method(:is_a?))
alias_method :renamed_is_a?, :is_a?
end
klass.instance_method(:renamed_is_a?).original_name.should == :is_a?
end
end
26 changes: 26 additions & 0 deletions core/unboundmethod/shared/to_s.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,30 @@
it "does not show the defining module if it is the same as the origin" do
UnboundMethodSpecs::A.instance_method(:baz).send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::A#baz"
end

it "shows the original name in parentheses for an aliased method" do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplication for UnboundMethod is not ideal. One way to fix it would be to use a separate shared spec describe, require it from both specs and pass a lambda as @object, for Method it's just be identity and for UnboundMethod it'd be -> meth { meth.unbind }.
This is a bigger/trickier change though so if you don't feel comfortable with it I think it's OK to keep the duplication in this PR.

klass = Class.new do
def original_method; end
alias_method :renamed_method, :original_method
end
klass.instance_method(:renamed_method).send(@method).should =~ /#renamed_method\(original_method\)/
end

it "shows the source UnboundMethod's name in parentheses for a define_method'd method" do
klass = Class.new { define_method(:renamed_is_a?, ::Kernel.instance_method(:is_a?)) }
klass.instance_method(:renamed_is_a?).send(@method).should =~ /#renamed_is_a\?\(is_a\?\)/
end

it "does not annotate a directly looked-up Kernel method with a shared internal name" do
Object.instance_method(:is_a?).send(@method).should_not =~ /\(kind_of\?\)/
Object.instance_method(:kind_of?).send(@method).should_not =~ /\(is_a\?\)/
end

it "shows the source name when aliasing a define_method'd Kernel method" do
klass = Class.new do
define_method(:is_a?, ::Kernel.instance_method(:is_a?))
alias_method :renamed_is_a?, :is_a?
end
klass.instance_method(:renamed_is_a?).send(@method).should =~ /#renamed_is_a\?\(is_a\?\)/
end
end
Loading