mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Update most of test_context
This commit is contained in:
parent
544ae03620
commit
57e7556722
2 changed files with 51 additions and 68 deletions
|
@ -203,16 +203,16 @@ ensure
|
||||||
file.close(true) if file
|
file.close(true) if file
|
||||||
end
|
end
|
||||||
|
|
||||||
def pry_tester(context = nil, &block)
|
def pry_tester(context = TOPLEVEL_BINDING, &block)
|
||||||
PryTester.new(context).tap do |t|
|
PryTester.new(context).tap do |t|
|
||||||
(class << t; self; end).class_eval(&block)
|
(class << t; self; end).class_eval(&block) if block
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class PryTester
|
class PryTester
|
||||||
attr_reader :pry
|
attr_reader :pry, :out
|
||||||
|
|
||||||
def initialize(context)
|
def initialize(context = TOPLEVEL_BINDING, *cmds)
|
||||||
@pry = Pry.new
|
@pry = Pry.new
|
||||||
|
|
||||||
if context
|
if context
|
||||||
|
@ -221,14 +221,33 @@ class PryTester
|
||||||
end
|
end
|
||||||
|
|
||||||
def eval(*strs)
|
def eval(*strs)
|
||||||
strs.flatten.map do |str|
|
@out = StringIO.new
|
||||||
|
@pry.output = @out
|
||||||
|
result = nil
|
||||||
|
|
||||||
|
strs.flatten.each do |str|
|
||||||
if @pry.process_command(str)
|
if @pry.process_command(str)
|
||||||
result = Thread.current[:__pry_cmd_result__]
|
result = last_command_result
|
||||||
result.retval if result
|
if result == Pry::Command::VOID_VALUE
|
||||||
|
result = last_output
|
||||||
|
end
|
||||||
else
|
else
|
||||||
@pry.evaluate_ruby(str)
|
result = @pry.evaluate_ruby(str)
|
||||||
end
|
end
|
||||||
end.last
|
end
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
def last_output
|
||||||
|
@out.string if @out
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def last_command_result
|
||||||
|
result = Thread.current[:__pry_cmd_result__]
|
||||||
|
result.retval if result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,7 @@
|
||||||
require 'helper'
|
require 'helper'
|
||||||
|
|
||||||
module ContextTestHelpers
|
|
||||||
def pry
|
|
||||||
@pry ||= Pry.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def process_commands(*args)
|
|
||||||
args.flatten.each do |cmd|
|
|
||||||
pry.process_command cmd
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def evaluate_ruby(ruby)
|
|
||||||
pry.evaluate_ruby ruby
|
|
||||||
end
|
|
||||||
|
|
||||||
def evaluate_self
|
|
||||||
pry.evaluate_ruby 'self'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "Pry::DefaultCommands::Context" do
|
describe "Pry::DefaultCommands::Context" do
|
||||||
before do
|
before do
|
||||||
extend ContextTestHelpers
|
|
||||||
|
|
||||||
@self = "Pad.self = self"
|
@self = "Pad.self = self"
|
||||||
@inner = "Pad.inner = self"
|
@inner = "Pad.inner = self"
|
||||||
@outer = "Pad.outer = self"
|
@outer = "Pad.outer = self"
|
||||||
|
@ -74,18 +52,16 @@ describe "Pry::DefaultCommands::Context" do
|
||||||
|
|
||||||
Cor.new.blimey!
|
Cor.new.blimey!
|
||||||
|
|
||||||
pry.binding_stack << Pad.binding
|
|
||||||
process_commands 'whereami'
|
|
||||||
|
|
||||||
# using [.] so the regex doesn't match itself
|
# using [.] so the regex doesn't match itself
|
||||||
# mock_pry(binding, 'whereami').should =~ /self[.]blimey!/
|
pry_tester(Pad.binding).eval('whereami').should =~ /self[.]blimey!/
|
||||||
|
|
||||||
Object.remove_const(:Cor)
|
Object.remove_const(:Cor)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should work in objects with no method methods' do
|
it 'should work in objects with no method methods' do
|
||||||
class Cor
|
class Cor
|
||||||
def blimey!
|
def blimey!
|
||||||
mock_pry(binding, 'whereami').should =~ /Cor[#]blimey!/
|
pry_tester(binding).eval('whereami').should =~ /Cor[#]blimey!/
|
||||||
end
|
end
|
||||||
|
|
||||||
def method; "moo"; end
|
def method; "moo"; end
|
||||||
|
@ -97,8 +73,9 @@ describe "Pry::DefaultCommands::Context" do
|
||||||
it 'should properly set _file_, _line_ and _dir_' do
|
it 'should properly set _file_, _line_ and _dir_' do
|
||||||
class Cor
|
class Cor
|
||||||
def blimey!
|
def blimey!
|
||||||
mock_pry(binding, 'whereami', '_file_') \
|
pry_tester(binding).
|
||||||
.should =~ /#{File.expand_path(__FILE__)}/
|
eval('whereami', '_file_').
|
||||||
|
should == File.expand_path(__FILE__)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -110,12 +87,12 @@ describe "Pry::DefaultCommands::Context" do
|
||||||
class Cor
|
class Cor
|
||||||
def blimey!
|
def blimey!
|
||||||
eval <<-END, binding, "test/test_default_commands/example.erb", 1
|
eval <<-END, binding, "test/test_default_commands/example.erb", 1
|
||||||
mock_pry(binding, 'whereami')
|
pry_tester(binding).eval('whereami')
|
||||||
END
|
END
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Cor.instance_method(:blimey!).source.should =~ /mock_pry/
|
Cor.instance_method(:blimey!).source.should =~ /pry_tester/
|
||||||
|
|
||||||
Cor.new.blimey!.should =~ /Cor#blimey!.*Look at me/m
|
Cor.new.blimey!.should =~ /Cor#blimey!.*Look at me/m
|
||||||
Object.remove_const(:Cor)
|
Object.remove_const(:Cor)
|
||||||
|
@ -125,7 +102,7 @@ describe "Pry::DefaultCommands::Context" do
|
||||||
class Cor
|
class Cor
|
||||||
eval <<-END, binding, "test/test_default_commands/example.erb", 1
|
eval <<-END, binding, "test/test_default_commands/example.erb", 1
|
||||||
def blimey!
|
def blimey!
|
||||||
mock_pry(binding, 'whereami')
|
pry_tester(binding).eval('whereami')
|
||||||
end
|
end
|
||||||
END
|
END
|
||||||
end
|
end
|
||||||
|
@ -136,26 +113,28 @@ describe "Pry::DefaultCommands::Context" do
|
||||||
|
|
||||||
Cor.new.blimey!.should =~ /Cor#blimey!.*Look at me/m
|
Cor.new.blimey!.should =~ /Cor#blimey!.*Look at me/m
|
||||||
Object.remove_const(:Cor)
|
Object.remove_const(:Cor)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should display a description and and error if reading the file goes wrong' do
|
it 'should display a description and and error if reading the file goes wrong' do
|
||||||
class Cor
|
class Cor
|
||||||
def blimey!
|
def blimey!
|
||||||
eval <<-END, binding, "not.found.file.erb", 7
|
eval <<-END, binding, "not.found.file.erb", 7
|
||||||
mock_pry(binding, 'whereami')
|
Pad.tester = pry_tester(binding)
|
||||||
|
Pad.tester.eval('whereami')
|
||||||
END
|
END
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Cor.new.blimey!.should =~ /From: not.found.file.erb @ line 7 Cor#blimey!:\n\nError: Cannot open "not.found.file.erb" for reading./m
|
proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
|
||||||
|
Pad.tester.last_output.should =~
|
||||||
|
/From: not.found.file.erb @ line 7 Cor#blimey!:/
|
||||||
Object.remove_const(:Cor)
|
Object.remove_const(:Cor)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should show code window (not just method source) if parameter passed to whereami' do
|
it 'should show code window (not just method source) if parameter passed to whereami' do
|
||||||
class Cor
|
class Cor
|
||||||
def blimey!
|
def blimey!
|
||||||
mock_pry(binding, 'whereami 3').should =~ /class Cor/
|
pry_tester(binding).eval('whereami 3').should =~ /class Cor/
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Cor.new.blimey!
|
Cor.new.blimey!
|
||||||
|
@ -166,7 +145,7 @@ describe "Pry::DefaultCommands::Context" do
|
||||||
old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1
|
old_size, Pry.config.default_window_size = Pry.config.default_window_size, 1
|
||||||
:litella
|
:litella
|
||||||
:pig
|
:pig
|
||||||
out = mock_pry(binding, 'whereami')
|
out = pry_tester(binding).eval('whereami')
|
||||||
:punk
|
:punk
|
||||||
:sanders
|
:sanders
|
||||||
|
|
||||||
|
@ -179,15 +158,16 @@ describe "Pry::DefaultCommands::Context" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should work at the top level" do
|
it "should work at the top level" do
|
||||||
mock_pry(Pry.toplevel_binding, 'whereami').should =~ /At the top level/
|
pry_tester(Pry.toplevel_binding).eval('whereami').should =~
|
||||||
|
/At the top level/
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should work inside a class" do
|
it "should work inside a class" do
|
||||||
mock_pry(Pry.binding_for(Pry), 'whereami').should =~ /Inside Pry/
|
pry_tester(Pry).eval('whereami').should =~ /Inside Pry/
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should work inside an object" do
|
it "should work inside an object" do
|
||||||
mock_pry(Pry.binding_for(Object.new), 'whereami').should =~ /Inside #<Object/
|
pry_tester(Object.new).eval('whereami').should =~ /Inside #<Object/
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -218,32 +198,16 @@ describe "Pry::DefaultCommands::Context" do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "jump-to" do
|
describe "jump-to" do
|
||||||
before do
|
|
||||||
@str_output = StringIO.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should jump to the proper binding index in the stack' do
|
it 'should jump to the proper binding index in the stack' do
|
||||||
redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 1", @self, "exit-all")) do
|
pry_tester.eval('cd 1', 'cd 2', 'jump-to 1', 'self').should == 1
|
||||||
Pry.start(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
Pad.self.should == 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should print error when trying to jump to a non-existent binding index' do
|
it 'should print error when trying to jump to a non-existent binding index' do
|
||||||
redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 100", "exit-all"), @str_output) do
|
pry_tester.eval("cd 1", "cd 2", "jump-to 100").should =~ /Invalid nest level/
|
||||||
Pry.start(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
@str_output.string.should =~ /Invalid nest level/
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should print error when trying to jump to the same binding index' do
|
it 'should print error when trying to jump to the same binding index' do
|
||||||
redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 2", "exit-all"), @str_output) do
|
pry_tester.eval("cd 1", "cd 2", "jump-to 2").should =~ /Already/
|
||||||
Pry.new.repl(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
@str_output.string.should =~ /Already/
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue