runnables.scm

  1; Functions names start with `Test`
  2(
  3  (
  4    (function_declaration name: (_) @run
  5      (#match? @run "^Test.*"))
  6  ) @_
  7  (#set! tag go-test)
  8)
  9
 10; Suite test methods (testify/suite)
 11(
 12    (method_declaration
 13      receiver: (parameter_list
 14        (parameter_declaration
 15            type: [
 16                (pointer_type (type_identifier) @_suite_name)
 17                (type_identifier) @_suite_name
 18            ]
 19        )
 20      )
 21      name: (field_identifier) @run @_subtest_name
 22      (#match? @_subtest_name "^Test.*")
 23      (#match? @_suite_name ".*Suite")
 24    ) @_
 25    (#set! tag go-testify-suite)
 26)
 27
 28; `go:generate` comments
 29(
 30    ((comment) @_comment @run
 31    (#match? @_comment "^//go:generate"))
 32    (#set! tag go-generate)
 33)
 34
 35; `t.Run`
 36(
 37  (
 38    (call_expression
 39      function: (
 40        selector_expression
 41        field: _ @run @_name
 42        (#eq? @_name "Run")
 43      )
 44      arguments: (
 45        argument_list
 46        .
 47        [
 48          (interpreted_string_literal)
 49          (raw_string_literal)
 50        ] @_subtest_name
 51        .
 52        (func_literal
 53          parameters: (
 54            parameter_list
 55            (parameter_declaration
 56              name: (identifier) @_param_name
 57              type: (pointer_type
 58                (qualified_type
 59                  package: (package_identifier) @_pkg
 60                  name: (type_identifier) @_type
 61                  (#eq? @_pkg "testing")
 62                  (#eq? @_type "T")
 63                )
 64              )
 65            )
 66          )
 67        ) @_second_argument
 68      )
 69    )
 70  ) @_
 71  (#set! tag go-subtest)
 72)
 73
 74; Functions names start with `Benchmark`
 75(
 76  (
 77    (function_declaration name: (_) @run @_name
 78      (#match? @_name "^Benchmark.*"))
 79  ) @_
 80  (#set! tag go-benchmark)
 81)
 82
 83; Functions names start with `Fuzz`
 84(
 85  (
 86    (function_declaration name: (_) @run @_name
 87      (#match? @_name "^Fuzz"))
 88  ) @_
 89  (#set! tag go-fuzz)
 90)
 91
 92; go run
 93(
 94  (
 95    (function_declaration name: (_) @run
 96      (#eq? @run "main"))
 97  ) @_
 98  (#set! tag go-main)
 99)
100
101; Table test cases - slice and map
102(
103  (short_var_declaration
104    left: (expression_list (identifier) @_collection_var)
105    right: (expression_list
106      (composite_literal
107        type: [
108          (slice_type)
109          (map_type
110            key: (type_identifier) @_key_type
111            (#eq? @_key_type "string")
112          )
113        ]
114        body: (literal_value
115          [
116            (literal_element
117              (literal_value
118                (keyed_element
119                  (literal_element
120                    (identifier) @_field_name
121                  )
122                  (literal_element
123                    [
124                      (interpreted_string_literal) @run @_table_test_case_name
125                      (raw_string_literal) @run @_table_test_case_name
126                    ]
127                  )
128                )
129              )
130            )
131            (keyed_element
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      )
142    )
143  )
144  (for_statement
145    (range_clause
146      left: (expression_list
147        [
148          (
149            (identifier)
150            (identifier) @_loop_var_inner
151          )
152          (identifier) @_loop_var_outer
153        ]
154      )
155      right: (identifier) @_range_var
156      (#eq? @_range_var @_collection_var)
157    )
158    body: (block
159      (expression_statement
160        (call_expression
161          function: (selector_expression
162            operand: (identifier)
163            field: (field_identifier) @_run_method
164            (#eq? @_run_method "Run")
165          )
166          arguments: (argument_list
167            .
168            [
169              (selector_expression
170                operand: (identifier) @_tc_var
171                (#eq? @_tc_var @_loop_var_inner)
172                field: (field_identifier) @_field_check
173                (#eq? @_field_check @_field_name)
174              )
175              (identifier) @_arg_var
176              (#eq? @_arg_var @_loop_var_outer)
177            ]
178            .
179            (func_literal
180              parameters: (parameter_list
181                (parameter_declaration
182                  type: (pointer_type
183                    (qualified_type
184                      package: (package_identifier) @_pkg
185                      name: (type_identifier) @_type
186                      (#eq? @_pkg "testing")
187                      (#eq? @_type "T")
188                    )
189                  )
190                )
191              )
192            )
193          )
194        )
195      )
196    )
197  ) @_
198  (#set! tag go-table-test-case)
199)