diff --git a/test/test_command_integration.rb b/test/test_command_integration.rb index 8febd88e..0d773b0d 100644 --- a/test/test_command_integration.rb +++ b/test/test_command_integration.rb @@ -1,5 +1,9 @@ require 'helper' + describe "commands" do + before do + @str_output = StringIO.new + end describe "alias_command" do it 'should make an aliasd command behave like its original' do @@ -112,6 +116,162 @@ describe "commands" do end end + describe "Pry::Command#run" do + it 'should allow running of commands with following whitespace' do + $_scratch = Object.new + o = Object.new + + set = Pry::CommandSet.new do + import Pry::Commands + command "test-run" do + run "cd / " + end + end + redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6/$_scratch", + "@nesting1 = _pry_.binding_stack.size", + "test-run", + "@obj = self", + "@nesting2 = _pry_.binding_stack.size", + "exit-all")) do + Pry.start(o, :commands => set) + end + + $_scratch.instance_variable_get(:@nesting1).should == 8 + o.instance_variable_get(:@obj).should == o + o.instance_variable_get(:@nesting2).should == 1 + $_scratch = nil + end + + it 'should allow running of cd command when contained in a single string' do + $_scratch = Object.new + o = Object.new + + set = Pry::CommandSet.new do + import Pry::Commands + command "test-run" do + run "cd /" + end + end + redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6/$_scratch", + "@nesting1 = _pry_.binding_stack.size", + "test-run", + "@obj = self", + "@nesting2 = _pry_.binding_stack.size", + "exit-all")) do + Pry.start(o, :commands => set) + end + + $_scratch.instance_variable_get(:@nesting1).should == 8 + o.instance_variable_get(:@obj).should == o + o.instance_variable_get(:@nesting2).should == 1 + $_scratch = nil + end + + it 'should allow running of cd command when split into array' do + $_scratch = Object.new + o = Object.new + + set = Pry::CommandSet.new do + import Pry::Commands + command "test-run" do + run "cd", "/" + end + end + redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6/$_scratch", + "@nesting1 = _pry_.binding_stack.size", + "test-run", + "@obj = self", + "@nesting2 = _pry_.binding_stack.size", + "exit-all")) do + Pry.start(o, :commands => set) + end + + $_scratch.instance_variable_get(:@nesting1).should == 8 + o.instance_variable_get(:@obj).should == o + o.instance_variable_get(:@nesting2).should == 1 + $_scratch = nil + end + + it 'should run a command from within a command' do + klass = Pry::CommandSet.new do + command "v" do + output.puts "v command" + end + + command "run_v" do + run "v" + end + end + + Pry.new(:input => InputTester.new("run_v"), :output => @str_output, :commands => klass).rep + + @str_output.string.should =~ /v command/ + end + + it 'should run a regex command from within a command' do + klass = Pry::CommandSet.new do + command /v(.*)?/ do |arg| + output.puts "v #{arg}" + end + + command "run_v" do + run "vbaby" + end + end + + redirect_pry_io(InputTester.new("run_v"), @str_output) do + Pry.new(:commands => klass).rep + end + + @str_output.string.should =~ /v baby/ + end + + it 'should run a command from within a command with arguments' do + klass = Pry::CommandSet.new do + command /v(\w+)/ do |arg1, arg2| + output.puts "v #{arg1} #{arg2}" + end + + command "run_v_explicit_parameter" do + run "vbaby", "param" + end + + command "run_v_embedded_parameter" do + run "vbaby param" + end + end + + ["run_v_explicit_parameter", "run_v_embedded_parameter"].each do |cmd| + redirect_pry_io(InputTester.new(cmd), @str_output) do + Pry.new(:commands => klass).rep + end + @str_output.string.should =~ /v baby param/ + end + end + end + + describe "Pry#run_command" do + it 'should run a command in a specified context' do + b = Pry.binding_for(7) + p = Pry.new(:output => @str_output) + p.run_command("ls -m", "", b) + p.output.string.should =~ /divmod/ + end + + it 'should run a command that modifies the passed in eval_string' do + b = Pry.binding_for(7) + p = Pry.new(:output => @str_output) + eval_string = "def hello\npeter pan\n" + p.run_command("amend-line !", eval_string, b) + eval_string.should =~ /def hello/ + eval_string.should.not =~ /peter pan/ + end + + it 'should run a command in the context of a session' do + mock_pry("@session_ivar = 10", "_pry_.run_command('ls')").should =~ /@session_ivar/ + end + end + it 'should interpolate ruby code into commands' do klass = Pry::CommandSet.new do command "hello", "", :keep_retval => true do |arg| @@ -120,19 +280,18 @@ describe "commands" do end $test_interpolation = "bing" - str_output = StringIO.new - Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep - str_output.string.should =~ /bing/ + Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => @str_output, :commands => klass).rep + @str_output.string.should =~ /bing/ $test_interpolation = nil end # bug fix for https://github.com/pry/pry/issues/170 it 'should not choke on complex string interpolation when checking if ruby code is a command' do - redirect_pry_io(InputTester.new('/#{Regexp.escape(File.expand_path("."))}/'), str_output = StringIO.new) do + redirect_pry_io(InputTester.new('/#{Regexp.escape(File.expand_path("."))}/'), @str_output) do pry end - str_output.string.should.not =~ /SyntaxError/ + @str_output.string.should.not =~ /SyntaxError/ end it 'should NOT interpolate ruby code into commands if :interpolate => false' do @@ -143,24 +302,24 @@ describe "commands" do end $test_interpolation = "bing" - str_output = StringIO.new - Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep - str_output.string.should =~ /test_interpolation/ + Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => @str_output, :commands => klass).rep + @str_output.string.should =~ /test_interpolation/ $test_interpolation = nil end it 'should NOT try to interpolate pure ruby code (no commands) ' do - str_output = StringIO.new - Pry.new(:input => StringIO.new('format \'#{aggy}\''), :output => str_output).rep - str_output.string.should.not =~ /NameError/ + Pry.new(:input => StringIO.new('format \'#{aggy}\''), :output => @str_output).rep + @str_output.string.should.not =~ /NameError/ - Pry.new(:input => StringIO.new('format #{aggy}'), :output => str_output).rep - str_output.string.should.not =~ /NameError/ + @str_output = StringIO.new + Pry.new(:input => StringIO.new('format #{aggy}'), :output => @str_output).rep + @str_output.string.should.not =~ /NameError/ $test_interpolation = "blah" - Pry.new(:input => StringIO.new('format \'#{$test_interpolation}\''), :output => str_output).rep + @str_output = StringIO.new + Pry.new(:input => StringIO.new('format \'#{$test_interpolation}\''), :output => @str_output).rep - str_output.string.should.not =~ /blah/ + @str_output.string.should.not =~ /blah/ $test_interpolation = nil end @@ -171,12 +330,11 @@ describe "commands" do end end - str_output = StringIO.new - redirect_pry_io(InputTester.new("hello baby", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hello baby", "exit-all"), @str_output) do Pry.new(:commands => set).rep end - str_output.string.should =~ /hello baby command/ + @str_output.string.should =~ /hello baby command/ end it 'should create a command with a space in its name and pass an argument' do @@ -186,12 +344,11 @@ describe "commands" do end end - str_output = StringIO.new - redirect_pry_io(InputTester.new("hello baby john"), str_output) do + redirect_pry_io(InputTester.new("hello baby john"), @str_output) do Pry.new(:commands => set).rep end - str_output.string.should =~ /hello baby command john/ + @str_output.string.should =~ /hello baby command john/ end it 'should create a regex command and be able to invoke it' do @@ -202,12 +359,11 @@ describe "commands" do end end - str_output = StringIO.new - redirect_pry_io(InputTester.new("hello1"), str_output) do + redirect_pry_io(InputTester.new("hello1"), @str_output) do Pry.new(:commands => set).rep end - str_output.string.should =~ /hello1/ + @str_output.string.should =~ /hello1/ end it 'should create a regex command and pass captures into the args list before regular arguments' do @@ -217,12 +373,11 @@ describe "commands" do end end - str_output = StringIO.new - redirect_pry_io(InputTester.new("hello1 baby"), str_output) do + redirect_pry_io(InputTester.new("hello1 baby"), @str_output) do Pry.new(:commands => set).rep end - str_output.string.should =~ /hello 1 baby/ + @str_output.string.should =~ /hello 1 baby/ end it 'should create a regex command and interpolate the captures' do @@ -232,13 +387,13 @@ describe "commands" do end end - str_output = StringIO.new $obj = "bing" - redirect_pry_io(InputTester.new('hello #{$obj}'), str_output) do + redirect_pry_io(InputTester.new('hello #{$obj}'), @str_output) do Pry.new(:commands => set).rep end - str_output.string.should =~ /hello bing/ + @str_output.string.should =~ /hello bing/ + $obj = nil end @@ -249,22 +404,18 @@ describe "commands" do end end - str_output = StringIO.new $a1 = "bing" $a2 = "bong" $a3 = "bang" - redirect_pry_io(InputTester.new('hellojohn #{$a1} #{$a2} #{$a3}'), str_output) do + 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/ + @str_output.string.should =~ /hello john bing bong bang/ - $a1 = nil - $a2 = nil - $a3 = nil + $a1 = $a2 = $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| @@ -272,22 +423,20 @@ describe "commands" do end end - str_output = StringIO.new - redirect_pry_io(InputTester.new("hello baby"), str_output) do + redirect_pry_io(InputTester.new("hello baby"), @str_output) do Pry.new(:commands => set).rep end - str_output.string.should =~ /hello nil baby/ + @str_output.string.should =~ /hello nil baby/ end it 'should create a command in a nested context and that command should be accessible from the parent' do - str_output = StringIO.new x = "@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit-all" - redirect_pry_io(StringIO.new("@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit-all"), str_output) do + redirect_pry_io(StringIO.new("@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit-all"), @str_output) do Pry.new.repl(0) end - str_output.string.should =~ /@x/ + @str_output.string.should =~ /@x/ end it 'should define a command that keeps its return value' do @@ -296,10 +445,10 @@ describe "commands" do :kept_hello end end - str_output = StringIO.new - Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep - str_output.string.should =~ /:kept_hello/ - str_output.string.should =~ /=>/ + + Pry.new(:input => StringIO.new("hello\n"), :output => @str_output, :commands => klass).rep + @str_output.string.should =~ /:kept_hello/ + @str_output.string.should =~ /=>/ end it 'should define a command that does NOT keep its return value' do @@ -308,10 +457,10 @@ describe "commands" do :kept_hello end end - str_output = StringIO.new - Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep - (str_output.string =~ /:kept_hello/).should == nil -str_output.string !~ /=>/ + + Pry.new(:input => StringIO.new("hello\n"), :output => @str_output, :commands => klass).rep + (@str_output.string =~ /:kept_hello/).should == nil + @str_output.string !~ /=>/ end it 'should define a command that keeps its return value even when nil' do @@ -320,10 +469,11 @@ str_output.string !~ /=>/ nil end end - str_output = StringIO.new - Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep - str_output.string.should =~ /nil/ - str_output.string.should =~ /=>/ + + Pry.new(:input => StringIO.new("hello\n"), :output => @str_output, :commands => klass).rep + + @str_output.string.should =~ /nil/ + @str_output.string.should =~ /=>/ end it 'should define a command that keeps its return value but does not return when value is void' do @@ -332,9 +482,9 @@ str_output.string !~ /=>/ void end end - str_output = StringIO.new - Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep - str_output.string.empty?.should == true + + Pry.new(:input => StringIO.new("hello\n"), :output => @str_output, :commands => klass).rep + @str_output.string.empty?.should == true end it 'a command (with :keep_retval => false) that replaces eval_string with a valid expression should not have the expression value suppressed' do @@ -343,9 +493,9 @@ str_output.string !~ /=>/ eval_string.replace("6") end end - str_output = StringIO.new - Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep - str_output.string.should =~ /6/ + + Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => @str_output, :commands => klass).rep + @str_output.string.should =~ /6/ end @@ -356,10 +506,11 @@ str_output.string !~ /=>/ 7 end end - str_output = StringIO.new - Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep - str_output.string.should =~ /7/ - str_output.string.should.not =~ /6/ + + Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => @str_output, :commands => klass).rep + + @str_output.string.should =~ /7/ + @str_output.string.should.not =~ /6/ end it 'a command that return a value in a multi-line expression should clear the expression and return the value' do @@ -368,11 +519,11 @@ str_output.string !~ /=>/ 5 end end - str_output = StringIO.new - Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => str_output, :commands => klass).rep - str_output.string.should =~ /5/ - end + Pry.new(:input => StringIO.new("def yo\nhello\n"), :output => @str_output, :commands => klass).rep + + @str_output.string.should =~ /5/ + end it 'should set the commands default, and the default should be overridable' do klass = Pry::CommandSet.new do @@ -383,9 +534,8 @@ str_output.string !~ /=>/ Pry.commands = klass - str_output = StringIO.new - Pry.new(:input => InputTester.new("hello"), :output => str_output).rep - str_output.string.should =~ /hello world/ + Pry.new(:input => InputTester.new("hello"), :output => @str_output).rep + @str_output.string.should =~ /hello world/ other_klass = Pry::CommandSet.new do command "goodbye", "" do @@ -393,10 +543,10 @@ str_output.string !~ /=>/ end end - str_output = StringIO.new + @str_output = StringIO.new - Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => other_klass).rep - str_output.string.should =~ /goodbye world/ + Pry.new(:input => InputTester.new("goodbye"), :output => @str_output, :commands => other_klass).rep + @str_output.string.should =~ /goodbye world/ end it 'should inherit commands from Pry::Commands' do @@ -421,144 +571,6 @@ str_output.string !~ /=>/ klass.commands["help"].description.should == "blah" end - -describe "Pry::Command#run" do -it 'should allow running of commands with following whitespace' do - $_scratch = Object.new - o = Object.new - - set = Pry::CommandSet.new do - import Pry::Commands - command "test-run" do - run "cd / " - end - end - redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6/$_scratch", - "@nesting1 = _pry_.binding_stack.size", - "test-run", - "@obj = self", - "@nesting2 = _pry_.binding_stack.size", - "exit-all")) do - Pry.start(o, :commands => set) - end - - $_scratch.instance_variable_get(:@nesting1).should == 8 - o.instance_variable_get(:@obj).should == o - o.instance_variable_get(:@nesting2).should == 1 - $_scratch = nil -end - -it 'should allow running of cd command when contained in a single string' do - $_scratch = Object.new - o = Object.new - - set = Pry::CommandSet.new do - import Pry::Commands - command "test-run" do - run "cd /" - end - end - redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6/$_scratch", - "@nesting1 = _pry_.binding_stack.size", - "test-run", - "@obj = self", - "@nesting2 = _pry_.binding_stack.size", - "exit-all")) do - Pry.start(o, :commands => set) - end - - $_scratch.instance_variable_get(:@nesting1).should == 8 - o.instance_variable_get(:@obj).should == o - o.instance_variable_get(:@nesting2).should == 1 - $_scratch = nil -end - -it 'should allow running of cd command when split into array' do - $_scratch = Object.new - o = Object.new - - set = Pry::CommandSet.new do - import Pry::Commands - command "test-run" do - run "cd", "/" - end - end - redirect_pry_io(InputTester.new("cd 1/2/3/4/5/6/$_scratch", - "@nesting1 = _pry_.binding_stack.size", - "test-run", - "@obj = self", - "@nesting2 = _pry_.binding_stack.size", - "exit-all")) do - Pry.start(o, :commands => set) - end - - $_scratch.instance_variable_get(:@nesting1).should == 8 - o.instance_variable_get(:@obj).should == o - o.instance_variable_get(:@nesting2).should == 1 - $_scratch = nil -end - - it 'should run a command from within a command' do - klass = Pry::CommandSet.new do - command "v" do - output.puts "v command" - end - - command "run_v" do - run "v" - end - end - - str_output = StringIO.new - Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => klass).rep - str_output.string.should =~ /v command/ - end - - it 'should run a regex command from within a command' do - klass = Pry::CommandSet.new do - command /v(.*)?/ do |arg| - output.puts "v #{arg}" - end - - command "run_v" do - run "vbaby" - end - end - - str_output = StringIO.new - redirect_pry_io(InputTester.new("run_v"), str_output) do - Pry.new(:commands => klass).rep - end - - str_output.string.should =~ /v baby/ - end - - it 'should run a command from within a command with arguments' do - klass = Pry::CommandSet.new do - command /v(\w+)/ do |arg1, arg2| - output.puts "v #{arg1} #{arg2}" - end - - command "run_v_explicit_parameter" do - run "vbaby", "param" - end - - command "run_v_embedded_parameter" do - run "vbaby param" - end - end - - ["run_v_explicit_parameter", "run_v_embedded_parameter"].each do |cmd| - str_output = StringIO.new - redirect_pry_io(InputTester.new(cmd), str_output) do - Pry.new(:commands => klass).rep - end - str_output.string.should =~ /v baby param/ - end - end - -end - it 'should enable an inherited method to access opts and output and target, due to instance_exec' do klass = Pry::CommandSet.new do command "v" do @@ -569,11 +581,10 @@ end child_klass = Pry::CommandSet.new klass do end - str_output = StringIO.new Pry.new(:print => proc {}, :input => InputTester.new("v"), - :output => str_output, :commands => child_klass).rep("john") + :output => @str_output, :commands => child_klass).rep("john") - str_output.string.rstrip.should == "john" + @str_output.string.rstrip.should == "john" end it 'should import commands from another command object' do @@ -617,13 +628,12 @@ end # suppress evaluation output Pry.print = proc {} - str_output = StringIO.new - Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => klass).rep - str_output.string.rstrip.should == "jump-to the music" + Pry.new(:input => InputTester.new("jump-to"), :output => @str_output, :commands => klass).rep + @str_output.string.rstrip.should == "jump-to the music" - str_output = StringIO.new - Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => klass).rep - str_output.string.should == "help to the music\n" + @str_output = StringIO.new + Pry.new(:input => InputTester.new("help"), :output => @str_output, :commands => klass).rep + @str_output.string.should == "help to the music\n" Pry.reset_defaults @@ -636,12 +646,11 @@ end pry_tester.input = InputTester.new("command1", "exit-all") pry_tester.commands = CommandTester - str_output = StringIO.new - pry_tester.output = str_output + pry_tester.output = @str_output pry_tester.rep - str_output.string.should =~ /command1/ + @str_output.string.should =~ /command1/ end it 'should run a command with one parameter' do @@ -650,35 +659,10 @@ end pry_tester.input = InputTester.new("command2 horsey", "exit-all") pry_tester.commands = CommandTester - str_output = StringIO.new - pry_tester.output = str_output + pry_tester.output = @str_output pry_tester.rep - str_output.string.should =~ /horsey/ + @str_output.string.should =~ /horsey/ end end - -describe "Pry#run_command" do - it 'should run a command in a specified context' do - b = Pry.binding_for(7) - p = Pry.new(:output => StringIO.new) - p.run_command("ls -m", "", b) - p.output.string.should =~ /divmod/ - end - - it 'should run a command that modifies the passed in eval_string' do - b = Pry.binding_for(7) - p = Pry.new(:output => StringIO.new) - eval_string = "def hello\npeter pan\n" - p.run_command("amend-line !", eval_string, b) - eval_string.should =~ /def hello/ - eval_string.should.not =~ /peter pan/ - end - - it 'should run a command in the context of a session' do - mock_pry("@session_ivar = 10", "_pry_.run_command('ls')").should =~ /@session_ivar/ - end -end - - diff --git a/test/test_default_commands/test_cd.rb b/test/test_default_commands/test_cd.rb index 10168ac2..f01fcc5d 100644 --- a/test/test_default_commands/test_cd.rb +++ b/test/test_default_commands/test_cd.rb @@ -9,7 +9,7 @@ describe 'Pry::DefaultCommands::Cd' 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 + redirect_pry_io(InputTester.new("cd x", "$obj = self", "exit-all")) do b.pry end @@ -20,7 +20,7 @@ describe 'Pry::DefaultCommands::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 + redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd ..", "$outer = self", "exit-all")) do b.pry end $inner.should == :inner @@ -31,7 +31,7 @@ describe 'Pry::DefaultCommands::Cd' do b = Pry.binding_for(Object.new) input = InputTester.new "cd ..", "$obj = self", "exit-all" - redirect_pry_io(input, StringIO.new) do + redirect_pry_io(input) do b.pry end @@ -42,7 +42,7 @@ describe 'Pry::DefaultCommands::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 + redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd /", "$outer = self", "exit-all")) do b.pry end $inner.should == :inner @@ -54,7 +54,7 @@ describe 'Pry::DefaultCommands::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 + redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd", "$outer = self", "exit-all")) do b.pry end $inner.should == :inner @@ -66,7 +66,7 @@ describe 'Pry::DefaultCommands::Cd' do $obj = Object.new $obj.instance_variable_set(:@x, 66) - redirect_pry_io(InputTester.new("cd $obj/@x", "$result = _pry_.binding_stack.dup", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd $obj/@x", "$result = _pry_.binding_stack.dup", "exit-all")) do Pry.start end $result.size.should == 3 @@ -78,7 +78,7 @@ describe 'Pry::DefaultCommands::Cd' do $obj = Object.new $obj.instance_variable_set(:@x, 66) - redirect_pry_io(InputTester.new("cd $obj/@x/", "$result = _pry_.binding_stack.dup", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd $obj/@x/", "$result = _pry_.binding_stack.dup", "exit-all")) do Pry.start end $result.size.should == 3 @@ -90,7 +90,7 @@ describe 'Pry::DefaultCommands::Cd' do $obj = Object.new $obj.instance_variable_set(:@x, 66) - redirect_pry_io(InputTester.new("cd $obj", "local = :local", "cd @x", "cd ../local", "$result = _pry_.binding_stack.dup", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd $obj", "local = :local", "cd @x", "cd ../local", "$result = _pry_.binding_stack.dup", "exit-all")) do Pry.start end $result.size.should == 3 @@ -102,7 +102,7 @@ describe 'Pry::DefaultCommands::Cd' do $obj = Object.new $obj.instance_variable_set(:@x, 66) - redirect_pry_io(InputTester.new("cd $obj/@x/..", "$result = _pry_.binding_stack.dup", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd $obj/@x/..", "$result = _pry_.binding_stack.dup", "exit-all")) do Pry.start end $result.size.should == 2 @@ -114,7 +114,7 @@ describe 'Pry::DefaultCommands::Cd' do $obj.instance_variable_set(:@x, 66) $obj.instance_variable_set(:@y, 79) - redirect_pry_io(InputTester.new("cd $obj/@x/../@y", "$result = _pry_.binding_stack.dup", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd $obj/@x/../@y", "$result = _pry_.binding_stack.dup", "exit-all")) do Pry.start end $result.size.should == 3 @@ -127,7 +127,7 @@ describe 'Pry::DefaultCommands::Cd' do $obj.instance_variable_set(:@x, 66) TOPLEVEL_BINDING.eval('@z = 20') - redirect_pry_io(InputTester.new("cd $obj/@x/", "cd /@z", "$result = _pry_.binding_stack.dup", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd $obj/@x/", "cd /@z", "$result = _pry_.binding_stack.dup", "exit-all")) do Pry.start end $result.size.should == 2 @@ -135,7 +135,7 @@ describe 'Pry::DefaultCommands::Cd' do end it 'should start a session on TOPLEVEL_BINDING with cd ::' do - redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all")) do 5.pry end $obj.should == TOPLEVEL_BINDING.eval('self') @@ -147,7 +147,7 @@ describe 'Pry::DefaultCommands::Cd' do :mon_ouie end - redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all")) do o.pry end $obj.should == :mon_ouie diff --git a/test/test_default_commands/test_context.rb b/test/test_default_commands/test_context.rb index 239c886e..62cfeae3 100644 --- a/test/test_default_commands/test_context.rb +++ b/test/test_default_commands/test_context.rb @@ -3,27 +3,27 @@ require 'helper' describe "Pry::DefaultCommands::Context" do describe "exit-all" do it 'should break out of the repl loop of Pry instance and return nil' do - redirect_pry_io(InputTester.new("exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("exit-all")) do Pry.new.repl(0).should == nil end end it 'should break out of the repl loop of Pry instance wth a user specified value' do - redirect_pry_io(InputTester.new("exit-all 'message'"), StringIO.new) do + redirect_pry_io(InputTester.new("exit-all 'message'")) do Pry.new.repl(0).should == 'message' end end it 'should break of the repl loop even if multiple bindings still on stack' do ins = nil - redirect_pry_io(InputTester.new("cd 1", "cd 2", "exit-all 'message'"), StringIO.new) do + redirect_pry_io(InputTester.new("cd 1", "cd 2", "exit-all 'message'")) do ins = Pry.new.tap { |v| v.repl(0).should == 'message' } end end it 'binding_stack should be empty after breaking out of the repl loop' do ins = nil - redirect_pry_io(InputTester.new("cd 1", "cd 2", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd 1", "cd 2", "exit-all")) do ins = Pry.new.tap { |v| v.repl(0) } end @@ -121,7 +121,7 @@ describe "Pry::DefaultCommands::Context" do b = Pry.binding_for(:outer) b.eval("x = :inner") - redirect_pry_io(InputTester.new("cd x", "$inner = self;", "exit", "$outer = self", "exit-all"), StringIO.new) do + redirect_pry_io(InputTester.new("cd x", "$inner = self;", "exit", "$outer = self", "exit-all")) do b.pry end $inner.should == :inner @@ -137,16 +137,19 @@ describe "Pry::DefaultCommands::Context" do end it 'should break out the repl loop of Pry instance even after an exception in user-given value' do - redirect_pry_io(InputTester.new("exit = 42", "exit"), StringIO.new) do + redirect_pry_io(InputTester.new("exit = 42", "exit")) do ins = Pry.new.tap { |v| v.repl(0).should == nil } end end end describe "jump-to" do + before do + @str_output = StringIO.new + end + it 'should jump to the proper binding index in the stack' do - outp = StringIO.new - redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 1", "$blah = self", "exit-all"), outp) do + redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 1", "$blah = self", "exit-all")) do Pry.start(0) end @@ -154,33 +157,31 @@ describe "Pry::DefaultCommands::Context" do end it 'should print error when trying to jump to a non-existent binding index' do - outp = StringIO.new - redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 100", "exit-all"), outp) do + redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 100", "exit-all"), @str_output) do Pry.start(0) end - outp.string.should =~ /Invalid nest level/ + @str_output.string.should =~ /Invalid nest level/ end it 'should print error when trying to jump to the same binding index' do - outp = StringIO.new - redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 2", "exit-all"), outp) do + redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 2", "exit-all"), @str_output) do Pry.new.repl(0) end - outp.string.should =~ /Already/ + @str_output.string.should =~ /Already/ end end describe "exit-program" do it 'should raise SystemExit' do - redirect_pry_io(InputTester.new("exit-program"), StringIO.new) do + redirect_pry_io(InputTester.new("exit-program")) do lambda { Pry.new.repl(0).should == 0 }.should.raise SystemExit end end it 'should exit the program with the provided value' do - redirect_pry_io(InputTester.new("exit-program 66"), StringIO.new) do + redirect_pry_io(InputTester.new("exit-program 66")) do begin Pry.new.repl(0) rescue SystemExit => e @@ -192,13 +193,13 @@ describe "Pry::DefaultCommands::Context" do describe "raise-up" do it "should raise the exception with raise-up" do - redirect_pry_io(InputTester.new("raise NoMethodError", "raise-up NoMethodError"),StringIO.new) do + redirect_pry_io(InputTester.new("raise NoMethodError", "raise-up NoMethodError")) do lambda { Pry.new.repl(0) }.should.raise NoMethodError end end it "should raise an unamed exception with raise-up" do - redirect_pry_io(InputTester.new("raise 'stop'","raise-up 'noreally'"),StringIO.new) do + redirect_pry_io(InputTester.new("raise 'stop'","raise-up 'noreally'")) do lambda { Pry.new.repl(0) }.should.raise RuntimeError, "noreally" end end @@ -208,7 +209,7 @@ describe "Pry::DefaultCommands::Context" do b.eval("x = :inner") redirect_pry_io(InputTester.new("x.pry", "raise NoMethodError", - "$inner = self", "raise-up NoMethodError", "$outer = self", "exit-all"),StringIO.new) do + "$inner = self", "raise-up NoMethodError", "$outer = self", "exit-all")) do b.pry end $inner.should == :inner @@ -224,7 +225,7 @@ describe "Pry::DefaultCommands::Context" do b = Pry.binding_for(:outer) b.eval("x = :inner") redirect_pry_io(InputTester.new("cd x", "raise NoMethodError","$inner = self", - "deep = :deep", "cd deep","$deep = self","raise-up NoMethodError", "raise-up", "$outer = self", "raise-up", "exit-all"),StringIO.new) do + "deep = :deep", "cd deep","$deep = self","raise-up NoMethodError", "raise-up", "$outer = self", "raise-up", "exit-all")) do lambda { b.pry }.should.raise NoMethodError end $deep.should == :deep diff --git a/test/test_default_commands/test_documentation.rb b/test/test_default_commands/test_documentation.rb index a54d76dd..4821b949 100644 --- a/test/test_default_commands/test_documentation.rb +++ b/test/test_default_commands/test_documentation.rb @@ -3,28 +3,32 @@ require 'helper' if !mri18_and_no_real_source_location? describe "Pry::DefaultCommands::Documentation" do describe "show-doc" do + before do + @str_output = StringIO.new + end + it 'should output a method\'s documentation' do - redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output=StringIO.new) do + redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), @str_output) do pry end - str_output.string.should =~ /sample doc/ + @str_output.string.should =~ /sample doc/ end it 'should output a method\'s documentation with line numbers' do - redirect_pry_io(InputTester.new("show-doc sample_method -l", "exit-all"), str_output=StringIO.new) do + redirect_pry_io(InputTester.new("show-doc sample_method -l", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\d: sample doc/ + @str_output.string.should =~ /\d: sample doc/ end it 'should output a method\'s documentation with line numbers (base one)' do - redirect_pry_io(InputTester.new("show-doc sample_method -b", "exit-all"), str_output=StringIO.new) do + redirect_pry_io(InputTester.new("show-doc sample_method -b", "exit-all"), @str_output) do pry end - str_output.string.should =~ /1: sample doc/ + @str_output.string.should =~ /1: sample doc/ end it 'should output a method\'s documentation if inside method without needing to use method name' do @@ -206,11 +210,11 @@ if !mri18_and_no_real_source_location? end end - redirect_pry_io(InputTester.new("show-doc BetaClass", "exit-all"), out=StringIO.new) do + redirect_pry_io(InputTester.new("show-doc BetaClass", "exit-all"), outp=StringIO.new) do AlphaClass.pry end - out.string.should =~ /nested beta/ + outp.string.should =~ /nested beta/ end end diff --git a/test/test_default_commands/test_find_method.rb b/test/test_default_commands/test_find_method.rb index 5ca40bd5..7208b0c4 100644 --- a/test/test_default_commands/test_find_method.rb +++ b/test/test_default_commands/test_find_method.rb @@ -20,7 +20,7 @@ unless Pry::Helpers::BaseHelpers.mri_18? it "should find a method by regex" do mock_pry("find-method hell MyKlass").should =~ /MyKlass.*?hello/m end - + it "should NOT match a method that does not match the regex" do mock_pry("find-method hell MyKlass").should.not =~ /MyKlass.*?goodbye/m end diff --git a/test/test_default_commands/test_input.rb b/test/test_default_commands/test_input.rb index 7832ef24..9ec73f40 100644 --- a/test/test_default_commands/test_input.rb +++ b/test/test_default_commands/test_input.rb @@ -1,149 +1,148 @@ require 'helper' describe "Pry::DefaultCommands::Input" do + before do + @str_output = StringIO.new + end describe "amend-line" 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 + 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/ + + @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 1 def goodbye", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 1 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/ + + @str_output.string.should =~ /\A\d+: def goodbye\n\d+: puts :bing\n\d+: puts :bang/ end it 'should correctly amend the specified line of input when line number given, 0 should behave as 1 ' 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 + 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/ + + @str_output.string.should =~ /\A\d+: def goodbye\n\d+: puts :bing\n\d+: puts :bang/ end it 'should correctly amend the specified line of input when line number given (negative number)' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line -1 puts :bink", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line -1 puts :bink", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing\n\d+: puts :bink/ - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line -2 puts :bink", "show-input", "exit-all"), str_output) do + @str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing\n\d+: puts :bink/ + + @str_output = StringIO.new + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line -2 puts :bink", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bink\n\d+: puts :bang/ + + @str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bink\n\d+: puts :bang/ end it 'should correctly amend the specified range of lines of input when range of negative numbers given (negative number)' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boat", "amend-line -3..-2 puts :bink", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boat", "amend-line -3..-2 puts :bink", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bink\n\d+: puts :boat/ + + @str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bink\n\d+: puts :boat/ end it 'should correctly amend the specified line with string interpolated text' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", 'amend-line puts "#{goodbye}"', "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", 'amend-line puts "#{goodbye}"', "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing\n\d+: puts \"\#\{goodbye\}\"/ + @str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing\n\d+: puts \"\#\{goodbye\}\"/ end it 'should display error if nothing to amend' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("amend-line", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("amend-line", "exit-all"), @str_output) do pry end - str_output.string.should =~ /No input to amend/ + + @str_output.string.should =~ /No input to amend/ end it 'should correctly amend the specified range of lines' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :heart", "amend-line 2..3 puts :bong", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :heart", "amend-line 2..3 puts :bong", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bong\n\d+: puts :heart/ + + @str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bong\n\d+: puts :heart/ end it 'should correctly delete a specific line using the ! for content' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 3 !", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 3 !", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\d+: def hello\n\d+: puts :bing\n\d+: puts :boast\n\d+: puts :heart/ + @str_output.string.should =~ /\d+: def hello\n\d+: puts :bing\n\d+: puts :boast\n\d+: puts :heart/ end it 'should correctly delete a range of lines using the ! for content' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 2..4 !", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 2..4 !", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\d+: def hello\n\d+: puts :heart\n\Z/ + @str_output.string.should =~ /\d+: def hello\n\d+: puts :heart\n\Z/ end it 'should correctly delete the previous line using the ! for content' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line !", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line !", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\d+: def hello\n\d+: puts :bing\n\d+: puts :bang\n\d+: puts :boast\n\Z/ + @str_output.string.should =~ /\d+: def hello\n\d+: puts :bing\n\d+: puts :bang\n\d+: puts :boast\n\Z/ end it 'should correctly amend the specified range of lines, using negative numbers in range' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 2..-2 puts :bong", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "puts :boast", "puts :heart", "amend-line 2..-2 puts :bong", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\d+: def hello\n\d+: puts :bong\n\d+: puts :heart/ + @str_output.string.should =~ /\d+: def hello\n\d+: puts :bong\n\d+: puts :heart/ end it 'should correctly insert a new line of input before a specified line using the > syntax' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 2 >puts :inserted", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 2 >puts :inserted", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\d+: def hello\n\d+: puts :inserted\n\d+: puts :bing\n\d+: puts :bang/ + + @str_output.string.should =~ /\d+: def hello\n\d+: puts :inserted\n\d+: puts :bing\n\d+: puts :bang/ end it 'should correctly insert a new line of input before a specified line using the > syntax (should ignore second value of range)' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 2..21 >puts :inserted", "show-input", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 2..21 >puts :inserted", "show-input", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\d+: def hello\n\d+: puts :inserted\n\d+: puts :bing\n\d+: puts :bang/ + + @str_output.string.should =~ /\d+: def hello\n\d+: puts :inserted\n\d+: puts :bing\n\d+: puts :bang/ 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 + 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/ + + @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 + 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 = @str_output.string.strip! stripped_output.each_line.count.should == 1 stripped_output.should =~ /Input buffer cleared!/ end @@ -153,21 +152,23 @@ describe "Pry::DefaultCommands::Input" do it 'should play a string variable (with no args)' do b = binding b.eval('x = "\"hello\""') - redirect_pry_io(InputTester.new("play x", "exit-all"), str_output = StringIO.new) do + redirect_pry_io(InputTester.new("play x", "exit-all"), @str_output) do Pry.start b, :hooks => Pry::Hooks.new end - str_output.string.should =~ /hello/ + + @str_output.string.should =~ /hello/ end it 'should play a string variable (with no args) using --lines to select what to play' do b = binding b.eval('x = "\"hello\"\n\"goodbye\"\n\"love\""') - redirect_pry_io(InputTester.new("play x --lines 1", "exit-all"), str_output = StringIO.new) do + redirect_pry_io(InputTester.new("play x --lines 1", "exit-all"), @str_output) do Pry.start b, :hooks => Pry::Hooks.new end - str_output.string.should =~ /hello/ - str_output.string.should.not =~ /love/ - str_output.string.should.not =~ /goodbye/ + + @str_output.string.should =~ /hello/ + @str_output.string.should.not =~ /love/ + @str_output.string.should.not =~ /goodbye/ end it 'should play documentation with the -d switch' do @@ -179,7 +180,7 @@ describe "Pry::DefaultCommands::Input" do :test_method_content end - redirect_pry_io(InputTester.new('play -d test_method', "exit-all"), str_output = StringIO.new) do + redirect_pry_io(InputTester.new('play -d test_method', "exit-all")) do o.pry end @@ -198,7 +199,7 @@ describe "Pry::DefaultCommands::Input" do :test_method_content end - redirect_pry_io(InputTester.new('play -d test_method --lines 2..3', "exit-all"), str_output = StringIO.new) do + redirect_pry_io(InputTester.new('play -d test_method --lines 2..3', "exit-all")) do o.pry end @@ -215,11 +216,11 @@ describe "Pry::DefaultCommands::Input" do :test_method_content end - redirect_pry_io(InputTester.new('play -m test_method --lines 2', "exit-all"), str_output = StringIO.new) do + redirect_pry_io(InputTester.new('play -m test_method --lines 2', "exit-all"), @str_output) do o.pry end - str_output.string.should =~ /:test_method_content/ + @str_output.string.should =~ /:test_method_content/ end it 'should APPEND to the input buffer when playing a line with play -m, not replace it' do @@ -228,11 +229,12 @@ describe "Pry::DefaultCommands::Input" do :test_method_content end - redirect_pry_io(InputTester.new('def another_test_method', 'play -m test_method --lines 2', 'show-input', 'exit-all'), str_output = StringIO.new) do + redirect_pry_io(InputTester.new('def another_test_method', 'play -m test_method --lines 2', 'show-input', 'exit-all'), @str_output) do o.pry end - str_output.string.should =~ /def another_test_method/ - str_output.string.should =~ /:test_method_content/ + + @str_output.string.should =~ /def another_test_method/ + @str_output.string.should =~ /:test_method_content/ end @@ -246,7 +248,7 @@ describe "Pry::DefaultCommands::Input" do @var3 = 40 end - redirect_pry_io(InputTester.new('play -m test_method --lines 3..4', "exit-all"), str_output = StringIO.new) do + redirect_pry_io(InputTester.new('play -m test_method --lines 3..4', "exit-all"), @str_output) do o.pry end @@ -254,8 +256,8 @@ describe "Pry::DefaultCommands::Input" do o.instance_variable_get(:@var1).should == 20 o.instance_variable_get(:@var2).should == 30 o.instance_variable_get(:@var3).should == nil - str_output.string.should =~ /30/ - str_output.string.should.not =~ /20/ + @str_output.string.should =~ /30/ + @str_output.string.should.not =~ /20/ end end @@ -268,11 +270,11 @@ describe "Pry::DefaultCommands::Input" do it 'should display the correct history' do @hist.push "hello" @hist.push "world" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hist", "exit-all"), @str_output) do pry end - str_output.string.should =~ /hello\n.*world/ + + @str_output.string.should =~ /hello\n.*world/ end it 'should replay history correctly (single item)' do @@ -280,8 +282,7 @@ describe "Pry::DefaultCommands::Input" do @hist.push "@x = 10" @hist.push "@y = 20" @hist.push "@z = 30" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --replay -1", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hist --replay -1", "exit-all")) do o.pry end o.instance_variable_get(:@x).should == nil @@ -293,8 +294,7 @@ describe "Pry::DefaultCommands::Input" do o = Object.new @hist.push "@x = 10" @hist.push "@y = 20" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all")) do o.pry end o.instance_variable_get(:@x).should == 10 @@ -312,25 +312,24 @@ describe "Pry::DefaultCommands::Input" do @hist.push "def boink 2" @hist.push "place holder" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --grep o", "exit-all"), str_output) do + 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/ + @str_output.string.should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/ # test more than one word in a regex match (def blah) - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --grep def blah", "exit-all"), str_output) do + @str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --grep def blah", "exit-all"), @str_output) do pry end - str_output.string.should =~ /def blah 1/ + @str_output.string.should =~ /def blah 1/ + @str_output = StringIO.new # test more than one word with leading white space in a regex match (def boink) - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --grep def boink", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hist --grep def boink", "exit-all"), @str_output) do pry end - str_output.string.should =~ /def boink 2/ + @str_output.string.should =~ /def boink 2/ end it 'should return last N lines in history with --tail switch' do @@ -338,13 +337,12 @@ describe "Pry::DefaultCommands::Input" do @hist.push v end - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --tail 3", "exit-all"), str_output) do + 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/ + @str_output.string.each_line.count.should == 3 + @str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/ end it 'should apply --tail after --grep' do @@ -355,12 +353,12 @@ describe "Pry::DefaultCommands::Input" do @hist.push "puts 5" str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --tail 2 --grep print", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hist --tail 2 --grep print", "exit-all"), @str_output) do pry end - str_output.string.each_line.count.should == 2 - str_output.string.should =~ /\d:.*?print 2\n\d:.*?print 4/ + @str_output.string.each_line.count.should == 2 + @str_output.string.should =~ /\d:.*?print 2\n\d:.*?print 4/ end it 'should apply --head after --grep' do @@ -370,13 +368,12 @@ describe "Pry::DefaultCommands::Input" do @hist.push "print 4" @hist.push "print 5" - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --head 2 --grep print", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hist --head 2 --grep print", "exit-all"), @str_output) do pry end - str_output.string.each_line.count.should == 2 - str_output.string.should =~ /\d:.*?print 2\n\d:.*?print 4/ + @str_output.string.each_line.count.should == 2 + @str_output.string.should =~ /\d:.*?print 2\n\d:.*?print 4/ end # strangeness in this test is due to bug in Readline::HISTORY not @@ -386,13 +383,12 @@ describe "Pry::DefaultCommands::Input" do @hist.push v end - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --head 4", "exit-all"), str_output) do + 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/ + @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 @@ -402,36 +398,31 @@ describe "Pry::DefaultCommands::Input" do @hist.push v end - str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --show 1..4", "exit-all"), str_output) do + 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/ + @str_output.string.each_line.count.should == 4 + @str_output.string.should =~ /b\n\d+:.*c\n\d+:.*d/ end it "should not contain duplicated lines" do - str_output = StringIO.new - redirect_pry_io(InputTester.new("3", "_ += 1", "_ += 1", "hist", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("3", "_ += 1", "_ += 1", "hist", "exit-all"), @str_output) do pry end - str_output.string.each_line.grep(/_ \+= 1/).count.should == 1 + @str_output.string.each_line.grep(/_ \+= 1/).count.should == 1 end it "should not contain duplicated lines" do - str_output = StringIO.new - redirect_pry_io(InputTester.new(":place_holder", "2 + 2", "", "", "3 + 3", "hist", "exit-all"), str_output) do + redirect_pry_io(InputTester.new(":place_holder", "2 + 2", "", "", "3 + 3", "hist", "exit-all"), @str_output) do pry end - a = str_output.string.each_line.to_a.index{|line| line.include?("2 + 2") } - b = str_output.string.each_line.to_a.index{|line| line.include?("3 + 3") } + a = @str_output.string.each_line.to_a.index{|line| line.include?("2 + 2") } + b = @str_output.string.each_line.to_a.index{|line| line.include?("3 + 3") } (a + 1).should == b end end - - end diff --git a/test/test_default_commands/test_introspection.rb b/test/test_default_commands/test_introspection.rb index 328c9bd9..c5ddd593 100644 --- a/test/test_default_commands/test_introspection.rb +++ b/test/test_default_commands/test_introspection.rb @@ -1,7 +1,6 @@ require 'helper' describe "Pry::DefaultCommands::Introspection" do - describe "edit" do before do @old_editor = Pry.config.editor @@ -11,6 +10,7 @@ describe "Pry::DefaultCommands::Introspection" do nil end end + after do Pry.config.editor = @old_editor end @@ -458,6 +458,10 @@ describe "Pry::DefaultCommands::Introspection" do # show-command only works in implementations that support Proc#source_location if Proc.method_defined?(:source_location) describe "show-command" do + before do + @str_output = StringIO.new + end + it 'should show source for an ordinary command' do set = Pry::CommandSet.new do import_from Pry::Commands, "show-command" @@ -465,11 +469,12 @@ describe "Pry::DefaultCommands::Introspection" do :body_of_foo end end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command foo"), str_output) do + + redirect_pry_io(InputTester.new("show-command foo"), @str_output) do Pry.new(:commands => set).rep end - str_output.string.should =~ /:body_of_foo/ + + @str_output.string.should =~ /:body_of_foo/ end it 'should show source for a command with spaces in its name' do @@ -479,11 +484,12 @@ describe "Pry::DefaultCommands::Introspection" do :body_of_foo_bar end end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command \"foo bar\""), str_output) do + + 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/ + + @str_output.string.should =~ /:body_of_foo_bar/ end it 'should show source for a command by listing name' do @@ -493,14 +499,13 @@ describe "Pry::DefaultCommands::Introspection" do :body_of_foo_regex end end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-command bar"), str_output) do + + 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/ + + @str_output.string.should =~ /:body_of_foo_regex/ end end end - - end diff --git a/test/test_default_commands/test_ls.rb b/test/test_default_commands/test_ls.rb index b0ab46cc..cbd75a38 100644 --- a/test/test_default_commands/test_ls.rb +++ b/test/test_default_commands/test_ls.rb @@ -1,4 +1,5 @@ require 'helper' + describe "ls" do describe "below ceiling" do it "should stop before Object by default" do @@ -92,6 +93,7 @@ describe "ls" do mock_pry("ls -c").should =~ /ArgumentError/ mock_pry("ls -c --grep Run").should.not =~ /ArgumentError/ end + it "should still output matching things" do mock_pry("ls -c --grep Run").should =~ /RuntimeError/ end diff --git a/test/test_default_commands/test_shell.rb b/test/test_default_commands/test_shell.rb index 0b07ab97..4f3801f5 100644 --- a/test/test_default_commands/test_shell.rb +++ b/test/test_default_commands/test_shell.rb @@ -192,6 +192,9 @@ describe "Pry::DefaultCommands::Shell" do end describe "cat" do + before do + @str_output = StringIO.new + end describe "on receiving a file that does not exist" do it 'should display an error message' do @@ -250,7 +253,7 @@ describe "Pry::DefaultCommands::Shell" do describe "with --ex N" do it 'should cat first level of backtrace when --ex used with no argument ' do - pry_instance = Pry.new(:input => StringIO.new("cat --ex"), :output => str_output = StringIO.new) + pry_instance = Pry.new(:input => StringIO.new("cat --ex"), :output => @str_output) temp_file do |f| f << "bt number 1" @@ -259,11 +262,11 @@ describe "Pry::DefaultCommands::Shell" do pry_instance.rep(self) end - str_output.string.should =~ /bt number 1/ + @str_output.string.should =~ /bt number 1/ end it 'should cat first level of backtrace when --ex 0 used ' do - pry_instance = Pry.new(:input => StringIO.new("cat --ex 0"), :output => str_output = StringIO.new) + pry_instance = Pry.new(:input => StringIO.new("cat --ex 0"), :output => @str_output) temp_file do |f| f << "bt number 1" @@ -272,11 +275,11 @@ describe "Pry::DefaultCommands::Shell" do pry_instance.rep(self) end - str_output.string.should =~ /bt number 1/ + @str_output.string.should =~ /bt number 1/ end it 'should cat second level of backtrace when --ex 1 used ' do - pry_instance = Pry.new(:input => StringIO.new("cat --ex 1"), :output => str_output = StringIO.new) + pry_instance = Pry.new(:input => StringIO.new("cat --ex 1"), :output => @str_output) temp_file do |f| f << "bt number 2" @@ -285,11 +288,11 @@ describe "Pry::DefaultCommands::Shell" do pry_instance.rep(self) end - str_output.string.should =~ /bt number 2/ + @str_output.string.should =~ /bt number 2/ end it 'should cat third level of backtrace when --ex 2 used' do - pry_instance = Pry.new(:input => StringIO.new("cat --ex 2"), :output => str_output = StringIO.new) + pry_instance = Pry.new(:input => StringIO.new("cat --ex 2"), :output => @str_output) temp_file do |f| f << "bt number 3" @@ -298,14 +301,14 @@ describe "Pry::DefaultCommands::Shell" do pry_instance.rep(self) end - str_output.string.should =~ /bt number 3/ + @str_output.string.should =~ /bt number 3/ end it 'should show error when backtrace level out of bounds' do - pry_instance = Pry.new(:input => StringIO.new("cat --ex 3"), :output => str_output = StringIO.new) + pry_instance = Pry.new(:input => StringIO.new("cat --ex 3"), :output => @str_output) pry_instance.last_exception = mock_exception("x", "x", "x") pry_instance.rep(self) - str_output.string.should =~ /out of bounds/ + @str_output.string.should =~ /out of bounds/ end it 'each successive cat --ex should show the next level of backtrace, and going past the final level should return to the first' do @@ -317,18 +320,18 @@ describe "Pry::DefaultCommands::Shell" do end pry_instance = Pry.new(:input => StringIO.new("cat --ex\n" * 4), - :output => (str_output = StringIO.new)) + :output => @str_output) pry_instance.last_exception = mock_exception(*temp_files.map { |f| "#{f.path}:1" }) 3.times do |i| pry_instance.rep(self) - str_output.string.should =~ /bt number #{i}/ + @str_output.string.should =~ /bt number #{i}/ end - str_output.reopen + @str_output.reopen pry_instance.rep(self) - str_output.string.should =~ /bt number 0/ + @str_output.string.should =~ /bt number 0/ temp_files.each do |file| file.close(true) diff --git a/test/test_default_commands/test_show_source.rb b/test/test_default_commands/test_show_source.rb index 50eda74f..a3c93522 100644 --- a/test/test_default_commands/test_show_source.rb +++ b/test/test_default_commands/test_show_source.rb @@ -2,13 +2,16 @@ require 'helper' if !mri18_and_no_real_source_location? describe "show-source" do + before do + @str_output = StringIO.new + end + it 'should output a method\'s source' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-source sample_method", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("show-source sample_method", "exit-all"), @str_output) do pry end - str_output.string.should =~ /def sample/ + @str_output.string.should =~ /def sample/ end it 'should output help' do @@ -16,21 +19,19 @@ if !mri18_and_no_real_source_location? end it 'should output a method\'s source with line numbers' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-source -l sample_method", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("show-source -l sample_method", "exit-all"), @str_output) do pry end - str_output.string.should =~ /\d+: def sample/ + @str_output.string.should =~ /\d+: def sample/ end it 'should output a method\'s source with line numbers starting at 1' do - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-source -b sample_method", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("show-source -b sample_method", "exit-all"), @str_output) do pry end - str_output.string.should =~ /1: def sample/ + @str_output.string.should =~ /1: def sample/ end it 'should output a method\'s source if inside method without needing to use method name' do @@ -70,11 +71,11 @@ if !mri18_and_no_real_source_location? self; end - str_output = StringIO.new - redirect_pry_io(InputTester.new("show-source o.foo('bar', 'baz bam').foo", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("show-source o.foo('bar', 'baz bam').foo", "exit-all"), @str_output) do binding.pry end - str_output.string.should =~ /Mr flibble/ + + @str_output.string.should =~ /Mr flibble/ end it "should find methods even if the object has an overridden method method" do @@ -155,44 +156,40 @@ if !mri18_and_no_real_source_location? # 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-source dyna_method"), str_output) do + redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-source dyna_method"), @str_output) do TOPLEVEL_BINDING.pry end - str_output.string.should =~ /def dyna_method/ + @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-source dyna_method"), str_output) do + redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-source dyna_method"), @str_output) do TOPLEVEL_BINDING.pry end - str_output.string.should =~ /def dyna_method/ + @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 Object.remove_const :A if defined?(A) - redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-source A#yo"), str_output) do + redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-source A#yo"), @str_output) do TOPLEVEL_BINDING.pry end - str_output.string.should =~ /def yo/ + @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 Object.remove_const :A if defined?(A) - redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "show-source A#yup"), str_output) do + redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "show-source A#yup"), @str_output) do TOPLEVEL_BINDING.pry end - str_output.string.should =~ /define_method\(:yup\)/ + @str_output.string.should =~ /define_method\(:yup\)/ Object.remove_const :A end end diff --git a/test/test_exception_whitelist.rb b/test/test_exception_whitelist.rb index c3c0da62..f3d8329c 100644 --- a/test/test_exception_whitelist.rb +++ b/test/test_exception_whitelist.rb @@ -1,15 +1,19 @@ require 'helper' describe "Pry.config.exception_whitelist" do + before do + @str_output = StringIO.new + end + it 'should rescue all exceptions NOT specified on whitelist' do Pry.config.exception_whitelist.include?(NameError).should == false - lambda { Pry.start(self, :input => StringIO.new("raise NameError\nexit"), :output => StringIO.new) }.should.not.raise NameError + lambda { Pry.start(self, :input => StringIO.new("raise NameError\nexit"), :output => @str_output) }.should.not.raise NameError end it 'should NOT rescue exceptions specified on whitelist' do old_whitelist = Pry.config.exception_whitelist Pry.config.exception_whitelist = [NameError] - lambda { Pry.start(self, :input => StringIO.new("raise NameError"), :output => StringIO.new) }.should.raise NameError + lambda { Pry.start(self, :input => StringIO.new("raise NameError"), :output => @str_output) }.should.raise NameError Pry.config.exception_whitelist = old_whitelist end end diff --git a/test/test_input_stack.rb b/test/test_input_stack.rb index 33997d04..03010096 100644 --- a/test/test_input_stack.rb +++ b/test/test_input_stack.rb @@ -2,6 +2,10 @@ require 'helper' describe "Pry#input_stack" do + before do + @str_output = StringIO.new + end + it 'should accept :input_stack as a config option' do stack = [StringIO.new("test")] Pry.new(:input_stack => stack).input_stack.should == stack @@ -16,30 +20,30 @@ describe "Pry#input_stack" do it 'should read from all input objects on stack and exit session (usingn repl)' do stack = [b = StringIO.new(":cloister\nexit\n"), c = StringIO.new(":baron\n")] instance = Pry.new(:input => StringIO.new(":alex\n"), - :output => str_output = StringIO.new, + :output => @str_output, :input_stack => stack) instance.repl - str_output.string.should =~ /:alex/ - str_output.string.should =~ /:baron/ - str_output.string.should =~ /:cloister/ + @str_output.string.should =~ /:alex/ + @str_output.string.should =~ /:baron/ + @str_output.string.should =~ /:cloister/ end it 'input objects should be popped off stack as they are used up' do stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")] instance = Pry.new(:input => StringIO.new(":alex\n"), - :output => str_output = StringIO.new, + :output => @str_output, :input_stack => stack) stack.size.should == 2 instance.rep - str_output.string.should =~ /:alex/ + @str_output.string.should =~ /:alex/ instance.rep - str_output.string.should =~ /:baron/ + @str_output.string.should =~ /:baron/ stack.size.should == 1 instance.rep - str_output.string.should =~ /:cloister/ + @str_output.string.should =~ /:cloister/ stack.size.should == 0 end @@ -47,25 +51,24 @@ describe "Pry#input_stack" do redirect_pry_io(StringIO.new(":rimbaud\nexit\n"), StringIO.new) do stack = [StringIO.new(":cloister\n"), StringIO.new(":baron\n")] instance = Pry.new(:input => StringIO.new(":alex\n"), - :output => str_output = StringIO.new, + :output => @str_output, :input_stack => stack) instance.repl - str_output.string.should =~ /:alex/ - str_output.string.should =~ /:baron/ - str_output.string.should =~ /:cloister/ - str_output.string.should =~ /:rimbaud/ + @str_output.string.should =~ /:alex/ + @str_output.string.should =~ /:baron/ + @str_output.string.should =~ /:cloister/ + @str_output.string.should =~ /:rimbaud/ end end it 'should display error and throw(:breakout) if at end of input after using up input_stack objects' do - str_output = StringIO.new catch(:breakout) do - redirect_pry_io(StringIO.new(":rimbaud\n"), str_output) do + redirect_pry_io(StringIO.new(":rimbaud\n"), @str_output) do Pry.new(:input_stack => [StringIO.new(":a\n"), StringIO.new(":b\n")]).repl end end - str_output.string.should =~ /Error: Pry ran out of things to read/ + @str_output.string.should =~ /Error: Pry ran out of things to read/ end if "".respond_to?(:encoding) diff --git a/test/test_pry.rb b/test/test_pry.rb index d7b68de9..1550ca4e 100644 --- a/test/test_pry.rb +++ b/test/test_pry.rb @@ -1,6 +1,9 @@ require 'helper' describe Pry do + before do + @str_output = StringIO.new + end if RUBY_VERSION =~ /1.9/ describe "Exotic object support" do @@ -44,10 +47,9 @@ describe Pry do # bug fix for https://github.com/banister/pry/issues/93 it 'should not leak pry constants into Object namespace' do input_string = "Command" - str_output = StringIO.new o = Object.new pry_tester = Pry.new(:input => StringIO.new(input_string), - :output => str_output, + :output => @str_output, :exception_handler => proc { |_, exception, _pry_| @excep = exception }, :print => proc {} ).rep(o) @@ -77,31 +79,28 @@ describe Pry do end it 'should display error and throw(:breakout) if Pry instance runs out of input' do - str_output = StringIO.new catch(:breakout) do - redirect_pry_io(StringIO.new(":nothing\n"), str_output) do + redirect_pry_io(StringIO.new(":nothing\n"), @str_output) do Pry.new.repl end end - str_output.string.should =~ /Error: Pry ran out of things to read/ + @str_output.string.should =~ /Error: Pry ran out of things to read/ end it 'should make self evaluate to the receiver of the rep session' do o = :john - str_output = StringIO.new - pry_tester = Pry.new(:input => InputTester.new("self"), :output => str_output) + pry_tester = Pry.new(:input => InputTester.new("self"), :output => @str_output) pry_tester.rep(o) - str_output.string.should =~ /:john/ + @str_output.string.should =~ /:john/ end it 'should work with multi-line input' do o = Object.new - str_output = StringIO.new - pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => str_output) + pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => @str_output) pry_tester.rep(o) - str_output.string.should =~ /5/ + @str_output.string.should =~ /5/ end it 'should define a nested class under Hello and not on top-level or Pry' do @@ -112,38 +111,34 @@ describe Pry do it 'should suppress output if input ends in a ";" and is an Exception object (single line)' do o = Object.new - str_output = StringIO.new - pry_tester = Pry.new(:input => InputTester.new("Exception.new;"), :output => str_output) + pry_tester = Pry.new(:input => InputTester.new("Exception.new;"), :output => @str_output) pry_tester.rep(o) - str_output.string.should == "" + @str_output.string.should == "" end it 'should suppress output if input ends in a ";" (single line)' do o = Object.new - str_output = StringIO.new - pry_tester = Pry.new(:input => InputTester.new("x = 5;"), :output => str_output) + pry_tester = Pry.new(:input => InputTester.new("x = 5;"), :output => @str_output) pry_tester.rep(o) - str_output.string.should == "" + @str_output.string.should == "" end it 'should suppress output if input ends in a ";" (multi-line)' do o = Object.new - str_output = StringIO.new - pry_tester = Pry.new(:input => InputTester.new("def self.blah", ":test", "end;"), :output => str_output) + pry_tester = Pry.new(:input => InputTester.new("def self.blah", ":test", "end;"), :output => @str_output) pry_tester.rep(o) - str_output.string.should == "" + @str_output.string.should == "" end it 'should be able to evaluate exceptions normally' do o = Exception.new - str_output = StringIO.new was_called = false pry_tester = Pry.new(:input => InputTester.new("self"), - :output => str_output, + :output => @str_output, :exception_handler => proc { was_called = true }) pry_tester.rep(o) @@ -152,11 +147,10 @@ describe Pry do it 'should notice when exceptions are raised' do o = Exception.new - str_output = StringIO.new was_called = false pry_tester = Pry.new(:input => InputTester.new("raise self"), - :output => str_output, + :output => @str_output, :exception_handler => proc { was_called = true }) pry_tester.rep(o) @@ -370,13 +364,12 @@ describe Pry do it 'should nest properly' do Pry.input = InputTester.new("cd 1", "cd 2", "cd 3", "\"nest:\#\{(_pry_.binding_stack.size - 1)\}\"", "exit-all") - str_output = StringIO.new - Pry.output = str_output + Pry.output = @str_output o = Object.new pry_tester = o.pry - str_output.string.should =~ /nest:3/ + @str_output.string.should =~ /nest:3/ end end diff --git a/test/test_pry_defaults.rb b/test/test_pry_defaults.rb index 23e6f003..6ae59eb2 100644 --- a/test/test_pry_defaults.rb +++ b/test/test_pry_defaults.rb @@ -1,6 +1,11 @@ require 'helper' + version = 1 + describe "test Pry defaults" do + before do + @str_output = StringIO.new + end after do Pry.reset_defaults @@ -8,7 +13,6 @@ describe "test Pry defaults" do end describe "input" do - after do Pry.reset_defaults Pry.color = false @@ -17,13 +21,12 @@ describe "test Pry defaults" do it 'should set the input default, and the default should be overridable' do Pry.input = InputTester.new("5") - str_output = StringIO.new - Pry.output = str_output + Pry.output = @str_output Pry.new.rep - str_output.string.should =~ /5/ + @str_output.string.should =~ /5/ Pry.new(:input => InputTester.new("6")).rep - str_output.string.should =~ /6/ + @str_output.string.should =~ /6/ end it 'should pass in the prompt if readline arity is 1' do @@ -74,19 +77,18 @@ describe "test Pry defaults" do it 'should set the output default, and the default should be overridable' do Pry.input = InputTester.new("5", "6", "7") - str_output = StringIO.new - Pry.output = str_output + Pry.output = @str_output Pry.new.rep - str_output.string.should =~ /5/ + @str_output.string.should =~ /5/ Pry.new.rep - str_output.string.should =~ /5\n.*6/ + @str_output.string.should =~ /5\n.*6/ - str_output2 = StringIO.new - Pry.new(:output => str_output2).rep - str_output2.string.should.not =~ /5\n.*6/ - str_output2.string.should =~ /7/ + @str_output = StringIO.new + Pry.new(:output => @str_output).rep + @str_output.string.should.not =~ /5\n.*6/ + @str_output.string.should =~ /7/ end it "should set the print default, and the default should be overridable" do @@ -94,19 +96,18 @@ describe "test Pry defaults" do Pry.print = new_print Pry.new.print.should == Pry.print - str_output = StringIO.new - Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep - str_output.string.should == "test\n" + Pry.new(:input => InputTester.new("\"test\""), :output => @str_output).rep + @str_output.string.should == "test\n" - str_output = StringIO.new - Pry.new(:input => InputTester.new("\"test\""), :output => str_output, + @str_output = StringIO.new + Pry.new(:input => InputTester.new("\"test\""), :output => @str_output, :print => proc { |out, value| out.puts value.reverse }).rep - str_output.string.should == "tset\n" + @str_output.string.should == "tset\n" Pry.new.print.should == Pry.print - str_output = StringIO.new - Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep - str_output.string.should == "test\n" + @str_output = StringIO.new + Pry.new(:input => InputTester.new("\"test\""), :output => @str_output).rep + @str_output.string.should == "test\n" end describe "pry return values" do @@ -333,70 +334,22 @@ describe "test Pry defaults" do end - it 'should set the hooks default, and the default should be overridable' do - Pry.input = InputTester.new("exit-all") - Pry.hooks = Pry::Hooks.new. - add_hook(:before_session, :my_name) { |out,_,_| out.puts "HELLO" }. - add_hook(:after_session, :my_name) { |out,_,_| out.puts "BYE" } - - str_output = StringIO.new - Pry.new(:output => str_output).repl - str_output.string.should =~ /HELLO/ - str_output.string.should =~ /BYE/ - - Pry.input.rewind - - str_output = StringIO.new - Pry.new(:output => str_output, - :hooks => Pry::Hooks.new. - add_hook( :before_session, :my_name) { |out,_,_| out.puts "MORNING" }. - add_hook(:after_session, :my_name) { |out,_,_| out.puts "EVENING" } - ).repl - - str_output.string.should =~ /MORNING/ - str_output.string.should =~ /EVENING/ - - # try below with just defining one hook - Pry.input.rewind - str_output = StringIO.new - Pry.new(:output => str_output, - :hooks => Pry::Hooks.new. - add_hook(:before_session, :my_name) { |out,_,_| out.puts "OPEN" } - ).repl - - str_output.string.should =~ /OPEN/ - - Pry.input.rewind - str_output = StringIO.new - Pry.new(:output => str_output, - :hooks => Pry::Hooks.new. - add_hook(:after_session, :my_name) { |out,_,_| out.puts "CLOSE" } - ).repl - - str_output.string.should =~ /CLOSE/ - - Pry.reset_defaults - Pry.color = false - end - describe 'quiet' do it 'should show whereami by default' do - output = StringIO.new Pry.start(binding, :input => InputTester.new("1", "exit-all"), - :output => output, + :output => @str_output, :hooks => Pry::DEFAULT_HOOKS) - output.string.should =~ /[w]hereami by default/ + @str_output.string.should =~ /[w]hereami by default/ end it 'should hide whereami if quiet is set' do - output = StringIO.new Pry.new(:input => InputTester.new("exit-all"), - :output => output, + :output => @str_output, :quiet => true, :hooks => Pry::DEFAULT_HOOKS) - output.string.should == "" + @str_output.string.should == "" end end @@ -423,4 +376,49 @@ describe "test Pry defaults" do end end end + + it 'should set the hooks default, and the default should be overridable' do + Pry.input = InputTester.new("exit-all") + Pry.hooks = Pry::Hooks.new. + add_hook(:before_session, :my_name) { |out,_,_| out.puts "HELLO" }. + add_hook(:after_session, :my_name) { |out,_,_| out.puts "BYE" } + + Pry.new(:output => @str_output).repl + @str_output.string.should =~ /HELLO/ + @str_output.string.should =~ /BYE/ + + Pry.input.rewind + + @str_output = StringIO.new + Pry.new(:output => @str_output, + :hooks => Pry::Hooks.new. + add_hook( :before_session, :my_name) { |out,_,_| out.puts "MORNING" }. + add_hook(:after_session, :my_name) { |out,_,_| out.puts "EVENING" } + ).repl + + @str_output.string.should =~ /MORNING/ + @str_output.string.should =~ /EVENING/ + + # try below with just defining one hook + Pry.input.rewind + @str_output = StringIO.new + Pry.new(:output => @str_output, + :hooks => Pry::Hooks.new. + add_hook(:before_session, :my_name) { |out,_,_| out.puts "OPEN" } + ).repl + + @str_output.string.should =~ /OPEN/ + + Pry.input.rewind + @str_output = StringIO.new + Pry.new(:output => @str_output, + :hooks => Pry::Hooks.new. + add_hook(:after_session, :my_name) { |out,_,_| out.puts "CLOSE" } + ).repl + + @str_output.string.should =~ /CLOSE/ + + Pry.reset_defaults + Pry.color = false + end end diff --git a/test/test_syntax_checking.rb b/test/test_syntax_checking.rb index 5527e048..cdb77109 100644 --- a/test/test_syntax_checking.rb +++ b/test/test_syntax_checking.rb @@ -1,5 +1,9 @@ require 'helper' + describe Pry do + before do + @str_output = StringIO.new + end [ ["p = '", "'"], @@ -11,12 +15,11 @@ describe Pry do ["pouts(< true/m end end