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

Rename accept_line to eval

This commit is contained in:
Ryan Fitzgerald 2012-12-27 22:06:50 -08:00
parent 06370d6c86
commit 1ea1927b43
11 changed files with 43 additions and 38 deletions

View file

@ -233,22 +233,25 @@ class Pry
@eval_string = "" @eval_string = ""
end end
# Pass a line of input to pry. # Pass a line of input to Pry.
#
# This is the equivalent of `Binding#eval` but with extra Pry!
# #
# This is the equivalent of `binding.eval` but with extra pry!
# In particular: # In particular:
# 1. Pry-commands will be executed immediately if the line matches, # 1. Pry commands will be executed immediately if the line matches.
# 2. Partial lines of input will be queued up until a complete expression has been # 2. Partial lines of input will be queued up until a complete expression has
# accepted, # been accepted.
# 3. Output is written to {#output} in pretty colours, not returned. # 3. Output is written to {#output} in pretty colours, not returned.
# #
# Once this method has raised an exception or returned false, this instance of pry # Once this method has raised an exception or returned false, this instance
# is no longer usable. You can return pry.exit_value to your caller. # is no longer usable. {#exit_value} will return the session's breakout
# value if applicable.
# #
# @param [String, nil] line The line of input, nil if the user types <ctrl-d> # @param [String?] line The line of input; `nil` if the user types `<Ctrl-D>`
# @return [Boolean] true if pry is ready for more input, false otherwise # @return [Boolean] Is Pry ready to accept more input?
# @raise [Exception] if the user has explicitly 'raise-up'd an exception # @raise [Exception] If the user uses the `raise-up` command, this method
def accept_line(line) # will raise that exception.
def eval(line)
return false if @stopped return false if @stopped
exit_value = nil exit_value = nil

View file

@ -50,7 +50,7 @@ class Pry
break break
else else
output.puts "" if val.nil? && output.tty? output.puts "" if val.nil? && output.tty?
return pry.exit_value unless pry.accept_line(val) return pry.exit_value unless pry.eval(val)
end end
end end
end end

View file

@ -105,7 +105,7 @@ class PryTester
attr_reader :pry, :out attr_reader :pry, :out
def_delegators :@pry, :eval_string, :eval_string=, :accept_line def_delegators :@pry, :eval_string, :eval_string=
def initialize(target = TOPLEVEL_BINDING, options = {}) def initialize(target = TOPLEVEL_BINDING, options = {})
@pry = Pry.new(options.merge(:target => target)) @pry = Pry.new(options.merge(:target => target))
@ -132,8 +132,10 @@ class PryTester
result result
end end
def accept_lines(*lines) def push(*lines)
lines.each(&method(:accept_line)) Array(lines).flatten.each do |line|
@pry.eval(line)
end
end end
def push_binding(context) def push_binding(context)

View file

@ -218,7 +218,7 @@ describe "commands" do
describe "Pry#run_command" do describe "Pry#run_command" do
it 'should run a command that modifies the passed in eval_string' do it 'should run a command that modifies the passed in eval_string' do
p = Pry.new(:output => @str_output) p = Pry.new(:output => @str_output)
p.accept_line "def hello\npeter pan\n" p.eval "def hello\npeter pan\n"
p.run_command "amend-line !" p.run_command "amend-line !"
p.eval_string.should =~ /def hello/ p.eval_string.should =~ /def hello/
p.eval_string.should.not =~ /peter pan/ p.eval_string.should.not =~ /peter pan/

View file

