big-code-analysis 2.0.0

Tool to compute and export code metrics
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
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
// Per-language metric and AST modules deliberately consume the macro-
// generated tree-sitter token enums via `use crate::*` and `use Foo::*`
// inside match expressions — explicit imports would list dozens of
// variants per arm and obscure the per-language token sets that are the
// point of these files. Allowed at the module level rather than per
// function so the per-language impl blocks stay readable.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]
// WMC stores cumulative cyclomatic as `f64` internally but exposes
// integral `u64` accessors (#530); the `f64 as u64` / `u64 as f64` casts
// are bounded by the counts they came from.
#![allow(
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss
)]

use std::fmt;

use crate::checker::Checker;
use crate::macros::implement_metric_trait;
use crate::*;

/// The `Wmc` metric.
///
/// This metric sums the cyclomatic complexities of all the methods defined in a class.
/// The `Wmc` (Weighted Methods per Class) is an object-oriented metric for classes.
///
/// Original paper and definition:
/// <https://www.researchgate.net/publication/3187649_Kemerer_CF_A_metric_suite_for_object_oriented_design_IEEE_Trans_Softw_Eng_206_476-493>
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct Stats {
    cyclomatic: f64,
    // Cumulative cyclomatic carried by descendant Class / Interface
    // spaces (anonymous classes, nested object literals, …). A method
    // that *contains* a nested class must not fold that class's
    // complexity into its enclosing class's WMC — the nested class is
    // its own WMC scope and already counts those methods. Tracking the
    // nested-class cyclomatic lets `merge` subtract it from a Function's
    // contribution, preventing double-attribution (#463).
    nested_class_cyclomatic: f64,
    class_wmc: f64,
    interface_wmc: f64,
    class_wmc_sum: f64,
    interface_wmc_sum: f64,
    space_kind: SpaceKind,
}

impl fmt::Display for Stats {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "classes: {}, interfaces: {}, total: {}",
            self.class_wmc_sum(),
            self.interface_wmc_sum(),
            self.total_wmc()
        )
    }
}

impl Stats {
    /// Merges a second `Wmc` metric into the first one
    pub fn merge(&mut self, other: &Stats) {
        use SpaceKind::*;

        // Rolls a child space's cyclomatic into the enclosing class /
        // interface WMC, subtracting any nested-class complexity so it is
        // not double-counted (#463). See the per-arm rationale below.
        match other.space_kind {
            // A method contributes its own cyclomatic minus the
            // complexity already claimed by nested class / interface
            // spaces it contains. Those nested classes form their own WMC
            // scope (their members roll up via `class_wmc_sum`), so the
            // method must not re-add them. Its nested-class total also
            // bubbles up so an *ancestor* method can exclude this whole
            // subtree in turn.
            Function => {
                let own_cyclomatic = other.cyclomatic - other.nested_class_cyclomatic;
                match self.space_kind {
                    Class => self.class_wmc += own_cyclomatic,
                    Interface => self.interface_wmc += own_cyclomatic,
                    _ => {}
                }
                self.nested_class_cyclomatic += other.nested_class_cyclomatic;
            }
            // A nested Class / Interface space (e.g. an anonymous class)
            // contributes its *cumulative* cyclomatic — which already
            // subsumes any classes nested inside it — so we record only
            // `other.cyclomatic` here, never also its
            // `nested_class_cyclomatic` (that would double-count the
            // inner classes, see #463 nested-anonymous case).
            Class | Interface => self.nested_class_cyclomatic += other.cyclomatic,
            _ => {}
        }

        self.class_wmc_sum += other.class_wmc_sum;
        self.interface_wmc_sum += other.interface_wmc_sum;
    }

    /// Returns the `Wmc` metric value of the classes in a space.
    #[inline]
    #[must_use]
    pub fn class_wmc(&self) -> u64 {
        self.class_wmc as u64
    }

    /// Returns the `Wmc` metric value of the interfaces in a space.
    #[inline]
    #[must_use]
    pub fn interface_wmc(&self) -> u64 {
        self.interface_wmc as u64
    }

    /// Returns the sum of the `Wmc` metric values of the classes in a space.
    #[inline]
    #[must_use]
    pub fn class_wmc_sum(&self) -> u64 {
        self.class_wmc_sum as u64
    }

    /// Returns the sum of the `Wmc` metric values of the interfaces in a space.
    #[inline]
    #[must_use]
    pub fn interface_wmc_sum(&self) -> u64 {
        self.interface_wmc_sum as u64
    }

    /// Returns the total `Wmc` metric value in a space.
    #[inline]
    #[must_use]
    pub fn total_wmc(&self) -> u64 {
        self.class_wmc_sum() + self.interface_wmc_sum()
    }

    // Accumulates the `Wmc` metric values
    // of classes and interfaces into the sums
    #[inline]
    pub(crate) fn compute_sum(&mut self) {
        self.class_wmc_sum += self.class_wmc;
        self.interface_wmc_sum += self.interface_wmc;
    }

    // Checks if the `Wmc` metric is disabled
    #[inline]
    pub(crate) fn is_disabled(&self) -> bool {
        matches!(self.space_kind, SpaceKind::Function | SpaceKind::Unknown)
    }
}

#[doc(hidden)]
/// Per-language computation of weighted methods per class.
pub(crate) trait Wmc
where
    Self: Checker,
{
    /// Walk `node` and update `stats` with this metric for the language
    /// implementing the trait.
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats);
}

// Shared WMC compute for languages with class / interface / function /
// unit space kinds (Java, C#, Kotlin). Records the space kind once and,
// for function spaces, captures the cyclomatic sum so the aggregator can
// roll it into the enclosing class / interface.
fn class_interface_compute(
    space_kind: SpaceKind,
    cyclomatic: &cyclomatic::Stats,
    stats: &mut Stats,
) {
    use SpaceKind::*;

    if let Unit | Class | Interface | Function = space_kind {
        if stats.space_kind == Unknown {
            stats.space_kind = space_kind;
        }
        // Record the cumulative cyclomatic for Function spaces (the
        // method's WMC contribution) and for Class / Interface spaces
        // (so an ancestor method can subtract a nested class's
        // complexity from its own contribution — see `merge`, #463).
        if let Function | Class | Interface = space_kind {
            stats.cyclomatic = cyclomatic.cyclomatic_sum() as f64;
        }
    }
}

impl Wmc for JavaCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

impl Wmc for GroovyCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

impl Wmc for CsharpCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

// Kotlin's `class_declaration` becomes either `Class` or `Interface` via
// `Getter::get_space_kind` (the keyword child disambiguates). `object`
// singletons map to `Class`. Function spaces (top-level `fun`, member
// `fun`, secondary constructors, lambdas, anonymous functions) contribute
// their cyclomatic to the enclosing class / interface. `companion_object`
// IS a `func_space` and opens its own `Class` scope (#431): `Checker::
// is_func_space` matches `Kotlin::CompanionObject` and `Getter::
// get_space_kind` maps it to `SpaceKind::Class`, so its members accrue to
// the companion's own class-WMC rather than folding into the surrounding
// class. See `kotlin_companion_object_opens_class_space` below.
impl Wmc for KotlinCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

impl Wmc for PhpCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        use SpaceKind::*;

        // Anonymous classes, enums, and traits all map to `Class` via
        // `Getter::get_space_kind`, so a single `Class` arm covers them.
        if let Unit | Class | Interface | Function = space_kind {
            if stats.space_kind == Unknown {
                stats.space_kind = space_kind;
            }
            // Record cyclomatic for Function spaces (the method's WMC
            // contribution) and for Class / Interface spaces (so an
            // ancestor method can exclude a nested class's complexity —
            // see `merge`, #463; matters for PHP `AnonymousClass` nested
            // inside a method).
            if let Function | Class | Interface = space_kind {
                stats.cyclomatic = cyclomatic.cyclomatic_sum() as f64;
            }
        }
    }
}

// Python WMC. The shared `class_interface_compute` already does the
// right thing for the four space kinds Python produces:
// - `Unit` (module-level — receives WMC totals from top-level
//   classes, mirroring the Java unit-space aggregation).
// - `Class` (every `ClassDefinition`).
// - `Function` (every `FunctionDefinition`; captures the
//   per-function cyclomatic sum that the aggregator rolls up into
//   the enclosing class).
// - `Unknown` (anything else — skipped).
//
// Lambdas (`Lambda`) are not `is_func` and therefore do not open a
// `Function` space, so they correctly do *not* contribute to WMC
// — they are anonymous expressions, not methods.
impl Wmc for PythonCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

// Rust WMC. Rust's `Impl` / `Trait` space kinds map onto the OO
// "class" / "interface" concept for WMC purposes: every `impl` block
// is a class, every `trait` is an interface, and each `function_item`
// inside contributes its cyclomatic complexity to the surrounding
// space.
//
// `class_interface_compute` is reused after mapping the space kind:
// the Wmc `Stats.space_kind` field is the recipient that
// `Stats::merge` keys off when rolling per-function cyclomatics into
// the parent. Mapping to `Class` / `Interface` means the existing
// merge logic produces the right numbers without touching the shared
// helpers.
//
// Multiple `impl Foo` blocks each open their own Impl space and
// accumulate independently; their `class_wmc_sum` values are merged
// into the parent space (Unit) during finalisation, so the
// file-level `class_wmc_sum` is the sum of cyclomatic complexity
// across every impl block in the file.
impl Wmc for RustCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        let mapped = match space_kind {
            SpaceKind::Impl => SpaceKind::Class,
            SpaceKind::Trait => SpaceKind::Interface,
            other => other,
        };
        class_interface_compute(mapped, cyclomatic, stats);
    }
}

// C++ WMC. C++'s `class_specifier` and `struct_specifier` both map to
// classes from the OO-metric perspective — `struct` and `class` differ
// only in default visibility, not in their ability to hold methods.
// The `Getter::get_space_kind` impl emits `SpaceKind::Struct` for
// `struct_specifier`, so we collapse it onto `Class` before delegating
// to `class_interface_compute`.
//
// `SpaceKind::Namespace` is intentionally dropped — namespaces are not
// classes; their member functions are free functions and do not
// contribute to a per-class WMC. The Unit space still accumulates the
// per-class sums for file-level reporting.
impl Wmc for CppCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        let mapped = match space_kind {
            SpaceKind::Struct => SpaceKind::Class,
            other => other,
        };
        class_interface_compute(mapped, cyclomatic, stats);
    }
}

impl Wmc for MozcppCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        let mapped = match space_kind {
            SpaceKind::Struct => SpaceKind::Class,
            other => other,
        };
        class_interface_compute(mapped, cyclomatic, stats);
    }
}

