diff --git a/lib/pry/commands/play.rb b/lib/pry/commands/play.rb index 9dc18731..3f53f58c 100644 --- a/lib/pry/commands/play.rb +++ b/lib/pry/commands/play.rb @@ -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 diff --git a/spec/commands/play_spec.rb b/spec/commands/play_spec.rb index 3f65ff32..ba1ba438 100644 --- a/spec/commands/play_spec.rb +++ b/spec/commands/play_spec.rb @@ -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