From f091b4fa9bf3441eb76667d413b176d8e5ab0b69 Mon Sep 17 00:00:00 2001 From: John Mair Date: Thu, 2 Jun 2011 04:21:55 +1200 Subject: [PATCH 01/21] can display core source on rbx (when installed with rvm) --- lib/pry/helpers/command_helpers.rb | 37 ++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index d42dc3fa..0b325ff9 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -48,6 +48,36 @@ class Pry end end + ########### RBX HELPERS ############# + def rbx_core?(meth) + defined?(RUBY_ENGINE) && + RUBY_ENGINE =~ /rbx/ && + meth.source_location && + meth.source_location.first.start_with?("kernel") + end + + def rvm_ruby?(path) + !!(path =~ /\.rvm/) + end + + def rbx_core_code_for(meth) + if rvm_ruby?(Rubinius::BIN_PATH) + rvm_rbx_core_code_for(meth) + else + # NOT implemented + end + end + + def rvm_rbx_core_code_for(meth) + ruby_name = File.dirname(Rubinius::BIN_PATH).split("/").last + source_path = File.join(File.dirname(File.dirname(File.dirname(Rubinius::BIN_PATH))), "src", ruby_name) + file_name = File.join(source_path, meth.source_location.first) + start_line = meth.source_location.last + MethodSource.source_helper([file_name, start_line]) + end + + ######### END RBX HELPERS ############### + def code_and_code_type_for(meth) case code_type = code_type_for(meth) when nil @@ -57,12 +87,15 @@ class Pry code = strip_comments_from_c_code(code) when :ruby if meth.source_location.first == Pry.eval_path - start_line = meth.source_location.last p = Pry.new(:input => StringIO.new(Pry.line_buffer[start_line..-1].join)).r(target) code = strip_leading_whitespace(p) else - code = strip_leading_whitespace(meth.source) + if rbx_core?(meth) + code = strip_leading_whitespace(rbx_core_code_for(meth)) + else + code = strip_leading_whitespace(meth.source) + end end set_file_and_dir_locals(meth.source_location.first) end From a8f152ecacb06b0c3255ca7ff6eacfef9f5920ad Mon Sep 17 00:00:00 2001 From: John Mair Date: Thu, 2 Jun 2011 04:23:17 +1200 Subject: [PATCH 02/21] fixed a failing test for rbx (actually a failing test in general, but only spotted thanks to rbx) --- test/test_default_commands.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_default_commands.rb b/test/test_default_commands.rb index 1ac369ff..dc92230b 100644 --- a/test/test_default_commands.rb +++ b/test/test_default_commands.rb @@ -225,14 +225,16 @@ describe "Pry::Commands" do $str_output = StringIO.new o = Object.new + + # sample comment def o.sample redirect_pry_io(InputTester.new("show-doc", "exit-all"), $str_output) do binding.pry - end + end end o.sample - $str_output.string.should =~ /sample doc/ + $str_output.string.should =~ /sample comment/ $str_output = nil end end From eb9238ab2be10090c98d843c007f1377679a94a7 Mon Sep 17 00:00:00 2001 From: John Mair Date: Thu, 2 Jun 2011 04:24:15 +1200 Subject: [PATCH 03/21] fixed . error message (dest local not found). Interpolation still not working yet --- lib/pry/default_commands/shell.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/pry/default_commands/shell.rb b/lib/pry/default_commands/shell.rb index cd01056a..f04d1fbf 100644 --- a/lib/pry/default_commands/shell.rb +++ b/lib/pry/default_commands/shell.rb @@ -5,8 +5,9 @@ class Pry command /\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => "." do |cmd| if cmd =~ /^cd\s+(.+)/i + dest = $1 begin - Dir.chdir File.expand_path($1) + Dir.chdir File.expand_path(dest) rescue Errno::ENOENT output.puts "No such directory: #{dest}" end @@ -72,8 +73,6 @@ class Pry render_output(opts.flood?, opts.l? ? normalized_start_line + 1 : false, contents) contents end - - end end From 94a1f993e6d18bb64f6a5fec8cb232c3e8ba30b6 Mon Sep 17 00:00:00 2001 From: John Mair Date: Thu, 2 Jun 2011 21:41:41 +1200 Subject: [PATCH 04/21] Replaced Pry.config.should_load_plugins with Pry.config.plugins.enabled. Also added Pry.config.plugins.strict_loading config option; also updated test helper to reflect changes --- lib/pry/config.rb | 7 +++++++ lib/pry/pry_class.rb | 7 +++++-- test/helper.rb | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/pry/config.rb b/lib/pry/config.rb index bcd9370d..acb61c8b 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -81,6 +81,13 @@ class Pry # saved to hist.file at session end. # @return [OpenStruct] attr_accessor :history + + # Config option for plugins: + # sub-options include: + # `plugins.enabled` (Boolean) to toggle the loading of plugins on and off wholesale. (defaults to true) + # `plugins.strict_loading` (Boolean) which toggles whether referring to a non-existent plugin should raise an exception (defaults to `false`) + # @return [OpenStruct] + attr_accessor :plugins end end diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index a836e005..5a2fe3d1 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -96,7 +96,7 @@ class Pry # we only want them loaded once per entire Pry lifetime, not # multiple times per each new session (i.e in debugging) load_rc if Pry.config.should_load_rc - load_plugins if Pry.config.should_load_plugins + load_plugins if Pry.config.plugins.enabled load_history if Pry.config.history.load @initial_session = false @@ -190,7 +190,10 @@ class Pry config.pager = true config.editor = default_editor_for_platform config.should_load_rc = true - config.should_load_plugins = true + + config.plugins ||= OpenStruct.new + config.plugins.enabled = true + config.plugins.strict_loading = true config.history ||= OpenStruct.new config.history.save = true diff --git a/test/helper.rb b/test/helper.rb index 237e5ec8..9aae3980 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -18,7 +18,7 @@ class << Pry Pry.color = false Pry.pager = false Pry.config.should_load_rc = false - Pry.config.should_load_plugins = false + Pry.config.plugins.enabled = false Pry.config.history.load = false Pry.config.history.save = false end From 688863021241f34441afc89ec33910ef62f40872 Mon Sep 17 00:00:00 2001 From: John Mair Date: Thu, 2 Jun 2011 21:45:53 +1200 Subject: [PATCH 05/21] plugin activation/disabling errors can be turned off using Pry.config.plugins.strict_loading = false --- lib/pry/plugins.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pry/plugins.rb b/lib/pry/plugins.rb index b8c4f4b6..942dd1d6 100644 --- a/lib/pry/plugins.rb +++ b/lib/pry/plugins.rb @@ -1,10 +1,10 @@ class Pry class PluginManager - PRY_PLUGIN_PREFIX = /^pry-/ - PluginNotFound = Class.new(LoadError) + MessageSink = Object.new.tap { |o| def o.method_missing(*args) end } + class Plugin attr_accessor :name, :gem_name, :enabled, :active @@ -55,7 +55,7 @@ class Pry # @return [Hash] A hash with all plugin names (minus the 'pry-') as # keys and Plugin objects as values. def plugins - h = {} + h = Pry.config.plugins.strict_loading ? {} : Hash.new { MessageSink } @plugins.each do |plugin| h[plugin.name] = plugin end From 61bb4d9a17a23c51d92029b9554089ec8baba406 Mon Sep 17 00:00:00 2001 From: John Mair Date: Thu, 2 Jun 2011 23:37:28 +1200 Subject: [PATCH 06/21] added std rbx folder search and doc search for rbx core methods --- lib/pry/helpers/command_helpers.rb | 42 +++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 0b325ff9..e3313f4a 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -61,19 +61,46 @@ class Pry end def rbx_core_code_for(meth) + rbx_core_code_or_doc_for(meth, :code) + end + + def rbx_core_doc_for(meth) + rbx_core_code_or_doc_for(meth, :doc) + end + + def rbx_core_code_or_doc_for(meth, code_or_doc) if rvm_ruby?(Rubinius::BIN_PATH) - rvm_rbx_core_code_for(meth) + rvm_rbx_core_code_or_doc_for(meth, code_or_doc) else - # NOT implemented + std_rbx_core_code_or_doc_for(meth, code_or_doc) end end - def rvm_rbx_core_code_for(meth) + def std_rbx_core_code_or_doc_for(meth, code_or_doc) + file_name = File.join(Rubinius::BIN_PATH, "..", meth.source_location.first) + raise "Cannot find rbx core source" if !File.exists?(file_name) + start_line meth.source_location.last + + case code_or_doc + when :code + MethodSource.source_helper([file_name, start_line]) + when :doc + MethodSource.comment_helper([file_name, start_line]) + end + end + + def rvm_rbx_core_code_or_doc_for(meth, code_or_doc) ruby_name = File.dirname(Rubinius::BIN_PATH).split("/").last source_path = File.join(File.dirname(File.dirname(File.dirname(Rubinius::BIN_PATH))), "src", ruby_name) file_name = File.join(source_path, meth.source_location.first) start_line = meth.source_location.last - MethodSource.source_helper([file_name, start_line]) + + case code_or_doc + when :code + MethodSource.source_helper([file_name, start_line]) + when :doc + MethodSource.comment_helper([file_name, start_line]) + end end ######### END RBX HELPERS ############### @@ -110,8 +137,11 @@ class Pry when :c doc = Pry::MethodInfo.info_for(meth).docstring when :ruby - doc = meth.comment - doc = strip_leading_hash_and_whitespace_from_ruby_comments(doc) + if rbx_core?(meth) + doc = strip_leading_hash_and_whitespace_from_ruby_comments(rbx_core_doc_for(meth)) + else + doc = strip_leading_hash_and_whitespace_from_ruby_comments(meth.comment) + end set_file_and_dir_locals(meth.source_location.first) end From 1a9738ff11f02fad328923a3254d725be184d362 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 03:22:59 +1200 Subject: [PATCH 07/21] refactored example initialization code (still need to update examples for new API) --- examples/example_basic.rb | 6 ++---- examples/example_command_override.rb | 7 ++----- examples/example_commands.rb | 5 +---- examples/example_hooks.rb | 7 ++----- examples/example_image_edit.rb | 12 ++++-------- examples/example_input.rb | 5 +---- examples/example_input2.rb | 5 +---- examples/example_output.rb | 5 +---- examples/example_print.rb | 7 ++----- examples/example_prompt.rb | 7 ++----- examples/helper.rb | 6 ++++++ 11 files changed, 24 insertions(+), 48 deletions(-) create mode 100644 examples/helper.rb diff --git a/examples/example_basic.rb b/examples/example_basic.rb index 8f893be6..ed3d4d52 100644 --- a/examples/example_basic.rb +++ b/examples/example_basic.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # define a local. a = "a local variable" @@ -14,4 +11,5 @@ end # Start pry session at top-level. # The local variable `a` and the `hello` method will # be accessible. +puts __LINE__ binding.pry diff --git a/examples/example_command_override.rb b/examples/example_command_override.rb index 889eaeee..237b1d61 100644 --- a/examples/example_command_override.rb +++ b/examples/example_command_override.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Inherit standard command set, but tweak them by importing some and # overriding others. @@ -18,7 +15,7 @@ class MyCommands < Pry::CommandBase # analogy to Ruby's native alias_method idiom for decorating a method alias_command "old_status", "status", "" - + # Invoke one command from within another using `run` command "status", "Modified status." do |x| output.puts "About to show status, are you ready?" diff --git a/examples/example_commands.rb b/examples/example_commands.rb index ad57e021..8e945db1 100644 --- a/examples/example_commands.rb +++ b/examples/example_commands.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) class MathCommands < Pry::CommandBase command "greet", "Greet a person, e.g: greet john" do |name| diff --git a/examples/example_hooks.rb b/examples/example_hooks.rb index 132b1ea1..e2a66b70 100644 --- a/examples/example_hooks.rb +++ b/examples/example_hooks.rb @@ -1,12 +1,9 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) my_hooks = { :before_session => proc { |out, target| out.puts "Opening #{target.eval('self')}." }, :after_session => proc { |out, target| out.puts "Closing #{target.eval('self')}." } } - + # Start a Pry session using the hooks hash defined in my_hooks Pry.start(TOPLEVEL_BINDING, :hooks => my_hooks) diff --git a/examples/example_image_edit.rb b/examples/example_image_edit.rb index 1eefa5e4..5f200c85 100644 --- a/examples/example_image_edit.rb +++ b/examples/example_image_edit.rb @@ -1,7 +1,7 @@ # Note: this requires you to have Gosu and TexPlay installed. # `gem install gosu` # `gem install texplay` -# +# # Extra instructions for installing Gosu on Linux can be found here: # http://code.google.com/p/gosu/wiki/GettingStartedOnLinux # @@ -10,11 +10,7 @@ # # Have fun! :) -direc = File.dirname(__FILE__) - -require 'rubygems' -require "texplay" -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) WIDTH = 640 HEIGHT = 480 @@ -42,12 +38,12 @@ class WinClass < Gosu::Window @img.rect 0, 0, @img.width - 1, @img.height - 1 @binding = Pry.binding_for(@img) - + @pry_instance = Pry.new(:commands => ImageCommands, :prompt => IMAGE_PROMPT) end def draw - @img.draw_rot(WIDTH / 2, HEIGHT / 2, 1, 0, 0.5, 0.5) + @img.draw_rot(WIDTH / 2, HEIGHT / 2, 1, 0, 0.5, 0.5) end def update diff --git a/examples/example_input.rb b/examples/example_input.rb index dd7c8d4e..dfa2239a 100644 --- a/examples/example_input.rb +++ b/examples/example_input.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Create a StringIO that contains the input data str_input = StringIO.new("puts 'hello world!'\nputs \"I am in \#{self}\"\nexit") diff --git a/examples/example_input2.rb b/examples/example_input2.rb index bd4979d5..13a9364b 100644 --- a/examples/example_input2.rb +++ b/examples/example_input2.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Create a StringIO that contains the input data for all the Pry objects cmds = <<-CMDS diff --git a/examples/example_output.rb b/examples/example_output.rb index e54dad77..6711a7b0 100644 --- a/examples/example_output.rb +++ b/examples/example_output.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Create a StringIO to contain the output data str_output = StringIO.new diff --git a/examples/example_print.rb b/examples/example_print.rb index 0b55a31a..330982ea 100644 --- a/examples/example_print.rb +++ b/examples/example_print.rb @@ -1,9 +1,6 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) my_print = proc { |out, value| out.puts "Output is: #{value.inspect}" } - + # Start a Pry session using the print object defined in my_print Pry.start(TOPLEVEL_BINDING, :print => my_print) diff --git a/examples/example_prompt.rb b/examples/example_prompt.rb index 44b68092..f11c5ef7 100644 --- a/examples/example_prompt.rb +++ b/examples/example_prompt.rb @@ -1,12 +1,9 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Remember, first prompt in array is the main prompt, second is the wait # prompt (used for multiline input when more input is required) my_prompt = [ proc { |obj, *| "inside #{obj}> " }, proc { |obj, *| "inside #{obj}* "} ] - + # Start a Pry session using the prompt defined in my_prompt Pry.start(TOPLEVEL_BINDING, :prompt => my_prompt) diff --git a/examples/helper.rb b/examples/helper.rb new file mode 100644 index 00000000..ce37f493 --- /dev/null +++ b/examples/helper.rb @@ -0,0 +1,6 @@ +require 'rubygems' +unless Object.const_defined? 'Pry' + $:.unshift File.expand_path '../../lib', __FILE__ + require 'pry' +end + From 597f1cc68c0f31dc9d7d6be5fbefcef65e95ee81 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 03:26:49 +1200 Subject: [PATCH 08/21] reorganized command tests into test_default_commands/ folder with separate test files for each command set --- test/default_commands/test_context.rb | 64 ++++ test/default_commands/test_documentation.rb | 31 ++ test/default_commands/test_input.rb | 135 +++++++ test/default_commands/test_introspection.rb | 146 ++++++++ test/test_default_commands.rb | 343 ------------------ test/test_default_commands/test_context.rb | 64 ++++ .../test_documentation.rb | 31 ++ test/test_default_commands/test_input.rb | 135 +++++++ .../test_introspection.rb | 146 ++++++++ 9 files changed, 752 insertions(+), 343 deletions(-) create mode 100644 test/default_commands/test_context.rb create mode 100644 test/default_commands/test_documentation.rb create mode 100644 test/default_commands/test_input.rb create mode 100644 test/default_commands/test_introspection.rb create mode 100644 test/test_default_commands/test_context.rb create mode 100644 test/test_default_commands/test_documentation.rb create mode 100644 test/test_default_commands/test_input.rb create mode 100644 test/test_default_commands/test_introspection.rb diff --git a/test/default_commands/test_context.rb b/test/default_commands/test_context.rb new file mode 100644 index 00000000..93c5f237 --- /dev/null +++ b/test/default_commands/test_context.rb @@ -0,0 +1,64 @@ +require 'helper' + +describe "Pry::DefaultCommands::Context" do + describe "cd" do + after do + $obj = nil + end + + it 'should cd into simple input' do + b = Pry.binding_for(Object.new) + b.eval("x = :mon_ouie") + + redirect_pry_io(InputTester.new("cd x", "$obj = self", "exit-all"), StringIO.new) do + b.pry + end + + $obj.should == :mon_ouie + end + + it 'should break out of session with cd ..' do + b = Pry.binding_for(:outer) + b.eval("x = :inner") + + redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd ..", "$outer = self", "exit-all"), StringIO.new) do + b.pry + end + $inner.should == :inner + $outer.should == :outer + end + + it 'should break out to outer-most session with cd /' do + b = Pry.binding_for(:outer) + b.eval("x = :inner") + + redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd /", "$outer = self", "exit-all"), StringIO.new) do + b.pry + end + $inner.should == :inner + $five.should == 5 + $outer.should == :outer + end + + it 'should start a session on TOPLEVEL_BINDING with cd ::' do + b = Pry.binding_for(:outer) + + redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all"), StringIO.new) do + 5.pry + end + $obj.should == TOPLEVEL_BINDING.eval('self') + end + + it 'should cd into complex input (with spaces)' do + o = Object.new + def o.hello(x, y, z) + :mon_ouie + end + + redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all"), StringIO.new) do + o.pry + end + $obj.should == :mon_ouie + end + end +end diff --git a/test/default_commands/test_documentation.rb b/test/default_commands/test_documentation.rb new file mode 100644 index 00000000..235c29b9 --- /dev/null +++ b/test/default_commands/test_documentation.rb @@ -0,0 +1,31 @@ +require 'helper' + +describe "Pry::DefaultCommands::Documentation" do + describe "show-doc" do + it 'should output a method\'s documentation' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /sample doc/ + end + + it 'should output a method\'s documentation if inside method without needing to use method name' do + $str_output = StringIO.new + + o = Object.new + + # sample comment + def o.sample + redirect_pry_io(InputTester.new("show-doc", "exit-all"), $str_output) do + binding.pry + end + end + o.sample + + $str_output.string.should =~ /sample comment/ + $str_output = nil + end + end +end diff --git a/test/default_commands/test_input.rb b/test/default_commands/test_input.rb new file mode 100644 index 00000000..6831563e --- /dev/null +++ b/test/default_commands/test_input.rb @@ -0,0 +1,135 @@ +require 'helper' + +describe "Pry::DefaultCommands::Input" do + + describe "amend-line-N" do + it 'should correctly amend the last line of input when no line number specified ' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("def hello", "puts :bing", "amend-line puts :blah", "show-input", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /\A\d+: def hello\n\d+: puts :blah/ + end + + it 'should correctly amend the specified line of input when line number given ' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line-0 def goodbye", "show-input", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /\A\d+: def goodbye\n\d+: puts :bing\n\d+: puts :bang/ + end + end + + describe "hist" do + push_first_hist_line = lambda do |hist, line| + hist.push line + end + + before do + Readline::HISTORY.shift until Readline::HISTORY.empty? + @hist = Readline::HISTORY + end + + it 'should display the correct history' do + push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") + @hist.push "hello" + @hist.push "world" + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /hello\n.*world/ + end + + it 'should replay history correctly (single item)' do + push_first_hist_line.call(@hist, ":hello") + @hist.push ":blah" + @hist.push ":bucket" + @hist.push ":ostrich" + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --replay -1", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /ostrich/ + end + + it 'should replay a range of history correctly (range of items)' do + push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") + @hist.push ":hello" + @hist.push ":carl" + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /:hello\n.*:carl/ + end + + it 'should grep for correct lines in history' do + push_first_hist_line.call(@hist, "apple") + @hist.push "abby" + @hist.push "box" + @hist.push "button" + @hist.push "pepper" + @hist.push "orange" + @hist.push "grape" + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --grep o", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/ + end + + it 'should return last N lines in history with --tail switch' do + push_first_hist_line.call(@hist, "0") + ("a".."z").each do |v| + @hist.push v + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --tail 3", "exit-all"), str_output) do + pry + end + + str_output.string.each_line.count.should == 3 + str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/ + end + + # strangeness in this test is due to bug in Readline::HISTORY not + # always registering first line of input + it 'should return first N lines in history with --head switch' do + push_first_hist_line.call(@hist, "0") + ("a".."z").each do |v| + @hist.push v + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --head 4", "exit-all"), str_output) do + pry + end + + str_output.string.each_line.count.should == 4 + str_output.string.should =~ /a\n\d+:.*b\n\d+:.*c/ + end + + # strangeness in this test is due to bug in Readline::HISTORY not + # always registering first line of input + it 'should show lines between lines A and B with the --show switch' do + push_first_hist_line.call(@hist, "0") + ("a".."z").each do |v| + @hist.push v + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --show 1..4", "exit-all"), str_output) do + pry + end + + str_output.string.each_line.count.should == 4 + str_output.string.should =~ /b\n\d+:.*c\n\d+:.*d/ + end + end + + +end diff --git a/test/default_commands/test_introspection.rb b/test/default_commands/test_introspection.rb new file mode 100644 index 00000000..0ccc3e1f --- /dev/null +++ b/test/default_commands/test_introspection.rb @@ -0,0 +1,146 @@ +require 'helper' + +describe "Pry::DefaultCommands::Introspection" do + describe "show-method" do + it 'should output a method\'s source' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-method sample_method", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /def sample/ + end + + it 'should output a method\'s source with line numbers' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-method -l sample_method", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /\d+: def sample/ + end + + it 'should output a method\'s source if inside method without needing to use method name' do + $str_output = StringIO.new + + o = Object.new + def o.sample + redirect_pry_io(InputTester.new("show-method", "exit-all"), $str_output) do + binding.pry + end + end + o.sample + + $str_output.string.should =~ /def o.sample/ + $str_output = nil + end + + it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do + $str_output = StringIO.new + + o = Object.new + def o.sample + redirect_pry_io(InputTester.new("show-method -l", "exit-all"), $str_output) do + binding.pry + end + end + o.sample + + $str_output.string.should =~ /\d+: def o.sample/ + $str_output = nil + end + + # dynamically defined method source retrieval is only supported in + # 1.9 - where Method#source_location is native + if RUBY_VERSION =~ /1.9/ + it 'should output a method\'s source for a method defined inside pry' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do + TOPLEVEL_BINDING.pry + end + + str_output.string.should =~ /def dyna_method/ + Object.remove_method :dyna_method + end + + it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do + TOPLEVEL_BINDING.pry + end + + str_output.string.should =~ /def dyna_method/ + Object.remove_method :dyna_method + end + + it 'should output an instance method\'s source for a method defined inside pry' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-method A#yo"), str_output) do + TOPLEVEL_BINDING.pry + end + + str_output.string.should =~ /def yo/ + Object.remove_const :A + end + + it 'should output an instance method\'s source for a method defined inside pry using define_method' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "end", "show-method A#yup"), str_output) do + TOPLEVEL_BINDING.pry + end + + str_output.string.should =~ /define_method\(:yup\)/ + Object.remove_const :A + end + end + end + + # show-command only works in implementations that support Proc#source_location + if Proc.method_defined?(:source_location) + describe "show-command" do + it 'should show source for an ordinary command' do + set = Pry::CommandSet.new do + import_from Pry::Commands, "show-command" + command "foo" do + :body_of_foo + end + end + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-command foo"), str_output) do + Pry.new(:commands => set).rep + end + str_output.string.should =~ /:body_of_foo/ + end + + it 'should show source for a command with spaces in its name' do + set = Pry::CommandSet.new do + import_from Pry::Commands, "show-command" + command "foo bar" do + :body_of_foo_bar + end + end + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-command \"foo bar\""), str_output) do + Pry.new(:commands => set).rep + end + str_output.string.should =~ /:body_of_foo_bar/ + end + + it 'should show source for a command by listing name' do + set = Pry::CommandSet.new do + import_from Pry::Commands, "show-command" + command /foo(.*)/, "", :listing => "bar" do + :body_of_foo_regex + end + end + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-command bar"), str_output) do + Pry.new(:commands => set).rep + end + str_output.string.should =~ /:body_of_foo_regex/ + end + end + end + + +end diff --git a/test/test_default_commands.rb b/test/test_default_commands.rb index dc92230b..1321b84d 100644 --- a/test/test_default_commands.rb +++ b/test/test_default_commands.rb @@ -1,349 +1,6 @@ require 'helper' describe "Pry::Commands" do - - after do - $obj = nil - end - - describe "hist" do - push_first_hist_line = lambda do |hist, line| - hist.push line - end - - before do - Readline::HISTORY.shift until Readline::HISTORY.empty? - @hist = Readline::HISTORY - end - - it 'should display the correct history' do - push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") - @hist.push "hello" - @hist.push "world" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist", "exit-all"), str_output) do - pry - end - str_output.string.should =~ /hello\n.*world/ - end - - it 'should replay history correctly (single item)' do - push_first_hist_line.call(@hist, ":hello") - @hist.push ":blah" - @hist.push ":bucket" - @hist.push ":ostrich" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --replay -1", "exit-all"), str_output) do - pry - end - str_output.string.should =~ /ostrich/ - end - - it 'should replay a range of history correctly (range of items)' do - push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") - @hist.push ":hello" - @hist.push ":carl" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all"), str_output) do - pry - end - str_output.string.should =~ /:hello\n.*:carl/ - end - - it 'should grep for correct lines in history' do - push_first_hist_line.call(@hist, "apple") - @hist.push "abby" - @hist.push "box" - @hist.push "button" - @hist.push "pepper" - @hist.push "orange" - @hist.push "grape" - - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --grep o", "exit-all"), str_output) do - pry - end - - str_output.string.should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/ - end - - it 'should return last N lines in history with --tail switch' do - push_first_hist_line.call(@hist, "0") - ("a".."z").each do |v| - @hist.push v - end - - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --tail 3", "exit-all"), str_output) do - pry - end - - str_output.string.each_line.count.should == 3 - str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/ - end - - # strangeness in this test is due to bug in Readline::HISTORY not - # always registering first line of input - it 'should return first N lines in history with --head switch' do - push_first_hist_line.call(@hist, "0") - ("a".."z").each do |v| - @hist.push v - end - - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --head 4", "exit-all"), str_output) do - pry - end - - str_output.string.each_line.count.should == 4 - str_output.string.should =~ /a\n\d+:.*b\n\d+:.*c/ - end - - # strangeness in this test is due to bug in Readline::HISTORY not - # always registering first line of input - it 'should show lines between lines A and B with the --show switch' do - push_first_hist_line.call(@hist, "0") - ("a".."z").each do |v| - @hist.push v - end - - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --show 1..4", "exit-all"), str_output) do - pry - end - - str_output.string.each_line.count.should == 4 - str_output.string.should =~ /b\n\d+:.*c\n\d+:.*d/ - end - end - - describe "show-method" do - it 'should output a method\'s source' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-method sample_method", "exit-all"), str_output) do - pry - end - - str_output.string.should =~ /def sample/ - end - - it 'should output a method\'s source with line numbers' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-method -l sample_method", "exit-all"), str_output) do - pry - end - - str_output.string.should =~ /\d+: def sample/ - end - - it 'should output a method\'s source if inside method without needing to use method name' do - $str_output = StringIO.new - - o = Object.new - def o.sample - redirect_pry_io(InputTester.new("show-method", "exit-all"), $str_output) do - binding.pry - end - end - o.sample - - $str_output.string.should =~ /def o.sample/ - $str_output = nil - end - - it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do - $str_output = StringIO.new - - o = Object.new - def o.sample - redirect_pry_io(InputTester.new("show-method -l", "exit-all"), $str_output) do - binding.pry - end - end - o.sample - - $str_output.string.should =~ /\d+: def o.sample/ - $str_output = nil - end - - # dynamically defined method source retrieval is only supported in - # 1.9 - where Method#source_location is native - if RUBY_VERSION =~ /1.9/ - it 'should output a method\'s source for a method defined inside pry' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do - TOPLEVEL_BINDING.pry - end - - str_output.string.should =~ /def dyna_method/ - Object.remove_method :dyna_method - end - - it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do - TOPLEVEL_BINDING.pry - end - - str_output.string.should =~ /def dyna_method/ - Object.remove_method :dyna_method - end - - it 'should output an instance method\'s source for a method defined inside pry' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-method A#yo"), str_output) do - TOPLEVEL_BINDING.pry - end - - str_output.string.should =~ /def yo/ - Object.remove_const :A - end - - it 'should output an instance method\'s source for a method defined inside pry using define_method' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "end", "show-method A#yup"), str_output) do - TOPLEVEL_BINDING.pry - end - - str_output.string.should =~ /define_method\(:yup\)/ - Object.remove_const :A - end - end - end - - describe "show-doc" do - it 'should output a method\'s documentation' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output) do - pry - end - - str_output.string.should =~ /sample doc/ - end - - it 'should output a method\'s documentation if inside method without needing to use method name' do - $str_output = StringIO.new - - o = Object.new - - # sample comment - def o.sample - redirect_pry_io(InputTester.new("show-doc", "exit-all"), $str_output) do - binding.pry - end - end - o.sample - - $str_output.string.should =~ /sample comment/ - $str_output = nil - end - end - - - describe "cd" do - it 'should cd into simple input' do - b = Pry.binding_for(Object.new) - b.eval("x = :mon_ouie") - - redirect_pry_io(InputTester.new("cd x", "$obj = self", "exit-all"), StringIO.new) do - b.pry - end - - $obj.should == :mon_ouie - end - - it 'should break out of session with cd ..' do - b = Pry.binding_for(:outer) - b.eval("x = :inner") - - redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd ..", "$outer = self", "exit-all"), StringIO.new) do - b.pry - end - $inner.should == :inner - $outer.should == :outer - end - - it 'should break out to outer-most session with cd /' do - b = Pry.binding_for(:outer) - b.eval("x = :inner") - - redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd /", "$outer = self", "exit-all"), StringIO.new) do - b.pry - end - $inner.should == :inner - $five.should == 5 - $outer.should == :outer - end - - it 'should start a session on TOPLEVEL_BINDING with cd ::' do - b = Pry.binding_for(:outer) - - redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all"), StringIO.new) do - 5.pry - end - $obj.should == TOPLEVEL_BINDING.eval('self') - end - - it 'should cd into complex input (with spaces)' do - o = Object.new - def o.hello(x, y, z) - :mon_ouie - end - - redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all"), StringIO.new) do - o.pry - end - $obj.should == :mon_ouie - end - end - - # show-command only works in implementations that support Proc#source_location - if Proc.method_defined?(:source_location) - describe "show-command" do - it 'should show source for an ordinary command' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command "foo" do - :body_of_foo - end - end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command foo"), str_output) do - Pry.new(:commands => set).rep - end - str_output.string.should =~ /:body_of_foo/ - end - - it 'should show source for a command with spaces in its name' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command "foo bar" do - :body_of_foo_bar - end - end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command \"foo bar\""), str_output) do - Pry.new(:commands => set).rep - end - str_output.string.should =~ /:body_of_foo_bar/ - end - - it 'should show source for a command by listing name' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command /foo(.*)/, "", :listing => "bar" do - :body_of_foo_regex - end - end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command bar"), str_output) do - Pry.new(:commands => set).rep - end - str_output.string.should =~ /:body_of_foo_regex/ - end - end - end - describe "help" do it 'should display help for a specific command' do str_output = StringIO.new diff --git a/test/test_default_commands/test_context.rb b/test/test_default_commands/test_context.rb new file mode 100644 index 00000000..93c5f237 --- /dev/null +++ b/test/test_default_commands/test_context.rb @@ -0,0 +1,64 @@ +require 'helper' + +describe "Pry::DefaultCommands::Context" do + describe "cd" do + after do + $obj = nil + end + + it 'should cd into simple input' do + b = Pry.binding_for(Object.new) + b.eval("x = :mon_ouie") + + redirect_pry_io(InputTester.new("cd x", "$obj = self", "exit-all"), StringIO.new) do + b.pry + end + + $obj.should == :mon_ouie + end + + it 'should break out of session with cd ..' do + b = Pry.binding_for(:outer) + b.eval("x = :inner") + + redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd ..", "$outer = self", "exit-all"), StringIO.new) do + b.pry + end + $inner.should == :inner + $outer.should == :outer + end + + it 'should break out to outer-most session with cd /' do + b = Pry.binding_for(:outer) + b.eval("x = :inner") + + redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd /", "$outer = self", "exit-all"), StringIO.new) do + b.pry + end + $inner.should == :inner + $five.should == 5 + $outer.should == :outer + end + + it 'should start a session on TOPLEVEL_BINDING with cd ::' do + b = Pry.binding_for(:outer) + + redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all"), StringIO.new) do + 5.pry + end + $obj.should == TOPLEVEL_BINDING.eval('self') + end + + it 'should cd into complex input (with spaces)' do + o = Object.new + def o.hello(x, y, z) + :mon_ouie + end + + redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all"), StringIO.new) do + o.pry + end + $obj.should == :mon_ouie + end + end +end diff --git a/test/test_default_commands/test_documentation.rb b/test/test_default_commands/test_documentation.rb new file mode 100644 index 00000000..235c29b9 --- /dev/null +++ b/test/test_default_commands/test_documentation.rb @@ -0,0 +1,31 @@ +require 'helper' + +describe "Pry::DefaultCommands::Documentation" do + describe "show-doc" do + it 'should output a method\'s documentation' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /sample doc/ + end + + it 'should output a method\'s documentation if inside method without needing to use method name' do + $str_output = StringIO.new + + o = Object.new + + # sample comment + def o.sample + redirect_pry_io(InputTester.new("show-doc", "exit-all"), $str_output) do + binding.pry + end + end + o.sample + + $str_output.string.should =~ /sample comment/ + $str_output = nil + end + end +end diff --git a/test/test_default_commands/test_input.rb b/test/test_default_commands/test_input.rb new file mode 100644 index 00000000..6831563e --- /dev/null +++ b/test/test_default_commands/test_input.rb @@ -0,0 +1,135 @@ +require 'helper' + +describe "Pry::DefaultCommands::Input" do + + describe "amend-line-N" do + it 'should correctly amend the last line of input when no line number specified ' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("def hello", "puts :bing", "amend-line puts :blah", "show-input", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /\A\d+: def hello\n\d+: puts :blah/ + end + + it 'should correctly amend the specified line of input when line number given ' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line-0 def goodbye", "show-input", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /\A\d+: def goodbye\n\d+: puts :bing\n\d+: puts :bang/ + end + end + + describe "hist" do + push_first_hist_line = lambda do |hist, line| + hist.push line + end + + before do + Readline::HISTORY.shift until Readline::HISTORY.empty? + @hist = Readline::HISTORY + end + + it 'should display the correct history' do + push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") + @hist.push "hello" + @hist.push "world" + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /hello\n.*world/ + end + + it 'should replay history correctly (single item)' do + push_first_hist_line.call(@hist, ":hello") + @hist.push ":blah" + @hist.push ":bucket" + @hist.push ":ostrich" + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --replay -1", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /ostrich/ + end + + it 'should replay a range of history correctly (range of items)' do + push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") + @hist.push ":hello" + @hist.push ":carl" + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /:hello\n.*:carl/ + end + + it 'should grep for correct lines in history' do + push_first_hist_line.call(@hist, "apple") + @hist.push "abby" + @hist.push "box" + @hist.push "button" + @hist.push "pepper" + @hist.push "orange" + @hist.push "grape" + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --grep o", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/ + end + + it 'should return last N lines in history with --tail switch' do + push_first_hist_line.call(@hist, "0") + ("a".."z").each do |v| + @hist.push v + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --tail 3", "exit-all"), str_output) do + pry + end + + str_output.string.each_line.count.should == 3 + str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/ + end + + # strangeness in this test is due to bug in Readline::HISTORY not + # always registering first line of input + it 'should return first N lines in history with --head switch' do + push_first_hist_line.call(@hist, "0") + ("a".."z").each do |v| + @hist.push v + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --head 4", "exit-all"), str_output) do + pry + end + + str_output.string.each_line.count.should == 4 + str_output.string.should =~ /a\n\d+:.*b\n\d+:.*c/ + end + + # strangeness in this test is due to bug in Readline::HISTORY not + # always registering first line of input + it 'should show lines between lines A and B with the --show switch' do + push_first_hist_line.call(@hist, "0") + ("a".."z").each do |v| + @hist.push v + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --show 1..4", "exit-all"), str_output) do + pry + end + + str_output.string.each_line.count.should == 4 + str_output.string.should =~ /b\n\d+:.*c\n\d+:.*d/ + end + end + + +end diff --git a/test/test_default_commands/test_introspection.rb b/test/test_default_commands/test_introspection.rb new file mode 100644 index 00000000..0ccc3e1f --- /dev/null +++ b/test/test_default_commands/test_introspection.rb @@ -0,0 +1,146 @@ +require 'helper' + +describe "Pry::DefaultCommands::Introspection" do + describe "show-method" do + it 'should output a method\'s source' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-method sample_method", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /def sample/ + end + + it 'should output a method\'s source with line numbers' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-method -l sample_method", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /\d+: def sample/ + end + + it 'should output a method\'s source if inside method without needing to use method name' do + $str_output = StringIO.new + + o = Object.new + def o.sample + redirect_pry_io(InputTester.new("show-method", "exit-all"), $str_output) do + binding.pry + end + end + o.sample + + $str_output.string.should =~ /def o.sample/ + $str_output = nil + end + + it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do + $str_output = StringIO.new + + o = Object.new + def o.sample + redirect_pry_io(InputTester.new("show-method -l", "exit-all"), $str_output) do + binding.pry + end + end + o.sample + + $str_output.string.should =~ /\d+: def o.sample/ + $str_output = nil + end + + # dynamically defined method source retrieval is only supported in + # 1.9 - where Method#source_location is native + if RUBY_VERSION =~ /1.9/ + it 'should output a method\'s source for a method defined inside pry' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do + TOPLEVEL_BINDING.pry + end + + str_output.string.should =~ /def dyna_method/ + Object.remove_method :dyna_method + end + + it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do + TOPLEVEL_BINDING.pry + end + + str_output.string.should =~ /def dyna_method/ + Object.remove_method :dyna_method + end + + it 'should output an instance method\'s source for a method defined inside pry' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-method A#yo"), str_output) do + TOPLEVEL_BINDING.pry + end + + str_output.string.should =~ /def yo/ + Object.remove_const :A + end + + it 'should output an instance method\'s source for a method defined inside pry using define_method' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "end", "show-method A#yup"), str_output) do + TOPLEVEL_BINDING.pry + end + + str_output.string.should =~ /define_method\(:yup\)/ + Object.remove_const :A + end + end + end + + # show-command only works in implementations that support Proc#source_location + if Proc.method_defined?(:source_location) + describe "show-command" do + it 'should show source for an ordinary command' do + set = Pry::CommandSet.new do + import_from Pry::Commands, "show-command" + command "foo" do + :body_of_foo + end + end + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-command foo"), str_output) do + Pry.new(:commands => set).rep + end + str_output.string.should =~ /:body_of_foo/ + end + + it 'should show source for a command with spaces in its name' do + set = Pry::CommandSet.new do + import_from Pry::Commands, "show-command" + command "foo bar" do + :body_of_foo_bar + end + end + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-command \"foo bar\""), str_output) do + Pry.new(:commands => set).rep + end + str_output.string.should =~ /:body_of_foo_bar/ + end + + it 'should show source for a command by listing name' do + set = Pry::CommandSet.new do + import_from Pry::Commands, "show-command" + command /foo(.*)/, "", :listing => "bar" do + :body_of_foo_regex + end + end + str_output = StringIO.new + redirect_pry_io(InputTester.new("show-command bar"), str_output) do + Pry.new(:commands => set).rep + end + str_output.string.should =~ /:body_of_foo_regex/ + end + end + end + + +end From 0cf048e2a65e8e3ba8bfa1ec98f35746d72519a9 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 03:28:27 +1200 Subject: [PATCH 09/21] added failing test for regex command interpolatino --- lib/pry/command_processor.rb | 13 +- test/default_commands/test_context.rb | 64 --------- test/default_commands/test_documentation.rb | 31 ----- test/default_commands/test_input.rb | 135 ------------------ test/default_commands/test_introspection.rb | 146 -------------------- test/test_pry.rb | 18 +++ 6 files changed, 28 insertions(+), 379 deletions(-) delete mode 100644 test/default_commands/test_context.rb delete mode 100644 test/default_commands/test_documentation.rb delete mode 100644 test/default_commands/test_input.rb delete mode 100644 test/default_commands/test_introspection.rb diff --git a/lib/pry/command_processor.rb b/lib/pry/command_processor.rb index 697c9077..cbc0e585 100644 --- a/lib/pry/command_processor.rb +++ b/lib/pry/command_processor.rb @@ -63,12 +63,19 @@ class Pry # multi-line input. # @param [Binding] target The receiver of the commands. def process_commands(val, eval_string, target) - command, captures, pos = command_matched(val) # no command was matched, so return to caller - return if !command + return if !valid_command?(val) + command, captures, pos = command_matched(val) + + + # perform ruby interpolation for commands + if command.options[:interpolate] + val.replace interpolate_string(val, target) + # command, captures, pos = command_matched(val) +# captures = captures.map { |v| interpolate_string(v, target) if v } + end - val.replace interpolate_string(val, target) if command.options[:interpolate] arg_string = val[pos..-1].strip args = arg_string ? Shellwords.shellwords(arg_string) : [] diff --git a/test/default_commands/test_context.rb b/test/default_commands/test_context.rb deleted file mode 100644 index 93c5f237..00000000 --- a/test/default_commands/test_context.rb +++ /dev/null @@ -1,64 +0,0 @@ -require 'helper' - -describe "Pry::DefaultCommands::Context" do - describe "cd" do - after do - $obj = nil - end - - it 'should cd into simple input' do - b = Pry.binding_for(Object.new) - b.eval("x = :mon_ouie") - - redirect_pry_io(InputTester.new("cd x", "$obj = self", "exit-all"), StringIO.new) do - b.pry - end - - $obj.should == :mon_ouie - end - - it 'should break out of session with cd ..' do - b = Pry.binding_for(:outer) - b.eval("x = :inner") - - redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd ..", "$outer = self", "exit-all"), StringIO.new) do - b.pry - end - $inner.should == :inner - $outer.should == :outer - end - - it 'should break out to outer-most session with cd /' do - b = Pry.binding_for(:outer) - b.eval("x = :inner") - - redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd /", "$outer = self", "exit-all"), StringIO.new) do - b.pry - end - $inner.should == :inner - $five.should == 5 - $outer.should == :outer - end - - it 'should start a session on TOPLEVEL_BINDING with cd ::' do - b = Pry.binding_for(:outer) - - redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all"), StringIO.new) do - 5.pry - end - $obj.should == TOPLEVEL_BINDING.eval('self') - end - - it 'should cd into complex input (with spaces)' do - o = Object.new - def o.hello(x, y, z) - :mon_ouie - end - - redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all"), StringIO.new) do - o.pry - end - $obj.should == :mon_ouie - end - end -end diff --git a/test/default_commands/test_documentation.rb b/test/default_commands/test_documentation.rb deleted file mode 100644 index 235c29b9..00000000 --- a/test/default_commands/test_documentation.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'helper' - -describe "Pry::DefaultCommands::Documentation" do - describe "show-doc" do - it 'should output a method\'s documentation' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output) do - pry - end - - str_output.string.should =~ /sample doc/ - end - - it 'should output a method\'s documentation if inside method without needing to use method name' do - $str_output = StringIO.new - - o = Object.new - - # sample comment - def o.sample - redirect_pry_io(InputTester.new("show-doc", "exit-all"), $str_output) do - binding.pry - end - end - o.sample - - $str_output.string.should =~ /sample comment/ - $str_output = nil - end - end -end diff --git a/test/default_commands/test_input.rb b/test/default_commands/test_input.rb deleted file mode 100644 index 6831563e..00000000 --- a/test/default_commands/test_input.rb +++ /dev/null @@ -1,135 +0,0 @@ -require 'helper' - -describe "Pry::DefaultCommands::Input" do - - describe "amend-line-N" do - it 'should correctly amend the last line of input when no line number specified ' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "amend-line puts :blah", "show-input", "exit-all"), str_output) do - pry - end - str_output.string.should =~ /\A\d+: def hello\n\d+: puts :blah/ - end - - it 'should correctly amend the specified line of input when line number given ' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line-0 def goodbye", "show-input", "exit-all"), str_output) do - pry - end - str_output.string.should =~ /\A\d+: def goodbye\n\d+: puts :bing\n\d+: puts :bang/ - end - end - - describe "hist" do - push_first_hist_line = lambda do |hist, line| - hist.push line - end - - before do - Readline::HISTORY.shift until Readline::HISTORY.empty? - @hist = Readline::HISTORY - end - - it 'should display the correct history' do - push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") - @hist.push "hello" - @hist.push "world" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist", "exit-all"), str_output) do - pry - end - str_output.string.should =~ /hello\n.*world/ - end - - it 'should replay history correctly (single item)' do - push_first_hist_line.call(@hist, ":hello") - @hist.push ":blah" - @hist.push ":bucket" - @hist.push ":ostrich" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --replay -1", "exit-all"), str_output) do - pry - end - str_output.string.should =~ /ostrich/ - end - - it 'should replay a range of history correctly (range of items)' do - push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") - @hist.push ":hello" - @hist.push ":carl" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all"), str_output) do - pry - end - str_output.string.should =~ /:hello\n.*:carl/ - end - - it 'should grep for correct lines in history' do - push_first_hist_line.call(@hist, "apple") - @hist.push "abby" - @hist.push "box" - @hist.push "button" - @hist.push "pepper" - @hist.push "orange" - @hist.push "grape" - - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --grep o", "exit-all"), str_output) do - pry - end - - str_output.string.should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/ - end - - it 'should return last N lines in history with --tail switch' do - push_first_hist_line.call(@hist, "0") - ("a".."z").each do |v| - @hist.push v - end - - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --tail 3", "exit-all"), str_output) do - pry - end - - str_output.string.each_line.count.should == 3 - str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/ - end - - # strangeness in this test is due to bug in Readline::HISTORY not - # always registering first line of input - it 'should return first N lines in history with --head switch' do - push_first_hist_line.call(@hist, "0") - ("a".."z").each do |v| - @hist.push v - end - - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --head 4", "exit-all"), str_output) do - pry - end - - str_output.string.each_line.count.should == 4 - str_output.string.should =~ /a\n\d+:.*b\n\d+:.*c/ - end - - # strangeness in this test is due to bug in Readline::HISTORY not - # always registering first line of input - it 'should show lines between lines A and B with the --show switch' do - push_first_hist_line.call(@hist, "0") - ("a".."z").each do |v| - @hist.push v - end - - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --show 1..4", "exit-all"), str_output) do - pry - end - - str_output.string.each_line.count.should == 4 - str_output.string.should =~ /b\n\d+:.*c\n\d+:.*d/ - end - end - - -end diff --git a/test/default_commands/test_introspection.rb b/test/default_commands/test_introspection.rb deleted file mode 100644 index 0ccc3e1f..00000000 --- a/test/default_commands/test_introspection.rb +++ /dev/null @@ -1,146 +0,0 @@ -require 'helper' - -describe "Pry::DefaultCommands::Introspection" do - describe "show-method" do - it 'should output a method\'s source' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-method sample_method", "exit-all"), str_output) do - pry - end - - str_output.string.should =~ /def sample/ - end - - it 'should output a method\'s source with line numbers' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-method -l sample_method", "exit-all"), str_output) do - pry - end - - str_output.string.should =~ /\d+: def sample/ - end - - it 'should output a method\'s source if inside method without needing to use method name' do - $str_output = StringIO.new - - o = Object.new - def o.sample - redirect_pry_io(InputTester.new("show-method", "exit-all"), $str_output) do - binding.pry - end - end - o.sample - - $str_output.string.should =~ /def o.sample/ - $str_output = nil - end - - it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do - $str_output = StringIO.new - - o = Object.new - def o.sample - redirect_pry_io(InputTester.new("show-method -l", "exit-all"), $str_output) do - binding.pry - end - end - o.sample - - $str_output.string.should =~ /\d+: def o.sample/ - $str_output = nil - end - - # dynamically defined method source retrieval is only supported in - # 1.9 - where Method#source_location is native - if RUBY_VERSION =~ /1.9/ - it 'should output a method\'s source for a method defined inside pry' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do - TOPLEVEL_BINDING.pry - end - - str_output.string.should =~ /def dyna_method/ - Object.remove_method :dyna_method - end - - it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do - TOPLEVEL_BINDING.pry - end - - str_output.string.should =~ /def dyna_method/ - Object.remove_method :dyna_method - end - - it 'should output an instance method\'s source for a method defined inside pry' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-method A#yo"), str_output) do - TOPLEVEL_BINDING.pry - end - - str_output.string.should =~ /def yo/ - Object.remove_const :A - end - - it 'should output an instance method\'s source for a method defined inside pry using define_method' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "end", "show-method A#yup"), str_output) do - TOPLEVEL_BINDING.pry - end - - str_output.string.should =~ /define_method\(:yup\)/ - Object.remove_const :A - end - end - end - - # show-command only works in implementations that support Proc#source_location - if Proc.method_defined?(:source_location) - describe "show-command" do - it 'should show source for an ordinary command' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command "foo" do - :body_of_foo - end - end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command foo"), str_output) do - Pry.new(:commands => set).rep - end - str_output.string.should =~ /:body_of_foo/ - end - - it 'should show source for a command with spaces in its name' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command "foo bar" do - :body_of_foo_bar - end - end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command \"foo bar\""), str_output) do - Pry.new(:commands => set).rep - end - str_output.string.should =~ /:body_of_foo_bar/ - end - - it 'should show source for a command by listing name' do - set = Pry::CommandSet.new do - import_from Pry::Commands, "show-command" - command /foo(.*)/, "", :listing => "bar" do - :body_of_foo_regex - end - end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command bar"), str_output) do - Pry.new(:commands => set).rep - end - str_output.string.should =~ /:body_of_foo_regex/ - end - end - end - - -end diff --git a/test/test_pry.rb b/test/test_pry.rb index 9413f4c7..b3a9b1d2 100644 --- a/test/test_pry.rb +++ b/test/test_pry.rb @@ -479,6 +479,24 @@ describe Pry do str_output.string.should =~ /hello 1 baby/ end + it 'should create a regex command and interpolate the captures' do + set = Pry::CommandSet.new do + command /hello (.*)/, "" do |c1| + output.puts "hello #{c1}" + end + end + + str_output = StringIO.new + $obj = "bing" + redirect_pry_io(InputTester.new('hello #{$obj}'), str_output) do + Pry.new(:commands => set).rep + end + +# binding.pry + str_output.string.should =~ /hello bing/ + $obj = nil + end + it 'if a regex capture is missing it should be nil' do set = Pry::CommandSet.new do command /hello(.)?/, "" do |c1, a1| From e9a1222b59d194981c9bc96f78cb1db77bf0686f Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 03:31:32 +1200 Subject: [PATCH 10/21] support for rubinius core source and doc browsing in both rvm installs and git installs --- lib/pry/helpers/command_helpers.rb | 53 ++++++++++++++++-------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index e3313f4a..4a7cd107 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -69,38 +69,43 @@ class Pry end def rbx_core_code_or_doc_for(meth, code_or_doc) - if rvm_ruby?(Rubinius::BIN_PATH) - rvm_rbx_core_code_or_doc_for(meth, code_or_doc) - else - std_rbx_core_code_or_doc_for(meth, code_or_doc) + path_line = rbx_core_path_line_for(meth) + + case code_or_doc + when :code + MethodSource.source_helper(path_line) + when :doc + MethodSource.comment_helper(path_line) end end - def std_rbx_core_code_or_doc_for(meth, code_or_doc) + def rbx_core_path_line_for(meth) + if rvm_ruby?(Rubinius::BIN_PATH) + rvm_rbx_core_path_line_for(meth) + else + std_rbx_core_path_line_for(meth) + end + end + + def std_rbx_core_path_line_for(meth) file_name = File.join(Rubinius::BIN_PATH, "..", meth.source_location.first) raise "Cannot find rbx core source" if !File.exists?(file_name) - start_line meth.source_location.last - case code_or_doc - when :code - MethodSource.source_helper([file_name, start_line]) - when :doc - MethodSource.comment_helper([file_name, start_line]) - end - end - - def rvm_rbx_core_code_or_doc_for(meth, code_or_doc) - ruby_name = File.dirname(Rubinius::BIN_PATH).split("/").last - source_path = File.join(File.dirname(File.dirname(File.dirname(Rubinius::BIN_PATH))), "src", ruby_name) - file_name = File.join(source_path, meth.source_location.first) start_line = meth.source_location.last - case code_or_doc - when :code - MethodSource.source_helper([file_name, start_line]) - when :doc - MethodSource.comment_helper([file_name, start_line]) - end + [file_name, start_line] + end + + def rvm_rbx_core_path_line_for(meth) + ruby_name = File.dirname(Rubinius::BIN_PATH).split("/").last + source_path = File.join(File.dirname(File.dirname(File.dirname(Rubinius::BIN_PATH))), "src", ruby_name) + + file_name = File.join(source_path, meth.source_location.first) + raise "Cannot find rbx core source" if !File.exists?(file_name) + + start_line = meth.source_location.last + + [file_name, start_line] end ######### END RBX HELPERS ############### From 81d44b06740a1a7f3ff0415202a65a9e44ed6330 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 21:50:50 +1200 Subject: [PATCH 11/21] allow edit-method to edit rbx core methods using the full path; also added path_line_for helper to wrap source_location and so support rbx core methods too --- lib/pry/default_commands/introspection.rb | 7 ++++++- lib/pry/helpers/command_helpers.rb | 14 +++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/pry/default_commands/introspection.rb b/lib/pry/default_commands/introspection.rb index 42147ea4..14fc51fd 100644 --- a/lib/pry/default_commands/introspection.rb +++ b/lib/pry/default_commands/introspection.rb @@ -136,7 +136,12 @@ class Pry # editor is invoked here else - file, line = meth.source_location + if rbx_core?(meth) + file, line = rbx_core_path_line_for(meth) + else + file, line = meth.source_location + end + set_file_and_dir_locals(file) if Pry.editor.respond_to?(:call) diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 4a7cd107..990eb610 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -129,7 +129,7 @@ class Pry code = strip_leading_whitespace(meth.source) end end - set_file_and_dir_locals(meth.source_location.first) + set_file_and_dir_locals(path_line_for(meth).first) end [code, code_type] @@ -147,7 +147,7 @@ class Pry else doc = strip_leading_hash_and_whitespace_from_ruby_comments(meth.comment) end - set_file_and_dir_locals(meth.source_location.first) + set_file_and_dir_locals(path_line_for(meth).first) end [doc, code_type] @@ -187,11 +187,19 @@ class Pry end end + def path_line_for(meth) + if rbx_core?(meth) + rbx_core_path_line_for(meth) + else + meth.source_location + end + end + def make_header(meth, code_type, content) num_lines = "Number of lines: #{Pry::Helpers::Text.bold(content.each_line.count.to_s)}" case code_type when :ruby - file, line = meth.source_location + file, line = path_line_for(meth) "\n#{Pry::Helpers::Text.bold('From:')} #{file} @ line #{line}:\n#{num_lines}\n\n" else file = Pry::MethodInfo.info_for(meth).file From 5c9dbe82b35804c71f8ad25c7bf57a291dd26226 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 21:51:41 +1200 Subject: [PATCH 12/21] removed obsolete try_to_load_pry_doc helper --- lib/pry/helpers/command_helpers.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 990eb610..283348de 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -5,15 +5,6 @@ class Pry module_function - def try_to_load_pry_doc - - # YARD crashes on rbx, so do not require it - if !Object.const_defined?(:RUBY_ENGINE) || RUBY_ENGINE !~ /rbx/ - require "pry-doc" - end - rescue LoadError - end - def meth_name_from_binding(b) meth_name = b.eval('__method__') if [:__script__, nil, :__binding__, :__binding_impl__].include?(meth_name) From cef1d2f6c6e51c0cee2bc5bba27d6bda415abb0b Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 21:55:06 +1200 Subject: [PATCH 13/21] just substituted 'text' for 'doc' in render_output() helper method for clarity --- lib/pry/helpers/command_helpers.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 283348de..dea56fcf 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -15,15 +15,15 @@ class Pry end # if start_line is not false then add line numbers starting with start_line - def render_output(should_flood, start_line, doc) + def render_output(should_flood, start_line, text) if start_line - doc = Pry::Helpers::Text.with_line_numbers doc, start_line + text = Pry::Helpers::Text.with_line_numbers text, start_line end if should_flood - output.puts doc + output.puts text else - stagger_output(doc) + stagger_output(text) end end From 9a245adb04d9dcfb0d0475931e187b38f9bcf099 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 21:55:23 +1200 Subject: [PATCH 14/21] added tests for show-input and \! commands --- test/test_default_commands/test_input.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test_default_commands/test_input.rb b/test/test_default_commands/test_input.rb index 6831563e..3a20162b 100644 --- a/test/test_default_commands/test_input.rb +++ b/test/test_default_commands/test_input.rb @@ -20,6 +20,28 @@ describe "Pry::DefaultCommands::Input" do end end + describe "show-input" do + it 'should correctly show the current lines in the input buffer' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("def hello", "puts :bing", "show-input", "exit-all"), str_output) do + pry + end + str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing/ + end + end + + describe "!" do + it 'should correctly clear the input buffer ' do + str_output = StringIO.new + redirect_pry_io(InputTester.new("def hello", "puts :bing", "!", "show-input", "exit-all"), str_output) do + pry + end + stripped_output = str_output.string.strip! + stripped_output.each_line.count.should == 1 + stripped_output.should =~ /Input buffer cleared!/ + end + end + describe "hist" do push_first_hist_line = lambda do |hist, line| hist.push line From 166e6e8f0bdb96669a89e10b3455776735b896a2 Mon Sep 17 00:00:00 2001 From: John Mair Date: Sat, 4 Jun 2011 01:08:42 +1200 Subject: [PATCH 15/21] added --no-plugins command line option to executable, removed unnecessary whitespace from introspectin.rb --- bin/pry | 5 ++++- lib/pry/default_commands/introspection.rb | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/pry b/bin/pry index 4cfd464b..0a0d4b7e 100644 --- a/bin/pry +++ b/bin/pry @@ -22,7 +22,7 @@ See: `https://github.com/banister` for more information. on :e, :exec, "A line of code to execute in context before the session starts", true on :f, "Suppress loading of ~/.pryrc" on "no-color", "Disable syntax highlighting for session" - + on "no-plugins", "Suppress loading of plugins." on "simple-prompt", "Enable simple prompt mode" do Pry.prompt = Pry::SIMPLE_PROMPT end @@ -53,6 +53,9 @@ Pry.cli = true # load ~/.pryrc, if not suppressed with -f option Pry.config.should_load_rc = !opts.f? +# suppress plugins if given --no-plugins optino +Pry.config.plugins.enabled = false if opts["no-plugins"] + # create the actual context context = Pry.binding_for(eval(opts[:context])) diff --git a/lib/pry/default_commands/introspection.rb b/lib/pry/default_commands/introspection.rb index 14fc51fd..118d1e1c 100644 --- a/lib/pry/default_commands/introspection.rb +++ b/lib/pry/default_commands/introspection.rb @@ -159,7 +159,6 @@ class Pry end end - helpers do def start_line_for_editor(line_number) From ffb9670b7ffb5ff0ce213c60a70ab146645f7454 Mon Sep 17 00:00:00 2001 From: John Mair Date: Sat, 4 Jun 2011 03:09:24 +1200 Subject: [PATCH 16/21] replaced implementation tests in edit-method with path_line_for --- lib/pry/default_commands/introspection.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/pry/default_commands/introspection.rb b/lib/pry/default_commands/introspection.rb index 118d1e1c..cd528813 100644 --- a/lib/pry/default_commands/introspection.rb +++ b/lib/pry/default_commands/introspection.rb @@ -136,12 +136,7 @@ class Pry # editor is invoked here else - if rbx_core?(meth) - file, line = rbx_core_path_line_for(meth) - else - file, line = meth.source_location - end - + file, line = path_line_for(meth) set_file_and_dir_locals(file) if Pry.editor.respond_to?(:call) From 570ff75cdd2f9a064a0d6c7eec7f92d273ce1264 Mon Sep 17 00:00:00 2001 From: John Mair Date: Sun, 5 Jun 2011 03:59:05 +1200 Subject: [PATCH 17/21] added --no-pager to binary options, and refactored options --- bin/pry | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/bin/pry b/bin/pry index 0a0d4b7e..e1100070 100644 --- a/bin/pry +++ b/bin/pry @@ -20,9 +20,25 @@ See: `https://github.com/banister` for more information. } on :e, :exec, "A line of code to execute in context before the session starts", true - on :f, "Suppress loading of ~/.pryrc" - on "no-color", "Disable syntax highlighting for session" - on "no-plugins", "Suppress loading of plugins." + + on "no-pager", "Disable pager for long output" do + Pry.pager = false + end + + on "no-color", "Disable syntax highlighting for session" do + Pry.color = false + end + + on :f, "Suppress loading of ~/.pryrc" do + # load ~/.pryrc, if not suppressed with -f option + Pry.config.should_load_rc = false + end + + on "no-plugins", "Suppress loading of plugins." do + # suppress plugins if given --no-plugins optino + Pry.config.plugins.enabled = false + end + on "simple-prompt", "Enable simple prompt mode" do Pry.prompt = Pry::SIMPLE_PROMPT end @@ -50,12 +66,6 @@ end # invoked via cli Pry.cli = true -# load ~/.pryrc, if not suppressed with -f option -Pry.config.should_load_rc = !opts.f? - -# suppress plugins if given --no-plugins optino -Pry.config.plugins.enabled = false if opts["no-plugins"] - # create the actual context context = Pry.binding_for(eval(opts[:context])) From 6d4e30f4a7a74f3cbdad0724fcb8e11c58434f6d Mon Sep 17 00:00:00 2001 From: John Mair Date: Sun, 5 Jun 2011 04:01:12 +1200 Subject: [PATCH 18/21] switched shortoptions for --no-jump and --no-reload for edit-method, -n now means --no-reload --- lib/pry/default_commands/introspection.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pry/default_commands/introspection.rb b/lib/pry/default_commands/introspection.rb index cd528813..00a14243 100644 --- a/lib/pry/default_commands/introspection.rb +++ b/lib/pry/default_commands/introspection.rb @@ -109,8 +109,8 @@ class Pry opt.on :M, "instance-methods", "Operate on instance methods." opt.on :m, :methods, "Operate on methods." - opt.on "no-reload", "Do not automatically reload the method's file after editting." - opt.on :n, "no-jump", "Do not fast forward editor to first line of method." + opt.on :n, "no-reload", "Do not automatically reload the method's file after editting." + opt.on "no-jump", "Do not fast forward editor to first line of method." opt.on :c, :context, "Select object context to run under.", true do |context| target = Pry.binding_for(target.eval(context)) end @@ -143,13 +143,13 @@ class Pry editor_invocation = Pry.editor.call(file, line) else # only use start line if -n option is not used - start_line_syntax = opts.n? ? "" : start_line_for_editor(line) + start_line_syntax = opts["no-jump"] ? "" : start_line_for_editor(line) editor_invocation = "#{Pry.editor} #{start_line_syntax} #{file}" end run ".#{editor_invocation}" silence_warnings do - load file if !opts["no-reload"] + load file if !opts.n? end end end From 71ccee62c87a3122f234488f4a698910767b38ef Mon Sep 17 00:00:00 2001 From: John Mair Date: Sun, 5 Jun 2011 04:01:36 +1200 Subject: [PATCH 19/21] bump version to 0.9.0pre2 --- lib/pry/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/version.rb b/lib/pry/version.rb index 248290a6..6bfb58fe 100644 --- a/lib/pry/version.rb +++ b/lib/pry/version.rb @@ -1,3 +1,3 @@ class Pry - VERSION = "0.9.0pre1" + VERSION = "0.9.0pre2" end From 3b1cac2efd0d96c4df198b87320ba1f39be3ba38 Mon Sep 17 00:00:00 2001 From: John Mair Date: Mon, 6 Jun 2011 03:30:36 +1200 Subject: [PATCH 20/21] fixed regex command string interpolation --- lib/pry/command_processor.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/pry/command_processor.rb b/lib/pry/command_processor.rb index cbc0e585..69fdd8f0 100644 --- a/lib/pry/command_processor.rb +++ b/lib/pry/command_processor.rb @@ -67,16 +67,15 @@ class Pry # no command was matched, so return to caller return if !valid_command?(val) command, captures, pos = command_matched(val) - + arg_string = val[pos..-1].strip # perform ruby interpolation for commands if command.options[:interpolate] val.replace interpolate_string(val, target) - # command, captures, pos = command_matched(val) -# captures = captures.map { |v| interpolate_string(v, target) if v } + arg_string.replace interpolate_string(arg_string, target) + captures = captures.map { |v| interpolate_string(v, target) if v } end - arg_string = val[pos..-1].strip args = arg_string ? Shellwords.shellwords(arg_string) : [] options = { From 12956b20b9e3f8c839544febe221d1ca3b69c2ea Mon Sep 17 00:00:00 2001 From: John Mair Date: Mon, 6 Jun 2011 03:31:36 +1200 Subject: [PATCH 21/21] added tests for regex command interpolation behaviour. All test in test suite should now pass. --- test/test_pry.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/test_pry.rb b/test/test_pry.rb index b3a9b1d2..5c102cfa 100644 --- a/test/test_pry.rb +++ b/test/test_pry.rb @@ -492,11 +492,33 @@ describe Pry do Pry.new(:commands => set).rep end -# binding.pry str_output.string.should =~ /hello bing/ $obj = nil end + it 'should create a regex command and arg_string should be interpolated' do + set = Pry::CommandSet.new do + command /hello(\w+)/, "" do |c1, a1, a2, a3| + output.puts "hello #{c1} #{a1} #{a2} #{a3}" + end + end + + str_output = StringIO.new + $a1 = "bing" + $a2 = "bong" + $a3 = "bang" + redirect_pry_io(InputTester.new('hellojohn #{$a1} #{$a2} #{$a3}'), str_output) do + Pry.new(:commands => set).rep + end + + str_output.string.should =~ /hello john bing bong bang/ + + $a1 = nil + $a2 = nil + $a3 = nil + end + + it 'if a regex capture is missing it should be nil' do set = Pry::CommandSet.new do command /hello(.)?/, "" do |c1, a1|