summaryrefslogtreecommitdiff
path: root/test/irb
diff options
context:
space:
mode:
authorst0012 <stan001212@gmail.com>2022-10-24 22:52:52 +0100
committerTakashi Kokubun <takashikkbn@gmail.com>2022-12-26 13:04:03 -0800
commit8038bf239af09b34f7a33a66d638cde416ba2893 (patch)
tree59d8aa96145f754f728e4ec0234aa3abe501f7f9 /test/irb
parent953d4c22edab50bdc6498e256d2d6400f8fcd78a (diff)
downloadruby-8038bf239af09b34f7a33a66d638cde416ba2893.tar.gz
[ruby/irb] Add test for the help command
https://github.com/ruby/irb/commit/9cacb5f352
Diffstat (limited to 'test/irb')
-rw-r--r--test/irb/test_cmd.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb
index 719a2bbf8f..a58cb3e8a5 100644
--- a/test/irb/test_cmd.rb
+++ b/test/irb/test_cmd.rb
@@ -376,6 +376,57 @@ module TestIRB
assert_match(/Please specify the file name./, out)
end
+ def test_help
+ IRB.init_config(nil)
+ input = TestInputMethod.new([
+ "help 'String#gsub'\n",
+ "\n",
+ ])
+ IRB.conf[:VERBOSE] = false
+ IRB.conf[:PROMPT_MODE] = :SIMPLE
+ irb = IRB::Irb.new(IRB::WorkSpace.new(self), input)
+ IRB.conf[:MAIN_CONTEXT] = irb.context
+ out, err = capture_output do
+ irb.eval_input
+ end
+
+ # the help command lazily loads rdoc by redefining the execute method
+ assert_match(/discarding old execute/, err) unless RUBY_ENGINE == 'truffleruby'
+
+ # the former is what we'd get without document content installed, like on CI
+ # the latter is what we may get locally
+ possible_rdoc_output = [/Nothing known about String#gsub/, /Returns a copy of self with all occurrences of the given pattern/]
+ assert(possible_rdoc_output.any? { |output| output.match?(out) }, "Expect the help command to match one of the possible outputs")
+ ensure
+ # this is the only way to reset the redefined method without coupling the test with its implementation
+ load "irb/cmd/help.rb"
+ end
+
+ def test_help_without_rdoc
+ IRB.init_config(nil)
+ input = TestInputMethod.new([
+ "help 'String#gsub'\n",
+ "\n",
+ ])
+ IRB.conf[:VERBOSE] = false
+ IRB.conf[:PROMPT_MODE] = :SIMPLE
+ irb = IRB::Irb.new(IRB::WorkSpace.new(self), input)
+ IRB.conf[:MAIN_CONTEXT] = irb.context
+ out, err = capture_output do
+ without_rdoc do
+ irb.eval_input
+ end
+ end
+
+ # since LoadError will be raised, the execute won't be redefined
+ assert_no_match(/discarding old execute/, err)
+ # if it fails to require rdoc, it only returns the command object
+ assert_match(/=> IRB::ExtendCommand::Help\n/, out)
+ ensure
+ # this is the only way to reset the redefined method without coupling the test with its implementation
+ load "irb/cmd/help.rb"
+ end
+
def test_irb_load
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
out, err = execute_lines(