// Objective-C: `@implementation` is a `SpaceKind::Class` and `@interface`
// / `@protocol` are `SpaceKind::Interface` (see `getter.rs`); each
// `method_definition` opens a `SpaceKind::Function` whose cyclomatic sum
// rolls into its enclosing class via the shared aggregator — the same
// shape as Java / TS. ObjC has no `struct`-as-class, so no `Struct`
// remap is needed.
impl Wmc for ObjcCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

// TypeScript / TSX both expose `class_declaration`,
// `abstract_class_declaration` (mapped to `SpaceKind::Class` in
// `getter.rs`) and `interface_declaration` (`SpaceKind::Interface`).
// Method bodies live in `method_definition` and `arrow_function`
// function spaces; their cyclomatic sums roll into the enclosing
// class / interface via `class_interface_compute`. Abstract method
// signatures (`abstract_method_signature`) have no body and so
// contribute zero to WMC, matching Java's `abstract` method rule.
impl Wmc for TypescriptCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

impl Wmc for TsxCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

// Ruby's `Class` and `SingletonClass` map to `SpaceKind::Class` via
// `Getter::get_space_kind`; `Module` maps to `SpaceKind::Namespace`
// and so does not contribute a `Wmc` bucket of its own. Every Ruby
// `Method` / `SingletonMethod` is a `SpaceKind::Function` whose
// cyclomatic sum rolls into the enclosing class via
// `class_interface_compute`. Ruby has no interface construct, so the
// `Interface` arm is unreachable but harmless.
impl Wmc for RubyCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

// JavaScript / Mozjs WMC. JS classes (`class_declaration`,
// `class_expression`) both map to `SpaceKind::Class` in `getter.rs`,
// and method bodies are `method_definition` / `arrow_function`
// function spaces — the same shape as TS/Java. `class_interface_compute`
// rolls per-function cyclomatic sums into the enclosing class.
impl Wmc for JavascriptCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

impl Wmc for MozjsCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

// Go WMC is intentionally a no-op. Go has no `class` syntactic
// construct; methods are declared as `MethodDeclaration` nodes
// attached to a receiver type that lives elsewhere as a `StructType`.
// The Wmc trait signature receives only the per-space `SpaceKind` and
// cyclomatic stats — it cannot tell which `Function` space corresponds
// to a `MethodDeclaration` (receiver method) versus a free-standing
// `FunctionDeclaration`, and the FuncSpace tree exposes no
// per-receiver "class" space to attribute methods to. Implementing
// Wmc correctly per the issue's "methods grouped by receiver = class"
// rule would require either a new `SpaceKind::Struct` variant for Go
// receiver methods or a richer trait signature, both of which are
// out of scope. See the issue body's explicit option (a): "keep
// scoring zero with a documented reason".

// Elixir WMC. Classes (`defmodule`) and Functions (`def` / `defp` /
// `defmacro` / `defmacrop`) are detected by source-aware Checker /
// Getter dispatch (#275), so the FuncSpace tree already carries the
// right `SpaceKind`s. Each method-defining macro opens a Function
// space whose cyclomatic complexity rolls into its enclosing
// `defmodule` Class via the shared aggregator. `defp` (private) is
// included — it is still a *method* of the class even though it is
// not part of the public API (the npm-style "public" filter belongs
// in `Npm`, not `Wmc`).
impl Wmc for ElixirCode {
    fn compute(space_kind: SpaceKind, cyclomatic: &cyclomatic::Stats, stats: &mut Stats) {
        class_interface_compute(space_kind, cyclomatic, stats);
    }
}

// Default no-op `Wmc` impls. Audited in #188. See the rationale block
// on `implement_metric_trait!(Npa, …)` in `src/metrics/npa.rs`. Wmc needs
// the same per-language class / method detection plumbing (Wmc =
// sum-of-cyclomatic-per-method), and mirrors Npa's set EXCEPT Go: Npa/Npm
// implement Go but Wmc no-ops it because Go's flat space model cannot
// attribute methods to a receiver class (see the Go rationale block above).
implement_metric_trait!(
    Wmc,
    CCode,
    PreprocCode,
    CcommentCode,
    GoCode,
    PerlCode,
    BashCode,
    LuaCode,
    TclCode,
    IrulesCode
);

#[cfg(test)]
#[allow(
    clippy::float_cmp,
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::similar_names,
    clippy::doc_markdown,
    clippy::needless_raw_string_hashes,
    clippy::too_many_lines
)]
mod tests {
    use crate::tools::{assert_child_space_kind, check_func_space, check_metrics};

    use super::*;

