2010-12-21 09:03:52 -05:00
|
|
|
direc = File.dirname(__FILE__)
|
|
|
|
|
2010-12-24 22:21:16 -05:00
|
|
|
require 'rubygems'
|
2010-12-21 09:03:52 -05:00
|
|
|
require 'bacon'
|
|
|
|
require "#{direc}/../lib/pry"
|
|
|
|
require "#{direc}/test_helper"
|
|
|
|
|
2010-12-25 07:05:48 -05:00
|
|
|
puts "Ruby Version #{RUBY_VERSION}"
|
|
|
|
puts "Testing Pry #{Pry::VERSION}"
|
|
|
|
puts "With method_source version #{MethodSource::VERSION}"
|
|
|
|
puts "--"
|
|
|
|
|
2010-12-21 09:03:52 -05:00
|
|
|
describe Pry do
|
|
|
|
describe "open a Pry session on an object" do
|
|
|
|
describe "rep" do
|
2010-12-24 08:22:08 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
class Hello
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.send(:remove_const, :Hello)
|
|
|
|
end
|
|
|
|
|
2010-12-21 09:03:52 -05:00
|
|
|
it 'should set an ivar on an object' do
|
|
|
|
input_string = "@x = 10"
|
|
|
|
input = InputTester.new(input_string)
|
|
|
|
o = Object.new
|
|
|
|
|
2011-01-10 01:29:26 -05:00
|
|
|
pry_tester = Pry.new(:input => input, :output => Pry::NullOutput)
|
2010-12-21 09:03:52 -05:00
|
|
|
pry_tester.rep(o)
|
|
|
|
o.instance_variable_get(:@x).should == 10
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should make self evaluate to the receiver of the rep session' do
|
|
|
|
o = Object.new
|
2011-01-04 22:23:11 -05:00
|
|
|
str_output = StringIO.new
|
2010-12-21 09:03:52 -05:00
|
|
|
|
2011-01-10 01:29:26 -05:00
|
|
|
pry_tester = Pry.new(:input => InputTester.new("self"), :output => str_output)
|
2010-12-21 09:03:52 -05:00
|
|
|
pry_tester.rep(o)
|
2011-01-04 22:23:11 -05:00
|
|
|
str_output.string.should =~ /#{o.to_s}/
|
2010-12-21 09:03:52 -05:00
|
|
|
end
|
2010-12-24 08:22:08 -05:00
|
|
|
|
|
|
|
it 'should work with multi-line input' do
|
2011-01-04 22:23:11 -05:00
|
|
|
o = Object.new
|
|
|
|
str_output = StringIO.new
|
|
|
|
|
2011-01-10 01:29:26 -05:00
|
|
|
pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => str_output)
|
2011-01-04 22:23:11 -05:00
|
|
|
pry_tester.rep(o)
|
|
|
|
str_output.string.should =~ /5/
|
2010-12-24 08:22:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define a nested class under Hello and not on top-level or Pry' do
|
2011-01-10 01:29:26 -05:00
|
|
|
pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => Pry::NullOutput)
|
2010-12-24 08:22:08 -05:00
|
|
|
pry_tester.rep(Hello)
|
2010-12-24 22:21:16 -05:00
|
|
|
Hello.const_defined?(:Nested).should == true
|
2010-12-24 08:22:08 -05:00
|
|
|
end
|
2010-12-21 09:03:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "repl" do
|
2010-12-24 03:30:51 -05:00
|
|
|
describe "basic functionality" do
|
|
|
|
it 'should set an ivar on an object and exit the repl' do
|
|
|
|
input_strings = ["@x = 10", "exit"]
|
|
|
|
input = InputTester.new(*input_strings)
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
|
2011-01-10 01:29:26 -05:00
|
|
|
pry_tester = Pry.start(o, :input => input, :output => Pry::NullOutput)
|
2010-12-24 03:30:51 -05:00
|
|
|
|
|
|
|
o.instance_variable_get(:@x).should == 10
|
2011-01-04 22:23:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should execute start session and end session hooks' do
|
|
|
|
input = InputTester.new("exit")
|
|
|
|
str_output = StringIO.new
|
|
|
|
o = Object.new
|
|
|
|
|
2011-01-10 01:29:26 -05:00
|
|
|
pry_tester = Pry.start(o, :input => input, :output => str_output)
|
2011-01-04 22:23:11 -05:00
|
|
|
str_output.string.should =~ /Beginning.*#{o}/
|
|
|
|
str_output.string.should =~ /Ending.*#{o}/
|
2010-12-24 03:30:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "nesting" do
|
2011-01-10 01:29:26 -05:00
|
|
|
after do
|
|
|
|
Pry.reset_defaults
|
|
|
|
end
|
|
|
|
|
2010-12-24 03:30:51 -05:00
|
|
|
it 'should nest properly' do
|
2011-01-05 17:07:06 -05:00
|
|
|
Pry.input = InputTester.new("pry", "pry", "pry", "\"nest:\#\{Pry.nesting.level\}\"", "exit_all")
|
2010-12-21 09:03:52 -05:00
|
|
|
|
2011-01-04 22:23:11 -05:00
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.output = str_output
|
2010-12-24 03:30:51 -05:00
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
|
2011-01-09 06:51:45 -05:00
|
|
|
pry_tester = o.pry
|
2011-01-04 22:23:11 -05:00
|
|
|
str_output.string.should =~ /nest:3/
|
2010-12-24 03:30:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-23 09:03:39 -05:00
|
|
|
describe "defining methods" do
|
|
|
|
it 'should define a method on the singleton class of an object when performing "def meth;end" inside the object' do
|
|
|
|
[Object.new, {}, []].each do |val|
|
|
|
|
str_input = StringIO.new("def hello;end")
|
|
|
|
Pry.new(:input => str_input, :output => StringIO.new).rep(val)
|
|
|
|
|
|
|
|
val.methods(false).map(&:to_sym).include?(:hello).should == true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define an instance method on the module when performing "def meth;end" inside the module' do
|
|
|
|
str_input = StringIO.new("def hello;end")
|
|
|
|
hello = Module.new
|
|
|
|
Pry.new(:input => str_input, :output => StringIO.new).rep(hello)
|
|
|
|
hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define an instance method on the class when performing "def meth;end" inside the class' do
|
|
|
|
str_input = StringIO.new("def hello;end")
|
|
|
|
hello = Class.new
|
|
|
|
Pry.new(:input => str_input, :output => StringIO.new).rep(hello)
|
|
|
|
hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should define a method on the class of an object when performing "def meth;end" inside an immediate value or Numeric' do
|
|
|
|
# should include float in here, but test fails for some reason
|
|
|
|
# on 1.8.7, no idea why!
|
|
|
|
[:test, 0, true, false, nil].each do |val|
|
|
|
|
str_input = StringIO.new("def hello;end")
|
|
|
|
Pry.new(:input => str_input, :output => StringIO.new).rep(val)
|
|
|
|
val.class.instance_methods(false).map(&:to_sym).include?(:hello).should == true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-01-05 17:07:06 -05:00
|
|
|
describe "commands" do
|
2011-01-23 09:03:39 -05:00
|
|
|
it 'should run a command with no parameter' do
|
2011-01-05 17:07:06 -05:00
|
|
|
pry_tester = Pry.new
|
2011-01-11 20:16:04 -05:00
|
|
|
pry_tester.commands = CommandTester
|
2011-01-05 17:07:06 -05:00
|
|
|
pry_tester.input = InputTester.new("command1", "exit_all")
|
2011-01-11 20:16:04 -05:00
|
|
|
pry_tester.commands = CommandTester
|
2011-01-05 17:07:06 -05:00
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
pry_tester.output = str_output
|
2011-01-05 17:07:06 -05:00
|
|
|
|
|
|
|
pry_tester.rep
|
|
|
|
|
|
|
|
str_output.string.should =~ /command1/
|
|
|
|
end
|
|
|
|
|
2011-01-23 09:03:39 -05:00
|
|
|
it 'should run a command with one parameter' do
|
2011-01-05 17:07:06 -05:00
|
|
|
pry_tester = Pry.new
|
2011-01-11 20:16:04 -05:00
|
|
|
pry_tester.commands = CommandTester
|
2011-01-05 17:07:06 -05:00
|
|
|
pry_tester.input = InputTester.new("command2 horsey", "exit_all")
|
2011-01-11 20:16:04 -05:00
|
|
|
pry_tester.commands = CommandTester
|
2011-01-05 17:07:06 -05:00
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
pry_tester.output = str_output
|
2011-01-05 17:07:06 -05:00
|
|
|
|
|
|
|
pry_tester.rep
|
|
|
|
|
|
|
|
str_output.string.should =~ /horsey/
|
|
|
|
end
|
2011-01-07 07:18:09 -05:00
|
|
|
end
|
2011-01-04 22:23:11 -05:00
|
|
|
|
2011-01-09 06:51:45 -05:00
|
|
|
describe "Object#pry" do
|
|
|
|
|
|
|
|
after do
|
|
|
|
Pry.reset_defaults
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should start a pry session on the receiver (first form)" do
|
|
|
|
Pry.input = InputTester.new("self", "exit")
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.output = str_output
|
2011-01-09 06:51:45 -05:00
|
|
|
|
|
|
|
20.pry
|
|
|
|
|
|
|
|
str_output.string.should =~ /20/
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should start a pry session on the receiver (second form)" do
|
|
|
|
Pry.input = InputTester.new("self", "exit")
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.output = str_output
|
2011-01-09 06:51:45 -05:00
|
|
|
|
|
|
|
pry 20
|
|
|
|
|
|
|
|
str_output.string.should =~ /20/
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should error if more than one argument is passed to Object#pry" do
|
2011-01-10 01:29:26 -05:00
|
|
|
lambda { pry(20, :input => Readline) }.should.raise ArgumentError
|
2011-01-09 06:51:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-16 11:27:55 -05:00
|
|
|
describe "Pry.binding_for" do
|
2011-01-09 06:51:45 -05:00
|
|
|
it 'should return TOPLEVEL_BINDING if parameter self is main' do
|
|
|
|
_main_ = lambda { TOPLEVEL_BINDING.eval('self') }
|
2011-02-16 11:27:55 -05:00
|
|
|
Pry.binding_for(_main_.call).is_a?(Binding).should == true
|
|
|
|
Pry.binding_for(_main_.call).should == TOPLEVEL_BINDING
|
|
|
|
Pry.binding_for(_main_.call).should == Pry.binding_for(_main_.call)
|
2011-01-09 06:51:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-01-07 07:18:09 -05:00
|
|
|
describe "test Pry defaults" do
|
2011-01-09 06:51:45 -05:00
|
|
|
|
|
|
|
after do
|
|
|
|
Pry.reset_defaults
|
|
|
|
end
|
|
|
|
|
2011-01-10 08:54:17 -05:00
|
|
|
describe "input" do
|
2011-01-04 22:23:11 -05:00
|
|
|
|
2011-01-10 08:54:17 -05:00
|
|
|
after do
|
|
|
|
Pry.reset_defaults
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the input default, and the default should be overridable' do
|
|
|
|
Pry.input = InputTester.new("5")
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
|
|
|
Pry.output = str_output
|
|
|
|
Pry.new.rep
|
|
|
|
str_output.string.should =~ /5/
|
|
|
|
|
|
|
|
Pry.new(:input => InputTester.new("6")).rep
|
|
|
|
str_output.string.should =~ /6/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should pass in the prompt if readline arity is 1' do
|
|
|
|
Pry.prompt = proc { "A" }
|
|
|
|
|
|
|
|
arity_one_input = Class.new do
|
|
|
|
attr_accessor :prompt
|
|
|
|
def readline(prompt)
|
|
|
|
@prompt = prompt
|
|
|
|
"exit"
|
|
|
|
end
|
|
|
|
end.new
|
|
|
|
|
|
|
|
Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
|
|
|
|
arity_one_input.prompt.should == Pry.prompt.call
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not pass in the prompt if the arity is 0' do
|
|
|
|
Pry.prompt = proc { "A" }
|
|
|
|
|
|
|
|
arity_zero_input = Class.new do
|
|
|
|
def readline
|
|
|
|
"exit"
|
|
|
|
end
|
|
|
|
end.new
|
|
|
|
|
|
|
|
lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not pass in the prompt if the arity is -1' do
|
|
|
|
Pry.prompt = proc { "A" }
|
|
|
|
|
|
|
|
arity_multi_input = Class.new do
|
|
|
|
attr_accessor :prompt
|
|
|
|
|
|
|
|
def readline(*args)
|
|
|
|
@prompt = args.first
|
|
|
|
"exit"
|
|
|
|
end
|
|
|
|
end.new
|
|
|
|
|
|
|
|
Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
|
|
|
|
arity_multi_input.prompt.should == nil
|
|
|
|
end
|
|
|
|
|
2011-01-07 07:18:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the output default, and the default should be overridable' do
|
|
|
|
Pry.input = InputTester.new("5", "6", "7")
|
2010-12-21 09:03:52 -05:00
|
|
|
|
2011-01-07 07:18:09 -05:00
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.output = str_output
|
2011-01-07 07:18:09 -05:00
|
|
|
|
|
|
|
Pry.new.rep
|
|
|
|
str_output.string.should =~ /5/
|
|
|
|
|
|
|
|
Pry.new.rep
|
|
|
|
str_output.string.should =~ /5\n.*6/
|
|
|
|
|
|
|
|
str_output2 = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.new(:output => str_output2).rep
|
2011-01-07 07:18:09 -05:00
|
|
|
str_output2.string.should.not =~ /5\n.*6/
|
|
|
|
str_output2.string.should =~ /7/
|
|
|
|
end
|
|
|
|
|
2011-01-11 20:16:04 -05:00
|
|
|
describe "commands" do
|
|
|
|
it 'should set the commands default, and the default should be overridable' do
|
|
|
|
class Command0 < Pry::CommandBase
|
|
|
|
command "hello" do
|
2011-01-21 04:17:12 -05:00
|
|
|
output.puts "hello world"
|
2011-01-11 20:16:04 -05:00
|
|
|
end
|
|
|
|
end
|
2011-01-07 07:18:09 -05:00
|
|
|
|
2011-01-11 20:16:04 -05:00
|
|
|
Pry.commands = Command0
|
2011-01-07 07:18:09 -05:00
|
|
|
|
2011-01-11 20:16:04 -05:00
|
|
|
str_output = StringIO.new
|
|
|
|
Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
|
|
|
|
str_output.string.should =~ /hello world/
|
2011-01-07 07:18:09 -05:00
|
|
|
|
2011-01-11 20:16:04 -05:00
|
|
|
class Command1 < Pry::CommandBase
|
2011-01-17 09:38:09 -05:00
|
|
|
command "goodbye", "" do
|
2011-01-21 04:17:12 -05:00
|
|
|
output.puts "goodbye world"
|
2011-01-11 20:16:04 -05:00
|
|
|
end
|
|
|
|
end
|
2011-01-07 07:18:09 -05:00
|
|
|
|
2011-01-11 20:16:04 -05:00
|
|
|
str_output = StringIO.new
|
|
|
|
|
|
|
|
Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => Command1).rep
|
|
|
|
str_output.string.should =~ /goodbye world/
|
2011-01-07 07:18:09 -05:00
|
|
|
|
2011-01-11 20:16:04 -05:00
|
|
|
Object.remove_const(:Command0)
|
|
|
|
Object.remove_const(:Command1)
|
|
|
|
end
|
2011-01-07 07:18:09 -05:00
|
|
|
|
2011-01-11 20:16:04 -05:00
|
|
|
it 'should inherit "help" command from Pry::CommandBase' do
|
|
|
|
class Command2 < Pry::CommandBase
|
2011-01-17 09:38:09 -05:00
|
|
|
command "h", "h command" do
|
2011-01-11 20:16:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Command2.commands.keys.size.should == 2
|
2011-01-20 09:41:41 -05:00
|
|
|
Command2.commands.keys.include?("help").should == true
|
|
|
|
Command2.commands.keys.include?("h").should == true
|
2011-01-11 20:16:04 -05:00
|
|
|
|
|
|
|
Object.remove_const(:Command2)
|
|
|
|
end
|
|
|
|
|
2011-01-20 09:41:41 -05:00
|
|
|
it 'should inherit commands from Pry::Commands' do
|
2011-01-11 20:16:04 -05:00
|
|
|
class Command3 < Pry::Commands
|
|
|
|
command "v" do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-20 09:41:41 -05:00
|
|
|
Command3.commands.include?("nesting").should == true
|
2011-02-16 11:27:55 -05:00
|
|
|
Command3.commands.include?("jump-to").should == true
|
2011-01-20 09:41:41 -05:00
|
|
|
Command3.commands.include?("cd").should == true
|
|
|
|
Command3.commands.include?("v").should == true
|
2011-01-11 20:16:04 -05:00
|
|
|
|
|
|
|
Object.remove_const(:Command3)
|
|
|
|
end
|
2011-01-20 09:41:41 -05:00
|
|
|
|
2011-01-26 13:05:40 -05:00
|
|
|
it 'should alias a command with another command' do
|
|
|
|
class Command6 < Pry::CommandBase
|
|
|
|
alias_command "help2", "help"
|
|
|
|
end
|
|
|
|
|
|
|
|
Command6.commands["help2"].should == Command6.commands["help"]
|
|
|
|
# str_output = StringIO.new
|
|
|
|
# Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command3).rep
|
|
|
|
# str_output.string.should =~ /v command/
|
|
|
|
|
|
|
|
Object.remove_const(:Command6)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should change description of a command using desc' do
|
|
|
|
|
|
|
|
class Command7 < Pry::Commands
|
|
|
|
end
|
|
|
|
|
|
|
|
orig = Command7.commands["help"][:description]
|
|
|
|
|
|
|
|
class Command7
|
|
|
|
desc "help", "blah"
|
|
|
|
end
|
|
|
|
|
|
|
|
Command7.commands["help"][:description].should.not == orig
|
|
|
|
Command7.commands["help"][:description].should == "blah"
|
|
|
|
|
|
|
|
Object.remove_const(:Command7)
|
|
|
|
end
|
|
|
|
|
2011-01-20 09:41:41 -05:00
|
|
|
it 'should run a command from within a command' do
|
2011-01-26 13:05:40 -05:00
|
|
|
class Command01 < Pry::Commands
|
2011-01-20 09:41:41 -05:00
|
|
|
command "v" do
|
|
|
|
output.puts "v command"
|
|
|
|
end
|
|
|
|
|
|
|
|
command "run_v" do
|
|
|
|
run "v"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-01-26 13:05:40 -05:00
|
|
|
Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command01).rep
|
2011-01-20 09:41:41 -05:00
|
|
|
str_output.string.should =~ /v command/
|
|
|
|
|
2011-01-26 13:05:40 -05:00
|
|
|
Object.remove_const(:Command01)
|
2011-01-20 09:41:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
|
|
|
|
class Command3 < Pry::Commands
|
|
|
|
command "v" do
|
|
|
|
output.puts "#{target.eval('self')}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Command4 < Command3
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
|
|
|
Pry.new(:print => proc {}, :input => InputTester.new("v"),
|
|
|
|
:output => str_output, :commands => Command4).rep("john")
|
|
|
|
str_output.string.chomp.should == "john"
|
|
|
|
|
|
|
|
Object.remove_const(:Command3)
|
|
|
|
Object.remove_const(:Command4)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should import commands from another command object' do
|
|
|
|
class Command3 < Pry::CommandBase
|
2011-02-16 11:27:55 -05:00
|
|
|
import_from Pry::Commands, "status", "jump-to"
|
2011-01-20 09:41:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
|
|
|
Pry.new(:print => proc {}, :input => InputTester.new("status"),
|
|
|
|
:output => str_output, :commands => Command3).rep("john")
|
|
|
|
str_output.string.should =~ /Status:/
|
|
|
|
|
|
|
|
Object.remove_const(:Command3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should delete some inherited commands when using delete method' do
|
|
|
|
class Command3 < Pry::Commands
|
|
|
|
command "v" do
|
|
|
|
end
|
|
|
|
|
|
|
|
delete "show_doc", "show_method"
|
|
|
|
delete "ls"
|
|
|
|
end
|
|
|
|
|
|
|
|
Command3.commands.include?("nesting").should == true
|
2011-02-16 11:27:55 -05:00
|
|
|
Command3.commands.include?("jump-to").should == true
|
2011-01-20 09:41:41 -05:00
|
|
|
Command3.commands.include?("cd").should == true
|
|
|
|
Command3.commands.include?("v").should == true
|
|
|
|
Command3.commands.include?("show_doc").should == false
|
|
|
|
Command3.commands.include?("show_method").should == false
|
|
|
|
Command3.commands.include?("ls").should == false
|
|
|
|
|
|
|
|
Object.remove_const(:Command3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should override some inherited commands' do
|
|
|
|
class Command3 < Pry::Commands
|
2011-02-16 11:27:55 -05:00
|
|
|
command "jump-to" do
|
|
|
|
output.puts "jump-to the music"
|
2011-01-20 09:41:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
command "help" do
|
|
|
|
output.puts "help to the music"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# suppress evaluation output
|
|
|
|
Pry.print = proc {}
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-02-16 11:27:55 -05:00
|
|
|
Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => Command3).rep
|
|
|
|
str_output.string.chomp.should == "jump-to the music"
|
2011-01-20 09:41:41 -05:00
|
|
|
|
|
|
|
str_output = StringIO.new
|
|
|
|
Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => Command3).rep
|
|
|
|
str_output.string.chomp.should == "help to the music"
|
|
|
|
|
|
|
|
Object.remove_const(:Command3)
|
|
|
|
|
|
|
|
Pry.reset_defaults
|
|
|
|
end
|
2011-01-11 20:16:04 -05:00
|
|
|
end
|
2011-01-07 07:18:09 -05:00
|
|
|
|
|
|
|
it "should set the print default, and the default should be overridable" do
|
|
|
|
new_print = proc { |out, value| out.puts value }
|
|
|
|
Pry.print = new_print
|
|
|
|
|
|
|
|
Pry.new.print.should == Pry.print
|
|
|
|
str_output = StringIO.new
|
|
|
|
Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
|
|
|
|
str_output.string.should == "test\n"
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
|
|
|
Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
|
|
|
|
:print => proc { |out, value| out.puts value.reverse }).rep
|
|
|
|
str_output.string.should == "tset\n"
|
|
|
|
|
|
|
|
Pry.new.print.should == Pry.print
|
|
|
|
str_output = StringIO.new
|
|
|
|
Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
|
|
|
|
str_output.string.should == "test\n"
|
|
|
|
end
|
2010-12-24 03:30:51 -05:00
|
|
|
|
2011-01-07 07:18:09 -05:00
|
|
|
describe "prompts" do
|
|
|
|
it 'should set the prompt default, and the default should be overridable (single prompt)' do
|
|
|
|
new_prompt = proc { "test prompt> " }
|
|
|
|
Pry.prompt = new_prompt
|
|
|
|
|
|
|
|
Pry.new.prompt.should == Pry.prompt
|
2011-01-09 06:51:45 -05:00
|
|
|
Pry.new.select_prompt(true, 0).should == "test prompt> "
|
|
|
|
Pry.new.select_prompt(false, 0).should == "test prompt> "
|
2011-01-07 07:18:09 -05:00
|
|
|
|
|
|
|
new_prompt = proc { "A" }
|
|
|
|
pry_tester = Pry.new(:prompt => new_prompt)
|
|
|
|
pry_tester.prompt.should == new_prompt
|
2011-01-09 06:51:45 -05:00
|
|
|
pry_tester.select_prompt(true, 0).should == "A"
|
|
|
|
pry_tester.select_prompt(false, 0).should == "A"
|
2011-01-07 07:18:09 -05:00
|
|
|
|
|
|
|
Pry.new.prompt.should == Pry.prompt
|
2011-01-09 06:51:45 -05:00
|
|
|
Pry.new.select_prompt(true, 0).should == "test prompt> "
|
|
|
|
Pry.new.select_prompt(false, 0).should == "test prompt> "
|
2011-01-07 07:18:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the prompt default, and the default should be overridable (multi prompt)' do
|
|
|
|
new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
|
|
|
|
Pry.prompt = new_prompt
|
|
|
|
|
|
|
|
Pry.new.prompt.should == Pry.prompt
|
2011-01-09 06:51:45 -05:00
|
|
|
Pry.new.select_prompt(true, 0).should == "test prompt> "
|
|
|
|
Pry.new.select_prompt(false, 0).should == "test prompt* "
|
2011-01-07 07:18:09 -05:00
|
|
|
|
|
|
|
new_prompt = [proc { "A" }, proc { "B" }]
|
|
|
|
pry_tester = Pry.new(:prompt => new_prompt)
|
|
|
|
pry_tester.prompt.should == new_prompt
|
2011-01-09 06:51:45 -05:00
|
|
|
pry_tester.select_prompt(true, 0).should == "A"
|
|
|
|
pry_tester.select_prompt(false, 0).should == "B"
|
2011-01-07 07:18:09 -05:00
|
|
|
|
|
|
|
Pry.new.prompt.should == Pry.prompt
|
2011-01-09 06:51:45 -05:00
|
|
|
Pry.new.select_prompt(true, 0).should == "test prompt> "
|
|
|
|
Pry.new.select_prompt(false, 0).should == "test prompt* "
|
2011-01-07 07:18:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the hooks default, and the default should be overridable' do
|
|
|
|
Pry.input = InputTester.new("exit")
|
|
|
|
Pry.hooks = {
|
2011-01-09 06:51:45 -05:00
|
|
|
:before_session => proc { |out,_| out.puts "HELLO" },
|
|
|
|
:after_session => proc { |out,_| out.puts "BYE" }
|
2011-01-07 07:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.new(:output => str_output).repl
|
2011-01-07 07:18:09 -05:00
|
|
|
str_output.string.should =~ /HELLO/
|
|
|
|
str_output.string.should =~ /BYE/
|
|
|
|
|
|
|
|
Pry.input.rewind
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.new(:output => str_output,
|
2011-01-07 07:18:09 -05:00
|
|
|
:hooks => {
|
2011-01-09 06:51:45 -05:00
|
|
|
:before_session => proc { |out,_| out.puts "MORNING" },
|
|
|
|
:after_session => proc { |out,_| out.puts "EVENING" }
|
2011-01-07 07:18:09 -05:00
|
|
|
}
|
|
|
|
).repl
|
|
|
|
|
|
|
|
str_output.string.should =~ /MORNING/
|
|
|
|
str_output.string.should =~ /EVENING/
|
|
|
|
|
|
|
|
# try below with just defining one hook
|
|
|
|
Pry.input.rewind
|
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.new(:output => str_output,
|
2011-01-07 07:18:09 -05:00
|
|
|
:hooks => {
|
2011-01-09 06:51:45 -05:00
|
|
|
:before_session => proc { |out,_| out.puts "OPEN" }
|
2011-01-07 07:18:09 -05:00
|
|
|
}
|
|
|
|
).repl
|
2010-12-21 09:03:52 -05:00
|
|
|
|
2011-01-07 07:18:09 -05:00
|
|
|
str_output.string.should =~ /OPEN/
|
|
|
|
|
|
|
|
Pry.input.rewind
|
|
|
|
str_output = StringIO.new
|
2011-01-10 01:29:26 -05:00
|
|
|
Pry.new(:output => str_output,
|
2011-01-07 07:18:09 -05:00
|
|
|
:hooks => {
|
2011-01-09 06:51:45 -05:00
|
|
|
:after_session => proc { |out,_| out.puts "CLOSE" }
|
2011-01-07 07:18:09 -05:00
|
|
|
}
|
|
|
|
).repl
|
|
|
|
|
|
|
|
str_output.string.should =~ /CLOSE/
|
|
|
|
|
|
|
|
Pry.reset_defaults
|
|
|
|
end
|
|
|
|
end
|
2010-12-21 09:03:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|