2012-12-23 00:03:29 -08:00
|
|
|
# This is for super-high-level integration testing.
|
|
|
|
|
2012-12-23 15:48:20 -08:00
|
|
|
require 'thread'
|
2012-12-23 19:58:18 -08:00
|
|
|
require 'delegate'
|
2012-12-23 00:03:29 -08:00
|
|
|
|
|
|
|
class ReplTester
|
|
|
|
class Input
|
2012-12-23 15:48:20 -08:00
|
|
|
def initialize(tester_mailbox)
|
|
|
|
@tester_mailbox = tester_mailbox
|
|
|
|
end
|
|
|
|
|
2012-12-23 00:03:29 -08:00
|
|
|
def readline(prompt)
|
2012-12-23 19:58:18 -08:00
|
|
|
@tester_mailbox.push prompt
|
2012-12-23 15:48:20 -08:00
|
|
|
mailbox.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
def mailbox
|
|
|
|
Thread.current[:mailbox]
|
|
|
|
end
|
2012-12-23 19:58:18 -08:00
|
|
|
end
|
2012-12-23 15:48:20 -08:00
|
|
|
|
2012-12-23 19:58:18 -08:00
|
|
|
class Output < SimpleDelegator
|
|
|
|
def clear
|
|
|
|
__setobj__(StringIO.new)
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-23 15:21:58 -08:00
|
|
|
def self.start(options = {}, &block)
|
2012-12-23 15:48:20 -08:00
|
|
|
Thread.current[:mailbox] = Queue.new
|
|
|
|
instance = nil
|
2012-12-23 19:58:18 -08:00
|
|
|
input = Input.new(Thread.current[:mailbox])
|
|
|
|
output = Output.new(StringIO.new)
|
2012-12-23 15:48:20 -08:00
|
|
|
|
2012-12-23 19:58:18 -08:00
|
|
|
redirect_pry_io input, output do
|
2012-12-23 15:21:58 -08:00
|
|
|
instance = new(options)
|
2012-12-23 12:25:48 -08:00
|
|
|
instance.instance_eval(&block)
|
|
|
|
instance.ensure_exit
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|
2012-12-23 15:48:20 -08:00
|
|
|
ensure
|
|
|
|
if instance && instance.thread && instance.thread.alive?
|
|
|
|
instance.thread.kill
|
|
|
|
end
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|
|
|
|
|
2012-12-23 19:58:18 -08:00
|
|
|
attr_accessor :thread, :mailbox, :last_prompt
|
2012-12-23 00:03:29 -08:00
|
|
|
|
2012-12-23 15:21:58 -08:00
|
|
|
def initialize(options = {})
|
2012-12-23 15:48:20 -08:00
|
|
|
@pry = Pry.new(options)
|
|
|
|
@repl = Pry::REPL.new(@pry)
|
|
|
|
@mailbox = Thread.current[:mailbox]
|
2012-12-23 00:03:29 -08:00
|
|
|
|
2012-12-23 15:48:20 -08:00
|
|
|
@thread = Thread.new do
|
|
|
|
begin
|
|
|
|
Thread.current[:mailbox] = Queue.new
|
|
|
|
@repl.start
|
|
|
|
ensure
|
|
|
|
Thread.current[:session_ended] = true
|
|
|
|
mailbox.push nil
|
|
|
|
end
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|
|
|
|
|
2015-01-22 22:52:20 +01:00
|
|
|
@should_exit_naturally = false
|
|
|
|
|
2012-12-23 20:05:37 -08:00
|
|
|
wait # wait until the instance reaches its first readline
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|
|
|
|
|
2012-12-23 15:48:20 -08:00
|
|
|
# Accept a line of input, as if entered by a user.
|
2012-12-23 12:25:48 -08:00
|
|
|
def input(input)
|
2012-12-23 15:48:20 -08:00
|
|
|
reset_output
|
|
|
|
repl_mailbox.push input
|
2012-12-23 20:05:37 -08:00
|
|
|
wait
|
2014-01-28 22:42:41 +09:00
|
|
|
@pry.output.string
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|
|
|
|
|
2012-12-23 15:48:20 -08:00
|
|
|
# Assert that the current prompt matches the given string or regex.
|
2012-12-23 00:03:29 -08:00
|
|
|
def prompt(match)
|
2012-12-23 19:58:18 -08:00
|
|
|
match.should === last_prompt
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|
|
|
|
|
2012-12-23 15:48:20 -08:00
|
|
|
# Assert that the most recent output (since the last time input was called)
|
|
|
|
# matches the given string or regex.
|
2012-12-23 12:25:48 -08:00
|
|
|
def output(match)
|
2014-01-28 22:42:41 +09:00
|
|
|
match.should === @pry.output.string.chomp
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|
2012-12-23 12:25:48 -08:00
|
|
|
|
2012-12-23 15:48:20 -08:00
|
|
|
# Assert that the Pry session ended naturally after the last input.
|
|
|
|
def assert_exited
|
|
|
|
@should_exit_naturally = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# @private
|
2012-12-23 12:25:48 -08:00
|
|
|
def ensure_exit
|
|
|
|
if @should_exit_naturally
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 16:26:47 -06:00
|
|
|
raise "Session was not ended!" unless @thread[:session_ended].equal?(true)
|
2012-12-23 12:25:48 -08:00
|
|
|
else
|
|
|
|
input "exit-all"
|
2012-12-23 15:48:20 -08:00
|
|
|
raise "REPL didn't die" unless @thread[:session_ended]
|
2012-12-23 12:25:48 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-23 15:48:20 -08:00
|
|
|
private
|
|
|
|
|
|
|
|
def reset_output
|
2014-01-28 22:42:41 +09:00
|
|
|
@pry.output.clear
|
2012-12-23 15:48:20 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def repl_mailbox
|
|
|
|
@thread[:mailbox]
|
2012-12-23 12:25:48 -08:00
|
|
|
end
|
2012-12-23 20:05:37 -08:00
|
|
|
|
|
|
|
def wait
|
|
|
|
@last_prompt = mailbox.pop
|
|
|
|
end
|
2012-12-23 00:03:29 -08:00
|
|
|
end
|