1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Retouch #aliases so it returns Array<String>

Also, refactor tests for `Method#aliases` and use a neat trick with
Sets, when matching two arrays.

  # Meh.
  [:a, :b, :c] == [:c, :b, :a] # => false

  # Yarr!
  Set.new([:a, :b, :c]) == Set.new([:c, :b, :a]) # => true

Last but not least, this commit fixes build errors on different rubies
(well, I hope so!).

Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
This commit is contained in:
Kyrylo Silin 2012-08-28 02:08:05 +03:00
parent 1b056e094a
commit be626cfada
2 changed files with 13 additions and 13 deletions

View file

@ -403,8 +403,9 @@ class Pry
source_file == Pry.eval_path source_file == Pry.eval_path
end end
# @return [Array<Symbol>] All known aliases for the method. For Ruby 1.8 and # @return [Array<String>] All known aliases for the method.
# friends returns [Array<String>]. # @note On Ruby 1.8 this method always returns an empty Array for methods
# implemented in C.
def aliases def aliases
owner = @method.owner owner = @method.owner
# Avoid using `to_sym` on {Method#name}, which returns a `String`, because # Avoid using `to_sym` on {Method#name}, which returns a `String`, because
@ -417,7 +418,7 @@ class Pry
end.flatten end.flatten
alias_list.delete(name) alias_list.delete(name)
alias_list alias_list.map(&:to_s)
end end
# @return [Boolean] Is the method definitely an alias? # @return [Boolean] Is the method definitely an alias?

View file

@ -1,4 +1,5 @@
require 'helper' require 'helper'
require 'set'
describe Pry::Method do describe Pry::Method do
it "should use String names for compatibility" do it "should use String names for compatibility" do
@ -414,12 +415,9 @@ describe Pry::Method do
it 'should be able to find method aliases' do it 'should be able to find method aliases' do
meth = Pry::Method(@class.new.method(:eat)) meth = Pry::Method(@class.new.method(:eat))
aliases = Set.new(meth.aliases)
if Pry::Helpers::BaseHelpers.mri_19? aliases.should == Set.new(["fress", "omnomnom"])
meth.aliases.should == [:fress, :omnomnom]
else
meth.aliases.sort.map(&:to_sym).should == [:fress, :omnomnom]
end
end end
it 'should return an empty Array if cannot find aliases' do it 'should return an empty Array if cannot find aliases' do
@ -429,15 +427,16 @@ describe Pry::Method do
it 'should not include the own name in the list of aliases' do it 'should not include the own name in the list of aliases' do
meth = Pry::Method(@class.new.method(:eat)) meth = Pry::Method(@class.new.method(:eat))
meth.aliases.should.not.include "eat"
meth.aliases.should.not.include :eat
meth.aliases.should.not.include "eat" # For Ruby 1.8 and friends.
end end
if Pry::Helpers::BaseHelpers.mri_18? || Pry::Helpers::BaseHelpers.mri_19? unless Pry::Helpers::BaseHelpers.mri_18?
# Ruby 1.8 doesn't support this feature.
it 'should be able to find aliases for methods implemented in C' do it 'should be able to find aliases for methods implemented in C' do
meth = Pry::Method(Hash.new.method(:key?)) meth = Pry::Method(Hash.new.method(:key?))
meth.aliases.should == [:include?, :member?, :has_key?] aliases = Set.new(meth.aliases)
aliases.should == Set.new(["include?", "member?", "has_key?"])
end end
end end