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

moved input select logic to Pry#readline; updated tests to reflect that Pry.input and Pry.output now accept duck-typed IOs

This commit is contained in:
John Mair 2011-01-10 19:29:26 +13:00
parent 70bc1222e2
commit 1666945e4b
4 changed files with 50 additions and 58 deletions

View file

@ -2,15 +2,6 @@ require 'readline'
class Pry class Pry
# default input class - uses Readline.
class Input
trap('INT') { exit }
def readline(prompt)
Readline.readline(prompt, true)
end
end
# read from any IO-alike # read from any IO-alike
class IOInput class IOInput
def initialize(io) def initialize(io)

View file

@ -1,20 +1,7 @@
class Pry class Pry
# default output class - just writes to STDOUT
class Output
attr_reader :out
def initialize(out=STDOUT)
@out = out
end
def puts(value)
out.puts value
end
end
# null output class - doesn't write anywwhere. # null output class - doesn't write anywwhere.
class NullOutput class NullOutput
def puts(*) end def self.puts(*) end
end end
end end

View file

@ -83,7 +83,7 @@ class Pry
exec_hook :after_session, output, target_self exec_hook :after_session, output, target_self
# we only enter here if :breakout has been thrown # keep throwing until we reach the desired nesting level
if nesting_level != break_level if nesting_level != break_level
throw :breakout, break_level throw :breakout, break_level
end end
@ -137,18 +137,11 @@ class Pry
def r(target=TOPLEVEL_BINDING) def r(target=TOPLEVEL_BINDING)
target = binding_for(target) target = binding_for(target)
eval_string = "" eval_string = ""
loop do loop do
current_prompt = select_prompt(eval_string.empty?, target.eval('self'))
val = if input == Readline val = readline(current_prompt)
input.readline(select_prompt(eval_string.empty?, target.eval('self')), true)
else
if input.method(:readline).arity == 1
input.readline(select_prompt(eval_string.empty?, target.eval('self')))
else
input.readline
end
end
val.chomp! val.chomp!
process_commands(val, eval_string, target) process_commands(val, eval_string, target)
@ -188,6 +181,26 @@ class Pry
end end
end end
# Returns the next line of input to be used by the pry instance.
# This method should not need to be invoked directly.
# @param [String] current_prompt The prompt to use for input.
# @return [String] The next line of input.
def readline(current_prompt)
if input == Readline
# Readline must be treated differently
# as it has a second parameter.
input.readline(current_prompt, true)
else
if input.method(:readline).arity == 1
input.readline(current_prompt)
else
input.readline
end
end
end
# Returns the appropriate prompt to use. # Returns the appropriate prompt to use.
# This method should not need to be invoked directly. # This method should not need to be invoked directly.
# @param [Boolean] first_line Whether this is the first line of input # @param [Boolean] first_line Whether this is the first line of input

View file

