2018-04-14 08:49:30 -04:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require 'test/unit'
|
|
|
|
require 'tempfile'
|
|
|
|
require 'irb'
|
|
|
|
require 'rubygems' if defined?(Gem)
|
|
|
|
|
|
|
|
module TestIRB
|
|
|
|
class TestContext < Test::Unit::TestCase
|
|
|
|
class TestInputMethod < ::IRB::InputMethod
|
2018-05-12 05:09:23 -04:00
|
|
|
attr_reader :list, :line_no
|
2018-04-14 08:49:30 -04:00
|
|
|
|
|
|
|
def initialize(list = [])
|
|
|
|
super("test")
|
|
|
|
@line_no = 0
|
2018-05-12 05:09:23 -04:00
|
|
|
@list = list
|
2018-04-14 08:49:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def gets
|
2018-05-12 05:09:23 -04:00
|
|
|
@list[@line_no]&.tap {@line_no += 1}
|
2018-04-14 08:49:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def eof?
|
|
|
|
@line_no >= @list.size
|
|
|
|
end
|
2018-05-12 05:13:45 -04:00
|
|
|
|
|
|
|
def encoding
|
|
|
|
Encoding.default_external
|
|
|
|
end
|
2018-04-14 08:49:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
IRB.init_config(nil)
|
|
|
|
IRB.conf[:USE_READLINE] = false
|
|
|
|
IRB.conf[:VERBOSE] = false
|
|
|
|
workspace = IRB::WorkSpace.new(Object.new)
|
|
|
|
@context = IRB::Context.new(nil, workspace, TestInputMethod.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_last_value
|
|
|
|
assert_nil(@context.last_value)
|
|
|
|
assert_nil(@context.evaluate('_', 1))
|
|
|
|
obj = Object.new
|
|
|
|
@context.set_last_value(obj)
|
|
|
|
assert_same(obj, @context.last_value)
|
|
|
|
assert_same(obj, @context.evaluate('_', 1))
|
|
|
|
end
|
2018-04-14 09:05:52 -04:00
|
|
|
|
|
|
|
def test_evaluate_with_exception
|
|
|
|
assert_nil(@context.evaluate("$!", 1))
|
|
|
|
e = assert_raise_with_message(RuntimeError, 'foo') {
|
|
|
|
@context.evaluate("raise 'foo'", 1)
|
|
|
|
}
|
|
|
|
assert_equal('foo', e.message)
|
|
|
|
assert_same(e, @context.evaluate('$!', 1, exception: e))
|
|
|
|
end
|
2018-05-12 05:13:45 -04:00
|
|
|
|
|
|
|
def test_eval_input
|
|
|
|
input = TestInputMethod.new([
|
|
|
|
"raise 'Foo'\n",
|
|
|
|
"_\n",
|
|
|
|
"0\n",
|
|
|
|
"_\n",
|
|
|
|
])
|
|
|
|
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
|
|
|
|
out, err = capture_io do
|
|
|
|
irb.eval_input
|
|
|
|
end
|
|
|
|
assert_empty err
|
|
|
|
assert_pattern_list([:*, /RuntimeError \(.*Foo.*\).*\n/,
|
|
|
|
:*, /#<RuntimeError: Foo>\n/,
|
|
|
|
:*, /0$/,
|
|
|
|
:*, /0$/,
|
|
|
|
/\s*/], out)
|
|
|
|
end
|
2018-04-14 08:49:30 -04:00
|
|
|
end
|
|
|
|
end
|