[前][次][番号順一覧][スレッド一覧]

ruby-changes:74264

From: st0012 <ko1@a...>
Date: Wed, 26 Oct 2022 23:44:51 +0900 (JST)
Subject: [ruby-changes:74264] d889e810f3 (master): [ruby/irb] Add test for the help command

https://git.ruby-lang.org/ruby.git/commit/?id=d889e810f3

From d889e810f3d9061fdab5a21470bf32d078d2b06a Mon Sep 17 00:00:00 2001
From: st0012 <stan001212@g...>
Date: Mon, 24 Oct 2022 22:52:52 +0100
Subject: [ruby/irb] Add test for the help command

---
 test/irb/test_cmd.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb
index 7b7ab55a64..e5e3a3fabf 100644
--- a/test/irb/test_cmd.rb
+++ b/test/irb/test_cmd.rb
@@ -406,6 +406,57 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_cmd.rb#L406
         ], 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
       IRB.init_config(nil)
       File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]