File: code_lens_expectations_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 (324 lines) | stat: -rw-r--r-- 9,682 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# typed: true
# frozen_string_literal: true

require "test_helper"
require_relative "support/expectations_test_runner"

class CodeLensExpectationsTest < ExpectationsTestRunner
  expectations_tests RubyLsp::Requests::CodeLens, "code_lens"

  def run_expectations(source)
    uri = URI("file://#{@_path}")
    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    stub_test_library("minitest")
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    listener.perform
  end

  def test_command_generation_for_minitest
    stub_test_library("minitest")
    source = <<~RUBY
      class FooTest < MiniTest::Test
        def test_bar; end
      end
    RUBY
    uri = URI("file:///test/fake.rb")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    assert_equal(6, response.size)

    assert_equal("▶ Run In Terminal", response[1]&.command&.title)
    assert_equal(
      "bundle exec ruby -Itest /test/fake.rb --name \"/^FooTest(#|::)/\"",
      response[1] #: as !nil
        .command.arguments[2],
    )
    assert_equal("▶ Run In Terminal", response[4]&.command&.title)
    assert_equal(
      "bundle exec ruby -Itest /test/fake.rb --name FooTest#test_bar",
      response[4] #: as !nil
        .command.arguments[2],
    )
  end

  def test_command_generation_for_minitest_spec
    stub_test_library("minitest")
    source = <<~RUBY
      class FooTest < MiniTest::Spec
        describe "a" do
          it "b"
        end
      end
    RUBY
    uri = URI("file:///spec/fake.rb")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    assert_equal(9, response.size)

    assert_equal("▶ Run In Terminal", response[1]&.command&.title)
    assert_equal(
      "bundle exec ruby -Ispec /spec/fake.rb --name \"/^FooTest(#|::)/\"",
      response[1] #: as !nil
        .command.arguments[2],
    )
    assert_equal("▶ Run In Terminal", response[4]&.command&.title)
    assert_equal(
      "bundle exec ruby -Ispec /spec/fake.rb --name \"/^FooTest::a(#|::)/\"",
      response[4] #: as !nil
        .command.arguments[2],
    )
    assert_equal("▶ Run In Terminal", response[7]&.command&.title)
    assert_equal(
      "bundle exec ruby -Ispec /spec/fake.rb --name \"/^FooTest::a#test_0001_b$/\"",
      response[7] #: as !nil
        .command.arguments[2],
    )
  end

  def test_command_generation_for_minitest_spec_handles_specify_alias_for_it
    stub_test_library("minitest")
    source = <<~RUBY
      describe "a" do
        specify "b"
      end
    RUBY
    uri = URI("file:///spec/fake.rb")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    # 3 for the describe, 3 for the specify
    assert_equal(6, response.size)
  end

  def test_command_generation_for_test_unit
    stub_test_library("test-unit")
    source = <<~RUBY
      class FooTest < Test::Unit::TestCase
        def test_bar; end
      end
    RUBY
    uri = URI("file:///test/fake.rb")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    assert_equal(6, response.size)

    assert_equal("▶ Run In Terminal", response[1]&.command&.title)
    assert_equal(
      "bundle exec ruby -Itest /test/fake.rb --testcase /FooTest/",
      response[1] #: as !nil
        .command.arguments[2],
    )
    assert_equal("▶ Run In Terminal", response[4]&.command&.title)
    assert_equal(
      "bundle exec ruby -Itest /test/fake.rb --testcase /FooTest/ --name test_bar",
      response[4] #: as !nil
        .command.arguments[2],
    )
  end

  def test_no_code_lens_for_unknown_test_framework
    source = <<~RUBY
      class FooTest < Test::Unit::TestCase
        def test_bar; end
      end
    RUBY
    uri = URI("file:///fake.rb")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    stub_test_library("unknown")
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    assert_empty(response)
  end

  def test_no_code_lens_for_rspec
    source = <<~RUBY
      class FooTest < Test::Unit::TestCase
        def test_bar; end
      end
    RUBY
    uri = URI("file:///fake.rb")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    stub_test_library("rspec")
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    assert_empty(response)
  end

  def test_no_code_lens_for_unsaved_files
    source = <<~RUBY
      class FooTest < Test::Unit::TestCase
        def test_bar; end
      end
    RUBY
    uri = URI::Generic.build(scheme: "untitled", opaque: "Untitled-1")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    stub_test_library("minitest")
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    assert_empty(response)
  end

  def test_no_code_lens_for_unsaved_specs
    source = <<~RUBY
      describe FooTest < Minitest::Spec
        it "does something"; end
      end
    RUBY
    uri = URI::Generic.build(scheme: "untitled", opaque: "Untitled-1")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    stub_test_library("minitest")
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    assert_empty(response)
  end

  def test_code_lens_addons
    source = <<~RUBY
      class Test < Minitest::Test; end
    RUBY

    begin
      create_code_lens_addon

      with_server(source) do |server, uri|
        server.process_message({
          id: 1,
          method: "textDocument/codeLens",
          params: { textDocument: { uri: uri }, position: { line: 1, character: 2 } },
        })

        # Pop the re-indexing notification
        server.pop_response

        result = server.pop_response
        assert_instance_of(RubyLsp::Result, result)

        response = result.response

        assert_equal(response.size, 4)
        assert_match("▶ Run", response[0].command.title)
        assert_match("▶ Run In Terminal", response[1].command.title)
        assert_match("Debug", response[2].command.title)
        assert_match("Run Test", response[3].command.title)
      ensure
        RubyLsp::Addon.addon_classes.clear
      end
    end
  end

  def test_no_code_lens_for_nested_defs
    stub_test_library("test-unit")
    source = <<~RUBY
      class FooTest < Test::Unit::TestCase
        def test_bar
          def test_baz; end
        end
      end
    RUBY
    uri = URI("file:///fake.rb")

    document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri, global_state: @global_state)

    dispatcher = Prism::Dispatcher.new
    listener = RubyLsp::Requests::CodeLens.new(@global_state, document, dispatcher)
    dispatcher.dispatch(document.ast)
    response = listener.perform

    assert_equal(6, response.size)
  end

  private

  def create_code_lens_addon
    Class.new(RubyLsp::Addon) do
      def create_code_lens_listener(response_builder, uri, dispatcher)
        raise "uri can't be nil" unless uri

        klass = Class.new do
          include RubyLsp::Requests::Support::Common

          def initialize(response_builder, uri, dispatcher)
            @response_builder = response_builder
            dispatcher.register(self, :on_class_node_enter)
          end

          def on_class_node_enter(node)
            range = self #: as untyped # rubocop:disable Style/RedundantSelf
              .range_from_node(node)

            @response_builder << RubyLsp::Interface::CodeLens.new(
              range: range,
              command: RubyLsp::Interface::Command.new(
                title: "Run #{node.constant_path.slice}",
                command: "rubyLsp.runTest",
              ),
            )
          end
        end

        klass.new(response_builder, uri, dispatcher)
      end

      def activate(global_state, outgoing_queue)
      end

      def deactivate; end

      def name; end

      def version
        "0.1.0"
      end
    end
  end

  def stub_test_library(name)
    @global_state.stubs(:test_library).returns(name)
  end
end