all tests passing, new functionality in form of: hash args to Pry.start() for :input and :output

This commit is contained in:
John Mair 2010-12-24 21:30:51 +13:00
parent aa03df81ca
commit 5ae3b69667
4 changed files with 155 additions and 35 deletions

View File

@ -9,8 +9,13 @@ require "#{direc}/pry/input"
require "#{direc}/pry/output"
class Pry
def self.start(target=TOPLEVEL_BINDING)
new.repl(target)
def self.start(target=TOPLEVEL_BINDING, options={})
options = {
:input => Pry.input,
:output => Pry.output
}.merge!(options)
new(options[:input], options[:output]).repl(target)
end
def self.view(obj)

81
lib/pry/commands.rb Normal file
View File

@ -0,0 +1,81 @@
class Pry
class Commands
def initialize(val, eval_string, target, output)
@commands = {
"exit_program" => proc { output.exit_program; exit },
"!" => proc { |opts| output.refresh; opts[:eval_string].clear },
"help" => proc { |opts| output.show_help; opts[:eval_string].clear },
"nesting" => proc { |opts| output.show_nesting(opts[:nesting]); opts[:eval_string].clear },
"status" => proc do |opts|
output.show_status(opts[:nesting], opts[:target])
eval_string.clear
end,
"exit_all" => proc { throw(:breakout, 0) },
"exit" => proc do
output.exit
throw(:breakout, opts[:nesting].level)
end,
"ls" => proc do |opts|
output.ls(opts[:target])
opts[:eval_string].clear
end,
/^cat\s+(.+)/ => proc do |opts|
obj = opts[:captures].first
output.cat(opts[:target], obj)
opts[:eval_string].clear
end,
/^cd\s+(.+)/ => proc do |opts|
obj = opts[:captures].first
opts[:target].eval("#{obj}.pry")
opts[:eval_string].clear
end,
/^show_doc\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
doc = opts[:target].eval("method(:#{meth_name})").comment
output.show_doc doc
opts[:eval_string].clear
end,
/^show_idoc\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
doc = opts[:target].eval("instance_method(:#{meth_name})").comment
output.show_doc doc
opts[:eval_string].clear
end,
/^show_method\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
code = opts[:target].eval("method(:#{meth_name})").source
output.show_method code
opts[:eval_string].clear
end,
/^show_imethod\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
code = opts[:target].eval("instance_method(:#{meth_name})").source
output.show_method code
opts[:eval_string].clear
end,
/^jump_to\s*(\d*)/ => proc do |opts|
break_level = opts[:captures].first.to_i
output.jump_to(break_level)
nesting = opts[:nesting]
case break_level
when nesting.level
output.warn_already_at_level(nesting.level)
opts[:eval_string].clear
when (0...nesting.level)
throw(:breakout, break_level + 1)
else
output.err_invalid_nest_level(break_level,
nesting.level - 1)
opts[:eval_string].clear
end
end
end
end
def process_commands(val, eval_string, target)
end
end
end

View File

@ -29,18 +29,54 @@ describe Pry do
end
describe "repl" do
describe "commands" do
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)
output = OutputTester.new
o = Object.new
pry_tester = Pry.new(input, output)
pry_tester.repl(o)
o.instance_variable_get(:@x).should == 10
output.session_end_invoked.should == true
end
end
describe "nesting" do
it 'should nest properly' do
Pry.input = InputTester.new("pry", "pry", "pry", "nesting", "exit", "exit", "exit", "exit")
Pry.output = OutputTester.new
def (Pry.output).show_nesting(level)
class << self; attr_reader :nest_output; end
@nest_output = level.last.first
end
o = Object.new
pry_tester = Pry.new
pry_tester.repl(o)
Pry.output.nest_output.should == 3
end
end
describe "commands" do
before do
Pry.input = InputTester.new("exit")
Pry.output = OutputTester.new
end
after do
Pry.reset_defaults
end
{ "!" => "refresh",
commands = {
"!" => "refresh",
"help" => "show_help",
"nesting" => "show_nesting",
"status" => "show_status",
@ -52,12 +88,14 @@ describe Pry do
"show_doc test_method" => "show_doc",
"show_idoc test_method" => "show_doc",
"jump_to 0" => "jump_to"
}.each do |command, meth|
}
commands.each do |command, meth|
eval %{
it "should invoke output##{meth} when #{command} command entered" do
input_strings = ["#{command}", "exit"]
input = InputTester.new(input_strings)
input = InputTester.new(*input_strings)
output = OutputTester.new
o = Class.new
@ -68,34 +106,31 @@ describe Pry do
output.session_end_invoked.should == true
end
}
end
commands.each do |command, meth|
# it "should invoke output#help when help command entered" do
# input_strings = ["help", "exit"]
# input = InputTester.new(input_strings)
# output = OutputTester.new
# o = Object.new
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, output)
# pry_tester.repl(o)
pry_tester = Pry.new(input, output)
pry_tester.repl(o)
# output.show_help_invoked.should == true
# output.session_end_invoked.should == true
# end
if "#{command}" != "!"
output.output_buffer.is_a?(NameError).should == true
else
# it 'should set an ivar on an object and exit the repl' do
# input_strings = ["@x = 10", "exit"]
# input = InputTester.new(input_strings)
# output = OutputTester.new
# o = Object.new
# pry_tester = Pry.new(input, output)
# pry_tester.repl(o)
# o.instance_variable_get(:@x).should == 10
# output.session_end_invoked.should == true
# end
# 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

View File

@ -4,9 +4,9 @@ class Object
end
class InputTester
def initialize(actions)
@orig_actions = Array(actions.dup)
@actions = Array(actions)
def initialize(*actions)
@orig_actions = actions.dup
@actions = actions
end
def read(*)
@ -27,7 +27,6 @@ class OutputTester
def print(val)
@output_buffer = val
puts val.inspect
end
def method_missing(meth_name, *args, &block)