2014-03-14 00:31:24 -04:00
|
|
|
require_relative 'helper'
|
2012-12-19 06:16:05 -05:00
|
|
|
|
|
|
|
describe Pry::CodeObject do
|
|
|
|
describe "basic lookups" do
|
|
|
|
before do
|
|
|
|
@obj = Object.new
|
|
|
|
def @obj.ziggy
|
|
|
|
"a flight of scarlet pigeons thunders round my thoughts"
|
|
|
|
end
|
|
|
|
|
|
|
|
class ClassyWassy
|
|
|
|
def piggy
|
2012-12-19 16:48:14 -05:00
|
|
|
binding
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
end
|
2013-01-11 14:34:48 -05:00
|
|
|
|
|
|
|
@p = Pry.new
|
|
|
|
@p.binding_stack = [binding]
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.remove_const(:ClassyWassy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should lookup methods' do
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("@obj.ziggy", @p)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.is_a?(Pry::Method).should == true
|
|
|
|
m.name.to_sym.should == :ziggy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should lookup modules' do
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("ClassyWassy", @p)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.source.should =~ /piggy/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should lookup procs' do
|
|
|
|
my_proc = proc { :hello }
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [binding]
|
|
|
|
m = Pry::CodeObject.lookup("my_proc", @p)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.is_a?(Pry::Method).should == true
|
|
|
|
m.source.should =~ /hello/
|
|
|
|
end
|
|
|
|
|
2012-12-25 14:24:02 -05:00
|
|
|
describe 'commands lookup' do
|
2012-12-25 14:31:57 -05:00
|
|
|
before do
|
|
|
|
@p = Pry.new
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [binding]
|
2012-12-25 14:31:57 -05:00
|
|
|
end
|
|
|
|
|
2012-12-27 16:53:51 -05:00
|
|
|
it 'should return command class' do
|
2012-12-25 14:31:57 -05:00
|
|
|
@p.commands.command "jeremy-jones" do
|
2012-12-25 14:24:02 -05:00
|
|
|
"lobster"
|
|
|
|
end
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("jeremy-jones", @p)
|
2012-12-25 14:24:02 -05:00
|
|
|
(m <= Pry::Command).should == true
|
|
|
|
m.source.should =~ /lobster/
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
|
2012-12-27 16:53:51 -05:00
|
|
|
describe "class commands" do
|
|
|
|
before do
|
|
|
|
class LobsterLady < Pry::ClassCommand
|
|
|
|
match "lobster-lady"
|
|
|
|
description "nada."
|
|
|
|
def process
|
|
|
|
"lobster"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.remove_const(:LobsterLady)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return Pry::ClassCommand class when looking up class command' do
|
|
|
|
Pry.commands.add_command(LobsterLady)
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("lobster-lady", @p)
|
2012-12-27 16:53:51 -05:00
|
|
|
(m <= Pry::ClassCommand).should == true
|
|
|
|
m.source.should =~ /class LobsterLady/
|
|
|
|
Pry.commands.delete("lobster-lady")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return Pry::WrappedModule when looking up command class directly (as a class, not as a command)' do
|
|
|
|
Pry.commands.add_command(LobsterLady)
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("LobsterLady", @p)
|
2012-12-27 16:53:51 -05:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.source.should =~ /class LobsterLady/
|
|
|
|
Pry.commands.delete("lobster-lady")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-25 14:24:02 -05:00
|
|
|
it 'looks up commands by :listing name as well' do
|
2012-12-25 14:31:57 -05:00
|
|
|
@p.commands.command /jeremy-.*/, "", :listing => "jeremy-baby" do
|
2012-12-25 14:24:02 -05:00
|
|
|
"lobster"
|
|
|
|
end
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("jeremy-baby", @p)
|
2012-12-25 14:24:02 -05:00
|
|
|
(m <= Pry::Command).should == true
|
|
|
|
m.source.should =~ /lobster/
|
2012-12-20 11:44:43 -05:00
|
|
|
end
|
2012-12-25 14:24:02 -05:00
|
|
|
|
|
|
|
it 'finds nothing when passing nil as the first argument' do
|
2013-01-11 14:34:48 -05:00
|
|
|
Pry::CodeObject.lookup(nil, @p).should == nil
|
2012-12-25 14:24:02 -05:00
|
|
|
end
|
|
|
|
|
2012-12-20 11:44:43 -05:00
|
|
|
end
|
|
|
|
|
2012-12-22 16:47:42 -05:00
|
|
|
it 'should lookup instance methods defined on classes accessed via local variable' do
|
|
|
|
o = Class.new do
|
|
|
|
def princess_bubblegum
|
|
|
|
"mathematic!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [binding]
|
|
|
|
m = Pry::CodeObject.lookup("o#princess_bubblegum", @p)
|
2012-12-22 16:47:42 -05:00
|
|
|
m.is_a?(Pry::Method).should == true
|
|
|
|
m.source.should =~ /mathematic!/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should lookup class methods defined on classes accessed via local variable' do
|
|
|
|
o = Class.new do
|
|
|
|
def self.finn
|
|
|
|
"4 realzies"
|
|
|
|
end
|
|
|
|
end
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [binding]
|
|
|
|
m = Pry::CodeObject.lookup("o.finn", @p)
|
2012-12-22 16:47:42 -05:00
|
|
|
m.is_a?(Pry::Method).should == true
|
|
|
|
m.source.should =~ /4 realzies/
|
|
|
|
end
|
|
|
|
|
2012-12-19 06:16:05 -05:00
|
|
|
it 'should lookup the class of an object (when given a variable)' do
|
|
|
|
moddy = ClassyWassy.new
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [binding]
|
|
|
|
m = Pry::CodeObject.lookup("moddy", @p)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.source.should =~ /piggy/
|
|
|
|
end
|
2012-12-19 16:48:14 -05:00
|
|
|
|
|
|
|
describe "inferring object from binding when lookup str is empty/nil" do
|
|
|
|
before do
|
|
|
|
@b1 = Pry.binding_for(ClassyWassy)
|
|
|
|
@b2 = Pry.binding_for(ClassyWassy.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "infer module objects" do
|
|
|
|
it 'should infer module object when binding self is a module' do
|
|
|
|
["", nil].each do |v|
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [@b1]
|
|
|
|
m = Pry::CodeObject.lookup(v, @p)
|
2012-12-19 16:48:14 -05:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.name.should =~ /ClassyWassy/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should infer module object when binding self is an instance' do
|
|
|
|
["", nil].each do |v|
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [@b2]
|
|
|
|
m = Pry::CodeObject.lookup(v, @p)
|
2012-12-19 16:48:14 -05:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.name.should =~ /ClassyWassy/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "infer method objects" do
|
|
|
|
it 'should infer method object from binding when inside method context' do
|
|
|
|
b = ClassyWassy.new.piggy
|
|
|
|
|
|
|
|
["", nil].each do |v|
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [b]
|
|
|
|
m = Pry::CodeObject.lookup(v, @p)
|
2012-12-19 16:48:14 -05:00
|
|
|
m.is_a?(Pry::Method).should == true
|
|
|
|
m.name.should =~ /piggy/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "lookups with :super" do
|
|
|
|
before do
|
|
|
|
class MyClassyWassy; end
|
|
|
|
class CuteSubclass < MyClassyWassy; end
|
2013-01-11 14:34:48 -05:00
|
|
|
@p = Pry.new
|
|
|
|
@p.binding_stack = [binding]
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.remove_const(:MyClassyWassy)
|
|
|
|
Object.remove_const(:CuteSubclass)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should lookup original class with :super => 0' do
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("CuteSubclass", @p, :super => 0)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.wrapped.should == CuteSubclass
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should lookup immediate super class with :super => 1' do
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("CuteSubclass", @p, :super => 1)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.wrapped.should == MyClassyWassy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should ignore :super parameter for commands' do
|
|
|
|
p = Pry.new
|
|
|
|
p.commands.command "jeremy-jones" do
|
|
|
|
"lobster"
|
|
|
|
end
|
2013-01-11 14:34:48 -05:00
|
|
|
p.binding_stack = [binding]
|
|
|
|
m = Pry::CodeObject.lookup("jeremy-jones", p, :super => 10)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.source.should =~ /lobster/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "precedence" do
|
|
|
|
before do
|
|
|
|
class ClassyWassy
|
|
|
|
class Puff
|
|
|
|
def tiggy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def Puff
|
|
|
|
end
|
|
|
|
|
|
|
|
def piggy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Object.class_eval do
|
|
|
|
def ClassyWassy
|
|
|
|
:ducky
|
|
|
|
end
|
|
|
|
end
|
2013-01-11 14:34:48 -05:00
|
|
|
|
|
|
|
@p = Pry.new
|
|
|
|
@p.binding_stack = [binding]
|
2012-12-19 06:16:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.remove_const(:ClassyWassy)
|
|
|
|
Object.remove_method(:ClassyWassy)
|
|
|
|
end
|
|
|
|
|
2013-09-06 17:58:12 -04:00
|
|
|
it 'should look up classes before methods (at top-level)' do
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("ClassyWassy", @p)
|
2013-09-06 17:58:12 -04:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.source.should =~ /piggy/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should look up methods before classes when ending in () (at top-level)' do
|
|
|
|
m = Pry::CodeObject.lookup("ClassyWassy()", @p)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.is_a?(Pry::Method).should == true
|
|
|
|
m.source.should =~ /ducky/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should look up classes before methods when namespaced' do
|
2013-01-11 14:34:48 -05:00
|
|
|
m = Pry::CodeObject.lookup("ClassyWassy::Puff", @p)
|
2012-12-19 06:16:05 -05:00
|
|
|
m.is_a?(Pry::WrappedModule).should == true
|
|
|
|
m.source.should =~ /tiggy/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should look up locals before methods' do
|
|
|
|
b = Pry.binding_for(ClassyWassy)
|
|
|
|
b.eval("piggy = Puff.new")
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [b]
|
|
|
|
o = Pry::CodeObject.lookup("piggy", @p)
|
2012-12-19 06:16:05 -05:00
|
|
|
o.is_a?(Pry::WrappedModule).should == true
|
|
|
|
end
|
|
|
|
|
2012-12-27 16:53:51 -05:00
|
|
|
# actually locals are never looked up (via co.default_lookup) when they're classes, it
|
2012-12-19 06:16:05 -05:00
|
|
|
# just falls through to co.method_or_class
|
|
|
|
it 'should look up classes before locals' do
|
|
|
|
c = ClassyWassy
|
2013-01-11 14:34:48 -05:00
|
|
|
@p.binding_stack = [binding]
|
|
|
|
o = Pry::CodeObject.lookup("c", @p)
|
2012-12-19 06:16:05 -05:00
|
|
|
o.is_a?(Pry::WrappedModule).should == true
|
|
|
|
o.wrapped.should == ClassyWassy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|