1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
# frozen_string_literal: true
require "irb"
require_relative "helper"
module TestIRB
class HelperMethodTestCase < TestCase
def setup
$VERBOSE = nil
@verbosity = $VERBOSE
save_encodings
IRB.instance_variable_get(:@CONF).clear
end
def teardown
$VERBOSE = @verbosity
restore_encodings
end
def execute_lines(*lines, conf: {}, main: self, irb_path: nil)
IRB.init_config(nil)
IRB.conf[:VERBOSE] = false
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf.merge!(conf)
input = TestInputMethod.new(lines)
irb = IRB::Irb.new(IRB::WorkSpace.new(main), input)
irb.context.return_format = "=> %s\n"
irb.context.irb_path = irb_path if irb_path
IRB.conf[:MAIN_CONTEXT] = irb.context
IRB.conf[:USE_PAGER] = false
capture_output do
irb.eval_input
end
end
end
module TestHelperMethod
class ConfTest < HelperMethodTestCase
def test_conf_returns_the_context_object
out, err = execute_lines("conf.ap_name")
assert_empty err
assert_include out, "=> \"irb\""
end
end
end
class HelperMethodIntegrationTest < IntegrationTestCase
def test_arguments_propogation
write_ruby <<~RUBY
require "irb/helper_method"
class MyHelper < IRB::HelperMethod::Base
description "This is a test helper"
def execute(
required_arg, optional_arg = nil, *splat_arg, required_keyword_arg:,
optional_keyword_arg: nil, **double_splat_arg, &block_arg
)
puts [required_arg, optional_arg, splat_arg, required_keyword_arg, optional_keyword_arg, double_splat_arg, block_arg.call].to_s
end
end
IRB::HelperMethod.register(:my_helper, MyHelper)
binding.irb
RUBY
output = run_ruby_file do
type <<~INPUT
my_helper(
"required", "optional", "splat", required_keyword_arg: "required",
optional_keyword_arg: "optional", a: 1, b: 2
) { "block" }
INPUT
type "exit"
end
optional = {a: 1, b: 2}
assert_include(output, %[["required", "optional", ["splat"], "required", "optional", #{optional.inspect}, "block"]])
end
def test_helper_method_injection_can_happen_after_irb_require
write_ruby <<~RUBY
require "irb"
class MyHelper < IRB::HelperMethod::Base
description "This is a test helper"
def execute
puts "Hello from MyHelper"
end
end
IRB::HelperMethod.register(:my_helper, MyHelper)
binding.irb
RUBY
output = run_ruby_file do
type "my_helper"
type "exit"
end
assert_include(output, 'Hello from MyHelper')
end
def test_helper_method_instances_are_memoized
write_ruby <<~RUBY
require "irb/helper_method"
class MyHelper < IRB::HelperMethod::Base
description "This is a test helper"
def execute(val)
@val ||= val
end
end
IRB::HelperMethod.register(:my_helper, MyHelper)
binding.irb
RUBY
output = run_ruby_file do
type "my_helper(100)"
type "my_helper(200)"
type "exit"
end
assert_include(output, '=> 100')
assert_not_include(output, '=> 200')
end
end
end
|