summaryrefslogtreecommitdiff
path: root/test/irb/command/test_custom_command.rb
blob: 3a3ad11d5a82245fbdffe790b341975308093f90 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true
require "irb"

require_relative "../helper"

module TestIRB
  class CustomCommandIntegrationTest < TestIRB::IntegrationTestCase
    def test_command_registration_can_happen_after_irb_require
      write_ruby <<~RUBY
        require "irb"
        require "irb/command"

        class PrintCommand < IRB::Command::Base
          category 'CommandTest'
          description 'print_command'
          def execute(*)
            puts "Hello from PrintCommand"
          end
        end

        IRB::Command.register(:print!, PrintCommand)

        binding.irb
      RUBY

      output = run_ruby_file do
        type "print!"
        type "exit"
      end

      assert_include(output, "Hello from PrintCommand")
    end

    def test_command_registration_accepts_string_too
      write_ruby <<~RUBY
        require "irb/command"

        class PrintCommand < IRB::Command::Base
          category 'CommandTest'
          description 'print_command'
          def execute(*)
            puts "Hello from PrintCommand"
          end
        end

        IRB::Command.register("print!", PrintCommand)

        binding.irb
      RUBY

      output = run_ruby_file do
        type "print!"
        type "exit"
      end

      assert_include(output, "Hello from PrintCommand")
    end

    def test_arguments_propagation
      write_ruby <<~RUBY
        require "irb/command"

        class PrintArgCommand < IRB::Command::Base
          category 'CommandTest'
          description 'print_command_arg'
          def execute(arg)
            $nth_execution ||= 0
            puts "\#{$nth_execution} arg=\#{arg.inspect}"
            $nth_execution += 1
          end
        end

        IRB::Command.register(:print_arg, PrintArgCommand)

        binding.irb
      RUBY

      output = run_ruby_file do
        type "print_arg"
        type "print_arg  \n"
        type "print_arg a r  g"
        type "print_arg  a r  g  \n"
        type "exit"
      end

      assert_include(output, "0 arg=\"\"")
      assert_include(output, "1 arg=\"\"")
      assert_include(output, "2 arg=\"a r  g\"")
      assert_include(output, "3 arg=\"a r  g\"")
    end

    def test_def_extend_command_still_works
      write_ruby <<~RUBY
        require "irb"

        class FooBarCommand < IRB::Command::Base
          category 'FooBarCategory'
          description 'foobar_description'
          def execute(*)
            $nth_execution ||= 1
            puts "\#{$nth_execution} FooBar executed"
            $nth_execution += 1
          end
        end

        IRB::ExtendCommandBundle.def_extend_command(:foobar, FooBarCommand, nil, [:fbalias, IRB::Command::OVERRIDE_ALL])

        binding.irb
      RUBY

      output = run_ruby_file do
        type "foobar"
        type "fbalias"
        type "help foobar"
        type "exit"
      end

      assert_include(output, "1 FooBar executed")
      assert_include(output, "2 FooBar executed")
      assert_include(output, "foobar_description")
    end

    def test_no_meta_command_also_works
      write_ruby <<~RUBY
        require "irb/command"

        class NoMetaCommand < IRB::Command::Base
          def execute(*)
            puts "This command does not override meta attributes"
          end
        end

        IRB::Command.register(:no_meta, NoMetaCommand)

        binding.irb
      RUBY

      output = run_ruby_file do
        type "no_meta"
        type "help no_meta"
        type "exit"
      end

      assert_include(output, "This command does not override meta attributes")
      assert_include(output, "No description provided.")
      assert_not_include(output, "Maybe IRB bug")
    end
  end
end