summaryrefslogtreecommitdiff
path: root/test/irb/command/test_custom_command.rb
blob: 6642d2b160a3157a729bfe75fca2089530a2241f (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
# frozen_string_literal: true
require "irb"

require_relative "../helper"

module TestIRB
  class CustomCommandIntegrationTest < TestIRB::IntegrationTestCase
    def test_command_regsitration_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"
            nil
          end
        end

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

        binding.irb
      RUBY

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

      assert_include(output, "Hello from PrintCommand")
    end

    def test_command_regsitration_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"
            nil
          end
        end

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

        binding.irb
      RUBY

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

      assert_include(output, "Hello from PrintCommand")
    end

    def test_arguments_propogation
      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
            nil
          end
        end

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

        binding.irb
      RUBY

      output = run_ruby_file do
        type "print_arg\n"
        type "print_arg  \n"
        type "print_arg a r  g\n"
        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
            nil
          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
  end
end