mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
adding back a bunch of tests, almost finished. Also renamed default_prompt to prompt and prompt to get_prompt, etc
This commit is contained in:
parent
12758922e8
commit
878ca414d7
5 changed files with 262 additions and 83 deletions
|
@ -1,5 +1,5 @@
|
||||||
class Pry
|
class Pry
|
||||||
STANDARD_PROMPT = [
|
DEFAULT_PROMPT = [
|
||||||
proc do |v, nest|
|
proc do |v, nest|
|
||||||
if nest == 0
|
if nest == 0
|
||||||
"pry(#{Pry.view(v)})> "
|
"pry(#{Pry.view(v)})> "
|
||||||
|
|
|
@ -50,13 +50,12 @@ class Pry
|
||||||
# :after_session => proc { puts "goodbye" }
|
# :after_session => proc { puts "goodbye" }
|
||||||
attr_accessor :hooks
|
attr_accessor :hooks
|
||||||
|
|
||||||
# Get/Set the array of Procs to be used for the prompts by default by
|
# Get the array of Procs to be used for the prompts by default by
|
||||||
# all Pry instances.
|
# all Pry instances.
|
||||||
# @return [Array<Proc>] The array of Procs to be used for the
|
# @return [Array<Proc>] The array of Procs to be used for the
|
||||||
# prompts by default by all Pry instances.
|
# prompts by default by all Pry instances.
|
||||||
attr_accessor :default_prompt
|
attr_accessor :prompt
|
||||||
end
|
end
|
||||||
|
|
||||||
# Start a Pry REPL.
|
# Start a Pry REPL.
|
||||||
# @param [Object, Binding] target The receiver of the Pry session
|
# @param [Object, Binding] target The receiver of the Pry session
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
|
@ -85,7 +84,7 @@ class Pry
|
||||||
@input = Input.new
|
@input = Input.new
|
||||||
@output = Output.new
|
@output = Output.new
|
||||||
@commands = Commands.new
|
@commands = Commands.new
|
||||||
@default_prompt = STANDARD_PROMPT
|
@prompt = DEFAULT_PROMPT
|
||||||
@print = DEFAULT_PRINT
|
@print = DEFAULT_PRINT
|
||||||
@hooks = DEFAULT_HOOKS
|
@hooks = DEFAULT_HOOKS
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,10 +2,10 @@ class Pry
|
||||||
|
|
||||||
# The list of configuration options.
|
# The list of configuration options.
|
||||||
ConfigOptions = [:input, :output, :commands, :print,
|
ConfigOptions = [:input, :output, :commands, :print,
|
||||||
:default_prompt, :hooks]
|
:prompt, :hooks]
|
||||||
|
|
||||||
attr_accessor *ConfigOptions
|
attr_accessor *ConfigOptions
|
||||||
|
|
||||||
# Create a new `Pry` object.
|
# Create a new `Pry` object.
|
||||||
# @param [Hash] options The optional configuration parameters.
|
# @param [Hash] options The optional configuration parameters.
|
||||||
# @option options [#read] :input The object to use for input. (see input.rb)
|
# @option options [#read] :input The object to use for input. (see input.rb)
|
||||||
|
@ -62,6 +62,8 @@ class Pry
|
||||||
|
|
||||||
exec_hook :before_session, output, target_self
|
exec_hook :before_session, output, target_self
|
||||||
|
|
||||||
|
# cannot rely on nesting.level as
|
||||||
|
# nesting.level changes with new sessions
|
||||||
nesting_level = nesting.size
|
nesting_level = nesting.size
|
||||||
|
|
||||||
Pry.active_instance = self
|
Pry.active_instance = self
|
||||||
|
@ -107,8 +109,14 @@ class Pry
|
||||||
# Pry.new.re(Object.new)
|
# Pry.new.re(Object.new)
|
||||||
def re(target=TOPLEVEL_BINDING)
|
def re(target=TOPLEVEL_BINDING)
|
||||||
target = binding_for(target)
|
target = binding_for(target)
|
||||||
|
|
||||||
|
# eval the expression and save to last_result
|
||||||
Pry.last_result = target.eval r(target)
|
Pry.last_result = target.eval r(target)
|
||||||
|
|
||||||
|
# save the pry instance to active_instance
|
||||||
Pry.active_instance = self
|
Pry.active_instance = self
|
||||||
|
|
||||||
|
# define locals _pry_ and _ (active instance and last expression)
|
||||||
target.eval("_pry_ = Pry.active_instance")
|
target.eval("_pry_ = Pry.active_instance")
|
||||||
target.eval("_ = Pry.last_result")
|
target.eval("_ = Pry.last_result")
|
||||||
rescue SystemExit => e
|
rescue SystemExit => e
|
||||||
|
@ -130,7 +138,7 @@ class Pry
|
||||||
target = binding_for(target)
|
target = binding_for(target)
|
||||||
eval_string = ""
|
eval_string = ""
|
||||||
loop do
|
loop do
|
||||||
val = input.read(prompt(eval_string, target))
|
val = input.read(get_prompt(eval_string.empty?, target.eval('self')))
|
||||||
process_commands(val, eval_string, target)
|
process_commands(val, eval_string, target)
|
||||||
eval_string << "#{val.chomp}\n"
|
eval_string << "#{val.chomp}\n"
|
||||||
|
|
||||||
|
@ -170,17 +178,16 @@ class Pry
|
||||||
|
|
||||||
# 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 [String] eval_string The cumulative lines of input for
|
# @param [Boolean] first_line Whether this is the first line of input
|
||||||
# multi-line input.
|
# (and not multi-line input).
|
||||||
# @param [Object] target The receiver of the Pry session.
|
# @param [Object] target_self The receiver of the Pry session.
|
||||||
# @return [String] The prompt.
|
# @return [String] The prompt.
|
||||||
def prompt(eval_string, target)
|
def get_prompt(first_line, target_self)
|
||||||
target_self = target.eval('self')
|
|
||||||
|
|
||||||
if eval_string.empty?
|
if first_line
|
||||||
default_prompt.first.call(target_self, nesting.level)
|
Array(prompt).first.call(target_self, nesting.level)
|
||||||
else
|
else
|
||||||
default_prompt.last.call(target_self, nesting.level)
|
Array(prompt).last.call(target_self, nesting.level)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
305
test/test.rb
305
test/test.rb
|
@ -105,10 +105,6 @@ describe Pry do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "commands" do
|
describe "commands" do
|
||||||
after do
|
|
||||||
Pry.reset_defaults
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should run command1' do
|
it 'should run command1' do
|
||||||
pry_tester = Pry.new
|
pry_tester = Pry.new
|
||||||
pry_tester.commands = CommandTester.new
|
pry_tester.commands = CommandTester.new
|
||||||
|
@ -126,7 +122,6 @@ describe Pry do
|
||||||
it 'should run command2' do
|
it 'should run command2' do
|
||||||
pry_tester = Pry.new
|
pry_tester = Pry.new
|
||||||
pry_tester.commands = CommandTester.new
|
pry_tester.commands = CommandTester.new
|
||||||
pry_tester.repl
|
|
||||||
pry_tester.input = InputTester.new("command2 horsey", "exit_all")
|
pry_tester.input = InputTester.new("command2 horsey", "exit_all")
|
||||||
pry_tester.commands = CommandTester.new
|
pry_tester.commands = CommandTester.new
|
||||||
|
|
||||||
|
@ -137,75 +132,253 @@ describe Pry do
|
||||||
|
|
||||||
str_output.string.should =~ /horsey/
|
str_output.string.should =~ /horsey/
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "test Pry defaults" do
|
||||||
|
it 'should set the input default, and the default should be overridable' do
|
||||||
|
Pry.input = InputTester.new("5")
|
||||||
|
|
||||||
# commands = {
|
str_output = StringIO.new
|
||||||
# "!" => "refresh",
|
Pry.output = Pry::Output.new(str_output)
|
||||||
# "help" => "show_help",
|
Pry.new.rep
|
||||||
# "nesting" => "show_nesting",
|
str_output.string.should =~ /5/
|
||||||
# "status" => "show_status",
|
|
||||||
# "cat dummy" => "cat",
|
|
||||||
# "cd 3" => "cd",
|
|
||||||
# "ls" => "ls",
|
|
||||||
# "jump_to 0" => "jump_to",
|
|
||||||
# "show_method test_method" => "show_method",
|
|
||||||
# "show_imethod test_method" => "show_method",
|
|
||||||
# "show_doc test_method" => "show_doc",
|
|
||||||
# "show_idoc test_method" => "show_doc"
|
|
||||||
# }
|
|
||||||
|
|
||||||
# commands.each do |command, meth|
|
|
||||||
|
|
||||||
# if RUBY_VERSION =~ /1.8/ && NOT_FOR_RUBY_18.any? { |v| v =~ command }
|
Pry.new(:input => InputTester.new("6")).rep
|
||||||
# next
|
str_output.string.should =~ /6/
|
||||||
# end
|
|
||||||
|
|
||||||
# eval %{
|
Pry.reset_defaults
|
||||||
# it "should invoke output##{meth} when #{command} command entered" do
|
end
|
||||||
# input_strings = ["#{command}", "exit"]
|
|
||||||
# input = InputTester.new(*input_strings)
|
it 'should set the output default, and the default should be overridable' do
|
||||||
# output = OutputTester.new
|
Pry.input = InputTester.new("5", "6", "7")
|
||||||
# o = Class.new
|
|
||||||
|
|
||||||
# pry_tester = Pry.new(:input => input, :output => output)
|
str_output = StringIO.new
|
||||||
# pry_tester.repl(o)
|
Pry.output = Pry::Output.new(str_output)
|
||||||
|
|
||||||
# output.#{meth}_invoked.should == true
|
|
||||||
# output.session_end_invoked.should == true
|
|
||||||
# end
|
|
||||||
# }
|
|
||||||
# end
|
|
||||||
|
|
||||||
# commands.each do |command, meth|
|
|
||||||
|
|
||||||
# if RUBY_VERSION =~ /1.8/ && NOT_FOR_RUBY_18.include?(command)
|
|
||||||
# next
|
|
||||||
# end
|
|
||||||
|
|
||||||
# eval %{
|
|
||||||
# it "should raise when trying to invoke #{command} command with preceding whitespace" do
|
|
||||||
# input_strings = [" #{command}", "exit"]
|
|
||||||
# input = InputTester.new(*input_strings)
|
|
||||||
# output = OutputTester.new
|
|
||||||
# o = Class.new
|
|
||||||
|
|
||||||
# pry_tester = Pry.new(:input => input, :output => output)
|
Pry.new.rep
|
||||||
# pry_tester.repl(o)
|
str_output.string.should =~ /5/
|
||||||
|
|
||||||
# if "#{command}" != "!"
|
Pry.new.rep
|
||||||
# output.output_buffer.is_a?(NameError).should == true
|
str_output.string.should =~ /5\n.*6/
|
||||||
# else
|
|
||||||
|
|
||||||
# # because entering " !" in pry doesnt cause error, it
|
str_output2 = StringIO.new
|
||||||
# # just creates a wait prompt which the subsquent
|
Pry.new(:output => Pry::Output.new(str_output2)).rep
|
||||||
# # "exit" escapes from
|
str_output2.string.should.not =~ /5\n.*6/
|
||||||
# output.output_buffer.should == ""
|
str_output2.string.should =~ /7/
|
||||||
# end
|
|
||||||
# end
|
Pry.reset_defaults
|
||||||
# }
|
end
|
||||||
# end
|
|
||||||
# end
|
it 'should set the commands default, and the default should be overridable' do
|
||||||
|
Pry.reset_defaults
|
||||||
|
|
||||||
|
commands = {
|
||||||
|
"hello" => proc { |opts| opts[:output].puts "hello world"; opts[:val].clear }
|
||||||
|
}
|
||||||
|
|
||||||
|
def commands.commands() self end
|
||||||
|
|
||||||
|
Pry.commands = commands
|
||||||
|
|
||||||
|
str_output = StringIO.new
|
||||||
|
Pry.new(:input => InputTester.new("hello"), :output => Pry::Output.new(str_output)).rep
|
||||||
|
str_output.string.should =~ /hello world/
|
||||||
|
|
||||||
|
commands = {
|
||||||
|
"goodbye" => proc { |opts| opts[:output].puts "goodbye world"; opts[:val].clear }
|
||||||
|
}
|
||||||
|
|
||||||
|
def commands.commands() self end
|
||||||
|
str_output = StringIO.new
|
||||||
|
|
||||||
|
Pry.new(:input => InputTester.new("goodbye"), :output => Pry::Output.new(str_output), :commands => commands).rep
|
||||||
|
str_output.string.should =~ /goodbye world/
|
||||||
|
|
||||||
|
Pry.reset_defaults
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
|
Pry.reset_defaults
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
Pry.new.get_prompt(true, 0).should == "test prompt> "
|
||||||
|
Pry.new.get_prompt(false, 0).should == "test prompt> "
|
||||||
|
|
||||||
|
new_prompt = proc { "A" }
|
||||||
|
pry_tester = Pry.new(:prompt => new_prompt)
|
||||||
|
pry_tester.prompt.should == new_prompt
|
||||||
|
pry_tester.get_prompt(true, 0).should == "A"
|
||||||
|
pry_tester.get_prompt(false, 0).should == "A"
|
||||||
|
|
||||||
|
Pry.new.prompt.should == Pry.prompt
|
||||||
|
Pry.new.get_prompt(true, 0).should == "test prompt> "
|
||||||
|
Pry.new.get_prompt(false, 0).should == "test prompt> "
|
||||||
|
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
|
||||||
|
Pry.new.get_prompt(true, 0).should == "test prompt> "
|
||||||
|
Pry.new.get_prompt(false, 0).should == "test prompt* "
|
||||||
|
|
||||||
|
new_prompt = [proc { "A" }, proc { "B" }]
|
||||||
|
pry_tester = Pry.new(:prompt => new_prompt)
|
||||||
|
pry_tester.prompt.should == new_prompt
|
||||||
|
pry_tester.get_prompt(true, 0).should == "A"
|
||||||
|
pry_tester.get_prompt(false, 0).should == "B"
|
||||||
|
|
||||||
|
Pry.new.prompt.should == Pry.prompt
|
||||||
|
Pry.new.get_prompt(true, 0).should == "test prompt> "
|
||||||
|
Pry.new.get_prompt(false, 0).should == "test prompt* "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should set the hooks default, and the default should be overridable' do
|
||||||
|
Pry.input = InputTester.new("exit")
|
||||||
|
Pry.hooks = {
|
||||||
|
:before_session => proc { |out| out.puts "HELLO" },
|
||||||
|
:after_session => proc { |out| out.puts "BYE" }
|
||||||
|
}
|
||||||
|
|
||||||
|
str_output = StringIO.new
|
||||||
|
Pry.new(:output => Pry::Output.new(str_output)).repl
|
||||||
|
str_output.string.should =~ /HELLO/
|
||||||
|
str_output.string.should =~ /BYE/
|
||||||
|
|
||||||
|
Pry.input.rewind
|
||||||
|
|
||||||
|
str_output = StringIO.new
|
||||||
|
Pry.new(:output => Pry::Output.new(str_output),
|
||||||
|
:hooks => {
|
||||||
|
:before_session => proc { |out| out.puts "MORNING" },
|
||||||
|
:after_session => proc { |out| out.puts "EVENING" }
|
||||||
|
}
|
||||||
|
).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
|
||||||
|
Pry.new(:output => Pry::Output.new(str_output),
|
||||||
|
:hooks => {
|
||||||
|
:before_session => proc { |out| out.puts "OPEN" }
|
||||||
|
}
|
||||||
|
).repl
|
||||||
|
|
||||||
|
str_output.string.should =~ /OPEN/
|
||||||
|
|
||||||
|
Pry.input.rewind
|
||||||
|
str_output = StringIO.new
|
||||||
|
Pry.new(:output => Pry::Output.new(str_output),
|
||||||
|
:hooks => {
|
||||||
|
:after_session => proc { |out| out.puts "CLOSE" }
|
||||||
|
}
|
||||||
|
).repl
|
||||||
|
|
||||||
|
str_output.string.should =~ /CLOSE/
|
||||||
|
|
||||||
|
Pry.reset_defaults
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
# commands = {
|
||||||
|
# "!" => "refresh",
|
||||||
|
# "help" => "show_help",
|
||||||
|
# "nesting" => "show_nesting",
|
||||||
|
# "status" => "show_status",
|
||||||
|
# "cat dummy" => "cat",
|
||||||
|
# "cd 3" => "cd",
|
||||||
|
# "ls" => "ls",
|
||||||
|
# "jump_to 0" => "jump_to",
|
||||||
|
# "show_method test_method" => "show_method",
|
||||||
|
# "show_imethod test_method" => "show_method",
|
||||||
|
# "show_doc test_method" => "show_doc",
|
||||||
|
# "show_idoc test_method" => "show_doc"
|
||||||
|
# }
|
||||||
|
|
||||||
|
# commands.each do |command, meth|
|
||||||
|
|
||||||
|
# if RUBY_VERSION =~ /1.8/ && NOT_FOR_RUBY_18.any? { |v| v =~ command }
|
||||||
|
# next
|
||||||
|
# end
|
||||||
|
|
||||||
|
# eval %{
|
||||||
|
# it "should invoke output##{meth} when #{command} command entered" do
|
||||||
|
# input_strings = ["#{command}", "exit"]
|
||||||
|
# input = InputTester.new(*input_strings)
|
||||||
|
# output = OutputTester.new
|
||||||
|
# o = Class.new
|
||||||
|
|
||||||
|
# pry_tester = Pry.new(:input => input, :output => output)
|
||||||
|
# pry_tester.repl(o)
|
||||||
|
|
||||||
|
# output.#{meth}_invoked.should == true
|
||||||
|
# output.session_end_invoked.should == true
|
||||||
|
# end
|
||||||
|
# }
|
||||||
|
# end
|
||||||
|
|
||||||
|
# commands.each do |command, meth|
|
||||||
|
|
||||||
|
# if RUBY_VERSION =~ /1.8/ && NOT_FOR_RUBY_18.include?(command)
|
||||||
|
# next
|
||||||
|
# end
|
||||||
|
|
||||||
|
# eval %{
|
||||||
|
# it "should raise when trying to invoke #{command} command with preceding whitespace" do
|
||||||
|
# input_strings = [" #{command}", "exit"]
|
||||||
|
# input = InputTester.new(*input_strings)
|
||||||
|
# output = OutputTester.new
|
||||||
|
# o = Class.new
|
||||||
|
|
||||||
|
# pry_tester = Pry.new(:input => input, :output => output)
|
||||||
|
# pry_tester.repl(o)
|
||||||
|
|
||||||
|
# if "#{command}" != "!"
|
||||||
|
# output.output_buffer.is_a?(NameError).should == true
|
||||||
|
# else
|
||||||
|
|
||||||
|
# # because entering " !" in pry doesnt cause error, it
|
||||||
|
# # just creates a wait prompt which the subsquent
|
||||||
|
# # "exit" escapes from
|
||||||
|
# output.output_buffer.should == ""
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
# }
|
||||||
|
# end
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ class InputTester
|
||||||
end
|
end
|
||||||
|
|
||||||
def rewind
|
def rewind
|
||||||
@actions = @orig_actions
|
@actions = @orig_actions.dup
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue