Skip to content

Commit

Permalink
Merge pull request #418 from st0012/add-test-for-help
Browse files Browse the repository at this point in the history
Add test for the help command
  • Loading branch information
peterzhu2118 authored Oct 26, 2022
2 parents 67477f6 + e08c981 commit c72fa9a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
49 changes: 49 additions & 0 deletions test/irb/test_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "irb"
require "irb/extend-command"

require_relative "test_helper"

module TestIRB
class ExtendCommand < Test::Unit::TestCase
class TestInputMethod < ::IRB::InputMethod
Expand Down Expand Up @@ -406,6 +408,53 @@ def test_irb_source
], out)
end

def test_help
IRB.init_config(nil)
input = TestInputMethod.new([
"help 'String#gsub'\n",
"\n",
])
IRB.conf[:PROMPT_MODE] = :SIMPLE
irb = IRB::Irb.new(IRB::WorkSpace.new(self), input)
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
EnvUtil.suppress_warning { 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[:PROMPT_MODE] = :SIMPLE
irb = IRB::Irb.new(IRB::WorkSpace.new(self), input)
out, err = capture_output do
IRB::TestHelper.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
EnvUtil.suppress_warning { load "irb/cmd/help.rb" }
end

def test_irb_load
IRB.init_config(nil)
File.write("#{@tmpdir}/a.rb", "a = 'hi'\n")
Expand Down
16 changes: 16 additions & 0 deletions test/irb/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module IRB
module TestHelper
def self.without_rdoc(&block)
::Kernel.send(:alias_method, :old_require, :require)

::Kernel.define_method(:require) do |name|
raise LoadError, "cannot load such file -- rdoc (test)" if name.match?("rdoc") || name.match?(/^rdoc\/.*/)
::Kernel.send(:old_require, name)
end

yield
ensure
EnvUtil.suppress_warning { ::Kernel.send(:alias_method, :require, :old_require) }
end
end
end
17 changes: 3 additions & 14 deletions test/irb/test_input_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "test/unit"
require "irb"

require_relative "test_helper"

module TestIRB
class TestRelineInputMethod < Test::Unit::TestCase
def setup
Expand Down Expand Up @@ -76,7 +78,7 @@ def test_initialization_with_use_autocomplete_but_without_rdoc

IRB.conf[:USE_AUTOCOMPLETE] = true

without_rdoc do
IRB::TestHelper.without_rdoc do
IRB::RelineInputMethod.new
end

Expand All @@ -86,19 +88,6 @@ def test_initialization_with_use_autocomplete_but_without_rdoc
ensure
Reline.add_dialog_proc(:show_doc, original_show_doc_proc, Reline::DEFAULT_DIALOG_CONTEXT)
end

def without_rdoc(&block)
::Kernel.send(:alias_method, :old_require, :require)

::Kernel.define_method(:require) do |name|
raise LoadError, "cannot load such file -- rdoc (test)" if name == "rdoc"
original_require(name)
end

yield
ensure
::Kernel.send(:alias_method, :require, :old_require)
end
end
end

1 change: 1 addition & 0 deletions test/lib/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ def windows? platform = RUBY_PLATFORM
end
end
end

0 comments on commit c72fa9a

Please sign in to comment.