miwa-cli 0.7.0

doc / req / test の3層の仕様乖離(ドリフト)を検知する CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
//! Test extraction for Go, Rust, C#, Java, Kotlin, TypeScript, JavaScript and Dart source files.

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtractedTest {
    pub id: String,
    pub name: String,
    pub markers: Vec<String>,
    pub content: String,
}

#[derive(Debug, thiserror::Error)]
pub enum ExtractError {
    #[error("parse error in {path}")]
    Parse { path: String },
}

/// Extract Go tests from a `*_test.go` file: top-level `function_declaration`s
/// whose name starts with `Test`. Markers are collected from the block of
/// comments immediately (no blank line) preceding the function.
pub fn extract_go_tests(path: &str, source: &str) -> Result<Vec<ExtractedTest>, ExtractError> {
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&tree_sitter_go::LANGUAGE.into())
        .expect("valid go grammar");
    let tree = parser
        .parse(source, None)
        .ok_or_else(|| ExtractError::Parse {
            path: path.to_string(),
        })?;
    let root = tree.root_node();
    if root.has_error() {
        return Err(ExtractError::Parse {
            path: path.to_string(),
        });
    }

    let dir = dir_of(path);
    let mut results = Vec::new();

    let mut cursor = root.walk();
    let children: Vec<tree_sitter::Node> = root.children(&mut cursor).collect();
    for (idx, node) in children.iter().enumerate() {
        if node.kind() != "function_declaration" {
            continue;
        }
        let Some(name_node) = node.child_by_field_name("name") else {
            continue;
        };
        let name = name_node.utf8_text(source.as_bytes()).unwrap_or("");
        if !name.starts_with("Test") {
            continue;
        }
        let markers = collect_preceding_markers(&children, idx, source, "comment");
        let content = node.utf8_text(source.as_bytes()).unwrap_or("").to_string();
        results.push(ExtractedTest {
            id: format!("{dir}:{name}"),
            name: name.to_string(),
            markers,
            content,
        });
    }
    Ok(results)
}

/// Extract Rust tests: `function_item`s carrying a test attribute, i.e. an
/// outer attribute whose path ends in `test` (`#[test]`, `#[tokio::test]`, ...)
/// or in one of `extra_attributes` (last path segment, e.g. `rstest`).
/// Markers come from the comment block immediately preceding the first
/// (topmost) test attribute. `content` is the source of the `function_item`
/// (attributes not included).
pub fn extract_rust_tests(
    path: &str,
    source: &str,
    extra_attributes: &[String],
) -> Result<Vec<ExtractedTest>, ExtractError> {
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&tree_sitter_rust::LANGUAGE.into())
        .expect("valid rust grammar");
    let tree = parser
        .parse(source, None)
        .ok_or_else(|| ExtractError::Parse {
            path: path.to_string(),
        })?;
    let root = tree.root_node();
    if root.has_error() {
        return Err(ExtractError::Parse {
            path: path.to_string(),
        });
    }

    let base = path.strip_suffix(".rs").unwrap_or(path);
    let mut results = Vec::new();
    walk_rust_items(root, source, base, &[], extra_attributes, &mut results);
    Ok(results)
}

fn walk_rust_items(
    container: tree_sitter::Node,
    source: &str,
    base: &str,
    mods: &[String],
    extra_attributes: &[String],
    results: &mut Vec<ExtractedTest>,
) {
    let mut cursor = container.walk();
    let children: Vec<tree_sitter::Node> = container.children(&mut cursor).collect();
    for (idx, node) in children.iter().enumerate() {
        match node.kind() {
            "function_item" => {
                let Some(name_node) = node.child_by_field_name("name") else {
                    continue;
                };
                let name = name_node
                    .utf8_text(source.as_bytes())
                    .unwrap_or("")
                    .to_string();
                let Some(markers) = rust_test_markers(&children, idx, source, extra_attributes)
                else {
                    continue;
                };
                let content = node.utf8_text(source.as_bytes()).unwrap_or("").to_string();
                let id = if mods.is_empty() {
                    format!("{base}::{name}")
                } else {
                    format!("{base}:{}:{name}", mods.join("/"))
                };
                results.push(ExtractedTest {
                    id,
                    name,
                    markers,
                    content,
                });
            }
            "mod_item" => {
                let Some(name_node) = node.child_by_field_name("name") else {
                    continue;
                };
                let Some(body) = node.child_by_field_name("body") else {
                    continue;
                };
                let mod_name = name_node
                    .utf8_text(source.as_bytes())
                    .unwrap_or("")
                    .to_string();
                let mut new_mods = mods.to_vec();
                new_mods.push(mod_name);
                walk_rust_items(body, source, base, &new_mods, extra_attributes, results);
            }
            _ => {}
        }
    }
}

/// Last row a comment occupies. tree-sitter-rust includes the trailing newline
/// of `///` / `//!` line comments, so such a node ends at column 0 of the
/// next row; that row is not part of the comment.
fn comment_end_row(node: tree_sitter::Node) -> usize {
    let end = node.end_position();
    if end.column == 0 && end.row > node.start_position().row {
        end.row - 1
    } else {
        end.row
    }
}

/// Whether only whitespace (or a BOM) precedes the node on its line in
/// `parsed` (the string the tree was built from). FR-064.
fn is_own_line_comment(parsed: &str, node: tree_sitter::Node) -> bool {
    let s = node.start_byte();
    let Some(head) = parsed.get(..s) else {
        return false;
    };
    let ls = head.rfind('\n').map_or(0, |i| i + 1);
    head[ls..]
        .chars()
        .all(|c| c.is_whitespace() || c == '\u{feff}')
}

/// Walks backward from `children[target_idx]`, collecting a contiguous
/// (no-blank-line) block of own-line comment nodes whose kind is in
/// `comment_kinds`, and extracts `[marker]` tokens from them, top to bottom.
/// A comment that does not start its line (e.g. trailing a previous item)
/// ends the block (FR-064). `parsed` is the string the tree was parsed from;
/// `text_of` yields the text of a node (Kotlin reads the original source, not
/// its ASCII shadow). Nodes for which `is_doc` holds are read over without
/// contributing markers and without ending the block.
fn collect_preceding_markers_with<'a>(
    children: &[tree_sitter::Node],
    target_idx: usize,
    parsed: &str,
    text_of: &dyn Fn(tree_sitter::Node) -> Option<&'a str>,
    comment_kinds: &[&str],
    is_doc: &dyn Fn(tree_sitter::Node) -> bool,
) -> Vec<String> {
    let mut next_start_row = children[target_idx].start_position().row;
    let mut j = target_idx as isize - 1;
    let mut comments = Vec::new();
    while j >= 0 {
        let node = children[j as usize];
        if !comment_kinds.contains(&node.kind()) {
            break;
        }
        let Some(text) = text_of(node) else {
            break;
        };
        if comment_end_row(node) + 1 != next_start_row || !is_own_line_comment(parsed, node) {
            break;
        }
        if !is_doc(node) {
            comments.push(text);
        }
        next_start_row = node.start_position().row;
        j -= 1;
    }
    comments.reverse();
    let mut markers = Vec::new();
    for text in comments {
        markers.extend(extract_markers(text));
    }
    markers
}

/// Single-kind variant of [`collect_preceding_markers_with`] (no doc skip).
fn collect_preceding_markers(
    children: &[tree_sitter::Node],
    target_idx: usize,
    source: &str,
    comment_kind: &str,
) -> Vec<String> {
    collect_preceding_markers_with(
        children,
        target_idx,
        source,
        &|n| n.utf8_text(source.as_bytes()).ok(),
        &[comment_kind],
        &|_| false,
    )
}

/// Rust doc comments (`///`, `//!`, `/** */`, `/*! */`) carry an
/// `outer_doc_comment_marker` / `inner_doc_comment_marker` child. Decided by
/// node structure so `////`, `/***` and `/**/` stay ordinary comments.
fn is_rust_doc_comment(node: tree_sitter::Node) -> bool {
    let mut cursor = node.walk();
    let found = node.children(&mut cursor).any(|c| {
        matches!(
            c.kind(),
            "outer_doc_comment_marker" | "inner_doc_comment_marker"
        )
    });
    found
}

/// Returns `None` if the attribute chain above `children[func_idx]` has no
/// test attribute. The chain is the contiguous run of `attribute_item`s and
/// comments directly above the `fn` (comments do not end the chain). A test
/// attribute is one whose path's last segment is `test` or listed in
/// `extra_attributes`. Otherwise returns the markers from the comment block
/// immediately (no blank line) preceding the topmost test attribute.
fn rust_test_markers(
    children: &[tree_sitter::Node],
    func_idx: usize,
    source: &str,
    extra_attributes: &[String],
) -> Option<Vec<String>> {
    let mut test_attr_idx: Option<usize> = None;
    let mut j = func_idx as isize - 1;
    while j >= 0 {
        let node = children[j as usize];
        if node.kind() == "attribute_item" {
            if rust_is_test_attribute(node, source, extra_attributes) {
                test_attr_idx = Some(j as usize);
            }
        } else if !RUST_COMMENT_KINDS.contains(&node.kind()) {
            break;
        }
        j -= 1;
    }
    let test_attr_idx = test_attr_idx?;
    Some(collect_preceding_markers_with(
        children,
        test_attr_idx,
        source,
        &|n| n.utf8_text(source.as_bytes()).ok(),
        &RUST_COMMENT_KINDS,
        &is_rust_doc_comment,
    ))
}

/// Whether an `attribute_item` is a test attribute. The attribute path's last
/// segment is the text of the first named child of `attribute` when it is an
/// `identifier`, or the `name` field when it is a `scoped_identifier`;
/// arguments are never inspected. It matches when that segment is `test` or
/// equals one of `extra_attributes`. `#![test]` is an `inner_attribute_item`,
/// so it never reaches this function.
fn rust_is_test_attribute(
    item: tree_sitter::Node,
    source: &str,
    extra_attributes: &[String],
) -> bool {
    let mut cursor = item.walk();
    let Some(attribute) = item
        .named_children(&mut cursor)
        .find(|c| c.kind() == "attribute")
    else {
        return false;
    };
    let Some(path) = attribute.named_child(0) else {
        return false;
    };
    let segment = match path.kind() {
        "identifier" => path,
        "scoped_identifier" => match path.child_by_field_name("name") {
            Some(n) => n,
            None => return false,
        },
        _ => return false,
    };
    let text = segment.utf8_text(source.as_bytes()).unwrap_or("");
    text == "test" || extra_attributes.iter().any(|a| a == text)
}

const RUST_COMMENT_KINDS: [&str; 2] = ["line_comment", "block_comment"];

/// Doc comments (Javadoc / KDoc) are never markers in Java / Kotlin (and do
/// not end the adjacent comment block).
fn is_doc_comment(text: &str) -> bool {
    text.starts_with("/**")
}

/// Test names that are empty or contain control characters / line separators
/// (U+0085, U+2028, U+2029) are ignored.
fn is_valid_test_name(name: &str) -> bool {
    !name.is_empty()
        && !name
            .chars()
            .any(|c| c.is_control() || matches!(c, '\u{85}' | '\u{2028}' | '\u{2029}'))
}

/// Strips the comment syntax once per kind and returns the body together with
/// whether it is a block comment body. Line comments lose `//` and at most one
/// more `/` (`///`); block comments lose `/*`, at most one more `*` (`/**`) and
/// the closing `*/`. Any further `/` or `*` stays in the body.
fn comment_body(text: &str) -> (&str, bool) {
    if let Some(rest) = text.strip_prefix("//") {
        return (rest.strip_prefix('/').unwrap_or(rest), false);
    }
    match text.strip_prefix("/*") {
        Some(rest) => {
            let rest = rest.strip_suffix("*/").unwrap_or(rest);
            (rest.strip_prefix('*').unwrap_or(rest), true)
        }
        None => (text, false),
    }
}

/// Returns the markers of one comment. The comment is a marker comment only
/// when its body (comment syntax and surrounding whitespace removed) consists
/// solely of whitespace-separated `[marker]` tokens, where `marker` is one or
/// more `[A-Za-z0-9._-]` characters. Any other text makes it a plain comment
/// with no markers. FR-121.
fn extract_markers(text: &str) -> Vec<String> {
    let (body, is_block) = comment_body(text);
    let mut markers = Vec::new();
    for (i, line) in body.lines().enumerate() {
        // Continuation lines of a block comment may start with one `*`.
        let line = match (is_block && i > 0, line.trim_start().strip_prefix('*')) {
            (true, Some(rest)) => rest,
            _ => line,
        };
        for token in line.split_whitespace() {
            let Some(inner) = token
                .strip_prefix('[')
                .and_then(|t| t.strip_suffix(']'))
                .filter(|inner| {
                    !inner.is_empty()
                        && inner
                            .chars()
                            .all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '_' || c == '-')
                })
            else {
                return Vec::new();
            };
            markers.push(inner.to_string());
        }
    }
    markers
}

fn dir_of(path: &str) -> String {
    match path.rsplit_once('/') {
        Some((dir, _)) => dir.to_string(),
        None => ".".to_string(),
    }
}

const CSHARP_TEST_ATTRIBUTES: [&str; 7] = [
    "Fact",
    "Theory",
    "Test",
    "TestCase",
    "TestCaseSource",
    "TestMethod",
    "DataTestMethod",
];

/// Extract C# tests (xUnit / NUnit / MSTest) from a `*.cs` file: methods inside
/// types (also through namespaces and `#if` blocks) carrying a test attribute.
/// Markers come from the comment block immediately preceding the method, and
/// `content` runs from the first non-attribute token to the end of the method.
pub fn extract_csharp_tests(path: &str, source: &str) -> Result<Vec<ExtractedTest>, ExtractError> {
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&tree_sitter_c_sharp::LANGUAGE.into())
        .expect("valid c# grammar");
    let tree = parser
        .parse(source, None)
        .ok_or_else(|| ExtractError::Parse {
            path: path.to_string(),
        })?;
    let root = tree.root_node();
    if root.has_error() {
        return Err(ExtractError::Parse {
            path: path.to_string(),
        });
    }
    let base = path.strip_suffix(".cs").unwrap_or(path);
    let mut results = Vec::new();
    walk_csharp_container(root, source, base, &[], &mut results);
    Ok(results)
}

fn walk_csharp_container(
    container: tree_sitter::Node,
    source: &str,
    base: &str,
    types: &[String],
    results: &mut Vec<ExtractedTest>,
) {
    let mut cursor = container.walk();
    let children: Vec<tree_sitter::Node> = container.children(&mut cursor).collect();
    for (idx, node) in children.iter().enumerate() {
        match node.kind() {
            "preproc_if" | "preproc_elif" | "preproc_else" => {
                walk_csharp_container(*node, source, base, types, results);
            }
            "namespace_declaration" => {
                if let Some(body) = node.child_by_field_name("body") {
                    walk_csharp_container(body, source, base, types, results);
                }
            }
            "class_declaration"
            | "struct_declaration"
            | "record_declaration"
            | "interface_declaration" => {
                let (Some(name_node), Some(body)) = (
                    node.child_by_field_name("name"),
                    node.child_by_field_name("body"),
                ) else {
                    continue;
                };
                let name = name_node.utf8_text(source.as_bytes()).unwrap_or("");
                let mut new_types = types.to_vec();
                new_types.push(name.to_string());
                walk_csharp_container(body, source, base, &new_types, results);
            }
            "method_declaration" if !types.is_empty() => {
                if !csharp_is_test_method(*node, source) {
                    continue;
                }
                let Some(name_node) = node.child_by_field_name("name") else {
                    continue;
                };
                let name = name_node
                    .utf8_text(source.as_bytes())
                    .unwrap_or("")
                    .to_string();
                let mut c = node.walk();
                let Some(first) = node
                    .children(&mut c)
                    .find(|ch| ch.kind() != "attribute_list" && ch.kind() != "comment")
                else {
                    continue;
                };
                let content = source[first.start_byte()..node.end_byte()].to_string();
                let markers = collect_preceding_markers(&children, idx, source, "comment");
                results.push(ExtractedTest {
                    id: format!("{base}:{}:{name}", types.join("/")),
                    name,
                    markers,
                    content,
                });
            }
            _ => {}
        }
    }
}

