Skip to content

Commit 1b75ae3

Browse files
authored
Use unformatted descriptions as test example labels (#642)
1 parent 6473287 commit 1b75ae3

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ def initialize(response_builder, global_state, dispatcher, uri)
6767
def on_class_node_enter(node)
6868
with_test_ancestor_tracking(node) do |name, ancestors|
6969
if declarative_minitest?(ancestors, name)
70+
label = constant_name(node.constant_path) || name_with_dynamic_reference(node.constant_path)
71+
7072
test_item = Requests::Support::TestItem.new(
7173
name,
72-
name,
74+
label,
7375
@uri,
7476
range_from_node(node),
7577
framework: :rails,
@@ -119,7 +121,7 @@ def on_call_node_enter(node)
119121
# Rails uses at runtime, ensuring proper test discovery and execution.
120122
rails_normalized_name = "test_#{test_name.gsub(/\s+/, "_")}"
121123

122-
add_test_item(node, rails_normalized_name)
124+
add_test_item(node, rails_normalized_name, test_name)
123125
end
124126

125127
#: (Prism::DefNode node) -> void
@@ -129,7 +131,7 @@ def on_def_node_enter(node)
129131
name = node.name.to_s
130132
return unless name.start_with?("test_")
131133

132-
add_test_item(node, name)
134+
add_test_item(node, name, name)
133135
end
134136

135137
private
@@ -145,14 +147,14 @@ def declarative_minitest?(attached_ancestors, fully_qualified_name)
145147
false
146148
end
147149

148-
#: (Prism::Node node, String test_name) -> void
149-
def add_test_item(node, test_name)
150+
#: (Prism::Node, String, String) -> void
151+
def add_test_item(node, test_id, label)
150152
parent = @parent_stack.last
151153
return unless parent.is_a?(Requests::Support::TestItem)
152154

153155
example_item = Requests::Support::TestItem.new(
154-
"#{parent.id}##{test_name}",
155-
test_name,
156+
"#{parent.id}##{test_id}",
157+
label,
156158
@uri,
157159
range_from_node(node),
158160
framework: :rails,

test/ruby_lsp_rails/rails_test_style_test.rb

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class SampleTest < ActiveSupport::TestCase
2222
with_active_support_declarative_tests(source) do |items|
2323
assert_equal(1, items.length)
2424
test_class = items.first
25-
assert_equal("SampleTest", test_class[:label])
25+
assert_equal("SampleTest", test_class[:id])
2626
assert_equal(2, test_class[:children].length)
2727

28-
test_labels = test_class[:children].map { |i| i[:label] }
29-
assert_includes(test_labels, "test_first_test")
30-
assert_includes(test_labels, "test_second_test")
28+
test_labels = test_class[:children].map { |i| i[:id] }
29+
assert_includes(test_labels, "SampleTest#test_first_test")
30+
assert_includes(test_labels, "SampleTest#test_second_test")
3131
assert_all_items_tagged_with(items, :rails)
3232
end
3333
end
@@ -48,11 +48,11 @@ class EmptyTest < ActiveSupport::TestCase
4848
with_active_support_declarative_tests(source) do |items|
4949
assert_equal(1, items.length)
5050
test_class = items.first
51-
assert_equal("EmptyTest", test_class[:label])
51+
assert_equal("EmptyTest", test_class[:id])
5252
assert_equal(2, test_class[:children].length)
5353

54-
test_labels = test_class[:children].map { |i| i[:label] }
55-
assert_includes(test_labels, "test_<empty_test_name>")
54+
test_labels = test_class[:children].map { |i| i[:id] }
55+
assert_includes(test_labels, "EmptyTest#test_<empty_test_name>")
5656
assert_all_items_tagged_with(items, :rails)
5757
end
5858
end
@@ -123,12 +123,12 @@ class SpecialCharsTest < ActiveSupport::TestCase
123123
with_active_support_declarative_tests(source) do |items|
124124
assert_equal(1, items.length)
125125
test_class = items.first
126-
assert_equal("SpecialCharsTest", test_class[:label])
126+
assert_equal("SpecialCharsTest", test_class[:id])
127127
assert_equal(2, test_class[:children].length)
128128

129-
test_labels = test_class[:children].map { |i| i[:label] }
130-
assert_includes(test_labels, "test_spaces_and_punctuation!")
131-
assert_includes(test_labels, "test_unicode:_你好")
129+
test_labels = test_class[:children].map { |i| i[:id] }
130+
assert_includes(test_labels, "SpecialCharsTest#test_spaces_and_punctuation!")
131+
assert_includes(test_labels, "SpecialCharsTest#test_unicode:_你好")
132132
assert_all_items_tagged_with(items, :rails)
133133
end
134134
end
@@ -234,11 +234,11 @@ class SpecialCharsTest < ActiveSupport::TestCase
234234
with_active_support_declarative_tests(source) do |items|
235235
assert_equal(1, items.length)
236236
test_class = items.first
237-
assert_equal("SampleTest", test_class[:label])
237+
assert_equal("SampleTest", test_class[:id])
238238
assert_equal(1, test_class[:children].length)
239239

240240
assert_equal(["SampleTest#test_hello_\"oh_noes\""], test_class[:children].map { |i| i[:id] })
241-
assert_equal(["test_hello_\"oh_noes\""], test_class[:children].map { |i| i[:label] })
241+
assert_equal(["hello \"oh noes\""], test_class[:children].map { |i| i[:label] })
242242
assert_all_items_tagged_with(items, :rails)
243243
end
244244
end
@@ -305,7 +305,7 @@ class AnotherGroupTest < ActiveSupport::TestCase
305305
assert_equal(["SampleTest"], items.map { |i| i[:id] })
306306
assert_equal(
307307
["SampleTest::InnerTest", "SampleTest::AnotherGroupTest"],
308-
items.dig(0, :children).map { |i| i[:label] },
308+
items.dig(0, :children).map { |i| i[:id] },
309309
)
310310
assert_equal(
311311
["SampleTest::InnerTest#test_first"],
@@ -337,6 +337,24 @@ class SampleTest < ActiveSupport::TestCase
337337
end
338338
end
339339

340+
test "labels include only unformatted descriptions" do
341+
source = <<~RUBY
342+
class SampleTest < ActiveSupport::TestCase
343+
class InnerGroup < ActiveSupport::TestCase
344+
test "first test" do
345+
assert true
346+
end
347+
end
348+
end
349+
RUBY
350+
351+
with_active_support_declarative_tests(source) do |items|
352+
assert_equal(["SampleTest"], items.map { |i| i[:label] })
353+
assert_equal(["InnerGroup"], items.dig(0, :children).map { |i| i[:label] })
354+
assert_equal(["first test"], items.dig(0, :children, 0, :children).map { |i| i[:label] })
355+
end
356+
end
357+
340358
private
341359

342360
def with_active_support_declarative_tests(source, file: "/fake.rb", &block)

0 commit comments

Comments
 (0)