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

Fix tester for nested-instance case

This commit is contained in:
Ryan Fitzgerald 2012-12-23 19:58:18 -08:00
parent 988b27c071
commit 4b5d47cd60
2 changed files with 32 additions and 10 deletions

View file

@ -1,6 +1,7 @@
# This is for super-high-level integration testing.
require 'thread'
require 'delegate'
class ReplTester
class Input
@ -9,24 +10,28 @@ class ReplTester
end
def readline(prompt)
awaken_tester
@tester_mailbox.push prompt
mailbox.pop
end
def mailbox
Thread.current[:mailbox]
end
end
def awaken_tester
@tester_mailbox.push nil
class Output < SimpleDelegator
def clear
__setobj__(StringIO.new)
end
end
def self.start(options = {}, &block)
Thread.current[:mailbox] = Queue.new
instance = nil
input = Input.new(Thread.current[:mailbox])
output = Output.new(StringIO.new)
redirect_pry_io Input.new(Thread.current[:mailbox]), StringIO.new do
redirect_pry_io input, output do
instance = new(options)
instance.instance_eval(&block)
instance.ensure_exit
@ -37,7 +42,7 @@ class ReplTester
end
end
attr_accessor :thread, :mailbox
attr_accessor :thread, :mailbox, :last_prompt
def initialize(options = {})
@pry = Pry.new(options)
@ -61,12 +66,12 @@ class ReplTester
def input(input)
reset_output
repl_mailbox.push input
mailbox.pop # wait until the instance either calls readline or ends
self.last_prompt = mailbox.pop
end
# Assert that the current prompt matches the given string or regex.
def prompt(match)
match.should === @pry.select_prompt
match.should === last_prompt
end
# Assert that the most recent output (since the last time input was called)
@ -93,7 +98,7 @@ class ReplTester
private
def reset_output
@pry.output = Pry.output = StringIO.new
Pry.output.clear
end
def repl_mailbox

View file

@ -14,7 +14,7 @@ describe "The REPL" do
end
describe "eval_string and binding_stack" do
it "shouldn't break if we start a nested session" do
it "shouldn't break if we start a nested REPL" do
ReplTester.start do
input 'Pry::REPL.new(_pry_, :target => 10).start'
output ''
@ -31,6 +31,23 @@ describe "The REPL" do
end
end
it "shouldn't break if we start a nested instance" do
ReplTester.start do
input 'Pry.start(10)'
output ''
prompt /10.*> $/
input 'self'
output '=> 10'
input nil # Ctrl-D
output '=> nil' # return value of Pry session
input 'self'
output '=> main'
end
end
it "shouldn't break if we pop bindings in Ruby" do
ReplTester.start do
input 'cd 10'
@ -59,7 +76,7 @@ describe "The REPL" do
ReplTester.start(:commands => set) do
input 'def x'
output ''
prompt /\* $/
prompt /\* $/
input 'hello!'
output '=> "hello"'