Skip to content

Commit 53c61e6

Browse files
authored
Consider extra Minitest spec descriptions (#3688)
1 parent af955d8 commit 53c61e6

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

lib/ruby_lsp/listeners/spec_style.rb

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ def handle_describe(node)
117117

118118
#: (Prism::CallNode) -> void
119119
def handle_example(node)
120-
# Minitest formats the descriptions into test method names by using the count of examples with the description
121-
# We are not guaranteed to discover examples in the exact order using static analysis, so we use the line number
122-
# instead. Note that anonymous examples mixed with meta-programming will not be handled correctly
123-
description = extract_description(node) || "anonymous"
120+
description = extract_it_description(node)
124121
line = node.location.start_line - 1
125122
parent = latest_group
126123
return unless parent.is_a?(Requests::Support::TestItem)
@@ -141,16 +138,37 @@ def handle_example(node)
141138

142139
#: (Prism::CallNode) -> String?
143140
def extract_description(node)
141+
arguments = node.arguments&.arguments
142+
return unless arguments
143+
144+
parts = arguments.map { |arg| extract_argument_content(arg) }
145+
return if parts.empty?
146+
147+
parts.join("::")
148+
end
149+
150+
#: (Prism::CallNode) -> String
151+
def extract_it_description(node)
152+
# Minitest formats the descriptions into test method names by using the count of examples with the description
153+
# We are not guaranteed to discover examples in the exact order using static analysis, so we use the line number
154+
# instead. Note that anonymous examples mixed with meta-programming will not be handled correctly
144155
first_argument = node.arguments&.arguments&.first
145-
return unless first_argument
156+
return "anonymous" unless first_argument
157+
158+
extract_argument_content(first_argument) || "anonymous"
159+
end
146160

147-
case first_argument
161+
#: (Prism::Node) -> String?
162+
def extract_argument_content(arg)
163+
case arg
148164
when Prism::StringNode
149-
first_argument.content
165+
arg.content
166+
when Prism::SymbolNode
167+
arg.value
150168
when Prism::ConstantReadNode, Prism::ConstantPathNode
151-
constant_name(first_argument)
169+
constant_name(arg)
152170
else
153-
first_argument.slice
171+
arg.slice
154172
end
155173
end
156174

test/requests/discover_tests_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,30 @@ def test_other_thing; end
745745
end
746746
end
747747

748+
def test_spec_using_describe_with_additional_descriptions
749+
source = <<~RUBY
750+
describe(Foo, :extra_1, :extra_2) do
751+
describe("#some_method") do
752+
it("does something") do
753+
assert(true)
754+
end
755+
end
756+
end
757+
RUBY
758+
759+
with_minitest_spec_configured(source) do |items|
760+
assert_equal(["Foo::extra_1::extra_2"], items.map { |i| i[:id] })
761+
assert_equal(
762+
["Foo::extra_1::extra_2::#some_method"],
763+
items.dig(0, :children).map { |i| i[:id] },
764+
)
765+
assert_equal(
766+
["Foo::extra_1::extra_2::#some_method#test_0002_does something"],
767+
items.dig(0, :children).dig(0, :children).map { |i| i[:id] },
768+
)
769+
end
770+
end
771+
748772
private
749773

750774
def create_test_discovery_addon

0 commit comments

Comments
 (0)