2011-06-02 11:26:49 -04:00
|
|
|
require 'helper'
|
|
|
|
|
|
|
|
describe "Pry::DefaultCommands::Input" do
|
|
|
|
|
2011-09-08 01:17:01 -04:00
|
|
|
describe "amend-line" do
|
2011-06-02 11:26:49 -04:00
|
|
|
it 'should correctly amend the last line of input when no line number specified ' do
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("def hello", "puts :bing", "amend-line puts :blah", "show-input", "exit-all"), str_output) do
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /\A\d+: def hello\n\d+: puts :blah/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly amend the specified line of input when line number given ' do
|
|
|
|
str_output = StringIO.new
|
2011-06-16 09:51:32 -04:00
|
|
|
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/
|
|
|
|
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
|
2011-06-02 11:26:49 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /\A\d+: def goodbye\n\d+: puts :bing\n\d+: puts :bang/
|
|
|
|
end
|
2011-06-10 07:16:51 -04:00
|
|
|
|
2011-06-13 08:51:09 -04:00
|
|
|
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
|
|
|
|
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
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
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
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bink\n\d+: puts :boat/
|
|
|
|
end
|
|
|
|
|
2011-06-10 07:16:51 -04:00
|
|
|
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
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
2011-06-10 10:10:07 -04:00
|
|
|
str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing\n\d+: puts \"\#{goodbye}\"/
|
2011-06-10 07:16:51 -04:00
|
|
|
end
|
|
|
|
|
2011-06-13 07:19:19 -04:00
|
|
|
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
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /No input to amend/
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-06-12 02:14:54 -04:00
|
|
|
it 'should correctly amend the specified range of lines' do
|
|
|
|
str_output = StringIO.new
|
2011-06-16 09:51:32 -04:00
|
|
|
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
|
2011-06-12 02:14:54 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
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
|
2011-06-16 09:51:32 -04:00
|
|
|
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
|
2011-06-12 02:14:54 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
2011-06-15 09:45:28 -04:00
|
|
|
str_output.string.should =~ /\d+: def hello\n\d+: puts :bing\n\d+: puts :boast\n\d+: puts :heart/
|
2011-06-12 02:14:54 -04:00
|
|
|
end
|
|
|
|
|
2011-06-13 07:19:19 -04:00
|
|
|
it 'should correctly delete a range of lines using the ! for content' do
|
|
|
|
str_output = StringIO.new
|
2011-06-16 09:51:32 -04:00
|
|
|
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
|
2011-06-13 07:19:19 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
2011-06-15 09:45:28 -04:00
|
|
|
str_output.string.should =~ /\d+: def hello\n\d+: puts :heart\n\Z/
|
2011-06-13 07:19:19 -04:00
|
|
|
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
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
2011-06-15 09:45:28 -04:00
|
|
|
str_output.string.should =~ /\d+: def hello\n\d+: puts :bing\n\d+: puts :bang\n\d+: puts :boast\n\Z/
|
2011-06-13 07:19:19 -04:00
|
|
|
end
|
|
|
|
|
2011-06-12 02:14:54 -04:00
|
|
|
it 'should correctly amend the specified range of lines, using negative numbers in range' do
|
|
|
|
str_output = StringIO.new
|
2011-09-08 01:17:01 -04:00
|
|
|
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
|
2011-06-12 02:14:54 -04:00
|
|
|
pry
|
|
|
|
end
|
2011-06-15 09:45:28 -04:00
|
|
|
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
|
2011-06-16 09:51:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 2 >puts :inserted", "show-input", "exit-all"), str_output) do
|
2011-06-15 09:45:28 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
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
|
2011-06-16 09:51:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("def hello", "puts :bing", "puts :bang", "amend-line 2..21 >puts :inserted", "show-input", "exit-all"), str_output) do
|
2011-06-15 09:45:28 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /\d+: def hello\n\d+: puts :inserted\n\d+: puts :bing\n\d+: puts :bang/
|
2011-06-12 02:14:54 -04:00
|
|
|
end
|
2011-06-02 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
2011-06-03 05:55:23 -04:00
|
|
|
describe "show-input" do
|
|
|
|
it 'should correctly show the current lines in the input buffer' do
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("def hello", "puts :bing", "show-input", "exit-all"), str_output) do
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /\A\d+: def hello\n\d+: puts :bing/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "!" do
|
|
|
|
it 'should correctly clear the input buffer ' do
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("def hello", "puts :bing", "!", "show-input", "exit-all"), str_output) do
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
stripped_output = str_output.string.strip!
|
|
|
|
stripped_output.each_line.count.should == 1
|
|
|
|
stripped_output.should =~ /Input buffer cleared!/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-12 09:07:42 -04:00
|
|
|
describe "play" do
|
2011-09-06 07:55:41 -04:00
|
|
|
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
|
|
|
|
Pry.start b, :hooks => {}
|
2011-06-12 09:07:42 -04:00
|
|
|
end
|
2011-09-06 07:55:41 -04:00
|
|
|
str_output.string.should =~ /hello/
|
2011-06-12 09:07:42 -04:00
|
|
|
end
|
|
|
|
|
2011-09-06 07:55:41 -04:00
|
|
|
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
|
|
|
|
Pry.start b, :hooks => {}
|
2011-06-12 09:07:42 -04:00
|
|
|
end
|
2011-09-06 07:55:41 -04:00
|
|
|
str_output.string.should =~ /hello/
|
|
|
|
str_output.string.should.not =~ /love/
|
|
|
|
str_output.string.should.not =~ /goodbye/
|
2011-06-12 09:07:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should play a method with the -m switch (a single line)' do
|
|
|
|
$o = Object.new
|
|
|
|
def $o.test_method
|
|
|
|
:test_method_content
|
|
|
|
end
|
|
|
|
|
2011-06-16 09:51:32 -04:00
|
|
|
redirect_pry_io(InputTester.new('play -m $o.test_method --lines 2', "exit-all"), str_output = StringIO.new) do
|
2011-06-12 09:07:42 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.should =~ /:test_method_content/
|
|
|
|
$o = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should play a method with the -m switch (multiple line)' do
|
|
|
|
$o = Object.new
|
|
|
|
def $o.test_method
|
|
|
|
1 + 102
|
|
|
|
5 * 6
|
|
|
|
end
|
|
|
|
|
2011-06-16 09:51:32 -04:00
|
|
|
redirect_pry_io(InputTester.new('play -m $o.test_method --lines 2..3', "exit-all"), str_output = StringIO.new) do
|
2011-06-12 09:07:42 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.should =~ /103\n.*30/
|
|
|
|
$o = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-06-02 11:26:49 -04:00
|
|
|
describe "hist" do
|
|
|
|
push_first_hist_line = lambda do |hist, line|
|
|
|
|
hist.push line
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.clear
|
|
|
|
@hist = Pry.history
|
2011-06-02 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should display the correct history' do
|
|
|
|
push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'")
|
|
|
|
@hist.push "hello"
|
|
|
|
@hist.push "world"
|
|
|
|
str_output = StringIO.new
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist", "exit-all", :history => @hist), str_output) do
|
2011-06-02 11:26:49 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /hello\n.*world/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should replay history correctly (single item)' do
|
|
|
|
push_first_hist_line.call(@hist, ":hello")
|
|
|
|
@hist.push ":blah"
|
|
|
|
@hist.push ":bucket"
|
|
|
|
@hist.push ":ostrich"
|
|
|
|
str_output = StringIO.new
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist --replay -1", "exit-all", :history => @hist), str_output) do
|
2011-06-02 11:26:49 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /ostrich/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should replay a range of history correctly (range of items)' do
|
|
|
|
push_first_hist_line.call(@hist, "'bug in 1.8 means this line is ignored'")
|
|
|
|
@hist.push ":hello"
|
|
|
|
@hist.push ":carl"
|
|
|
|
str_output = StringIO.new
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist --replay 0..2", "exit-all", :history => @hist), str_output) do
|
2011-06-02 11:26:49 -04:00
|
|
|
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"
|
2011-06-16 09:51:32 -04:00
|
|
|
@hist.push "def blah 1"
|
|
|
|
@hist.push "def boink 2"
|
|
|
|
@hist.push "place holder"
|
2011-06-02 11:26:49 -04:00
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist --grep o", "exit-all", :history => @hist), str_output) do
|
2011-06-02 11:26:49 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/
|
2011-06-16 09:51:32 -04:00
|
|
|
|
|
|
|
# test more than one word in a regex match (def blah)
|
|
|
|
str_output = StringIO.new
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist --grep def blah", "exit-all", :history => @hist), str_output) do
|
2011-06-16 09:51:32 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /def blah 1/
|
|
|
|
|
|
|
|
# test more than one word with leading white space in a regex match (def boink)
|
|
|
|
str_output = StringIO.new
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist --grep def boink", "exit-all", :history => @hist), str_output) do
|
2011-06-16 09:51:32 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /def boink 2/
|
2011-06-02 11:26:49 -04:00
|
|
|
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
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist --tail 3", "exit-all", :history => @hist), str_output) do
|
2011-06-02 11:26:49 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.each_line.count.should == 3
|
|
|
|
str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/
|
|
|
|
end
|
|
|
|
|
|
|
|
# strangeness in this test is due to bug in Readline::HISTORY not
|
|
|
|
# always registering first line of input
|
|
|
|
it 'should return first N lines in history with --head switch' do
|
|
|
|
push_first_hist_line.call(@hist, "0")
|
|
|
|
("a".."z").each do |v|
|
|
|
|
@hist.push v
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist --head 4", "exit-all", :history => @hist), str_output) do
|
2011-06-02 11:26:49 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.each_line.count.should == 4
|
|
|
|
str_output.string.should =~ /a\n\d+:.*b\n\d+:.*c/
|
|
|
|
end
|
|
|
|
|
|
|
|
# strangeness in this test is due to bug in Readline::HISTORY not
|
|
|
|
# always registering first line of input
|
|
|
|
it 'should show lines between lines A and B with the --show switch' do
|
|
|
|
push_first_hist_line.call(@hist, "0")
|
|
|
|
("a".."z").each do |v|
|
|
|
|
@hist.push v
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output = StringIO.new
|
2011-07-24 19:20:32 -04:00
|
|
|
redirect_pry_io(InputTester.new("hist --show 1..4", "exit-all", :history => @hist), str_output) do
|
2011-06-02 11:26:49 -04:00
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.each_line.count.should == 4
|
|
|
|
str_output.string.should =~ /b\n\d+:.*c\n\d+:.*d/
|
|
|
|
end
|
2011-08-07 04:50:33 -04:00
|
|
|
|
|
|
|
it "should not contain duplicated lines" do
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("3", "_ += 1", "_ += 1", "hist", "exit-all", :history => @hist), str_output) do
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.each_line.grep(/_ \+= 1/).count.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not contain duplicated lines" do
|
|
|
|
str_output = StringIO.new
|
2011-08-11 07:38:45 -04:00
|
|
|
redirect_pry_io(InputTester.new(":place_holder", "2 + 2", "", "", "3 + 3", "hist", "exit-all", :history => @hist), str_output) do
|
2011-08-07 04:50:33 -04:00
|
|
|
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 + 1).should == b
|
|
|
|
end
|
2011-06-02 11:26:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|