@ -30,7 +30,7 @@ describe Pry do
input = InputTester.new(input_string) input = InputTester.new(input_string)
o = Object.new o = Object.new
pry_tester = Pry.new(:input => input, :output => Pry::NullOutput.new) pry_tester = Pry.new(:input => input, :output => Pry::NullOutput)
pry_tester.rep(o) pry_tester.rep(o)
o.instance_variable_get(:@x).should == 10 o.instance_variable_get(:@x).should == 10
end end
@ -39,7 +39,7 @@ describe Pry do
o = Object.new o = Object.new
str_output = StringIO.new str_output = StringIO.new
pry_tester = Pry.new(:input => InputTester.new("self"), :output => Pry::Output.new(str_output)) pry_tester = Pry.new(:input => InputTester.new("self"), :output => str_output)
pry_tester.rep(o) pry_tester.rep(o)
str_output.string.should =~ /#{o.to_s}/ str_output.string.should =~ /#{o.to_s}/
end end
@ -48,13 +48,13 @@ describe Pry do
o = Object.new o = Object.new
str_output = StringIO.new str_output = StringIO.new
pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => Pry::Output.new(str_output)) pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => str_output)
pry_tester.rep(o) pry_tester.rep(o)
str_output.string.should =~ /5/ str_output.string.should =~ /5/
end end
it 'should define a nested class under Hello and not on top-level or Pry' do it 'should define a nested class under Hello and not on top-level or Pry' do
pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => Pry::NullOutput.new) pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => Pry::NullOutput)
pry_tester.rep(Hello) pry_tester.rep(Hello)
Hello.const_defined?(:Nested).should == true Hello.const_defined?(:Nested).should == true
end end
@ -68,7 +68,7 @@ describe Pry do
o = Object.new o = Object.new
pry_tester = Pry.start(o, :input => input, :output => Pry::NullOutput.new) pry_tester = Pry.start(o, :input => input, :output => Pry::NullOutput)
o.instance_variable_get(:@x).should == 10 o.instance_variable_get(:@x).should == 10
end end
@ -78,26 +78,27 @@ describe Pry do
str_output = StringIO.new str_output = StringIO.new
o = Object.new o = Object.new
pry_tester = Pry.start(o, :input => input, :output => Pry::Output.new(str_output)) pry_tester = Pry.start(o, :input => input, :output => str_output)
str_output.string.should =~ /Beginning.*#{o}/ str_output.string.should =~ /Beginning.*#{o}/
str_output.string.should =~ /Ending.*#{o}/ str_output.string.should =~ /Ending.*#{o}/
end end
end end
describe "nesting" do describe "nesting" do
after do
Pry.reset_defaults
end
it 'should nest properly' do it 'should nest properly' do
Pry.input = InputTester.new("pry", "pry", "pry", "\"nest:\#\{Pry.nesting.level\}\"", "exit_all") Pry.input = InputTester.new("pry", "pry", "pry", "\"nest:\#\{Pry.nesting.level\}\"", "exit_all")
str_output = StringIO.new str_output = StringIO.new
Pry.output = Pry::Output.new(str_output) Pry.output = str_output
o = Object.new o = Object.new
pry_tester = o.pry pry_tester = o.pry
str_output.string.should =~ /nest:3/ str_output.string.should =~ /nest:3/
Pry.input = Pry::Input.new
Pry.output = Pry::Output.new
end end
end end
@ -109,7 +110,7 @@ describe Pry do
pry_tester.commands = CommandTester.new pry_tester.commands = CommandTester.new
str_output = StringIO.new str_output = StringIO.new
pry_tester.output = Pry::Output.new(str_output) pry_tester.output = str_output
pry_tester.rep pry_tester.rep
@ -123,7 +124,7 @@ describe Pry do
pry_tester.commands = CommandTester.new pry_tester.commands = CommandTester.new
str_output = StringIO.new str_output = StringIO.new
pry_tester.output = Pry::Output.new(str_output) pry_tester.output = str_output
pry_tester.rep pry_tester.rep
@ -141,7 +142,7 @@ describe Pry do
Pry.input = InputTester.new("self", "exit") Pry.input = InputTester.new("self", "exit")
str_output = StringIO.new str_output = StringIO.new
Pry.output = Pry::Output.new(str_output) Pry.output = str_output
20.pry 20.pry
@ -152,7 +153,7 @@ describe Pry do
Pry.input = InputTester.new("self", "exit") Pry.input = InputTester.new("self", "exit")
str_output = StringIO.new str_output = StringIO.new
Pry.output = Pry::Output.new(str_output) Pry.output = str_output
pry 20 pry 20
@ -160,7 +161,7 @@ describe Pry do
end end
it "should error if more than one argument is passed to Object#pry" do it "should error if more than one argument is passed to Object#pry" do
lambda { pry(20, :input => Pry::Input.new) }.should.raise ArgumentError lambda { pry(20, :input => Readline) }.should.raise ArgumentError
end end
end end
@ -184,7 +185,7 @@ describe Pry do
Pry.input = InputTester.new("5") Pry.input = InputTester.new("5")
str_output = StringIO.new str_output = StringIO.new
Pry.output = Pry::Output.new(str_output) Pry.output = str_output
Pry.new.rep Pry.new.rep
str_output.string.should =~ /5/ str_output.string.should =~ /5/
@ -196,7 +197,7 @@ describe Pry do
Pry.input = InputTester.new("5", "6", "7") Pry.input = InputTester.new("5", "6", "7")
str_output = StringIO.new str_output = StringIO.new
Pry.output = Pry::Output.new(str_output) Pry.output = str_output
Pry.new.rep Pry.new.rep
str_output.string.should =~ /5/ str_output.string.should =~ /5/
@ -205,7 +206,7 @@ describe Pry do
str_output.string.should =~ /5\n.*6/ str_output.string.should =~ /5\n.*6/
str_output2 = StringIO.new str_output2 = StringIO.new
Pry.new(:output => Pry::Output.new(str_output2)).rep Pry.new(:output => str_output2).rep
str_output2.string.should.not =~ /5\n.*6/ str_output2.string.should.not =~ /5\n.*6/
str_output2.string.should =~ /7/ str_output2.string.should =~ /7/
end end
@ -220,7 +221,7 @@ describe Pry do
Pry.commands = commands Pry.commands = commands
str_output = StringIO.new str_output = StringIO.new
Pry.new(:input => InputTester.new("hello"), :output => Pry::Output.new(str_output)).rep Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
str_output.string.should =~ /hello world/ str_output.string.should =~ /hello world/
commands = { commands = {
@ -230,7 +231,7 @@ describe Pry do
def commands.commands() self end def commands.commands() self end
str_output = StringIO.new str_output = StringIO.new
Pry.new(:input => InputTester.new("goodbye"), :output => Pry::Output.new(str_output), :commands => commands).rep Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => commands).rep
str_output.string.should =~ /goodbye world/ str_output.string.should =~ /goodbye world/
end end
@ -303,14 +304,14 @@ describe Pry do
} }
str_output = StringIO.new str_output = StringIO.new
Pry.new(:output => Pry::Output.new(str_output)).repl Pry.new(:output => str_output).repl
str_output.string.should =~ /HELLO/ str_output.string.should =~ /HELLO/
str_output.string.should =~ /BYE/ str_output.string.should =~ /BYE/
Pry.input.rewind Pry.input.rewind
str_output = StringIO.new str_output = StringIO.new
Pry.new(:output => Pry::Output.new(str_output), Pry.new(:output => str_output,
:hooks => { :hooks => {
:before_session => proc { |out,_| out.puts "MORNING" }, :before_session => proc { |out,_| out.puts "MORNING" },
:after_session => proc { |out,_| out.puts "EVENING" } :after_session => proc { |out,_| out.puts "EVENING" }
@ -323,7 +324,7 @@ describe Pry do
# try below with just defining one hook # try below with just defining one hook
Pry.input.rewind Pry.input.rewind
str_output = StringIO.new str_output = StringIO.new
Pry.new(:output => Pry::Output.new(str_output), Pry.new(:output => str_output,
:hooks => { :hooks => {
:before_session => proc { |out,_| out.puts "OPEN" } :before_session => proc { |out,_| out.puts "OPEN" }
} }
@ -333,7 +334,7 @@ describe Pry do
Pry.input.rewind Pry.input.rewind
str_output = StringIO.new str_output = StringIO.new
Pry.new(:output => Pry::Output.new(str_output), Pry.new(:output => str_output,
:hooks => { :hooks => {
:after_session => proc { |out,_| out.puts "CLOSE" } :after_session => proc { |out,_| out.puts "CLOSE" }
} }