    #[test]
    fn java_single_class() {
        check_metrics::<JavaParser>(
            "public class Example { // wmc = 13

                public boolean m1(boolean a, boolean b) { // +1
                    boolean r = false;
                    if (a && b == a || b) { // +3
                        r = true;
                    }
                    return r;
                }

                public boolean m2(int n) { // +1
                    for (int i = 0; i < n; i++) { // +1
                        int j = n;
                        while (j > i) { // +1
                            j--;
                        }
                    }
                    return (n % 2 == 0) ? true : false; // +1
                }

                public int m3(int x, int y, int z) { // +1
                    int ret;
                    try {
                        z = x/y + y/x;
                    } catch (ArithmeticException e) { // +1
                        z = (x == 0) ? -1 : -2; // +1
                    }
                    switch (z) {
                        case -1: // +1
                            ret = y * y;
                            break;
                        case -2: // +1
                            ret = x * x;
                            break;
                        default:
                            ret = x + y;
                    }
                    return ret;
                }
            }",
            "foo.java",
            |metric| {
                // 1 class
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 13,
                  "interface_wmc_sum": 0,
                  "total": 13
                }
                "#
                );
            },
        );
    }

    #[test]
    fn groovy_single_class() {
        // WMC = sum of method cyclomatic complexities for the class.
        check_metrics::<GroovyParser>(
            "class Example {
                boolean m1(boolean a, boolean b) {
                    boolean r = false
                    if (a && b == a || b) {
                        r = true
                    }
                    return r
                }
                boolean m2(int n) {
                    for (int i = 0; i < n; i++) {
                        int j = n
                        while (j > i) {
                            j--
                        }
                    }
                    return (n % 2 == 0) ? true : false
                }
            }",
            "foo.groovy",
            |metric| {
                // m1: entry(1) + if(1) + &&(1) + ||(1) = 4
                // m2: entry(1) + for(1) + while(1) + ternary(1) = 4
                // WMC = 4 + 4 = 8
                assert_eq!(metric.wmc.class_wmc_sum(), 8);
            },
        );
    }

    #[test]
    fn groovy_empty_class() {
        check_metrics::<GroovyParser>("class Empty {}", "foo.groovy", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 0);
        });
    }

    #[test]
    fn groovy_class_with_single_method() {
        check_metrics::<GroovyParser>(
            "class A {
                void foo() {
                    println 'hi'
                }
            }",
            "foo.groovy",
            |metric| {
                // single method has entry +1 = 1
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
            },
        );
    }

    #[test]
    fn groovy_multiple_classes() {
        check_metrics::<GroovyParser>(
            "class A {
                void f() { if (true) {} }
            }
            class B {
                void g() {}
            }",
            "foo.groovy",
            |metric| {
                // A.f: 1 + 1 (if) = 2, B.g: 1 → total = 3
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
            },
        );
    }

    #[test]
    fn groovy_class_with_branching_methods() {
        check_metrics::<GroovyParser>(
            "class Calc {
                int abs(int x) {
                    if (x < 0) {
                        return -x
                    }
                    return x
                }
                int sign(int x) {
                    if (x > 0) return 1
                    if (x < 0) return -1
                    return 0
                }
            }",
            "foo.groovy",
            |metric| {
                // abs: 1 + 1 (if) = 2; sign: 1 + 2 (two ifs) = 3 → 5
                assert_eq!(metric.wmc.class_wmc_sum(), 5);
            },
        );
    }

    #[test]
    fn groovy_interface_wmc_is_zero() {
        // Interfaces declare method signatures with no body — wmc = 0.
        check_metrics::<GroovyParser>(
            "interface I {
                void a()
                void b()
            }",
            "foo.groovy",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
            },
        );
    }

    #[test]
    fn groovy_static_nested_class() {
        // Mirrors `java_static_nested_class`: nested classes get
        // their own WMC space tied to their parent class's scope.
        check_metrics::<GroovyParser>(
            "class TopLevelClass {
                static class StaticNestedClass {
                    private void m() {
                        println 'Test'
                    }
                }
            }",
            "foo.groovy",
            |metric| {
                // TopLevelClass(0) + StaticNestedClass(1 = entry only).
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
            },
        );
    }

    #[test]
    #[ignore = "dekobon Groovy grammar v1 does not yet support inner classes inside class bodies"]
    fn groovy_nested_inner_classes_wmc() {
        // Three nested classes each with one trivial method.
        // Mirrors `java_nested_inner_classes` (wmc.rs flavor).
        check_metrics::<GroovyParser>(
            "class X {
                void a() {}
                class Y {
                    void b() {}
                    class Z {
                        void c() {}
                    }
                }
            }",
            "foo.groovy",
            |metric| {
                // 3 classes, each with one method => 1 + 1 + 1 = 3.
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
            },
        );
    }

    #[test]
    fn groovy_local_inner_class() {
        // A class declared inside a method body. WMC counts its method
        // like any other class, and the local class is its own WMC scope:
        // `Local.l` (entry 1 + if 1 = 2) must not also fold into
        // `Outer.m`'s contribution to `Outer`. `Outer.m` itself = 1, so
        // total = 1 + 2 = 3 with no double-attribution (#463; the prior
        // value of 6 double-counted the local class's complexity into both
        // scopes, compounded by the Groovy grammar wrapping `m`'s body in a
        // `closure`).
        check_metrics::<GroovyParser>(
            "class Outer {
                void m() {
                    class Local {
                        void l() {
                            if (true) {}
                        }
                    }
                }
            }",
            "foo.groovy",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
            },
        );
    }

    #[test]
    #[ignore = "dekobon Groovy grammar v1 does not yet support anonymous inner classes (`new T() { … }`)"]
    fn groovy_anonymous_inner_class_wmc() {
        // `new Runnable() { ... }` anonymous inner class. WMC
        // includes the inner's method bodies.
        check_metrics::<GroovyParser>(
            "abstract class Base {
                abstract void m1()
            }
            class Top {
                void m() {
                    def b = new Base() {
                        void m1() {
                            for (int i = 0; i < 5; i++) {
                                println(i)
                            }
                        }
                    }
                }
            }",
            "foo.groovy",
            |metric| {
                // Base.m1(1) + Top.m(1) + anonymous.m1(1+for(1)) = 4
                assert_eq!(metric.wmc.class_wmc_sum(), 4);
            },
        );
    }

    #[test]
    fn groovy_lambda_expression_wmc() {
        // Lambdas inside a method body don't form their own class
        // space, but the surrounding methods still count toward WMC.
        check_metrics::<GroovyParser>(
            "class Top {
                void m1() {
                    def list = [1, 2, 3]
                    list.each { n -> println(n) }
                }
                void m2() {
                    if (true) {}
                }
            }",
            "foo.groovy",
            |metric| {
                // m1(1) + m2(1 + if(1)) = 3
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
            },
        );
    }

    #[test]
    fn groovy_single_interface_wmc() {
        // Default methods inside an interface contribute to WMC.
        // Mirrors `java_single_interface`.
        check_metrics::<GroovyParser>(
            "interface Example {
                default boolean m1(boolean a, boolean b) {
                    return (a && b == a || b)
                }
                default int m2(int n) {
                    return (n != 0) ? 1/n : n
                }
                void m3()
            }",
            "foo.groovy",
            |metric| {
                // m1(1 + && + ||) + m2(1 + ternary) + m3(1) = 6
                assert_eq!(metric.wmc.interface_wmc_sum(), 6);
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
            },
        );
    }

    #[test]
    #[ignore = "dekobon Groovy grammar v1 does not yet support inner classes inside interface bodies"]
    fn groovy_class_in_interface() {
        // Inner class inside an interface — its methods count
        // toward `class_wmc`, not `interface_wmc`.
        check_metrics::<GroovyParser>(
            "interface Outer {
                void api()
                class Inner {
                    void f() {
                        if (true) {}
                    }
                }
            }",
            "foo.groovy",
            |metric| {
                // Outer interface: api(1) = 1; Inner class: f(1+if) = 2.
                assert_eq!(metric.wmc.interface_wmc_sum(), 1);
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
            },
        );
    }

    // Regression for issue #280: Groovy enum bodies fold method-level
    // cyclomatic into `class_wmc_sum` just like Java.
    #[test]
    fn groovy_enum_wmc_aggregates_method_complexity() {
        check_metrics::<GroovyParser>(
            "enum Status {
                ACTIVE, INACTIVE;
                public int code(int n) {
                    if (n > 0) { return n }
                    return 0
                }
            }",
            "foo.groovy",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
            },
        );
    }

    // Mirror of `java_annotation_type_opens_interface_space_with_zero_wmc`
    // — verifies #280 wired `Groovy::AnnotationTypeDeclaration` into
    // `is_func_space` (the structural check) while keeping
    // `interface_wmc_sum` at 0 because elements are not method
    // declarations. The structural assertion is what distinguishes a
    // working fix from a vacuous one (see the Java sibling for the
    // rationale).
    #[test]
    #[ignore = "dekobon Groovy grammar v1 does not support annotation type elements with `default` values"]
    fn groovy_annotation_type_opens_interface_space_with_zero_wmc() {
        check_func_space::<GroovyParser, _>(
            "public @interface Marker {
                String value() default \"\";
                int priority() default 0;
            }",
            "foo.groovy",
            |func_space| {
                assert_eq!(func_space.metrics.wmc.interface_wmc_sum(), 0);
                assert_child_space_kind(&func_space, "Marker", SpaceKind::Interface);
            },
        );
    }

    // Constructors are considered as methods
    // Reference: https://pdepend.org/documentation/software-metrics/weighted-method-count.html
    #[test]
    fn java_multiple_classes() {
        check_metrics::<JavaParser>(
            "public class MainClass { // wmc = 3
                private int a;
                public MainClass() { // +1
                    a = 0;
                }
                public void setA(int n) { // +1
                    a = n;
                }
                public int getA() { // +1
                    return a;
                }
            }

            class TopLevelClass { // wmc = 2
                private int b;
                public TopLevelClass() { // +1
                    b = 0;
                }
                public int getB() { // +1
                    return b;
                }
            }",
            "foo.java",
            |metric| {
                // 2 classes (3 + 2)
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 5,
                  "interface_wmc_sum": 0,
                  "total": 5
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_static_nested_class() {
        check_metrics::<JavaParser>(
            "public class TopLevelClass { // wmc = 0
                public static class StaticNestedClass { // wmc = 1
                    private void m() { // +1
                        System.out.println(\"Test\");
                    }
                }
            }",
            "foo.java",
            |metric| {
                // 2 classes (0 + 2)
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 1,
                  "interface_wmc_sum": 0,
                  "total": 1
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_nested_inner_classes() {
        check_metrics::<JavaParser>(
            "public class TopLevelClass { // wmc = 2
                private int a;

                class InnerClassBefore { // wmc = 1
                    private boolean b = (a % 2 == 0) ? true : false;
                    public boolean getB() { // +1
                        return b;
                    }
                }

                public TopLevelClass(int n) { // +1
                    if (a != n) { // +1
                        a = n;
                    }
                }

                class InnerClassAfter { // wmc = 2
                    private int c = a;

                    public int getC() { // +1
                        return c;
                    }
                    public void setC(int n) { // +1
                        c = n;
                    }

                    class InnerClass1 { // wmc = 1
                        private int p1;
                        class InnerClass2 { // wmc = 1
                            private int p2;
                            public int getP2() { // +1
                                return p2;
                            }
                            class InnerClass3 { // wmc = 2
                                private int p3;
                                public int getP3() { // +1
                                    return p3;
                                }
                                public void setP3(int n) { // +1
                                    p3 = n;
                                }
                            }
                        }
                        public void setP1(int n) { // +1
                            p1 = n;
                        }
                    }
                }
            }",
            "foo.java",
            |metric| {
                // 6 classes (2 + 1 + 2 + 1 + 1 + 2)
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 9,
                  "interface_wmc_sum": 0,
                  "total": 9
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_local_inner_class() {
        check_metrics::<JavaParser>(
            "import java.util.LinkedList;
            import java.util.List;

            public final class FinalClass { // wmc = 1 (test only)
                private int a = 1;
                public void test() { // +1
                    final List<String> localList = new LinkedList<String>();

                    class LocalInnerClass { // wmc = 2 (print only)
                        private int b = (a == 1) ? 1 : 0; // field init, not a method
                        public void print() { // +1
                            for ( String s : localList ) { // +1
                                System.out.println(s);
                            }
                        }
                    }
                }
            }",
            "foo.java",
            |metric| {
                // Two classes: `FinalClass` (its only method `test` = 1)
                // and the local `LocalInnerClass` (its only method `print`
                // = base 1 + for 1 = 2). The ternary in `b`'s initializer
                // is class-body cyclomatic, not a method, so it does not
                // count toward WMC. `LocalInnerClass` is its own WMC scope,
                // so its complexity must NOT also fold into `test`'s
                // contribution to `FinalClass` — total = 1 + 2 = 3, with no
                // double-attribution (#463; previously the local class's
                // complexity was double-counted into both scopes, giving an
                // inflated 7).
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 3,
                  "interface_wmc_sum": 0,
                  "total": 3
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_anonymous_inner_class() {
        check_metrics::<JavaParser>(
            "abstract class AbstractClass { // wmc = 1
                abstract void m1(); // +1
            }
            public class TopLevelClass{ // wmc = 3
                public void m(){ // +1
                    AbstractClass ac1 = new AbstractClass() {
                        void m1() { // +1
                            for (int i = 0; i < 5; i++) { // +1
                                System.out.println(\"Test 1: \" + i);
                            }
                        }
                    };
                    ac1.m1();
                }
            }",
            "foo.java",
            |metric| {
                // 2 classes (1 + 3)
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 4,
                  "interface_wmc_sum": 0,
                  "total": 4
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_nested_anonymous_inner_classes() {
        check_metrics::<JavaParser>(
            "abstract class AbstractClass{ // wmc = 2
                abstract void m1(); // +1
                abstract void m2(); // +1
            }
            public class TopLevelClass{ // wmc = 6
                public void m(){ // +1

                    AbstractClass ac1 = new AbstractClass() {
                        void m1() { // +1
                            for (int i = 0; i < 5; i++) { // +1
                                System.out.println(\"Test 1: \" + i);
                            }
                        }
                        void m2() { // +1
                            AbstractClass ac2 = new AbstractClass() {
                                void m1() { // +1
                                    System.out.println(\"Test A\");
                                }
                                void m2() { // +1
                                    System.out.println(\"Test B\");
                                }
                            };
                            ac2.m2();
                            System.out.println(\"Test 2\");
                        }
                    };
                    ac1.m1();
                }
            }",
            "foo.java",
            |metric| {
                // 2 classes (2 + 6)
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 8,
                  "interface_wmc_sum": 0,
                  "total": 8
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_lambda_expression() {
        check_metrics::<JavaParser>(
            "import java.util.ArrayList;

            public class TopLevelClass { // wmc = 2
                private ArrayList<Integer> numbers;

                public void m1() { // +1
                    numbers = new ArrayList<Integer>();
                    numbers.add(1);
                    numbers.add(2);
                    numbers.add(3);
                }

                public void m2() { // +1
                    numbers.forEach( (n) -> { System.out.println(n); } );
                }
            }",
            "foo.java",
            |metric| {
                // 1 class
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 2,
                  "interface_wmc_sum": 0,
                  "total": 2
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_single_interface() {
        check_metrics::<JavaParser>(
            "interface Example { // wmc = 6
                default boolean m1(boolean a, boolean b) { // +1
                    return (a && b == a || b); // +2
                }
                default int m2(int n) { // +1
                    return (n != 0) ? 1/n : n; // +1
                };
                void m3(); // +1
            }",
            "foo.java",
            |metric| {
                // 1 interface
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 0,
                  "interface_wmc_sum": 6,
                  "total": 6
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_multiple_interfaces() {
        check_metrics::<JavaParser>(
            "interface FirstInterface { // wmc = 1
                int a = 0;
                default int getA() { // +1
                    return a;
                }
            }

            interface SecondInterface { // wmc = 2
                void setB(int n); // +1
                int getB(); // +1
            }",
            "foo.java",
            |metric| {
                // 2 interfaces (1 + 2)
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 0,
                  "interface_wmc_sum": 3,
                  "total": 3
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_nested_inner_interfaces() {
        check_metrics::<JavaParser>(
            "interface TopLevelInterface { // wmc = 1
                interface InnerInterfaceBefore { // wmc = 1
                    void m1(); // +1
                }

                void m2(); // +1

                interface InnerInterfaceAfter { // wmc = 2
                    void m3(); // +1
                    interface InnerInterface { // wmc = 1
                        void m4(); // +1
                    }
                    void m5(); // +1
                }
            }",
            "foo.java",
            |metric| {
                // 4 interfaces (1 + 1 + 2 + 1)
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 0,
                  "interface_wmc_sum": 5,
                  "total": 5
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_class_in_interface() {
        check_metrics::<JavaParser>(
            "interface TopLevelInterface { // wmc = 2
                int getA(); // +1
                boolean getB(); // +1

                class InnerClass { // wmc = 2
                    float c;
                    double d;
                    float getC() { // +1
                        return c;
                    }
                    double getD() { // +1
                        return d;
                    }
                }
            }",
            "foo.java",
            |metric| {
                // 1 class 1 interface
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 2,
                  "interface_wmc_sum": 2,
                  "total": 4
                }
                "#
                );
            },
        );
    }

    #[test]
    fn java_interface_in_class() {
        check_metrics::<JavaParser>(
            "class TopLevelClass { // wmc = 2
                int a;
                boolean b;
                int getA() { // +1
                    return a;
                }
                boolean getB() { // +1
                    return b;
                }

                interface InnerInterface { // wmc = 2
                    float getC(); // +1
                    double getD(); // +1
                }
            }",
            "foo.java",
            |metric| {
                // 1 class 1 interface
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 2,
                  "interface_wmc_sum": 2,
                  "total": 4
                }
                "#
                );
            },
        );
    }

    // Regression for issue #280: Java `EnumDeclaration` opens a class
    // space, so method-level cyclomatic complexity inside the enum
    // body folds into `class_wmc_sum`.
    #[test]
    fn java_enum_wmc_aggregates_method_complexity() {
        check_metrics::<JavaParser>(
            "enum Status {
                ACTIVE, INACTIVE;
                public int code(int n) {        // entry +1
                    if (n > 0) {                // if +1
                        return n;
                    }
                    return 0;
                }
            }",
            "foo.java",
            |metric| {
                // 1 enum (class), 1 method with cyclomatic = 2.
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
            },
        );
    }

    // Regression for issue #280: Java `RecordDeclaration` is treated as
    // a class space; methods inside its explicit body contribute to
    // WMC.
    #[test]
    fn java_record_wmc_aggregates_method_complexity() {
        check_metrics::<JavaParser>(
            "record Point(int x, int y) {
                public int describe() {         // entry +1
                    return (x == 0) ? 0 : 1;    // ternary +1
                }
            }",
            "foo.java",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
            },
        );
    }

    // Regression for issue #280: Java `AnnotationTypeDeclaration` must
    // open a `SpaceKind::Interface` FuncSpace (the `is_func_space`
    // change) AND must not aggregate WMC because annotation type
    // elements parse as `AnnotationTypeElementDeclaration`, not
    // `MethodDeclaration`, so no `Function` space is opened for them
    // and their entry cyclomatic is not folded into
    // `interface_wmc_sum`. Asserting only `interface_wmc_sum == 0`
    // would pass vacuously even if `AnnotationTypeDeclaration` were
    // dropped from `is_func_space` (the FuncSpace tree would simply
    // omit the annotation type space, and `0 == 0` would still hold);
    // the structural check on `space.kind` is what catches that
    // regression.
    #[test]
    fn java_annotation_type_opens_interface_space_with_zero_wmc() {
        check_func_space::<JavaParser, _>(
            "@interface Marker {
                String value() default \"\";
                int priority() default 0;
            }",
            "foo.java",
            |func_space| {
                assert_eq!(func_space.metrics.wmc.interface_wmc_sum(), 0);
                // Without `AnnotationTypeDeclaration` in `is_func_space`,
                // the file-level Unit would have zero child spaces here.
                assert_child_space_kind(&func_space, "Marker", SpaceKind::Interface);
            },
        );
    }

    #[test]
    fn csharp_single_class() {
        check_metrics::<CsharpParser>(
            "public class Example {
                public bool M1(bool a, bool b) {
                    bool r = false;
                    if (a && b == a || b) {
                        r = true;
                    }
                    return r;
                }
                public int M2(int n) {
                    for (int i = 0; i < n; i++) {
                        int j = n;
                        while (j > i) {
                            j--;
                        }
                    }
                    return (n % 2 == 0) ? 1 : 0;
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 8);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_multiple_classes() {
        check_metrics::<CsharpParser>(
            "public class A {
                private int a;
                public A() { a = 0; }
                public void SetA(int n) { a = n; }
                public int GetA() { return a; }
            }
            class B {
                private int b;
                public B() { b = 0; }
                public int GetB() { return b; }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 5);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_static_nested_class() {
        check_metrics::<CsharpParser>(
            "public class Outer {
                public static class Nested {
                    private void M() {
                        System.Console.WriteLine(\"Test\");
                    }
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_nested_inner_classes() {
        check_metrics::<CsharpParser>(
            "public class Outer {
                private int a;
                public class Inner {
                    public int GetX() { return 0; }
                    public class Innermost {
                        public int GetY() { return 1; }
                    }
                }
                public int GetA() { return a; }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_local_inner_class() {
        // C# uses local functions instead of Java's local classes.
        check_metrics::<CsharpParser>(
            "public class A {
                public int M(int x) {
                    int Local(int y) {
                        if (y > 0) return y;
                        return -y;
                    }
                    return Local(x);
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_anonymous_inner_class() {
        check_metrics::<CsharpParser>(
            "public class A {
                public void Run() {
                    System.Action f = delegate(int x) {
                        if (x > 0) System.Console.WriteLine(x);
                    };
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_nested_anonymous_inner_classes() {
        check_metrics::<CsharpParser>(
            "public class A {
                public void Run() {
                    System.Action f = delegate(int x) {
                        System.Action g = delegate(int y) {
                            if (y > 0) System.Console.WriteLine(y);
                        };
                    };
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 4);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_lambda_expression() {
        check_metrics::<CsharpParser>(
            "public class A {
                public void Run() {
                    System.Func<int, int> f = x => x > 0 ? x : -x;
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_indexer_wmc() {
        // A bodied indexer folds its accessor complexities into the
        // enclosing class. Before #464 the `indexer_declaration` node
        // itself ALSO opened a method space, folding an extra entry on
        // top of get=1 + set=1 (`class_wmc_sum == 3`). The correct sum is
        // 2 — one unit of complexity per accessor — matching the npm path.
        check_metrics::<CsharpParser>(
            "class A {
                private int[] _d;
                public int this[int i] { get => _d[i]; set => _d[i] = value; }
            }",
            "foo.cs",
            |metric| {
                // expected: get (cyclomatic 1) + set (cyclomatic 1) = 2;
                // no extra entry from the IndexerDeclaration node.
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                assert_eq!(metric.npm.class_nm_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_expression_bodied_indexer_wmc() {
        // The accessor-less expression-bodied form (`this[int i] => _d[i];`)
        // has no `accessor_declaration` child, so the #464 gate keeps the
        // IndexerDeclaration node itself opening a single method space —
        // it must stay at 1, not regress to 0 (mirrors npm `.max(1)`).
        check_metrics::<CsharpParser>(
            "class A {
                private int[] _d;
                public int this[int i] => _d[i];
            }",
            "foo.cs",
            |metric| {
                // expected: one implicit getter (cyclomatic 1) = 1.
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                assert_eq!(metric.npm.class_nm_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_property_wmc() {
        // A bodied property folds its accessor complexities into the
        // enclosing class. The `property_declaration` node must NOT open an
        // extra method space on top of get=1 + set=1 (the property analogue
        // of the #464 double-count). The correct sum is 2.
        check_metrics::<CsharpParser>(
            "class A {
                private int _w;
                public int W { get => _w; set => _w = value; }
            }",
            "foo.cs",
            |metric| {
                // expected: get (cyclomatic 1) + set (cyclomatic 1) = 2;
                // no extra entry from the PropertyDeclaration node (#472).
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                assert_eq!(metric.npm.class_nm_sum(), 2);
            },
        );
    }

    #[test]
    fn csharp_expression_bodied_property_wmc() {
        // The accessor-less expression-bodied form (`int W => _w;`) has no
        // `accessor_declaration` child, so the #472 gate lets the
        // PropertyDeclaration node itself open a single method space — it
        // must be 1, not 0 as before the fix (mirrors npm `.max(1)`).
        check_metrics::<CsharpParser>(
            "class A {
                private int _w;
                public int W => _w;
            }",
            "foo.cs",
            |metric| {
                // expected: one implicit getter (cyclomatic 1) = 1.
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                assert_eq!(metric.npm.class_nm_sum(), 1);
            },
        );
    }

    #[test]
    fn csharp_single_interface() {
        check_metrics::<CsharpParser>(
            "public interface I {
                int GetA();
                int GetB();
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                assert_eq!(metric.wmc.interface_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_multiple_interfaces() {
        check_metrics::<CsharpParser>(
            "public interface I1 { int GetA(); }
            public interface I2 { bool GetB(); float GetC(); }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                assert_eq!(metric.wmc.interface_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_nested_inner_interfaces() {
        check_metrics::<CsharpParser>(
            "public interface I1 {
                int GetA();
                public interface I2 {
                    bool GetB();
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                assert_eq!(metric.wmc.interface_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_class_in_interface() {
        check_metrics::<CsharpParser>(
            "public interface I {
                int GetA();
                public class Helper {
                    public int M() { return 0; }
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn csharp_interface_in_class() {
        check_metrics::<CsharpParser>(
            "class Outer {
                int a;
                bool b;
                public int GetA() { return a; }
                public bool GetB() { return b; }
                public interface InnerI {
                    float GetC();
                    double GetD();
                }
            }",
            "foo.cs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn php_no_classes() {
        check_metrics::<PhpParser>(
            "<?php function f(): int { return 1; }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_one_class_simple() {
        check_metrics::<PhpParser>(
            "<?php
            class A {
                public function a(): int { return 1; }
                public function b(): int { return 2; }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_one_class_with_loops() {
        check_metrics::<PhpParser>(
            "<?php
            class A {
                public function f(int $n): int {
                    $sum = 0;
                    for ($i = 0; $i < $n; $i++) {
                        $sum += $i;
                    }
                    return $sum;
                }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_one_class_with_branches() {
        check_metrics::<PhpParser>(
            "<?php
            class A {
                public function f(int $x): int {
                    if ($x > 0) {
                        return 1;
                    }
                    if ($x < 0) {
                        return -1;
                    }
                    return 0;
                }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_class_with_methods_only() {
        check_metrics::<PhpParser>(
            "<?php
            class A {
                public function a(): void {}
                public function b(): void {}
                public function c(): void {}
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_multiple_classes() {
        check_metrics::<PhpParser>(
            "<?php
            class A {
                public function f(int $x): int {
                    if ($x > 0) { return 1; }
                    return 0;
                }
            }
            class B {
                public function g(int $x): int {
                    return $x;
                }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_anonymous_class() {
        check_metrics::<PhpParser>(
            "<?php
            $obj = new class {
                public function f(int $x): int {
                    if ($x > 0) { return 1; }
                    return 0;
                }
            };",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_class_with_static_methods() {
        check_metrics::<PhpParser>(
            "<?php
            class A {
                public static function f(int $x): int {
                    if ($x > 0) { return 1; }
                    return 0;
                }
                public static function g(): int { return 1; }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_interface_wmc() {
        check_metrics::<PhpParser>(
            "<?php
            interface I {
                public function a(): void;
                public function b(): int;
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_trait_wmc() {
        check_metrics::<PhpParser>(
            "<?php
            trait T {
                public function f(int $x): int {
                    if ($x > 0) { return 1; }
                    return 0;
                }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_enum_with_methods() {
        check_metrics::<PhpParser>(
            "<?php
            enum Color {
                case Red;
                case Green;
                public function label(): string {
                    return match ($this) {
                        Color::Red => 'r',
                        Color::Green => 'g',
                    };
                }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_class_inside_namespace() {
        check_metrics::<PhpParser>(
            "<?php
            namespace App;
            class A {
                public function f(int $x): int {
                    if ($x > 0) { return 1; }
                    return 0;
                }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    #[test]
    fn php_class_complex() {
        check_metrics::<PhpParser>(
            "<?php
            class Calc {
                public function add(int $a, int $b): int {
                    if ($a > 0 && $b > 0) {
                        return $a + $b;
                    }
                    return 0;
                }
                public function loop(int $n): int {
                    $s = 0;
                    for ($i = 0; $i < $n; $i++) {
                        if ($i % 2 === 0) { $s += $i; }
                    }
                    return $s;
                }
            }",
            "foo.php",
            |metric| insta::assert_json_snapshot!(metric.wmc),
        );
    }

    // --- Kotlin WMC tests -------------------------------------------------
    //
    // Reference: Kotlin `class_declaration` carries either a `class` or
    // `interface` keyword child; the getter routes the former to
    // `SpaceKind::Class` and the latter to `SpaceKind::Interface`. Member
    // function cyclomatic complexity accumulates into the enclosing
    // class/interface bucket, mirroring the Java impl.

    #[test]
    fn kotlin_empty_class() {
        // Empty class — no methods, WMC = 0.
        check_metrics::<KotlinParser>("class Empty {}", "foo.kt", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 0);
            assert_eq!(metric.wmc.interface_wmc_sum(), 0);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn kotlin_single_class() {
        // wmc = 1 (method base) + 1 (if) + 1 (explicit when arm; `else`
        // skipped per #282) = 3
        check_metrics::<KotlinParser>(
            "class C {
                fun m(x: Int): Int {       // +1
                    if (x > 0) {           // +1
                        return x
                    }
                    return when (x) {
                        0 -> 0             // +1 (WhenEntry)
                        else -> -x         // skipped (else is default)
                    }
                }
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_multiple_classes() {
        // A: constructor 1 + setA 1 + getA 1 = 3
        // B: constructor 1 + getB 1 = 2
        check_metrics::<KotlinParser>(
            "class A {
                private var a: Int = 0
                constructor(n: Int) { a = n }   // +1
                fun setA(n: Int) { a = n }      // +1
                fun getA(): Int = a             // +1
            }
            class B {
                private var b: Int = 0
                constructor(n: Int) { b = n }   // +1
                fun getB(): Int = b             // +1
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 5);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_nested_class() {
        // Outer: 0 methods. Nested: m(): +1
        check_metrics::<KotlinParser>(
            "class Outer {
                class Nested {
                    fun m() { println(\"hi\") }   // +1
                }
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_inner_class() {
        // `inner class` differs semantically (captures outer reference) but
        // structurally still opens a new class space.
        check_metrics::<KotlinParser>(
            "class Outer {
                fun outerM() {}                    // +1
                inner class Inner {
                    fun innerM() {}                // +1
                }
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_data_class() {
        // `data class` synthesizes copy/equals/hashCode/toString at
        // compile time, but only user-written methods are counted —
        // compiler-generated members are not user code.
        check_metrics::<KotlinParser>(
            "data class Point(val x: Int, val y: Int) {
                fun manhattan(): Int = kotlin.math.abs(x) + kotlin.math.abs(y)  // +1
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_object_singleton() {
        // `object` declarations are singletons; the getter routes them to
        // `SpaceKind::Class` so their methods count as class methods.
        check_metrics::<KotlinParser>(
            "object Util {
                fun add(a: Int, b: Int): Int = a + b   // +1
                fun gtZero(n: Int): Boolean {          // +1
                    return n > 0
                }
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_companion_object() {
        // A `companion object` opens its own Class space, exactly like a
        // named `object` declaration (#431). The companion's `mk` and the
        // enclosing class's `get` are each attributed to their own Class
        // space; the file-level `class_wmc_sum` aggregates both (1 + 1 = 2)
        // with no member lost or double-counted.
        check_metrics::<KotlinParser>(
            "class Holder {
                val instance: Int = 1
                fun get(): Int = instance               // +1
                companion object {
                    val SCALE: Int = 10
                    fun mk(): Holder = Holder()         // +1
                }
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_companion_object_opens_class_space() {
        // Structural guard for #431: a named `companion object` must open
        // its own Class space, mirroring the named-object handling, rather
        // than folding its members into the enclosing class. Reverting the
        // `CompanionObject` arm in `get_space_kind` / `is_func_space` drops
        // the `Companion` child space, failing this assertion.
        check_func_space::<KotlinParser, _>(
            "class Holder {
                fun get(): Int = 1
                companion object Companion {
                    fun mk(): Holder = Holder()
                }
            }",
            "foo.kt",
            |func_space| {
                let holder = func_space
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("Holder"))
                    .expect("expected a child FuncSpace named \"Holder\"");
                // The companion is a Class space nested inside Holder, not a
                // sibling at the file level and not absorbed into Holder.
                assert_child_space_kind(holder, "Companion", crate::SpaceKind::Class);
                let companion = holder
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("Companion"))
                    .expect("expected a child FuncSpace named \"Companion\"");
                // `mk` is attributed to the companion space (wmc = 1), and
                // Holder's roll-up totals get + mk = 2 with no double-count
                // (the file-level aggregate stays 2, not 3).
                assert_eq!(companion.metrics.wmc.class_wmc_sum(), 1);
                assert_eq!(holder.metrics.wmc.class_wmc_sum(), 2);
            },
        );
    }

    #[test]
    fn kotlin_object_literal_opens_class_space() {
        // Structural guard for #463: an anonymous `object : T { ... }`
        // (`object_literal`) must open its own Class space, exactly like a
        // named `object` or `companion object`, rather than folding its
        // members into the enclosing function. Reverting the
        // `ObjectLiteral` arm in `get_space_kind` / `is_func_space`
        // attributes `run` and `helper` to `Holder.get`, failing the
        // structural assertions below (verified by revert).
        check_func_space::<KotlinParser, _>(
            "class Holder {
                fun get(): Int {
                    val r = object : Runnable {
                        override fun run() {}
                        fun helper(): Int = 42
                    }
                    return 1
                }
            }",
            "foo.kt",
            |func_space| {
                let holder = func_space
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("Holder"))
                    .expect("expected a child FuncSpace named \"Holder\"");
                let get = holder
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("get"))
                    .expect("expected a child FuncSpace named \"get\"");
                // The anonymous object opens a Class space nested inside
                // `get`, named `<anonymous>` (default `get_func_space_name`).
                assert_child_space_kind(get, "<anonymous>", crate::SpaceKind::Class);
                let anon = get
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("<anonymous>"))
                    .expect("expected a child FuncSpace named \"<anonymous>\"");
                // `run` and `helper` are attributed to the anonymous space
                // (its two child Function spaces), NOT to `get`. `get`'s
                // only direct child is the anonymous space — there are no
                // stray `run` / `helper` siblings folded into `get`.
                assert_eq!(
                    get.spaces.len(),
                    1,
                    "get's only child is the anonymous class"
                );
                let anon_methods = anon
                    .spaces
                    .iter()
                    .filter(|s| s.kind == crate::SpaceKind::Function)
                    .count();
                assert_eq!(
                    anon_methods, 2,
                    "run + helper attributed to the anonymous class"
                );
                // Issue's core requirement: members are *removed* from the
                // enclosing method, not merely added to the anonymous space.
                // `get`'s own function count is just itself; reverting the
                // space-opening arm folds run/helper back in, lifting it to 3.
                assert_eq!(
                    get.metrics.nom.functions(),
                    1,
                    "get owns only itself; run/helper are not folded in"
                );
                // The anonymous class rolls up both methods' WMC (run +
                // helper = 2). These two methods are accounted for in the
                // anonymous space, not double-counted into `get`.
                assert_eq!(anon.metrics.wmc.class_wmc_sum(), 2);
            },
        );
    }

    #[test]
    fn java_anonymous_class_opens_space() {
        // #463: a Java anonymous class (`new Runnable() { ... }`) opens its
        // own Class space so its members are attributed to it, not the
        // enclosing method. A plain `new Object()` (no `class_body` child)
        // and a lambda (`() -> {}`, a distinct `lambda_expression`) must
        // NOT open a Class space — the gate is on the `class_body` child.
        // Reverting the `ObjectCreationExpression` gate drops the anonymous
        // Class space and re-attributes `run` to `m`, failing this test.
        check_func_space::<JavaParser, _>(
            "class C {
                void m() {
                    Runnable r = new Runnable() {
                        public void run() {}
                    };
                    Object o = new Object();
                    Runnable l = () -> {};
                }
            }",
            "C.java",
            |func_space| {
                let c = func_space
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("C"))
                    .expect("expected a child FuncSpace named \"C\"");
                let m = c
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("m"))
                    .expect("expected a child FuncSpace named \"m\"");
                // Exactly one Class child under `m`: the anonymous class.
                // Plain `new Object()` and the lambda must not over-open;
                // the lambda is a Function space (Java tags
                // `LambdaExpression` as Function), so count Class children.
                let anon_classes: Vec<_> = m
                    .spaces
                    .iter()
                    .filter(|s| s.kind == crate::SpaceKind::Class)
                    .collect();
                assert_eq!(anon_classes.len(), 1, "exactly one anonymous Class space");
                assert_eq!(anon_classes[0].name.as_deref(), Some("<anonymous>"));
                // `run` is attributed to the anonymous class (its single
                // child Function space), not to `m`.
                let anon_methods = anon_classes[0]
                    .spaces
                    .iter()
                    .filter(|s| s.kind == crate::SpaceKind::Function)
                    .count();
                assert_eq!(anon_methods, 1, "run attributed to the anonymous class");
                assert_eq!(anon_classes[0].metrics.wmc.class_wmc_sum(), 1);
            },
        );
    }

    #[test]
    fn java_lambda_opens_no_class_space() {
        // Guard against mis-detection (#463): a Java lambda is a
        // `lambda_expression`, NOT an `object_creation_expression`, so it
        // must never trip the anonymous-class gate. It opens a Function
        // space (existing behaviour), never a Class space.
        check_func_space::<JavaParser, _>(
            "class C {
                void m() {
                    Runnable l = () -> { int x = 1; };
                }
            }",
            "C.java",
            |func_space| {
                let c = func_space
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("C"))
                    .expect("expected a child FuncSpace named \"C\"");
                let m = c
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("m"))
                    .expect("expected a child FuncSpace named \"m\"");
                let class_children = m
                    .spaces
                    .iter()
                    .filter(|s| s.kind == crate::SpaceKind::Class)
                    .count();
                assert_eq!(class_children, 0, "a lambda must not open a Class space");
            },
        );
    }

    #[test]
    fn groovy_anonymous_class_models_body_as_closure() {
        // #463 upstream-grammar note: the pinned dekobon Groovy grammar
        // does NOT attach an anonymous-class body to its
        // `object_creation_expression`. It parses `new Runnable()` as a
        // bare constructor call and the trailing `{ ... }` as a separate
        // `closure`, which already opens a Function space. So Groovy gets
        // no Class space for an anonymous class (unlike Java), but its
        // members are still NOT mis-attributed to the enclosing method —
        // they land in the closure's Function space. This pins that
        // behaviour so a future grammar bump that starts modelling
        // `class_body` here is caught and the Groovy `get_space_kind` arm
        // can be revisited.
        check_func_space::<GroovyParser, _>(
            "class C {
                void m() {
                    def r = new Runnable() {
                        void run() {}
                    }
                }
            }",
            "C.groovy",
            |func_space| {
                let c = func_space
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("C"))
                    .expect("expected a child FuncSpace named \"C\"");
                let m = c
                    .spaces
                    .iter()
                    .find(|s| s.name.as_deref() == Some("m"))
                    .expect("expected a child FuncSpace named \"m\"");
                // No Class space (grammar limitation), but `run` lands in a
                // nested Function space (the closure), not in `m` itself.
                let class_children = m
                    .spaces
                    .iter()
                    .filter(|s| s.kind == crate::SpaceKind::Class)
                    .count();
                assert_eq!(
                    class_children, 0,
                    "Groovy grammar models the body as a closure, not a class"
                );
                assert_eq!(m.metrics.nom.functions(), 1, "`m` itself, not `run`");
                let nested_funcs: u64 = m.spaces.iter().map(|s| s.metrics.nom.functions()).sum();
                assert_eq!(
                    nested_funcs, 1,
                    "`run` is attributed to the nested closure space"
                );
            },
        );
    }

    #[test]
    fn kotlin_interface_simple() {
        // Interface methods all contribute to the interface bucket.
        check_metrics::<KotlinParser>(
            "interface I {
                fun work(): Int                         // +1
                fun describe(): String                  // +1
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                assert_eq!(metric.wmc.interface_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_interface_with_default_method() {
        // Default method with control flow counts its full cyclomatic.
        check_metrics::<KotlinParser>(
            "interface I {
                fun abs(n: Int): Int {                   // +1
                    return if (n < 0) -n else n          // +1 if
                }
                fun pure(): Int                          // +1
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                assert_eq!(metric.wmc.interface_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_override_function() {
        // `override fun` is structurally just a `function_declaration` with
        // an `override` modifier — counts like any other method.
        check_metrics::<KotlinParser>(
            "open class Base {
                open fun greet(): String = \"hi\"        // +1
            }
            class Sub : Base() {
                override fun greet(): String = \"yo\"    // +1
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_secondary_constructor() {
        // Secondary constructors are explicit `secondary_constructor`
        // nodes; they count as methods.
        check_metrics::<KotlinParser>(
            "class C {
                private var a: Int = 0
                constructor(n: Int) {                    // +1
                    a = n
                }
                constructor(n: Int, m: Int) {            // +1
                    a = n + m
                }
                fun get(): Int = a                       // +1
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_init_block() {
        // `init` blocks are anonymous initializers, not function spaces;
        // they do not add to WMC directly. The class still has whatever
        // methods it declares.
        check_metrics::<KotlinParser>(
            "class C(val n: Int) {
                init {                                   // not counted
                    require(n >= 0) { \"n must be non-negative\" }
                }
                fun get(): Int = n                       // +1
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_top_level_function_excluded() {
        // Top-level `fun` and `val` belong to the `Unit` space, not a class
        // space — they must not contribute to any class metric.
        check_metrics::<KotlinParser>(
            "fun freeFunction(): Int = 42
            val freeVal: Int = 0
            class C { fun m(): Int = 1 }                 // +1
            ",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_extension_function_excluded() {
        // Extension functions look syntactically like methods but the
        // grammar parses them as top-level `function_declaration` with a
        // receiver-type prefix; they belong to the `Unit` space, not a
        // class. Class still gets +1 for its declared method.
        check_metrics::<KotlinParser>(
            "fun List<Int>.sum2(): Int = this.size       // top-level
            class C { fun m(): Int = 1 }                 // +1
            ",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_generic_class() {
        // Generic class with two methods.
        check_metrics::<KotlinParser>(
            "class Box<T>(val value: T) {
                fun get(): T = value                     // +1
                fun mapTo(f: (T) -> T): T = f(value)     // +1
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_class_in_interface() {
        // Nested class inside an interface: the inner class is a class
        // space (its method counts toward classes_wmc), and the interface
        // is the outer.
        check_metrics::<KotlinParser>(
            "interface Outer {
                fun work(): Int                          // +1 (interface)
                class Helper {
                    fun help(): Int = 0                  // +1 (class)
                }
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn kotlin_interface_in_class() {
        // Inverse of the prior test.
        check_metrics::<KotlinParser>(
            "class Outer {
                fun work(): Int = 1                      // +1 (class)
                interface Sub {
                    fun help(): Int                      // +1 (interface)
                }
            }",
            "foo.kt",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                assert_eq!(metric.wmc.interface_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    // --- TypeScript / TSX WMC tests --------------------------------------
    //
    // Each class method contributes its cyclomatic complexity to the
    // enclosing class's WMC. Arrow function class members behave as
    // methods. Interface method signatures have no bodies and add zero
    // (matching Java's abstract-method rule).

    #[test]
    fn typescript_class_wmc_single_method() {
        check_metrics::<TypescriptParser>(
            "class C {
                m(): number { return 1; }       // cyclomatic 1
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_class_wmc_two_methods() {
        check_metrics::<TypescriptParser>(
            "class C {
                a(): number { return 1; }       // +1
                b(x: number): number {          // +2 (if branch)
                    if (x > 0) return x;
                    return 0;
                }
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_class_wmc_with_branches() {
        check_metrics::<TypescriptParser>(
            "class C {
                m(x: number): number {
                    if (x > 0) {                // +1
                        return 1;
                    } else if (x < 0) {         // +1
                        return -1;
                    }
                    return 0;
                }                                // base 1
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_class_wmc_arrow_field() {
        // Arrow-function class fields contribute their cyclomatic to
        // the enclosing class — they are function spaces.
        check_metrics::<TypescriptParser>(
            "class C {
                arrow = (x: number) => {
                    if (x > 0) return x;        // +1
                    return 0;
                };                              // base 1
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_class_wmc_with_loops() {
        check_metrics::<TypescriptParser>(
            "class C {
                m(xs: number[]): number {
                    let total = 0;
                    for (const x of xs) {       // +1
                        total += x;
                    }
                    return total;
                }                                // base 1
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_abstract_class_wmc() {
        // Abstract method signatures have no body — contribute 0.
        check_metrics::<TypescriptParser>(
            "abstract class C {
                abstract a(): void;             // signature only, 0
                m(): number { return 1; }       // +1
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_interface_wmc_zero() {
        // Interface method signatures have no bodies → 0 WMC.
        check_metrics::<TypescriptParser>(
            "interface I {
                a(): void;
                b(): number;
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_constructor_wmc() {
        // Constructor counts as a method; its cyclomatic adds to the
        // class WMC.
        check_metrics::<TypescriptParser>(
            "class C {
                x: number;
                constructor(n: number) {
                    if (n > 0) {                // +1
                        this.x = n;
                    } else {
                        this.x = 0;
                    }
                }                                // base 1
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_getter_setter_wmc() {
        // Getter and setter each contribute 1 (base).
        check_metrics::<TypescriptParser>(
            "class C {
                _x: number = 0;
                get x(): number { return this._x; }
                set x(v: number) { this._x = v; }
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_multiple_classes_wmc_independent() {
        check_metrics::<TypescriptParser>(
            "class A { m(): number { return 1; } }
             class B {
                m(x: number): number {
                    if (x > 0) return x;        // +1
                    return 0;
                }                                // base 1
             }",
            "foo.ts",
            |metric| {
                // A: 1 + B: 2 = 3 total.
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_class_wmc_with_ternary_and_logical() {
        check_metrics::<TypescriptParser>(
            "class C {
                m(x: number, y: number): number {
                    return x > 0 && y > 0      // +1 (ternary) +1 (&&)
                        ? x + y
                        : 0;
                }                                // base 1
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn typescript_generic_class_wmc() {
        check_metrics::<TypescriptParser>(
            "class Box<T> {
                value: T;
                set(v: T): void { this.value = v; }
                get(): T { return this.value; }
            }",
            "foo.ts",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    // TSX parity

    #[test]
    fn tsx_class_wmc_single_method() {
        check_metrics::<TsxParser>(
            "class C { m(): number { return 1; } }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_class_wmc_two_methods() {
        check_metrics::<TsxParser>(
            "class C {
                a(): number { return 1; }
                b(x: number): number {
                    if (x > 0) return x;
                    return 0;
                }
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_class_wmc_with_branches() {
        check_metrics::<TsxParser>(
            "class C {
                m(x: number): number {
                    if (x > 0) return 1;
                    else if (x < 0) return -1;
                    return 0;
                }
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_class_wmc_arrow_field() {
        check_metrics::<TsxParser>(
            "class C {
                arrow = (x: number) => {
                    if (x > 0) return x;
                    return 0;
                };
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_class_wmc_with_loops() {
        check_metrics::<TsxParser>(
            "class C {
                m(xs: number[]): number {
                    let total = 0;
                    for (const x of xs) { total += x; }
                    return total;
                }
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_abstract_class_wmc() {
        check_metrics::<TsxParser>(
            "abstract class C {
                abstract a(): void;
                m(): number { return 1; }
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_interface_wmc_zero() {
        check_metrics::<TsxParser>(
            "interface I { a(): void; b(): number; }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_constructor_wmc() {
        check_metrics::<TsxParser>(
            "class C {
                x: number;
                constructor(n: number) {
                    if (n > 0) this.x = n;
                    else this.x = 0;
                }
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_getter_setter_wmc() {
        check_metrics::<TsxParser>(
            "class C {
                _x: number = 0;
                get x(): number { return this._x; }
                set x(v: number) { this._x = v; }
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_multiple_classes_wmc_independent() {
        check_metrics::<TsxParser>(
            "class A { m(): number { return 1; } }
             class B {
                m(x: number): number {
                    if (x > 0) return x;
                    return 0;
                }
             }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_class_wmc_with_ternary_and_logical() {
        check_metrics::<TsxParser>(
            "class C {
                m(x: number, y: number): number {
                    return x > 0 && y > 0 ? x + y : 0;
                }
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn tsx_generic_class_wmc() {
        check_metrics::<TsxParser>(
            "class Box<T> {
                value: T;
                set(v: T): void { this.value = v; }
                get(): T { return this.value; }
            }",
            "foo.tsx",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    // --- Ruby WMC tests ---------------------------------------------------
    //
    // Reference: Ruby `Class` and `SingletonClass` map to `SpaceKind::Class`
    // via `Getter::get_space_kind`; `Module` is a `SpaceKind::Namespace`
    // and does not contribute to WMC. Method cyclomatic complexities
    // accumulate into the enclosing class via `class_interface_compute`.

    #[test]
    fn ruby_no_classes() {
        // File with only a top-level method — no class space, WMC = 0.
        check_metrics::<RubyParser>("def foo\n  1\nend\n", "foo.rb", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 0);
            assert_eq!(metric.wmc.interface_wmc_sum(), 0);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn ruby_empty_class() {
        // Class with no methods → wmc = 0.
        check_metrics::<RubyParser>("class Foo\nend\n", "foo.rb", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 0);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn ruby_one_class_simple() {
        // Two methods, each with cyclomatic = 1 (the method base) → wmc = 2.
        check_metrics::<RubyParser>(
            "class A\n  def a\n    1\n  end\n  def b\n    2\n  end\nend\n",
            "foo.rb",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_one_class_with_branch() {
        // One method with cyclomatic 1 (base) + 1 (if) = 2.
        check_metrics::<RubyParser>(
            "class A\n  def f(x)\n    if x > 0\n      1\n    else\n      0\n    end\n  end\nend\n",
            "foo.rb",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_one_class_with_loop() {
        // One method with cyclomatic 1 (base) + 1 (while) = 2.
        check_metrics::<RubyParser>(
            "class A\n  def f(n)\n    while n > 0\n      n -= 1\n    end\n  end\nend\n",
            "foo.rb",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_singleton_method_included() {
        // Mix of regular and singleton (`def self.x`) methods, both
        // contribute to the class WMC.
        check_metrics::<RubyParser>(
            "class A\n  def f\n    1\n  end\n  def self.g\n    2\n  end\nend\n",
            "foo.rb",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_singleton_class_methods_included() {
        // Methods inside `class << self` belong to the enclosing class
        // (singleton class is a `SpaceKind::Class` of its own).
        check_metrics::<RubyParser>(
            "class A\n  class << self\n    def s\n      1\n    end\n  end\nend\n",
            "foo.rb",
            |metric| {
                // Two class spaces: outer A (wmc 0, no methods) and the
                // singleton class with its single method (wmc 1).
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_multiple_classes() {
        // Each class contributes its method-cyclomatic sum to the rollup.
        check_metrics::<RubyParser>(
            "class A\n  def f(x)\n    if x > 0\n      1\n    end\n  end\nend\nclass B\n  def g\n    1\n  end\nend\n",
            "foo.rb",
            |metric| {
                // A: 2 (base + if). B: 1 (base only). Sum = 3.
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_module_only() {
        // Module is a `Namespace` space — does NOT contribute to WMC even
        // though the body has methods.
        check_metrics::<RubyParser>(
            "module M\n  def f\n    1\n  end\nend\n",
            "foo.rb",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_class_with_inheritance() {
        // `class A < B` inherits — irrelevant to WMC, which depends only on
        // the method bodies inside this class.
        check_metrics::<RubyParser>(
            "class A < B\n  def f\n    1\n  end\n  def g\n    2\n  end\nend\n",
            "foo.rb",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_class_with_visibility_keywords() {
        // Visibility keywords do NOT affect WMC — every method body
        // contributes regardless of `private` / `protected`.
        check_metrics::<RubyParser>(
            "class A\n  def a\n    1\n  end\n  private\n  def b\n    1\n  end\n  protected\n  def c\n    1\n  end\nend\n",
            "foo.rb",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn ruby_class_complex() {
        // Class with two methods whose cyclomatic sums combine.
        // `add`: base(1) + `if`(1) + `&&`(1) = 3.
        // `loop`: base(1) + `while`(1) + `if`(1) = 3.
        // Class WMC = 6.
        check_metrics::<RubyParser>(
            "class Calc\n  def add(a, b)\n    if a > 0 && b > 0\n      a + b\n    end\n  end\n  def loop(n)\n    s = 0\n    while n > 0\n      if n.even?\n        s += n\n      end\n      n -= 1\n    end\n    s\n  end\nend\n",
            "foo.rb",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 6);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    // ---------------------------------------------------------------
    // Default-impl placeholder smoke tests (audited in #188).
    //
    // Each test feeds a class / struct with multiple branchy methods
    // to a language whose `Wmc` is currently the default no-op. The
    // assertion pins the current 0 value; when the real impl lands
    // the assertion will fire and force a test update.
    // ---------------------------------------------------------------

    // --- Python WMC ---------------------------------------------------

    #[test]
    fn python_empty_class_zero_wmc() {
        check_metrics::<PythonParser>("class C:\n    pass\n", "foo.py", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 0);
            assert_eq!(metric.wmc.interface_wmc_sum(), 0);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn python_single_method_wmc_one() {
        // Single straight-line method → cyclomatic 1 → WMC 1.
        check_metrics::<PythonParser>(
            "class C:\n    def m(self):\n        return 1\n",
            "foo.py",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn python_method_with_if_adds_to_wmc() {
        // Cyclomatic: 1 (base) + 1 (if) = 2. WMC = 2.
        check_metrics::<PythonParser>(
            "class C:\n    def m(self, x):\n        if x > 0:\n            return 1\n        return 0\n",
            "foo.py",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn python_multiple_methods_wmc_sums() {
        // method1 cyclomatic 1, method2 cyclomatic 2 (if), method3
        // cyclomatic 3 (if + for). WMC sum = 1 + 2 + 3 = 6.
        check_metrics::<PythonParser>(
            "class C:\n\
             \x20   def m1(self):\n\
             \x20       return 1\n\
             \x20   def m2(self, x):\n\
             \x20       if x:\n\
             \x20           return 1\n\
             \x20       return 0\n\
             \x20   def m3(self, xs):\n\
             \x20       for x in xs:\n\
             \x20           if x:\n\
             \x20               return x\n\
             \x20       return None\n",
            "foo.py",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 6);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn python_top_level_function_does_not_contribute_to_class_wmc() {
        // Top-level function lives in the module/unit space, not in a
        // class space — class_wmc stays at 0.
        check_metrics::<PythonParser>(
            "def f(x):\n    if x:\n        return 1\n    return 0\n",
            "foo.py",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn python_multiple_classes_wmc_independent() {
        // Each class accumulates its own methods' cyclomatic. The
        // file-level class_wmc_sum is the sum of every class's WMC.
        // A.m1 (1) + B.m2 (2 — has an if) = 3.
        check_metrics::<PythonParser>(
            "class A:\n\
             \x20   def m1(self):\n\
             \x20       return 1\n\
             class B:\n\
             \x20   def m2(self, x):\n\
             \x20       if x:\n\
             \x20           return 1\n\
             \x20       return 0\n",
            "foo.py",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn rust_empty_unit_zero_wmc() {
        check_metrics::<RustParser>("", "empty.rs", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 0);
            assert_eq!(metric.wmc.interface_wmc_sum(), 0);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn rust_single_impl_method_wmc_one() {
        // Single straight-line method → cyclomatic 1 → WMC 1.
        check_metrics::<RustParser>(
            "struct Foo;\nimpl Foo { fn m(&self) -> i32 { 1 } }\n",
            "foo.rs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn rust_method_with_if_adds_to_wmc() {
        // Cyclomatic: 1 (base) + 1 (if) = 2. WMC = 2.
        check_metrics::<RustParser>(
            "struct Foo;\n\
             impl Foo {\n\
             \x20   fn m(&self, x: i32) -> i32 {\n\
             \x20       if x > 0 { 1 } else { 0 }\n\
             \x20   }\n\
             }\n",
            "foo.rs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn rust_multiple_methods_wmc_sums() {
        // m1 cyclomatic 1, m2 cyclomatic 2 (if), m3 cyclomatic 3 (if
        // inside for). WMC = 1 + 2 + 3 = 6.
        check_metrics::<RustParser>(
            "struct Foo;\n\
             impl Foo {\n\
             \x20   fn m1(&self) -> i32 { 1 }\n\
             \x20   fn m2(&self, x: i32) -> i32 { if x > 0 { 1 } else { 0 } }\n\
             \x20   fn m3(&self, xs: &[i32]) -> i32 {\n\
             \x20       for x in xs { if *x > 0 { return *x; } }\n\
             \x20       0\n\
             \x20   }\n\
             }\n",
            "foo.rs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 6);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn rust_multiple_impls_wmc_aggregate() {
        // Two `impl` blocks for Foo, each contributing 1 method with
        // cyclomatic 1. Unit-level class_wmc_sum = 2.
        check_metrics::<RustParser>(
            "struct Foo;\n\
             impl Foo { fn m1(&self) {} }\n\
             impl Foo { fn m2(&self) {} }\n",
            "foo.rs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn rust_trait_default_method_contributes_to_interface_wmc() {
        // A trait method with a default body — `area` is a function
        // space inside the trait. Cyclomatic = 1 → interface_wmc = 1.
        // The signature-only `draw` has no body and contributes
        // nothing.
        check_metrics::<RustParser>(
            "trait T { fn draw(&self); fn area(&self) -> f64 { 0.0 } }",
            "foo.rs",
            |metric| {
                assert_eq!(metric.wmc.interface_wmc_sum(), 1);
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn rust_top_level_function_does_not_contribute_to_class_wmc() {
        // Free `fn f()` opens a Function space but no class/trait
        // surrounds it. The Unit space is not a class space, so
        // class_wmc_sum stays at 0.
        check_metrics::<RustParser>(
            "fn f(x: i32) -> i32 { if x > 0 { 1 } else { 0 } }",
            "foo.rs",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    // ----- Go -----

    #[test]
    fn go_wmc_is_zero_documented_limitation() {
        // Go's flat space model does not expose per-receiver class
        // spaces, and the Wmc trait signature receives only a
        // `SpaceKind` (Function for both `MethodDeclaration` and
        // free `FunctionDeclaration`). Implementing receiver-grouped
        // WMC would require space-model changes that are out of
        // scope for this fix; per the issue's option (a), the metric
        // stays at zero with a documented reason. This test pins
        // that behaviour so any future Wmc work for Go has to update
        // it deliberately.
        check_metrics::<GoParser>(
            "package main\n\
             type Foo struct{}\n\
             func (f Foo) M(x int) int { if x > 0 { return 1 } else { return 0 } }\n\
             func (f Foo) N() {}\n",
            "foo.go",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    // ----- Elixir -----

    // Issue #275: Elixir's `def` / `defp` declarations parse as
    // `Call` nodes whose `target` Identifier text spells the
    // keyword. The source-aware Checker / Getter dispatch promotes
    // them to Function spaces inside the surrounding `defmodule`
    // Class. WMC then aggregates cyclomatic per method into the
    // class via the shared `class_interface_compute` aggregator.
    #[test]
    fn elixir_wmc_aggregates_def_methods() {
        check_metrics::<ElixirParser>(
            "defmodule Foo do\n  def m(x) do\n    if x > 0 do\n      1\n    else\n      0\n    end\n  end\n  def n, do: :ok\nend\n",
            "foo.ex",
            |metric| {
                // m: entry(1) + if(1) = 2; n: entry(1) = 1 → wmc = 3.
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(
                    metric.wmc,
                    @r#"
                {
                  "class_wmc_sum": 3,
                  "interface_wmc_sum": 0,
                  "total": 3
                }
                "#
                );
            },
        );
    }

    #[test]
    fn elixir_wmc_def_plus_defp_counts_both() {
        check_metrics::<ElixirParser>(
            "defmodule Foo do\n  def pub_one, do: 1\n  defp priv_one, do: 1\nend\n",
            "foo.ex",
            |metric| {
                // Both `def` and `defp` are methods of the class — npm
                // distinguishes public vs private, wmc does not.
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
            },
        );
    }

    #[test]
    fn elixir_wmc_defmacro_counts() {
        check_metrics::<ElixirParser>(
            "defmodule Foo do\n  defmacro stuff(x) do\n    if x > 0, do: :pos, else: :neg\n  end\nend\n",
            "foo.ex",
            |metric| {
                // defmacro is a method; body has entry(1) + if(1) = 2.
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
            },
        );
    }

    #[test]
    fn elixir_wmc_multiple_clauses_each_a_method() {
        // Each `def f(...)` head is a Call with its own Function
        // space, so multiple clauses for the same name each count.
        check_metrics::<ElixirParser>(
            "defmodule Foo do\n  def f(0), do: :zero\n  def f(_), do: :other\nend\n",
            "foo.ex",
            |metric| {
                // Two clauses, entry(1) each → wmc = 2.
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
            },
        );
    }

    #[test]
    fn elixir_wmc_nested_defmodule_isolates() {
        check_metrics::<ElixirParser>(
            "defmodule Outer do\n  def o, do: 1\n  defmodule Inner do\n    def i, do: 1\n  end\nend\n",
            "foo.ex",
            |metric| {
                // Outer.o(1) + Inner.i(1) → file-level sum is 2.
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
            },
        );
    }

    #[test]
    fn elixir_wmc_user_macro_not_classified_as_method() {
        // A user-defined `defmacro custom_def`, then invoking
        // `custom_def foo, do: ...` must NOT be classified as a
        // method — the literal-text comparison in
        // `elixir_call_keyword` only matches the four built-in
        // method-defining macros. The `def unquote(name)` inside the
        // `quote do … end` block is also rejected (it is a code
        // template emitted on macro expansion, not a real definition
        // of any method of `Foo`); `elixir_is_inside_quote_block`
        // filters it out, keeping `Wmc` aligned with `Npm` (#310).
        check_metrics::<ElixirParser>(
            "defmodule Foo do\n  defmacro custom_def(name, body) do\n    quote do\n      def unquote(name), do: unquote(body)\n    end\n  end\n  custom_def foo, do: 1\nend\n",
            "foo.ex",
            |metric| {
                // Only the `defmacro custom_def` itself is a method
                // of `Foo`. Body cyclomatic: entry(1) → wmc = 1.
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
            },
        );
    }

    #[test]
    fn elixir_wmc_quoted_defs_do_not_inflate_method_count() {
        // Regression test for #310: previously, every `def` lexically
        // present in the source was promoted to a Function space and
        // counted toward `Wmc`, even when nested inside `quote do …
        // end` (a metaprogramming template that does not declare
        // methods of the enclosing module). That made `Wmc` disagree
        // with `Npm`'s direct-children classification.
        //
        // Here `Foo` has exactly one real method (the `defmacro
        // multi`); the three quoted `def`s inside its body are not
        // methods of `Foo`. `Wmc` should now agree.
        check_metrics::<ElixirParser>(
            "defmodule Foo do\n  defmacro multi do\n    quote do\n      def a, do: 1\n      def b, do: 2\n      defp c, do: 3\n    end\n  end\nend\n",
            "foo.ex",
            |metric| {
                // Only `defmacro multi` is a method of Foo: entry(1)
                // → wmc = 1.
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
            },
        );
    }

    // ----- Objective-C -----

    #[test]
    fn objc_wmc() {
        // `@implementation` is a Class space; each `method_definition`
        // opens a Function space whose cyclomatic rolls up. `a` has one
        // `if` (cyclomatic 2), `b` is empty (cyclomatic 1) → class WMC = 3.
        // The `@interface` is an Interface space whose method *declaration*
        // has no body, so it contributes 0 — `interface_wmc_sum` stays 0
        // even though a declared method exists.
        check_metrics::<ObjcParser>(
            "@interface Foo : NSObject\n\
             - (int)a:(int)x;\n\
             @end\n\
             @implementation Foo\n\
             - (int)a:(int)x { if (x > 0) { return 1; } return 0; }\n\
             - (void)b { }\n\
             @end\n",
            "foo.m",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
            },
        );
    }

    // ----- C++ -----

    #[test]
    fn cpp_empty_unit_zero_wmc() {
        // No code → no class spaces → wmc = 0. Wires up the trait.
        check_metrics::<CppParser>("", "empty.cpp", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 0);
            assert_eq!(metric.wmc.interface_wmc_sum(), 0);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn cpp_single_method_wmc_one() {
        // One method with no control flow → cyclomatic = 1 → wmc = 1.
        check_metrics::<CppParser>("class Foo { public: void m() {} };", "foo.cpp", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 1);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn cpp_method_with_if_adds_to_wmc() {
        // One method with one `if` → cyclomatic = 2 → wmc = 2.
        check_metrics::<CppParser>(
            "class Foo {\n\
                 public:\n\
                     int m(int x) {\n\
                         if (x > 0) { return 1; }\n\
                         return 0;\n\
                     }\n\
             };",
            "foo.cpp",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn cpp_struct_wmc_maps_to_class() {
        // `struct` opens a `SpaceKind::Struct` space — the C++ Wmc
        // impl maps it to `Class` so the same `class_wmc_sum`
        // accumulator receives the cyclomatic of struct methods.
        check_metrics::<CppParser>(
            "struct Foo {\n\
                 int m(int x) {\n\
                     if (x > 0) { return 1; }\n\
                     return 0;\n\
                 }\n\
             };",
            "foo.cpp",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn cpp_free_function_does_not_contribute_to_class_wmc() {
        // A top-level function is not inside any class — its
        // cyclomatic complexity must NOT contribute to class_wmc_sum.
        // The `Unit` space is mapped through `class_interface_compute`
        // unchanged; only `Function` spaces inside a `Class` /
        // `Struct` propagate up.
        check_metrics::<CppParser>(
            "int free_fn(int x) { if (x > 0) { return 1; } return 0; }",
            "foo.cpp",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 0);
                assert_eq!(metric.wmc.interface_wmc_sum(), 0);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn cpp_multiple_methods_wmc_sums() {
        // Two methods, one with `if` (cyclomatic 2), one without
        // (cyclomatic 1). class_wmc_sum = 3.
        check_metrics::<CppParser>(
            "class Foo {\n\
                 public:\n\
                     int a(int x) { if (x > 0) { return 1; } return 0; }\n\
                     int b() { return 42; }\n\
             };",
            "foo.cpp",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 3);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn cpp_multiple_classes_wmc_aggregate() {
        // File-level rollup: Foo has wmc 1, Bar has wmc 1. Unit
        // class_wmc_sum = 2.
        check_metrics::<CppParser>(
            "class Foo { public: void a() {} };\nstruct Bar { void b() {} };",
            "foo.cpp",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn javascript_empty_unit_zero_wmc() {
        check_metrics::<JavascriptParser>("", "empty.js", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 0);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn javascript_single_method_wmc_one() {
        // Class with a single straight-line method has wmc = 1 (the
        // method's cyclomatic) rolling into the class space.
        check_metrics::<JavascriptParser>("class Foo { a() { return 1; } }", "foo.js", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 1);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    #[test]
    fn javascript_method_with_if_adds_to_wmc() {
        // Method body with an `if` has cyclomatic = 2 → class_wmc = 2.
        check_metrics::<JavascriptParser>(
            "class Foo { a(x) { if (x > 0) return 1; return 0; } }",
            "foo.js",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn javascript_free_function_does_not_contribute_to_class_wmc() {
        // Top-level functions are not class methods; their
        // cyclomatic does not roll into a class.
        check_metrics::<JavascriptParser>(
            "function f(x) { if (x > 0) return 1; return 0; }\nclass Foo { a() { return 1; } }",
            "foo.js",
            |metric| {
                // Only the class method contributes.
                assert_eq!(metric.wmc.class_wmc_sum(), 1);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn javascript_multiple_classes_wmc_aggregate() {
        // File-level rollup: Foo has wmc 1, Bar has wmc 1. Unit
        // class_wmc_sum = 2.
        check_metrics::<JavascriptParser>(
            "class Foo { a() { return 1; } }\nclass Bar { b() { return 1; } }",
            "foo.js",
            |metric| {
                assert_eq!(metric.wmc.class_wmc_sum(), 2);
                insta::assert_json_snapshot!(metric.wmc);
            },
        );
    }

    #[test]
    fn mozjs_single_method_wmc_one() {
        check_metrics::<MozjsParser>("class Foo { a() { return 1; } }", "foo.js", |metric| {
            assert_eq!(metric.wmc.class_wmc_sum(), 1);
            insta::assert_json_snapshot!(metric.wmc);
        });
    }

    // #530: a method that *contains* a nested class must yield a
    // non-negative integer `class_wmc_sum`. The nested class is its own
    // WMC scope (#463), so `Outer.m` (base 1) plus the nested `Inner.n`
    // (base 1) sum to exactly 2 with no double-attribution and no
    // negative intermediate. Mirrors `java_local_inner_class`, kept
    // minimal to pin the `u64` accessor's non-negativity.
    #[test]
    fn java_method_with_nested_class_wmc_is_non_negative_integer() {
        check_metrics::<JavaParser>(
            "public class Outer {
                public void m() {
                    class Inner {
                        public void n() {}
                    }
                }
            }",
            "Outer.java",
            |metric| {
                let total = metric.wmc.class_wmc_sum();
                assert_eq!(total, 2, "Outer.m (1) + Inner.n (1) with no double-count");
            },
        );
    }
}