runnables.scm

  1; Functions names start with `Test`
  2(
  3  (
  4    (function_declaration name: (_) @run
  5      (#match? @run "^Test.*")
  6      (#not-match? @run "^TestMain$"))
  7  ) @_
  8  (#set! tag go-test)
  9)
 10
 11; Suite test methods (testify/suite)
 12(
 13    (method_declaration
 14      receiver: (parameter_list
 15        (parameter_declaration
 16            type: [
 17                (pointer_type (type_identifier) @_suite_name)
 18                (type_identifier) @_suite_name
 19            ]
 20        )
 21      )
 22      name: (field_identifier) @run @_subtest_name
 23      (#match? @_subtest_name "^Test.*")
 24      (#match? @_suite_name ".*Suite")
 25    ) @_
 26    (#set! tag go-testify-suite)
 27)
 28
 29; `go:generate` comments
 30(
 31    ((comment) @_comment @run
 32    (#match? @_comment "^//go:generate"))
 33    (#set! tag go-generate)
 34)
 35
 36; `t.Run`
 37(
 38  (
 39    (call_expression
 40      function: (
 41        selector_expression
 42        field: _ @run @_name
 43        (#eq? @_name "Run")
 44      )
 45      arguments: (
 46        argument_list
 47        .
 48        [
 49          (interpreted_string_literal)
 50          (raw_string_literal)
 51        ] @_subtest_name
 52        .
 53        (func_literal
 54          parameters: (
 55            parameter_list
 56            (parameter_declaration
 57              name: (identifier) @_param_name
 58              type: (pointer_type
 59                (qualified_type
 60                  package: (package_identifier) @_pkg
 61                  name: (type_identifier) @_type
 62                  (#eq? @_pkg "testing")
 63                  (#eq? @_type "T")
 64                )
 65              )
 66            )
 67          )
 68        ) @_second_argument
 69      )
 70    )
 71  ) @_
 72  (#set! tag go-subtest)
 73)
 74
 75; Functions names start with `Example`
 76(
 77  (
 78    (function_declaration name: (_) @run @_name
 79      (#match? @_name "^Example.*"))
 80  ) @_
 81  (#set! tag go-example)
 82)
 83
 84; Functions names start with `Benchmark`
 85(
 86  (
 87    (function_declaration name: (_) @run @_name
 88      (#match? @_name "^Benchmark.*"))
 89  ) @_
 90  (#set! tag go-benchmark)
 91)
 92
 93; Functions names start with `Fuzz`
 94(
 95  (
 96    (function_declaration name: (_) @run @_name
 97      (#match? @_name "^Fuzz"))
 98  ) @_
 99  (#set! tag go-fuzz)
100)
101
102; go run
103(
104  (
105    (function_declaration name: (_) @run
106      (#eq? @run "main"))
107  ) @_
108  (#set! tag go-main)
109)
110
111; Table test cases - slice and map with explicit variable
112(
113  (short_var_declaration
114    left: (expression_list (identifier) @_collection_var)
115    right: (expression_list
116      (composite_literal
117        type: [
118          (slice_type)
119          (map_type
120            key: (type_identifier) @_key_type
121            (#eq? @_key_type "string")
122          )
123        ]
124        body: (literal_value
125          [
126            (literal_element
127              (literal_value
128                (keyed_element
129                  (literal_element
130                    (identifier) @_field_name
131                  )
132                  (literal_element
133                    [
134                      (interpreted_string_literal) @run @_table_test_case_name
135                      (raw_string_literal) @run @_table_test_case_name
136                    ]
137                  )
138                )
139              )
140            )
141            (keyed_element
142              (literal_element
143                [
144                  (interpreted_string_literal) @run @_table_test_case_name
145                  (raw_string_literal) @run @_table_test_case_name
146                ]
147              )
148            )
149          ]
150        )
151      )
152    )
153  )
154  (for_statement
155    (range_clause
156      left: (expression_list
157        [
158          (
159            (identifier)
160            (identifier) @_loop_var_inner
161          )
162          (identifier) @_loop_var_outer
163        ]
164      )
165      right: (identifier) @_range_var
166      (#eq? @_range_var @_collection_var)
167    )
168    body: (block
169      (statement_list
170        (expression_statement
171          (call_expression
172            function: (selector_expression
173              operand: (identifier)
174              field: (field_identifier) @_run_method
175              (#eq? @_run_method "Run")
176            )
177            arguments: (argument_list
178              .
179              [
180                (selector_expression
181                  operand: (identifier) @_tc_var
182                  (#eq? @_tc_var @_loop_var_inner)
183                  field: (field_identifier) @_field_check
184                  (#eq? @_field_check @_field_name)
185                )
186                (identifier) @_arg_var
187                (#eq? @_arg_var @_loop_var_outer)
188              ]
189              .
190              (func_literal
191                parameters: (parameter_list
192                  (parameter_declaration
193                    type: (pointer_type
194                      (qualified_type
195                        package: (package_identifier) @_pkg
196                        name: (type_identifier) @_type
197                        (#eq? @_pkg "testing")
198                        (#eq? @_type "T")
199                      )
200                    )
201                  )
202                )
203              )
204            )
205          )
206        )
207      )
208    )
209  ) @_
210  (#set! tag go-table-test-case)
211)
212
213; Table test cases - slice and map declared right inside the loop without
214; explicit variable
215(
216  (for_statement
217    (range_clause
218      left: (expression_list
219        [
220          (
221            (identifier)
222            (identifier) @_loop_var_inner
223          )
224          (identifier) @_loop_var_outer
225        ]
226      )
227      right: (composite_literal
228        type: [
229          (slice_type)
230          (map_type
231            key: (type_identifier) @_key_type
232            (#eq? @_key_type "string")
233          )
234        ]
235        body: (literal_value
236          [
237            (literal_element
238              (literal_value
239                (keyed_element
240                  (literal_element
241                    (identifier) @_field_name
242                  )
243                  (literal_element
244                    [
245                      (interpreted_string_literal) @run @_table_test_case_name
246                      (raw_string_literal) @run @_table_test_case_name
247                    ]
248                  )
249                )
250              )
251            )
252            (keyed_element
253              (literal_element
254                [
255                  (interpreted_string_literal) @run @_table_test_case_name
256                  (raw_string_literal) @run @_table_test_case_name
257                ]
258              )
259            )
260          ]
261        )
262      )
263    )
264    body: (block
265      (statement_list
266        (expression_statement
267          (call_expression
268            function: (selector_expression
269              operand: (identifier)
270              field: (field_identifier) @_run_method
271              (#eq? @_run_method "Run")
272            )
273            arguments: (argument_list
274              .
275              [
276                (selector_expression
277                  operand: (identifier) @_tc_var
278                  (#eq? @_tc_var @_loop_var_inner)
279                  field: (field_identifier) @_field_check
280                  (#eq? @_field_check @_field_name)
281                )
282                (identifier) @_arg_var
283                (#eq? @_arg_var @_loop_var_outer)
284              ]
285              .
286              (func_literal
287                parameters: (parameter_list
288                  (parameter_declaration
289                    type: (pointer_type
290                      (qualified_type
291                        package: (package_identifier) @_pkg
292                        name: (type_identifier) @_type
293                        (#eq? @_pkg "testing")
294                        (#eq? @_type "T")
295                      )
296                    )
297                  )
298                )
299              )
300            )
301          )
302        )
303      )
304    )
305  ) @_
306  (#set! tag go-table-test-case-without-explicit-variable)
307)