Skip to content

Commit 42e984d

Browse files
committed
Fix pushing incorrect test item as code lens
1 parent d615deb commit 42e984d

3 files changed

Lines changed: 91 additions & 3 deletions

File tree

lib/ruby_lsp/listeners/test_style.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def on_def_node_enter(node)
202202
framework: @framework,
203203
)
204204
test_item.add(example_item)
205-
@response_builder.add_code_lens(test_item)
205+
@response_builder.add_code_lens(example_item)
206206
end
207207

208208
#: (Prism::CallNode node) -> void

lib/ruby_lsp/response_builders/test_collection.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ def add(item)
2525

2626
#: (ResponseType item) -> void
2727
def add_code_lens(item)
28-
range = item.range
2928
arguments = [item.uri.to_standardized_path, item.id]
29+
start = item.range.start
30+
range = Interface::Range.new(
31+
start: start,
32+
end: Interface::Position.new(line: start.line, character: start.character + 1),
33+
)
3034

3135
@code_lens << Interface::CodeLens.new(
3236
range: range,
@@ -40,7 +44,7 @@ def add_code_lens(item)
4044
@code_lens << Interface::CodeLens.new(
4145
range: range,
4246
command: Interface::Command.new(
43-
title: "▶ Run In Terminal",
47+
title: "▶ Run in terminal",
4448
command: "rubyLsp.runTestInTerminal",
4549
arguments: arguments,
4650
),

test/requests/discover_tests_test.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,90 @@ def test_hello; end
9797
end
9898
end
9999

100+
def test_collects_code_lenses
101+
source = <<~RUBY
102+
module Foo
103+
class MyTest < Test::Unit::TestCase
104+
def test_something; end
105+
106+
def test_something_else; end
107+
end
108+
end
109+
RUBY
110+
111+
with_server(source) do |server, uri|
112+
server.global_state.index.index_single(URI("/other_file.rb"), <<~RUBY)
113+
module Test
114+
module Unit
115+
class TestCase; end
116+
end
117+
end
118+
RUBY
119+
120+
server.global_state.stubs(:enabled_feature?).returns(true)
121+
122+
server.process_message(id: 1, method: "textDocument/codeLens", params: {
123+
textDocument: { uri: uri },
124+
})
125+
126+
# Discard the indexing log message
127+
server.pop_response
128+
items = get_response(server)
129+
assert_equal(9, items.length)
130+
131+
# MyTest
132+
assert_equal("▶ Run", items[0].command.title)
133+
assert_equal(
134+
{ start: { line: 1, character: 2 }, end: { line: 1, character: 3 } },
135+
JSON.parse(items[0].range.to_json, symbolize_names: true),
136+
)
137+
assert_equal("▶ Run in terminal", items[1].command.title)
138+
assert_equal(
139+
{ start: { line: 1, character: 2 }, end: { line: 1, character: 3 } },
140+
JSON.parse(items[1].range.to_json, symbolize_names: true),
141+
)
142+
assert_equal("⚙ Debug", items[2].command.title)
143+
assert_equal(
144+
{ start: { line: 1, character: 2 }, end: { line: 1, character: 3 } },
145+
JSON.parse(items[2].range.to_json, symbolize_names: true),
146+
)
147+
148+
# test_something
149+
assert_equal("▶ Run", items[3].command.title)
150+
assert_equal(
151+
{ start: { line: 2, character: 4 }, end: { line: 2, character: 5 } },
152+
JSON.parse(items[3].range.to_json, symbolize_names: true),
153+
)
154+
assert_equal("▶ Run in terminal", items[4].command.title)
155+
assert_equal(
156+
{ start: { line: 2, character: 4 }, end: { line: 2, character: 5 } },
157+
JSON.parse(items[4].range.to_json, symbolize_names: true),
158+
)
159+
assert_equal("⚙ Debug", items[5].command.title)
160+
assert_equal(
161+
{ start: { line: 2, character: 4 }, end: { line: 2, character: 5 } },
162+
JSON.parse(items[5].range.to_json, symbolize_names: true),
163+
)
164+
165+
# test_something_else
166+
assert_equal("▶ Run", items[6].command.title)
167+
assert_equal(
168+
{ start: { line: 4, character: 4 }, end: { line: 4, character: 5 } },
169+
JSON.parse(items[6].range.to_json, symbolize_names: true),
170+
)
171+
assert_equal("▶ Run in terminal", items[7].command.title)
172+
assert_equal(
173+
{ start: { line: 4, character: 4 }, end: { line: 4, character: 5 } },
174+
JSON.parse(items[7].range.to_json, symbolize_names: true),
175+
)
176+
assert_equal("⚙ Debug", items[8].command.title)
177+
assert_equal(
178+
{ start: { line: 4, character: 4 }, end: { line: 4, character: 5 } },
179+
JSON.parse(items[8].range.to_json, symbolize_names: true),
180+
)
181+
end
182+
end
183+
100184
def test_ignores_minitest_tests_that_extend_active_support_declarative
101185
source = <<~RUBY
102186
class MyTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)