@ -6,7 +6,7 @@ describe "amend-line" do
end end
it 'should amend the last line of input when no line number specified' do it 'should amend the last line of input when no line number specified' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
STR STR
@ -20,7 +20,7 @@ describe "amend-line" do
end end
it 'should amend the specified line of input when line number given' do it 'should amend the specified line of input when line number given' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -36,7 +36,7 @@ describe "amend-line" do
end end
it 'should amend the first line of input when 0 given as line number' do it 'should amend the first line of input when 0 given as line number' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -52,7 +52,7 @@ describe "amend-line" do
end end
it 'should amend a specified line when negative number given' do it 'should amend a specified line when negative number given' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -76,7 +76,7 @@ describe "amend-line" do
end end
it 'should amend a range of lines of input when negative numbers given' do it 'should amend a range of lines of input when negative numbers given' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -93,7 +93,7 @@ describe "amend-line" do
end end
it 'should correctly amend the specified line with interpolated text' do it 'should correctly amend the specified line with interpolated text' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -122,7 +122,7 @@ describe "amend-line" do
end end
it 'should correctly amend the specified range of lines' do it 'should correctly amend the specified range of lines' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -139,7 +139,7 @@ describe "amend-line" do
end end
it 'should correctly delete a specific line using the ! for content' do it 'should correctly delete a specific line using the ! for content' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -158,7 +158,7 @@ describe "amend-line" do
end end
it 'should correctly delete a range of lines using the ! for content' do it 'should correctly delete a range of lines using the ! for content' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -175,7 +175,7 @@ describe "amend-line" do
end end
it 'should correctly delete the previous line using the ! for content' do it 'should correctly delete the previous line using the ! for content' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -194,7 +194,7 @@ describe "amend-line" do
end end
it 'should amend the specified range of lines, with numbers < 0 in range' do it 'should amend the specified range of lines, with numbers < 0 in range' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -212,7 +212,7 @@ describe "amend-line" do
end end
it 'should correctly insert a line before a specified line using >' do it 'should correctly insert a line before a specified line using >' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -229,7 +229,7 @@ describe "amend-line" do
end end
it 'should ignore second value of range with > syntax' do it 'should ignore second value of range with > syntax' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
puts :bang puts :bang

View file

@ -6,7 +6,7 @@ describe "!" do
end end
it 'should correctly clear the input buffer ' do it 'should correctly clear the input buffer ' do
@t.accept_line unindent(<<-STR) @t.push unindent(<<-STR)
def hello def hello
puts :bing puts :bing
STR STR

View file

@ -234,7 +234,7 @@ describe "edit" do
end end
it "should edit the current expression if it's incomplete" do it "should edit the current expression if it's incomplete" do
@t.accept_line 'def a' @t.push 'def a'
@t.process_command 'edit' @t.process_command 'edit'
@contents.should == "def a\n" @contents.should == "def a\n"
end end
@ -250,7 +250,7 @@ describe "edit" do
end end
it "should use a blank file if -t given, even during an expression" do it "should use a blank file if -t given, even during an expression" do
@t.accept_line 'def a;' @t.push 'def a;'
@t.process_command 'edit -t' @t.process_command 'edit -t'
@contents.should == "\n" @contents.should == "\n"
end end

View file

@ -109,7 +109,7 @@ describe "play" do
end end
it 'should APPEND to the input buffer when playing a line with play -m, not replace it' do it 'should APPEND to the input buffer when playing a line with play -m, not replace it' do
@t.accept_line 'def another_test_method' @t.push 'def another_test_method'
@t.process_command 'play -m test_method --lines 2' @t.process_command 'play -m test_method --lines 2'
@t.eval_string.should == unindent(<<-STR) @t.eval_string.should == unindent(<<-STR)
def another_test_method def another_test_method

View file

@ -6,7 +6,7 @@ describe "show-input" do
end end
it 'should correctly show the current lines in the input buffer' do it 'should correctly show the current lines in the input buffer' do
@t.accept_lines *unindent(<<-STR).split("\n") @t.push *unindent(<<-STR).split("\n")
def hello def hello
puts :bing puts :bing
STR STR

View file

@ -19,7 +19,7 @@ describe Pry::DEFAULT_CONTROL_D_HANDLER do
it 'should break out of a REPL loop' do it 'should break out of a REPL loop' do
instance = Pry.new instance = Pry.new
instance.binding_stack.should.not.be.empty instance.binding_stack.should.not.be.empty
instance.accept_line(nil).should.be.false instance.eval(nil).should.be.false
instance.binding_stack.should.be.empty instance.binding_stack.should.be.empty
end end
end end
@ -29,7 +29,7 @@ describe Pry::DEFAULT_CONTROL_D_HANDLER do
t = pry_tester t = pry_tester
t.eval "cd Object.new" t.eval "cd Object.new"
t.eval("_pry_.binding_stack.size").should == 2 t.eval("_pry_.binding_stack.size").should == 2
t.eval("_pry_.accept_line(nil)").should.be.true t.eval("_pry_.eval(nil)").should.be.true
t.eval("_pry_.binding_stack.size").should == 1 t.eval("_pry_.binding_stack.size").should == 1
end end
end end

View file

@ -140,9 +140,9 @@ describe "test Pry defaults" do
def get_prompts(pry) def get_prompts(pry)
a = pry.select_prompt a = pry.select_prompt
pry.accept_line "[" pry.eval "["
b = pry.select_prompt b = pry.select_prompt
pry.accept_line "]" pry.eval "]"
[a, b] [a, b]
end end