Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kyrylo committed Dec 1, 2012
1 parent bf11c06 commit de50544
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 15 deletions.
32 changes: 30 additions & 2 deletions lib/pry/commands/play.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -69,7 +76,11 @@ def process_non_opt
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])
Expand All @@ -81,5 +92,22 @@ def perform_play

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
70 changes: 57 additions & 13 deletions spec/commands/play_spec.rb
Expand Up @@ -5,22 +5,66 @@
@t = pry_tester
end

it 'should play a string variable (with no args)' do
eval_str = ''

@t.eval 'x = "\"hello\""'
@t.process_command 'play x', eval_str

eval_str.should == '"hello"'
end
describe "with an argument" do
before do
@eval_str = ''
end

it 'should play a string variable (with no args) using --lines to select what to play' do
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

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

@t.eval 'x = "\"hello\"\n\"goodbye\"\n\"love\""'
@t.process_command 'play x --lines 1', 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 %|_pry_.last_file = "#{ @tempfile.path }"|
end

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

eval_str.should == "\"hello\"\n"
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
Expand Down

0 comments on commit de50544

Please sign in to comment.