@@ -54,6 +54,8 @@ def resolve_test_commands(items)
5454 def initialize ( response_builder , global_state , dispatcher , uri )
5555 super ( response_builder , global_state , dispatcher , uri )
5656
57+ @parent_stack = [ @response_builder ] #: Array[(Requests::Support::TestItem | ResponseBuilders::TestCollection)?]
58+
5759 dispatcher . register (
5860 self ,
5961 :on_class_node_enter ,
@@ -74,12 +76,32 @@ def on_class_node_enter(node)
7476 framework : :rails ,
7577 )
7678
77- @response_builder . add ( test_item )
79+ last_test_group . add ( test_item )
7880 @response_builder . add_code_lens ( test_item )
81+ @parent_stack << test_item
82+ else
83+ @parent_stack << nil
7984 end
8085 end
8186 end
8287
88+ #: (Prism::ClassNode node) -> void
89+ def on_class_node_leave ( node )
90+ @parent_stack . pop
91+ super
92+ end
93+
94+ #: (Prism::ModuleNode node) -> void
95+ def on_module_node_enter ( node )
96+ @parent_stack << nil
97+ end
98+
99+ #: (Prism::ModuleNode node) -> void
100+ def on_module_node_leave ( node )
101+ @parent_stack . pop
102+ super
103+ end
104+
83105 #: (Prism::CallNode node) -> void
84106 def on_call_node_enter ( node )
85107 return unless node . name == :test
@@ -125,28 +147,24 @@ def declarative_minitest?(attached_ancestors, fully_qualified_name)
125147
126148 #: (Prism::Node node, String test_name) -> void
127149 def add_test_item ( node , test_name )
128- test_item = group_test_item
129- return unless test_item
150+ parent = @parent_stack . last
151+ return unless parent . is_a? ( Requests :: Support :: TestItem )
130152
131153 example_item = Requests ::Support ::TestItem . new (
132- "#{ test_item . id } ##{ test_name } " ,
154+ "#{ parent . id } ##{ test_name } " ,
133155 test_name ,
134156 @uri ,
135157 range_from_node ( node ) ,
136158 framework : :rails ,
137159 )
138- test_item . add ( example_item )
160+ parent . add ( example_item )
139161 @response_builder . add_code_lens ( example_item )
140162 end
141163
142- #: -> Requests::Support::TestItem?
143- def group_test_item
144- current_group_name = RubyIndexer ::Index . actual_nesting ( @nesting , nil ) . join ( "::" )
145-
146- # If we're finding a test method, but for the wrong framework, then the group test item will not have been
147- # previously pushed and thus we return early and avoid adding items for a framework this listener is not
148- # interested in
149- @response_builder [ current_group_name ]
164+ #: -> (Requests::Support::TestItem | ResponseBuilders::TestCollection)
165+ def last_test_group
166+ index = @parent_stack . rindex { |i | i } #: as !nil
167+ @parent_stack [ index ] #: as Requests::Support::TestItem | ResponseBuilders::TestCollection
150168 end
151169 end
152170 end
0 commit comments