File: use_register_with_handler_method_test.rb

package info (click to toggle)
ruby-ruby-lsp 0.26.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,676 kB
  • sloc: ruby: 35,294; javascript: 29; sh: 7; makefile: 4
file content (118 lines) | stat: -rw-r--r-- 3,844 bytes parent folder | download
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
# typed: true
# frozen_string_literal: true

require "test_helper"

require "rubocop-minitest"
require "rubocop/minitest/assert_offense"

class UseRegisterWithHandlerMethodTest < Minitest::Test
  include RuboCop::Minitest::AssertOffense

  def setup
    @cop = ::RuboCop::Cop::RubyLsp::UseRegisterWithHandlerMethod.new
  end

  def test_registers_offense_when_use_listener_without_handler
    assert_offense(<<~RUBY)
       class MyListener
        def initialize(dispatcher)
          dispatcher.register(
            self,
            :on_string_node_enter,
            ^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Registered to `on_string_node_enter` without a handler defined.

          )
        end
      end
    RUBY
  end

  def test_registers_offense_when_use_handler_without_listener
    assert_offense(<<~RUBY)
       class MyListener
        def initialize(dispatcher)
          dispatcher.register(
            self,
          )
        end

        def on_string_node_enter(node)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Created a handler without registering the associated `on_string_node_enter` event.
        end
      end
    RUBY
  end

  def test_registers_offense_when_both_are_mismatching
    assert_offense(<<~RUBY)
       class MyListener
        def initialize(dispatcher)
          dispatcher.register(
            self,
            :on_string_node_enter,
            ^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Registered to `on_string_node_enter` without a handler defined.
          )
        end

        def on_string_node_leave(node)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Created a handler without registering the associated `on_string_node_leave` event.
        end
      end
    RUBY
  end

  def test_registers_multiple_offenses_for_listeners
    assert_offense(<<~RUBY)
       class MyListener
        def initialize(dispatcher)
          dispatcher.register(
            self,
            :on_string_node_enter,
            ^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Registered to `on_string_node_enter` without a handler defined.
            :on_string_node_leave,
            ^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Registered to `on_string_node_leave` without a handler defined.
            :on_constant_path_node_enter
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Registered to `on_constant_path_node_enter` without a handler defined.
          )
        end
      end
    RUBY
  end

  def test_registers_multiple_offenses_for_handlers
    assert_offense(<<~RUBY)
       class MyListener
        def initialize(dispatcher)
          dispatcher.register(
            self,
          )
        end
        def on_string_node_enter(node)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Created a handler without registering the associated `on_string_node_enter` event.
        end
        def on_string_node_leave(node)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Created a handler without registering the associated `on_string_node_leave` event.
        end
        def on_constant_path_node_enter(node)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RubyLsp/UseRegisterWithHandlerMethod: Created a handler without registering the associated `on_constant_path_node_enter` event.
        end
      end
    RUBY
  end

  def test_does_not_register_offense_when_using_listener_with_handler
    assert_no_offenses(<<~RUBY)
       class MyListener
        def initialize(dispatcher)
          dispatcher.register(
            self,
            :on_string_node_enter
          )
        end
        def on_string_node_enter(node)
        end
      end
    RUBY
  end
end