diff --git a/lib/ruby_lsp/listeners/spec_style.rb b/lib/ruby_lsp/listeners/spec_style.rb index 5f79a418a..8057b76d2 100644 --- a/lib/ruby_lsp/listeners/spec_style.rb +++ b/lib/ruby_lsp/listeners/spec_style.rb @@ -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, ) @@ -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 diff --git a/test/requests/discover_tests_test.rb b/test/requests/discover_tests_test.rb index d727dc0d7..01072f8be 100644 --- a/test/requests/discover_tests_test.rb +++ b/test/requests/discover_tests_test.rb @@ -822,6 +822,44 @@ 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 + + class NotDescribeSpec < Minitest::Spec + def test_not_duplicate; end + end + RUBY + + with_minitest_spec_configured(source) do |items| + assert_equal(["MySpec", "NotDescribeSpec"], 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_equal( + ["NotDescribeSpec#test_not_duplicate"], + items.dig(1, :children).map { |i| i[:id] }, + ) + assert_all_items_tagged_with(items, :minitest) + end + end + private def create_test_discovery_addon