[ruby/irb] Group command test cases with class

(https://github.com/ruby/irb/pull/491)

Currently, IRB has 35 test cases for 10 non-debugging commands, with the
rest 10 commands untested. So in the long-term, it could have around 70
test cases for all existing commands.

With this number of test cases, I think it's easier to manage them by grouping
them in classes (by command).
This commit is contained in:
Stan Lo 2023-01-05 13:06:15 +00:00 committed by git
parent 4959e01932
commit f9148d1580

View File

@ -7,23 +7,6 @@ require_relative "helper"
module TestIRB module TestIRB
class CommandTestCase < TestCase class CommandTestCase < TestCase
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
capture_output do
irb.eval_input
end
end
end
class ExtendCommandTest < CommandTestCase
def setup def setup
@pwd = Dir.pwd @pwd = Dir.pwd
@tmpdir = File.join(Dir.tmpdir, "test_reline_config_#{$$}") @tmpdir = File.join(Dir.tmpdir, "test_reline_config_#{$$}")
@ -50,7 +33,40 @@ module TestIRB
restore_encodings restore_encodings
end end
class InfoCommandTest < ExtendCommandTest 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
capture_output do
irb.eval_input
end
end
end
class CommnadAliasTest < CommandTestCase
def test_vars_with_aliases
@foo = "foo"
$bar = "bar"
out, err = execute_lines(
"@foo\n",
"$bar\n",
)
assert_empty err
assert_match(/"foo"/, out)
assert_match(/"bar"/, out)
ensure
remove_instance_variable(:@foo)
$bar = nil
end
end
class InfoTest < CommandTestCase
def setup def setup
super super
@locals_backup = ENV.delete("LANG"), ENV.delete("LC_ALL") @locals_backup = ENV.delete("LANG"), ENV.delete("LC_ALL")
@ -192,6 +208,7 @@ module TestIRB
end end
end end
class MeasureTest < CommandTestCase
def test_measure def test_measure
conf = { conf = {
PROMPT: { PROMPT: {
@ -349,7 +366,9 @@ module TestIRB
assert_match(/\A=> 3\nBLOCK is added\.\n=> nil\naaa\n=> 3\nBLOCK is added.\naaa\n=> nil\nbbb\n=> 3\n=> nil\n=> 3\n/, out) assert_match(/\A=> 3\nBLOCK is added\.\n=> nil\naaa\n=> 3\nBLOCK is added.\naaa\n=> nil\nbbb\n=> 3\n=> nil\n=> 3\n/, out)
assert_empty(c.class_variables) assert_empty(c.class_variables)
end end
end
class IrbSourceTest < CommandTestCase
def test_irb_source def test_irb_source
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n") File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
out, err = execute_lines( out, err = execute_lines(
@ -375,7 +394,9 @@ module TestIRB
assert_empty err assert_empty err
assert_match(/Please specify the file name./, out) assert_match(/Please specify the file name./, out)
end end
end
class IrbLoadTest < CommandTestCase
def test_irb_load def test_irb_load
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n") File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
out, err = execute_lines( out, err = execute_lines(
@ -402,7 +423,92 @@ module TestIRB
assert_empty err assert_empty err
assert_match(/Please specify the file name./, out) assert_match(/Please specify the file name./, out)
end end
end
class ShowSourceTest < CommandTestCase
def test_show_source
out, err = execute_lines(
"show_source IRB.conf\n",
)
assert_empty err
assert_match(%r[/irb\.rb], out)
end
def test_show_source_method
out, err = execute_lines(
"p show_source('IRB.conf')\n",
)
assert_empty err
assert_match(%r[/irb\.rb], out)
end
def test_show_source_string
out, err = execute_lines(
"show_source 'IRB.conf'\n",
)
assert_empty err
assert_match(%r[/irb\.rb], out)
end
def test_show_source_alias
out, err = execute_lines(
"$ 'IRB.conf'\n",
conf: { COMMAND_ALIASES: { :'$' => :show_source } }
)
assert_empty err
assert_match(%r[/irb\.rb], out)
end
def test_show_source_end_finder
pend if RUBY_ENGINE == 'truffleruby'
eval(code = <<-EOS, binding, __FILE__, __LINE__ + 1)
def show_source_test_method
unless true
end
end unless defined?(show_source_test_method)
EOS
out, err = execute_lines(
"show_source '#{self.class.name}#show_source_test_method'\n",
)
assert_empty err
assert_include(out, code)
end
end
class WhereamiTest < CommandTestCase
def test_whereami
out, err = execute_lines(
"whereami\n",
)
assert_empty err
assert_match(/^From: .+ @ line \d+ :\n/, out)
end
def test_whereami_alias
out, err = execute_lines(
"@\n",
)
assert_empty err
assert_match(/^From: .+ @ line \d+ :\n/, out)
end
end
class ShowCmdsTest < CommandTestCase
def test_show_cmds
out, err = execute_lines(
"show_cmds\n"
)
assert_empty err
assert_match(/List all available commands and their description/, out)
assert_match(/Start the debugger of debug\.gem/, out)
end
end
class LsTest < CommandTestCase
def test_ls def test_ls
out, err = execute_lines( out, err = execute_lines(
"class P\n", "class P\n",
@ -488,96 +594,6 @@ module TestIRB
assert_match(/Numeric#methods:\s+/, out) assert_match(/Numeric#methods:\s+/, out)
assert_match(/Integer#methods:\s+/, out) assert_match(/Integer#methods:\s+/, out)
end end
def test_show_source
out, err = execute_lines(
"show_source IRB.conf\n",
)
assert_empty err
assert_match(%r[/irb\.rb], out)
end
def test_show_source_method
out, err = execute_lines(
"p show_source('IRB.conf')\n",
)
assert_empty err
assert_match(%r[/irb\.rb], out)
end
def test_show_source_string
out, err = execute_lines(
"show_source 'IRB.conf'\n",
)
assert_empty err
assert_match(%r[/irb\.rb], out)
end
def test_show_source_alias
out, err = execute_lines(
"$ 'IRB.conf'\n",
conf: { COMMAND_ALIASES: { :'$' => :show_source } }
)
assert_empty err
assert_match(%r[/irb\.rb], out)
end
def test_show_source_end_finder
pend if RUBY_ENGINE == 'truffleruby'
eval(code = <<-EOS, binding, __FILE__, __LINE__ + 1)
def show_source_test_method
unless true
end
end unless defined?(show_source_test_method)
EOS
out, err = execute_lines(
"show_source 'TestIRB::ExtendCommandTest#show_source_test_method'\n",
)
assert_empty err
assert_include(out, code)
end
def test_whereami
out, err = execute_lines(
"whereami\n",
)
assert_empty err
assert_match(/^From: .+ @ line \d+ :\n/, out)
end
def test_whereami_alias
out, err = execute_lines(
"@\n",
)
assert_empty err
assert_match(/^From: .+ @ line \d+ :\n/, out)
end
def test_vars_with_aliases
@foo = "foo"
$bar = "bar"
out, err = execute_lines(
"@foo\n",
"$bar\n",
)
assert_empty err
assert_match(/"foo"/, out)
assert_match(/"bar"/, out)
ensure
remove_instance_variable(:@foo)
$bar = nil
end
def test_show_cmds
out, err = execute_lines(
"show_cmds\n"
)
assert_empty err
assert_match(/List all available commands and their description/, out)
assert_match(/Start the debugger of debug\.gem/, out)
end end
class ShowDocTest < CommandTestCase class ShowDocTest < CommandTestCase
@ -688,5 +704,4 @@ module TestIRB
assert_match("command: ': code'", out) assert_match("command: ': code'", out)
end end
end end
end
end end