summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2022-11-03 15:09:51 -0700
committergit <svn-admin@ruby-lang.org>2022-11-03 22:09:55 +0000
commita13836e70d9cc2eb569911030cbd735d68b4042c (patch)
tree4ebb9e319a6416d51152f80c71083a47aaf84234 /test
parentd24ac6d2811e461562e3bb95525399bb9cf464f8 (diff)
[ruby/irb] Allow non-identifier aliases like Pry's @ and $
(https://github.com/ruby/irb/pull/426) * Allow non-identifier aliases * Move the configuration to IRB.conf * Avoid abusing method lookup for symbol aliases * Add more alias tests * A small optimization * Assume non-nil Context * Load IRB.conf earlier https://github.com/ruby/irb/commit/e23db5132e
Diffstat (limited to 'test')
-rw-r--r--test/irb/test_cmd.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb
index 060f70c9cc..034d825bbc 100644
--- a/test/irb/test_cmd.rb
+++ b/test/irb/test_cmd.rb
@@ -570,6 +570,24 @@ module TestIRB
assert_match(%r[/irb\.rb], out)
end
+ def test_show_source_alias
+ input = TestInputMethod.new([
+ "$ 'IRB.conf'\n",
+ ])
+ IRB.init_config(nil)
+ IRB.conf[:COMMAND_ALIASES] = { :'$' => :show_source }
+ workspace = IRB::WorkSpace.new(Object.new)
+ IRB.conf[:VERBOSE] = false
+ irb = IRB::Irb.new(workspace, input)
+ IRB.conf[:MAIN_CONTEXT] = irb.context
+ irb.context.return_format = "=> %s\n"
+ out, err = capture_output do
+ irb.eval_input
+ end
+ 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)
@@ -610,5 +628,45 @@ module TestIRB
assert_empty err
assert_match(/^From: .+ @ line \d+ :\n/, out)
end
+
+ def test_whereami_alias
+ input = TestInputMethod.new([
+ "@\n",
+ ])
+ IRB.init_config(nil)
+ IRB.conf[:COMMAND_ALIASES] = { :'@' => :whereami }
+ workspace = IRB::WorkSpace.new(Object.new)
+ irb = IRB::Irb.new(workspace, input)
+ IRB.conf[:MAIN_CONTEXT] = irb.context
+ out, err = capture_output do
+ irb.eval_input
+ end
+ assert_empty err
+ assert_match(/^From: .+ @ line \d+ :\n/, out)
+ end
+
+ def test_vars_with_aliases
+ input = TestInputMethod.new([
+ "@foo\n",
+ "$bar\n",
+ ])
+ IRB.init_config(nil)
+ IRB.conf[:COMMAND_ALIASES] = {
+ :'@' => :whereami,
+ :'$' => :show_source,
+ }
+ main = Object.new
+ main.instance_variable_set(:@foo, "foo")
+ $bar = "bar"
+ workspace = IRB::WorkSpace.new(main)
+ irb = IRB::Irb.new(workspace, input)
+ IRB.conf[:MAIN_CONTEXT] = irb.context
+ out, err = capture_output do
+ irb.eval_input
+ end
+ assert_empty err
+ assert_match(/"foo"/, out)
+ assert_match(/"bar"/, out)
+ end
end
end