fn csharp_is_test_method(method: tree_sitter::Node, source: &str) -> bool {
    let mut cursor = method.walk();
    for list in method.children(&mut cursor) {
        if list.kind() != "attribute_list" {
            continue;
        }
        let mut c2 = list.walk();
        for attr in list.children(&mut c2) {
            if attr.kind() != "attribute" {
                continue;
            }
            let Some(mut name) = attr.child_by_field_name("name") else {
                continue;
            };
            while name.kind() == "qualified_name" {
                match name.child_by_field_name("name") {
                    Some(n) => name = n,
                    None => break,
                }
            }
            let text = name.utf8_text(source.as_bytes()).unwrap_or("");
            if CSHARP_TEST_ATTRIBUTES.contains(&text) {
                return true;
            }
        }
    }
    false
}

const TYPESCRIPT_TEST_MODIFIERS: [&str; 6] =
    ["skip", "only", "todo", "fails", "concurrent", "sequential"];
const TYPESCRIPT_DESCRIBE_MODIFIERS: [&str; 6] = [
    "skip",
    "only",
    "todo",
    "concurrent",
    "sequential",
    "shuffle",
];

/// File extensions (without the dot) of TypeScript / JavaScript (Vitest)
/// test files: `<name>.test.<ext>` / `<name>.spec.<ext>`.
pub const TYPESCRIPT_TEST_EXTENSIONS: [&str; 8] =
    ["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs"];

/// Whether `path` is a TypeScript / JavaScript (Vitest) test file, i.e. ends
/// with `.test.<ext>` or `.spec.<ext>` for one of the supported extensions.
pub fn is_typescript_test_file(path: &str) -> bool {
    TYPESCRIPT_TEST_EXTENSIONS.iter().any(|ext| {
        path.ends_with(&format!(".test.{ext}")) || path.ends_with(&format!(".spec.{ext}"))
    })
}

/// Extract TypeScript / JavaScript (Vitest) tests from a `*.test.<ext>` /
/// `*.spec.<ext>` file (see [`TYPESCRIPT_TEST_EXTENSIONS`]): `it` / `test`
/// call statements at the top level or directly inside `describe` callbacks.
/// Markers come from the comment block immediately preceding the call, and
/// `content` is the whole call statement.
pub fn extract_typescript_tests(
    path: &str,
    source: &str,
) -> Result<Vec<ExtractedTest>, ExtractError> {
    let mut parser = tree_sitter::Parser::new();
    let lang: tree_sitter::Language = if [".ts", ".mts", ".cts"]
        .iter()
        .any(|ext| path.ends_with(ext))
    {
        tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()
    } else {
        tree_sitter_typescript::LANGUAGE_TSX.into()
    };
    parser
        .set_language(&lang)
        .expect("valid typescript grammar");
    let tree = parser
        .parse(source, None)
        .ok_or_else(|| ExtractError::Parse {
            path: path.to_string(),
        })?;
    let root = tree.root_node();
    if root.has_error() {
        return Err(ExtractError::Parse {
            path: path.to_string(),
        });
    }
    let base = TYPESCRIPT_TEST_EXTENSIONS
        .iter()
        .find_map(|ext| path.strip_suffix(&format!(".{ext}")))
        .unwrap_or(path);
    let mut results = Vec::new();
    walk_typescript_statements(root, source, base, &[], &mut results);
    Ok(results)
}

/// Returns the base identifier and the property chain of a callee made only
/// of identifiers and member accesses (`test.concurrent.only`).
fn typescript_callee_chain<'a>(
    callee: tree_sitter::Node,
    source: &'a str,
) -> Option<(&'a str, Vec<&'a str>)> {
    let mut props = Vec::new();
    let mut cur = callee;
    loop {
        match cur.kind() {
            "identifier" => {
                props.reverse();
                return Some((cur.utf8_text(source.as_bytes()).ok()?, props));
            }
            "member_expression" => {
                let mut c = cur.walk();
                if cur.children(&mut c).any(|ch| ch.kind() == "optional_chain") {
                    return None;
                }
                let prop = cur.child_by_field_name("property")?;
                if prop.kind() != "property_identifier" {
                    return None;
                }
                props.push(prop.utf8_text(source.as_bytes()).ok()?);
                cur = cur.child_by_field_name("object")?;
            }
            _ => return None,
        }
    }
}

/// First-argument name: a plain string or a substitution-free template
/// string, sliced from the source without interpreting escapes.
fn typescript_name_arg<'a>(args: tree_sitter::Node, source: &'a str) -> Option<&'a str> {
    let first = args.named_child(0)?;
    match first.kind() {
        "string" => {}
        "template_string" => {
            let mut c = first.walk();
            if first
                .children(&mut c)
                .any(|ch| ch.kind() == "template_substitution")
            {
                return None;
            }
        }
        _ => return None,
    }
    let text = first.utf8_text(source.as_bytes()).ok()?;
    if text.len() < 2 {
        return None;
    }
    let name = &text[1..text.len() - 1];
    if !is_valid_test_name(name) {
        return None;
    }
    Some(name)
}

fn walk_typescript_statements(
    container: tree_sitter::Node,
    source: &str,
    base: &str,
    describes: &[String],
    results: &mut Vec<ExtractedTest>,
) {
    let mut cursor = container.walk();
    let children: Vec<tree_sitter::Node> = container.children(&mut cursor).collect();
    for (idx, node) in children.iter().enumerate() {
        if node.kind() != "expression_statement" {
            continue;
        }
        let Some(call) = node.named_child(0) else {
            continue;
        };
        if call.kind() != "call_expression" {
            continue;
        }
        let (Some(callee), Some(args)) = (
            call.child_by_field_name("function"),
            call.child_by_field_name("arguments"),
        ) else {
            continue;
        };
        let Some((head, props)) = typescript_callee_chain(callee, source) else {
            continue;
        };
        let Some(name) = typescript_name_arg(args, source) else {
            continue;
        };
        if (head == "test" || head == "it")
            && props.iter().all(|p| TYPESCRIPT_TEST_MODIFIERS.contains(p))
        {
            let markers = collect_preceding_markers(&children, idx, source, "comment");
            let content = node.utf8_text(source.as_bytes()).unwrap_or("").to_string();
            results.push(ExtractedTest {
                id: format!("{base}:{}:{name}", describes.join("/")),
                name: name.to_string(),
                markers,
                content,
            });
        } else if head == "describe"
            && props
                .iter()
                .all(|p| TYPESCRIPT_DESCRIBE_MODIFIERS.contains(p))
        {
            let Some(cb) = args.named_child(1) else {
                continue;
            };
            if cb.kind() != "arrow_function" && cb.kind() != "function_expression" {
                continue;
            }
            let Some(body) = cb.child_by_field_name("body") else {
                continue;
            };
            if body.kind() != "statement_block" {
                continue;
            }
            let mut new_desc = describes.to_vec();
            new_desc.push(name.to_string());
            walk_typescript_statements(body, source, base, &new_desc, results);
        }
    }
}

// ---- Dart / Flutter (package:test / flutter_test) ----

/// Whether `path` is a Dart test file, i.e. ends with `_test.dart`.
pub fn is_dart_test_file(path: &str) -> bool {
    path.ends_with("_test.dart")
}

/// Dart doc comments (`///`, `/** */`) are never markers and do not end the
/// adjacent comment block. Decided by text so `////`, `/***` and `/**/` stay
/// ordinary comments (as for Rust).
fn is_dart_doc_comment(text: &str) -> bool {
    (text.starts_with("///") && !text.starts_with("////"))
        || (text.starts_with("/**") && !text.starts_with("/***") && !text.starts_with("/**/"))
}

/// Extract Dart (`package:test` / `flutter_test`) tests from a `*_test.dart`
/// file: `test` / `testWidgets` call statements directly inside `main` or
/// inside the callback of a `group` that is itself reachable that way.
/// Markers come from the comment block immediately preceding the call, and
/// `content` is the whole call statement.
pub fn extract_dart_tests(path: &str, source: &str) -> Result<Vec<ExtractedTest>, ExtractError> {
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&tree_sitter_dart::LANGUAGE.into())
        .expect("valid dart grammar");
    let tree = parser
        .parse(source, None)
        .ok_or_else(|| ExtractError::Parse {
            path: path.to_string(),
        })?;
    let root = tree.root_node();
    if root.has_error() {
        return Err(ExtractError::Parse {
            path: path.to_string(),
        });
    }
    let base = path.strip_suffix(".dart").unwrap_or(path);
    let mut results = Vec::new();
    let mut cursor = root.walk();
    for decl in root.children(&mut cursor) {
        if decl.kind() != "function_declaration" {
            continue;
        }
        let is_main = decl
            .child_by_field_name("signature")
            .and_then(|s| s.child_by_field_name("name"))
            .and_then(|n| n.utf8_text(source.as_bytes()).ok())
            == Some("main");
        if !is_main {
            continue;
        }
        let Some(block) = decl
            .child_by_field_name("body")
            .and_then(|b| b.named_child(0))
            .filter(|b| b.kind() == "block")
        else {
            continue;
        };
        walk_dart_statements(block, source, base, &[], &mut results);
    }
    Ok(results)
}

/// Positional arguments of a Dart `arguments` node (named arguments such as
/// `skip:` are left out).
fn dart_positional_args(args: tree_sitter::Node) -> Vec<tree_sitter::Node> {
    let mut cursor = args.walk();
    args.named_children(&mut cursor)
        .filter(|n| n.kind() != "named_argument" && n.kind() != "comment")
        .collect()
}

/// Name from a single-line (possibly raw, possibly adjacent-concatenated)
/// `string_literal`: the source text inside each part's quotes, joined. Strings
/// with interpolation or triple quotes yield `None`; escapes are not
/// interpreted.
fn dart_string_name(lit: tree_sitter::Node, source: &str) -> Option<String> {
    if lit.kind() != "string_literal" {
        return None;
    }
    let mut name = String::new();
    let mut cursor = lit.walk();
    let mut parts = 0;
    for part in lit.children(&mut cursor) {
        let prefix = match part.kind() {
            "string_literal_single_quotes" | "string_literal_double_quotes" => 1,
            "raw_string_literal_single_quotes" | "raw_string_literal_double_quotes" => 2,
            "comment" => continue,
            _ => return None,
        };
        let mut c = part.walk();
        if part
            .children(&mut c)
            .any(|ch| ch.kind() == "template_substitution")
        {
            return None;
        }
        let text = part.utf8_text(source.as_bytes()).ok()?;
        if text.len() < prefix + 1 {
            return None;
        }
        name.push_str(text.get(prefix..text.len() - 1)?);
        parts += 1;
    }
    if parts == 0 || !is_valid_test_name(&name) {
        return None;
    }
    Some(name)
}

fn walk_dart_statements(
    block: tree_sitter::Node,
    source: &str,
    base: &str,
    groups: &[String],
    results: &mut Vec<ExtractedTest>,
) {
    let mut cursor = block.walk();
    let children: Vec<tree_sitter::Node> = block.children(&mut cursor).collect();
    for (idx, node) in children.iter().enumerate() {
        if node.kind() != "expression_statement" {
            continue;
        }
        let Some(call) = node.named_child(0) else {
            continue;
        };
        if call.kind() != "call_expression" {
            continue;
        }
        let (Some(callee), Some(args)) = (
            call.child_by_field_name("function"),
            call.child_by_field_name("arguments"),
        ) else {
            continue;
        };
        if callee.kind() != "identifier" {
            continue;
        }
        let head = callee.utf8_text(source.as_bytes()).unwrap_or("");
        if !matches!(head, "test" | "testWidgets" | "group") {
            continue;
        }
        let positional = dart_positional_args(args);
        let Some(name) = positional
            .first()
            .and_then(|a| dart_string_name(*a, source))
        else {
            continue;
        };
        if head == "group" {
            let Some(cb) = positional
                .get(1)
                .filter(|cb| cb.kind() == "function_expression")
            else {
                continue;
            };
            let Some(inner) = cb
                .child_by_field_name("body")
                .filter(|b| b.kind() == "function_expression_body")
                .and_then(|b| b.named_child(0))
                .filter(|b| b.kind() == "block")
            else {
                continue;
            };
            let mut new_groups = groups.to_vec();
            new_groups.push(name);
            walk_dart_statements(inner, source, base, &new_groups, results);
        } else {
            let markers = collect_preceding_markers_with(
                &children,
                idx,
                source,
                &|n| n.utf8_text(source.as_bytes()).ok(),
                &["comment"],
                &|n| {
                    n.utf8_text(source.as_bytes())
                        .is_ok_and(is_dart_doc_comment)
                },
            );
            let content = node.utf8_text(source.as_bytes()).unwrap_or("").to_string();
            results.push(ExtractedTest {
                id: format!("{base}:{}:{name}", groups.join("/")),
                name,
                markers,
                content,
            });
        }
    }
}

// ---- Java / Kotlin (JUnit) ----

/// Annotation names (last identifier, qualifiers ignored) marking a JUnit
/// style test method / function in Java and Kotlin.
const JVM_TEST_ANNOTATIONS: [&str; 5] = [
    "Test",
    "ParameterizedTest",
    "RepeatedTest",
    "TestFactory",
    "TestTemplate",
];

const JVM_COMMENT_KINDS: [&str; 2] = ["line_comment", "block_comment"];

/// Start byte of the hashed range: the first token of the declaration that is
/// neither an annotation nor a comment (annotations / comments that lead the
/// modifiers and the comments right after them are skipped).
fn jvm_content_start(decl: tree_sitter::Node) -> Option<usize> {
    fn skip(kind: &str) -> bool {
        matches!(
            kind,
            "annotation" | "marker_annotation" | "line_comment" | "block_comment"
        )
    }
    let mut cursor = decl.walk();
    for child in decl.children(&mut cursor) {
        if child.kind() == "modifiers" {
            let mut c2 = child.walk();
            let first = child.children(&mut c2).find(|n| !skip(n.kind()));
            if let Some(first) = first {
                return Some(first.start_byte());
            }
        } else if !skip(child.kind()) {
            return Some(child.start_byte());
        }
    }
    None
}

fn jvm_name_is_test(name: &str) -> bool {
    JVM_TEST_ANNOTATIONS.contains(&name)
}

// -- Java --

/// Extract Java (JUnit) tests: methods annotated with one of
/// [`JVM_TEST_ANNOTATIONS`] directly inside class / record / interface / enum
/// bodies. Markers come from the `//` / `/* */` comment block immediately
/// preceding the method; `content` starts at the first non-annotation token.
pub fn extract_java_tests(path: &str, source: &str) -> Result<Vec<ExtractedTest>, ExtractError> {
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&tree_sitter_java::LANGUAGE.into())
        .expect("valid java grammar");
    let tree = parser
        .parse(source, None)
        .ok_or_else(|| ExtractError::Parse {
            path: path.to_string(),
        })?;
    let root = tree.root_node();
    if root.has_error() {
        return Err(ExtractError::Parse {
            path: path.to_string(),
        });
    }
    let base = path.strip_suffix(".java").unwrap_or(path);
    let mut results = Vec::new();
    walk_java_container(root, source, base, &[], &mut results);
    Ok(results)
}

