Skip to content

Commit 25a8b76

Browse files
st0012matzbot
authored andcommitted
[ruby/irb] Command registration should take both strings and symbols
as names (ruby/irb#932) This will save users some heads scratching when they try to register a command with a string name and found that it doesn't work. I also rewrote converted custom command tests into integration tests to make test setup/cleanup easier. ruby/irb@a91a212dbe
1 parent 140c59c commit 25a8b76

File tree

4 files changed

+129
-72
lines changed

4 files changed

+129
-72
lines changed

lib/irb/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class << self
1616
# Registers a command with the given name.
1717
# Aliasing is intentionally not supported at the moment.
1818
def register(name, command_class)
19-
@commands[name] = [command_class, []]
19+
@commands[name.to_sym] = [command_class, []]
2020
end
2121
end
2222
end

lib/irb/default_commands.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class << self
3939
# This API is for IRB's internal use only and may change at any time.
4040
# Please do NOT use it.
4141
def _register_with_aliases(name, command_class, *aliases)
42-
@commands[name] = [command_class, aliases]
42+
@commands[name.to_sym] = [command_class, aliases]
4343
end
4444

4545
def all_commands_info
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# frozen_string_literal: true
2+
require "irb"
3+
4+
require_relative "../helper"
5+
6+
module TestIRB
7+
class CustomCommandIntegrationTest < TestIRB::IntegrationTestCase
8+
def test_command_regsitration_can_happen_after_irb_require
9+
write_ruby <<~RUBY
10+
require "irb"
11+
require "irb/command"
12+
13+
class PrintCommand < IRB::Command::Base
14+
category 'CommandTest'
15+
description 'print_command'
16+
def execute(*)
17+
puts "Hello from PrintCommand"
18+
nil
19+
end
20+
end
21+
22+
IRB::Command.register(:print!, PrintCommand)
23+
24+
binding.irb
25+
RUBY
26+
27+
output = run_ruby_file do
28+
type "print!\n"
29+
type "exit"
30+
end
31+
32+
assert_include(output, "Hello from PrintCommand")
33+
end
34+
35+
def test_command_regsitration_accepts_string_too
36+
write_ruby <<~RUBY
37+
require "irb/command"
38+
39+
class PrintCommand < IRB::Command::Base
40+
category 'CommandTest'
41+
description 'print_command'
42+
def execute(*)
43+
puts "Hello from PrintCommand"
44+
nil
45+
end
46+
end
47+
48+
IRB::Command.register("print!", PrintCommand)
49+
50+
binding.irb
51+
RUBY
52+
53+
output = run_ruby_file do
54+
type "print!\n"
55+
type "exit"
56+
end
57+
58+
assert_include(output, "Hello from PrintCommand")
59+
end
60+
61+
def test_arguments_propogation
62+
write_ruby <<~RUBY
63+
require "irb/command"
64+
65+
class PrintArgCommand < IRB::Command::Base
66+
category 'CommandTest'
67+
description 'print_command_arg'
68+
def execute(arg)
69+
$nth_execution ||= 0
70+
puts "\#{$nth_execution} arg=\#{arg.inspect}"
71+
$nth_execution += 1
72+
nil
73+
end
74+
end
75+
76+
IRB::Command.register(:print_arg, PrintArgCommand)
77+
78+
binding.irb
79+
RUBY
80+
81+
output = run_ruby_file do
82+
type "print_arg\n"
83+
type "print_arg \n"
84+
type "print_arg a r g\n"
85+
type "print_arg a r g \n"
86+
type "exit"
87+
end
88+
89+
assert_include(output, "0 arg=\"\"")
90+
assert_include(output, "1 arg=\"\"")
91+
assert_include(output, "2 arg=\"a r g\"")
92+
assert_include(output, "3 arg=\"a r g\"")
93+
end
94+
95+
def test_def_extend_command_still_works
96+
write_ruby <<~RUBY
97+
require "irb"
98+
99+
class FooBarCommand < IRB::Command::Base
100+
category 'FooBarCategory'
101+
description 'foobar_description'
102+
def execute(*)
103+
$nth_execution ||= 1
104+
puts "\#{$nth_execution} FooBar executed"
105+
$nth_execution += 1
106+
nil
107+
end
108+
end
109+
110+
IRB::ExtendCommandBundle.def_extend_command(:foobar, FooBarCommand, nil, [:fbalias, IRB::Command::OVERRIDE_ALL])
111+
112+
binding.irb
113+
RUBY
114+
115+
output = run_ruby_file do
116+
type "foobar"
117+
type "fbalias"
118+
type "help foobar"
119+
type "exit"
120+
end
121+
122+
assert_include(output, "1 FooBar executed")
123+
assert_include(output, "2 FooBar executed")
124+
assert_include(output, "foobar_description")
125+
end
126+
end
127+
end

test/irb/test_command.rb

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -210,76 +210,6 @@ def test_irb_info_lang
210210
end
211211
end
212212

213-
class CustomCommandTestCase < CommandTestCase
214-
def setup
215-
@commands_backup = IRB::Command.commands
216-
IRB::Command.class_variable_set(:@@command_override_policies, nil)
217-
end
218-
219-
def teardown
220-
IRB::Command.class_variable_set(:@@command_override_policies, nil)
221-
IRB::Command.instance_variable_set(:@commands, @commands_backup)
222-
end
223-
end
224-
225-
class CommandArgTest < CustomCommandTestCase
226-
class PrintArgCommand < IRB::Command::Base
227-
category 'CommandTest'
228-
description 'print_command_arg'
229-
def execute(arg)
230-
puts "arg=#{arg.inspect}"
231-
end
232-
end
233-
234-
def test_arg
235-
IRB::Command._register_with_aliases(:print_arg, PrintArgCommand, [:pa, IRB::Command::OVERRIDE_ALL])
236-
out, err = execute_lines("print_arg\n")
237-
assert_empty err
238-
assert_include(out, 'arg=""')
239-
240-
out, err = execute_lines("print_arg \n")
241-
assert_empty err
242-
assert_include(out, 'arg=""')
243-
244-
out, err = execute_lines("print_arg a r g\n")
245-
assert_empty err
246-
assert_include(out, 'arg="a r g"')
247-
248-
out, err = execute_lines("print_arg a r g \n")
249-
assert_empty err
250-
assert_include(out, 'arg="a r g"')
251-
252-
out, err = execute_lines("pa a r g \n")
253-
assert_empty err
254-
assert_include(out, 'arg="a r g"')
255-
end
256-
end
257-
258-
class ExtendCommandBundleCompatibilityTest < CustomCommandTestCase
259-
class FooBarCommand < IRB::Command::Base
260-
category 'FooBarCategory'
261-
description 'foobar_description'
262-
def execute(_arg)
263-
puts "FooBar executed"
264-
end
265-
end
266-
267-
def test_def_extend_command
268-
IRB::ExtendCommandBundle.def_extend_command(:foobar, FooBarCommand, nil, [:fbalias, IRB::Command::OVERRIDE_ALL])
269-
out, err = execute_lines("foobar\n")
270-
assert_empty err
271-
assert_include(out, "FooBar executed")
272-
273-
out, err = execute_lines("fbalias\n")
274-
assert_empty err
275-
assert_include(out, "FooBar executed")
276-
277-
out, err = execute_lines("show_cmds\n")
278-
assert_include(out, "FooBarCategory")
279-
assert_include(out, "foobar_description")
280-
end
281-
end
282-
283213
class MeasureTest < CommandTestCase
284214
def test_measure
285215
conf = {

0 commit comments

Comments
 (0)