diff --git a/Rakefile b/Rakefile index 412e9b16..62e804cb 100644 --- a/Rakefile +++ b/Rakefile @@ -49,7 +49,7 @@ task :test do check_dependencies unless ENV['SKIP_DEP_CHECK'] all_specs = Dir['spec/**/*_spec.rb'] all_specs.shuffle! if all_specs.respond_to? :shuffle! - system "bacon -Ilib/pry/test -Ispec -rubygems -a -q #{all_specs.join ' '}" + system "bacon -Ispec -rubygems -a -q #{all_specs.join ' '}" end task :spec => :test diff --git a/lib/pry/test/helper.rb b/lib/pry/test/helper.rb index 86a594e5..058da260 100644 --- a/lib/pry/test/helper.rb +++ b/lib/pry/test/helper.rb @@ -1,60 +1,9 @@ -unless Object.const_defined? 'Pry' - $:.unshift File.expand_path '../../../../lib', __FILE__ - require 'pry' -end +require 'pry' puts "Ruby v#{RUBY_VERSION} (#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"}), Pry v#{Pry::VERSION}, method_source v#{MethodSource::VERSION}, CodeRay v#{CodeRay::VERSION}, Slop v#{Slop::VERSION}" -require File.join(File.expand_path(File.dirname(__FILE__)), 'bacon_helper') if defined?(Bacon) - -# A global space for storing temporary state during tests. -Pad = OpenStruct.new -def Pad.clear - @table = {} -end - -# turn warnings off (esp for Pry::Hooks which will generate warnings -# in tests) -$VERBOSE = nil - -# inject a variable into a binding -def inject_var(name, value, b) - Thread.current[:__pry_local__] = value - b.eval("#{name} = Thread.current[:__pry_local__]") -ensure - Thread.current[:__pry_local__] = nil -end - -def constant_scope(*names) - names.each do |name| - Object.remove_const name if Object.const_defined?(name) - end - - yield -ensure - names.each do |name| - Object.remove_const name if Object.const_defined?(name) - end -end - -def mri18_and_no_real_source_location? - Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method) -end - -# used by test_show_source.rb and test_documentation.rb -class TestClassForShowSource - def alpha - end -end - -class TestClassForShowSourceClassEval - def alpha - end -end - -class TestClassForShowSourceInstanceEval - def alpha - end +if defined?(Bacon) + require File.join(File.expand_path(File.dirname(__FILE__)), 'bacon_helper') end # in case the tests call reset_defaults, ensure we reset them to @@ -76,42 +25,51 @@ class << Pry Pry.config.collision_warning = false end end - -def mock_exception(*mock_backtrace) - e = StandardError.new("mock exception") - (class << e; self; end).class_eval do - define_method(:backtrace) { mock_backtrace } - end - e -end - Pry.reset_defaults -# this is to test exception code (cat --ex) -def broken_method - this method is broken +# A global space for storing temporary state during tests. +Pad = OpenStruct.new +def Pad.clear + @table = {} end -# sample doc -def sample_method - :sample -end +module PryTestHelpers + # inject a variable into a binding + def self.inject_var(name, value, b) + Thread.current[:__pry_local__] = value + b.eval("#{name} = Thread.current[:__pry_local__]") + ensure + Thread.current[:__pry_local__] = nil + end -# Set I/O streams. -# -# Out defaults to an anonymous StringIO. -# -def redirect_pry_io(new_in, new_out = StringIO.new) - old_in = Pry.input - old_out = Pry.output + def self.constant_scope(*names) + names.each do |name| + Object.remove_const name if Object.const_defined?(name) + end - Pry.input = new_in - Pry.output = new_out - begin yield ensure - Pry.input = old_in - Pry.output = old_out + names.each do |name| + Object.remove_const name if Object.const_defined?(name) + end + end + + def self.mri18_and_no_real_source_location? + Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method) + end + + # Open a temp file and yield it to the block, closing it after + # @return [String] The path of the temp file + def self.temp_file(ext='.rb') + file = Tempfile.new(['pry', ext]) + yield file + ensure + file.close(true) if file + File.unlink("#{file.path}c") if File.exists?("#{file.path}c") # rbx + end + + def self.unindent(*args) + Pry::Helpers::CommandHelpers.unindent(*args) end end @@ -135,64 +93,12 @@ def mock_command(cmd, args=[], opts={}) Struct.new(:output, :return).new(output.string, ret) end -def redirect_global_pry_input(new_io) - old_io = Pry.input - Pry.input = new_io - begin - yield - ensure - Pry.input = old_io - end -end - -def redirect_global_pry_output(new_io) - old_io = Pry.output - Pry.output = new_io - begin - yield - ensure - Pry.output = old_io - end -end - -class Module - public :remove_const - public :remove_method -end - - -class InputTester - def initialize(*actions) - @orig_actions = actions.dup - @actions = actions +def mock_exception(*mock_backtrace) + e = StandardError.new("mock exception") + (class << e; self; end).class_eval do + define_method(:backtrace) { mock_backtrace } end - - def readline(*) - @actions.shift - end - - def rewind - @actions = @orig_actions.dup - end -end - -class Pry - - # null output class - doesn't write anywwhere. - class NullOutput - def self.puts(*) end - def self.string(*) end - end -end - -# Open a temp file and yield it to the block, closing it after -# @return [String] The path of the temp file -def temp_file(ext='.rb') - file = Tempfile.new(['pry', ext]) - yield file -ensure - file.close(true) if file - File.unlink("#{file.path}c") if File.exists?("#{file.path}c") # rbx + e end def pry_tester(*args, &block) @@ -298,25 +204,3 @@ class PryTester @pry.output = @out end end - -CommandTester = Pry::CommandSet.new do - command "command1", "command 1 test" do - output.puts "command1" - end - - command "command2", "command 2 test" do |arg| - output.puts arg - end -end - -def unindent(*args) - Pry::Helpers::CommandHelpers.unindent(*args) -end - -# to help with tracking down bugs that cause an infinite loop in the test suite -if ENV["SET_TRACE_FUNC"] - require 'set_trace' if Pry::Helpers::BaseHelpers.rbx? - set_trace_func proc { |event, file, line, id, binding, classname| - STDERR.printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname - } -end diff --git a/spec/code_spec.rb b/spec/code_spec.rb index 520df34d..6ed09098 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -12,19 +12,19 @@ describe Pry::Code do end should 'default to Ruby' do - temp_file('') do |f| + PryTestHelpers.temp_file('') do |f| Pry::Code.from_file(f.path).code_type.should == :ruby end end should 'check the extension' do - temp_file('.c') do |f| + PryTestHelpers.temp_file('.c') do |f| Pry::Code.from_file(f.path).code_type.should == :c end end should 'use the provided extension' do - temp_file('.c') do |f| + PryTestHelpers.temp_file('.c') do |f| Pry::Code.from_file(f.path, :ruby).code_type.should == :ruby end end diff --git a/spec/command_integration_spec.rb b/spec/command_integration_spec.rb index 5fba8c10..7287406f 100644 --- a/spec/command_integration_spec.rb +++ b/spec/command_integration_spec.rb @@ -1,5 +1,6 @@ require 'helper' + describe "commands" do before do @str_output = StringIO.new @@ -12,6 +13,16 @@ describe "commands" do @self = "Pad.self = self" + @command_tester = Pry::CommandSet.new do + command "command1", "command 1 test" do + output.puts "command1" + end + + command "command2", "command 2 test" do |arg| + output.puts arg + end + end + Pad.bong = "bong" end @@ -46,17 +57,10 @@ describe "commands" do alias_command "test-alias", "test-command" end - redirect_pry_io(InputTester.new("test-command hello baby duck"), out1 = StringIO.new) do - Pry.start self, :commands => set - end + t = pry_tester(:commands => set) - out1.string.should =~ /hello baby duck/ - - redirect_pry_io(InputTester.new("test-alias hello baby duck"), out2 = StringIO.new) do - Pry.start self, :commands => set - end - - out2.string.should == out1.string + t.process_command "test-alias hello baby duck" + t.last_output.should =~ /testing hello baby duck/ end it 'should pass option arguments to original' do @@ -66,17 +70,10 @@ describe "commands" do end obj = Class.new { @x = 10 } - redirect_pry_io(InputTester.new("ls -i"), out1 = StringIO.new) do - Pry.start obj, :commands => set - end + t = pry_tester(obj, :commands => set) - out1.string.should =~ /@x/ - - redirect_pry_io(InputTester.new("test-alias -i"), out2 = StringIO.new) do - Pry.start obj, :commands => set - end - - out2.string.should == out1.string + t.process_command "test-alias -i" + t.last_output.should =~ /@x/ end it 'should pass option arguments to original with additional parameters' do @@ -86,17 +83,9 @@ describe "commands" do end obj = Class.new { @x = Class.new { define_method(:plymouth) {} } } - redirect_pry_io(InputTester.new("ls -M @x"), out1 = StringIO.new) do - Pry.start obj, :commands => set - end - - out1.string.should =~ /plymouth/ - - redirect_pry_io(InputTester.new("test-alias @x"), out2 = StringIO.new) do - Pry.start obj, :commands => set - end - - out2.string.should == out1.string + t = pry_tester(obj, :commands => set) + t.process_command "test-alias @x" + t.last_output.should =~ /plymouth/ end it 'should be able to alias a regex command' do @@ -107,11 +96,9 @@ describe "commands" do alias_command "test-alias", "duck" end - redirect_pry_io(InputTester.new("test-alias"), out1 = StringIO.new) do - Pry.start self, :commands => set - end - - out1.string.should =~ /ducky/ + t = pry_tester(:commands => set) + t.process_command "test-alias" + t.last_output.should =~ /ducky/ end it 'should be able to make the alias a regex' do @@ -138,6 +125,7 @@ describe "commands" do run "cd / " end end + redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6", @bs1, "test-run", @self, @bs2, "exit-all")) do Pry.start(@o, :commands => set) @@ -630,9 +618,9 @@ describe "commands" do it 'should run a command with no parameter' do pry_tester = Pry.new - pry_tester.commands = CommandTester + pry_tester.commands = @command_tester pry_tester.input = InputTester.new("command1", "exit-all") - pry_tester.commands = CommandTester + pry_tester.commands = @command_tester pry_tester.output = @str_output @@ -643,9 +631,9 @@ describe "commands" do it 'should run a command with one parameter' do pry_tester = Pry.new - pry_tester.commands = CommandTester + pry_tester.commands = @command_tester pry_tester.input = InputTester.new("command2 horsey", "exit-all") - pry_tester.commands = CommandTester + pry_tester.commands = @command_tester pry_tester.output = @str_output diff --git a/spec/command_spec.rb b/spec/command_spec.rb index 57242141..92ef5f14 100644 --- a/spec/command_spec.rb +++ b/spec/command_spec.rb @@ -469,7 +469,7 @@ describe "Pry::Command" do before do @context = Object.new @set.command "walking-spanish", "down the hall", :takes_block => true do - inject_var(:@x, command_block.call, target) + PryTestHelpers.inject_var(:@x, command_block.call, target) end @set.import Pry::Commands @@ -490,9 +490,9 @@ describe "Pry::Command" do @set.block_command "walking-spanish", "litella's been screeching for a blind pig.", :takes_block => true do |x, y| - inject_var(:@x, x, target) - inject_var(:@y, y, target) - inject_var(:@block_var, command_block.call, target) + PryTestHelpers.inject_var(:@x, x, target) + PryTestHelpers.inject_var(:@y, y, target) + PryTestHelpers.inject_var(:@block_var, command_block.call, target) end @t.eval 'walking-spanish john carl| { :jesus }' @@ -524,8 +524,8 @@ describe "Pry::Command" do describe "arg_string" do it 'should remove block-related content from arg_string (with one normal arg)' do @set.block_command "walking-spanish", "down the hall", :takes_block => true do |x, y| - inject_var(:@arg_string, arg_string, target) - inject_var(:@x, x, target) + PryTestHelpers.inject_var(:@arg_string, arg_string, target) + PryTestHelpers.inject_var(:@x, x, target) end @t.eval 'walking-spanish john| { :jesus }' @@ -535,7 +535,7 @@ describe "Pry::Command" do it 'should remove block-related content from arg_string (with no normal args)' do @set.block_command "walking-spanish", "down the hall", :takes_block => true do - inject_var(:@arg_string, arg_string, target) + PryTestHelpers.inject_var(:@arg_string, arg_string, target) end @t.eval 'walking-spanish | { :jesus }' @@ -546,7 +546,7 @@ describe "Pry::Command" do it 'should NOT remove block-related content from arg_string when :takes_block => false' do block_string = "| { :jesus }" @set.block_command "walking-spanish", "homemade special", :takes_block => false do - inject_var(:@arg_string, arg_string, target) + PryTestHelpers.inject_var(:@arg_string, arg_string, target) end @t.eval "walking-spanish #{block_string}" @@ -559,8 +559,8 @@ describe "Pry::Command" do describe "block_command" do it "should remove block-related content from arguments" do @set.block_command "walking-spanish", "glass is full of sand", :takes_block => true do |x, y| - inject_var(:@x, x, target) - inject_var(:@y, y, target) + PryTestHelpers.inject_var(:@x, x, target) + PryTestHelpers.inject_var(:@y, y, target) end @t.eval 'walking-spanish | { :jesus }' @@ -571,8 +571,8 @@ describe "Pry::Command" do it "should NOT remove block-related content from arguments if :takes_block => false" do @set.block_command "walking-spanish", "litella screeching for a blind pig", :takes_block => false do |x, y| - inject_var(:@x, x, target) - inject_var(:@y, y, target) + PryTestHelpers.inject_var(:@x, x, target) + PryTestHelpers.inject_var(:@y, y, target) end @t.eval 'walking-spanish | { :jesus }' @@ -586,8 +586,8 @@ describe "Pry::Command" do it "should remove block-related content from arguments" do @set.create_command "walking-spanish", "punk sanders carved one out of wood", :takes_block => true do def process(x, y) - inject_var(:@x, x, target) - inject_var(:@y, y, target) + PryTestHelpers.inject_var(:@x, x, target) + PryTestHelpers.inject_var(:@y, y, target) end end @@ -600,8 +600,8 @@ describe "Pry::Command" do it "should NOT remove block-related content from arguments if :takes_block => false" do @set.create_command "walking-spanish", "down the hall", :takes_block => false do def process(x, y) - inject_var(:@x, x, target) - inject_var(:@y, y, target) + PryTestHelpers.inject_var(:@x, x, target) + PryTestHelpers.inject_var(:@y, y, target) end end @@ -618,7 +618,7 @@ describe "Pry::Command" do describe "{} style blocks" do it 'should accept multiple parameters' do @set.block_command "walking-spanish", "down the hall", :takes_block => true do - inject_var(:@x, command_block.call(1, 2), target) + PryTestHelpers.inject_var(:@x, command_block.call(1, 2), target) end @t.eval 'walking-spanish | { |x, y| [x, y] }' @@ -631,7 +631,7 @@ describe "Pry::Command" do it 'should accept multiple parameters' do @set.create_command "walking-spanish", "litella", :takes_block => true do def process - inject_var(:@x, command_block.call(1, 2), target) + PryTestHelpers.inject_var(:@x, command_block.call(1, 2), target) end end @@ -657,7 +657,7 @@ describe "Pry::Command" do describe "block_command" do it "should expose block in command_block method" do @set.block_command "walking-spanish", "glass full of sand", :takes_block => true do - inject_var(:@x, command_block.call, target) + PryTestHelpers.inject_var(:@x, command_block.call, target) end @t.eval 'walking-spanish | { :jesus }' @@ -683,7 +683,7 @@ describe "Pry::Command" do it "should expose block in command_block method" do @set.create_command "walking-spanish", "homemade special", :takes_block => true do def process - inject_var(:@x, command_block.call, target) + PryTestHelpers.inject_var(:@x, command_block.call, target) end end @@ -719,7 +719,7 @@ describe "Pry::Command" do pry_eval("my---test").should =~ /my-testmy-test/ end - if !mri18_and_no_real_source_location? + if !PryTestHelpers.mri18_and_no_real_source_location? it "should show the source of the process method" do pry_eval("show-source my-test").should =~ /output.puts command_name/ end diff --git a/spec/commands/amend_line_spec.rb b/spec/commands/amend_line_spec.rb index 86526137..81bba2c7 100644 --- a/spec/commands/amend_line_spec.rb +++ b/spec/commands/amend_line_spec.rb @@ -6,21 +6,21 @@ describe "amend-line" do end it 'should amend the last line of input when no line number specified' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing STR @t.process_command 'amend-line puts :blah', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :blah STR end it 'should amend the specified line of input when line number given' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -28,7 +28,7 @@ describe "amend-line" do @t.process_command 'amend-line 1 def goodbye', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def goodbye puts :bing puts :bang @@ -36,7 +36,7 @@ describe "amend-line" do end it 'should amend the first line of input when 0 given as line number' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -44,7 +44,7 @@ describe "amend-line" do @t.process_command 'amend-line 0 def goodbye', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def goodbye puts :bing puts :bang @@ -52,7 +52,7 @@ describe "amend-line" do end it 'should amend a specified line when negative number given' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -60,7 +60,7 @@ describe "amend-line" do @t.process_command 'amend-line -1 puts :bink', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bink @@ -68,7 +68,7 @@ describe "amend-line" do @t.process_command 'amend-line -2 puts :bink', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :bink puts :bink @@ -76,7 +76,7 @@ describe "amend-line" do end it 'should amend a range of lines of input when negative numbers given' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -85,7 +85,7 @@ describe "amend-line" do @t.process_command 'amend-line -3..-2 puts :bink', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :bink puts :boat @@ -93,7 +93,7 @@ describe "amend-line" do end it 'should correctly amend the specified line with interpolated text' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -101,7 +101,7 @@ describe "amend-line" do @t.process_command 'amend-line puts "#{goodbye}"', eval_str - eval_str.should == unindent(<<-'STR') + eval_str.should == PryTestHelpers.unindent(<<-'STR') def hello puts :bing puts "#{goodbye}" @@ -122,7 +122,7 @@ describe "amend-line" do end it 'should correctly amend the specified range of lines' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -131,7 +131,7 @@ describe "amend-line" do @t.process_command 'amend-line 2..3 puts :bong', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :bong puts :heart @@ -139,7 +139,7 @@ describe "amend-line" do end it 'should correctly delete a specific line using the ! for content' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -149,7 +149,7 @@ describe "amend-line" do @t.process_command 'amend-line 3 !', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :boast @@ -158,7 +158,7 @@ describe "amend-line" do end it 'should correctly delete a range of lines using the ! for content' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -168,14 +168,14 @@ describe "amend-line" do @t.process_command 'amend-line 2..4 !', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :heart STR end it 'should correctly delete the previous line using the ! for content' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -185,7 +185,7 @@ describe "amend-line" do @t.process_command 'amend-line !', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -194,7 +194,7 @@ describe "amend-line" do end it 'should amend the specified range of lines, with numbers < 0 in range' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -204,7 +204,7 @@ describe "amend-line" do @t.process_command 'amend-line 2..-2 puts :bong', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :bong puts :heart @@ -212,7 +212,7 @@ describe "amend-line" do end it 'should correctly insert a line before a specified line using >' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -220,7 +220,7 @@ describe "amend-line" do @t.process_command 'amend-line 2 > puts :inserted', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :inserted puts :bing @@ -229,7 +229,7 @@ describe "amend-line" do end it 'should ignore second value of range with > syntax' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing puts :bang @@ -237,7 +237,7 @@ describe "amend-line" do @t.process_command 'amend-line 2..21 > puts :inserted', eval_str - eval_str.should == unindent(<<-STR) + eval_str.should == PryTestHelpers.unindent(<<-STR) def hello puts :inserted puts :bing @@ -245,4 +245,3 @@ describe "amend-line" do STR end end - diff --git a/spec/commands/bang_spec.rb b/spec/commands/bang_spec.rb index 6200db37..949b3215 100644 --- a/spec/commands/bang_spec.rb +++ b/spec/commands/bang_spec.rb @@ -6,7 +6,7 @@ describe "!" do end it 'should correctly clear the input buffer ' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing STR diff --git a/spec/commands/cat_spec.rb b/spec/commands/cat_spec.rb index 217754b4..a2e36707 100644 --- a/spec/commands/cat_spec.rb +++ b/spec/commands/cat_spec.rb @@ -25,7 +25,7 @@ describe "cat" do describe "with --in" do it 'should display the last few expressions with indices' do - @t.eval('10', '20', 'cat --in').should == unindent(<<-STR) + @t.eval('10', '20', 'cat --in').should == PryTestHelpers.unindent(<<-STR) 1: 10 2: @@ -52,7 +52,7 @@ describe "cat" do @t.insert_nil_input # normally happens when a command is executed @t.eval ':hello' - @t.eval('cat --in 1..3').should == unindent(<<-EOS) + @t.eval('cat --in 1..3').should == PryTestHelpers.unindent(<<-EOS) 1: 10 3: @@ -64,9 +64,18 @@ describe "cat" do # this doesnt work so well on rbx due to differences in backtrace # so we currently skip rbx until we figure out a workaround describe "with --ex" do + before do + @o = Object.new + + # this is to test exception code (cat --ex) + def @o.broken_method + this method is broken + end + end + if !Pry::Helpers::BaseHelpers.rbx? it 'cat --ex should display repl code that generated exception' do - @t.eval unindent(<<-EOS) + @t.eval PryTestHelpers.unindent(<<-EOS) begin this raises error rescue => e @@ -78,7 +87,7 @@ describe "cat" do it 'cat --ex should correctly display code that generated exception' do begin - broken_method + @o.broken_method rescue => e @t.last_exception = e end @@ -89,7 +98,7 @@ describe "cat" do describe "with --ex N" do it 'should cat first level of backtrace when --ex used with no argument ' do - temp_file do |f| + PryTestHelpers.temp_file do |f| f << "bt number 1" f.flush @t.last_exception = mock_exception("#{f.path}:1", 'x', 'x') @@ -98,7 +107,7 @@ describe "cat" do end it 'should cat first level of backtrace when --ex 0 used ' do - temp_file do |f| + PryTestHelpers.temp_file do |f| f << "bt number 1" f.flush @t.last_exception = mock_exception("#{f.path}:1", 'x', 'x') @@ -107,7 +116,7 @@ describe "cat" do end it 'should cat second level of backtrace when --ex 1 used ' do - temp_file do |f| + PryTestHelpers.temp_file do |f| f << "bt number 2" f.flush @t.last_exception = mock_exception('x', "#{f.path}:1", 'x') @@ -116,7 +125,7 @@ describe "cat" do end it 'should cat third level of backtrace when --ex 2 used' do - temp_file do |f| + PryTestHelpers.temp_file do |f| f << "bt number 3" f.flush @t.last_exception = mock_exception('x', 'x', "#{f.path}:1") diff --git a/spec/commands/edit_spec.rb b/spec/commands/edit_spec.rb index 874ae7d1..871eed2c 100644 --- a/spec/commands/edit_spec.rb +++ b/spec/commands/edit_spec.rb @@ -49,7 +49,7 @@ describe "edit" do end it "should reload the file if it is a ruby file" do - temp_file do |tf| + PryTestHelpers.temp_file do |tf| counter = Pad.counter path = tf.path @@ -60,7 +60,7 @@ describe "edit" do end it "should not reload the file if it is not a ruby file" do - temp_file('.py') do |tf| + PryTestHelpers.temp_file('.py') do |tf| counter = Pad.counter path = tf.path @@ -71,7 +71,7 @@ describe "edit" do end it "should not reload a ruby file if -n is given" do - temp_file do |tf| + PryTestHelpers.temp_file do |tf| counter = Pad.counter path = tf.path @@ -80,7 +80,7 @@ describe "edit" do end it "should reload a non-ruby file if -r is given" do - temp_file('.pryrc') do |tf| + PryTestHelpers.temp_file('.pryrc') do |tf| counter = Pad.counter path = tf.path diff --git a/spec/commands/play_spec.rb b/spec/commands/play_spec.rb index c2e967ce..090377a4 100644 --- a/spec/commands/play_spec.rb +++ b/spec/commands/play_spec.rb @@ -81,7 +81,7 @@ describe "play" do pry_tester(@o).process_command 'play -d test_method', @eval_str - @eval_str.should == unindent(<<-STR) + @eval_str.should == PryTestHelpers.unindent(<<-STR) @v = 10 @y = 20 STR @@ -98,7 +98,7 @@ describe "play" do pry_tester(@o).process_command 'play -d test_method --lines 2..3', @eval_str - @eval_str.should == unindent(<<-STR) + @eval_str.should == PryTestHelpers.unindent(<<-STR) @v = 10 @y = 20 STR @@ -110,13 +110,13 @@ describe "play" do end it 'should APPEND to the input buffer when playing a line with play -m, not replace it' do - @eval_str = unindent(<<-STR) + @eval_str = PryTestHelpers.unindent(<<-STR) def another_test_method STR pry_tester(@o).process_command 'play -m test_method --lines 2', @eval_str - @eval_str.should == unindent(<<-STR) + @eval_str.should == PryTestHelpers.unindent(<<-STR) def another_test_method :test_method_content STR @@ -132,7 +132,7 @@ describe "play" do pry_tester(@o).process_command 'play -m test_method --lines 3..4', @eval_str - @eval_str.should == unindent(<<-STR, 2) + @eval_str.should == PryTestHelpers.unindent(<<-STR, 2) @var1 = 20 @var2 = 30 STR diff --git a/spec/commands/save_file_spec.rb b/spec/commands/save_file_spec.rb index 381cf2a6..51e347ce 100644 --- a/spec/commands/save_file_spec.rb +++ b/spec/commands/save_file_spec.rb @@ -13,7 +13,7 @@ describe "save-file" do describe "-f" do it 'should save a file to a file' do - temp_file do |f| + PryTestHelpers.temp_file do |f| path = f.path f.puts ":cute_horse" f.flush @@ -163,4 +163,3 @@ describe "save-file" do end end end - diff --git a/spec/commands/show_doc_spec.rb b/spec/commands/show_doc_spec.rb index 6ce624fe..8af22de3 100644 --- a/spec/commands/show_doc_spec.rb +++ b/spec/commands/show_doc_spec.rb @@ -1,21 +1,27 @@ require 'helper' +require "fixtures/show_source_doc_examples" -if !mri18_and_no_real_source_location? +if !PryTestHelpers.mri18_and_no_real_source_location? describe "show-doc" do before do @o = Object.new + + # sample doc + def @o.sample_method + :sample + end end it 'should output a method\'s documentation' do - pry_eval("show-doc sample_method").should =~ /sample doc/ + pry_eval(binding, "show-doc @o.sample_method").should =~ /sample doc/ end it 'should output a method\'s documentation with line numbers' do - pry_eval("show-doc sample_method -l").should =~ /\d: sample doc/ + pry_eval(binding, "show-doc @o.sample_method -l").should =~ /\d: sample doc/ end it 'should output a method\'s documentation with line numbers (base one)' do - pry_eval("show-doc sample_method -b").should =~ /1: sample doc/ + pry_eval(binding, "show-doc @o.sample_method -b").should =~ /1: sample doc/ end it 'should output a method\'s documentation if inside method without needing to use method name' do @@ -203,7 +209,7 @@ if !mri18_and_no_real_source_location? end it 'should lookup module name with respect to current context' do - constant_scope(:AlphaClass, :BetaClass) do + PryTestHelpers.constant_scope(:AlphaClass, :BetaClass) do # top-level beta class BetaClass def alpha @@ -223,7 +229,7 @@ if !mri18_and_no_real_source_location? end it 'should look up nested modules' do - constant_scope(:AlphaClass) do + PryTestHelpers.constant_scope(:AlphaClass) do class AlphaClass # nested beta class BetaClass diff --git a/spec/commands/show_input_spec.rb b/spec/commands/show_input_spec.rb index bf10bf5b..ae6cb7b9 100644 --- a/spec/commands/show_input_spec.rb +++ b/spec/commands/show_input_spec.rb @@ -6,7 +6,7 @@ describe "show-input" do end it 'should correctly show the current lines in the input buffer' do - eval_str = unindent(<<-STR) + eval_str = PryTestHelpers.unindent(<<-STR) def hello puts :bing STR diff --git a/spec/commands/show_source_spec.rb b/spec/commands/show_source_spec.rb index ed831fd2..f0a3a70c 100644 --- a/spec/commands/show_source_spec.rb +++ b/spec/commands/show_source_spec.rb @@ -1,10 +1,15 @@ require 'helper' +require "fixtures/show_source_doc_examples" -if !mri18_and_no_real_source_location? +if !PryTestHelpers.mri18_and_no_real_source_location? describe "show-source" do before do @str_output = StringIO.new @o = Object.new + def @o.sample_method + :sample + end + Object.const_set(:Test, Module.new) end @@ -13,7 +18,7 @@ if !mri18_and_no_real_source_location? end it "should output a method's source" do - pry_eval('show-source sample_method').should =~ /def sample/ + pry_eval(binding, 'show-source @o.sample_method').should =~ /def @o.sample/ end it "should output help" do @@ -21,11 +26,11 @@ if !mri18_and_no_real_source_location? end it "should output a method's source with line numbers" do - pry_eval('show-source -l sample_method').should =~ /\d+: def sample/ + pry_eval(binding, 'show-source -l @o.sample_method').should =~ /\d+: def @o.sample/ end it "should output a method's source with line numbers starting at 1" do - pry_eval('show-source -b sample_method').should =~ /1: def sample/ + pry_eval(binding, 'show-source -b @o.sample_method').should =~ /1: def @o.sample/ end it "should output a method's source if inside method and no name given" do @@ -196,7 +201,7 @@ if !mri18_and_no_real_source_location? describe "on variables that shadow methods" do before do - @t = pry_tester.eval unindent(<<-EOS) + @t = pry_tester.eval PryTestHelpers.unindent(<<-EOS) class ::TestHost def hello hello = proc { ' smile ' } @@ -334,7 +339,7 @@ if !mri18_and_no_real_source_location? if !Pry::Helpers::BaseHelpers.mri_18? before do - pry_eval unindent(<<-EOS) + pry_eval PryTestHelpers.unindent(<<-EOS) class Dog def woof end @@ -362,8 +367,9 @@ if !mri18_and_no_real_source_location? end end - it 'should lookup module name with respect to current context' do - constant_scope(:AlphaClass, :BetaClass) do + it 'should lookup module name with respect to current context' do + + PryTestHelpers.constant_scope(:AlphaClass, :BetaClass) do class BetaClass def alpha end @@ -381,7 +387,7 @@ if !mri18_and_no_real_source_location? end it 'should lookup nested modules' do - constant_scope(:AlphaClass) do + PryTestHelpers.constant_scope(:AlphaClass) do class AlphaClass class BetaClass def beta diff --git a/spec/fixtures/show_source_doc_examples.rb b/spec/fixtures/show_source_doc_examples.rb new file mode 100644 index 00000000..53804baa --- /dev/null +++ b/spec/fixtures/show_source_doc_examples.rb @@ -0,0 +1,15 @@ +# used by test_show_source.rb and test_documentation.rb +class TestClassForShowSource + def alpha + end +end + +class TestClassForShowSourceClassEval + def alpha + end +end + +class TestClassForShowSourceInstanceEval + def alpha + end +end diff --git a/spec/helper.rb b/spec/helper.rb new file mode 100644 index 00000000..5c46345b --- /dev/null +++ b/spec/helper.rb @@ -0,0 +1,55 @@ +unless Object.const_defined? 'Pry' + $:.unshift File.expand_path '../../lib', __FILE__ + require 'pry' +end + +require File.join(File.expand_path(File.dirname(__FILE__)), '../lib/pry/test/helper') + +class Module + public :remove_const + public :remove_method +end + +# turn warnings off (esp for Pry::Hooks which will generate warnings +# in tests) +$VERBOSE = nil + +# Set I/O streams. +# +# Out defaults to an anonymous StringIO. +def redirect_pry_io(new_in, new_out = StringIO.new) + old_in = Pry.input + old_out = Pry.output + + Pry.input = new_in + Pry.output = new_out + begin + yield + ensure + Pry.input = old_in + Pry.output = old_out + end +end + +class InputTester + def initialize(*actions) + @orig_actions = actions.dup + @actions = actions + end + + def readline(*) + @actions.shift + end + + def rewind + @actions = @orig_actions.dup + end +end + +# to help with tracking down bugs that cause an infinite loop in the test suite +if ENV["SET_TRACE_FUNC"] + require 'set_trace' if Pry::Helpers::BaseHelpers.rbx? + set_trace_func proc { |event, file, line, id, binding, classname| + STDERR.printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname + } +end diff --git a/spec/pry_defaults_spec.rb b/spec/pry_defaults_spec.rb index e79706fa..288b9b07 100644 --- a/spec/pry_defaults_spec.rb +++ b/spec/pry_defaults_spec.rb @@ -40,7 +40,7 @@ describe "test Pry defaults" do end end.new - Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput) + Pry.start(self, :input => arity_one_input, :output => StringIO.new) arity_one_input.prompt.should == Pry.prompt.call end @@ -53,7 +53,7 @@ describe "test Pry defaults" do end end.new - lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception + lambda { Pry.start(self, :input => arity_zero_input, :output => StringIO.new) }.should.not.raise Exception end it 'should not pass in the prompt if the arity is -1' do @@ -68,7 +68,7 @@ describe "test Pry defaults" do end end.new - Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput) + Pry.start(self, :input => arity_multi_input, :output => StringIO.new) arity_multi_input.prompt.should == nil end @@ -112,23 +112,23 @@ describe "test Pry defaults" do describe "pry return values" do it 'should return nil' do - Pry.start(self, :input => StringIO.new("exit-all"), :output => Pry::NullOutput).should == nil + Pry.start(self, :input => StringIO.new("exit-all"), :output => StringIO.new).should == nil end it 'should return the parameter given to exit-all' do - Pry.start(self, :input => StringIO.new("exit-all 10"), :output => Pry::NullOutput).should == 10 + Pry.start(self, :input => StringIO.new("exit-all 10"), :output => StringIO.new).should == 10 end it 'should return the parameter (multi word string) given to exit-all' do - Pry.start(self, :input => StringIO.new("exit-all \"john mair\""), :output => Pry::NullOutput).should == "john mair" + Pry.start(self, :input => StringIO.new("exit-all \"john mair\""), :output => StringIO.new).should == "john mair" end it 'should return the parameter (function call) given to exit-all' do - Pry.start(self, :input => StringIO.new("exit-all 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba' + Pry.start(self, :input => StringIO.new("exit-all 'abc'.reverse"), :output => StringIO.new).should == 'cba' end it 'should return the parameter (self) given to exit-all' do - Pry.start("carl", :input => StringIO.new("exit-all self"), :output => Pry::NullOutput).should == "carl" + Pry.start("carl", :input => StringIO.new("exit-all self"), :output => StringIO.new).should == "carl" end end diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index aee20530..47da48a8 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -107,7 +107,7 @@ describe Pry do input = InputTester.new(input_string) o = Object.new - pry_tester = Pry.new(:input => input, :output => Pry::NullOutput) + pry_tester = Pry.new(:input => input, :output => StringIO.new) pry_tester.rep(o) o.instance_variable_get(:@x).should == 10 end @@ -136,7 +136,7 @@ describe Pry do end it 'should define a nested class under Hello and not on top-level or Pry' do - pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => Pry::NullOutput) + pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => StringIO.new) pry_tester.rep(Hello) Hello.const_defined?(:Nested).should == true end @@ -204,7 +204,7 @@ describe Pry do o = Object.new - pry_tester = Pry.start(o, :input => input, :output => Pry::NullOutput) + pry_tester = Pry.start(o, :input => input, :output => StringIO.new) o.instance_variable_get(:@x).should == 10 end @@ -223,7 +223,7 @@ describe Pry do it 'sets _ to the last result' do res = [] input = InputTester.new *[":foo", "self << _", "42", "self << _"] - pry = Pry.new(:input => input, :output => Pry::NullOutput) + pry = Pry.new(:input => input, :output => StringIO.new) pry.repl(res) res.should == [:foo, 42] @@ -232,7 +232,7 @@ describe Pry do it 'sets out to an array with the result' do res = {} input = InputTester.new *[":foo", "42", "self[:res] = _out_"] - pry = Pry.new(:input => input, :output => Pry::NullOutput) + pry = Pry.new(:input => input, :output => StringIO.new) pry.repl(res) res[:res].should.be.kind_of Pry::HistoryArray @@ -242,7 +242,7 @@ describe Pry do it 'sets _in_ to an array with the entered lines' do res = {} input = InputTester.new *[":foo", "42", "self[:res] = _in_"] - pry = Pry.new(:input => input, :output => Pry::NullOutput) + pry = Pry.new(:input => input, :output => StringIO.new) pry.repl(res) res[:res].should.be.kind_of Pry::HistoryArray @@ -252,7 +252,7 @@ describe Pry do it 'uses 100 as the size of _in_ and _out_' do res = [] input = InputTester.new *["self << _out_.max_size << _in_.max_size"] - pry = Pry.new(:input => input, :output => Pry::NullOutput) + pry = Pry.new(:input => input, :output => StringIO.new) pry.repl(res) res.should == [100, 100] @@ -261,7 +261,7 @@ describe Pry do it 'can change the size of the history arrays' do res = [] input = InputTester.new *["self << _out_.max_size << _in_.max_size"] - pry = Pry.new(:input => input, :output => Pry::NullOutput, + pry = Pry.new(:input => input, :output => StringIO.new, :memory_size => 1000) pry.repl(res) @@ -271,7 +271,7 @@ describe Pry do it 'store exceptions' do res = [] input = InputTester.new *["foo!","self << _in_[-1] << _out_[-1]"] - pry = Pry.new(:input => input, :output => Pry::NullOutput, + pry = Pry.new(:input => input, :output => StringIO.new, :memory_size => 1000) pry.repl(res) @@ -322,10 +322,10 @@ describe Pry do it "should never run the rc file twice" do Pry.config.should_load_rc = true - Pry.start(self, :input => StringIO.new("exit-all\n"), :output => Pry::NullOutput) + Pry.start(self, :input => StringIO.new("exit-all\n"), :output => StringIO.new) TEST_RC.should == [0] - Pry.start(self, :input => StringIO.new("exit-all\n"), :output => Pry::NullOutput) + Pry.start(self, :input => StringIO.new("exit-all\n"), :output => StringIO.new) TEST_RC.should == [0] end @@ -334,7 +334,7 @@ describe Pry do old_rc = Pry.config.should_load_rc ENV['HOME'] = nil Pry.config.should_load_rc = true - lambda { Pry.start(self, :input => StringIO.new("exit-all\n"), :output => Pry::NullOutput) }.should.not.raise + lambda { Pry.start(self, :input => StringIO.new("exit-all\n"), :output => StringIO.new) }.should.not.raise ENV['HOME'] = old_home Pry.config.should_load_rc = old_rc @@ -342,13 +342,13 @@ describe Pry do it "should not run the rc file at all if Pry.config.should_load_rc is false" do Pry.config.should_load_rc = false - Pry.start(self, :input => StringIO.new("exit-all\n"), :output => Pry::NullOutput) + Pry.start(self, :input => StringIO.new("exit-all\n"), :output => StringIO.new) Object.const_defined?(:TEST_RC).should == false end it "should not load the rc file if #repl method invoked" do Pry.config.should_load_rc = true - Pry.new(:input => StringIO.new("exit-all\n"), :output => Pry::NullOutput).repl(self) + Pry.new(:input => StringIO.new("exit-all\n"), :output => StringIO.new).repl(self) Object.const_defined?(:TEST_RC).should == false Pry.config.should_load_rc = false end @@ -368,7 +368,7 @@ describe Pry do } @doing_it = lambda{ - Pry.start(self, :input => StringIO.new("Object::TEST_AFTER_RAISE=1\nexit-all\n"), :output => Pry::NullOutput) + Pry.start(self, :input => StringIO.new("Object::TEST_AFTER_RAISE=1\nexit-all\n"), :output => StringIO.new) putsed } end