fn walk_java_container(
    container: tree_sitter::Node,
    source: &str,
    base: &str,
    types: &[String],
    results: &mut Vec<ExtractedTest>,
) {
    let mut cursor = container.walk();
    let children: Vec<tree_sitter::Node> = container.children(&mut cursor).collect();
    for (idx, node) in children.iter().enumerate() {
        match node.kind() {
            "class_declaration"
            | "record_declaration"
            | "interface_declaration"
            | "enum_declaration" => {
                let (Some(name_node), Some(body)) = (
                    node.child_by_field_name("name"),
                    node.child_by_field_name("body"),
                ) else {
                    continue;
                };
                let name = name_node.utf8_text(source.as_bytes()).unwrap_or("");
                let mut new_types = types.to_vec();
                new_types.push(name.to_string());
                if body.kind() == "enum_body" {
                    let mut c = body.walk();
                    let decls: Vec<_> = body
                        .children(&mut c)
                        .filter(|n| n.kind() == "enum_body_declarations")
                        .collect();
                    for d in decls {
                        walk_java_container(d, source, base, &new_types, results);
                    }
                } else {
                    walk_java_container(body, source, base, &new_types, results);
                }
            }
            "method_declaration" if !types.is_empty() => {
                if !java_is_test_method(*node, source) {
                    continue;
                }
                let Some(name_node) = node.child_by_field_name("name") else {
                    continue;
                };
                let Some(start) = jvm_content_start(*node) else {
                    continue;
                };
                let name = name_node
                    .utf8_text(source.as_bytes())
                    .unwrap_or("")
                    .to_string();
                results.push(ExtractedTest {
                    id: format!("{base}:{}:{name}", types.join("/")),
                    name,
                    markers: collect_preceding_markers_with(
                        &children,
                        idx,
                        source,
                        &|n| n.utf8_text(source.as_bytes()).ok(),
                        &JVM_COMMENT_KINDS,
                        &|n| n.utf8_text(source.as_bytes()).is_ok_and(is_doc_comment),
                    ),
                    content: source[start..node.end_byte()].to_string(),
                });
            }
            _ => {}
        }
    }
}

fn java_is_test_method(method: tree_sitter::Node, source: &str) -> bool {
    let mut cursor = method.walk();
    let Some(modifiers) = method
        .children(&mut cursor)
        .find(|n| n.kind() == "modifiers")
    else {
        return false;
    };
    let mut c2 = modifiers.walk();
    for ann in modifiers.children(&mut c2) {
        if ann.kind() != "marker_annotation" && ann.kind() != "annotation" {
            continue;
        }
        let Some(mut name) = ann.child_by_field_name("name") else {
            continue;
        };
        while name.kind() == "scoped_identifier" {
            match name.child_by_field_name("name") {
                Some(n) => name = n,
                None => break,
            }
        }
        if jvm_name_is_test(name.utf8_text(source.as_bytes()).unwrap_or("")) {
            return true;
        }
    }
    false
}

// -- Kotlin --

/// The Kotlin grammar rejects identifiers with many non-Latin characters.
/// The source is therefore parsed as an "ASCII shadow": every non-ASCII
/// code point becomes one ASCII character (`' '` for whitespace and U+FEFF,
/// `'x'` otherwise). `map` records `(shadow_offset, original_offset)` pairs at
/// every point where the two offsets diverge, so shadow byte ranges can be
/// mapped back to the original source.
struct KotlinShadow<'a> {
    original: &'a str,
    shadow: String,
    map: Vec<(usize, usize)>,
}

impl<'a> KotlinShadow<'a> {
    fn new(original: &'a str) -> Self {
        let mut shadow = String::with_capacity(original.len());
        let mut map = vec![(0usize, 0usize)];
        for (off, ch) in original.char_indices() {
            if ch.is_ascii() {
                shadow.push(ch);
            } else {
                shadow.push(if ch.is_whitespace() || ch == '\u{feff}' {
                    ' '
                } else {
                    'x'
                });
                map.push((shadow.len(), off + ch.len_utf8()));
            }
        }
        KotlinShadow {
            original,
            shadow,
            map,
        }
    }

    /// Replaces the given shadow byte ranges with spaces (newlines kept).
    fn blank_ranges(&mut self, ranges: &[(usize, usize)]) {
        let mut bytes = std::mem::take(&mut self.shadow).into_bytes();
        for &(a, b) in ranges {
            for byte in &mut bytes[a..b] {
                if *byte != b'\n' {
                    *byte = b' ';
                }
            }
        }
        self.shadow = String::from_utf8(bytes).expect("ascii shadow");
    }

    fn to_original(&self, shadow_offset: usize) -> usize {
        let i = self.map.partition_point(|&(s, _)| s <= shadow_offset) - 1;
        let (s, o) = self.map[i];
        o + (shadow_offset - s)
    }

    fn slice(&self, start: usize, end: usize) -> Option<&'a str> {
        self.original
            .get(self.to_original(start)..self.to_original(end))
    }

    fn node_text(&self, node: tree_sitter::Node) -> Option<&'a str> {
        self.slice(node.start_byte(), node.end_byte())
    }
}

/// Extract Kotlin (JUnit) tests: functions annotated with one of
/// [`JVM_TEST_ANNOTATIONS`] at the top level or directly inside class /
/// object / companion object bodies. Parsing uses an ASCII shadow of the
/// source (see [`KotlinShadow`]); names and `content` come from the original.
pub fn extract_kotlin_tests(path: &str, source: &str) -> Result<Vec<ExtractedTest>, ExtractError> {
    kotlin_extract(path, source, true)
}

/// Top-level node kinds the Kotlin grammar may legitimately produce under
/// `source_file`. Anything else (e.g. a leftover `annotated_expression`)
/// means the grammar failed to recognise a declaration.
const KOTLIN_ROOT_KINDS: [&str; 11] = [
    "shebang",
    "file_annotation",
    "package_header",
    "import",
    "class_declaration",
    "object_declaration",
    "function_declaration",
    "property_declaration",
    "type_alias",
    "line_comment",
    "block_comment",
];

/// `retry_annotated_expression` enables the class-annotation blanking
/// workaround (disabled only by tests of the fallback error path).
fn kotlin_extract(
    path: &str,
    source: &str,
    retry_annotated_expression: bool,
) -> Result<Vec<ExtractedTest>, ExtractError> {
    let parse_err = || ExtractError::Parse {
        path: path.to_string(),
    };
    let mut shadow = KotlinShadow::new(source);
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&tree_sitter_kotlin_ng::LANGUAGE.into())
        .expect("valid kotlin grammar");
    let mut tree = parser.parse(&shadow.shadow, None).ok_or_else(parse_err)?;
    if tree.root_node().has_error() {
        return Err(parse_err());
    }
    // Grammar bugs that leave an `annotated_expression` at file level with no
    // error: (1) a class with 2+ annotations, no supertype and an
    // `@Annotation inner class` in its body; (2) an annotation with
    // positional arguments (`@S("L")`) after a top-level declaration and a
    // comment. The annotations are not needed for extraction, so blank them
    // out and parse again.
    if retry_annotated_expression && kotlin_has_annotated_expression(tree.root_node()) {
        let ranges = kotlin_annotated_expression_ranges(tree.root_node());
        drop(tree);
        shadow.blank_ranges(&ranges);
        tree = parser.parse(&shadow.shadow, None).ok_or_else(parse_err)?;
        if tree.root_node().has_error() {
            return Err(parse_err());
        }
    }
    let root = tree.root_node();
    // A structure other than declarations at file level means the grammar
    // missed a declaration: never silently report zero tests.
    let mut cursor = root.walk();
    let unexpected = root
        .children(&mut cursor)
        .any(|n| !KOTLIN_ROOT_KINDS.contains(&n.kind()));
    if unexpected {
        return Err(parse_err());
    }
    let base = path.strip_suffix(".kt").unwrap_or(path);
    let mut results = Vec::new();
    walk_kotlin_container(root, &shadow, base, &[], &mut results).map_err(|()| parse_err())?;
    Ok(results)
}

fn kotlin_has_annotated_expression(root: tree_sitter::Node) -> bool {
    let mut cursor = root.walk();
    let found = root
        .children(&mut cursor)
        .any(|n| n.kind() == "annotated_expression");
    found
}

/// Byte ranges to blank for each file-level `annotated_expression`. With
/// positional arguments (`@S("L")`) the grammar splits the annotation into an
/// `annotation` (`@S`) and a `parenthesized_expression` (`("L")`); only the
/// latter is blanked, which leaves a plain annotation that parses as part of
/// the declaration's modifiers (so marker adjacency still sees its first
/// row). Otherwise (the class case) the `annotation` children are blanked. The
/// expression as a whole is never blanked: in the class case it spans the
/// entire class body.
fn kotlin_annotated_expression_ranges(root: tree_sitter::Node) -> Vec<(usize, usize)> {
    let mut ranges = Vec::new();
    let mut cursor = root.walk();
    for expr in root.children(&mut cursor) {
        if expr.kind() != "annotated_expression" {
            continue;
        }
        let mut c2 = expr.walk();
        let children: Vec<tree_sitter::Node> = expr.children(&mut c2).collect();
        let args: Vec<_> = children
            .windows(2)
            .filter(|w| w[0].kind() == "annotation" && w[1].kind() == "parenthesized_expression")
            .map(|w| &w[1])
            .collect();
        let picked: Vec<_> = if args.is_empty() {
            children
                .iter()
                .filter(|c| c.kind() == "annotation")
                .collect()
        } else {
            args
        };
        ranges.extend(picked.iter().map(|n| (n.start_byte(), n.end_byte())));
    }
    ranges
}

fn kotlin_ident(text: &str) -> String {
    text.strip_prefix('`')
        .and_then(|t| t.strip_suffix('`'))
        .unwrap_or(text)
        .to_string()
}

fn walk_kotlin_container(
    container: tree_sitter::Node,
    shadow: &KotlinShadow,
    base: &str,
    types: &[String],
    results: &mut Vec<ExtractedTest>,
) -> Result<(), ()> {
    let mut cursor = container.walk();
    let children: Vec<tree_sitter::Node> = container.children(&mut cursor).collect();
    for (idx, node) in children.iter().enumerate() {
        match node.kind() {
            "class_declaration" | "object_declaration" | "companion_object" => {
                let name = match node.child_by_field_name("name") {
                    Some(n) => match shadow.node_text(n) {
                        Some(t) => kotlin_ident(t),
                        None => {
                            debug_assert!(false, "kotlin name range off a char boundary");
                            return Err(());
                        }
                    },
                    None if node.kind() == "companion_object" => "Companion".to_string(),
                    None => continue,
                };
                let mut c = node.walk();
                let Some(body) = node
                    .children(&mut c)
                    .find(|n| matches!(n.kind(), "class_body" | "enum_class_body"))
                else {
                    continue;
                };
                let mut new_types = types.to_vec();
                new_types.push(name);
                walk_kotlin_container(body, shadow, base, &new_types, results)?;
            }
            "function_declaration" => {
                if !kotlin_is_test_function(*node, shadow) {
                    continue;
                }
                let Some(name_node) = node.child_by_field_name("name") else {
                    continue;
                };
                let Some(name_text) = shadow.node_text(name_node) else {
                    debug_assert!(false, "kotlin name range off a char boundary");
                    return Err(());
                };
                let name = kotlin_ident(name_text);
                if !is_valid_test_name(&name) {
                    continue;
                }
                let Some(start) = jvm_content_start(*node) else {
                    continue;
                };
                let Some(content) = shadow.slice(start, node.end_byte()) else {
                    debug_assert!(false, "kotlin content range off a char boundary");
                    return Err(());
                };
                results.push(ExtractedTest {
                    id: format!("{base}:{}:{name}", types.join("/")),
                    name,
                    // Markers are read from the original source: the shadow
                    // turns `[正常系]` into a bogus `[xxx]`.
                    markers: collect_preceding_markers_with(
                        &children,
                        idx,
                        &shadow.shadow,
                        &|n| shadow.node_text(n),
                        &JVM_COMMENT_KINDS,
                        &|n| shadow.node_text(n).is_some_and(is_doc_comment),
                    ),
                    content: content.to_string(),
                });
            }
            _ => {}
        }
    }
    Ok(())
}

fn kotlin_is_test_function(func: tree_sitter::Node, shadow: &KotlinShadow) -> bool {
    let mut cursor = func.walk();
    let Some(modifiers) = func.children(&mut cursor).find(|n| n.kind() == "modifiers") else {
        return false;
    };
    let mut c2 = modifiers.walk();
    let found = modifiers
        .children(&mut c2)
        .filter(|n| n.kind() == "annotation")
        .any(|ann| kotlin_annotation_has_test(ann, shadow));
    found
}

