2011-08-17 04:53:20 +00:00
|
|
|
require 'helper'
|
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
def new_completer(bind, pry=nil)
|
|
|
|
Pry::InputCompleter.build_completion_proc(Pry.binding_for(bind), pry)
|
|
|
|
end
|
|
|
|
|
|
|
|
def completer_test(bind, pry=nil, assert_flag=true)
|
|
|
|
completer = new_completer(bind, pry)
|
|
|
|
test = proc {|symbol| completer.call(symbol[0..-2]).include?(symbol).should == assert_flag}
|
|
|
|
return proc {|*symbols| symbols.each(&test) }
|
|
|
|
end
|
|
|
|
|
2012-10-21 05:54:33 +00:00
|
|
|
if defined?(Bond) && Readline::VERSION !~ /editline/i
|
2012-09-06 05:39:29 +00:00
|
|
|
describe 'bond-based completion' do
|
|
|
|
it 'should pull in Bond by default' do
|
|
|
|
Pry.config.completer.should == Pry::BondCompleter
|
|
|
|
end
|
2012-08-18 22:49:20 +00:00
|
|
|
end
|
|
|
|
end
|
2012-08-12 04:38:32 +00:00
|
|
|
|
2011-08-17 04:53:20 +00:00
|
|
|
describe Pry::InputCompleter do
|
|
|
|
|
|
|
|
before do
|
|
|
|
# The AMQP gem has some classes like this:
|
|
|
|
# pry(main)> AMQP::Protocol::Test::ContentOk.name
|
|
|
|
# => :content_ok
|
|
|
|
module SymbolyName
|
|
|
|
def self.name; :symboly_name; end
|
|
|
|
end
|
2012-08-18 22:49:20 +00:00
|
|
|
|
|
|
|
$default_completer = Pry.config.completer
|
|
|
|
Pry.config.completer = Pry::InputCompleter
|
2011-08-17 04:53:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2012-08-18 22:49:20 +00:00
|
|
|
Pry.config.completer = $default_completer
|
2011-08-17 04:53:20 +00:00
|
|
|
Object.remove_const :SymbolyName
|
|
|
|
end
|
|
|
|
|
2011-09-06 17:20:04 +00:00
|
|
|
# another jruby hack :((
|
2011-12-27 22:38:25 +00:00
|
|
|
if !Pry::Helpers::BaseHelpers.jruby?
|
2011-09-06 17:20:04 +00:00
|
|
|
it "should not crash if there's a Module that has a symbolic name." do
|
|
|
|
completer = Pry::InputCompleter.build_completion_proc(Pry.binding_for(Object.new))
|
|
|
|
lambda{ completer.call "a.to_s." }.should.not.raise Exception
|
|
|
|
end
|
2011-08-17 04:53:20 +00:00
|
|
|
end
|
2012-06-19 10:32:03 +00:00
|
|
|
|
2012-06-19 09:40:58 +00:00
|
|
|
it 'should take parenthesis and other characters into account for symbols' do
|
|
|
|
b = Pry.binding_for(Object.new)
|
|
|
|
completer = Pry::InputCompleter.build_completion_proc(b)
|
|
|
|
|
|
|
|
lambda { completer.call(":class)") }.should.not.raise(RegexpError)
|
|
|
|
end
|
2012-06-19 13:47:52 +00:00
|
|
|
|
2012-06-19 10:32:03 +00:00
|
|
|
it 'should complete instance variables' do
|
2012-09-06 05:56:45 +00:00
|
|
|
object = Class.new.new
|
2012-06-19 10:32:03 +00:00
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
# set variables in appropriate scope
|
2012-06-19 10:32:03 +00:00
|
|
|
object.instance_variable_set(:'@name', 'Pry')
|
2012-06-19 11:09:55 +00:00
|
|
|
object.class.send(:class_variable_set, :'@@number', 10)
|
2012-06-19 10:32:03 +00:00
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
# check to see if variables are in scope
|
|
|
|
object.instance_variables.
|
|
|
|
map { |v| v.to_sym }.
|
|
|
|
include?(:'@name').should == true
|
2012-08-10 02:32:23 +00:00
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
object.class.class_variables.
|
|
|
|
map { |v| v.to_sym }.
|
|
|
|
include?(:'@@number').should == true
|
2012-06-19 10:32:03 +00:00
|
|
|
|
|
|
|
# Complete instance variables.
|
2012-08-12 04:38:32 +00:00
|
|
|
b = Pry.binding_for(object)
|
|
|
|
completer_test(b).call('@name', '@name.downcase')
|
2012-06-19 10:32:03 +00:00
|
|
|
|
|
|
|
# Complete class variables.
|
2012-08-12 04:38:32 +00:00
|
|
|
b = Pry.binding_for(object.class)
|
|
|
|
completer_test(b).call('@@number', '@@number.class')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
it 'should complete for stdlib symbols' do
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
# Regexp
|
|
|
|
completer_test(o).call('/foo/.extend')
|
|
|
|
|
|
|
|
# Array
|
|
|
|
completer_test(o).call('[1].push')
|
2013-01-14 12:36:57 +00:00
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
# Hash
|
|
|
|
completer_test(o).call('{"a" => "b"}.keys')
|
2013-01-14 12:36:57 +00:00
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
# Proc
|
|
|
|
completer_test(o).call('{2}.call')
|
|
|
|
|
|
|
|
# Symbol
|
|
|
|
completer_test(o).call(':symbol.to_s')
|
|
|
|
|
|
|
|
# Absolute Constant
|
|
|
|
completer_test(o).call('::IndexError')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should complete for target symbols' do
|
|
|
|
o = Object.new
|
|
|
|
|
|
|
|
# Constant
|
|
|
|
module Mod
|
|
|
|
Con = 'Constant'
|
|
|
|
module Mod2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
completer_test(Mod).call('Con')
|
|
|
|
|
|
|
|
# Constants or Class Methods
|
|
|
|
completer_test(o).call('Mod::Con')
|
|
|
|
|
|
|
|
# Symbol
|
|
|
|
foo = :symbol
|
|
|
|
completer_test(o).call(':symbol')
|
2012-06-19 10:32:03 +00:00
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
# Variables
|
|
|
|
class << o
|
|
|
|
attr_accessor :foo
|
|
|
|
end
|
|
|
|
o.foo = 'bar'
|
|
|
|
completer_test(binding).call('o.foo')
|
|
|
|
|
|
|
|
# trailing slash
|
|
|
|
new_completer(Mod).call('Mod2/').include?('Mod2/').should == true
|
2012-06-19 10:32:03 +00:00
|
|
|
end
|
2012-08-12 04:38:32 +00:00
|
|
|
|
|
|
|
it 'should complete for arbitrary scopes' do
|
|
|
|
module Bar
|
|
|
|
@barvar = :bar
|
|
|
|
end
|
|
|
|
|
|
|
|
module Baz
|
|
|
|
@bar = Bar
|
|
|
|
@bazvar = :baz
|
|
|
|
Con = :constant
|
|
|
|
end
|
|
|
|
|
|
|
|
pry = Pry.new()
|
|
|
|
stack = pry.binding_stack
|
|
|
|
stack.push(Pry.binding_for(Baz))
|
|
|
|
stack.push(Pry.binding_for(Bar))
|
|
|
|
|
|
|
|
b = Pry.binding_for(Bar)
|
|
|
|
completer_test(b, pry).call("../@bazvar")
|
|
|
|
completer_test(b, pry).call('/Con')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should complete for stdlib symbols' do
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
# Regexp
|
|
|
|
completer_test(o).call('/foo/.extend')
|
|
|
|
|
|
|
|
# Array
|
|
|
|
completer_test(o).call('[1].push')
|
2013-01-14 12:36:57 +00:00
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
# Hash
|
|
|
|
completer_test(o).call('{"a" => "b"}.keys')
|
2013-01-14 12:36:57 +00:00
|
|
|
|
2012-08-12 04:38:32 +00:00
|
|
|
# Proc
|
|
|
|
completer_test(o).call('{2}.call')
|
|
|
|
|
|
|
|
# Symbol
|
|
|
|
completer_test(o).call(':symbol.to_s')
|
|
|
|
|
|
|
|
# Absolute Constant
|
|
|
|
completer_test(o).call('::IndexError')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should complete for target symbols' do
|
|
|
|
o = Object.new
|
|
|
|
|
|
|
|
# Constant
|
|
|
|
module Mod
|
|
|
|
Con = 'Constant'
|
|
|
|
module Mod2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
completer_test(Mod).call('Con')
|
|
|
|
|
|
|
|
# Constants or Class Methods
|
|
|
|
completer_test(o).call('Mod::Con')
|
|
|
|
|
|
|
|
# Symbol
|
|
|
|
foo = :symbol
|
|
|
|
completer_test(o).call(':symbol')
|
|
|
|
|
|
|
|
# Variables
|
|
|
|
class << o
|
|
|
|
attr_accessor :foo
|
|
|
|
end
|
|
|
|
o.foo = 'bar'
|
|
|
|
completer_test(binding).call('o.foo')
|
|
|
|
|
|
|
|
# trailing slash
|
|
|
|
new_completer(Mod).call('Mod2/').include?('Mod2/').should == true
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should complete for arbitrary scopes' do
|
|
|
|
module Bar
|
|
|
|
@barvar = :bar
|
|
|
|
end
|
|
|
|
|
|
|
|
module Baz
|
|
|
|
@bar = Bar
|
|
|
|
@bazvar = :baz
|
|
|
|
Con = :constant
|
|
|
|
end
|
|
|
|
|
|
|
|
pry = Pry.new()
|
|
|
|
stack = pry.binding_stack
|
|
|
|
stack.push(Pry.binding_for(Baz))
|
|
|
|
stack.push(Pry.binding_for(Bar))
|
|
|
|
|
|
|
|
b = Pry.binding_for(Bar)
|
|
|
|
completer_test(b, pry).call("../@bazvar")
|
|
|
|
completer_test(b, pry).call('/Con')
|
|
|
|
end
|
|
|
|
|
2013-01-14 12:36:57 +00:00
|
|
|
it 'should not return nil in its output' do
|
|
|
|
pry = Pry.new
|
|
|
|
new_completer(binding, pry).call("pry.").should.not.include nil
|
|
|
|
end
|
2011-08-17 04:53:20 +00:00
|
|
|
end
|