From 68002e75b62c3a59f0d722daa7ec42771208162a Mon Sep 17 00:00:00 2001 From: tompng Date: Sun, 18 Feb 2024 02:46:02 +0900 Subject: [PATCH] Add test for ExtendCommandBundle.def_extend_command --- test/irb/test_command.rb | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/test/irb/test_command.rb b/test/irb/test_command.rb index 8eed27a29..444c9c265 100644 --- a/test/irb/test_command.rb +++ b/test/irb/test_command.rb @@ -209,6 +209,56 @@ def test_irb_info_lang end end + class ExtendCommandBundleCompatibilityTest < CommandTestCase + class FooBarCommand < IRB::Command::Base + category 'FooBarCategory' + description 'foobar_description' + def execute(_arg) + puts "FooBar executed" + end + end + + def setup + super + execute_lines("show_cmds\n") # To ensure command initialization is done + @ivars_backup = IRB::ExtendCommandBundle.instance_variables.to_h do |ivar| + [ivar, IRB::ExtendCommandBundle.instance_variable_get(ivar)] + end + @cvars_backup = IRB::ExtendCommandBundle.class_variables.to_h do |cvar| + [cvar, IRB::ExtendCommandBundle.class_variable_get(cvar)] + end + IRB::Command.const_set :FooBarCommand, FooBarCommand + end + + def teardown + super + @ivars_backup.each do |ivar, value| + IRB::ExtendCommandBundle.instance_variable_set(ivar, value) + end + @cvars_backup.each do |cvar, value| + IRB::ExtendCommandBundle.class_variable_set(cvar, value) + end + IRB::Command.send(:remove_const, :FooBarCommand) + end + + def test_def_extend_command + command = [:foobar, :FooBarCommand, nil, [:fbalias, IRB::ExtendCommandBundle::OVERRIDE_ALL]] + IRB::ExtendCommandBundle.instance_variable_get(:@EXTEND_COMMANDS).push(command) + IRB::ExtendCommandBundle.def_extend_command(*command) + out, err = execute_lines("foobar\n") + assert_empty err + assert_include(out, "FooBar executed") + + out, err = execute_lines("fbalias\n") + assert_empty err + assert_include(out, "FooBar executed") + + out, err = execute_lines("show_cmds\n") + assert_include(out, "FooBarCategory") + assert_include(out, "foobar_description") + end + end + class MeasureTest < CommandTestCase def test_measure conf = {