/// Whether any `user_type` under the annotation (outside argument lists) ends
/// in a test annotation name.
fn kotlin_annotation_has_test(node: tree_sitter::Node, shadow: &KotlinShadow) -> bool {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "user_type" => {
                let mut c = child.walk();
                let last = child
                    .children(&mut c)
                    .filter(|n| matches!(n.kind(), "identifier" | "type_identifier"))
                    .last();
                if let Some(last) = last {
                    if jvm_name_is_test(shadow.node_text(last).unwrap_or("")) {
                        return true;
                    }
                }
            }
            "value_arguments" | "use_site_target" => {}
            _ => {
                if kotlin_annotation_has_test(child, shadow) {
                    return true;
                }
            }
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    fn find<'a>(tests: &'a [ExtractedTest], name: &str) -> &'a ExtractedTest {
        tests
            .iter()
            .find(|t| t.name == name)
            .unwrap_or_else(|| panic!("test named {name} not found in {tests:?}"))
    }

    // [FR-012]
    #[test]
    fn goは直前に隣接するコメントをマーカーとして抽出する() {
        let source = r#"package auth

// [FR-001]
func TestRejectsShortPassword(t *testing.T) {
    doSomething()
}
"#;
        let tests = extract_go_tests("internal/auth/login_test.go", source).unwrap();
        let t = find(&tests, "TestRejectsShortPassword");
        assert_eq!(t.markers, vec!["FR-001".to_string()]);
    }

    // [FR-012]
    #[test]
    fn goは空行を挟んだコメントや欠落をマーカーなしとして無視する() {
        let source = r#"package auth

// [FR-002]

func TestWithBlankLine(t *testing.T) {
}

func TestNoComment(t *testing.T) {
}
"#;
        let tests = extract_go_tests("internal/auth/login_test.go", source).unwrap();
        assert!(find(&tests, "TestWithBlankLine").markers.is_empty());
        assert!(find(&tests, "TestNoComment").markers.is_empty());
    }

    // [FR-013]
    #[test]
    fn goのtest識別子はディレクトリと関数名を組み合わせた形式になる() {
        let source = "package auth\n\n// [FR-001]\nfunc TestLogin(t *testing.T) {}\n";
        let tests = extract_go_tests("internal/auth/login_test.go", source).unwrap();
        assert_eq!(find(&tests, "TestLogin").id, "internal/auth:TestLogin");
    }

    // [FR-013]
    #[test]
    fn goのtest識別子はリポジトリ直下ならドットになる() {
        let source = "package main\n\n// [FR-001]\nfunc TestRoot(t *testing.T) {}\n";
        let tests = extract_go_tests("login_test.go", source).unwrap();
        assert_eq!(find(&tests, "TestRoot").id, ".:TestRoot");
    }

    // [FR-003]
    #[test]
    fn rustの関数本体の変更はcontentに反映される() {
        let base = r#"// [FR-001]
#[test]
fn rejects_short_password() {
    assert!(true);
}
"#;
        let changed = r#"// [FR-001]
#[test]
fn rejects_short_password() {
    assert!(false);
}
"#;
        let a = find(
            &extract_rust_tests("src/auth.rs", base, &[]).unwrap(),
            "rejects_short_password",
        )
        .content
        .clone();
        let b = find(
            &extract_rust_tests("src/auth.rs", changed, &[]).unwrap(),
            "rejects_short_password",
        )
        .content
        .clone();
        assert_ne!(a, b);
        assert!(a.contains("fn rejects_short_password"));
        assert!(a.contains("assert!(true)"));
    }

    // [FR-003]
    #[test]
    fn goの関数本体の変更はcontentに反映される() {
        let base = "package auth\n\n// [FR-001]\nfunc TestRejectsShortPassword(t *testing.T) {\n\tassertTrue(t, true)\n}\n";
        let changed = "package auth\n\n// [FR-001]\nfunc TestRejectsShortPassword(t *testing.T) {\n\tassertTrue(t, false)\n}\n";
        let a = find(
            &extract_go_tests("internal/auth/login_test.go", base).unwrap(),
            "TestRejectsShortPassword",
        )
        .content
        .clone();
        let b = find(
            &extract_go_tests("internal/auth/login_test.go", changed).unwrap(),
            "TestRejectsShortPassword",
        )
        .content
        .clone();
        assert_ne!(a, b);
        assert!(a.contains("func TestRejectsShortPassword"));
        assert!(a.contains("assertTrue(t, true)"));
    }

    // [FR-004]
    #[test]
    fn rustのcontentは外側の属性とマーカーコメントを含まない() {
        let source = r#"// [FR-001]
#[test]
fn rejects_short_password() {
    assert!(true);
}
"#;
        let extracted = extract_rust_tests("src/auth.rs", source, &[]).unwrap();
        let t = find(&extracted, "rejects_short_password");
        assert!(!t.content.contains("#[test]"));
        assert!(!t.content.contains("[FR-001]"));
        assert!(t.content.contains("fn rejects_short_password"));
    }

    fn rs(source: &str, extra: &[&str]) -> Vec<ExtractedTest> {
        let extra: Vec<String> = extra.iter().map(|s| s.to_string()).collect();
        extract_rust_tests("src/auth.rs", source, &extra).unwrap()
    }

    fn has(tests: &[ExtractedTest], name: &str) -> bool {
        tests.iter().any(|t| t.name == name)
    }

    // [FR-015]
    #[test]
    fn rustは属性パスの末尾がtestならtestとして抽出する() {
        let source = r#"// [FR-001]
#[test]
fn plain_test() {}

// [FR-002]
#[tokio::test]
async fn async_test() {}

// [FR-003]
#[async_std::test]
async fn async_std_test() {}

// [FR-004]
#[tokio::test(flavor = "multi_thread")]
async fn multi_thread_test() {}

// [FR-005]
#[ test ]
fn spaced_test() {}
"#;
        let tests = rs(source, &[]);
        for name in [
            "plain_test",
            "async_test",
            "async_std_test",
            "multi_thread_test",
            "spaced_test",
        ] {
            assert!(has(&tests, name), "{name} should be extracted");
        }
        assert_eq!(
            find(&tests, "async_test").markers,
            vec!["FR-002".to_string()]
        );
    }

    // [FR-015]
    #[test]
    fn rustのrstestは追加指定がなければ対象外で指定があれば対象になる() {
        let source = "// [FR-001]\n#[rstest]\nfn t() {}\n";
        assert!(!has(&rs(source, &[]), "t"));
        assert!(has(&rs(source, &["rstest"]), "t"));

        let scoped = "// [FR-001]\n#[rstest::rstest]\nfn t() {}\n";
        assert!(!has(&rs(scoped, &[]), "t"));
        assert!(has(&rs(scoped, &["rstest"]), "t"));
    }

    // [FR-015]
    #[test]
    fn rustのtest_caseとcfg_attrのtestは対象外() {
        let source = r#"// [FR-001]
#[test_case(1)]
fn case_test() {}

// [FR-002]
#[cfg_attr(x, test)]
fn cfg_attr_test() {}
"#;
        let tests = rs(source, &[]);
        assert!(!has(&tests, "case_test"));
        assert!(!has(&tests, "cfg_attr_test"));

        let tests = rs(source, &["test_case"]);
        assert!(has(&tests, "case_test"));
        assert!(!has(&tests, "cfg_attr_test"));
    }

    // [FR-015]
    #[test]
    fn rustのinner_attributeのtestはtestを作らない() {
        let source = "#![test]\n\n// [FR-001]\nfn helper() {}\n";
        assert!(rs(source, &[]).is_empty());
    }

    // [FR-016]
    #[test]
    fn rustは最初の対象属性の直前のコメントをマーカーにする() {
        let source = "// [FR-001]\n#[rstest]\n#[case(1)]\n#[tokio::test]\nfn t() {}\n";
        let tests = rs(source, &["rstest"]);
        assert_eq!(find(&tests, "t").markers, vec!["FR-001".to_string()]);
    }

    // [FR-016]
    #[test]
    fn rustは対象属性の設定によりマーカー位置が変わる() {
        let source = "#[rstest]\n// [FR-001]\n#[tokio::test]\nfn t() {}\n";
        let without = rs(source, &[]);
        assert_eq!(find(&without, "t").markers, vec!["FR-001".to_string()]);
        let with = rs(source, &["rstest"]);
        assert!(find(&with, "t").markers.is_empty());
    }

    // [FR-016]
    #[test]
    fn rustは属性間や属性末尾のコメントがあってもtestとして検出しマーカーを取る() {
        let between = "// [FR-001]\n#[test]\n// note\n#[ignore]\nfn between() {}\n";
        let tests = rs(between, &[]);
        assert_eq!(find(&tests, "between").markers, vec!["FR-001".to_string()]);

        let trailing = "// [FR-001]\n#[test] // trailing\n#[ignore]\nfn trailing() {}\n";
        let tests = rs(trailing, &[]);
        assert_eq!(find(&tests, "trailing").markers, vec!["FR-001".to_string()]);

        let block = "// [FR-001]\n#[test]\n/* block */\n#[ignore]\nfn block() {}\n";
        assert_eq!(
            find(&rs(block, &[]), "block").markers,
            vec!["FR-001".to_string()]
        );
    }

    // [FR-004]
    #[test]
    fn tokioのtest属性はcontentに含まれない() {
        let source = "// [FR-001]\n#[tokio::test]\nasync fn t() { assert!(true); }\n";
        let tests = rs(source, &[]);
        let t = find(&tests, "t");
        assert!(!t.content.contains("tokio::test"));
        assert!(!t.content.contains("#["));
        assert!(t.content.contains("async fn t"));
    }

    // [FR-016]
    #[test]
    fn rustはtest属性の直前に隣接するコメントをマーカーとする() {
        let source = r#"// [FR-001]
#[test]
fn adjacent() {}

// [FR-002]

#[test]
fn blank_line_between() {}

#[test]
fn no_comment() {}
"#;
        let tests = extract_rust_tests("src/auth.rs", source, &[]).unwrap();
        assert_eq!(find(&tests, "adjacent").markers, vec!["FR-001".to_string()]);
        assert!(find(&tests, "blank_line_between").markers.is_empty());
        assert!(find(&tests, "no_comment").markers.is_empty());
    }

    // [FR-017]
    #[test]
    fn rustのtest識別子はトップレベルなら空モジュールパスになる() {
        let source = "// [FR-001]\n#[test]\nfn roundtrip() {}\n";
        let tests = extract_rust_tests("src/lock.rs", source, &[]).unwrap();
        assert_eq!(find(&tests, "roundtrip").id, "src/lock::roundtrip");
    }

    // [FR-017]
    #[test]
    fn rustのtest識別子はinline_modを外側から内側の順に連結する() {
        let source = r#"mod tests {
    // [FR-001]
    #[test]
    fn roundtrip() {}
}
"#;
        let tests = extract_rust_tests("src/lock.rs", source, &[]).unwrap();
        assert_eq!(find(&tests, "roundtrip").id, "src/lock:tests:roundtrip");
    }

    // [FR-017]
    #[test]
    fn rustのtest識別子はネストしたinline_modをスラッシュで連結する() {
        let source = r#"mod outer {
    mod inner {
        // [FR-001]
        #[test]
        fn t() {}
    }
}
"#;
        let tests = extract_rust_tests("src/x.rs", source, &[]).unwrap();
        assert_eq!(find(&tests, "t").id, "src/x:outer/inner:t");
    }

    // [FR-016]
    #[test]
    fn rustは属性が間に挟まるとtestの直前のコメントをマーカーにしない() {
        let source = r#"// [FR-001]
#[cfg(feature = "x")]
#[test]
fn not_adjacent() {}
"#;
        let tests = extract_rust_tests("src/auth.rs", source, &[]).unwrap();
        assert!(find(&tests, "not_adjacent").markers.is_empty());
    }

    // [FR-016]
    #[test]
    fn rustは他の属性の後ろでもtestの直前のコメントはマーカーになる() {
        let source = r#"#[cfg(feature = "x")]
// [FR-001]
#[test]
fn adjacent_after_other_attr() {}
"#;
        let tests = extract_rust_tests("src/auth.rs", source, &[]).unwrap();
        assert_eq!(
            find(&tests, "adjacent_after_other_attr").markers,
            vec!["FR-001".to_string()]
        );
    }

    // [FR-016]
    #[test]
    fn rustはtestとfnの間にコメントがあってもtestとして検出しマーカーを採用する() {
        let source = r#"// [FR-001]
#[test]
// 補足
fn t() {}
"#;
        let tests = extract_rust_tests("src/auth.rs", source, &[]).unwrap();
        let found = find(&tests, "t");
        assert_eq!(found.markers, vec!["FR-001".to_string()]);
        assert!(!found.content.contains("補足"));
    }

    // ---- C# ----

    fn cs(source: &str) -> Vec<ExtractedTest> {
        extract_csharp_tests("tests/AuthTests.cs", source).unwrap()
    }

    fn cs_wrap(method: &str) -> String {
        format!("public class AuthTests\n{{\n{method}\n}}\n")
    }

    // [FR-049]
    #[test]
    fn csharpはif_endif内のtestメソッドも検出する() {
        let source = "public class A {\n#if DEBUG\n    // [FR-001]\n    [Fact]\n    public void T() { }\n#endif\n}\n";
        let tests = extract_csharp_tests("tests/A.cs", source).unwrap();
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].id, "tests/A:A:T");
        assert_eq!(tests[0].markers, vec!["FR-001".to_string()]);
    }

    // [FR-049]
    #[test]
    fn csharpは7種類の属性をそれぞれtestとして検出する() {
        for attr in [
            "Fact",
            "Theory",
            "Test",
            "TestCase",
            "TestCaseSource",
            "TestMethod",
            "DataTestMethod",
        ] {
            let source = cs_wrap(&format!("    [{attr}]\n    public void T() {{ }}"));
            let tests = cs(&source);
            assert_eq!(tests.len(), 1, "attribute {attr} should be detected");
            assert_eq!(tests[0].name, "T");
        }
    }

    // [FR-049]
    #[test]
    fn csharpはattribute接尾辞付きや無関係な属性のみのメソッドを検出しない() {
        let source = cs_wrap(
            "    [FactAttribute]\n    public void A() { }\n\n    [Obsolete]\n    public void B() { }\n\n    public void C() { }\n\n    [Category(\"x\")]\n    public void D() { }",
        );
        assert!(cs(&source).is_empty());
    }

    // [FR-049]
    #[test]
    fn csharpは引数付きの属性も検出する() {
        let source = cs_wrap("    [Fact(Skip = \"x\")]\n    public void T() { }");
        assert_eq!(cs(&source).len(), 1);
    }

    // [FR-049]
    #[test]
    fn csharpは名前空間修飾された属性も検出する() {
        let source = cs_wrap(
            "    [Xunit.Fact]\n    public void A() { }\n\n    [global::Xunit.Fact]\n    public void B() { }",
        );
        let tests = cs(&source);
        assert_eq!(tests.len(), 2);
        find(&tests, "A");
        find(&tests, "B");
    }

    // [FR-049]
    #[test]
    fn csharpはtarget指定付きの属性も検出する() {
        let source = cs_wrap("    [method: Fact]\n    public void T() { }");
        assert_eq!(cs(&source).len(), 1);
    }

    // [FR-049]
    #[test]
    fn csharpは同一属性リスト内の他の属性と並んだ属性も検出する() {
        let source = cs_wrap("    [Trait(\"a\", \"b\"), Fact]\n    public void T() { }");
        assert_eq!(cs(&source).len(), 1);
    }

    // [FR-049]
    #[test]
    fn csharpは複数の属性リストの順序に関わらず検出する() {
        let source = cs_wrap(
            "    [Theory]\n    [InlineData(1)]\n    public void A(int x) { }\n\n    [InlineData(1)]\n    [Theory]\n    public void B(int x) { }",
        );
        let tests = cs(&source);
        assert_eq!(tests.len(), 2);
        find(&tests, "A");
        find(&tests, "B");
    }

    // [FR-050]
    #[test]
    fn csharpは直前に隣接するコメントをマーカーとして抽出し空行を挟むと無視する() {
        let source = cs_wrap(
            "    // [FR-001]\n    [Fact]\n    public void Adjacent() { }\n\n    // [FR-002]\n\n    [Fact]\n    public void Blank() { }\n\n    [Fact]\n    public void None() { }",
        );
        let tests = cs(&source);
        assert_eq!(find(&tests, "Adjacent").markers, vec!["FR-001".to_string()]);
        assert!(find(&tests, "Blank").markers.is_empty());
        assert!(find(&tests, "None").markers.is_empty());
    }

    // [FR-050]
    #[test]
    fn csharpはドキュメントコメントとブロックコメントもマーカーにできる() {
        let source = cs_wrap(
            "    /// [FR-001]\n    [Fact]\n    public void Doc() { }\n\n    /* [FR-002] */\n    [Fact]\n    public void Block() { }",
        );
        let tests = cs(&source);
        assert_eq!(find(&tests, "Doc").markers, vec!["FR-001".to_string()]);
        assert_eq!(find(&tests, "Block").markers, vec!["FR-002".to_string()]);
    }

    // [FR-050]
    #[test]
    fn csharpは属性と署名の間のコメントをマーカーにせずcontentにも含めない() {
        let source = cs_wrap(
            "    [Fact]\n    // [FR-001]\n    public void T() { }\n\n    [Fact]\n    [Trait(\"a\", \"b\")]\n    // 補足\n    public void U() { }",
        );
        let tests = cs(&source);
        let t = find(&tests, "T");
        assert!(t.markers.is_empty());
        assert!(!t.content.contains("FR-001"));
        let u = find(&tests, "U");
        assert!(!u.content.contains("補足"));
    }

    // [FR-050]
    #[test]
    fn csharpは属性リストの間のコメントをマーカーにせずcontentにも含めない() {
        let source = cs_wrap(
            "    [Theory]\n    // [FR-001]\n    [InlineData(1)]\n    public void T(int x) { }",
        );
        let tests = cs(&source);
        let t = find(&tests, "T");
        assert!(t.markers.is_empty());
        assert!(!t.content.contains("FR-001"));
    }

    // [FR-051]
    #[test]
    fn csharpのtest識別子はトップレベルclassならファイルパスと型名とメソッド名になる() {
        let source =
            "public class AuthTests\n{\n    [Fact]\n    public void RejectsShort() { }\n}\n";
        let tests = cs(source);
        assert_eq!(tests[0].id, "tests/AuthTests:AuthTests:RejectsShort");
    }

    // [FR-051]
    #[test]
    fn csharpのtest識別子はネストした型を外側から内側へスラッシュで連結する() {
        let source = "public class Outer\n{\n    public class Inner\n    {\n        [Test]\n        public void T3() { }\n    }\n}\n";
        let tests = cs(source);
        assert_eq!(tests[0].id, "tests/AuthTests:Outer/Inner:T3");
    }

    // [FR-051]
    #[test]
    fn csharpのtest識別子はstruct_record_interfaceの中でも型名を辿る() {
        let source = "public struct S\n{\n    [Fact]\n    public void A() { }\n}\n\npublic record R\n{\n    [Fact]\n    public void B() { }\n}\n\npublic interface I\n{\n    [Fact]\n    public void C() { }\n}\n";
        let tests = cs(source);
        assert_eq!(find(&tests, "A").id, "tests/AuthTests:S:A");
        assert_eq!(find(&tests, "B").id, "tests/AuthTests:R:B");
        assert_eq!(find(&tests, "C").id, "tests/AuthTests:I:C");
    }

    // [FR-051]
    #[test]
    fn csharpのtest識別子はblock_namespaceを含まない() {
        let source = "namespace Foo.Bar\n{\n    public class AuthTests\n    {\n        [Fact]\n        public void T() { }\n    }\n}\n";
        let tests = cs(source);
        assert_eq!(tests[0].id, "tests/AuthTests:AuthTests:T");
    }

    // [FR-051]
    #[test]
    fn csharpのtest識別子はfile_scoped_namespaceを含まない() {
        let source =
            "namespace Foo;\n\npublic class AuthTests\n{\n    [Fact]\n    public void T() { }\n}\n";
        let tests = cs(source);
        assert_eq!(tests[0].id, "tests/AuthTests:AuthTests:T");
    }

    // [FR-052]
    #[test]
    fn csharpのcontentは属性を含まず修飾子から始まる() {
        let source = cs_wrap(
            "    // [FR-001]\n    [Fact]\n    [Trait(\"a\", \"b\")]\n    public void T() { Assert.True(true); }",
        );
        let tests = cs(&source);
        let c = &tests[0].content;
        assert!(c.starts_with("public void T()"), "content = {c:?}");
        assert!(c.ends_with('}'));
        assert!(!c.contains("Fact"));
        assert!(!c.contains("Trait"));
        assert!(!c.contains("FR-001"));
    }

    // [FR-052]
    #[test]
    fn csharpの本体変更はcontentに反映される() {
        let a = cs(&cs_wrap(
            "    [Fact]\n    public void T() { Assert.True(true); }",
        ));
        let b = cs(&cs_wrap(
            "    [Fact]\n    public void T() { Assert.True(false); }",
        ));
        assert_ne!(a[0].content, b[0].content);
    }

    // [FR-052]
    #[test]
    fn csharpの属性だけの変更はcontentを変えない() {
        let a = cs(&cs_wrap(
            "    [Fact]\n    public void T() { Assert.True(true); }",
        ));
        let b = cs(&cs_wrap(
            "    [Fact(Skip = \"x\")]\n    [Trait(\"a\", \"b\")]\n    public void T() { Assert.True(true); }",
        ));
        assert_eq!(a[0].content, b[0].content);
    }

    // [FR-014]
    #[test]
    fn csharpは構文エラーのソースでエラーを返す() {
        let source = "public class A {\n    [Fact]\n    public void T( {\n}\n";
        assert!(extract_csharp_tests("tests/A.cs", source).is_err());
    }

    // ---- TypeScript / JavaScript ----

    fn ts(source: &str) -> Vec<ExtractedTest> {
        extract_typescript_tests("src/a.test.ts", source).unwrap()
    }

    // [FR-054]
    #[test]
    fn typescriptはtestとitの呼び出しを検出する() {
        let tests = ts("test(\"a\", () => {});\nit(\"b\", () => {});\n");
        assert_eq!(tests.len(), 2);
        find(&tests, "a");
        find(&tests, "b");
    }

    // [FR-054]
    #[test]
    fn typescriptは許可された修飾子付きの呼び出しを検出する() {
        let source = "it.skip(\"a\", () => {});\ntest.only(\"b\", () => {});\ntest.concurrent.only(\"c\", () => {});\ntest.todo(\"d\", () => {});\n";
        let tests = ts(source);
        assert_eq!(tests.len(), 4);
        for n in ["a", "b", "c", "d"] {
            find(&tests, n);
        }
    }

    // [FR-054]
    #[test]
    fn typescriptはdescribe_skipの中のtestも検出する() {
        let tests = ts("describe.skip(\"d\", () => {\n  it(\"a\", () => {});\n});\n");
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].id, "src/a.test:d:a");
    }

    // [FR-054]
    #[test]
    fn typescriptはfunction式のコールバックも検出する() {
        let source = "describe(\"d\", function () {\n  it(\"a\", function () {});\n});\n";
        let tests = ts(source);
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].id, "src/a.test:d:a");
    }

    // [FR-053]
    #[test]
    fn typescriptはtsxファイルのjsxを含むソースも解析できる() {
        let source = "it(\"renders\", () => {\n  const x = <div className=\"a\">hi</div>;\n  expect(x).toBeTruthy();\n});\n";
        let tests = extract_typescript_tests("src/a.test.tsx", source).unwrap();
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].id, "src/a.test::renders");
    }

    // [FR-054]
    #[test]
    fn typescriptは許可外の修飾子や呼び出し結果の呼び出しを検出しない() {
        let source = r#"test.each([1, 2])("each %i", (n) => {});
test.skipIf(c)("skipif", () => {});
test.extend({ a: 1 });
test.describe("desc", () => {});
test.runIf(c)("runif", () => {});
it?.skip("optional", () => {});
"#;
        assert!(ts(source).is_empty());
    }

    // [FR-054]
    #[test]
    fn typescriptはexport_defaultやskipif_describeやforループ内のitを検出しない() {
        let source = r#"export default describe("d", () => { it("y", () => {}); })
describe.skipIf(c)("d", () => { it("x", () => {}); });
for (const n of [1]) {
  it("loop", () => {});
}
"#;
        assert!(ts(source).is_empty());
    }

    // [FR-054]
    #[test]
    fn typescriptは許可された修飾子を検出し走査する() {
        let source = r#"test.fails("f", () => {});
it.sequential("s", () => {});
describe.shuffle("d", () => {
  it("inner", () => {});
});
"#;
        let tests = ts(source);
        assert_eq!(tests.len(), 3);
        find(&tests, "f");
        find(&tests, "s");
        assert_eq!(find(&tests, "inner").id, "src/a.test:d:inner");
    }

    // [FR-054]
    #[test]
    fn typescriptはdescribe_eachの中のtestを検出しない() {
        let source = "describe.each([1])(\"d %i\", (n) => {\n  it(\"a\", () => {});\n});\n";
        assert!(ts(source).is_empty());
    }

    // [FR-054]
    #[test]
    fn typescriptはawaitなど式文直下でない呼び出しを検出しない() {
        let source = "await it(\"a\", () => {});\nvoid it(\"b\", () => {});\n";
        assert!(ts(source).is_empty());
    }

    // [FR-054]
    #[test]
    fn typescriptは第1引数が文字列リテラルでない呼び出しを検出しない() {
        let source = "const name = \"x\";\nit(name, () => {});\nit(\"a\" + \"b\", () => {});\n";
        assert!(ts(source).is_empty());
    }

    // [FR-054]
    #[test]
    fn typescriptはhelper関数やtest本体やif文の中のitを検出しない() {
        let source = r#"function helper() {
  it("in helper", () => {});
}
test("outer", () => {
  it("in test", () => {});
});
if (cond) {
  it("in if", () => {});
}
"#;
        let tests = ts(source);
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "outer");
    }

    // [FR-056]
    #[test]
    fn typescriptは置換なしのテンプレート文字列を対象にし置換ありを無視する() {
        let source = "it(`plain`, () => {});\nit(`with ${x}`, () => {});\n";
        let tests = ts(source);
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "plain");
    }

    // [FR-056]
    #[test]
    fn typescriptは空文字名と改行を含む名前を無視する() {
        let source = "it(\"\", () => {});\nit(`line1\nline2`, () => {});\nit(\"ok\", () => {});\n";
        let tests = ts(source);
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "ok");
    }

    // [FR-056]
    #[test]
    fn typescriptは行区切り文字を含む名前を無視する() {
        let source = "it(\"a\u{2028}b\", () => {});\nit(\"ok\", () => {});\n";
        let tests = ts(source);
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "ok");
    }

    // [FR-056]
    #[test]
    fn typescriptはエスケープを解釈せずソース表記のまま名前にする() {
        let source = "it('it\\'s', () => {});\n";
        let tests = ts(source);
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "it\\'s");
        assert_eq!(tests[0].id, "src/a.test::it\\'s");
    }

    // [FR-056]
    #[test]
    fn typescriptのtest識別子はdescribeの外なら空のdescribeパスになる() {
        let tests = ts("it(\"top\", () => {});\n");
        assert_eq!(tests[0].id, "src/a.test::top");
    }

    // [FR-056]
    #[test]
    fn typescriptのtest識別子はdescribeを外側から内側へスラッシュで連結する() {
        let source = "describe(\"auth\", () => {\n  it(\"one\", () => {});\n  describe(\"inner\", () => {\n    it(\"two\", () => {});\n  });\n});\n";
        let tests = ts(source);
        assert_eq!(find(&tests, "one").id, "src/a.test:auth:one");
        assert_eq!(find(&tests, "two").id, "src/a.test:auth/inner:two");
    }

    // [FR-056]
    #[test]
    fn typescriptのtest識別子は対象拡張子をすべて除去しtestやspecは残す() {
        let src = "it(\"t\", () => {});\n";
        for (path, id) in [
            ("src/a.test.ts", "src/a.test::t"),
            ("src/a.spec.tsx", "src/a.spec::t"),
            ("src/a.test.mjs", "src/a.test::t"),
            ("src/a.spec.cts", "src/a.spec::t"),
            ("src/a.test.jsx", "src/a.test::t"),
            ("src/a.test.js", "src/a.test::t"),
            ("src/a.spec.cjs", "src/a.spec::t"),
            ("src/a.test.mts", "src/a.test::t"),
        ] {
            let tests = extract_typescript_tests(path, src).unwrap();
            assert_eq!(tests[0].id, id, "{path}");
        }
    }

    // [FR-056]
    #[test]
    fn typescriptのtest識別子は対象外の拡張子を除去しない() {
        let tests =
            extract_typescript_tests("src/a.test.ts.bak", "it(\"t\", () => {});\n").unwrap();
        assert_eq!(tests[0].id, "src/a.test.ts.bak::t");
    }

    // [FR-053]
    #[test]
    fn typescriptはjs系拡張子のファイルからtestを検出する() {
        for path in [
            "src/a.test.js",
            "src/a.spec.mjs",
            "src/a.test.cjs",
            "src/a.spec.mts",
            "src/a.test.cts",
        ] {
            let tests = extract_typescript_tests(path, "it(\"t\", () => {});\n").unwrap();
            assert_eq!(tests.len(), 1, "{path}");
        }
    }

    // [FR-053]
    #[test]
    fn typescriptはts系拡張子でtsx文法が拒否する角括弧キャストを解析できる() {
        let source = "it(\"t\", () => {\n  const n = <number>x;\n});\n";
        for path in ["src/a.test.ts", "src/a.test.mts", "src/a.spec.cts"] {
            let tests = extract_typescript_tests(path, source).unwrap();
            assert_eq!(tests.len(), 1, "{path}");
        }
    }

    // [FR-053]
    #[test]
    fn typescriptはjsxとjsファイルのjsxを含むソースも解析できる() {
        let source = "it(\"renders\", () => {\n  render(<App />);\n});\n";
        for path in ["src/a.test.jsx", "src/a.test.js"] {
            let tests = extract_typescript_tests(path, source).unwrap();
            assert_eq!(tests.len(), 1, "{path}");
            assert_eq!(tests[0].id, "src/a.test::renders", "{path}");
        }
    }

    // [FR-055]
    #[test]
    fn typescriptは直前に隣接するコメントをマーカーとして抽出し空行を挟むと無視する() {
        let source = "// [FR-001]\nit(\"adjacent\", () => {});\n\n// [FR-002]\n\nit(\"blank\", () => {});\n\nit(\"none\", () => {});\n";
        let tests = ts(source);
        assert_eq!(find(&tests, "adjacent").markers, vec!["FR-001".to_string()]);
        assert!(find(&tests, "blank").markers.is_empty());
        assert!(find(&tests, "none").markers.is_empty());
    }

    // [FR-055]
    #[test]
    fn typescriptはdescribe内のtestでも直前のコメントをマーカーにする() {
        let source = "describe(\"d\", () => {\n  // [FR-001]\n  it(\"a\", () => {});\n});\n";
        let tests = ts(source);
        assert_eq!(tests[0].markers, vec!["FR-001".to_string()]);
    }

    // [FR-003]
    #[test]
    fn typescriptのcontentは呼び出し文全体でマーカーコメントを含まない() {
        let source = "// [FR-001]\nit(\"a\", () => {\n  expect(1).toBe(1);\n});\n";
        let tests = ts(source);
        let c = &tests[0].content;
        assert!(c.starts_with("it(\"a\""), "content = {c:?}");
        assert!(c.ends_with(");"), "content = {c:?}");
        assert!(!c.contains("FR-001"));
    }

    // [FR-003]
    #[test]
    fn typescriptの本体変更はcontentに反映される() {
        let a = ts("it(\"a\", () => { expect(1).toBe(1); });\n");
        let b = ts("it(\"a\", () => { expect(1).toBe(2); });\n");
        assert_ne!(a[0].content, b[0].content);
    }

    // [FR-014]
    #[test]
    fn typescriptは構文エラーのソースでエラーを返す() {
        let source = "it(\"a\", () => {\n";
        assert!(extract_typescript_tests("src/a.test.ts", source).is_err());
    }

    // ---- Java ----

    fn jv(source: &str) -> Vec<ExtractedTest> {
        extract_java_tests("tests/AuthTests.java", source).unwrap()
    }

    fn jv_wrap(body: &str) -> String {
        format!("public class AuthTests {{\n{body}\n}}\n")
    }

    // [FR-059]
    #[test]
    fn javaは5種類の注釈をそれぞれtestとして検出する() {
        for ann in [
            "Test",
            "ParameterizedTest",
            "RepeatedTest",
            "TestFactory",
            "TestTemplate",
        ] {
            let source = jv_wrap(&format!("    @{ann}\n    void t() {{ }}"));
            let tests = jv(&source);
            assert_eq!(tests.len(), 1, "annotation {ann} should be detected");
            assert_eq!(tests[0].name, "t");
        }
    }

    // [FR-059]
    #[test]
    fn javaは修飾名の注釈と引数付きの注釈も検出する() {
        let source = jv_wrap(
            "    @org.junit.jupiter.api.Test\n    void a() { }\n\n    @Test(timeout = 1)\n    void b() { }",
        );
        let tests = jv(&source);
        assert_eq!(tests.len(), 2);
        find(&tests, "a");
        find(&tests, "b");
    }

    // [FR-059]
    #[test]
    fn javaは無関係な注釈のみのメソッドを検出しない() {
        let source = jv_wrap(
            "    @Disabled\n    void a() { }\n\n    void b() { }\n\n    @Tag(\"x\")\n    void c() { }",
        );
        assert!(jv(&source).is_empty());
    }

    // [FR-059]
    #[test]
    fn javaは修飾子の後ろに書かれた注釈も検出する() {
        let source = jv_wrap("    public @Test void t() { }");
        let tests = jv(&source);
        assert_eq!(tests.len(), 1);
        assert!(tests[0].content.contains("@Test"), "{:?}", tests[0].content);
    }

    // [FR-060]
    #[test]
    fn javaは直前に隣接するコメントをマーカーとして抽出し空行を挟むと無視する() {
        let source = jv_wrap(
            "    // [FR-001]\n    @Test\n    void adjacent() { }\n\n    // [FR-002]\n\n    @Test\n    void blank() { }\n\n    @Test\n    void none() { }",
        );
        let tests = jv(&source);
        assert_eq!(find(&tests, "adjacent").markers, vec!["FR-001".to_string()]);
        assert!(find(&tests, "blank").markers.is_empty());
        assert!(find(&tests, "none").markers.is_empty());
    }

    // [FR-060]
    #[test]
    fn javaはブロックコメントをマーカーにできる() {
        let source = jv_wrap("    /* [FR-002] */\n    @Test\n    void t() { }");
        assert_eq!(jv(&source)[0].markers, vec!["FR-002".to_string()]);
    }

    // [FR-060]
    #[test]
    fn javaはjavadocコメントをマーカーにしない() {
        let source = jv_wrap("    /** [FR-001] */\n    @Test\n    void t() { }");
        assert!(jv(&source)[0].markers.is_empty());
    }

    // [FR-060]
    #[test]
    fn javaは注釈と署名の間のコメントをマーカーにせずcontentにも含めない() {
        let source = jv_wrap("    @Test\n    // [FR-001]\n    // 補足\n    void t() { }");
        let tests = jv(&source);
        assert!(tests[0].markers.is_empty());
        assert!(!tests[0].content.contains("FR-001"));
        assert!(!tests[0].content.contains("補足"));
    }

    // [FR-061]
    #[test]
    fn javaのtest識別子はclassならファイルパスと型名とメソッド名になる() {
        let tests = jv(&jv_wrap("    @Test\n    void rejectsShort() { }"));
        assert_eq!(tests[0].id, "tests/AuthTests:AuthTests:rejectsShort");
    }

    // [FR-061]
    #[test]
    fn javaのtest識別子はネストした型を外側から内側へスラッシュで連結する() {
        let source = "public class Outer {\n    @Nested\n    class Inner {\n        @Test\n        void t() { }\n    }\n}\n";
        let tests = jv(source);
        assert_eq!(tests[0].id, "tests/AuthTests:Outer/Inner:t");
    }

    // [FR-061]
    #[test]
    fn javaのtest識別子はrecord_interface_enumの中でも型名を辿る() {
        let source = "record R() {\n    @Test\n    void a() { }\n}\n\ninterface I {\n    @Test\n    default void b() { }\n}\n\nenum E {\n    X, Y;\n\n    @Test\n    void c() { }\n}\n";
        let tests = jv(source);
        assert_eq!(find(&tests, "a").id, "tests/AuthTests:R:a");
        assert_eq!(find(&tests, "b").id, "tests/AuthTests:I:b");
        assert_eq!(find(&tests, "c").id, "tests/AuthTests:E:c");
    }

    // [FR-061]
    #[test]
    fn javaのtest識別子はpackageを含まない() {
        let source =
            "package com.example;\n\npublic class AuthTests {\n    @Test\n    void t() { }\n}\n";
        assert_eq!(jv(source)[0].id, "tests/AuthTests:AuthTests:t");
    }

    // [FR-059]
    #[test]
    fn javaはトップレベルメソッドを対象外にする() {
        let source = "@Test\nvoid t() { }\n";
        let tests = extract_java_tests("tests/AuthTests.java", source).unwrap();
        assert!(tests.is_empty());
        // a class alongside is still extracted
        let source = "class A {\n    @Test\n    void u() { }\n}\n";
        assert_eq!(jv(source).len(), 1);
    }

    // [FR-059]
    #[test]
    fn javaは匿名クラスとenum定数本体の中を対象外にする() {
        let source = "public class A {\n    void helper() {\n        Runnable r = new Runnable() {\n            @Test\n            public void run() { }\n        };\n    }\n\n    @Test\n    void real() { }\n}\n\nenum E {\n    X {\n        @Test\n        void inConst() { }\n    };\n\n    @Test\n    void outside() { }\n}\n";
        let tests = jv(source);
        let names: Vec<&str> = tests.iter().map(|t| t.name.as_str()).collect();
        assert_eq!(names, vec!["real", "outside"], "{names:?}");
    }

    // [FR-062]
    #[test]
    fn javaのcontentは注釈を含まず修飾子から始まる() {
        let source = jv_wrap(
            "    // [FR-001]\n    @Test\n    @Tag(\"a\")\n    public void t() { assertTrue(true); }",
        );
        let c = &jv(&source)[0].content;
        assert!(c.starts_with("public void t()"), "content = {c:?}");
        assert!(c.ends_with('}'));
        assert!(!c.contains("@Test"));
        assert!(!c.contains("@Tag"));
        assert!(!c.contains("FR-001"));
    }

    // [FR-062]
    #[test]
    fn javaのcontentは型パラメータ付きでも修飾子から始まる() {
        let source = jv_wrap("    @Test\n    static <T> void t() { }");
        let c = &jv(&source)[0].content;
        assert!(c.starts_with("static <T> void t()"), "content = {c:?}");
    }

    // [FR-062]
    #[test]
    fn javaのcontentは修飾子なしなら型パラメータから始まる() {
        let source = jv_wrap("    @Test\n    <T> void t() { }");
        let c = &jv(&source)[0].content;
        assert!(c.starts_with("<T> void t()"), "content = {c:?}");
    }

    // [FR-003]
    #[test]
    fn javaの本体変更はcontentに反映される() {
        let a = jv(&jv_wrap("    @Test\n    void t() { assertTrue(true); }"));
        let b = jv(&jv_wrap("    @Test\n    void t() { assertTrue(false); }"));
        assert_ne!(a[0].content, b[0].content);
    }

    // [FR-062]
    #[test]
    fn javaの注釈だけの変更はcontentを変えない() {
        let a = jv(&jv_wrap("    @Test\n    void t() { assertTrue(true); }"));
        let b = jv(&jv_wrap(
            "    @Test(timeout = 1)\n    @Tag(\"a\")\n    void t() { assertTrue(true); }",
        ));
        assert_eq!(a[0].content, b[0].content);
    }

    // [FR-014]
    #[test]
    fn javaは構文エラーのソースでエラーを返す() {
        let source = "public class A {\n    @Test\n    void t( {\n}\n";
        assert!(extract_java_tests("tests/A.java", source).is_err());
    }

    // ---- Kotlin ----

    fn kt(source: &str) -> Vec<ExtractedTest> {
        extract_kotlin_tests("tests/AuthTest.kt", source).unwrap()
    }

    fn kt_wrap(body: &str) -> String {
        format!("class AuthTest {{\n{body}\n}}\n")
    }

    // [FR-059]
    #[test]
    fn kotlinは5種類の注釈をそれぞれtestとして検出する() {
        for ann in [
            "Test",
            "ParameterizedTest",
            "RepeatedTest",
            "TestFactory",
            "TestTemplate",
        ] {
            let source = kt_wrap(&format!("    @{ann}\n    fun t() {{ }}"));
            let tests = kt(&source);
            assert_eq!(tests.len(), 1, "annotation {ann} should be detected");
            assert_eq!(tests[0].name, "t");
        }
    }

    // [FR-059]
    #[test]
    fn kotlinは修飾名と複数注釈リストとuse_site_target付きの注釈も検出する() {
        let source = kt_wrap(
            "    @org.junit.jupiter.api.Test\n    fun a() { }\n\n    @[Test Tag(\"a\")]\n    fun b() { }\n\n    @get:Test\n    fun c() { }",
        );
        let tests = kt(&source);
        assert_eq!(tests.len(), 3, "{tests:?}");
        find(&tests, "a");
        find(&tests, "b");
        find(&tests, "c");
    }

    // [FR-059]
    #[test]
    fn kotlinは無関係な注釈のみの関数を検出しない() {
        let source = kt_wrap("    @Disabled\n    fun a() { }\n\n    fun b() { }");
        assert!(kt(&source).is_empty());
    }

    // [FR-060]
    #[test]
    fn kotlinは直前に隣接するコメントをマーカーとして抽出し空行を挟むと無視する() {
        let source = kt_wrap(
            "    // [FR-001]\n    @Test\n    fun adjacent() { }\n\n    // [FR-002]\n\n    @Test\n    fun blank() { }\n\n    /* [FR-003] */\n    @Test\n    fun block() { }",
        );
        let tests = kt(&source);
        assert_eq!(find(&tests, "adjacent").markers, vec!["FR-001".to_string()]);
        assert!(find(&tests, "blank").markers.is_empty());
        assert_eq!(find(&tests, "block").markers, vec!["FR-003".to_string()]);
    }

    // [FR-060]
    #[test]
    fn kotlinはkdocコメントをマーカーにしない() {
        let source = kt_wrap("    /** [Foo] */\n    @Test\n    fun t() { }");
        assert!(kt(&source)[0].markers.is_empty());
    }

    // [FR-060]
    #[test]
    fn kotlinは注釈と署名の間のコメントをマーカーにせずcontentにも含めない() {
        let source = kt_wrap(
            "    @Test // [FR-001]\n    fun a() { }\n\n    @Test\n    // [FR-002]\n    fun b() { }",
        );
        let tests = kt(&source);
        for name in ["a", "b"] {
            let t = find(&tests, name);
            assert!(t.markers.is_empty(), "{name}: {:?}", t.markers);
            assert!(!t.content.contains("FR-00"), "{name}: {:?}", t.content);
        }
    }

    // [FR-061]
    #[test]
    fn kotlinのバッククォート名はバッククォートを除いた名前になる() {
        let source = kt_wrap("    @Test\n    fun `一覧 取得 は 200`() { }");
        let tests = kt(&source);
        assert_eq!(tests[0].name, "一覧 取得 は 200");
        assert_eq!(tests[0].id, "tests/AuthTest:AuthTest:一覧 取得 は 200");
    }

    // [FR-061]
    #[test]
    fn kotlinのtest識別子はネストした型を外側から内側へスラッシュで連結する() {
        let source = "class AuthTest {\n    @Nested\n    inner class Inner {\n        @Test\n        fun t() { }\n    }\n}\n";
        assert_eq!(kt(source)[0].id, "tests/AuthTest:AuthTest/Inner:t");
    }

    // [FR-061]
    #[test]
    fn kotlinのtest識別子はobjectとcompanion_objectを辿る() {
        let source = "object O {\n    @Test\n    fun a() { }\n}\n\nclass C {\n    companion object {\n        @Test\n        fun b() { }\n    }\n\n    companion object Factory {\n        @Test\n        fun c() { }\n    }\n}\n";
        // two companion objects in one class is invalid Kotlin semantically but
        // syntactically fine; the grammar does not reject it.
        let tests = kt(source);
        assert_eq!(find(&tests, "a").id, "tests/AuthTest:O:a");
        assert_eq!(find(&tests, "b").id, "tests/AuthTest:C/Companion:b");
        assert_eq!(find(&tests, "c").id, "tests/AuthTest:C/Factory:c");
    }

    // [FR-061]
    #[test]
    fn kotlinのトップレベル関数は型部分が空の識別子になる() {
        let source = "@Test\nfun top() { }\n";
        let tests = kt(source);
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].id, "tests/AuthTest::top");
    }

    // [FR-061]
    #[test]
    fn kotlinのtest識別子はpackageを含まない() {
        let source = "package com.example\n\nclass AuthTest {\n    @Test\n    fun t() { }\n}\n";
        assert_eq!(kt(source)[0].id, "tests/AuthTest:AuthTest:t");
    }

    // [FR-061]
    #[test]
    fn kotlinはコンストラクタ注釈付きの主コンストラクタを持つクラスでもtestを取れる() {
        let source = "@SpringBootTest\nclass X @Autowired constructor(private val a: Int) : Base(a) {\n    @Test\n    fun `日本語名`() { }\n}\n";
        let tests = kt(source);
        assert_eq!(tests.len(), 1, "{tests:?}");
        assert_eq!(tests[0].id, "tests/AuthTest:X:日本語名");
    }

    // [FR-059]
    #[test]
    fn kotlinは関数本体内のローカル関数や匿名objectやenum定数本体やgetterを対象外にする() {
        let source = "class A {\n    @Test\n    fun real() {\n        @Test\n        fun local() { }\n    }\n\n    val x: Int\n        @Test\n        get() = 1\n\n    val r = object : Runnable {\n        @Test\n        fun inObj() { }\n    }\n}\n\nenum class E {\n    X {\n        @Test\n        fun inConst() { }\n    };\n\n    @Test\n    fun outside() { }\n}\n";
        let tests = kt(source);
        let names: Vec<&str> = tests.iter().map(|t| t.name.as_str()).collect();
        assert_eq!(names, vec!["real", "outside"], "{names:?}");
    }

    // [FR-063]
    #[test]
    fn kotlinは漢字の関数名と型名のtestを抽出し識別子とcontentが元の文字を保つ() {
        let source = "class AuthTest {\n    @Nested\n    inner class 一覧取得 {\n        @Test\n        fun 取得() { println(\"漢字\") }\n    }\n}\n";
        let tests = kt(source);
        assert_eq!(tests.len(), 1, "{tests:?}");
        assert_eq!(tests[0].id, "tests/AuthTest:AuthTest/一覧取得:取得");
        assert_eq!(tests[0].name, "取得");
        assert!(tests[0].content.contains("取得"));
        assert!(tests[0].content.contains("漢字"));
        assert!(!tests[0].content.contains('x'), "{:?}", tests[0].content);
    }

    // [FR-063]
    #[test]
    fn kotlinはカタカナとハングルの関数名も抽出する() {
        let source = kt_wrap("    @Test\n    fun テスト() { }\n\n    @Test\n    fun 테스트() { }");
        let tests = kt(&source);
        assert_eq!(tests.len(), 2, "{tests:?}");
        find(&tests, "テスト");
        find(&tests, "테스트");
    }

    // [FR-063]
    #[test]
    fn kotlinは非asciiの文字リテラルを含むファイルも解析できる() {
        let source =
            kt_wrap("    @Test\n    fun t() {\n        val c = 'あ'\n        val d = '漢'\n    }");
        let tests = kt(&source);
        assert_eq!(tests.len(), 1);
        assert!(tests[0].content.contains("'あ'"));
    }

    // [FR-063]
    #[test]
    fn kotlinはbom付きファイルも解析できる() {
        let source = format!("\u{feff}{}", kt_wrap("    @Test\n    fun t() { }"));
        let tests = kt(&source);
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "t");
    }

    // [FR-062]
    #[test]
    fn kotlinのcontentは注釈を含まず修飾子から始まる() {
        let source = kt_wrap(
            "    // [FR-001]\n    @Test\n    internal suspend fun t() { assertTrue(true) }",
        );
        let c = &kt(&source)[0].content;
        assert!(c.starts_with("internal suspend fun t()"), "content = {c:?}");
        assert!(c.ends_with('}'));
        assert!(!c.contains("@Test"));
        assert!(!c.contains("FR-001"));
    }

    // [FR-062]
    #[test]
    fn kotlinのcontentは注釈のみならfunから始まる() {
        let source = kt_wrap("    @Test\n    @Tag(\"a\")\n    fun t() { }");
        let c = &kt(&source)[0].content;
        assert!(c.starts_with("fun t()"), "content = {c:?}");
    }

    // [FR-062]
    #[test]
    fn kotlinの修飾子の後ろに書かれた注釈はcontentに含まれる() {
        let source = kt_wrap("    internal @Test fun t() { }");
        let tests = kt(&source);
        assert_eq!(tests.len(), 1);
        assert!(
            tests[0].content.starts_with("internal"),
            "{:?}",
            tests[0].content
        );
        assert!(tests[0].content.contains("@Test"), "{:?}", tests[0].content);
    }

    // [FR-062]
    #[test]
    fn kotlinの式本体は終端までcontentに含まれる() {
        let source = kt_wrap("    @Test\n    fun t() = assertTrue(computeSomething())");
        let c = &kt(&source)[0].content;
        assert!(c.ends_with("computeSomething())"), "content = {c:?}");
    }

    // [FR-003]
    #[test]
    fn kotlinの本体変更はcontentに反映される() {
        let a = kt(&kt_wrap("    @Test\n    fun t() { assertTrue(true) }"));
        let b = kt(&kt_wrap("    @Test\n    fun t() { assertTrue(false) }"));
        assert_ne!(a[0].content, b[0].content);
    }

    // [FR-062]
    #[test]
    fn kotlinの注釈だけの変更はcontentを変えない() {
        let a = kt(&kt_wrap("    @Test\n    fun t() { assertTrue(true) }"));
        let b = kt(&kt_wrap(
            "    @Test\n    @Tag(\"a\")\n    fun t() { assertTrue(true) }",
        ));
        assert_eq!(a[0].content, b[0].content);
    }

    // [FR-014]
    #[test]
    fn kotlinは構文エラーのソースでエラーを返す() {
        let source = "class A { fun x() {} }\n";
        assert!(extract_kotlin_tests("tests/A.kt", source).is_err());
    }

    // [FR-060]
    #[test]
    fn kotlinのマーカーは元のソースから取り非asciiの角括弧は偽マーカーにならない() {
        let source = kt_wrap(
            "    // [正常系]\n    // [FR-001]\n    @Test\n    fun t() { }\n\n    // [仮] メモ\n    @Test\n    fun u() { }\n\n    // [FR\u{2010}001]\n    @Test\n    fun v() { }",
        );
        let tests = kt(&source);
        assert_eq!(find(&tests, "t").markers, vec!["FR-001".to_string()]);
        assert!(find(&tests, "u").markers.is_empty());
        assert!(find(&tests, "v").markers.is_empty());
    }

    // [FR-060]
    #[test]
    fn kotlinはdocコメントを挟んでも手前のマーカーが有効になる() {
        let source = kt_wrap("    // [FR-001]\n    /** KDoc */\n    @Test\n    fun b() { }");
        assert_eq!(kt(&source)[0].markers, vec!["FR-001".to_string()]);
    }

    // [FR-060]
    #[test]
    fn javaはdocコメントを挟んでも手前のマーカーが有効になる() {
        let source = jv_wrap("    // [FR-001]\n    /** Javadoc */\n    @Test\n    void b() { }");
        assert_eq!(jv(&source)[0].markers, vec!["FR-001".to_string()]);
    }

    // [FR-060]
    #[test]
    fn javaは注釈同士の間のコメントをマーカーにせずcontentにも含めない() {
        let source =
            jv_wrap("    @Test\n    // [FR-001]\n    @Tag(\"a\")\n    // 補足\n    void t() { }");
        let t = &jv(&source)[0];
        assert!(t.markers.is_empty());
        assert!(!t.content.contains("FR-001"));
        assert!(!t.content.contains("補足"));
    }

    // [FR-062]
    #[test]
    fn kotlinは注釈の間と直後のコメントをcontentに含めない() {
        let source = kt_wrap(
            "    @Test\n    // 間のメモ\n    @Tag(\"a\")\n    // 直後のメモ\n    fun t() { }",
        );
        let t = &kt(&source)[0];
        assert!(!t.content.contains("メモ"), "{:?}", t.content);
        assert!(t.content.starts_with("fun t()"), "{:?}", t.content);
    }

    // [FR-060]
    #[test]
    fn kotlinは注釈同士の間のコメントをマーカーにしない() {
        let source = kt_wrap("    @Test\n    // [FR-001]\n    @Tag(\"a\")\n    fun t() { }");
        assert!(kt(&source)[0].markers.is_empty());
    }

    // [FR-061]
    #[test]
    fn kotlinはdata_classとinterfaceの直下のtestも辿る() {
        let source = "data class D(val a: Int) {\n    @Test\n    fun a() { }\n}\n\ninterface I {\n    @Test\n    fun b() { }\n}\n";
        let tests = kt(source);
        assert_eq!(find(&tests, "a").id, "tests/AuthTest:D:a");
        assert_eq!(find(&tests, "b").id, "tests/AuthTest:I:b");
    }

    // [FR-061]
    #[test]
    fn kotlinは制御文字や空を含む名前のtestを無視する() {
        let source =
            kt_wrap("    @Test\n    fun `tab\there`() { }\n\n    @Test\n    fun `ok name`() { }");
        let tests = kt(&source);
        assert_eq!(tests.len(), 1, "{tests:?}");
        assert_eq!(tests[0].name, "ok name");
    }

    // [FR-063]
    #[test]
    fn kotlinは注釈2つで基底型なしのクラス内の注釈付きinner_classでもtestを取れる() {
        let source = "@A\n@C\nclass P {\n    @Nested\n    inner class X {\n        // [FR-001]\n        @Test\n        fun t() { }\n    }\n}\n";
        let tests = kt(source);
        assert_eq!(tests.len(), 1, "{tests:?}");
        assert_eq!(tests[0].id, "tests/AuthTest:P/X:t");
        assert_eq!(tests[0].markers, vec!["FR-001".to_string()]);
    }

    // [FR-063]
    #[test]
    fn kotlinはnestedを含む複数注釈クラスでも非asciiの名前を元のまま取る() {
        let source = "@A\n@C\nclass P {\n    @Nested\n    inner class 一覧取得 {\n        @Test\n        fun `取得 する`() { }\n    }\n}\n";
        let tests = kt(source);
        assert_eq!(tests[0].id, "tests/AuthTest:P/一覧取得:取得 する");
    }

    // [FR-063]
    #[test]
    fn kotlinは宣言以外のトップレベル構造やキーワードを含む文字列でもok空になる() {
        for source in [
            "package a\n// helper for class X\nval x = 1\n",
            "package a\nval fixture = object : Runnable {\n    override fun run() {}\n}\n",
            "package a\nval f = fun() { }\n",
            "package a\nconst val NAME = \"class\"\n",
        ] {
            let tests = kt(source);
            assert!(tests.is_empty(), "{source:?}: {tests:?}");
        }
    }

    // [FR-063]
    #[test]
    fn kotlinは文法の不具合で宣言が認識されなかった場合に構文エラーを返す() {
        let source = "@A\n@C\nclass P {\n    @Nested\n    inner class X {\n    }\n}\n";
        assert!(kotlin_extract("tests/P.kt", source, true).is_ok());
        assert!(kotlin_extract("tests/P.kt", source, false).is_err());
    }

    // [FR-063]
    #[test]
    fn kotlinはshebangやimportやtypealiasなど宣言以外の通常の構造でok空になる() {
        let source = "#!/usr/bin/env kotlin\n@file:JvmName(\"X\")\npackage a\n\nimport b.C\n\ntypealias N = String\nval x = 1\nval y: Int\n    get() = 2\n";
        assert!(kt(source).is_empty());
    }

    // ---- コメント隣接判定(FR-016 / FR-064) ----

    // [FR-016]
    #[test]
    fn rustはdocコメントを挟んでもマーカーを取る() {
        let tests = rs("// [FR-001]\n/// doc\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, vec!["FR-001".to_string()]);
    }

    // [FR-016]
    #[test]
    fn rustの外側docコメントはマーカーにならない() {
        let tests = rs("/// [FR-001]\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, Vec::<String>::new());
    }

    // [FR-016]
    #[test]
    fn rustの内側docコメントはマーカーにならない() {
        let tests = rs("mod m {\n//! [FR-001]\n#[test]\nfn t() {}\n}\n", &[]);
        assert_eq!(find(&tests, "t").markers, Vec::<String>::new());
    }

    // [FR-016]
    #[test]
    fn rustはブロックdocコメントを挟んでもマーカーを取る() {
        let tests = rs("// [FR-001]\n/** doc */\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, vec!["FR-001".to_string()]);
    }

    // [FR-016]
    #[test]
    fn rustのスラッシュ4つのコメントは通常コメントだが余分な記号が本文に残りマーカーにならない() {
        let tests = rs("//// [FR-001]\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, Vec::<String>::new());
    }

    // [FR-016]
    #[test]
    fn rustはdocコメントの後に空行があるとマーカーを取らない() {
        let tests = rs("// [FR-001]\n/// doc\n\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, Vec::<String>::new());
    }

    // [FR-016]
    #[test]
    fn rustはcrlfでもdocコメントを挟んでマーカーを取る() {
        let tests = rs("// [FR-001]\r\n/// doc\r\n#[test]\r\nfn t() {}\r\n", &[]);
        assert_eq!(find(&tests, "t").markers, vec!["FR-001".to_string()]);
    }

    // [FR-064]
    #[test]
    fn goは前の宣言の末尾行コメントを次のtestのマーカーにしない() {
        let source =
            "package a\n\nfunc TestA(t *testing.T) {} // [FR-001]\nfunc TestB(t *testing.T) {}\n";
        let tests = extract_go_tests("a/x_test.go", source).unwrap();
        assert!(find(&tests, "TestB").markers.is_empty());
    }

    // [FR-064]
    #[test]
    fn rustは前の宣言の末尾行コメントを次のtestのマーカーにしない() {
        let tests = rs("fn x() {} // [FR-001]\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, Vec::<String>::new());
    }

    // [FR-064]
    #[test]
    fn csharpは前の宣言の末尾行コメントを次のtestのマーカーにしない() {
        let source =
            cs_wrap("    public void X() {} // [FR-001]\n    [Fact]\n    public void T() {}");
        let tests = cs(&source);
        assert!(find(&tests, "T").markers.is_empty());
    }

    // [FR-064]
    #[test]
    fn typescriptは前の文の末尾行コメントを次のtestのマーカーにしない() {
        let tests = ts("it(\"a\", () => {}); // [FR-001]\nit(\"b\", () => {});\n");
        assert!(find(&tests, "b").markers.is_empty());
    }

    // [FR-064]
    #[test]
    fn javaは前の宣言の末尾行コメントを次のtestのマーカーにしない() {
        let source = jv_wrap("    void x() {} // [FR-001]\n    @Test\n    void t() {}");
        let tests = jv(&source);
        assert!(find(&tests, "t").markers.is_empty());
    }

    // [FR-064]
    #[test]
    fn kotlinは非ascii識別子の後の末尾行コメントを次のtestのマーカーにしない() {
        let source = kt_wrap("    fun `日本語`() {} // [FR-001]\n    @Test\n    fun t() {}");
        let tests = kt(&source);
        assert!(find(&tests, "t").markers.is_empty());
    }

    // [FR-064]
    #[test]
    fn rustはインデント付きの行頭コメントをマーカーにする() {
        let tests = rs(
            "mod m {\n    // [FR-001]\n    #[test]\n    fn t() {}\n}\n",
            &[],
        );
        assert_eq!(find(&tests, "t").markers, vec!["FR-001".to_string()]);
    }

    // [FR-064]
    #[test]
    fn typescriptは同じ行に複数のコメントがあると2つ目以降は行頭でないので打ち切る() {
        let tests = ts("/* [FR-001] */ // note\nit(\"b\", () => {});\n");
        assert!(find(&tests, "b").markers.is_empty());
    }

    // [FR-064]
    #[test]
    fn rustはbomの直後の1行目マーカーを取る() {
        let tests = rs("\u{feff}// [FR-001]\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, vec!["FR-001".to_string()]);
    }

    // [FR-064]
    #[test]
    fn goはbomの直後の1行目マーカーを取る() {
        let source = "\u{feff}// [FR-001]\nfunc TestA(t *testing.T) {}\n";
        let tests = extract_go_tests("a/x_test.go", source).unwrap();
        assert_eq!(find(&tests, "TestA").markers, vec!["FR-001".to_string()]);
    }

    // [FR-016]
    #[test]
    fn rustの内側ブロックdocコメントはマーカーにならない() {
        let tests = rs("mod m {\n/*! [FR-001] */\n#[test]\nfn t() {}\n}\n", &[]);
        assert_eq!(find(&tests, "t").markers, Vec::<String>::new());
    }

    // [FR-016]
    #[test]
    fn rustのスラッシュ星3つのコメントは通常コメントだが余分な記号が本文に残りマーカーにならない() {
        let tests = rs("/*** [FR-001] */\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, Vec::<String>::new());
    }

    // [FR-016]
    #[test]
    fn rustの外側ブロックdocコメントはマーカーにならない() {
        let tests = rs("/** [FR-001] */\n#[test]\nfn t() {}\n", &[]);
        assert_eq!(find(&tests, "t").markers, Vec::<String>::new());
    }

    // ---- マーカーの厳格化(FR-121) ----

    /// (コメント, 期待するマーカー)。コメントの直後に test が続く。
    fn marker_cases() -> Vec<(&'static str, Vec<&'static str>)> {
        vec![
            ("// [FR-001]", vec!["FR-001"]),
            ("//   [FR-001]   ", vec!["FR-001"]),
            ("// [FR-001] [FR-002]", vec!["FR-001", "FR-002"]),
            ("// [FR-001]\n// [FR-002]", vec!["FR-001", "FR-002"]),
            ("//// [FR-001]", vec![]),
            ("/*** [FR-001] */", vec![]),
            ("// [FR-001] 説明", vec![]),
            ("// 説明 [FR-001]", vec![]),
            ("// 説明 [0] 説明", vec![]),
            ("// [FR-001] [正常系]", vec![]),
            ("// [FR-001][FR-002]", vec![]),
            ("//", vec![]),
            ("/* [FR-001] */", vec!["FR-001"]),
            ("/*\n * [FR-001]\n */", vec!["FR-001"]),
            (
                "/*\n * [FR-001]\n * [FR-002]\n */",
                vec!["FR-001", "FR-002"],
            ),
            ("/* [FR-001] [FR-002] */", vec!["FR-001", "FR-002"]),
            ("/*\n * [FR-001] 説明\n */", vec![]),
            ("/* 説明 [0] */", vec![]),
        ]
    }

    fn assert_marker_cases(markers_of: impl Fn(&str) -> Vec<String>) {
        for (comment, expected) in marker_cases() {
            assert_eq!(markers_of(comment), expected, "comment: {comment:?}");
        }
    }

    // [FR-121]
    #[test]
    fn goはコメントが単一トークンだけのときマーカーにする() {
        assert_marker_cases(|c| {
            let source = format!("{c}\nfunc TestA(t *testing.T) {{}}\n");
            find(&extract_go_tests("a/x_test.go", &source).unwrap(), "TestA")
                .markers
                .clone()
        });
    }

    // [FR-121]
    #[test]
    fn rustはコメントが単一トークンだけのときマーカーにする() {
        assert_marker_cases(|c| {
            rs(&format!("{c}\n#[test]\nfn t() {{}}\n"), &[])[0]
                .markers
                .clone()
        });
    }

    // [FR-121]
    #[test]
    fn csharpはコメントが単一トークンだけのときマーカーにする() {
        assert_marker_cases(|c| {
            cs(&cs_wrap(&format!("{c}\n[Fact]\npublic void T() {{ }}")))[0]
                .markers
                .clone()
        });
    }

    // [FR-121]
    #[test]
    fn javaはコメントが単一トークンだけのときマーカーにする() {
        assert_marker_cases(|c| {
            jv(&jv_wrap(&format!("{c}\n@Test\nvoid t() {{ }}")))[0]
                .markers
                .clone()
        });
    }

    // [FR-121]
    #[test]
    fn kotlinはコメントが単一トークンだけのときマーカーにする() {
        assert_marker_cases(|c| {
            kt(&kt_wrap(&format!("{c}\n@Test\nfun t() {{ }}")))[0]
                .markers
                .clone()
        });
    }

    // [FR-121]
    #[test]
    fn typescriptはコメントが単一トークンだけのときマーカーにする() {
        assert_marker_cases(|c| {
            ts(&format!("{c}\nit(\"t\", () => {{}});\n"))[0]
                .markers
                .clone()
        });
    }

    // [FR-121]
    #[test]
    fn dartはコメントが単一トークンだけのときマーカーにする() {
        assert_marker_cases(|c| {
            dt(&format!("void main() {{\n{c}\ntest('t', () {{}});\n}}\n"))[0]
                .markers
                .clone()
        });
    }
    // [FR-121]
    #[test]
    fn csharpの三連スラッシュコメントも単一トークンだけならマーカーにする() {
        let tests = cs(&cs_wrap("/// [FR-001]\n[Fact]\npublic void T() { }"));
        assert_eq!(tests[0].markers, vec!["FR-001".to_string()]);
        let tests = cs(&cs_wrap("/// [FR-001] 説明\n[Fact]\npublic void T() { }"));
        assert!(tests[0].markers.is_empty());
    }

    // ---- Dart / Flutter ----

    fn dt(source: &str) -> Vec<ExtractedTest> {
        extract_dart_tests("mobile/test/a_test.dart", source).unwrap()
    }

    // [FR-122]
    #[test]
    fn dartはtest_dartで終わるパスだけをテストファイルとして扱う() {
        assert!(is_dart_test_file("test/a_test.dart"));
        assert!(is_dart_test_file("a_test.dart"));
        assert!(!is_dart_test_file("test/a.dart"));
        assert!(!is_dart_test_file("test/a_test.dart.bak"));
        assert!(!is_dart_test_file("test/a_test.rs"));
        assert!(!is_dart_test_file("test/atest.dart"));
    }

    // [FR-123]
    #[test]
    fn dartはmain直下のtestとtestwidgetsを検出する() {
        let tests =
            dt("void main() {\n  test('a', () {});\n  testWidgets('b', (tester) async {});\n}\n");
        assert_eq!(tests.len(), 2);
        find(&tests, "a");
        find(&tests, "b");
    }

    // [FR-123]
    #[test]
    fn dartはgroupの入れ子を辿る() {
        let tests = dt("void main() {\n  group('outer', () {\n    group('inner', () {\n      test('t', () {});\n    });\n    test('u', () {});\n  });\n}\n");
        assert_eq!(tests.len(), 2);
        assert_eq!(find(&tests, "t").id, "mobile/test/a_test:outer/inner:t");
        assert_eq!(find(&tests, "u").id, "mobile/test/a_test:outer:u");
    }

    // [FR-125]
    #[test]
    fn dartのtest識別子はgroupの外なら空のgroupパスになる() {
        let tests = dt("void main() { test('t', () {}); }\n");
        assert_eq!(tests[0].id, "mobile/test/a_test::t");
    }

    // [FR-123]
    #[test]
    fn dartはfuture_void_main_asyncでも検出する() {
        let tests = dt("Future<void> main() async {\n  test('a', () {});\n}\n");
        assert_eq!(tests.len(), 1);
        let tests = dt("main() { test('a', () {}); }\n");
        assert_eq!(tests.len(), 1);
    }

    // [FR-123]
    #[test]
    fn dartはasyncコールバックとアロー本体のtestも検出する() {
        let tests = dt("void main() {\n  test('a', () async {});\n  test('b', () => expect(1, 1));\n  group('g', () async {\n    test('c', () {});\n  });\n}\n");
        assert_eq!(tests.len(), 3);
        assert_eq!(find(&tests, "c").id, "mobile/test/a_test:g:c");
    }

    // [FR-123]
    #[test]
    fn dartのgroupはアロー本体だと中へ降りない() {
        let tests = dt("void main() {\n  group('g', () => test('t', () {}));\n}\n");
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-123]
    #[test]
    fn dartは名前付き引数付きでも1件のtestとして扱う() {
        let tests = dt("void main() {\n  test('a', () {}, skip: true, timeout: Timeout.none);\n  group('g', () {\n    test('b', () {});\n  }, skip: 'x');\n}\n");
        assert_eq!(tests.len(), 2);
        assert_eq!(find(&tests, "b").id, "mobile/test/a_test:g:b");
    }

    // [FR-125]
    #[test]
    fn dartはraw文字列を引用符の内側のソース表記のまま名前にする() {
        let tests = dt("void main() {\n  test(r'a\\n$b', () {});\n  test(r\"c\\d\", () {});\n}\n");
        assert_eq!(tests.len(), 2);
        find(&tests, "a\\n$b");
        find(&tests, "c\\d");
    }

    // [FR-125]
    #[test]
    fn dartは隣接文字列連結を各部分の内側で連結して名前にする() {
        let tests = dt("void main() {\n  test('abc '\n      \"def \"\n      r'g\\h', () {});\n}\n");
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "abc def g\\h");
    }

    // [FR-125]
    #[test]
    fn dartはエスケープを解釈せずソース表記のまま名前にする() {
        let tests = dt("void main() {\n  test('it\\'s \\$x', () {});\n}\n");
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "it\\'s \\$x");
    }

    // [FR-125]
    #[test]
    fn dartは補間を含む名前のtestとgroupを無視する() {
        let tests = dt("void main() {\n  test('a $x', () {});\n  test('b ${y}', () {});\n  test('c' 'd$z', () {});\n  group('g$x', () {\n    test('t', () {});\n  });\n}\n");
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-125]
    #[test]
    fn dartは三重引用符の名前を無視する() {
        let tests = dt("void main() {\n  test('''a''', () {});\n  test(\"\"\"b\"\"\", () {});\n  test(r'''c''', () {});\n  test('d' '''e''', () {});\n}\n");
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-125]
    #[test]
    fn dartは無効な名前のgroup配下を走査しない() {
        for name in ["''", "'''x'''", "'a\u{2028}b'", "'a\u{1}b'"] {
            let source = format!(
                "void main() {{\n  group({name}, () {{\n    test('t', () {{}});\n  }});\n}}\n"
            );
            let tests = dt(&source);
            assert!(tests.is_empty(), "{name:?}: {tests:?}");
        }
    }

    // [FR-125]
    #[test]
    fn dartは行区切り文字や制御文字を含むtest名を無視する() {
        let tests = dt("void main() {\n  test('a\u{2028}b', () {});\n  test('c\u{1}d', () {});\n  test('ok', () {});\n}\n");
        assert_eq!(tests.len(), 1, "{tests:?}");
        assert_eq!(tests[0].name, "ok");
    }

    // [FR-125]
    #[test]
    fn dartは空文字名を無視する() {
        let tests = dt(
            "void main() {\n  test('', () {});\n  test('' '', () {});\n  test('ok', () {});\n}\n",
        );
        assert_eq!(tests.len(), 1);
        assert_eq!(tests[0].name, "ok");
    }

    // [FR-125]
    #[test]
    fn dartは第1引数が文字列リテラルでない呼び出しを無視する() {
        let tests = dt("void main() {\n  test(name, () {});\n  group(name, () {\n    test('t', () {});\n  });\n  test();\n  test(skip: true);\n}\n");
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-123]
    #[test]
    fn dartは修飾付きやメソッドチェーンやawait付きの呼び出しを無視する() {
        let tests = dt("import 'package:test/test.dart' as t;\nvoid main() {\n  t.test('a', () {});\n  t.group('g', () {\n    test('b', () {});\n  });\n  test('c', () {}).foo();\n  await test('d', () {});\n  test('e', () {});\n}\n");
        assert_eq!(tests.len(), 1, "{tests:?}");
        assert_eq!(tests[0].name, "e");
    }

    // [FR-123]
    #[test]
    fn dartはtestとgroupとtestwidgets以外の呼び出しを無視する() {
        let tests =
            dt("void main() {\n  setUp(() {});\n  it('a', () {});\n  Test('b', () {});\n}\n");
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-123]
    #[test]
    fn dartはforとifと関数とtest本体の中のtestを無視する() {
        let source = "void helper() {\n  test('h', () {});\n}\nvoid main() {\n  for (final x in [1]) {\n    test('f', () {});\n  }\n  if (true) {\n    test('i', () {});\n  }\n  test('outer', () {\n    test('inner', () {});\n  });\n  void local() {\n    test('l', () {});\n  }\n  group('g', () {\n    for (var i = 0; i < 2; i++) {\n      test('gf', () {});\n    }\n    test('ok', () {});\n  });\n}\n";
        let tests = dt(source);
        let mut names: Vec<&str> = tests.iter().map(|t| t.name.as_str()).collect();
        names.sort();
        assert_eq!(names, vec!["ok", "outer"], "{tests:?}");
    }

    // [FR-123]
    #[test]
    fn dartはmain以外のトップレベル関数を入口にしない() {
        let tests =
            dt("void other() {\n  test('a', () {});\n}\nvoid mainx() {\n  test('b', () {});\n}\n");
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-123]
    #[test]
    fn dartはmainが無いファイルを0件にする() {
        assert!(dt("int x = 1;\n").is_empty());
        assert!(dt("").is_empty());
    }

    // [FR-124]
    #[test]
    fn dartは直前に隣接するコメントをマーカーとして抽出し空行を挟むと無視する() {
        let source = "void main() {\n  // [FR-001]\n  test('a', () {});\n\n  // [FR-002]\n\n  test('b', () {});\n  /* [FR-003] */\n  test('c', () {});\n}\n";
        let tests = dt(source);
        assert_eq!(find(&tests, "a").markers, vec!["FR-001".to_string()]);
        assert!(find(&tests, "b").markers.is_empty());
        assert_eq!(find(&tests, "c").markers, vec!["FR-003".to_string()]);
    }

    // [FR-124]
    #[test]
    fn dartはgroup内のtestでも直前のコメントをマーカーにする() {
        let tests = dt(
            "void main() {\n  group('g', () {\n    // [FR-001]\n    test('t', () {});\n  });\n}\n",
        );
        assert_eq!(tests[0].markers, vec!["FR-001".to_string()]);
    }

    // [FR-124]
    #[test]
    fn dartのドキュメントコメントはマーカーにならない() {
        let source = "void main() {\n  /// [FR-001]\n  test('a', () {});\n  /** [FR-002] */\n  test('b', () {});\n  //// [FR-003]\n  test('c', () {});\n}\n";
        let tests = dt(source);
        for n in ["a", "b", "c"] {
            assert!(find(&tests, n).markers.is_empty(), "{n}");
        }
    }

    // [FR-124]
    #[test]
    fn dartはマーカーとtestの間にdocコメントが挟まってもマーカーを有効にする() {
        let source = "void main() {\n  // [FR-001]\n  /// 説明 [Foo]\n  test('a', () {});\n  // [FR-002]\n  /** doc */\n  test('b', () {});\n}\n";
        let tests = dt(source);
        assert_eq!(find(&tests, "a").markers, vec!["FR-001".to_string()]);
        assert_eq!(find(&tests, "b").markers, vec!["FR-002".to_string()]);
    }

    // [FR-124]
    #[test]
    fn dartは前の文の末尾行コメントを次のtestのマーカーにしない() {
        let tests = dt("void main() {\n  test('a', () {}); // [FR-001]\n  test('b', () {});\n}\n");
        assert!(find(&tests, "b").markers.is_empty());
    }

    // [FR-123]
    #[test]
    fn dartのcontentは呼び出し文全体でマーカーコメントを含まない() {
        let tests = dt("void main() {\n  // [FR-001]\n  test('a', () { expect(1, 1); });\n}\n");
        assert_eq!(tests[0].content, "test('a', () { expect(1, 1); });");
    }

    // [FR-123]
    #[test]
    fn dartの本体変更はcontentに反映される() {
        let a = dt("void main() { test('a', () { expect(1, 1); }); }\n");
        let b = dt("void main() { test('a', () { expect(1, 2); }); }\n");
        assert_ne!(a[0].content, b[0].content);
    }

    // [FR-123]
    #[test]
    fn dartは同名のtestをそのまま複数返す() {
        let tests = dt("void main() {\n  test('a', () {});\n  test('a', () {});\n}\n");
        assert_eq!(tests.len(), 2);
        assert_eq!(tests[0].id, tests[1].id);
    }

    // [FR-014]
    #[test]
    fn dartは構文エラーのソースでエラーを返す() {
        let source = "void main() {\n  test('a', () {\n}\n";
        assert!(extract_dart_tests("mobile/test/a_test.dart", source).is_err());
    }

    // ---- Kotlin の位置引数付き注釈(FR-063) ----

    const KOTLIN_KDOC_FIXTURE: &str = include_str!("../fixtures/kotlin_kdoc_annotation.kt");
    const KOTLIN_SUPPORT_FIXTURE: &str = include_str!("../fixtures/kotlin_annotation_support.kt");

    // [FR-063]
    #[test]
    fn kotlinは直前の宣言とコメントの後の位置引数付き注釈でも解析できる() {
        let tests = kt("fun g() {}\n// a\n@S(\"L\")\nfun f() {}\n");
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-063]
    #[test]
    fn kotlinは非asciiのkdocと位置引数付き注釈の組み合わせでも解析できる() {
        let tests = kt(KOTLIN_KDOC_FIXTURE);
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-063]
    #[test]
    fn kotlinは位置引数付き注釈を持つサポートコードのファイルを解析できる() {
        let tests = kt(KOTLIN_SUPPORT_FIXTURE);
        assert!(tests.is_empty(), "{tests:?}");
    }

    // [FR-063]
    #[test]
    fn kotlinは位置引数付き注釈の後ろにtestが続いてもtestを取る() {
        let tests = kt("fun g(){}\n// a\n@DisplayName(\"x\")\n@Test\nfun f(){}\n");
        assert_eq!(tests.len(), 1, "{tests:?}");
        assert_eq!(tests[0].name, "f");
    }

    // [FR-063]
    #[test]
    fn kotlinは位置引数付き注釈の後ろのtestのマーカーも取る() {
        let tests = kt("fun g(){}\n// a\n// [FR-001]\n@DisplayName(\"x\")\n@Test\nfun f(){}\n");
        assert_eq!(tests.len(), 1, "{tests:?}");
        assert_eq!(tests[0].markers, vec!["FR-001".to_string()]);
    }

    // [FR-014]
    #[test]
    fn kotlinは位置引数付き注釈のある本当の構文エラーを引き続きエラーにする() {
        let source = "fun g() {}\n// a\n@S(\"L\")\nfun f( {\n";
        assert!(extract_kotlin_tests("tests/A.kt", source).is_err());
    }

    // [FR-063]
    #[test]
    fn kotlinは位置引数付き注釈が複数あってもtestを取る() {
        let tests = kt("fun g(){}\n// a\n@A(\"x\")\n@B(\"y\")\n@Test\nfun f(){}\n");
        assert_eq!(tests.len(), 1, "{tests:?}");
    }
}