Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/ruby_lsp/listeners/spec_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def initialize(response_builder, global_state, dispatcher, uri)
register_events(
dispatcher,
:on_class_node_enter,
:on_def_node_enter,
:on_call_node_enter,
:on_call_node_leave,
)
Expand Down Expand Up @@ -56,6 +57,31 @@ def on_module_node_leave(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerM
super
end

#: (Prism::DefNode) -> void
def on_def_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
name = node.name.to_s
return unless name.start_with?("test_")

current_group = @spec_group_id_stack.last
return unless current_group.is_a?(DescribeGroup)

parent = latest_group
return unless parent.is_a?(Requests::Support::TestItem)

id = "#{parent.id}##{name}"

test_item = Requests::Support::TestItem.new(
id,
name,
@uri,
range_from_node(node),
framework: :minitest,
)

parent.add(test_item)
@response_builder.add_code_lens(test_item)
end

#: (Prism::CallNode) -> void
def on_call_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
case node.name
Expand Down
30 changes: 30 additions & 0 deletions test/requests/discover_tests_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,36 @@ module OtherNamespace
end
end

def test_discovers_test_methods
source = <<~RUBY
describe "MySpec" do
def test_foo; end
def helper_method; end

describe "nested" do
def test_nested; end
end
end

class NotASpec
def test_ignored; end
end
RUBY

with_minitest_spec_configured(source) do |items|
assert_equal(["MySpec"], items.map { |i| i[:id] })
assert_equal(
["MySpec#test_foo", "MySpec::nested"],
items.dig(0, :children).map { |i| i[:id] },
)
assert_equal(
["MySpec::nested#test_nested"],
items.dig(0, :children, 1, :children).map { |i| i[:id] },
)
assert_all_items_tagged_with(items, :minitest)
end
end

private

def create_test_discovery_addon
Expand Down