1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Add support for play 69 syntax

`play 69` is a shortcut for `play --file #{_file_} --lines 69`. It plays
lines from the current file. Example (I omitted some useless
information):

  pry(main)> show-source hello
    def hello
      binding.pry
      true
      puts "hi"
      69
    end
  pry(main)> hello
    1: def hello
 => 2:   binding.pry
    3:   true
    4:   puts "hi"
    5:   69
    6: end
  pry(main)> play 5
  => 69
  pry(main)> play 3..4
  hi
  => nil
  pry(main)>

Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
This commit is contained in:
Kyrylo Silin 2012-12-01 05:22:16 +02:00
parent bf11c06a87
commit de50544759
2 changed files with 85 additions and 13 deletions

View file

@ -22,7 +22,14 @@ class Pry
attr_accessor :content
def setup
self.content = ""
self.content = ""
@integer_or_range = %r/
\A # Example:
\d+ # 22
(?:\.{2,3} # ...
\d+)? # 24
\z
/x # Matches: "22..24" or "22".
end
def options(opt)
@ -69,7 +76,11 @@ class Pry
end
def perform_play
process_non_opt
if args.first =~ @integer_or_range
process_first_argument
else
process_non_opt
end
if opts.present?(:lines)
self.content = restrict_to_lines(self.content, opts[:l])
@ -81,5 +92,22 @@ class Pry
eval_string << self.content
end
# Tries to play lines from a file.
# Mimicking `play --file #{_file_} --lines 69`.
def process_first_argument
return unless _pry_.last_file
start_line, exclusive, end_line = args.first.split(/(\.{2,3})/)
lines = if exclusive.nil?
start_line.to_i
else
Range.new(start_line.to_i, end_line.to_i, exclusive.length == 3)
end
self.content << File.read(_pry_.last_file)
self.content = restrict_to_lines(self.content, lines)
end
end
end

View file

@ -5,22 +5,66 @@ describe "play" do
@t = pry_tester
end
it 'should play a string variable (with no args)' do
eval_str = ''
describe "with an argument" do
before do
@eval_str = ''
end
@t.eval 'x = "\"hello\""'
@t.process_command 'play x', eval_str
describe "string variable" do
it "without --lines switch" do
@t.eval 'x = "\"hello\""'
@t.process_command 'play x', @eval_str
@eval_str.should == '"hello"'
end
eval_str.should == '"hello"'
end
it 'using --lines switch to select what to play' do
@t.eval 'x = "\"hello\"\n\"goodbye\"\n\"love\""'
@t.process_command 'play x --lines 1', @eval_str
@eval_str.should == "\"hello\"\n"
end
end
it 'should play a string variable (with no args) using --lines to select what to play' do
eval_str = ''
describe "numbers" do
before do
@tempfile = Tempfile.new(%w|pry .rb|)
@tempfile.puts <<-EOS
bing = :bing
bang = :bang
bong = :bong
EOS
@tempfile.flush
@t.eval 'x = "\"hello\"\n\"goodbye\"\n\"love\""'
@t.process_command 'play x --lines 1', eval_str
@t.eval %|_pry_.last_file = "#{ @tempfile.path }"|
end
eval_str.should == "\"hello\"\n"
after do
@tempfile.close(true)
end
describe "integer" do
it "should process one line from _pry_.last_file" do
@t.process_command 'play 1', @eval_str
@eval_str.should =~ /bing = :bing\n/
end
end
describe "range" do
it "should process multiple lines at once from _pry_.last_file" do
@t.process_command 'play 1..3', @eval_str
[/bing = :bing\n/, /bang = :bang\n/, /bong = :bong\n/].each { |str|
@eval_str.should =~ str
}
end
end
end
describe "malformed" do
it "should return nothing" do
@t.process_command 'play 69', @eval_str
@eval_str.should == ''
lambda { @t.process_command('play zZz') }.should.raise Pry::CommandError
end
end
end
it 'should play documentation with the -d switch' do