diff --git a/test/helper.rb b/test/helper.rb index fdc0cd39..0a7d70a1 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -8,6 +8,7 @@ require 'bacon' # Ensure we do not execute any rc files Pry::RC_FILES.clear Pry.color = false +Pry.pager = false Pry.should_load_rc = false # sample doc diff --git a/test/test_command_set.rb b/test/test_command_set.rb index 10dfa63f..82211298 100644 --- a/test/test_command_set.rb +++ b/test/test_command_set.rb @@ -177,4 +177,14 @@ describe Pry::CommandSet do @set.command('foo') { should.respond_to :imported_helper_method } @set.run_command(Pry::CommandContext.new, 'foo') end + + it 'should provide a :listing for a command that defaults to its name' do + @set.command 'foo', "" do;end + @set.commands['foo'].options[:listing].should == 'foo' + end + + it 'should provide a :listing for a command that differs from its name' do + @set.command 'foo', "", :listing => 'bar' do;end + @set.commands['foo'].options[:listing].should == 'bar' + end end diff --git a/test/test_default_commands.rb b/test/test_default_commands.rb index 0edfe464..0a140a1b 100644 --- a/test/test_default_commands.rb +++ b/test/test_default_commands.rb @@ -7,13 +7,17 @@ describe "Pry::Commands" do end describe "hist" do + push_first_hist_line = lambda do |hist, line| + hist.push line while hist.empty? + end + before do Readline::HISTORY.shift until Readline::HISTORY.empty? @hist = Readline::HISTORY end it 'should display the correct history' do - @hist.push "'bug in 1.8 means this line is ignored'" if RUBY_VERSION =~ /1.8/ + push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") @hist.push "hello" @hist.push "world" str_output = StringIO.new @@ -24,33 +28,83 @@ describe "Pry::Commands" do end it 'should replay history correctly (single item)' do - @hist.push "'bug in 1.8 means this line is ignored'" if RUBY_VERSION =~ /1.8/ - @hist.push "cd :hello" + push_first_hist_line.call(@hist, ":hello") + @hist.push ":blah" + @hist.push ":bucket" + @hist.push ":ostrich" str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --replay 0", "self", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hist --replay -1", "exit-all"), str_output) do pry end - str_output.string.should =~ /hello/ + str_output.string.should =~ /ostrich/ end it 'should replay a range of history correctly (range of items)' do - @hist.push "'bug in 1.8 means this line is ignored'" if RUBY_VERSION =~ /1.8/ + push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'") @hist.push ":hello" @hist.push ":carl" str_output = StringIO.new - redirect_pry_io(InputTester.new("hist --replay 0..1", "exit-all"), str_output) do + redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all"), str_output) do pry end str_output.string.should =~ /:hello\n.*:carl/ end + it 'should grep for correct lines in history' do + push_first_hist_line.call(@hist, "apple") + @hist.push "abby" + @hist.push "box" + @hist.push "button" + @hist.push "pepper" + @hist.push "orange" + @hist.push "grape" + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --grep o", "exit-all"), str_output) do + pry + end + + str_output.string.should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/ + end + + it 'should return last N lines in history with --tail switch' do + push_first_hist_line.call(@hist, "0") + ("a".."z").each do |v| + @hist.push v + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --tail 3", "exit-all"), str_output) do + pry + end + + str_output.string.each_line.count.should == 3 + str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/ + end + + # strangeness in this test is due to bug in Readling::HISTORY not + # always registering first line of input + it 'should return first N lines in history with --head switch' do + push_first_hist_line.call(@hist, "0") + ("a".."z").each do |v| + @hist.push v + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hist --head 4", "exit-all"), str_output) do + pry + end + + str_output.string.each_line.count.should == 4 + str_output.string.should =~ /a\n\d+:.*b\n\d+:.*c/ + end end describe "show-method" do it 'should output a method\'s source' do str_output = StringIO.new redirect_pry_io(InputTester.new("show-method sample_method", "exit-all"), str_output) do - Pry.new.repl(TOPLEVEL_BINDING) + pry end str_output.string.should =~ /def sample/ @@ -59,7 +113,7 @@ describe "Pry::Commands" do it 'should output a method\'s source with line numbers' do str_output = StringIO.new redirect_pry_io(InputTester.new("show-method -l sample_method", "exit-all"), str_output) do - Pry.new.repl(TOPLEVEL_BINDING) + pry end str_output.string.should =~ /\d+: def sample/ @@ -99,7 +153,7 @@ describe "Pry::Commands" do it 'should output a method\'s source for a method defined inside pry' do str_output = StringIO.new redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do - Pry.new.repl(TOPLEVEL_BINDING) + TOPLEVEL_BINDING.pry end str_output.string.should =~ /def dyna_method/ @@ -109,7 +163,7 @@ describe "Pry::Commands" do it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do str_output = StringIO.new redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do - Pry.new.repl(TOPLEVEL_BINDING) + TOPLEVEL_BINDING.pry end str_output.string.should =~ /def dyna_method/ @@ -119,7 +173,7 @@ describe "Pry::Commands" do it 'should output an instance method\'s source for a method defined inside pry' do str_output = StringIO.new redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-method A#yo"), str_output) do - Pry.new.repl(TOPLEVEL_BINDING) + TOPLEVEL_BINDING.pry end str_output.string.should =~ /def yo/ @@ -129,7 +183,7 @@ describe "Pry::Commands" do it 'should output an instance method\'s source for a method defined inside pry using define_method' do str_output = StringIO.new redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "end", "show-method A#yup"), str_output) do - Pry.new.repl(TOPLEVEL_BINDING) + TOPLEVEL_BINDING.pry end str_output.string.should =~ /define_method\(:yup\)/ @@ -142,7 +196,7 @@ describe "Pry::Commands" do it 'should output a method\'s documentation' do str_output = StringIO.new redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output) do - Pry.new.repl(TOPLEVEL_BINDING) + pry end str_output.string.should =~ /sample doc/ @@ -171,7 +225,7 @@ describe "Pry::Commands" do b.eval("x = :mon_ouie") redirect_pry_io(InputTester.new("cd x", "$obj = self", "exit-all"), StringIO.new) do - Pry.new.repl(b) + b.pry end $obj.should == :mon_ouie @@ -182,7 +236,7 @@ describe "Pry::Commands" do b.eval("x = :inner") redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd ..", "$outer = self", "exit-all"), StringIO.new) do - Pry.new.repl(b) + b.pry end $inner.should == :inner $outer.should == :outer @@ -193,7 +247,7 @@ describe "Pry::Commands" do 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 - Pry.new.repl(b) + b.pry end $inner.should == :inner $five.should == 5 @@ -204,7 +258,7 @@ describe "Pry::Commands" do b = Pry.binding_for(:outer) redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all"), StringIO.new) do - Pry.new.repl(5) + 5.pry end $obj.should == TOPLEVEL_BINDING.eval('self') end @@ -216,7 +270,7 @@ describe "Pry::Commands" do end redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all"), StringIO.new) do - Pry.new.repl(o) + o.pry end $obj.should == :mon_ouie end diff --git a/test/test_pry.rb b/test/test_pry.rb index e8b09bb5..bec86378 100644 --- a/test/test_pry.rb +++ b/test/test_pry.rb @@ -415,7 +415,83 @@ describe Pry do $test_interpolation = nil end - it 'should create a comand in a nested context and that command should be accessible from the parent' do + it 'should create a command with a space in its name' do + set = Pry::CommandSet.new do + command "hello baby", "" do + output.puts "hello baby command" + end + end + + str_output = StringIO.new + 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/ + end + + it 'should create a command with a space in its name and pass an argument' do + set = Pry::CommandSet.new do + command "hello baby", "" do |arg| + output.puts "hello baby command #{arg}" + end + end + + str_output = StringIO.new + 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/ + end + + it 'should create a regex command and be able to invoke it' do + set = Pry::CommandSet.new do + command /hello(.)/, "" do + c = captures.first + output.puts "hello#{c}" + end + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hello1"), str_output) do + Pry.new(:commands => set).rep + end + + str_output.string.should =~ /hello1/ + end + + it 'should create a regex command and pass captures into the args list before regular arguments' do + set = Pry::CommandSet.new do + command /hello(.)/, "" do |c1, a1| + output.puts "hello #{c1} #{a1}" + end + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hello1 baby"), str_output) do + Pry.new(:commands => set).rep + end + + str_output.string.should =~ /hello 1 baby/ + end + + it 'if a regex capture is missing it should be nil' do + set = Pry::CommandSet.new do + command /hello(.)?/, "" do |c1, a1| + output.puts "hello #{c1.inspect} #{a1}" + end + end + + str_output = StringIO.new + redirect_pry_io(InputTester.new("hello baby"), str_output) do + Pry.new(:commands => set).rep + end + + 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_input = StringIO.new("@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit") str_output = StringIO.new Pry.input = str_input @@ -502,8 +578,6 @@ describe Pry do klass = Pry::CommandSet.new do alias_command "help2", "help" end - - klass.commands["help2"].block.should == klass.commands["help"].block end