Debian Codegraph @

test/irb/command/test_custom_command.rb

raw symbols SCIP
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_registration_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 end
19 end
20
21 IRB::Command.register(:print!, PrintCommand)
22
23 binding.irb
24 RUBY
25
26 output = run_ruby_file do
27 type "print!"
28 type "exit"
29 end
30
31 assert_include(output, "Hello from PrintCommand")
32 end
33
34 def test_command_registration_accepts_string_too
35 write_ruby <<~RUBY
36 require "irb/command"
37
38 class PrintCommand < IRB::Command::Base
39 category 'CommandTest'
40 description 'print_command'
41 def execute(*)
42 puts "Hello from PrintCommand"
43 end
44 end
45
46 IRB::Command.register("print!", PrintCommand)
47
48 binding.irb
49 RUBY
50
51 output = run_ruby_file do
52 type "print!"
53 type "exit"
54 end
55
56 assert_include(output, "Hello from PrintCommand")
57 end
58
59 def test_arguments_propagation
60 write_ruby <<~RUBY
61 require "irb/command"
62
63 class PrintArgCommand < IRB::Command::Base
64 category 'CommandTest'
65 description 'print_command_arg'
66 def execute(arg)
67 $nth_execution ||= 0
68 puts "\#{$nth_execution} arg=\#{arg.inspect}"
69 $nth_execution += 1
70 end
71 end
72
73 IRB::Command.register(:print_arg, PrintArgCommand)
74
75 binding.irb
76 RUBY
77
78 output = run_ruby_file do
79 type "print_arg"
80 type "print_arg \n"
81 type "print_arg a r g"
82 type "print_arg a r g \n"
83 type "exit"
84 end
85
86 assert_include(output, "0 arg=\"\"")
87 assert_include(output, "1 arg=\"\"")
88 assert_include(output, "2 arg=\"a r g\"")
89 assert_include(output, "3 arg=\"a r g\"")
90 end
91
92 def test_def_extend_command_still_works
93 write_ruby <<~RUBY
94 require "irb"
95
96 class FooBarCommand < IRB::Command::Base
97 category 'FooBarCategory'
98 description 'foobar_description'
99 def execute(*)
100 $nth_execution ||= 1
101 puts "\#{$nth_execution} FooBar executed"
102 $nth_execution += 1
103 end
104 end
105
106 IRB::ExtendCommandBundle.def_extend_command(:foobar, FooBarCommand, nil, [:fbalias, IRB::Command::OVERRIDE_ALL])
107
108 binding.irb
109 RUBY
110
111 output = run_ruby_file do
112 type "foobar"
113 type "fbalias"
114 type "help foobar"
115 type "exit"
116 end
117
118 assert_include(output, "1 FooBar executed")
119 assert_include(output, "2 FooBar executed")
120 assert_include(output, "foobar_description")
121 end
122
123 def test_no_meta_command_also_works
124 write_ruby <<~RUBY
125 require "irb/command"
126
127 class NoMetaCommand < IRB::Command::Base
128 def execute(*)
129 puts "This command does not override meta attributes"
130 end
131 end
132
133 IRB::Command.register(:no_meta, NoMetaCommand)
134
135 binding.irb
136 RUBY
137
138 output = run_ruby_file do
139 type "no_meta"
140 type "help no_meta"
141 type "exit"
142 end
143
144 assert_include(output, "This command does not override meta attributes")
145 assert_include(output, "No description provided.")
146 assert_not_include(output, "Maybe IRB bug")
147 end
148 end
149 end