aboutsummaryrefslogtreecommitdiffstats
path: root/test/irb/test_input_method.rb
blob: 05580256fae0b0bcd5007e1b0f1d302c43688e63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: false

require "test/unit"
require "irb"

module TestIRB
  class TestRelineInputMethod < Test::Unit::TestCase
    def setup
      @conf_backup = IRB.conf.dup
      IRB.conf[:LC_MESSAGES] = IRB::Locale.new

      # RelineInputMethod#initialize calls IRB.set_encoding, which mutates standard input/output's encoding
      # so we need to make sure we set them back
      @original_io_encodings = {
        STDIN => [STDIN.external_encoding, STDIN.internal_encoding],
        STDOUT => [STDOUT.external_encoding, STDOUT.internal_encoding],
        STDERR => [STDERR.external_encoding, STDERR.internal_encoding],
      }
      @original_default_encodings = [Encoding.default_external, Encoding.default_internal]
    end

    def teardown
      IRB.conf.replace(@conf_backup)

      @original_io_encodings.each do |io, (external_encoding, internal_encoding)|
        io.set_encoding(external_encoding, internal_encoding)
      end

      EnvUtil.suppress_warning { Encoding.default_external, Encoding.default_internal = @original_default_encodings }
    end

    def test_initialization
      IRB::RelineInputMethod.new

      assert_nil Reline.completion_append_character
      assert_equal '', Reline.completer_quote_characters
      assert_equal IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS, Reline.basic_word_break_characters
      assert_equal IRB::InputCompletor::CompletionProc, Reline.completion_proc
      assert_equal IRB::InputCompletor::PerfectMatchedProc, Reline.dig_perfect_match_proc
    end

    def test_initialization_without_use_autocomplete
      original_show_doc_proc = Reline.dialog_proc(:show_doc)&.dialog_proc
      empty_proc = Proc.new {}
      Reline.add_dialog_proc(:show_doc, empty_proc)

      IRB.conf[:USE_AUTOCOMPLETE] = false

      IRB::RelineInputMethod.new

      refute Reline.autocompletion
      assert_equal empty_proc, Reline.dialog_proc(:show_doc).dialog_proc
    ensure
      Reline.add_dialog_proc(:show_doc, original_show_doc_proc, Reline::DEFAULT_DIALOG_CONTEXT)
    end

    def test_initialization_with_use_autocomplete
      original_show_doc_proc = Reline.dialog_proc(:show_doc)&.dialog_proc
      empty_proc = Proc.new {}
      Reline.add_dialog_proc(:show_doc, empty_proc)

      IRB.conf[:USE_AUTOCOMPLETE] = true

      IRB::RelineInputMethod.new

      assert Reline.autocompletion
      assert_equal IRB::RelineInputMethod::SHOW_DOC_DIALOG, Reline.dialog_proc(:show_doc).dialog_proc
    ensure
      Reline.add_dialog_proc(:show_doc, original_show_doc_proc, Reline::DEFAULT_DIALOG_CONTEXT)
    end

    def test_initialization_with_use_autocomplete_but_without_rdoc
      original_show_doc_proc = Reline.dialog_proc(:show_doc)&.dialog_proc
      empty_proc = Proc.new {}
      Reline.add_dialog_proc(:show_doc, empty_proc)

      IRB.conf[:USE_AUTOCOMPLETE] = true

      without_rdoc do
        IRB::RelineInputMethod.new
      end

      assert Reline.autocompletion
      # doesn't register show_doc dialog
      assert_equal empty_proc, Reline.dialog_proc(:show_doc).dialog_proc
    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