ahk 0.6.2

The ahk language: a lexer, an AST, a parser and an interpreter for AutoHotkey-style scripts
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
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
//! The language and the runtime, against a fake desktop.
//!
//! No SSH, no box, no ydotool: the desktop is a fake that records the
//! calls, so these tests are about what the language means, not about what
//! the desktop does. The desktop part is verified against the real box
//! separately.
//!
//! These are a port of the archived Python tests, and the two
//! implementations have to agree.

use ahk::desktop::WindowAnswer;
use ahk::desktop::{
    Clipboard, Com, Desktop, Dialogs, Files, Hotkeys, Keyboard, Machine, Native, Network, Pictures,
    Pointer, Processes, Screen, Sound, Timers, TitleMatch, Windows,
};
use ahk::runtime::{Report, Runtime, Value};
use ahk::{Node, parse};
use std::time::Duration;

/// A desktop that records what it was asked to do.
#[derive(Debug, Default)]
struct Fake {
    calls: Vec<(String, String)>,
    pixels: Vec<((i64, i64), String)>,
    /// Where the pointer is.
    pointer: (i64, i64),
    /// The windows the fake knows about, and the one that is active.
    windows: Vec<String>,
    active: Option<String>,
    /// When it is set, the window questions cannot be answered.
    window_answer_unknown: bool,
}

impl Fake {
    fn types(&self) -> Vec<String> {
        self.calls
            .iter()
            .filter(|(kind, _)| kind == "type")
            .map(|(_, value)| value.clone())
            .collect()
    }
}

impl Desktop for Fake {}
impl Pictures for Fake {}
impl Network for Fake {}
impl Sound for Fake {}
impl Machine for Fake {}
impl Dialogs for Fake {}

impl Files for Fake {
    fn list_files(
        &mut self,
        folder: &str,
        pattern: &str,
        mode: &str,
    ) -> Result<Vec<String>, String> {
        self.calls
            .push(("list_files".into(), format!("{folder}|{pattern}|{mode}")));
        // The pattern of v2 is the tail of the name: "*.txt" is every
        // name that ends with ".txt".
        let wanted = pattern.strip_prefix('*').unwrap_or(pattern);
        Ok(["/work/one.txt", "/work/two.txt", "/work/notes.log"]
            .into_iter()
            .filter(|file| wanted.is_empty() || file.ends_with(wanted))
            .map(|file| file.to_string())
            .collect())
    }
    fn working_directory(&mut self) -> Result<String, String> {
        self.calls.push(("working_directory".into(), String::new()));
        Ok("/work".to_string())
    }
}

impl Clipboard for Fake {}

impl Timers for Fake {}

impl Hotkeys for Fake {}

impl Keyboard for Fake {
    fn type_text(&mut self, text: &str) -> Result<String, String> {
        self.calls.push(("type".into(), text.into()));
        Ok(format!("typed {text:?}"))
    }
    fn press_key(&mut self, name: &str, modifiers: &[String]) -> Result<String, String> {
        let mut spelled = modifiers.join("+");
        if !spelled.is_empty() {
            spelled.push('+');
        }
        spelled.push_str(name);
        self.calls.push(("key".into(), spelled.clone()));
        Ok(format!("pressed {spelled}"))
    }
    fn key_down(&mut self, name: &str) -> Result<String, String> {
        self.calls.push(("key_down".into(), name.into()));
        Ok(format!("{name} is down"))
    }
    fn key_up(&mut self, name: &str) -> Result<String, String> {
        self.calls.push(("key_up".into(), name.into()));
        Ok(format!("{name} is up"))
    }
}

impl Pointer for Fake {
    fn mouse_down(
        &mut self,
        _x: Option<i64>,
        _y: Option<i64>,
        button: &str,
    ) -> Result<String, String> {
        self.calls.push(("mouse_down".into(), button.into()));
        Ok(format!("{button} is down"))
    }
    fn mouse_up(
        &mut self,
        _x: Option<i64>,
        _y: Option<i64>,
        button: &str,
    ) -> Result<String, String> {
        self.calls.push(("mouse_up".into(), button.into()));
        Ok(format!("{button} is up"))
    }
    fn mouse_wheel(
        &mut self,
        _x: Option<i64>,
        _y: Option<i64>,
        direction: &str,
        amount: u32,
    ) -> Result<String, String> {
        self.calls
            .push(("wheel".into(), format!("{direction},{amount}")));
        Ok(format!("the wheel went {direction}"))
    }
    fn click(&mut self, x: i64, y: i64, button: &str, count: u32) -> Result<String, String> {
        self.calls
            .push(("click".into(), format!("{x},{y},{button},{count}")));
        Ok(format!("clicked ({x}, {y})"))
    }
    fn move_pointer(&mut self, x: i64, y: i64) -> Result<String, String> {
        self.pointer = (x, y);
        self.calls.push(("move".into(), format!("{x},{y}")));
        Ok(format!("moved to ({x}, {y})"))
    }
    fn mouse_position(&mut self) -> Result<(i64, i64), String> {
        Ok(self.pointer)
    }
}

impl Screen for Fake {
    fn capture(
        &mut self,
        x: i64,
        y: i64,
        width: i64,
        height: i64,
        filename: &str,
    ) -> Result<String, String> {
        self.calls.push((
            "capture".into(),
            format!("{x},{y},{width},{height},{filename}"),
        ));
        Ok(format!("/tmp/{filename}"))
    }
    fn pixel(&mut self, x: i64, y: i64) -> Result<String, String> {
        self.calls.push(("pixel".into(), format!("{x},{y}")));
        Ok(self
            .pixels
            .iter()
            .find(|(at, _)| *at == (x, y))
            .map(|(_, colour)| colour.clone())
            .unwrap_or_else(|| "#000000".into()))
    }
    fn pixel_search(
        &mut self,
        _x1: i64,
        _y1: i64,
        _x2: i64,
        _y2: i64,
        colour: &str,
    ) -> Result<Option<(i64, i64)>, String> {
        Ok(self
            .pixels
            .iter()
            .find(|(_, value)| value == colour)
            .map(|(at, _)| *at))
    }
}

impl Windows for Fake {
    fn window_active(&mut self, title: &str, mode: TitleMatch) -> Result<WindowAnswer, String> {
        if self.window_answer_unknown {
            return Ok(WindowAnswer::Unknown("this desktop cannot tell".into()));
        }
        // The window that has the focus is a window that is there.
        if let Some(active) = &self.active {
            if mode.matches(active, title)? {
                return Ok(WindowAnswer::Active);
            }
        }
        for window in &self.windows {
            if mode.matches(window, title)? {
                return Ok(WindowAnswer::NotActive);
            }
        }
        // A window that is not there is not the active one.
        Ok(WindowAnswer::Absent)
    }
    fn window_exists(&mut self, title: &str, mode: TitleMatch) -> Result<WindowAnswer, String> {
        if self.window_answer_unknown {
            return Ok(WindowAnswer::Unknown("this desktop cannot list".into()));
        }
        if let Some(active) = &self.active {
            if mode.matches(active, title)? {
                return Ok(WindowAnswer::Active);
            }
        }
        for window in &self.windows {
            if mode.matches(window, title)? {
                return Ok(WindowAnswer::NotActive);
            }
        }
        // A window that is not there is not there.
        Ok(WindowAnswer::Absent)
    }
    fn active_window_title(&mut self) -> Result<Option<String>, String> {
        Ok(self.active.clone())
    }
}

impl Processes for Fake {
    fn run(&mut self, command: &str, _window: &str) -> Result<(i32, String, String), String> {
        self.calls.push(("run".into(), command.into()));
        Ok((0, format!("ran {command}\n"), String::new()))
    }
}

impl Native for Fake {}
impl Com for Fake {}

fn run(source: &str) -> (Report, Fake) {
    // The run goes on a thread with a stack of its own: a debug build's
    // frames are heavy, and on a machine with a small test-thread stack
    // (macOS of sixty-four bits ARM) the shallow call of a closure would
    // otherwise reach the stack budget before it returns. The budget is
    // generous; the recursion that never ends still stops at the depth
    // counter, with the same reason.
    let source = source.to_string();
    std::thread::Builder::new()
        .stack_size(16 * 1024 * 1024)
        .spawn(move || {
            let mut desktop = Fake::default();
            desktop.pixels.push(((10, 20), "#ff8800".into()));
            let statements = parse(&source).expect("parses");
            let report = Runtime::new(&mut desktop, Duration::from_secs(10))
                .with_stack_budget(8 * 1024 * 1024)
                .with_source(&source)
                .run(&statements);
            (report, desktop)
        })
        .expect("the test thread starts")
        .join()
        .expect("the test thread ends")
}

/// The same, with a timeout of its own.
fn run_with_timeout(source: &str, timeout: Duration) -> (Report, Fake) {
    let mut desktop = Fake::default();
    desktop.pixels.push(((10, 20), "#ff8800".into()));
    let statements = parse(source).expect("parses");
    let report = Runtime::new(&mut desktop, timeout)
        .with_source(source)
        .run(&statements);
    (report, desktop)
}

/// The same, but speaking the dialect the caller names.
fn run_in(source: &str, dialect: &'static dyn ahk::dialect::Dialect) -> (Report, Fake) {
    let mut desktop = Fake::default();
    let statements = ahk::parse_with(source, dialect).expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10))
        .with_dialect(dialect)
        .with_source(source)
        .run(&statements);
    (report, desktop)
}

// -- #Requires ---------------------------------------------------------------
//
// Measured on the binaries of both dialects, and on the tree after the
// change: the line names the dialect the script needs -- `v1` or `v2` -- and
// nothing else. The dialects are names and not sub-versions, and the tree has
// no second name of its own to put in the line: the project is called `ahk`,
// and a name made up for the directive would be a name to keep in step with
// the first.
//
// The line still tells the two implementations apart, because mainline
// AutoHotkey reads the first word as the name of the interpreter: measured on
// the binary of v2.0.31, `#Requires v2` is refused with "This script requires
// v2. Current interpreter: AutoHotkey v2.0.31 64-bit.".

#[test]
fn the_dialect_alone_is_the_requirement() {
    let (report, _) = run("#Requires v2\nFileAppend \"ok\", \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ok"]);
}

#[test]
fn the_tree_answers_to_the_mainline_name_as_well() {
    // For a script that is meant to run in both: the mainline binary accepts
    // this line too.
    let (report, _) = run("#Requires AutoHotkey v2\nFileAppend \"ok\", \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ok"]);
}

#[test]
fn the_mainline_name_alone_is_accepted() {
    // Measured on the binary of v2.0.31: the name without a dialect asks for
    // the interpreter and not for a dialect, and it runs.
    let (report, _) = run("#Requires AutoHotkey\nFileAppend \"ok\", \"*\"");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn the_other_dialect_is_refused() {
    let (report, _) = run("#Requires v1");
    assert!(!report.ok);
    let error = report.error.as_deref().unwrap_or("");
    assert!(error.contains("This interpreter is v2."), "{error}");
}

#[test]
fn a_dialect_that_does_not_exist_is_refused() {
    let (report, _) = run("#Requires v3");
    assert!(!report.ok);
    let error = report.error.as_deref().unwrap_or("");
    assert!(error.contains("This script requires v3."), "{error}");
}

#[test]
fn the_invented_name_of_the_tree_is_not_a_name_of_the_tree() {
    // `RustAhk` was a candidate for the line and it is not the name of the
    // project. It is refused like any other word that is not the dialect.
    let (report, _) = run("#Requires RustAhk v2");
    assert!(!report.ok);
    let error = report.error.as_deref().unwrap_or("");
    assert!(
        error.contains("This script requires RustAhk v2."),
        "{error}"
    );
}

#[test]
fn an_empty_requires_says_the_parameter_is_required() {
    let (report, _) = run("#Requires");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Parameter #1 required"));
}

#[test]
fn a_requires_with_a_third_word_is_refused() {
    let (report, _) = run("#Requires AutoHotkey v2 extra");
    assert!(!report.ok);
}

#[test]
fn the_v1_dialect_is_met_under_the_v1_dialect() {
    let (report, _) = run_in("#Requires v1\nFileAppend, ok, *", ahk::dialect::v1());
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ok"]);
}

#[test]
fn the_v2_dialect_is_refused_under_the_v1_dialect() {
    let (report, _) = run_in("#Requires v2", ahk::dialect::v1());
    assert!(!report.ok);
    let error = report.error.as_deref().unwrap_or("");
    assert!(error.contains("This interpreter is v1."), "{error}");
}

#[test]
fn the_version_of_the_interpreter_is_met() {
    // The line of mainline AutoHotkey names the version and not the dialect.
    // Measured on the binary of v2.0.19: all the forms below
    // are met by the 2.0.19, and a version that is written without the `v`
    // is the same version as the one with it.
    //
    // The release of the tree is the 2.0.32, and the forms below name it:
    // the comparison is numeric, and the numbers are the ones of the
    // dialect that runs.
    for requirement in [
        "#Requires AutoHotkey v2.0",
        "#Requires AutoHotkey v2.0.32",
        "#Requires AutoHotkey v2.0.32+",
        "#Requires AutoHotkey 2.0.32",
        "#Requires AutoHotkey >=2.0",
        "#Requires AutoHotkey >2.0",
        "#Requires AutoHotkey <2.1",
        "#Requires AutoHotkey <=2.0.32",
        "#Requires AutoHotkey =2.0.32",
    ] {
        let (report, _) = run(&format!("{requirement}\nFileAppend \"ok\", \"*\""));
        assert!(report.ok, "{requirement}: {:?}", report.error);
        assert_eq!(report.output, vec!["ok"]);
    }
}

#[test]
fn a_later_version_is_refused() {
    // The release of the tree is the 2.0.32, so the version that is later
    // than it is the 2.0.33.
    for requirement in [
        "#Requires AutoHotkey v2.0.33",
        "#Requires AutoHotkey v2.1",
        "#Requires AutoHotkey >=2.1",
        "#Requires AutoHotkey <>2.0.32",
        "#Requires AutoHotkey =2.0",
    ] {
        let (report, _) = run(requirement);
        assert!(
            !report.ok,
            "{requirement} is met, and it is not a version of this tree"
        );
    }
}

#[test]
fn a_requirement_of_another_major_version_is_refused() {
    // Measured on the binary of v2.0.19: the v2 does not meet a
    // requirement of the v1, although 2.0.19 is the later version. The
    // dialects are not versions of one another.
    let (report, _) = run("#Requires AutoHotkey v1.1");
    assert!(!report.ok);
    let error = report.error.as_deref().unwrap_or("");
    assert!(
        error.contains("This script requires AutoHotkey v1.1."),
        "{error}"
    );
}

#[test]
fn a_pre_release_requirement_is_older_than_the_release() {
    // Measured on the binary of v2.0.31: the pre-release is
    // everything from the first dash on, and a version that carries it is
    // older than the same version without it -- the rule of `VerCompare`.
    // The interpreter is a release, so it meets a requirement of its own
    // pre-release and is greater than it, but it is not equal to it. A bare
    // trailing dash is a pre-release too.
    for requirement in [
        "#Requires AutoHotkey v2.0.32-alpha.1",
        "#Requires AutoHotkey v2.0.32-alpha",
        "#Requires AutoHotkey v2.0.32-beta.2",
        "#Requires AutoHotkey v2.0.32-rc1",
        "#Requires AutoHotkey v2.0.32-",
        "#Requires AutoHotkey 2.0.32-alpha.1",
        "#Requires AutoHotkey >=2.0.32-alpha.1",
        "#Requires AutoHotkey >2.0.32-alpha.1",
        "#Requires AutoHotkey v2.0.32-alpha.1+",
        "#Requires AutoHotkey v2.0-alpha.31",
        "#Requires AutoHotkey v2.0.32-alpha.1.2.3.4",
    ] {
        let (report, _) = run(&format!("{requirement}\nFileAppend \"ok\", \"*\""));
        assert!(report.ok, "{requirement}: {:?}", report.error);
        assert_eq!(report.output, vec!["ok"]);
    }
    // The release is not equal to its own pre-release, and a later version
    // is refused whatever pre-release it carries.
    for requirement in [
        "#Requires AutoHotkey =2.0.32-alpha.1",
        "#Requires AutoHotkey =2.0.32-",
        "#Requires AutoHotkey v2.1-alpha.10",
    ] {
        let (report, _) = run(requirement);
        assert!(
            !report.ok,
            "{requirement} is met, and it is not a version of this tree"
        );
    }
}

#[test]
fn a_send_types_the_text() {
    let (report, desktop) = run("Send \"hello\"");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["hello"]);
}

#[test]
fn a_send_with_a_key_presses_it() {
    let (report, desktop) = run("Send \"a{Enter}b\"");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["a", "b"]);
    assert!(desktop.calls.contains(&("key".into(), "Enter".into())));
}

#[test]
fn an_assignment_sets_a_variable() {
    let (report, desktop) = run("x := 41 + 1\nSend \"%x%\"");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["42"]);
}

#[test]
fn a_concatenation_uses_the_dot() {
    let (report, desktop) = run("a := \"x\"\nb := a . \"y\"\nSend b");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["xy"]);
}

#[test]
fn an_if_takes_the_branch_that_holds() {
    let (report, desktop) =
        run("x := 5\nIf x > 3 {\n  Send \"big\"\n} Else {\n  Send \"small\"\n}");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["big"]);
}

#[test]
fn an_if_with_no_else_does_nothing_when_false() {
    let (report, desktop) = run("x := 1\nIf x > 3 {\n  Send \"big\"\n}");
    assert!(report.ok);
    assert!(desktop.types().is_empty());
}

#[test]
fn a_while_runs_until_the_condition_breaks() {
    let (report, desktop) = run("i := 0\nWhile i < 3 {\n  i := i + 1\n}\nSend \"%i%\"");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["3"]);
}

#[test]
fn a_loop_runs_its_body_n_times() {
    let (report, desktop) = run("Loop 3 {\n  Send \"x\"\n}");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["x", "x", "x"]);
}

#[test]
fn a_break_leaves_the_loop() {
    let (report, desktop) =
        run("i := 0\nLoop 10 {\n  i := i + 1\n  If i = 2 {\n    Break\n  }\n}\nSend \"%i%\"");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["2"]);
}

#[test]
fn a_continue_skips_the_rest_of_the_body() {
    let (report, desktop) =
        run("i := 0\nLoop 3 {\n  i := i + 1\n  If i < 3 {\n    Continue\n  }\n  Send \"%i%\"\n}");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["3"]);
}

#[test]
fn a_run_carries_the_output() {
    let (report, _desktop) = run("Run \"echo hello\"");
    assert!(report.ok);
    assert!(
        report
            .output
            .iter()
            .any(|line| line.contains("ran echo hello"))
    );
}

#[test]
fn the_power_binds_tighter_than_the_unary_minus() {
    // Measured on the binary of v2.0.19: `-2 ** 2` is -4
    // there, and not 4.
    let (report, _desktop) = run("x := -2 ** 2");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("x"), Some(&Value::Int(-4)));
}

#[test]
fn a_pixel_get_reads_the_colour() {
    let (report, _desktop) = run("PixelGetColor 10, 20");
    assert!(report.ok);
    assert_eq!(
        report.variables.get("pixel"),
        Some(&Value::Text("#ff8800".into()))
    );
}

#[test]
fn a_screenshot_captures_the_region() {
    let (report, desktop) = run("Screenshot 0, 0, 100, 50, \"shot.png\"");
    assert!(report.ok);
    assert!(
        desktop
            .calls
            .contains(&("capture".into(), "0,0,100,50,shot.png".into()))
    );
}

#[test]
fn a_click_clicks_at_the_position() {
    let (report, desktop) = run("Click 40, 50");
    assert!(report.ok);
    assert!(
        desktop
            .calls
            .contains(&("click".into(), "40,50,left,1".into()))
    );
}

#[test]
fn a_msg_is_collected_as_output() {
    let (report, _desktop) = run("MsgBox \"the answer\"");
    assert_eq!(report.output, vec!["the answer"]);
}

#[test]
fn a_directive_that_is_already_the_case_is_accepted() {
    let (report, _desktop) = run("#NoTrayIcon\n#SingleInstance\n#ErrorStdOut\nMsgBox \"ok\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ok"]);
}

#[test]
fn a_requires_that_is_met_is_accepted() {
    let (report, _desktop) = run("#Requires AutoHotkey v2\nMsgBox \"ok\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ok"]);
}

#[test]
fn a_requires_for_the_dialect_of_the_run_is_accepted() {
    let (report, _desktop) = run("#Requires v2\nMsgBox \"ok\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ok"]);
}

#[test]
fn a_requires_that_is_not_met_fails_the_script() {
    let (report, _desktop) = run("#Requires AutoHotkey v1\nMsgBox \"ok\"");
    assert!(!report.ok);
    assert!(
        report.error.as_deref().unwrap_or("").contains("requires"),
        "{:?}",
        report.error
    );
}

#[test]
fn a_directive_this_runtime_does_not_carry_fails_with_the_reason() {
    // The `#MaxMem` is a directive of the v1, and the v2.0.31 refuses
    // it at the load with "This line does not contain a recognized
    // action." -- measured on the binary of v2.0.31. The
    // tree refuses it the same way.
    let error = parse("#MaxMem 100\nMsgBox \"ok\"").expect_err("the directive is refused");
    assert!(
        error.message.contains("recognized action"),
        "{:?}",
        error.message
    );
    assert_eq!(error.line, 1);
}

#[test]
fn a_directive_that_the_v2_accepts_and_the_tree_cannot_do_is_accepted() {
    // The `#UseHook` is a directive of the v2, and the v2.0.31 accepts
    // it -- measured on the binary of v2.0.31. The tree
    // has one hook, the one of the box, and the directive changes
    // nothing here.
    let (report, _desktop) = run("#UseHook\nMsgBox \"ok\"");
    assert!(report.ok, "{:?}", report.error);
}

/// The options of a hotstring are the ones of the v2.0.32.
///
/// Measured on the binary of the v2.0.32: an option of a hotstring that is
/// not one of them is refused at the load with "Invalid option." and the
/// text that is from it to the end of the options. The v2.0.31 ignored it.
#[test]
fn an_option_of_a_hotstring_that_the_v2_does_not_know_is_refused() {
    let error = parse("#Hotstring Y\n").expect_err("the option is refused");
    assert_eq!(error.message, "Invalid option.\n     Specifically: Y");
    assert_eq!(error.line, 1);
}

/// The text of the refusal names the option and the rest of it.
///
/// Measured on the binary of the v2.0.32: the `R` of the `RndChars` is an
/// option of a hotstring, and the `n` that follows it is not, so the refusal
/// names the text that is from the `n`.
#[test]
fn the_refusal_of_an_option_names_the_text_that_is_from_it() {
    let error = parse("#Hotstring RndChars\n").expect_err("the option is refused");
    assert_eq!(
        error.message,
        "Invalid option.\n     Specifically: ndChars"
    );
}

/// The end characters of the hotstrings are a word of their own.
///
/// Measured on the binary of the v2.0.32: the `EndChars` and the characters
/// that follow it are the end characters, and the `EndCharsX` is an option
/// that the v2 does not know.
#[test]
fn the_end_characters_of_the_hotstrings_are_not_an_option() {
    let (report, _desktop) = run("#Hotstring EndChars `,\nMsgBox \"ok\"");
    assert!(report.ok, "{:?}", report.error);
    let error = parse("#Hotstring EndCharsX `,\n").expect_err("the option is refused");
    assert_eq!(
        error.message,
        "Invalid option.\n     Specifically: EndCharsX `,"
    );
}

/// The options of a hotstring of the script are checked at the load.
///
/// Measured on the binary of the v2.0.32: the options of a hotstring are the
/// text that is between the first colon and the two colons of the trigger,
/// and an option that the v2 does not know is refused with "Invalid option."
#[test]
fn the_options_of_a_hotstring_of_the_script_are_checked_at_the_load() {
    let error = parse(":Y:btw::by the way\n").expect_err("the option is refused");
    assert_eq!(
        error.message,
        "Invalid option.\n     Specifically: Y:btw"
    );
    let (report, _desktop) = run(":C:btw::by the way\nMsgBox \"ok\"");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn a_loop_that_parses_a_text_runs_the_body_once_for_each_piece() {
    let (report, desktop) = run("Loop Parse \"a,b,c\", \",\" {\n  Send \"%A_LoopField%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["a", "b", "c"]);
}

#[test]
fn a_loop_that_parses_a_text_counts_the_pieces() {
    let (report, _desktop) = run("Loop Parse \"a,b\", \",\" {\n  Send \"%A_Index%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    let (report, _desktop) =
        run("Loop Parse \"a,b\", \",\" {\n  last := A_Index\n}\nSend \"%last%\"");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn a_loop_over_the_registry_fails_with_the_reason() {
    let (report, _desktop) = run("Loop Reg \"HKLM\\\\*\" {\n}");
    assert!(!report.ok);
    assert!(
        report.error.as_deref().unwrap_or("").contains("registry"),
        "{:?}",
        report.error
    );
}

#[test]
fn a_string_is_read_where_it_is_written() {
    // The name is the name of the scope that is writing the string, so
    // a string a function returns still knows its own parameter.
    let (report, desktop) =
        run("Greet(name) {\n  return \"hello %name%\"\n}\nSend Greet(\"world\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["hello world"]);
}

#[test]
fn a_property_can_be_in_a_string() {
    let (report, desktop) = run("class Point {\n  x := 3\n}\np := Point()\nSend \"%p.x%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["3"]);
}

#[test]
fn a_builtin_is_a_value() {
    let (report, desktop) = run("f := Abs\nSend \"%f.Name%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["Abs"]);
}

#[test]
fn a_function_in_a_variable_can_be_called() {
    let (report, desktop) = run("f := Abs\nSend f.Call(3)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["3"]);
}

#[test]
fn a_function_knows_how_many_parameters_it_wants() {
    let (report, desktop) = run("f := Abs\nSend \"%f.MinParams% %f.MaxParams%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1 1"]);
}

#[test]
fn a_builtin_is_a_builtin() {
    // `IsBuiltIn` is a property of v2, and not a method.
    let (report, desktop) = run("f := Abs\nSend f.IsBuiltIn");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1"]);
}

#[test]
fn the_is_built_in_of_a_function_is_not_a_method() {
    let (report, _) = run("f := Abs\nMsgBox f.IsBuiltIn()");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Func\" has no method named \"IsBuiltIn\".")
    );
}

#[test]
fn an_object_knows_what_it_has() {
    let (report, desktop) = run(
        "class Point {\n  x := 3\n  Show() {\n    return \"x\"\n  }\n}\n\
         p := Point()\n\
         Send p.HasProp(\"x\") . \" \" . p.HasProp(\"y\") . \" \" . p.HasMethod(\"Show\")",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1 0 1"]);
}

#[test]
fn an_object_knows_the_class_it_is_of() {
    let (report, desktop) = run(
        "class Base {\n}\nclass Child extends Base {\n}\nclass Other {\n}\nc := Child()\n\
         Send c.HasBase(Base.Prototype) . \" \" . c.HasBase(Other.Prototype)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1 0"]);
}

#[test]
fn a_property_can_be_defined_and_taken_away() {
    let (report, desktop) = run(
        "class Box {\n}\nb := Box()\nb.DefineProp(\"x\", {Value: 7})\ny := b.x\n\
         z := b.DeleteProp(\"x\")\nSend \"%y% %z%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["7 7"]);
}

#[test]
fn an_object_that_is_called_is_asked_through_call() {
    // Measured on the binary of v2.0.19: the object is
    // asked through its `Call` method.
    let (report, desktop) = run("class Adder {\n  Call(a, b) {\n    return a + b\n  }\n}\n\
         add := Adder()\nSend add(2, 3)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["5"]);
}

#[test]
fn an_object_without_a_call_method_says_so() {
    let (report, _) = run("class Plain {\n}\n\
         p := Plain()\nMsgBox p(2, 3)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Plain\" has no method named \"Call\".")
    );
}

#[test]
fn a_method_that_is_not_there_is_asked_through_the_call_of_the_class() {
    // The `__Call` of v2 takes the name of the method and the
    // parameters of the call.
    let (report, desktop) = run(
        "class Adder {\n  __Call(name, params) {\n    return \"%name% %params.Length%\"\n  }\n}\n\
         add := Adder()\nSend add.Unknown(2, 3)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["Unknown 2"]);
}

#[test]
fn a_class_has_a_method_and_a_property() {
    let (report, desktop) = run(
        "class Point {\n  x := 3\n  Greet() {\n    return \"hello\"\n  }\n}\n\
         p := Point()\nSend p.Greet()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["hello"]);
}

#[test]
fn a_property_of_an_instance_can_be_read() {
    let (report, _desktop) = run("class Point {\n  x := 3\n}\np := Point()\nSend \"%p.x%\"");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn a_property_of_an_instance_can_be_written() {
    let (report, desktop) =
        run("class Point {\n  x := 3\n}\np := Point()\np.x := 7\nSend \"%p.x%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["7"]);
}

#[test]
fn a_method_sees_the_object_it_was_called_on() {
    let (report, desktop) = run(
        "class Point {\n  x := 3\n  Show() {\n    return \"x is %this.x%\"\n  }\n}\n\
         p := Point()\nSend p.Show()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["x is 3"]);
}

#[test]
fn a_class_can_extend_another_one() {
    let (report, desktop) = run("class Base {\n  Who() {\n    return \"base\"\n  }\n}\n\
         class Child extends Base {\n  Who() {\n    return \"child\"\n  }\n}\n\
         c := Child()\nSend c.Who()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["child"]);
}

#[test]
fn a_method_of_the_class_it_extends_is_there() {
    let (report, desktop) = run("class Base {\n  Who() {\n    return \"base\"\n  }\n}\n\
         class Child extends Base {\n}\nc := Child()\nSend c.Who()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["base"]);
}

#[test]
fn a_new_may_build_the_object() {
    let (report, desktop) = run(
        "class Point {\n  __New(x) {\n    this.x := x\n  }\n  x := 0\n}\n\
         p := Point(9)\nSend \"%p.x%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["9"]);
}

#[test]
fn a_get_is_asked_when_the_property_is_not_there() {
    let (report, desktop) = run(
        "class Box {\n  __Get(name, params) {\n    return \"asked for %name%\"\n  }\n}\n\
         b := Box()\nSend b.Whatever",
    );
    assert!(report.ok, "{:?}", report.error);
    // The name is the name the script wrote.
    assert_eq!(desktop.types(), vec!["asked for Whatever"]);
}

#[test]
fn a_set_is_told_when_the_property_is_written() {
    let (report, desktop) = run(
        "class Box {\n  __Set(name, params, value) {\n    this.seen := name\n    this.written := value\n  }\n\
         seen := \"\"\n  written := \"\"\n}\n\
         b := Box()\nb.thing := 1\nSend b.seen \" \" b.written",
    );
    assert!(report.ok, "{:?}", report.error);
    // The value that is written goes with the name.
    assert_eq!(desktop.types(), vec!["thing 1"]);
}

#[test]
fn a_get_takes_the_parameters_of_the_access() {
    // Measured on the binary of v2.0.19: the brackets of
    // the access are the parameters of the meta-function.
    let (report, desktop) = run(
        "class Box {\n  __Get(name, params) {\n    return \"%name% %params.Length%\"\n  }\n}\n\
         b := Box()\nSend b.Whatever[1]",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["Whatever 1"]);
}

#[test]
fn a_get_handles_the_whole_access() {
    // The answer of the meta-function is the answer of the access: the
    // brackets are its parameters and not an index of its answer.
    let (report, desktop) = run(
        "class Box {\n  __Get(name, params) {\n    return \"the answer\"\n  }\n}\n\
         b := Box()\nSend b.Whatever[1]",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["the answer"]);
}

#[test]
fn a_set_takes_the_value_that_is_written() {
    let (report, desktop) = run(
        "class Box {\n  __Set(name, params, value) {\n    this.written := value\n  }\n\
         written := \"\"\n}\n\
         b := Box()\nb.thing := 7\nSend b.written",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["7"]);
}

#[test]
fn a_function_is_a_value() {
    // The name of a function is the name the script wrote, and not the
    // name it is looked up by.
    let (report, desktop) = run("Add(a, b) {\n  return a + b\n}\nf := Add\nSend f.Name");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["Add"]);
}

#[test]
fn a_parameter_may_carry_a_default() {
    let (report, desktop) = run("Add(a, b := 2) {\n  return a + b\n}\nSend Add(1)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["3"]);
}

#[test]
fn a_method_called_with_too_few_arguments_fails_with_the_reason() {
    // Measured on the binary of v2.0.19.
    let (report, _desktop) =
        run("class Thing {\n  Method(a) {\n    return a\n  }\n}\nt := Thing()\nt.Method()");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Missing a required parameter.")
    );
    assert_eq!(report.error_line, Some(7));
}

#[test]
fn a_method_called_with_too_many_arguments_fails_with_the_reason() {
    let (report, _desktop) =
        run("class Thing {\n  Method(a) {\n    return a\n  }\n}\nt := Thing()\nt.Method(1, 2)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Too many parameters passed to function.")
    );
    assert_eq!(report.error_line, Some(7));
}

#[test]
fn a_method_without_parameters_runs_when_none_are_given() {
    let (report, desktop) = run(
        "class Thing {\n  Method() {\n    return \"the answer\"\n  }\n}\nt := Thing()\nSend t.Method()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["the answer"]);
}

#[test]
fn a_method_takes_the_default_of_a_parameter_that_is_not_given() {
    let (report, desktop) = run(
        "class Thing {\n  Method(a := \"the default\") {\n    return a\n  }\n}\nt := Thing()\nSend t.Method()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["the default"]);
}

#[test]
fn a_parameter_may_take_the_rest_of_the_arguments() {
    let (report, desktop) = run("Add(a, rest*) {\n  return a + rest.Length\n}\nSend Add(1, 2, 3)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["3"]);
}

#[test]
fn a_call_with_too_few_arguments_fails_with_the_reason() {
    // The v2 checks the arity of a function of the script at the load.
    let error = parse("Add(a, b) {\n  return a + b\n}\nAdd(1)").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Too few parameters passed to function.\n     Specifically: Add"
    );
    assert_eq!(error.line, 4);
}

#[test]
fn a_call_with_too_many_arguments_fails_with_the_reason() {
    // The v2 checks the arity of a function of the script at the load.
    let error = parse("Add(a) {\n  return a\n}\nAdd(1, 2)").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Too many parameters passed to function.\n     Specifically: Add"
    );
    assert_eq!(error.line, 4);
}

#[test]
fn a_function_that_is_held_in_a_variable_can_be_called() {
    let (report, desktop) = run("Add(a, b) {\n  return a + b\n}\nf := Add\nSend f(2, 3)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["5"]);
}

#[test]
fn a_method_that_is_not_there_fails_with_the_reason() {
    let (report, _desktop) = run("class Point {\n}\np := Point()\np.Nothing()");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Point\" has no method named \"Nothing\".")
    );
}

#[test]
fn a_variable_that_was_never_set_is_an_error() {
    let (report, _desktop) = run("x := nope");
    assert!(!report.ok);
    assert_eq!(report.error_line, Some(1));
}

#[test]
fn an_unknown_name_in_a_string_is_left_alone() {
    let (report, desktop) = run("Send \"%nope%\"");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["%nope%"]);
}

#[test]
fn a_division_by_zero_is_an_error() {
    let (report, _desktop) = run("x := 1 / 0");
    assert!(!report.ok);
}

#[test]
fn an_endless_loop_is_stopped() {
    let (report, _desktop) = run("Loop 100000 {\n  Send \"x\"\n}");
    assert!(!report.ok);
    assert!(report.error.as_deref().unwrap_or("").contains("more than"));
}

#[test]
fn a_while_body_runs_as_many_turns_as_the_script_asks_for() {
    // Measured on the binary of AutoHotkey v1.1.37.02: the v1 has no limit
    // of turns. The `while` of five thousand turns is a script of the v1,
    // and the tree runs it.
    let (report, _desktop) =
        run("count := 0\nwhile count < 5000 {\n  count := count + 1\n}\nMsgBox count");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["5000"]);
}

#[test]
fn a_script_runs_at_most_twenty_thousand_statements() {
    // A counted `Loop` has no guard of its own: the step count is what stops
    // it, which is why the message is about statements and not about turns.
    let (report, _desktop) = run("Loop 100000 {\n  Send \"x\"\n}");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("the script ran more than 20000 statements")
    );
    assert!(report.error_line.is_some());
}

#[test]
fn a_script_that_outlives_its_timeout_is_stopped() {
    let (report, _desktop) =
        run_with_timeout("Sleep 200\nMsgBox \"late\"", Duration::from_millis(50));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("the script ran for more than 0.05s")
    );
    // The timeout is not a statement: it stops the script between two of
    // them, so it does not know a line, and it says so by leaving it out.
    assert_eq!(report.error_line, None);
}

#[test]
fn every_step_is_reported() {
    let (report, _desktop) = run("Send \"a\"\nSend \"b\"");
    assert_eq!(report.steps.len(), 2);
    assert_eq!(report.steps[0].op, "Send");
    assert_eq!(report.steps[1].op, "Send");
    assert!(report.steps.iter().all(|step| step.ok && step.line > 0));
}

#[test]
fn the_statements_of_a_script_keep_their_order() {
    let statements = parse("x := 1\nSend \"a\"\nSleep 100").expect("parses");
    let names: Vec<&str> = statements.iter().map(ahk::runtime::op_name).collect();
    assert_eq!(names, vec!["Assign", "Send", "Sleep"]);
    assert!(matches!(statements[0], Node::Assign { .. }));
}

// -- the basic syntax -----------------------------------------------------------

#[test]
fn an_else_if_is_taken_when_the_first_one_is_false() {
    let (report, desktop) = run(
        "x := 2\nIf x = 1 {\n  Send \"one\"\n} Else If x = 2 {\n  Send \"two\"\n} Else {\n  Send \"other\"\n}",
    );
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["two"]);
}

#[test]
fn an_else_if_chain_takes_only_the_first_branch_that_holds() {
    let (report, desktop) = run(
        "x := 5\nIf x = 1 {\n  Send \"one\"\n} Else If x = 5 {\n  Send \"two\"\n} Else If x = 5 {\n  Send \"three\"\n} Else {\n  Send \"other\"\n}",
    );
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["two"]);
}

#[test]
fn a_switch_runs_the_case_that_matches() {
    let (report, desktop) = run(
        "x := 2\nSwitch x {\n  Case 1:\n    Send \"one\"\n  Case 2:\n    Send \"two\"\n  Default:\n    Send \"other\"\n}",
    );
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["two"]);
}

#[test]
fn a_switch_without_a_match_runs_the_default() {
    let (report, desktop) =
        run("x := 9\nSwitch x {\n  Case 1:\n    Send \"one\"\n  Default:\n    Send \"other\"\n}");
    assert!(report.ok);
    assert_eq!(desktop.types(), vec!["other"]);
}

#[test]
fn a_function_returns_a_value() {
    let (report, desktop) =
        run("Add(a, b) {\n  return a + b\n}\ntotal := Add(2, 3)\nSend \"%total%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["5"]);
}

#[test]
fn a_function_can_be_called_as_a_statement() {
    let (report, _desktop) = run("Greet(name) {\n  MsgBox \"hello %name%\"\n}\nGreet(\"world\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["hello world"]);
}

#[test]
fn a_function_may_be_defined_after_the_call() {
    let (report, desktop) =
        run("text := Later()\nSend \"%text%\"\nLater() {\n  return \"here\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["here"]);
}

#[test]
fn a_return_leaves_the_function_at_once() {
    let (report, desktop) =
        run("text := First()\nSend \"%text%\"\nFirst() {\n  return \"one\"\n  Send \"never\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["one"]);
}

#[test]
fn a_variable_assigned_inside_a_function_is_local() {
    let (report, _desktop) =
        run("x := \"outside\"\nSet() {\n  x := \"inside\"\n}\nSet()\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["outside"]);
}

#[test]
fn exitapp_ends_the_script_and_carries_a_code() {
    let (report, desktop) = run("Send \"before\"\nExitApp 3\nSend \"after\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.exit_code, Some(3));
    assert_eq!(desktop.types(), vec!["before"]);
}

// -- the reference operator --------------------------------------------------------

#[test]
fn a_parameter_written_by_reference_is_an_alias_of_the_variable_of_the_caller() {
    // The ByRef marker of v2: the parameter and the variable of the
    // caller are the same thing.
    let (report, _desktop) = run("Swap(&a, &b) {\n  tmp := a\n  a := b\n  b := tmp\n}\n\
         a := \"one\"\nb := \"two\"\nSwap(&a, &b)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("a"), Some(&Value::Text("two".into())));
    assert_eq!(report.variables.get("b"), Some(&Value::Text("one".into())));
}

#[test]
fn a_method_may_have_a_parameter_written_by_reference() {
    let (report, _desktop) = run(
        "class Counter {\n  Total(&value) {\n    value := value + 1\n  }\n}\n\
         c := Counter()\nn := 5\nc.Total(&n)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("n"), Some(&Value::Int(6)));
}

#[test]
fn a_parameter_written_by_reference_wants_a_reference() {
    let (report, _desktop) = run("Show(&x) {\n  out := x\n}\nShow(5)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Parameter #1 of Show requires a variable reference, but received an Integer.")
    );
}

#[test]
fn a_parameter_written_by_reference_that_the_call_leaves_out_is_a_local_one() {
    // The default of v2 is a value of its own: the parameter is a local
    // one when the call does not give it a reference.
    let (report, _desktop) = run("Show(&x := \"default\") {\n  return x\n}\nout := Show()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.variables.get("out"),
        Some(&Value::Text("default".into()))
    );
}

#[test]
fn a_reference_is_not_a_value() {
    let (report, _desktop) = run("x := 1\ny := &x\nout := \"v=\" . y");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a String but got a VarRef.")
    );
}

#[test]
fn a_plain_parameter_that_is_given_a_reference_holds_it() {
    // The reference is a value of its own, and the parameter that is
    // not written `&x` holds it.
    let (report, _desktop) = run("Show(x) {\n  out := \"in=\" . x\n}\ny := \"orig\"\nShow(&y)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a String but got a VarRef.")
    );
}

#[test]
fn a_plain_parameter_that_is_given_a_reference_writes_its_own_copy() {
    let (report, _desktop) = run("Show(x) {\n  x := \"written\"\n}\ny := \"orig\"\nShow(&y)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("y"), Some(&Value::Text("orig".into())));
}

#[test]
fn a_reference_where_the_built_in_wants_a_value_fails() {
    // The built-ins that write into a variable of the caller want a
    // reference in that place and nowhere else. Measured on the binary of
    // v2.0.19: the first parameter of the `SplitPath`
    // wants a text, and the reference is refused with the name of it.
    let (report, _desktop) = run("x := \"C:\\file.txt\"\nSplitPath &x, &dir");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Parameter #1 of SplitPath requires a String, but received a VarRef.")
    );
}

#[test]
fn the_reference_operator_wants_a_variable() {
    let (report, _desktop) = run("class Box {\n  Prop := 1\n}\nb := Box()\nr := &b.Prop");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("\"&\" requires a variable."));
}

#[test]
fn a_built_in_variable_cannot_have_its_reference_taken() {
    let (report, _desktop) = run("r := &A_ScreenWidth");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some(
            "This built-in variable cannot have its reference taken.\n     Specifically: A_ScreenWidth"
        )
    );
}

#[test]
fn a_function_cannot_have_its_reference_taken() {
    let (report, _desktop) = run("r := &Abs");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This Func cannot have its reference taken.\n     Specifically: Abs")
    );
}

// -- the focus check ------------------------------------------------------------

#[test]
fn a_loop_over_the_files_takes_the_pattern_of_v2() {
    // v2 takes the pattern first and the mode second, and the folder is
    // the one the script runs in.
    let (report, desktop) = run("Loop Files \"*.txt\" {\n  Send \"%A_LoopFileName%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["one.txt", "two.txt"]);
    assert!(
        desktop
            .calls
            .contains(&("working_directory".to_string(), String::new())),
        "{:?}",
        desktop.calls
    );
    assert!(
        desktop
            .calls
            .contains(&("list_files".to_string(), "/work|*.txt|".to_string())),
        "{:?}",
        desktop.calls
    );
}

#[test]
fn a_loop_over_the_files_writes_the_variables_of_v2() {
    let (report, _desktop) = run(concat!(
        "Loop Files \"*.txt\" {\n",
        "  name := A_LoopFileName\n",
        "  ext := A_LoopFileExt\n",
        "  dir := A_LoopFileDir\n",
        "  full := A_LoopFileFullPath\n",
        "  path := A_LoopFilePath\n",
        "}\n",
        "out := name . \"|\" . ext . \"|\" . dir . \"|\" . full . \"|\" . path\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.variables.get("out"),
        Some(&Value::Text(
            "two.txt|txt|/work|/work/two.txt|/work/two".into()
        ))
    );
}

#[test]
fn the_short_names_of_the_files_are_refused_with_the_reason() {
    // The short names of the files are a thing of Windows, and this box has
    // none: the script that reads the name is told that, and the tree does
    // not make up a name for the file.
    //
    // Measured on the binary of AutoHotkey v2.0.19: the
    // `A_LoopFileShortName` is one of the variables of the loop of the files.
    let (report, _desktop) = run("Loop Files \"*.txt\" {\n  short := A_LoopFileShortName\n}\n");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some(
            "the short names of the files are a thing of Windows, and this desktop is not Windows."
        )
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn a_loop_over_the_files_takes_the_comma_of_v1_too() {
    // v2 takes the comma after the kind of the loop, and does not need
    // it: both forms are the same loop.
    let (report, desktop) = run("Loop Files, \"*.txt\" {\n  Send \"%A_LoopFileName%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["one.txt", "two.txt"]);
}

#[test]
fn a_loop_over_the_files_takes_the_mode_of_v2() {
    let (report, desktop) = run("Loop Files \"*.txt\", \"R\" {\n  Send \"%A_LoopFileName%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert!(
        desktop
            .calls
            .contains(&("list_files".to_string(), "/work|*.txt|R".to_string())),
        "{:?}",
        desktop.calls
    );
}

#[test]
fn a_loop_over_the_files_takes_no_more_than_two_parameters() {
    // Measured on the binary of v2.0.19: the command is
    // refused when the script loads, and the first parameter is named
    // with it.
    let error = parse("Loop Files \"*.txt\", \"R\", \"x\" {\n}\n").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"Loop Files\" accepts at most 2 parameters.\n     Specifically: Loop Files \"*.txt\""
    );
}

#[test]
fn a_recursion_that_never_ends_stops_with_the_reason() {
    // The release has no depth counter: it stops when the stack of the
    // thread runs out, with "Function recursion limit exceeded." -- the
    // words of the source of the v2.0.31 (`source/AutoHotkey.cpp:346`).
    // Measured, the binary of the v2.0.19 answers up to
    // 1 365 nested calls and the binary of the v2.0.31 up to 1 148. The
    // tree has a counter of its own (1 000) and stops before its own
    // stack is gone, saying the same thing.
    let (report, _desktop) = run("F(k) {\n  return F(k + 1)\n}\nF(1)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Function recursion limit exceeded.")
    );
}

#[test]
fn two_values_side_by_side_are_one_value() {
    // The implicit concatenation of v2: measured on the binary of
    // v2.0.19, `"a" 1 + 2` is "a3" and `1 + 2 "d"` is
    // "3d": the concatenation binds like the dot, and the dot is looser
    // than the arithmetic.
    let (report, _desktop) = run("x := \"a\" 1 + 2\ny := \"a\" \"b\" \"c\"\nz := 1 + 2 \"d\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("x"), Some(&Value::Text("a3".into())));
    assert_eq!(report.variables.get("y"), Some(&Value::Text("abc".into())));
    assert_eq!(report.variables.get("z"), Some(&Value::Text("3d".into())));
}

#[test]
fn the_assignment_operators_of_v2_are_there() {
    // The bitwise and the shift forms of v2 are `&=`, `|=`, `^=`,
    // `<<=`, `>>=` and `>>>=`.
    let (report, _desktop) = run(concat!(
        "a := 6\na |= 2\n",
        "\nb := 6\nb &= 2\n",
        "\nc := 6\nc ^= 2\n",
        "\nd := 6\nd <<= 2\n",
        "\ne := 6\ne >>= 1\n",
        "\nf := 6\nf >>>= 1\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("a"), Some(&Value::Int(6)));
    assert_eq!(report.variables.get("b"), Some(&Value::Int(2)));
    assert_eq!(report.variables.get("c"), Some(&Value::Int(4)));
    assert_eq!(report.variables.get("d"), Some(&Value::Int(24)));
    assert_eq!(report.variables.get("e"), Some(&Value::Int(3)));
    assert_eq!(report.variables.get("f"), Some(&Value::Int(3)));
}

#[test]
fn the_match_operator_of_v2_is_the_tilde_one() {
    // v2 spells the match `~=`, and it answers the place of the match:
    // it is zero when the pattern is not there.
    let (report, _desktop) = run(concat!(
        "a := \"a1\" ~= \"\\d\"\n",
        "\nb := \"abc\" ~= \"\\d\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("a"), Some(&Value::Int(2)));
    assert_eq!(report.variables.get("b"), Some(&Value::Int(0)));
}

#[test]
fn a_list_can_be_picked_by_its_position() {
    let (report, _desktop) = run("MouseGetPos &x, &y\nfound := [10, 20]\nfx := found[1]");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("x"), Some(&Value::Int(0)));
    assert_eq!(report.variables.get("y"), Some(&Value::Int(0)));
    assert_eq!(report.variables.get("fx"), Some(&Value::Int(10)));
}

#[test]
fn a_text_cannot_be_picked_by_its_position() {
    // Measured on the binary of v2.0.19: a text has no
    // item, and `SubStr` is the way to take a letter out of one.
    let (report, _desktop) = run("letter := \"hello\"[2]");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"String\" has no property named \"__Item\".")
    );
}

#[test]
fn a_letter_is_taken_out_of_a_text_with_substr() {
    let (report, _desktop) = run("letter := SubStr(\"hello\", 2, 1)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.variables.get("letter"),
        Some(&Value::Text("e".into()))
    );
}

#[test]
fn a_position_that_is_not_there_is_an_error() {
    let (report, _desktop) = run("letter := \"hello\"[99]");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"String\" has no property named \"__Item\".")
    );
}

#[test]
fn a_modifier_before_a_key_is_a_modifier() {
    let (report, desktop) = run("Send \"^a\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("key".to_string(), "ctrl+a".to_string())]
    );
}

#[test]
fn a_modifier_on_its_own_is_text() {
    let (report, desktop) = run("Send \"^\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["^"]);
}

#[test]
fn a_modifier_in_braces_is_text() {
    let (report, desktop) = run("Send \"{+}\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["+"]);
}

#[test]
fn a_key_that_goes_down_stays_down() {
    let (report, desktop) = run("Send \"{a down}\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("key_down".to_string(), "a".to_string())]
    );
}

#[test]
fn a_key_that_comes_up_is_told_to() {
    let (report, desktop) = run("Send \"{a up}\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.calls, vec![("key_up".to_string(), "a".to_string())]);
}

#[test]
fn a_mouse_button_can_be_held_down() {
    let (report, desktop) = run("Send \"{RButton down}\"\nSend \"{RButton up}\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![
            ("mouse_down".to_string(), "right".to_string()),
            ("mouse_up".to_string(), "right".to_string())
        ]
    );
}

#[test]
fn a_click_can_have_a_count() {
    let (report, desktop) = run("Click 10, 20, 2");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("click".to_string(), "10,20,left,2".to_string())]
    );
}

#[test]
fn a_click_can_name_the_button_and_the_count() {
    // Measured on the binary of v2.0.19: the button of a
    // click is a value of the dialect, and not a word of the language:
    // the word `right` is a variable of the script.
    let (report, desktop) = run("right := \"right\"\nClick 10, 20, right, 3");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("click".to_string(), "10,20,right,3".to_string())]
    );
}

#[test]
fn a_number_alone_is_the_number_of_clicks() {
    // The place is two numbers, so a number on its own is the number of
    // clicks: `Click 2` is a double click where the pointer is.
    let (report, desktop) = run("Click 2");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("click".to_string(), "0,0,left,2".to_string())]
    );
}

#[test]
fn the_components_of_a_click_may_be_in_one_string() {
    let (report, desktop) = run("Click \"100 200 Right\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("click".to_string(), "100,200,right,1".to_string())]
    );
}

#[test]
fn a_click_that_would_hold_the_button_holds_it() {
    // Measured on the binary of v2.0.31: the `Click "Down"`
    // aperta o botao e o script continua.
    let (report, desktop) = run("Click \"Down\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("mouse_down".to_string(), "left".to_string())]
    );
}

#[test]
fn a_mouse_button_can_be_held_at_a_place() {
    let (report, desktop) =
        run("MouseMove 100, 200\nSend \"{LButton down}\"\nSend \"{LButton up}\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.calls.len(), 3);
}

#[test]
fn mouse_get_pos_says_where_the_pointer_is() {
    // v2 writes the place of the pointer into the variables of the
    // caller.
    let (report, _desktop) = run("MouseMove 120, 340\nMouseGetPos &x, &y");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("x"), Some(&Value::Int(120)));
    assert_eq!(report.variables.get("y"), Some(&Value::Int(340)));
}

#[test]
fn a_pixel_search_finds_the_colour() {
    // Measured on the binary of v2.0.19: the command takes
    // seven parameters, the first two are references, and it answers one
    // when the colour is there.
    let (report, _desktop) = run("found := PixelSearch(&x, &y, 0, 0, 100, 100, \"#ff8800\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("found"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("x"), Some(&Value::Int(10)));
    assert_eq!(report.variables.get("y"), Some(&Value::Int(20)));
}

#[test]
fn a_pixel_search_that_finds_nothing_answers_zero() {
    let (report, _desktop) = run("found := PixelSearch(&x, &y, 0, 0, 100, 100, \"#ffffff\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("found"), Some(&Value::Int(0)));
    // The place of v2 is blank when the colour is not there.
    assert_eq!(report.variables.get("x"), Some(&Value::Text(String::new())));
    assert_eq!(report.variables.get("y"), Some(&Value::Text(String::new())));
}

#[test]
fn win_active_says_whether_the_window_is_active() {
    let mut desktop = Fake {
        active: Some("Steam".into()),
        ..Default::default()
    };
    let statements = parse("active := WinActive(\"Steam\")").expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("active"), Some(&Value::Int(1)));
}

/// A window that is there but is not in front is not the active one.
#[test]
fn win_active_of_a_window_that_is_not_in_front_is_false() {
    let mut desktop = Fake {
        windows: vec!["Steam".into(), "Firefox".into()],
        active: Some("Steam".into()),
        ..Default::default()
    };
    let statements = parse("active := WinActive(\"Firefox\")").expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("active"), Some(&Value::Int(0)));
}

/// A window that is not there is not the active one.
///
/// This is an answer and not a failure: the desktop said that the window
/// is not there, and that is the end of the question.
#[test]
fn win_active_of_a_window_that_is_not_there_is_false() {
    let mut desktop = Fake {
        windows: vec!["Steam".into()],
        active: Some("Steam".into()),
        ..Default::default()
    };
    let statements = parse("active := WinActive(\"Firefox\")").expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("active"), Some(&Value::Int(0)));
}

/// The turn of the cases that change the mode of the titles.
///
/// The mode is a setting of the crate, and the cases of one file run in
/// threads: a case that reads the mode while another writes it would see the
/// mode of the other one.
static THE_TITLE_MODE_TURN: std::sync::Mutex<()> = std::sync::Mutex::new(());

/// The mode of the matching of the titles is a setting of the script,
/// and the desktop is told it.
#[test]
fn the_mode_of_the_matching_of_the_titles_is_the_one_of_the_script() {
    let _turn = THE_TITLE_MODE_TURN
        .lock()
        .unwrap_or_else(|error| error.into_inner());
    let mut desktop = Fake {
        windows: vec!["Steam Client".into()],
        ..Default::default()
    };
    let statements = parse(
        "SetTitleMatchMode(3)\nthere := WinExist(\"Steam\")\nSetTitleMatchMode(1)\nstarts := WinExist(\"Steam\")\nSetTitleMatchMode(2)\ninside := WinExist(\"Steam\")\nSetTitleMatchMode(\"RegEx\")\npattern := WinExist(\"Steam .*\")\nfolded := WinExist(\"i)steam .*\")\nSetTitleMatchMode(2)",
    )
    .expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(report.ok, "{:?}", report.error);
    // The mode 3 wants the title to be the text, and it is not.
    assert_eq!(report.variables.get("there"), Some(&Value::Int(0)));
    // The mode 1 wants the title to start with the text, and it does.
    assert_eq!(report.variables.get("starts"), Some(&Value::Int(1)));
    // The mode 2 wants the title to have the text in it.
    assert_eq!(report.variables.get("inside"), Some(&Value::Int(1)));
    // The pattern is a regular expression, and the `i)` at the front of it
    // takes the case away.
    assert_eq!(report.variables.get("pattern"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("folded"), Some(&Value::Int(1)));
}

/// A window that is there is there.
#[test]
fn win_exist_says_that_a_window_that_is_there_is_there() {
    let mut desktop = Fake {
        windows: vec!["Steam".into()],
        active: Some("Steam".into()),
        ..Default::default()
    };
    let statements = parse("there := WinExist(\"Steam\")").expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("there"), Some(&Value::Int(1)));
}

/// A window that is not there is not there.
#[test]
fn win_exist_of_a_window_that_is_not_there_is_false() {
    let mut desktop = Fake {
        windows: vec!["Steam".into()],
        active: Some("Steam".into()),
        ..Default::default()
    };
    let statements = parse("there := WinExist(\"Firefox\")").expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("there"), Some(&Value::Int(0)));
}

/// The title of the window that has the focus.
#[test]
fn win_get_title_says_the_title_of_the_window_that_has_the_focus() {
    let mut desktop = Fake {
        windows: vec!["Steam".into(), "Firefox".into()],
        active: Some("Firefox".into()),
        ..Default::default()
    };
    let statements = parse("title := WinGetTitle()").expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.variables.get("title"),
        Some(&Value::Text("Firefox".into()))
    );
}

/// A desktop with no window in front has no title to give.
#[test]
fn win_get_title_without_a_window_in_front_is_empty() {
    let mut desktop = Fake::default();
    let statements = parse("title := WinGetTitle()").expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.variables.get("title"),
        Some(&Value::Text(String::new()))
    );
}

#[test]
fn a_window_question_that_cannot_be_answered_fails_the_script() {
    let mut desktop = Fake {
        window_answer_unknown: true,
        ..Default::default()
    };
    let statements = parse("active := WinActive(\"Steam\")").expect("parses");
    let report = Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements);
    assert!(!report.ok);
    let error = report.error.unwrap_or_default();
    assert!(error.contains("cannot answer"), "{error}");
}

// -- the builtins ---------------------------------------------------------------

#[test]
fn the_numbers_have_their_functions() {
    let (report, _desktop) = run(
        "a := Abs(-3)\nb := Sqrt(16)\nc := Mod(7, 3)\nd := Floor(2.7)\ne := Ceil(2.1)\nf := Round(2.345, 2)\ng := Min(3, 1, 2)\nh := Max(3, 1, 2)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("a"), Some(&Value::Int(3)));
    // AutoHotkey v2 always answers a float for a square root.
    assert_eq!(report.variables.get("b"), Some(&Value::Float(4.0)));
    assert_eq!(report.variables.get("c"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("d"), Some(&Value::Int(2)));
    assert_eq!(report.variables.get("e"), Some(&Value::Int(3)));
    // Measured on the binary of v2.0.19: the `Round` that
    // takes a number of decimals answers a String of exactly that many
    // digits, and not a Float.
    assert_eq!(report.variables.get("f"), Some(&Value::Text("2.35".into())));
    assert_eq!(report.variables.get("g"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("h"), Some(&Value::Int(3)));
}

#[test]
fn the_is_operator_says_whether_an_object_is_of_a_class() {
    let (report, _desktop) = run(
        "class Alpha {\n}\nclass Beta extends Alpha {\n}\nb := Beta()\n\
         a := b is Beta\nc := b is Alpha\nd := b is Object\ne := 3 is Object",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("a"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("c"), Some(&Value::Int(1)));
    // Every object is an Object: it is the root of the chain.
    assert_eq!(report.variables.get("d"), Some(&Value::Int(1)));
    // A number is not an object.
    assert_eq!(report.variables.get("e"), Some(&Value::Int(0)));
}

#[test]
fn a_class_is_a_class_and_an_object_and_not_an_instance_of_itself() {
    // Measured on the binary of v2.0.19: `C is Class` is 1,
    // `C is Object` is 1 and `C is C` is 0 -- a class is not an instance of
    // itself -- and a prototype is not an instance of its own class either.
    let (report, _desktop) = run("class Alpha {\n}\nm := Map\n\
         a := Alpha is Class\nb := Alpha is Object\nc := Alpha is Alpha\n\
         d := m is Class\ne := m is Object\nf := Alpha.Prototype is Alpha");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("a"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("b"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("c"), Some(&Value::Int(0)));
    // The class of a class type of the runtime is the same kind.
    assert_eq!(report.variables.get("d"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("e"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("f"), Some(&Value::Int(0)));
}

#[test]
fn the_is_operator_wants_a_class() {
    let (report, _desktop) = run("class Alpha {\n}\nb := Alpha()\na := b is \"Alpha\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a Class but got a String.")
    );
}

#[test]
fn a_class_has_a_prototype() {
    let (report, _desktop) = run("class Alpha {\n}\np := Alpha.Prototype\nt := Type(Alpha)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.variables.get("t"),
        Some(&Value::Text("Class".into()))
    );
    match report.variables.get("p") {
        Some(Value::Object(object)) => assert_eq!(object.borrow().class, "prototype"),
        other => panic!("the prototype is {other:?}"),
    }
}

#[test]
fn the_text_has_its_functions() {
    let (report, _desktop) = run(
        "a := StrLen(\"hello\")\nb := SubStr(\"hello\", 2)\nc := StrReplace(\"hello\", \"l\", \"L\")\nd := InStr(\"hello\", \"ll\")\ne := StrUpper(\"hi\")\nf := Format(\"{1} of {2}\", 2, 3)\ng := Format(\"{1:.2}\", 2.345)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("a"), Some(&Value::Int(5)));
    // AutoHotkey counts the positions from one.
    assert_eq!(report.variables.get("b"), Some(&Value::Text("ello".into())));
    assert_eq!(
        report.variables.get("c"),
        Some(&Value::Text("heLLo".into()))
    );
    assert_eq!(report.variables.get("d"), Some(&Value::Int(3)));
    assert_eq!(report.variables.get("e"), Some(&Value::Text("HI".into())));
    assert_eq!(
        report.variables.get("f"),
        Some(&Value::Text("2 of 3".into()))
    );
    // Measured on the binary of v2.0.19: the type of the
    // format is `s` when it is not written, and the precision of a String is
    // its length, so `{1:.2}` of 2.345 is the first two characters.
    assert_eq!(report.variables.get("g"), Some(&Value::Text("2.".into())));
}

#[test]
fn a_function_with_the_name_of_a_builtin_wins() {
    let (report, desktop) = run(
        "StrUpper(text) {\n  return \"mine\"\n}\nresult := StrUpper(\"hi\")\nSend \"%result%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["mine"]);
}

#[test]
fn a_builtin_variable_can_be_in_a_string() {
    // The built-in variables are names of the runtime, so a name in a
    // string finds them.
    let (report, desktop) = run("Send \"[%A_AhkVersion%]\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["[2.0.32]"]);
}

#[test]
fn a_builtin_variable_of_the_date_is_answered_in_a_string() {
    // The clock of the host knows the date, and the name of the date is
    // answered with the fourteen digits of v2.
    let (report, desktop) = run("Send \"%A_Now%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types().len(), 1);
    assert_eq!(desktop.types()[0].len(), 14);
}

#[test]
fn an_item_of_a_map_may_be_written() {
    let (report, desktop) = run("m := Map()\nm[\"a\"] := 1\nv := m[\"a\"]\nSend \"%v%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1"]);
}

#[test]
fn an_item_of_an_array_may_be_written() {
    let (report, desktop) = run("a := Array(0)\na[1] := 5\nv := a[1]\nSend \"%v%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["5"]);
}

#[test]
fn an_item_of_an_array_that_is_not_there_may_not_be_written() {
    // An array grows with `Push` and with `InsertAt`, and not with an
    // assignment: the index has to be an item that is there.
    let (report, _desktop) = run("a := Array(0)\na[3] := 5");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Invalid index."));
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn an_item_of_a_class_of_the_script_may_not_be_written() {
    // v2.0.19 asks only the `__Item` property of the item, and the
    // meta-functions answer for the properties that are not there.
    // Measured on the binary of v2.0.19.
    let (report, _desktop) = run("class Box {\n}\nb := Box()\nb[\"a\"] := 1");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Box\" has no property named \"__Item\".")
    );
}

#[test]
fn a_built_in_may_be_written_as_a_call_statement() {
    // The parentheses may be left out when the answer is not needed, and
    // the name is at the start of the line. The answer is discarded.
    let (report, _desktop) = run("Abs 5");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn a_function_of_the_script_may_be_written_as_a_call_statement() {
    let (report, desktop) = run("Greet() {\n    Send \"hello\"\n}\nGreet");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["hello"]);
}

#[test]
fn a_name_that_is_neither_a_built_in_nor_a_function_is_a_variable_of_the_script() {
    // Measured on the binary of v2.0.19: the name of a call
    // statement is not refused at the load. It is a variable of the
    // script, and v2 reads it: a variable that has no value is a variable
    // that has no value.
    let (report, _desktop) = run("Nonsense 1");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This global variable has not been assigned a value.")
    );
    assert_eq!(report.error_line, Some(1));
}

#[test]
fn a_value_that_is_not_an_object_cannot_be_called() {
    // Measured on the binary of v2.0.19: a value that
    // cannot be called is refused with the kind of it, and the failure is
    // of the class `MethodError`.
    let (report, _desktop) = run("x := 5\nx");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Integer\" has no method named \"Call\".")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn a_built_in_variable_that_is_a_number_cannot_be_called_either() {
    // Measured on the binary of v2.0.19: the value of a
    // built-in variable is the value the call asks for.
    let (report, _desktop) = run("A_HotkeyInterval");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Integer\" has no method named \"Call\".")
    );
}

#[test]
fn the_object_literal_of_v2_makes_an_object() {
    let (report, desktop) = run("x := {a: 1, b: 2}\nSend \"%x.a%%x.b%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["12"]);
}

#[test]
fn the_empty_object_literal_is_an_object_of_nothing() {
    let (report, desktop) = run("x := {}\nSend \"%x.__Class%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["Object"]);
}

#[test]
fn the_names_of_the_properties_of_an_object_do_not_know_their_case() {
    let (report, desktop) = run("x := {a: 1}\nSend \"%x.A%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1"]);
}

#[test]
fn the_name_of_a_property_of_an_object_literal_must_be_a_name() {
    // Measured on the binary of v2.0.19: the script does
    // not load at all.
    let error = parse("x := {\"a b\": 1}").expect_err("does not parse");
    assert_eq!(error.message, "Invalid property name in object literal.");
    assert_eq!(error.line, 1);
}

// -- a body that has no head, and a chain of bases that closes -----------------
//
// Measured on the binaries of v2.0.31 and of v1.1.37.02. The
// two versions changed the answer of the first group: v2.0.19 let the line
// pass and ran nothing, and the changelog of v2.0.20 records the fix. The
// second group is the fix of v2.0.27, and the v1 runs the same script, so the
// rule is the dialect's.

#[test]
fn the_else_without_an_if_is_refused_by_the_v2() {
    let error = parse("else\n    FileAppend \"ran\", \"*\"\n").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Else\"");
}

#[test]
fn the_catch_without_a_try_is_refused_by_the_v2() {
    let error = parse("catch\n    FileAppend \"ran\", \"*\"\n").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Catch\"");
}

#[test]
fn the_finally_without_a_try_is_refused_by_the_v2() {
    let error = parse("finally\n    FileAppend \"ran\", \"*\"\n").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Finally\"");
}

#[test]
fn the_v1_says_what_the_body_was_missing() {
    // The v1 does not name the word it found: it says which head is absent.
    for (source, message) in [
        ("else\n    FileAppend, ran, *\n", "ELSE with no matching IF"),
        (
            "catch\n    FileAppend, ran, *\n",
            "CATCH with no matching TRY",
        ),
        (
            "finally\n    FileAppend, ran, *\n",
            "FINALLY with no matching TRY or CATCH",
        ),
    ] {
        let error = ahk::parse_with(source, ahk::dialect::v1()).expect_err("does not parse");
        assert_eq!(error.message, message);
    }
}

#[test]
fn the_bodies_that_have_a_head_still_run() {
    // The refusal is for the word on its own: the legitimate forms reach
    // their own head and are untouched by it.
    let (report, _) = run(
        "if (1)\n    FileAppend \"a\", \"*\"\nelse\n    FileAppend \"b\", \"*\"\n\
         try {\n    FileAppend \"t\", \"*\"\n} catch {\n    FileAppend \"c\", \"*\"\n}\
         finally {\n    FileAppend \"f\", \"*\"\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a", "t", "f"]);
}

#[test]
fn a_class_that_extends_itself_is_refused() {
    let error = parse("class A extends A\n{\n}\n").expect_err("does not parse");
    assert_eq!(error.message, "Invalid base class.\n     Specifically: A");
}

#[test]
fn the_reason_names_the_class_and_not_the_spelling_of_the_base() {
    let error = parse("class A extends a\n{\n}\n").expect_err("does not parse");
    assert_eq!(error.message, "Invalid base class.\n     Specifically: A");
}

#[test]
fn a_pair_that_is_each_others_base_is_refused() {
    let error =
        parse("class A extends B\n{\n}\nclass B extends A\n{\n}\n").expect_err("does not parse");
    assert_eq!(error.message, "Invalid base class.\n     Specifically: B");
}

#[test]
fn a_trio_that_closes_on_itself_is_refused() {
    let error =
        parse("class A extends B\n{\n}\nclass B extends C\n{\n}\nclass C extends A\n{\n}\n")
            .expect_err("does not parse");
    assert_eq!(error.message, "Invalid base class.\n     Specifically: C");
}

#[test]
fn a_chain_of_bases_that_does_not_close_is_a_class() {
    let (report, _) = run("class A\n{\n}\nclass B extends A\n{\n}\nFileAppend \"ran\", \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ran"]);
}

#[test]
fn the_v1_runs_a_class_that_extends_itself() {
    // Measured on the binary of v1.1.37.02: the class is its
    // own base and the script runs.
    let statements = ahk::parse_with(
        "class A extends A\n{\n}\nFileAppend, ran, *\n",
        ahk::dialect::v1(),
    )
    .expect("the v1 parses it");
    let mut desktop = Fake::default();
    let report = Runtime::new(&mut desktop, Duration::from_secs(10))
        .with_dialect(ahk::dialect::v1())
        .run(&statements);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ran"]);
}

#[test]
fn a_property_of_an_object_literal_needs_its_colon() {
    let error = parse("x := {a 1}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"propertyname:\" in object literal."
    );
    assert_eq!(error.line, 1);
}

/// The literal of an object of the v2 is a list of parameters, and the number
/// of them has to be even.
///
/// Measured on the binary of v2.0.19: the literal is a list of
/// parameters: the key of a property and its value are two, and a parameter
/// that is in blank is one. The `x := {, 1}` is an object of no property, and
/// the `x := {, b: 1}`, the `x := {, 1, 2}`, the `x := {b: 1, , c: 2}`, the
/// `x := {b: 1, c}` and the `x := {1, 2}` are refused with "Missing
/// "propertyname:" in object literal.".
#[test]
fn the_literal_of_an_object_of_the_v2_is_a_list_of_parameters() {
    let (report, _) = run("x := {, 1}\nFileAppend ObjOwnPropCount(x), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0".to_string()]);
    let error = parse("x := {, b: 1}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"propertyname:\" in object literal."
    );
    let error = parse("x := {, 1, 2}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"propertyname:\" in object literal."
    );
    let error = parse("x := {b: 1, , c: 2}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"propertyname:\" in object literal."
    );
    let error = parse("x := {b: 1, c}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"propertyname:\" in object literal."
    );
    let error = parse("x := {1, 2}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"propertyname:\" in object literal."
    );
}

/// The blank at the end of the literal of an object of the v2 is accepted.
///
/// Measured on the binary of v2.0.19: the `x := {a: 1,}` is
/// an object of one property, and the `x := {,}` and the `x := {a: 1,,}` are
/// objects of no property. The v1 refuses the comma at the end of the
/// literal with "Blank parameter".
#[test]
fn the_blank_at_the_end_of_the_literal_of_the_v2_is_accepted() {
    let (report, _) = run("x := {a: 1,}\nFileAppend ObjOwnPropCount(x), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1".to_string()]);
    let (report, _) = run("x := {,}\nFileAppend ObjOwnPropCount(x), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0".to_string()]);
    let (report, _) = run("x := {a: 1,,}\nFileAppend ObjOwnPropCount(x), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1".to_string()]);
}

/// The value of the literal of an object of the v2 that has no name is
/// evaluated, and it is dropped.
///
/// Measured on the binary of v2.0.19: the `x := {, f()}` calls
/// f, and the object of it has no property.
#[test]
fn the_value_of_the_literal_of_the_v2_that_has_no_name_is_evaluated_and_dropped() {
    let (report, _) = run(
        "n := 0\nf() {\n    global n\n    n := n + 1\n    return 0\n}\n\
         x := {, f()}\nFileAppend n, \"*\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1".to_string()]);
}

#[test]
fn the_name_of_a_property_of_an_object_literal_may_be_a_keyword() {
    let (report, desktop) = run("x := {class: 1}\nSend \"%x.class%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1"]);
}

#[test]
fn a_comma_at_the_end_of_a_list_is_allowed() {
    // Measured on the binary of v2.0.19: v2 takes a comma at
    // the end of the list of an array, of an object and of the arguments of
    // a call.
    let (report, _desktop) = run(
        "a := [1, 2,]\nx := {a: 1,}\ny := StrLen(\"ab\",)\nMsgBox \"%a.Length%\"\nMsgBox \"%x.a%\"\nMsgBox \"%y%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2", "1", "2"]);
}

#[test]
fn the_arrow_function_of_v2_is_a_function() {
    let (report, _desktop) = run("f := (a) => a + 1\nMsgBox f(1)");
    assert!(report.ok, "{:?}", report.error);
    // Measured on the binary of v2.0.19: the arrow
    // function of v2 is a function, and it takes the parameters it names.
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_arrow_function_without_parentheses_is_a_function() {
    let (report, _desktop) = run("f := a => a + 1\nMsgBox f(1)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_arrow_function_closes_over_the_scope_where_it_was_written() {
    let (report, _desktop) =
        run("Outer() {\n    n := 10\n    f := (a) => a + n\n    return f(1)\n}\nMsgBox Outer()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["11"]);
}

#[test]
fn the_function_that_is_written_inside_another_one_closes_over_it() {
    let (report, _desktop) = run(
        "Outer() {\n    n := 10\n    Inner() {\n        return n\n    }\n    return Inner()\n}\nMsgBox Outer()",
    );
    assert!(report.ok, "{:?}", report.error);
    // Measured on the binary of v2.0.19: a function that is
    // written inside another one sees the variables of the one around it.
    assert_eq!(report.output, vec!["10"]);
}

#[test]
fn the_function_that_is_written_inside_another_one_is_there_before_it_is_defined() {
    let (report, _desktop) = run(
        "Outer() {\n    return Inner()\n    Inner() {\n        return \"inner\"\n    }\n}\nMsgBox Outer()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["inner"]);
}

#[test]
fn the_closure_writes_the_variable_of_the_scope_it_closes_over() {
    let (report, _desktop) = run(
        "Outer() {\n    n := 10\n    Inner() {\n        n := 20\n    }\n    Inner()\n    return n\n}\nMsgBox Outer()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["20"]);
}

#[test]
fn the_assignment_of_v2_is_an_expression() {
    let (report, _desktop) = run("x := (y := 1)\nMsgBox x\nMsgBox y");
    assert!(report.ok, "{:?}", report.error);
    // Measured on the binary of v2.0.19: the assignment of
    // v2 is an expression, and its value is the value that was written.
    assert_eq!(report.output, vec!["1", "1"]);
}

#[test]
fn the_equal_sign_of_v1_is_not_the_assignment_of_v2() {
    // Measured on the binary of v2.0.19: the script does
    // not load at all.
    let error = parse("x = 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Syntax error. Did you mean to use \":=\"?\n     Specifically: x = 1"
    );
    assert_eq!(error.line, 1);
}

#[test]
fn a_parameter_name_that_is_a_number_is_refused() {
    // Measured on the binary of v2.0.19: the name that
    // begins with a number is refused the way the lexer refuses it.
    let error = parse("f(1) {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This variable name starts with a number, which is not allowed:\n\"1\""
    );
}

#[test]
fn a_parameter_name_that_is_a_text_is_refused() {
    // Measured on the binary of v2.0.19: the name that is
    // missing is named, and the line of the script says where it was.
    let error = parse("f(\"x\") {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing parameter name.\n     Specifically: f(\"x\") {"
    );
}

#[test]
fn a_parameter_name_that_is_a_reserved_word_is_refused() {
    // Measured on the binary of v2.0.19.
    let error = parse("f(if) {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "The following reserved word must not be used as a variable name:\n\"if\""
    );
}

#[test]
fn a_parameter_name_that_is_a_comma_is_refused() {
    // Measured on the binary of v2.0.19.
    let error = parse("f(a, b,) {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing parameter name.\n     Specifically: f(a, b,) {"
    );
}

#[test]
fn a_class_name_that_is_a_reserved_word_is_refused() {
    // Measured on the binary of v2.0.19.
    let error = parse("class if {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "The following reserved word must not be used as a class name:\n\"if\""
    );
}

#[test]
fn a_class_name_that_is_a_number_is_refused() {
    // Measured on the binary of v2.0.19.
    let error = parse("class 1 {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This class name starts with a number, which is not allowed:\n\"1\""
    );
}

#[test]
fn a_block_that_is_not_closed_is_refused() {
    // Measured on the binary of v2.0.19: the brace that is
    // not closed is named, and the block that is empty names the
    // construct that opened it.
    let error = parse("f(a) {\n").expect_err("does not parse");
    assert_eq!(error.message, "Missing \"}\"\n     Specifically: f(a) {");
}

#[test]
fn a_class_that_is_not_closed_names_the_class() {
    // Measured on the binary of v2.0.19: the class that was
    // open is the one that is named with the brace that is not closed.
    let error = parse("class Foo {\n    x := 1\n").expect_err("does not parse");
    assert_eq!(error.message, "Missing \"}\"\n     Specifically: Foo");
}

#[test]
fn a_switch_that_is_not_closed_is_refused() {
    // Measured on the binary of v2.0.19: v2 looks for the
    // brace that closes the switch before it reads the cases. The switch
    // that has a statement in it names nothing.
    let error = parse("Switch 1 {\n    x := 1\n").expect_err("does not parse");
    assert_eq!(error.message, "Missing \"}\"");
    // Measured on the binary of v2.0.19: the switch that is
    // empty names the statement of it.
    let error = parse("Switch 1 {").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"}\"\n     Specifically: Switch 1 {"
    );
}

#[test]
fn a_case_needs_its_colon() {
    // Measured on the binary of v2.0.19: the colon of a case
    // is not optional, and the line of the script says where it was.
    let error = parse("Switch 1 {\n    case 1\n        x := 1\n}").expect_err("does not parse");
    assert_eq!(error.message, "Missing \":\"\n     Specifically: case 1");
}

#[test]
fn a_statement_that_is_not_a_case_is_refused() {
    let error = parse("Switch 1 {\n    x := 1\n}").expect_err("does not parse");
    assert_eq!(error.message, "Expected Case/Default.");
}

#[test]
fn a_bracket_that_is_not_closed_is_refused() {
    // Measured on the binary of v2.0.19.
    let error = parse("x := (1\n").expect_err("does not parse");
    assert_eq!(error.message, "Missing \")\"\n     Specifically: x := (1");
}

#[test]
fn a_token_that_is_not_wanted_is_refused() {
    // Measured on the binary of v2.0.19: the token is named,
    // and the line of the script says where it was.
    let error = parse("x := 1 )").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Unexpected \")\"\n     Specifically: x := 1 )"
    );
}

#[test]
fn the_static_variable_of_a_class_is_the_class_that_has_it() {
    // Measured on the binary of v2.0.19: a variable that
    // belongs to the class is the class that has it, and an instance does
    // not see it.
    let (report, _desktop) = run("class Thing {\n    static x := 1\n}\nMsgBox Thing.x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    let (report, _desktop) = run("class Thing {\n    static x := 1\n}\nt := Thing()\nMsgBox t.x");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Thing\" has no property named \"x\".")
    );
}

#[test]
fn the_class_of_v2_is_initialised_when_it_is_first_used() {
    // Measured on the binary of v2.0.19: the class is
    // initialised when it is first used, and the `__New` that is static is
    // the one that runs: once.
    let (report, _desktop) = run(
        "count := 0\nclass Thing {\n    static __New() {\n        global count\n        count := count + 1\n    }\n}\nt := Thing()\nu := Thing()\nMsgBox count",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_method_of_a_class_is_read_only() {
    // Measured on the binary of v2.0.19.
    let (report, _desktop) = run(
        "class Thing {\n    static Say() {\n        return \"said\"\n    }\n}\nThing.Say := () => 1",
    );
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Property is read-only."));
}

#[test]
fn the_concatenation_of_an_object_is_refused() {
    let (report, _desktop) = run("x := {}\nMsgBox \"a\" . x");
    assert!(!report.ok);
    // Measured on the binary of v2.0.19: the concatenation
    // wants a text or a number, and the message names the kind of the value
    // that arrived.
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a String but got an Object.")
    );
}

#[test]
fn the_concatenation_of_an_array_is_refused() {
    let (report, _desktop) = run("x := [1]\nMsgBox \"a\" . x");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a String but got an Array.")
    );
}

#[test]
fn the_concatenation_of_a_function_is_refused() {
    let (report, _desktop) = run("x := () => 1\nMsgBox \"a\" . x");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a String but got a Func.")
    );
}

#[test]
fn the_concatenation_of_a_number_is_a_text() {
    let (report, _desktop) = run("MsgBox \"a\" 1");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a1"]);
}

#[test]
fn the_maths_refuses_an_object() {
    let (report, _desktop) = run("x := {}\nMsgBox 1 + x");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a Number but got an Object.")
    );
}

#[test]
fn the_maths_refuses_an_array() {
    let (report, _desktop) = run("x := [1]\nMsgBox 1 + x");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a Number but got an Array.")
    );
}

#[test]
fn the_maths_refuses_a_text_that_is_not_a_number() {
    let (report, _desktop) = run("x := \"a\"\nMsgBox 1 + x");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a Number but got a String.")
    );
}

#[test]
fn the_parenthesis_may_open_an_argument_of_a_command() {
    let (report, _desktop) = run("MsgBox (1 + 2)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
}

#[test]
fn a_name_and_a_parenthesis_with_a_space_are_not_a_call() {
    // Measured on the binary of v2.0.19: the space before
    // the parenthesis tells a call from the concatenation of v2.
    let (report, _desktop) = run("x := 1\nMsgBox x (2)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["12"]);
}

#[test]
fn the_whole_number_of_the_bitwise_operations_is_a_number_first() {
    let (report, _desktop) = run("MsgBox 1 & \"2\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0"]);
}

#[test]
fn a_number_with_a_point_is_not_a_whole_number() {
    let (report, _desktop) = run("MsgBox 1 & 1.5");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected an Integer but got a Float.")
    );
}

#[test]
fn the_hex_of_v2_is_a_whole_number() {
    let (report, desktop) = run("x := 0xE9\nSend \"%x%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["233"]);
}

#[test]
fn the_hex_of_v2_wraps_the_way_the_machine_does() {
    let (report, desktop) = run("x := 0xFFFFFFFFFFFFFFFF\nSend \"%x%\"");
    assert!(report.ok, "{:?}", report.error);
    // Measured on the binary of v2.0.19: the hex of v2 is
    // a number of the machine, and it wraps the way the machine does.
    assert_eq!(desktop.types(), vec!["-1"]);
}

#[test]
fn a_number_with_an_exponent_is_a_float() {
    let (report, desktop) = run("x := 1e3\nSend \"%x%\"");
    assert!(report.ok, "{:?}", report.error);
    // Measured on the binary of v2.0.19: the exponent makes
    // the number a float, and a float keeps its point.
    assert_eq!(desktop.types(), vec!["1000.0"]);
}

#[test]
fn a_number_with_a_point_is_a_float() {
    let (report, desktop) = run("x := 3.0\nSend \"%x%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["3.0"]);
}

#[test]
fn a_name_that_begins_with_a_number_does_not_parse() {
    // The check is a check of the load, the way v2 has it: the script
    // does not run at all.
    let error = parse("x := 12abc").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This variable name starts with a number, which is not allowed:\n\"12abc\""
    );
    assert_eq!(error.line, 1);
}

#[test]
fn an_item_may_be_written_with_an_operator() {
    let (report, desktop) =
        run("m := Map()\nm[\"a\"] := 1\nm[\"a\"] += 2\nv := m[\"a\"]\nSend \"%v%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["3"]);
}

// -- the array and the for loop ---------------------------------------------------

#[test]
fn an_array_literal_is_an_array() {
    let (report, desktop) = run("a := [\"x\", \"y\"]\nSend \"%a.Length%\"\nSend a[1]");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["2", "x"]);
}

#[test]
fn an_empty_array_literal_is_an_array_of_nothing() {
    let (report, desktop) = run("a := []\nSend \"%a.Length%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["0"]);
}

#[test]
fn an_array_literal_holds_the_values_it_is_given() {
    let (report, desktop) = run("x := 2\na := [x + 1, \"n\" . x]\nSend a[1]\nSend a[2]");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["3", "n2"]);
}

#[test]
fn a_for_walks_the_values_of_an_array() {
    let (report, desktop) = run("for v in [\"a\", \"b\"] {\n  Send v\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["a", "b"]);
}

#[test]
fn a_for_with_two_variables_walks_the_places_and_the_values() {
    let (report, desktop) = run("for i, v in [\"a\", \"b\"] {\n  Send \"%i%:%v%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1:a", "2:b"]);
}

#[test]
fn a_for_walks_the_keys_of_a_map() {
    // The enumerator of a map gives the key, and the value under it
    // when the loop wants two.
    let (report, desktop) = run("m := Map()\nm.Set(\"a\", 1)\nfor v in m {\n  Send v\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["a"]);
}

#[test]
fn a_for_with_two_variables_walks_the_keys_and_the_values_of_a_map() {
    let (report, desktop) =
        run("m := Map()\nm.Set(\"a\", 1)\nfor k, v in m {\n  Send \"%k%=%v%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["a=1"]);
}

#[test]
fn a_for_may_leave_the_key_out() {
    let (report, desktop) = run("m := Map()\nm.Set(\"a\", 1)\nfor , v in m {\n  Send v\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1"]);
}

#[test]
fn a_for_counts_its_turns() {
    let (report, desktop) = run("for v in [\"a\", \"b\"] {\n  Send \"%A_Index%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1", "2"]);
}

#[test]
fn a_break_leaves_a_for() {
    let (report, desktop) =
        run("for v in [\"a\", \"b\", \"c\"] {\n  If v = \"b\" {\n    Break\n  }\n  Send v\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["a"]);
}

#[test]
fn a_continue_skips_the_rest_of_a_for() {
    let (report, desktop) =
        run("for v in [\"a\", \"b\"] {\n  If v = \"a\" {\n    Continue\n  }\n  Send v\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["b"]);
}

#[test]
fn a_call_statement_may_take_an_array() {
    // The space is the reason: `f[1, 2]` is the item of the value `f`,
    // and `f [1, 2]` is a call whose argument is the array.
    let (report, _desktop) = run("f(a) {\n  return a.Length\n}\nf [1, 2]");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn a_for_gives_the_variables_back() {
    let (report, desktop) = run("v := \"before\"\nfor v in [\"a\"] {\n}\nSend v");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["before"]);
}

#[test]
fn a_for_leaves_no_variable_behind() {
    // A name that had no value before the loop has no value after it.
    let (report, _desktop) = run("for v in [\"a\"] {\n}\nSend v");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This global variable has not been assigned a value.")
    );
}

#[test]
fn a_for_walks_what_a_class_of_the_script_enumerates() {
    // Measured on the binary of v2.0.19: the enumerator of
    // v2 is something that can be called, and a function of the script is
    // one: it writes the values into the variables of the loop.
    let (report, desktop) = run(
        "values := [\"a\", \"b\"]\nindex := 0\nNext(&v) {\n  global values, index\n  if index >= values.Length\n    return false\n  v := values[index + 1]\n  index += 1\n  return true\n}\nclass Bag {\n  __Enum(n) {\n    return Next\n  }\n}\nfor v in Bag() {\n  Send v\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["a", "b"]);
}

#[test]
fn an_enumerator_that_is_a_list_is_refused() {
    // Measured on the binary of v2.0.19: the enumerator of
    // v2 is something that can be called, and a list is not one.
    let (report, _desktop) =
        run("class Bag {\n  __Enum(n) {\n    return [\"a\", \"b\"]\n  }\n}\nfor v in Bag() {\n}");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Value not enumerable."));
}

#[test]
fn an_enumerator_that_is_a_number_is_refused() {
    // Measured on the binary of v2.0.19: a value that is
    // not an object at all is a mismatch of the type.
    let (report, _desktop) =
        run("class Bag {\n  __Enum(n) {\n    return 5\n  }\n}\nfor v in Bag() {\n}");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Type mismatch."));
}

#[test]
fn a_for_gives_the_variables_back_inside_a_function() {
    let (report, desktop) =
        run("f() {\n  v := \"before\"\n  for v in [\"a\"] {\n  }\n  return v\n}\nSend f()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["before"]);
}

#[test]
fn a_for_over_a_value_that_cannot_be_enumerated_fails_with_the_reason() {
    let (report, _desktop) = run("for v in 42 {\n}");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Value not enumerable."));
    assert_eq!(report.error_line, Some(1));
}

#[test]
fn a_for_over_an_object_without_an_enumerator_fails_with_the_reason() {
    let (report, _desktop) = run("class Box {\n}\nfor v in Box() {\n}");
    assert!(!report.ok);
    let error = report.error.unwrap_or_default();
    assert_eq!(error, "Value not enumerable.");
}

#[test]
fn a_for_walks_every_value_of_the_source() {
    // Measured on the binary of AutoHotkey v1.1.37.02: the v1 has no limit
    // of turns, and the `for` of the v1 is the walk of the keys of an
    // array: an array of a thousand names is a script of the v1.
    let (report, _desktop) = run(
        "a := []\nLoop 5000 {\n  a.Push(1)\n}\ncount := 0\nfor v in a {\n  count := count + 1\n}\nMsgBox count",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["5000"]);
}

// -- the error handling --------------------------------------------------------

#[test]
fn a_try_catches_the_error_of_the_runtime() {
    let (report, _desktop) = run(concat!(
        "try {\n",
        "  MsgBox NoSuchVariable\n",
        "} catch {\n",
        "  MsgBox \"caught\"\n",
        "}\n",
        "MsgBox \"after\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["caught", "after"]);
}

#[test]
fn a_variable_of_a_function_that_has_no_value_is_a_local_one() {
    // Measured on the binary of v2.0.19.
    let (report, _) =
        run("Helper()\n\nHelper() {\n    for answer in [\"a\"] {\n    }\n    MsgBox answer\n}");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This local variable has not been assigned a value.")
    );
}

#[test]
fn a_catch_binds_the_value_that_was_thrown() {
    let (report, _desktop) = run(concat!(
        "try {\n",
        "  MsgBox NoSuchVariable\n",
        "} catch as e {\n",
        "  MsgBox e.Message\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["This global variable has not been assigned a value."]
    );
}

#[test]
fn the_else_of_a_try_runs_when_nothing_was_thrown() {
    let (report, _desktop) = run(concat!(
        "try {\n",
        "  MsgBox \"in the try\"\n",
        "} catch {\n",
        "  MsgBox \"in the catch\"\n",
        "} else {\n",
        "  MsgBox \"in the else\"\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["in the try", "in the else"]);
}

#[test]
fn the_finally_of_a_try_always_runs() {
    let (report, _desktop) = run(concat!(
        "try {\n",
        "  MsgBox \"in the try\"\n",
        "} catch {\n",
        "  MsgBox \"in the catch\"\n",
        "} finally {\n",
        "  MsgBox \"in the finally\"\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["in the try", "in the finally"]);
}

#[test]
fn a_thrown_value_is_not_an_error_and_a_catch_of_error_does_not_take_it() {
    // Measured on the binary of v2.0.19: a catch without a
    // class takes the class `Error`, and a text that is thrown is not one.
    let (report, _desktop) = run(concat!(
        "try {\n",
        "  throw \"the error of the script\"\n",
        "} catch {\n",
        "  MsgBox \"in the catch\"\n",
        "}\n",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Unhandled exception.\n     Specifically: the error of the script")
    );
}

#[test]
fn a_catch_of_any_takes_a_thrown_value() {
    let (report, _desktop) = run(concat!(
        "try {\n",
        "  throw \"the error of the script\"\n",
        "} catch Any as e {\n",
        "  MsgBox e\n",
        "}\n",
        "MsgBox \"after\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the error of the script", "after"]);
}

#[test]
fn the_message_of_an_uncaught_error_is_the_message_of_the_error() {
    // Measured on the binary of v2.0.19: the dialog of an
    // uncaught error shows the message of the error, and the report of the
    // runner carries the same text, so a tool that reads it learns what the
    // script threw instead of a generic line. A value that is not an error
    // keeps the generic line.
    let (report, _desktop) = run("f() {\n    throw Error(\"boom\")\n}\nf()");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("boom"));
    let (report, _desktop) = run("throw ValueError(\"bad value\")");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("bad value"));
    let (report, _desktop) = run("throw {a: 1}");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Unhandled exception."));
}

#[test]
fn a_throw_without_a_value_throws_again_what_the_catch_took() {
    let (report, _desktop) = run(concat!(
        "try {\n",
        "  throw \"the error of the script\"\n",
        "} catch Any as e {\n",
        "  throw\n",
        "}\n",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Unhandled exception.\n     Specifically: the error of the script")
    );
}

#[test]
fn the_error_of_the_runtime_that_nobody_catches_is_the_error_of_the_run() {
    let (report, _desktop) = run("MsgBox NoSuchVariable\n");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This global variable has not been assigned a value.")
    );
}

#[test]
fn a_catch_of_a_class_that_the_tree_does_not_know_fails_with_the_reason() {
    let (report, _desktop) = run(concat!(
        "try {\n",
        "  MsgBox NoSuchVariable\n",
        "} catch NoSuchClass {\n",
        "  MsgBox \"caught\"\n",
        "}\n",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("the class of a catch is not a class of this runtime: NoSuchClass")
    );
}

#[test]
fn the_names_of_the_variables_are_the_names_of_the_dialect() {
    // Measured on the binary of v2.0.19: the dialect does
    // not know the case of the names.
    let (report, _) = run("x := 1\nMsgBox X\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn a_variable_written_in_another_case_is_the_same_variable() {
    let (report, _) = run("x := 1\nX := 2\nMsgBox x\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn a_parameter_read_in_another_case_is_the_same_parameter() {
    let (report, _) = run("f(p) {\n  return P\n}\nMsgBox f(\"the parameter\")\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the parameter"]);
}

#[test]
fn a_property_that_is_not_there_names_the_type_of_the_value() {
    // Measured on the binary of v2.0.19.
    let (report, _) = run("x := 5\nMsgBox x.nothing");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Integer\" has no property named \"nothing\".")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn a_method_that_is_not_there_names_the_type_of_the_value() {
    let (report, _) = run("x := 5\nMsgBox x.nothing()");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Integer\" has no method named \"nothing\".")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn an_item_that_is_not_there_names_the_type_of_the_value() {
    let (report, _) = run("x := 5\nMsgBox x[1]");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Integer\" has no property named \"__Item\".")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn the_refusal_of_a_text_names_the_type_of_the_value() {
    let (report, _) = run("x := \"abc\"\nMsgBox x.nothing");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"String\" has no property named \"nothing\".")
    );
}

#[test]
fn the_refusal_of_an_object_names_the_class_of_the_script() {
    let (report, _) = run("class Thing {\n}\nt := Thing()\nMsgBox t.nothing");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Thing\" has no property named \"nothing\".")
    );
}

#[test]
fn the_refusal_of_a_class_type_names_the_class() {
    let (report, _) = run("m := Map()\nMsgBox m.nothing");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Map\" has no property named \"nothing\".")
    );
}

#[test]
fn the_write_of_a_property_that_is_not_there_names_the_type() {
    // Measured on the binary of v2.0.19: the write is
    // refused the same way the read is.
    let (report, _) = run("x := 5\nx.nothing := 2");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Integer\" has no property named \"nothing\".")
    );
}

#[test]
fn the_write_of_an_item_that_is_not_there_names_the_type() {
    let (report, _) = run("x := 5\nx[1] := 2");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Integer\" has no property named \"__Item\".")
    );
}

#[test]
fn the_write_of_a_property_of_a_class_of_the_script_makes_the_property() {
    // Measured on the binary of v2.0.19: v2 does not refuse
    // the write of a property that the class does not declare, and the
    // property is there afterwards.
    let (report, _) = run("class Thing {\n}\nt := Thing()\nt.nothing := 2\nMsgBox t.nothing");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn a_method_of_the_script_is_a_value() {
    // Measured on the binary of v2.0.19: a method is
    // handed over as a function whose first parameter is `this`.
    let (report, _) = run(
        "class Thing {\n  Method() {\n    return \"the answer\"\n  }\n}\nt := Thing()\nf := t.Method\nMsgBox \"%f.Name% %f.MinParams% %f.MaxParams%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Thing.Prototype.Method 1 1"]);
}

#[test]
fn a_name_that_is_a_statement_calls_the_method() {
    // Measured on the binary of v2.0.19: a name that is a
    // statement is a call, and the parentheses may be left out.
    let (report, _) =
        run("class Thing {\n  Method(a) {\n    return a\n  }\n}\nt := Thing()\nt.Method");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Missing a required parameter.")
    );
}

#[test]
fn a_name_that_is_a_statement_of_a_property_says_that_it_is_a_method() {
    // Measured on the binary of v2.0.19: `m.Count` is a
    // call of a method, and `Count` is a property.
    let (report, _) = run("m := Map()\nm.Count");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Map\" has no method named \"Count\".")
    );
}

#[test]
fn an_item_that_is_a_statement_is_read() {
    // Measured on the binary of v2.0.19: `m["a"]` reads the
    // item, and the value it leaves behind is not used.
    let (report, _) = run("m := Map()\nm[\"a\"]");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Item has no value."));
}

#[test]
fn the_call_of_a_function_is_a_value() {
    // Measured on the binary of v2.0.19.
    let (report, _) = run("f := Abs.Call\nMsgBox f.Name");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Func.Prototype.Call"]);
}

#[test]
fn a_method_held_in_a_variable_is_called_with_the_object() {
    let (report, desktop) = run(
        "class Thing {\n  Method() {\n    return \"the answer\"\n  }\n}\nt := Thing()\nf := t.Method\nSend f(t)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["the answer"]);
}

#[test]
fn a_method_held_in_a_variable_without_the_object_fails() {
    let (report, _) = run(
        "class Thing {\n  Method() {\n    return \"the answer\"\n  }\n}\nt := Thing()\nf := t.Method\nSend f()",
    );
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Missing a required parameter.")
    );
}

#[test]
fn the_method_of_the_class_is_not_a_value() {
    // A class is a value of its own kind, and v2 does not hand its
    // methods over.
    let (report, _) = run("class Thing {\n  Method() {\n    return 1\n  }\n}\nMsgBox Thing.Method");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Class\" has no property named \"Method\".")
    );
}

#[test]
fn a_function_says_whether_it_is_built_in() {
    // `IsBuiltIn` and `IsVariadic` are properties of v2, and not
    // methods.
    let (report, _) = run("f := Abs\nMsgBox \"%f.IsBuiltIn%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn an_error_carries_its_message() {
    // Measured on the binary of v2.0.19.
    let (report, _) =
        run("try {\n    throw Error(\"the message\")\n} catch as e {\n    MsgBox e.Message\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the message"]);
}

#[test]
fn an_error_carries_what_and_extra() {
    let (report, _) = run(
        "try {\n    throw Error(\"the message\", \"the what\", \"the extra\")\n} catch as e {\n    MsgBox \"%e.What% %e.Extra%\"\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the what the extra"]);
}

#[test]
fn an_error_thrown_in_a_function_names_it() {
    let (report, _) = run(
        "try {\n    Helper()\n} catch as e {\n    MsgBox e.What\n}\n\nHelper() {\n    throw Error(\"the message\")\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Helper"]);
}

#[test]
fn the_extra_of_a_missing_parameter_is_its_name() {
    let (report, _) = run(
        "class Thing {\n  Method(a) {\n    return a\n  }\n}\ntry {\n    t := Thing()\n    t.Method()\n} catch as e {\n    MsgBox e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a"]);
}

#[test]
fn the_extra_of_too_many_arguments_is_the_method() {
    let (report, _) = run(
        "class Thing {\n  Method(a) {\n    return a\n  }\n}\ntry {\n    t := Thing()\n    t.Method(1, 2)\n} catch as e {\n    MsgBox e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Thing.Prototype.Method"]);
}

#[test]
fn the_init_of_a_class_runs_when_the_instance_is_made() {
    // Measured on the binary of v2.0.19.
    let (report, _) = run(concat!(
        "class Thing {\n",
        "  __Init() {\n",
        "    MsgBox \"init\"\n",
        "  }\n",
        "}\n",
        "t := Thing()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["init"]);
}

#[test]
fn the_init_of_a_class_runs_before_the_new() {
    let (report, _) = run(concat!(
        "class Thing {\n",
        "  __New() {\n",
        "    MsgBox \"new\"\n",
        "  }\n",
        "  __Init() {\n",
        "    MsgBox \"init\"\n",
        "  }\n",
        "}\n",
        "t := Thing()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["init", "new"]);
}

#[test]
fn the_init_of_the_derived_replaces_the_one_of_the_base() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  __Init() {\n",
        "    MsgBox \"base init\"\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  __Init() {\n",
        "    MsgBox \"derived init\"\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["derived init"]);
}

#[test]
fn the_init_of_the_base_runs_when_the_derived_has_none() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  __Init() {\n",
        "    MsgBox \"base init\"\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "}\n",
        "d := Derived()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["base init"]);
}

#[test]
fn the_init_of_the_base_runs_when_the_derived_calls_super() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  __Init() {\n",
        "    MsgBox \"base init\"\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  __Init() {\n",
        "    MsgBox \"derived init\"\n",
        "    super.__Init()\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["derived init", "base init"]);
}

#[test]
fn the_variables_of_the_base_are_not_assigned_when_the_derived_has_an_init() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  value := \"the value of the base\"\n",
        "}\n",
        "class Derived extends Base {\n",
        "  __Init() {\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.value\n",
    ));
    assert!(!report.ok);
    assert!(
        report.error.as_deref().unwrap_or("").contains("value"),
        "{:?}",
        report.error
    );
}

#[test]
fn the_variables_of_the_base_are_assigned_when_the_derived_calls_super() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  value := \"the value of the base\"\n",
        "}\n",
        "class Derived extends Base {\n",
        "  __Init() {\n",
        "    super.__Init()\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.value\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the value of the base"]);
}

#[test]
fn the_init_of_a_class_runs_for_every_instance() {
    let (report, _) = run(concat!(
        "class Thing {\n",
        "  __Init() {\n",
        "    MsgBox \"init\"\n",
        "  }\n",
        "}\n",
        "a := Thing()\n",
        "b := Thing()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["init", "init"]);
}

#[test]
fn the_init_of_a_class_does_not_run_when_the_class_is_only_referenced() {
    let (report, _) = run(concat!(
        "class Thing {\n",
        "  __Init() {\n",
        "    MsgBox \"init\"\n",
        "  }\n",
        "}\n",
        "x := Thing\n",
        "MsgBox \"after the reference\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["after the reference"]);
}

#[test]
fn a_super_calls_the_method_of_the_base() {
    // Measured on the binary of v2.0.19.
    let (report, _) = run(concat!(
        "class Base {\n",
        "  m() {\n",
        "    return \"the base\"\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  m() {\n",
        "    return \"the derived and \" . super.m()\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.m()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the derived and the base"]);
}

#[test]
fn a_super_gives_the_base_the_same_this() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  m() {\n",
        "    return this.value\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  m() {\n",
        "    this.value := \"the value\"\n",
        "    return super.m()\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.m()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the value"]);
}

#[test]
fn a_super_in_a_grandchild_asks_the_middle() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  m() {\n",
        "    return \"the base\"\n",
        "  }\n",
        "}\n",
        "class Middle extends Base {\n",
        "  m() {\n",
        "    return \"the middle and \" . super.m()\n",
        "  }\n",
        "}\n",
        "class Derived extends Middle {\n",
        "  m() {\n",
        "    return \"the derived and \" . super.m()\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.m()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["the derived and the middle and the base"]
    );
}

#[test]
fn a_super_reads_the_property_of_the_base_through_its_getter() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  __Get(name, params) {\n",
        "    return \"the value of \" . name\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  m() {\n",
        "    return super.value\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.m()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the value of value"]);
}

#[test]
fn a_super_asks_the_base_for_the_item() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  __Item(key) {\n",
        "    return \"the item \" . key\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  m() {\n",
        "    return super[\"value\"]\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.m()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the item value"]);
}

#[test]
fn a_super_in_a_static_method_is_the_base_class_object() {
    // Measured on the binary of v2.0.31 and read in its
    // expression evaluator: in a static method `super` is the class that the
    // running class extends, so the static members of the base are reached
    // through it. In an instance method `super` stays the base prototype.
    let (report, _) = run(concat!(
        "class Base {\n",
        "  static Foo := 42\n",
        "}\n",
        "class Child extends Base {\n",
        "  static __New() {\n",
        "    FileAppend super.Foo, \"*\"\n",
        "  }\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["42"]);
}

#[test]
fn a_super_in_a_static_method_writes_the_base_prototype() {
    // `super.Prototype` is the prototype of the base class: a member written
    // on it is read back from it. This is the mechanism the dark-theme
    // ListView libraries use to graft methods onto a built-in control class.
    let (report, _) = run(concat!(
        "class Base {\n",
        "}\n",
        "class Child extends Base {\n",
        "  static __New() {\n",
        "    super.Prototype.X := 5\n",
        "    FileAppend super.Prototype.X, \"*\"\n",
        "  }\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["5"]);
}

#[test]
fn a_property_of_the_prototype_reaches_the_instances() {
    // Measured on the binary of v2.0.31: a member written on
    // the prototype is read on the instances of the class -- the lookup walks
    // the base chain. This is how the dark-theme libraries graft members onto
    // a built-in control class.
    let (report, _) = run(concat!(
        "class Base {\n",
        "}\n",
        "Base.Prototype.X := 5\n",
        "FileAppend Base().X, \"*\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["5"]);
}

#[test]
fn a_function_grafted_onto_the_prototype_is_called_on_the_instances() {
    // Measured on the binary of v2.0.31: a function written on
    // the prototype is called as the method of the instances. This is the
    // mechanism the dark-theme ListView libraries use -- SetDarkMode grafted
    // onto Gui.ListView.Prototype and called on the control.
    let (report, _) = run(concat!(
        "class Base {\n",
        "}\n",
        "Base.Prototype.Hello := (*) => \"hi\"\n",
        "FileAppend Base().Hello(), \"*\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["hi"]);
}

#[test]
fn a_struct_lays_out_its_fields_and_reads_them() {
    // Measured on the binary of v2.1-alpha.39: the fields are
    // laid out with the alignment of their types, the constructor takes them
    // in order, and the size is the whole of the layout.
    let (report, _) = run_in(
        concat!(
            "struct POINT {\n",
            "    x : Int32, y : Int32\n",
            "}\n",
            "p := POINT(10, 20)\n",
            "FileAppend p.x \",\" p.y \" size=\" p.Size, \"*\"\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["10,20 size=8"]);
}

#[test]
fn a_struct_field_is_written_into_its_memory() {
    let (report, _) = run_in(
        concat!(
            "struct POINT {\n",
            "    x : Int32, y : Int32\n",
            "}\n",
            "p := POINT()\n",
            "p.x := 7\n",
            "p.y := 9\n",
            "FileAppend p.x \",\" p.y, \"*\"\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["7,9"]);
}

#[test]
fn a_struct_instance_has_an_address() {
    let (report, _) = run_in(
        concat!(
            "struct POINT {\n",
            "    x : Int32, y : Int32\n",
            "}\n",
            "p := POINT(10, 20)\n",
            "FileAppend (p.Ptr != 0), \"*\"\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn a_numeric_type_is_a_struct_of_one_value() {
    // Measured on the binary of v2.1-alpha.39: Int32(42) is a
    // boxed struct of size 4, and the class itself answers its size.
    let (report, _) = run_in(
        concat!(
            "n := Int32(42)\n",
            "FileAppend n.__Value \" size=\" n.Size \" \" Int32.Size, \"*\"\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["42 size=4 4"]);
}

#[test]
fn a_nested_struct_field_borrows_the_memory_of_the_outer() {
    // A field that is another struct answers a view into the memory of the
    // outer struct, so a write through the view lands in the outer.
    let (report, _) = run_in(
        concat!(
            "struct INNER {\n",
            "    a : Int32\n",
            "}\n",
            "struct OUTER {\n",
            "    inner : INNER\n",
            "    b : Int32\n",
            "}\n",
            "o := OUTER()\n",
            "o.inner.a := 5\n",
            "o.b := 7\n",
            "FileAppend o.inner.a \",\" o.b \" size=\" o.Size, \"*\"\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["5,7 size=8"]);
}

#[test]
fn a_struct_field_initializer_reads_the_size_of_the_struct() {
    // Measured on the binary of v2.1-alpha.39: `cbSize : UInt32 := this.Size`
    // initializes the field with the whole size of the struct.
    let (report, _) = run_in(
        concat!(
            "struct LVINSERTMARK {\n",
            "    cbSize : UInt32 := this.Size\n",
            "    dwFlags : UInt32\n",
            "    iItem : Int32\n",
            "}\n",
            "im := LVINSERTMARK()\n",
            "FileAppend im.cbSize \" size=\" im.Size, \"*\"\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["12 size=12"]);
}

#[test]
fn a_super_is_the_call_of_the_base() {
    // `super()` is `super.call()`.
    let (report, _) = run(concat!(
        "class Base {\n",
        "  Call(x) {\n",
        "    return \"called with \" . x\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  m() {\n",
        "    return super(\"the argument\")\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.m()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["called with the argument"]);
}

#[test]
fn a_super_in_a_static_method_asks_the_base() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  static m() {\n",
        "    return \"the base\"\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  static m() {\n",
        "    return super.m()\n",
        "  }\n",
        "}\n",
        "MsgBox Derived.m()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the base"]);
}

#[test]
fn a_base_that_has_not_the_method_refuses_with_the_name_of_the_class() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  m() {\n",
        "    return \"the base\"\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  n() {\n",
        "    return super.n()\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.n()\n",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Derived\" has no method named \"n\".")
    );
}

#[test]
fn a_class_without_a_base_refuses_with_its_own_name() {
    let (report, _) = run(concat!(
        "class Alone {\n",
        "  m() {\n",
        "    return super.m()\n",
        "  }\n",
        "}\n",
        "a := Alone()\n",
        "MsgBox a.m()\n",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Alone\" has no method named \"m\".")
    );
}

#[test]
fn a_base_that_has_not_the_property_refuses_with_the_name_of_the_class() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  m() {\n",
        "    return \"the base\"\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  n() {\n",
        "    return super.value\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.n()\n",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Derived\" has no property named \"value\".")
    );
}

#[test]
fn an_inherited_method_asks_the_base_of_the_class_that_defined_it() {
    let (report, _) = run(concat!(
        "class Base {\n",
        "  m() {\n",
        "    return super.n()\n",
        "  }\n",
        "  n() {\n",
        "    return \"the base\"\n",
        "  }\n",
        "}\n",
        "class Derived extends Base {\n",
        "  n() {\n",
        "    return \"the derived\"\n",
        "  }\n",
        "}\n",
        "d := Derived()\n",
        "MsgBox d.m()\n",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Derived\" has no method named \"n\".")
    );
}

#[test]
fn a_global_in_a_function_writes_the_global_of_the_script() {
    // Measured on the binary of v2.0.19.
    let (report, _) = run(concat!(
        "g := 0\n",
        "f() {\n",
        "  global g\n",
        "  g := 1\n",
        "  return g\n",
        "}\n",
        "MsgBox f() \", \" g\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1, 1"]);
}

#[test]
fn a_global_with_a_value_writes_the_global_of_the_script() {
    let (report, _) = run("global g := 1\nMsgBox g\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn a_local_in_a_function_is_local() {
    let (report, _) = run(concat!(
        "g := 1\n",
        "f() {\n",
        "  local l := 2\n",
        "  return l\n",
        "}\n",
        "MsgBox f() \", \" g\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2, 1"]);
}

#[test]
fn a_function_that_assumes_the_scope_of_the_script_writes_the_global() {
    // The bare `global` of the first line of a function is the mode of the
    // function.
    let (report, _) = run(concat!(
        "g := 0\n",
        "f() {\n",
        "  global\n",
        "  g := 1\n",
        "  return g\n",
        "}\n",
        "MsgBox f() \", \" g\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1, 1"]);
}

#[test]
fn a_local_in_a_function_that_assumes_the_scope_of_the_script_is_local() {
    let (report, _) = run(concat!(
        "g := 0\n",
        "f() {\n",
        "  global\n",
        "  local l := 2\n",
        "  g := 1\n",
        "  return l \" \" g\n",
        "}\n",
        "MsgBox f() \", \" g\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2 1, 1"]);
}

#[test]
fn a_declaration_inside_a_block_takes_effect_for_the_whole_function() {
    let (report, _) = run(concat!(
        "g := 0\n",
        "f() {\n",
        "  if 1 {\n",
        "    global g\n",
        "  }\n",
        "  g := 1\n",
        "  return g\n",
        "}\n",
        "MsgBox f() \", \" g\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1, 1"]);
}

#[test]
fn a_declaration_may_name_several_variables() {
    let (report, _) = run(concat!(
        "a := 0\n",
        "b := 0\n",
        "f() {\n",
        "  global a, b := 1\n",
        "  a := 2\n",
        "  return a \" \" b\n",
        "}\n",
        "MsgBox f() \", \" a \" \" b\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2 1, 2 1"]);
}

#[test]
fn the_call_of_the_call_method_of_a_value_names_that_method() {
    // Measured on the binary of v2.0.19: the failure of
    // the call of the `Call` method of a value carries the name of that
    // method in the `What`, and the name of the callee in the `Extra`.
    let (report, _desktop) = run(
        "try {\n    f := Abs\n    f.Call()\n} catch Any as e {\n    MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["Too few parameters passed to function. | Func.Prototype.Call | Abs"]
    );
}

#[test]
fn the_call_of_a_value_carries_no_name_of_a_method() {
    let (report, _desktop) = run(
        "try {\n    f := Abs\n    x := f()\n} catch Any as e {\n    MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["Too few parameters passed to function. |  | Abs"]
    );
}

#[test]
fn every_value_carries_its_class() {
    // Measured on the binary of v2.0.19: every value
    // carries its class, and the `__Class` of it is the name of it.
    let (report, _desktop) = run(
        "whole := 5\nfrac := 5.5\ntext := \"text\"\nlist := [1]\nMsgBox whole.__Class \" \" frac.__Class \" \" text.__Class \" \" list.__Class",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Integer Float String Array"]);
}

#[test]
fn the_class_of_an_object_is_the_class_of_it() {
    // Measured on the binary of v2.0.19: the name of a class
    // is a name of the script, and the variable may not have it.
    let (report, _desktop) = run(
        "class Thing {\n}\ninstance := Thing()\nm := Map()\nMsgBox instance.__Class \" \" m.__Class \" \" Abs.__Class",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Thing Map Func"]);
}

#[test]
fn the_class_of_a_class_is_a_class() {
    let (report, _desktop) = run("class Thing {\n}\nMsgBox Thing.__Class \" \" Map.__Class");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Class Class"]);
}

#[test]
fn the_class_of_a_prototype_is_the_class_it_is_the_prototype_of() {
    let (report, _desktop) =
        run("class Thing {\n}\nMsgBox Thing.Prototype.__Class \" \" Map.Prototype.__Class");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Thing Map"]);
}

#[test]
fn the_class_of_a_value_that_is_not_an_object_cannot_be_written() {
    // Measured on the binary of v2.0.19: the property is
    // read-only, and the name of it goes in the `Extra`.
    let (report, _desktop) = run(
        "try {\n    x := 5\n    x.__Class := \"Map\"\n} catch Any as e {\n    MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Property is read-only. |  | __Class"]);
}

#[test]
fn the_class_of_an_object_can_be_written_and_it_shadows_the_class() {
    let (report, _desktop) = run("m := Map()\nm.__Class := \"X\"\nMsgBox m.__Class");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["X"]);
}

#[test]
fn the_member_of_the_answer_of_a_call_is_read() {
    // Measured on the binary of v2.0.19: the answer of a
    // call is a value, and the members of it are read the way the members
    // of any other value are.
    let (report, _desktop) = run("x := StrLen(\"abc\").__Class\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Integer"]);
}

#[test]
fn the_item_of_the_answer_of_a_call_is_read() {
    let (report, _desktop) = run("m := Map()\nm.Set(\"a\", 1)\nMsgBox m.Get(\"a\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_assignment_of_a_call_is_refused() {
    // Measured on the binary of v2.0.19: the assignment of
    // an expression is refused at the load, and the text of it is named.
    let error = parse("StrLen(\"abc\") := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: := 1"
    );
}

#[test]
fn the_operator_that_wants_a_number_may_not_follow_a_literal_text() {
    // Measured on the binary of v2.0.19: the script does not
    // load, and the text that is named is the operator and the rest of the
    // expression that holds it.
    let error = parse("x := \"a\" + 0").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Unexpected operator following literal string.\n     Specifically: + 0"
    );
    assert_eq!(error.line, 1);
}

#[test]
fn the_text_that_is_named_is_the_operator_and_the_rest_of_the_expression() {
    // Measured on the binary of v2.0.19: the expression that
    // holds the operator is the one of the statement, and the group is not
    // the end of it.
    let error = parse("x := (\"a\" + 0) + 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Unexpected operator following literal string.\n     Specifically: + 0) + 1"
    );
}

#[test]
fn the_command_of_v2_carries_the_parenthesis_that_closes_it() {
    // Measured on the binary of v2.0.19: the command is a call
    // whose parentheses are not written, and the text that is named carries
    // the one that closes it.
    let error = parse("StrLen \"a\" + 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Unexpected operator following literal string.\n     Specifically: + 1)"
    );
}

#[test]
fn the_concatenation_of_two_literal_texts_is_a_literal_text() {
    // Measured on the binary of v2.0.19: the concatenation of
    // two literal texts is a literal text, and the concatenation of a text
    // and a number is not.
    let error = parse("x := \"a\" \"b\" + 0").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Unexpected operator following literal string.\n     Specifically: + 0"
    );
}

#[test]
fn a_group_is_not_a_literal_text() {
    // Measured on the binary of v2.0.19: the value of a group
    // is not a constant, and v2 refuses it at the run and not at the load.
    let (report, _desktop) = run("x := (\"a\") + 0");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a Number but got a String.")
    );
}

#[test]
fn the_body_of_a_block_may_be_one_statement() {
    // Measured on the binary of v2.0.19: the body of a block
    // may be one statement, and the braces may be absent.
    let (report, _desktop) = run("if (1)\n    x := 1\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_body_of_a_block_without_braces_is_one_statement() {
    // Measured on the binary of v2.0.19: the body is the one
    // statement that follows, and the rest of the line is outside of it.
    let (report, _desktop) = run("if (0)\n    MsgBox \"a\"\nMsgBox \"b\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["b"]);
}

#[test]
fn the_body_of_an_if_may_not_be_on_the_line_of_the_if() {
    // Measured on the binary of v2.0.19: the `if` names its
    // own word.
    let error = parse("if (1) x := 1").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"If\"");
}

#[test]
fn the_body_of_an_else_may_be_on_the_line_of_the_else() {
    // Measured on the binary of v2.0.19: the `else` takes the
    // body on its line and on the next one.
    let (report, _desktop) = run("if (0)\n    x := 1\nelse x := 2\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn a_block_that_has_no_body_is_refused() {
    // Measured on the binary of v2.0.19: the `if` needs a
    // body, and it names its own word.
    let error = parse("if (1)").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"If\"");
}

#[test]
fn the_condition_of_an_if_is_not_there() {
    // Measured on the binary of v2.0.19: the `if` needs a
    // condition, and it says so in its own words.
    let error = parse("if\n    x := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"If\" requires at least 1 parameter.\n     Specifically: if"
    );
}

#[test]
fn the_class_needs_its_braces() {
    // Measured on the binary of v2.0.19: the class names the
    // line of it.
    let error = parse("class Thing\n    x := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"{\"\n     Specifically: class Thing"
    );
}

#[test]
fn a_method_needs_its_braces() {
    // Measured on the binary of v2.0.19: the method names the
    // line of it.
    let error = parse("class Thing {\n    M()\n        x := 1\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Not a valid method, class or property definition.\n     Specifically: M()"
    );
}

#[test]
fn the_base_of_an_object_is_another_object_and_not_a_name() {
    // Measured on the binary of v2.0.19: the base is an
    // object, and a name is refused with the reason.
    let (report, _desktop) = run("o := {}\nMsgBox HasBase(o, \"Object\")");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected an object but got a String.")
    );
}

#[test]
fn the_base_of_an_object_is_the_prototype_of_its_class() {
    // Measured on the binary of v2.0.19: the question is
    // whether the base of the object is the other object, and the chain of
    // the bases is walked.
    let (report, _desktop) = run(
        "m := Map()\nMsgBox HasBase(m, Map.Prototype) \" \" HasBase(m, Object.Prototype) \" \" HasBase(m, Array.Prototype)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1 1 0"]);
}

#[test]
fn a_value_that_is_not_an_object_has_no_base() {
    let (report, _desktop) = run("MsgBox HasBase(5, Object.Prototype)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0"]);
}

#[test]
fn a_class_has_no_base_of_itself() {
    let (report, _desktop) = run("MsgBox HasBase(Map, Map.Prototype)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0"]);
}

#[test]
fn the_methods_of_the_base_object_are_the_methods_of_every_object() {
    // Measured on the binary of v2.0.19: the methods of the
    // base object are inherited.
    let (report, _desktop) =
        run("o := {}\nMsgBox HasMethod(o, \"OwnProps\") \" \" HasMethod(o, \"Nothing\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1 0"]);
}

#[test]
fn the_question_with_no_name_asks_whether_the_value_is_a_class() {
    // Measured on the binary of v2.0.19: a class has the
    // methods of the base object, and an instance has the methods of its
    // class.
    let (report, _desktop) =
        run("m := Map()\nMsgBox HasMethod(Map) \" \" HasMethod(m) \" \" HasMethod({})");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1 0 0"]);
}

#[test]
fn the_name_of_a_class_is_not_the_name_of_a_variable() {
    // Measured on the binary of v2.0.19: the name of a class
    // is a name of the script, and it may not be the output variable of an
    // assignment. The refusal is at the load.
    let error = parse("class Foo {\n}\nfoo := 5").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This Class cannot be used as an output variable.\n     Specifically: Foo"
    );
}

#[test]
fn the_name_of_a_class_is_not_written_by_a_compound_assignment() {
    // Measured on the binary of v2.0.19: the compound
    // assignment says that the class cannot be assigned a value.
    let error = parse("class Foo {\n}\nfoo .= \"x\"").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This Class cannot be assigned a value.\n     Specifically: Foo"
    );
}

#[test]
fn the_name_of_a_class_of_the_runtime_is_not_the_name_of_a_variable() {
    let error = parse("Map := 5").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This Class cannot be used as an output variable.\n     Specifically: Map"
    );
}

#[test]
fn a_class_may_not_be_named_like_a_function() {
    // Measured on the binary of v2.0.19: a name may be
    // declared only once, and the message names the kind of the one that is
    // already there.
    let error = parse("f() {\n}\nclass f {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This class declaration conflicts with an existing Func.\n     Specifically: f"
    );
}

#[test]
fn a_function_may_not_be_named_like_a_class() {
    let error = parse("class f {\n}\nf() {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This function declaration conflicts with an existing Class.\n     Specifically: f"
    );
}

#[test]
fn a_name_may_be_declared_only_once() {
    let error = parse("class f {\n}\nclass f {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This class declaration conflicts with an existing Class.\n     Specifically: f"
    );
}

#[test]
fn a_local_that_is_declared_is_not_the_global() {
    // Measured on the binary of v2.0.19: `local g` and a
    // global `g` are two names, and the local is the one that the function
    // reads.
    let (report, _desktop) = run("g := \"G\"\nf() {\n    local g\n    return g\n}\nf()");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This local variable has not been assigned a value.")
    );
}

#[test]
fn a_local_that_is_declared_may_hold_its_own_value() {
    let (report, _desktop) = run(concat!(
        "g := \"G\"\n",
        "f() {\n    local g\n    g := \"L\"\n    return g\n}\n",
        "MsgBox f()",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["L"]);
}

#[test]
fn a_local_may_not_hold_the_class_of_its_name() {
    // Measured on the binary of v2.0.19: the name of a class
    // is a name of the script, and a local that has that name may not hold
    // the class or an instance of it.
    let (report, _desktop) = run("class Foo {\n}\nf() {\n    foo := Foo()\n    return foo\n}\nf()");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some(
            "This local variable has not been assigned a value. A global declaration inside the function may be required."
        )
    );
}

#[test]
fn a_local_that_is_declared_may_not_hold_the_class_of_its_name_either() {
    // Measured on the binary of v2.0.19: a local that is
    // declared says the same words without the hint.
    let (report, _desktop) =
        run("class Foo {\n}\nf() {\n    local foo\n    foo := Foo()\n    return foo\n}\nf()");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This local variable has not been assigned a value.")
    );
}

#[test]
fn every_object_has_the_property_of_the_class_of_it() {
    let (report, _desktop) =
        run("m := Map()\nMsgBox m.HasProp(\"__Class\") \" \" m.HasOwnProp(\"__Class\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1 0"]);
}

#[test]
fn the_concat_of_a_variable_that_has_no_value_is_an_empty_text() {
    // Measured on the binary of v2.0.19: the concat reads a
    // variable that has no value as an empty text, and the write makes it.
    let (report, _desktop) = run("s .= \"x\"\nMsgBox s");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["x"]);
}

#[test]
fn the_concat_of_a_local_that_has_no_value_is_an_empty_text() {
    // Measured on the binary of v2.0.19: the same is true of
    // a local of a function.
    let (report, _desktop) = run("f() {\n    s .= \"x\"\n    return s\n}\nMsgBox f()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["x"]);
}

#[test]
fn the_add_of_a_variable_that_has_no_value_is_an_error() {
    // Measured on the binary of v2.0.19: the concat is the
    // one operator that reads a variable that has no value as an empty
    // text. The others want a value that is there.
    let (report, _desktop) = run("n += 2");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This global variable has not been assigned a value.")
    );
}

#[test]
fn the_compound_assignment_of_a_property_that_is_not_there_is_an_error() {
    // Measured on the binary of v2.0.19: the compound
    // operators read the property before they write it, and a property that
    // is not there is an error that names it.
    let (report, _desktop) = run("o := {}\no.x .= \"y\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Object\" has no property named \"x\".")
    );
}

#[test]
fn the_compound_assignment_of_a_property_that_is_there_is_written() {
    let (report, _desktop) = run("o := {}\no.x := \"a\"\no.x .= \"b\"\nMsgBox o.x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ab"]);
}

#[test]
fn a_member_of_a_class_that_is_not_a_definition_names_the_line() {
    // Measured on the binary of v2.0.19: the member that is
    // not a definition is refused, and the line is named.
    let error = parse("class Thing {\n    x .= \"y\"\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Not a valid method, class or property definition.\n     Specifically: x .= \"y\""
    );
}

#[test]
fn a_member_of_a_class_that_is_not_a_name_names_the_line() {
    // Measured on the binary of v2.0.19: a member that is not
    // a name is not a valid definition either.
    let error = parse("class Thing {\n    1 + 2\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Not a valid method, class or property definition.\n     Specifically: 1 + 2"
    );
}

#[test]
fn a_member_of_a_class_that_is_a_name_alone_is_a_line_with_no_action() {
    // Measured on the binary of v2.0.19: a member that is a
    // name and nothing else is a line that has no action in it.
    let error = parse("class Thing {\n    x\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: x"
    );
}

#[test]
fn a_goto_goes_to_the_label_and_the_run_goes_on() {
    // Measured on the binary of v2.0.19: the `Goto` jumps to
    // the label and continues the run.
    let (report, _desktop) =
        run("if (true) {\n    Goto later\n    MsgBox \"skipped\"\n}\nlater:\nMsgBox \"done\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["done"]);
}

#[test]
fn a_goto_that_leaves_a_block_goes_on_outside_it() {
    let (report, _desktop) = run("if (true) {\n    Goto later\n}\nlater:\nMsgBox \"done\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["done"]);
}

#[test]
fn the_name_of_a_label_does_not_know_its_case() {
    let (report, _desktop) = run("if (true) {\n    Goto DONE\n}\ndone:\nMsgBox \"done\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["done"]);
}

#[test]
fn a_goto_to_a_label_that_is_not_there_fails_with_the_reason() {
    // Measured on the binary of v2.0.19: the load refuses the
    // goto whose label is not in the scope, and it names the label.
    let error = parse("if (true) {\n    Goto nowhere\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Label not found in current scope.\n     Specifically: nowhere"
    );
    assert_eq!(error.line, 2);
}

#[test]
fn a_goto_into_a_block_that_does_not_enclose_it_fails_with_the_reason() {
    // Measured on the binary of v2.0.19: the jump may go to
    // a label of the block it is in, or of a block around it, and to no
    // other.
    let (report, _desktop) = run("if (true) {\n    Goto inner\n}\nif (true) {\n    inner:\n}");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("A Goto must not jump into a block that doesn't enclose it.")
    );
}

#[test]
fn the_failure_of_a_goto_is_not_taken_by_a_catch() {
    // Measured on the binary of v2.0.19: the goto whose label
    // is not there is refused at the load, and a catch does not take it.
    let error = parse("try {\n    Goto nowhere\n} catch Any as e {\n    MsgBox \"caught\"\n}")
        .expect_err("does not parse");
    assert_eq!(
        error.message,
        "Label not found in current scope.\n     Specifically: nowhere"
    );
}

#[test]
fn a_label_of_a_function_is_not_a_label_of_the_script() {
    // Measured on the binary of v2.0.19: the label of a
    // function is not the label of the script, and the load says so.
    let error = parse("f() {\n    Goto inner\n}\nf()\ninner:\nMsgBox \"done\"")
        .expect_err("does not parse");
    assert_eq!(
        error.message,
        "Label not found in current scope.\n     Specifically: inner"
    );
    assert_eq!(error.line, 2);
}

#[test]
fn is_label_answers_whether_the_label_is_there() {
    // Measured on the binary of v2.0.19: the label is asked
    // in the scope that runs, and the answer is one or zero.
    let (report, _desktop) = run("later:\nMsgBox IsLabel(\"later\") \" \" IsLabel(\"nope\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1 0"]);
}

#[test]
fn is_label_asks_the_scope_that_runs() {
    let (report, _desktop) = run("f() {\n    MsgBox IsLabel(\"later\")\n}\nf()\nlater:");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0"]);
}

#[test]
fn is_label_of_an_empty_name_answers_zero() {
    let (report, _desktop) = run("MsgBox IsLabel(\"\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0"]);
}

#[test]
fn a_line_that_ends_with_an_operator_continues_on_the_next_one() {
    // Measured on the binary of v2.0.19: the line that ends
    // with an operator continues on the next one.
    let (report, _desktop) = run("x :=\n1\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_empty_lines_of_a_continuation_are_not_a_part_of_it() {
    // Measured on the binary of v2.0.19: the empty lines
    // between the two are not a part of the continuation.
    let (report, _desktop) = run("x :=\n\n1\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn a_line_that_begins_with_an_operator_continues_the_one_before_it() {
    // Measured on the binary of v2.0.19: the line that begins
    // with an operator continues the line before it.
    let (report, _desktop) = run("x := 1\n+ 2\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
}

#[test]
fn the_group_may_be_written_in_more_than_one_line() {
    // Measured on the binary of v2.0.19: the group may be
    // written in more than one line.
    let (report, _desktop) = run("x := (1\n)\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_array_may_be_written_in_more_than_one_line() {
    let (report, _desktop) = run("x := [1\n, 2]\nMsgBox x.Length");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_arguments_of_a_call_may_be_written_in_more_than_one_line() {
    let (report, _desktop) = run("x := StrLen(\n\"ab\")\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_operand_that_is_not_there_is_refused() {
    // Measured on the binary of v2.0.19: the operator that
    // wants the operand names the failure.
    let error = parse("x :=").expect_err("does not parse");
    assert_eq!(error.message, "Missing operand.\n     Specifically: :=");
}

#[test]
fn the_operand_that_is_not_there_names_the_statement() {
    // Measured on the binary of v2.0.19: the brace that
    // closes a block is not an operand that is missing, and the statement
    // it is in is named with it.
    let error = parse("if (x) {\ny :=\n}").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"}\"\n     Specifically: y := }");
    assert_eq!(error.line, 2);
}

#[test]
fn an_operator_that_may_not_begin_a_value_is_refused() {
    let error = parse("x := *").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: *");
}

#[test]
fn an_operator_that_may_not_begin_a_value_names_the_rest_of_the_line() {
    // Measured on the binary of v2.0.19: the text that the
    // refusal names is the rest of the line.
    let error = parse("x := * 2").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: * 2");
}

#[test]
fn the_operator_that_wanted_the_operand_names_the_failure() {
    // Measured on the binary of v2.0.19: the operator that
    // wanted the operand names the failure, and the rest of the line is
    // named with it.
    let error = parse("x := 1 + * 2").expect_err("does not parse");
    assert_eq!(error.message, "Missing operand.\n     Specifically: + * 2");
    let error = parse("x := 1 +").expect_err("does not parse");
    assert_eq!(error.message, "Missing operand.\n     Specifically: +");
}

#[test]
fn the_operator_that_is_the_last_item_of_a_list_names_no_text() {
    // Measured on the binary of v2.0.19: the text of the
    // refusal is not there when the operator is the last item of the
    // arguments of a call or of the items of an array. The group is not a
    // list, and the text is there.
    let error = parse("x := f(*)").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.");
    let error = parse("x := [*]").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.");
    let error = parse("x := (*)").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: *)");
}

#[test]
fn the_dot_that_reaches_into_nothing_names_the_rest_of_the_line() {
    // Measured on the binary of v2.0.19: the dot that does not
    // reach into a value is a "Syntax error." and the rest of the line, from
    // the dot.
    let error = parse("x := a. b").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: . b");
    let error = parse("x := a.").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: .");
}

#[test]
fn a_brace_that_closes_a_block_is_not_an_operand() {
    let error = parse("x := )").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \")\"\n     Specifically: x := )");
}

#[test]
fn the_colon_of_the_ternary_needs_its_question() {
    let error = parse("x := :").expect_err("does not parse");
    assert_eq!(error.message, "A \":\" is missing its \"?\"");
}

#[test]
fn the_section_joins_the_line_that_has_code_above_it() {
    // Measured on the binary of v2.0.19: a section begins with
    // a parenthesis at the start of a line, and it continues the last line
    // that has code. A name that ends that line is separated from the
    // contents of the section by a space.
    let (report, _desktop) = run("x := 1\n(\n2\n)\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["12"]);
}

#[test]
fn the_section_joins_the_line_above_the_comments() {
    // Measured on the binary of v2.0.19: the blank lines and
    // the comments between the line and the section are of no account.
    let (report, _desktop) = run("x := 1\n; a comment\n\n(\n2\n)\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["12"]);
}

#[test]
fn the_section_that_is_inside_a_text_is_text() {
    // Measured on the binary of v2.0.19: the section that
    // begins inside a text keeps the quotes as literal characters, and the
    // newlines of the section are kept.
    let (report, _desktop) = run("x := \"\n(\na\nb\n)\"\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a\nb"]);
}

#[test]
fn the_lines_of_a_section_are_spaces_in_an_expression() {
    // Measured on the binary of v2.0.19: the newlines of a
    // section are spaces in an expression.
    let (report, _desktop) = run("x := 1\n(\n2\n3\n)\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["123"]);
}

#[test]
fn the_join_option_puts_the_lines_together() {
    // Measured on the binary of v2.0.19: the option `Join`
    // connects the lines without anything between them.
    let (report, _desktop) = run("x := \"\n( Join\na\nb\n)\"\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ab"]);
}

#[test]
fn the_ltrim_option_takes_the_indentation_away() {
    // Measured on the binary of v2.0.19: the option `LTrim`
    // removes the leading whitespace of every line.
    let (report, _desktop) = run("x := \"\n( LTrim\n  a\n  b\n)\"\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a\nb"]);
}

#[test]
fn the_indentation_of_the_first_line_goes_from_the_lines() {
    // Measured on the binary of v2.0.19: the leading
    // whitespace of the lines follows the indentation of the first one, and
    // a line that is indented less is left as it is.
    let (report, _desktop) = run("x := \"\n(\n  a\n b\n    c\n)\"\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a\n b\n  c"]);
}

#[test]
fn the_comments_option_takes_the_comments_away() {
    // Measured on the binary of v2.0.19: the option
    // `Comments` removes the comments of the lines.
    let (report, _desktop) = run("x := \"\n( Comments\na ; a comment\nb\n)\"\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a\nb"]);
}

#[test]
fn the_option_that_v2_does_not_know_is_refused() {
    // Measured on the binary of v2.0.19: the option that v2
    // does not know is named, and the script does not load.
    let error = parse("x := \"\n( Nope\na\n)\"").expect_err("does not parse");
    assert_eq!(error.message, "Invalid option.\n     Specifically: Nope");
    assert_eq!(error.line, 2);
}

#[test]
fn the_section_that_is_not_closed_is_refused() {
    // Measured on the binary of v2.0.19: the section that is
    // not closed is named, and the text it merged with the line above is
    // named with it.
    let error = parse("x := 1\n(\n2\n").expect_err("does not parse");
    assert_eq!(error.message, "Missing \")\"\n     Specifically: x := 1 2");
}

#[test]
fn a_line_that_begins_with_a_parenthesis_is_an_expression() {
    // Measured on the binary of v2.0.19: a line that begins
    // with a parenthesis is an expression, and the value it leaves behind is
    // not used.
    let (report, _desktop) = run("(2 + 3)\nMsgBox \"done\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["done"]);
}

#[test]
fn the_literal_at_the_start_of_a_statement_holds_no_action() {
    // Measured on the binary of v2.0.19: a literal at the
    // start of a statement is a line that holds no action, and the line is
    // named with it.
    let error = parse("2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: 2"
    );
    let error = parse("1 + 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: 1 + 2"
    );
}

#[test]
fn the_value_that_came_too_close_names_the_rest_of_the_line() {
    // Measured on the binary of v2.0.19: the value that came
    // too close is named, and the rest of the line is named with it.
    let error = parse("x := \"a\"\"b\"\"c\"").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing space or operator before this.\n     Specifically: \"b\"\"c\""
    );
}

#[test]
fn the_command_carries_the_parenthesis_that_closes_it() {
    // Measured on the binary of v2.0.19: the command of v2 is
    // a call whose parentheses are not written, and the text that names the
    // operator carries the one that closes it.
    let error = parse("MsgBox \"x\"2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing space or operator before this.\n     Specifically: 2)"
    );
}

#[test]
fn the_bracket_that_is_not_there_names_the_statement() {
    // Measured on the binary of v2.0.19: the bracket that is
    // not there is named with the statement that holds it, and the lines of
    // the statement are written in one.
    let error = parse("FileAppend \"[\" (\nabc").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \")\"\n     Specifically: FileAppend \"[\" ( abc"
    );
}

#[test]
fn the_escape_of_a_space_is_a_space() {
    // Measured on the binary of v2.0.19: `s is a space, and
    // it is allowed wherever `t is.
    let (report, _desktop) = run("MsgBox \"a`sb\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a b"]);
}

#[test]
fn the_increment_may_be_a_statement() {
    // Measured on the binary of v2.0.19: the increment may
    // be a statement of its own, and it writes to the value that is before
    // it.
    let (report, _desktop) = run("x := 1\nx++\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    let (report, _desktop) = run("x := 1\n++x\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_increment_writes_to_a_property_and_to_an_item() {
    // Measured on the binary of v2.0.19: the increment of a
    // property and of an item writes to them.
    let (report, _desktop) = run("x := {a: 1}\nx.a++\nMsgBox x.a");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    let (report, _desktop) = run("x := [1]\nx[1]++\nMsgBox x[1]");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    let (report, _desktop) = run("x := Map()\nx[\"a\"] := 1\nx[\"a\"]++\nMsgBox x[\"a\"]");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_postfix_answers_the_value_of_before() {
    // Measured on the binary of v2.0.19: the postfix
    // answers the value that was there, and the prefix answers the one that
    // is there.
    let (report, _desktop) = run("x := 1\ny := x++\nMsgBox y\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1", "2"]);
    let (report, _desktop) = run("x := 1\ny := ++x\nMsgBox y\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2", "2"]);
}

#[test]
fn the_increment_of_a_value_that_cannot_be_written_is_refused() {
    // Measured on the binary of v2.0.19: the increment of a
    // value that cannot be written is refused at the load, and the source
    // of the operator is named with it.
    let error = parse("x := StrLen(\"ab\")++").expect_err("does not parse");
    assert_eq!(error.message, "Invalid assignment.\n     Specifically: ++");
    let error = parse("x := ++2").expect_err("does not parse");
    assert_eq!(error.message, "Invalid assignment.\n     Specifically: ++2");
    let error = parse("++2").expect_err("does not parse");
    assert_eq!(error.message, "Invalid assignment.\n     Specifically: ++2");
}

#[test]
fn the_number_at_the_left_of_an_assignment_is_refused_at_the_parse() {
    // Measured on the binary of v2.0.19: the number that is
    // the left side of an assignment is refused at the parse, and the text
    // it names is the number.
    let error = parse("1 := 2").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: 1");
    let error = parse("1e3 := 2").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: 1e3");
    let error = parse("0x10 := 2").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: 0x10");
}

#[test]
fn the_number_that_carries_a_point_is_refused_by_the_assignment() {
    // Measured on the binary of v2.0.19: the number that
    // carries a point is not refused at the parse: the assignment is the one
    // that is refused, and the text it names is the operator and the value.
    let error = parse("1.5 := 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: := 2"
    );
    let error = parse("2.0 := 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: := 2"
    );
    let error = parse("1.5 := 2 + 3").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: := 2 + 3"
    );
}

#[test]
fn the_compound_assignment_refuses_the_number() {
    // Measured on the binary of v2.0.19: the compound
    // operators refuse the number that is their left side, and the text
    // they name is the operator and the value.
    let error = parse("1 += 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: += 2"
    );
    let error = parse("1e3 -= 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: -= 2"
    );
    let error = parse("1.5 *= 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: *= 2"
    );
}

#[test]
fn the_number_with_a_point_at_its_end_holds_no_action() {
    // Measured on the binary of v2.0.19: the point at the end
    // of the number is the dot of a member that is not there, and the line
    // holds no action.
    let error = parse("1. := 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: 1. := 2"
    );
}

#[test]
fn the_array_at_the_start_of_a_statement_holds_no_action() {
    // Measured on the binary of v2.0.19: the array at the
    // start of a statement is a line that holds no action, and the line is
    // named with it.
    let error = parse("[1] := 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: [1] := 2"
    );
    let error = parse("[1]").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: [1]"
    );
}

#[test]
fn a_static_keeps_its_value_between_the_calls() {
    // Measured on the binary of v2.0.19: the static of a
    // function is the same one in every call of it.
    let (report, _desktop) =
        run("F() {\nstatic count := 0\ncount++\nreturn count\n}\nMsgBox F()\nMsgBox F()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1", "2"]);
}

#[test]
fn the_value_of_a_static_declaration_runs_once() {
    // Measured on the binary of v2.0.19: the value of the
    // declaration runs the first time that declaration is reached.
    let (report, _desktop) =
        run("F() {\nstatic count := 5\ncount++\nreturn count\n}\nMsgBox F()\nMsgBox F()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["6", "7"]);
    // The declaration that the function does not reach on the first call
    // runs its value on the call that reaches it.
    let (report, _desktop) = run(
        "F(n) {\nif n = 0\nreturn \"none\"\nstatic count := 5\nreturn count\n}\nMsgBox F(0)\nMsgBox F(1)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["none", "5"]);
}

#[test]
fn a_static_belongs_to_the_function_that_declares_it() {
    // Measured on the binary of v2.0.19: two functions have
    // two statics, and the static of a recursion is the same one in every
    // turn of it.
    let (report, _desktop) = run(
        "F() {\nstatic count := 1\ncount++\nreturn count\n}\nG() {\nstatic count := 10\ncount++\nreturn count\n}\nMsgBox F()\nMsgBox F()\nMsgBox G()\nMsgBox G()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2", "3", "11", "12"]);
    let (report, _desktop) = run(
        "F(n) {\nstatic count := 0\ncount++\nif n = 0\nreturn count\nreturn F(n - 1)\n}\nMsgBox F(2)",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
}

#[test]
fn a_static_shadows_a_global_inside_the_function() {
    // Measured on the binary of v2.0.19: the static of a
    // function is not the global of the same name.
    let (report, _desktop) = run(
        "global count := 1\nF() {\nstatic count := 2\nreturn count\n}\nMsgBox F()\nMsgBox count",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2", "1"]);
}

#[test]
fn a_static_of_a_method_is_the_same_one_in_every_call() {
    // Measured on the binary of v2.0.19: the static of a
    // method belongs to the method, and not to the instance.
    let (report, _desktop) = run(
        "class C {\nCount() {\nstatic n := 0\nn++\nreturn n\n}\n}\nMsgBox C().Count()\nMsgBox C().Count()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1", "2"]);
}

#[test]
fn a_static_without_a_value_is_a_declaration() {
    // Measured on the binary of v2.0.19: the `static` with
    // no value declares the name, and the assignment that follows writes
    // it.
    let (report, _desktop) = run("F() {\nstatic count\ncount := 1\nreturn count\n}\nMsgBox F()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    // The bare `static` inside a function is accepted.
    let (report, _desktop) = run("F() {\nstatic\nreturn 1\n}\nMsgBox F()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn a_static_outside_a_function_is_refused() {
    // Measured on the binary of v2.0.19: the `static` at
    // the top level has no function to live in, and the whole declaration
    // is named with it.
    let error = parse("static count := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Unexpected declaration.\n     Specifically: static count := 1"
    );
}

#[test]
fn a_static_that_fights_with_a_parameter_is_refused() {
    // Measured on the binary of v2.0.19: the `static` may
    // not name a parameter of the function.
    let error = parse("F(count) {\nstatic count := 1\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This static variable declaration conflicts with an existing parameter.\n     Specifically: count"
    );
}

#[test]
fn a_number_may_begin_with_a_point() {
    // Measured on the binary of v2.0.19: the numbers of v2
    // may begin with a point, and `.5` is the number 0.5.
    let (report, _desktop) = run("x := .5\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0.5"]);
    let (report, _desktop) = run("x := .5e2\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["50.0"]);
    let (report, _desktop) = run("x := .5 + 1\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1.5"]);
    let (report, _desktop) = run("x := (.5)\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0.5"]);
    let (report, _desktop) = run("x := [.5, 1]\nMsgBox x[1]");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0.5"]);
    let (report, _desktop) = run("x := StrLen(.5)\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
    let (report, _desktop) = run("x := {a: .5}\nMsgBox x.a");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0.5"]);
}

#[test]
fn the_point_of_a_member_is_not_the_point_of_a_number() {
    // Measured on the binary of v2.0.19: the point that
    // follows a value is the dot of the member, and the number that begins
    // with a point is a number.
    let (report, _desktop) = run("x := 1.5\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1.5"]);
    let (report, _desktop) = run("x := {5: 1}\nMsgBox x.5");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_name_of_a_property_may_be_a_whole_number() {
    // Measured on the binary of v2.0.19: the name of a
    // property may be a whole number, and the number with a point is not a
    // name.
    let (report, _desktop) = run("x := {5: 1}\nMsgBox x.5");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    let (report, _desktop) = run("x := {0x10: 1}\nMsgBox x.0x10");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    let error = parse("x := {1.5: 1}").expect_err("does not parse");
    assert_eq!(error.message, "Invalid property name in object literal.");
}

#[test]
fn a_statement_that_begins_with_an_operator_is_a_line_that_holds_no_action() {
    // Measured on the binary of v2.0.19: a statement that
    // begins with an operator is a line that holds no action, and the text
    // that is named is the line itself.
    let error = parse("-1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: -1"
    );
    let error = parse("*x").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: *x"
    );
    let error = parse("&x").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: &x"
    );
    let error = parse("== 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: == 1"
    );
    // The statement that carries an assignment after the operator is
    // refused with the whole line.
    let error = parse("-x := 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: -x := 2"
    );
    let error = parse("!x := 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: !x := 2"
    );
}

#[test]
fn the_operators_that_want_a_word_of_their_own_are_named_apart() {
    // Measured on the binary of v2.0.19: the equal wants the
    // operator of an assignment, the assignment wants a name on its left,
    // the comma separates the parameters of a call, the question asks a
    // question that is not there, and the double colon is the separator of
    // a hotkey.
    let error = parse("= 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Syntax error. Did you mean to use \":=\"?\n     Specifically: = 1"
    );
    let error = parse(":= 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\":=\" requires that parameter #1 be non-blank."
    );
    let error = parse("+= 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: += 1"
    );
    let error = parse(", 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Function calls require a space or \"(\".  Use comma only between parameters.\n     Specifically: , 1"
    );
    let error = parse("? 1").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: ? 1");
    let error = parse(":: 1").expect_err("does not parse");
    assert_eq!(error.message, "Invalid hotkey.\n     Specifically: :: 1");
}

#[test]
fn the_dot_is_the_member_of_a_value_that_is_not_there() {
    // Measured on the binary of v2.0.19: the dot with a name
    // after it is the member of a value that is not there, and the v2 names
    // the call that it wanted to make.
    let error = parse(".x").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: .x()");
    let error = parse(".x 1").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: .x(1)");
    let error = parse(".x := 2").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: .x := 2");
    // The dot alone is a line that holds no action.
    let error = parse(".").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: ."
    );
    // The dot in the place of an operand names the rest of the line.
    let error = parse("x := 1 + .").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: .");
    let error = parse("f(.)").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: .)");
}

#[test]
fn the_line_that_begins_with_an_operator_continues_the_one_before_it() {
    // Measured on the binary of v2.0.19: the line that begins
    // with an operator continues the one before it, and the variable is
    // read before it is assigned.
    let (report, _desktop) = run("x := 1\n-x");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This global variable has not been assigned a value.")
    );
    // The assignment that the continuation makes is the one of the
    // operator: `x := 5 - (x := 2)` leaves x with 3.
    let (report, _desktop) = run("x := 5\n-x := 2\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
}

#[test]
fn a_loop_of_a_kind_without_its_parameter_is_refused_at_the_load() {
    // Measured on the binary of v2.0.19: the command of a
    // kind checks its parameters when the script loads, and the parameter
    // that is not there is named.
    let error = parse("Loop Read {\n  x := 1\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 required\n     Specifically: Loop Read "
    );
    let error = parse("Loop Parse {\n  x := 1\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 required\n     Specifically: Loop Parse "
    );
    let error = parse("Loop Files {\n  x := 1\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 required\n     Specifically: Loop Files "
    );
    let error = parse("Loop Reg {\n  x := 1\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 required\n     Specifically: Loop Reg "
    );
    // The name of the kind is the one that the script wrote.
    let error = parse("Loop read {\n  x := 1\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 required\n     Specifically: Loop read "
    );
}

#[test]
fn the_loop_of_a_kind_names_itself_when_it_is_not_there() {
    // Measured on the binary of v2.0.19: the list of the
    // parameters that is empty says so, and the statement that is not
    // complete names itself.
    let error = parse("Loop Read,").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"Loop Read\" requires at least 1 parameter.\n     Specifically: Loop Read,"
    );
    let error = parse("Loop Read, \"a\"").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Loop Read\"");
    let error = parse("Loop Parse, \"a\"").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Loop Parse\"");
    // The word that follows the `Loop` is the count of the loop when the
    // line ends there, and the loop of a count is named "Loop".
    let error = parse("Loop Read").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Loop\"");
    let error = parse("Loop Parse").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Loop\"");
    let error = parse("Loop Files").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Loop\"");
}

#[test]
fn the_loop_of_a_kind_takes_the_parameters_that_it_takes() {
    // Measured on the binary of v2.0.19: `Loop Read`,
    // `Loop Files` and `Loop Reg` take two parameters, and `Loop Parse`
    // takes three. The one that is written too many is named with the
    // first of them.
    let error = parse("Loop Read, \"a\", \"b\", \"c\" {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"Loop Read\" accepts at most 2 parameters.\n     Specifically: Loop Read, \"a\""
    );
    let error = parse("Loop Parse, \"a\", \"b\", \"c\", \"d\" {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"Loop Parse\" accepts at most 3 parameters.\n     Specifically: Loop Parse, \"a\""
    );
    let error = parse("Loop Files, \"a\", \"b\", \"c\" {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"Loop Files\" accepts at most 2 parameters.\n     Specifically: Loop Files, \"a\""
    );
    let error = parse("Loop Reg, \"a\", \"b\", \"c\" {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"Loop Reg\" accepts at most 2 parameters.\n     Specifically: Loop Reg, \"a\""
    );
}

#[test]
fn the_count_of_the_loop_is_not_a_name_of_a_kind() {
    // Measured on the binary of v2.0.19: a comma after the
    // count says that the type of the loop is not a valid one.
    let error = parse("Loop 5, 6 {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid loop type.\n     Specifically: Loop 5, 6 {"
    );
    let error = parse("Loop Foo, 5 {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid loop type.\n     Specifically: Loop Foo, 5 {"
    );
    // The comma of a call is not there.
    let error = parse("Loop,").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Function calls require a space or \"(\".  Use comma only between parameters.\n     Specifically: Loop,"
    );
}

#[test]
fn a_loop_without_a_count_goes_on_until_a_break() {
    // Measured on the binary of v2.0.19: the loop without
    // a count goes on until a break.
    let (report, _desktop) =
        run("i := 0\nLoop {\n  i := i + 1\n  If i = 3 {\n    Break\n  }\n}\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
}

#[test]
fn a_loop_that_runs_away_is_stopped_by_the_guard_rail() {
    // The loop of v2 has no bound of its own, and the tree has one: the
    // count of the statements.
    let (report, _desktop) = run("Loop {\n}");
    assert!(!report.ok);
    assert!(
        report
            .error
            .as_deref()
            .unwrap_or("")
            .contains("ran more than"),
        "{:?}",
        report.error
    );
    assert_eq!(report.error_line, Some(1));
}

// -- the `until` of the loop of v2 -------------------------------------------
//
// Measured on the binary of v2.0.19, with the probes under
// `egm-review/keysharp-audit/wp1/`: the `until` follows the body of the
// loop -- on the line of the closing brace or on the line that comes next,
// with blank lines and comments between them -- and the condition is read
// after every turn of the body. The loop ends when the condition is true,
// and the count of the loop is the other end of it.

#[test]
fn the_until_of_the_loop_ends_it_after_the_body() {
    // `accept_basic.ahk` A1: the loop without a count ends on the third
    // turn.
    let (report, _desktop) = run("i := 0\nLoop {\n  i += 1\n} Until i >= 3\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
    // A3: the word does not know its case.
    let (report, _desktop) = run("i := 0\nLoop {\n  i += 1\n} UNTIL i >= 3\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
    // A7: the space between the closing brace and the word is not needed.
    let (report, _desktop) = run("i := 0\nLoop {\n  i += 1\n}Until i >= 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_until_of_the_loop_is_a_condition_of_v2() {
    // A4: the `=` of v2 is a comparison and not an assignment.
    let (report, _desktop) = run("i := 0\nLoop {\n  i += 1\n} until i = 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    // A5: the condition is a call of a function of the script.
    let (report, _desktop) =
        run("f() {\n  return true\n}\ni := 0\nLoop {\n  i += 1\n} until f()\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    // A6: the condition reads A_Index.
    let (report, _desktop) = run("i := 0\nLoop {\n  i += 1\n} until A_Index >= 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    // A14 and A15: the condition is an expression of two operands, and the
    // function of the script may be one of them.
    let (report, _desktop) = run("i := 0\nLoop {\n  i += 1\n} until (i >= 2 and i < 10)\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    let (report, _desktop) =
        run("f() {\n  return true\n}\ni := 0\nLoop {\n  i += 1\n} until (i > 5 or f())\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    // P7: the condition may write a variable.
    let (report, _desktop) =
        run("x := 0\ny := 0\nLoop {\n  x += 1\n} until (y := x) >= 2\nMsgBox y");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_count_of_the_loop_is_the_other_end_of_the_until() {
    // A2 and A9: the until ends the loop before the count does.
    let (report, _desktop) = run("i := 0\nLoop 10 {\n  i += 1\n} Until i > 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
    // A9b: the count ends the loop before the until does.
    let (report, _desktop) = run("i := 0\nLoop 2 {\n  i += 1\n} Until i > 5\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_until_of_the_loop_may_be_on_the_line_after_the_closing_brace() {
    // `accept_nextline.ahk` B1: the word on the line after the brace.
    let (report, _desktop) = run("i := 0\nLoop 3\n{\n  i += 1\n}\nUntil i >= 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    // B2: a blank line between them.
    let (report, _desktop) = run("i := 0\nLoop 3\n{\n  i += 1\n}\n\nUntil i >= 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    // B3: a comment on the line of the closing brace.
    let (report, _desktop) =
        run("i := 0\nLoop 3\n{\n  i += 1\n} ; the body is done\nUntil i >= 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    // B4: a comment after the condition.
    let (report, _desktop) =
        run("i := 0\nLoop 3\n{\n  i += 1\n}\nUntil i >= 2 ; the condition\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    // B5 and N2: the content of the block on the line of the brace that
    // opens it, and the until on the line of the closing brace.
    let (report, _desktop) = run("i := 0\nLoop 3\n{ i += 1\n} Until i >= 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_until_of_the_loop_follows_a_body_that_has_no_braces() {
    // F1 and F2: the body is one statement on the line after the loop.
    let (report, _desktop) = run("i := 0\nLoop 10\n  i += 1\nUntil i > 2\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
    let (report, _desktop) = run("i := 0\nLoop 10\n  i += 2\nUntil i >= 4\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["4"]);
}

#[test]
fn the_until_of_the_loop_is_read_after_a_turn_that_used_continue() {
    // A10: the count of the loop and the turn that continued, and the
    // condition that is read after it. The loop without a count is the
    // same rule in v2 (A11 answers 2), and the runtime does not read the
    // until after a `continue` there yet.
    let (report, _desktop) = run(
        "i := 0\nLoop 10 {\n  i += 1\n  if (i = 2) {\n    continue\n  }\n} Until i = 2\nMsgBox i",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn a_break_leaves_the_loop_before_the_until_is_read() {
    // A13: the `break` leaves the loop, and the condition is not read.
    let (report, _desktop) =
        run("x := 0\ni := 0\nLoop {\n  i += 1\n  break\n} Until (x := 1)\nMsgBox i \"|\" x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1|0"]);
}

#[test]
fn a_loop_of_zero_turns_does_not_read_the_until() {
    // A12: the loop whose count is zero never reads the condition.
    let (report, _desktop) = run("x := 0\nLoop 0 {\n} Until (x := 1)\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0"]);
}

#[test]
fn the_until_of_the_loop_binds_the_loop_that_it_follows() {
    // E1: the until of the inner loop is the one read there, and the
    // until of the outer loop is read after each turn of the outer body.
    let (report, _desktop) = run(
        "outer := 0\ninner := 0\nLoop 3 {\n  Loop 2 {\n    inner += 1\n  } Until true\n  outer += 1\n} Until outer >= 2\nMsgBox outer \"|\" inner",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2|2"]);
    // E2: the outer loop has no until, and it counts its turns.
    let (report, _desktop) = run(
        "outer := 0\ninner := 0\nLoop 3 {\n  Loop 2 {\n    inner += 1\n  } Until true\n  outer += 1\n}\nMsgBox outer \"|\" inner",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3|3"]);
}

#[test]
fn the_until_of_the_loop_needs_a_condition_and_names_the_word_as_written() {
    // Measured on the binary of v2.0.19: the word with no
    // condition is refused with this message, and the text that it names
    // is the word as the script wrote it.
    let error = parse("Loop 1\n{\n} Until").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"Until\" requires at least 1 parameter.\n     Specifically: Until"
    );
    assert_eq!(error.line, 3);
    let error = parse("Loop 1\n{\n} until").expect_err("does not parse");
    assert_eq!(
        error.message,
        "\"Until\" requires at least 1 parameter.\n     Specifically: until"
    );
}

#[test]
fn the_until_of_the_while_is_not_a_condition_of_the_loop() {
    // Measured on the binary of v2.0.19: the `while` refuses
    // the word with "Unexpected \"Until\"", and the line that it names is
    // the line of the word. The `for` takes it, and the node of the `for`
    // of this tree has no field for it yet.
    let error =
        parse("i := 0\nWhile i < 5 {\n  i += 1\n}\nUntil i >= 3").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Until\"");
    assert_eq!(error.line, 5);
}

#[test]
fn only_the_access_the_mark_follows_is_lenient() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: the mark of the
    // chain tolerates the one access it follows -- `holder.Prop?.Inner` is the
    // absent value -- and the access that comes before it is read the ordinary
    // way: `holder.Missing.Inner?.Value` is the `PropertyError` of the access
    // before the mark.
    let (report, _desktop) = run_in(
        concat!(
            "class Holder {\n",
            "    Prop => unset\n",
            "}\n",
            "h := Holder()\n",
            "v := h.Prop?.Inner\n",
            "MsgBox \"a\" IsSet(v)\n",
            "try {\n",
            "    v := h.Missing.Inner?.Value\n",
            "    MsgBox \"no throw\"\n",
            "} catch as e {\n",
            "    MsgBox \"b\" Type(e)\n",
            "}\n",
            "w := (h.Prop?)[1]\n",
            "MsgBox \"c\" IsSet(w)\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a0", "bPropertyError", "c0"]);
}

#[test]
fn a_member_that_is_not_there_is_asked_by_isset_but_its_holder_is_read() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: `IsSet` of a member
    // that is not there answers zero -- `holder.Present.Missing` has no value --
    // and the expression that holds the member is read: the failure of
    // `holder.Missing` is the failure of the question.
    let (report, _desktop) = run_in(
        concat!(
            "class Holder {\n",
            "    Present => { Inner: 9 }\n",
            "}\n",
            "h := Holder()\n",
            "MsgBox \"a\" IsSet(h.Present.Missing)\n",
            "try {\n",
            "    IsSet(h.Missing.Inner)\n",
            "    MsgBox \"no throw\"\n",
            "} catch as e {\n",
            "    MsgBox \"b\" Type(e)\n",
            "}\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a0", "bPropertyError"]);
}

#[test]
fn the_absent_value_of_an_operand_is_the_absent_value_of_the_operation() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: an operator that
    // has the absent value for an operand answers the absent value, and the
    // other operand is not read -- `missing?.value + Bump2()` does not call
    // `Bump2()`; the unary operation and the question of the ternary are the
    // same. The assignment through the chain that has no value is skipped, the
    // value expression included.
    let (report, _desktop) = run_in(
        concat!(
            "sideEffects := 0\n",
            "Bump2() {\n",
            "    global sideEffects\n",
            "    return ++sideEffects\n",
            "}\n",
            "missing := unset\n",
            "optVal := missing?.value + Bump2()\n",
            "MsgBox \"a\" IsSet(optVal) sideEffects\n",
            "optVal := -(missing?) + Bump2()\n",
            "MsgBox \"b\" IsSet(optVal) sideEffects\n",
            "optVal := (missing?) ? Bump2() : Bump2()\n",
            "MsgBox \"c\" IsSet(optVal) sideEffects\n",
            "present := { value: 5 }\n",
            "optVal := present?.value + Bump2()\n",
            "MsgBox \"d\" optVal sideEffects\n",
            "missing?.value := Bump2()\n",
            "MsgBox \"e\" sideEffects\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a00", "b00", "c00", "d61", "e1"]);
}

#[test]
fn the_absent_value_of_an_expression_is_asked_by_isset() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: `IsSet` of an
    // expression asks whether the expression has a value, and the name is still
    // asked for its variable.
    let (report, _desktop) = run_in(
        concat!(
            "F(a?) {\n",
            "    return a\n",
            "}\n",
            "MsgBox \"a\" IsSet(F())\n",
            "MsgBox \"b\" IsSet(F(1))\n",
            "x := 5\n",
            "MsgBox \"c\" IsSet(x)\n",
            "y := unset\n",
            "MsgBox \"d\" IsSet(y)\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a0", "b1", "c1", "d0"]);
}

#[test]
fn a_property_may_take_the_parameters_of_its_access() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: the property that
    // takes parameters -- `Items[Index] => unset`, `Pair[a, b] => ...` and the
    // accessor that has a setter -- is read and written with them, and the
    // brackets after the name of a property that has none are the item of the
    // value of the property.
    let (report, _desktop) = run_in(
        concat!(
            "class OptHolder\n",
            "{\n",
            "    Stored := 0\n",
            "    Items[Index] => unset\n",
            "    Writable[Index]\n",
            "    {\n",
            "        get\n",
            "        {\n",
            "            return this.Stored\n",
            "        }\n",
            "        set => this.Stored := Value\n",
            "    }\n",
            "    Pair[a, b] => a . \"|\" . b\n",
            "}\n",
            "o := OptHolder()\n",
            "v := o.Items[1]\n",
            "MsgBox \"a\" IsSet(v)\n",
            "o.Writable[1] := 7\n",
            "MsgBox \"b\" o.Stored \" \" o.Writable[2]\n",
            "MsgBox \"c\" o.Pair[1, 2]\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a0", "b7 7", "c1|2"]);
}

#[test]
fn a_property_of_the_v2_1_may_hold_the_absent_value() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: `{prop: unset}`
    // keeps the property with the absent value in the object of the v2.1 -- the
    // v2.0.19 leaves it out -- and the read of it is the absent value, so the
    // chain of the v2.1 that follows it is the absent value as well.
    let (report, _desktop) = run_in(
        concat!(
            "o := { prop: unset }\n",
            "MsgBox \"a\" ObjOwnPropCount(o) \" \" (o.prop is unset)\n",
            "v := o?.prop?.inner\n",
            "MsgBox \"b\" IsSet(v)\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a1 1", "b0"]);
}

#[test]
fn the_maybe_of_a_dynamic_name_is_skipped_when_the_name_has_no_value() {
    // Measured on the test suite of KeySharp (the corpus script
    // `assign-unset.ahk`) and on the notes of v2.1-alpha: the `?` before the
    // percent that closes a name the script builds marks the name as one that
    // may be absent. The member is not read and the parameters of the call and
    // of the item are not evaluated when the name has no value, and the name
    // that has one is read the way it was.
    let (report, _desktop) = run_in(
        concat!(
            "named := {X: 1, __Item: 9}\n",
            "missingName := unset\n",
            "evaluated := false\n",
            "MsgBox \"a\" (named.%missingName?% ?? \"none\")\n",
            "MsgBox \"b\" (named.%missingName?%(evaluated := true) ?? \"none\")\n",
            "MsgBox \"c\" (named.%missingName?%[evaluated := true] ?? \"none\")\n",
            "MsgBox \"d\" evaluated\n",
            "MsgBox \"e\" (%missingName?% ?? \"none\")\n",
            "presentName := \"X\"\n",
            "MsgBox \"f\" (named.%presentName?% ?? \"none\")\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["anone", "bnone", "cnone", "d0", "enone", "f1"]
    );
}

#[test]
fn the_comparison_with_the_absent_value_asks_whether_the_other_side_has_one() {
    // Measured on the test suite of KeySharp (the corpus script
    // `assign-unset.ahk`) and on the notes of v2.1-alpha: `x = unset` is one
    // when `x` has no value and zero when it has one, `unset = x` asks the same
    // question, and a name that has no value is the answer and not a failure.
    let (report, _desktop) = run_in(
        concat!(
            "x := 123\n",
            "MsgBox \"a\" (x = unset)\n",
            "x := unset\n",
            "MsgBox \"b\" (x = unset)\n",
            "MsgBox \"c\" (unset = x)\n",
            "MsgBox \"d\" (x != unset)\n",
            "MsgBox \"e\" (1 = unset)\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a0", "b1", "c1", "d0", "e0"]);
}

#[test]
fn the_chain_of_the_v2_1_is_skipped_when_the_value_is_absent() {
    // Measured on the test suite of KeySharp (the corpus scripts
    // `op-null-assign.ahk` and `class-ownprops.ahk`) and on the notes of
    // v2.1-alpha: the `?` that is glued to the `.`, the `[` or the `(` asks
    // whether the left side has a value. The chain is the absent value when it
    // has none, the steps after it are not read, a write through it is not
    // written, and a step whose left side has a value answers it.
    let (report, _desktop) = run_in(
        concat!(
            "o := {a: {b: 2}}\n",
            "MsgBox \"a\" o?.a?.b\n",
            "x := unset\n",
            "MsgBox \"b\" (x?.a is unset)\n",
            "MsgBox \"c\" (x?[1] is unset)\n",
            "F(a?) {\n",
            "    return a\n",
            "}\n",
            "MsgBox \"d\" (F()?.z is unset)\n",
            "MsgBox \"e\" (F({z: 5})?.z)\n",
            "h := {w: {i: 1}}\n",
            "h?.w.i := 7\n",
            "MsgBox \"f\" h.w.i\n",
            "y := unset\n",
            "y?.w.i := 9\n",
            "G(a?) => a\n",
            "MsgBox \"g\" (G()?() is unset)\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["a2", "b1", "c1", "d1", "e5", "f7", "g1"]
    );
}

#[test]
fn the_setter_of_a_descriptor_writes_the_object_it_holds() {
    // Measured on the test suite of KeySharp (the corpus script
    // `class-ownprops.ahk`): the write of a property that is a
    // descriptor calls its setter, and the setter writes the same object. The
    // write kept the `Ref` of the object alive and panicked with "RefCell
    // already borrowed".
    let (report, _desktop) = run(concat!(
        "class C {\n",
        "    a := 0\n",
        "}\n",
        "o := C()\n",
        "o.DefineProp(\"a\", {get: (this) => 123, set: (this, v) => this.b := v})\n",
        "o.a := 100\n",
        "MsgBox o.a \" \" o.b\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["123 100"]);
}

#[test]
fn the_mechanisms_of_the_v2_1_ask_about_the_absent_value() {
    // Measured on the test suite of KeySharp (the corpus scripts
    // `flow-if.ahk`, `func-global-static.ahk`, `op-logical-not.ahk`,
    // `assign-unset.ahk` and `op-null-assign.ahk`) and on the notes of
    // v2.1-alpha: `x is unset` answers whether the left side has a value and
    // does not read it, `??` takes any expression on its left, and `??=`
    // writes the value only when the name has none.
    let (report, _desktop) = run_in(
        concat!(
            "x := 5\n",
            "MsgBox \"a\" (x is unset)\n",
            "F(a?) {\n",
            "    return a\n",
            "}\n",
            "MsgBox \"b\" (F() is unset)\n",
            "MsgBox \"c\" (F(7) is unset)\n",
            "MsgBox \"d\" (F() ?? \"none\")\n",
            "MsgBox \"e\" (F(7) ?? \"none\")\n",
            "MsgBox \"f\" ((1 + 1) ?? 9)\n",
            "z := unset\n",
            "z ??= 5\n",
            "MsgBox \"g\" z\n",
            "z ??= 7\n",
            "MsgBox \"h\" z\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["a0", "b1", "c0", "dnone", "e7", "f2", "g5", "h5"]
    );
}

#[test]
fn a_static_without_a_value_shadows_the_global_of_the_same_name() {
    // Measured on the binary of v2.0.19: the `static z` of a
    // function has no value, and the read of it fails with the local variable
    // that has not been assigned a value; it does not read the global `z`.
    let (report, _desktop) = run(concat!(
        "z := 3\n",
        "F() {\n",
        "    static z\n",
        "    MsgBox z\n",
        "}\n",
        "F()\n",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This local variable has not been assigned a value.")
    );
}

#[test]
fn the_until_of_a_while_is_the_mechanism_of_the_v2_1() {
    // Measured on the test suite of KeySharp (the corpus script
    // `flow-until.ahk`) and on the notes of v2.1-alpha: the `Until` of a
    // `while` comes after the body, the body runs before the condition is read
    // on every turn, and the loop ends when it is true. The x of the script is
    // six when the fifth turn ends, and the index of the turn is five.
    let (report, _desktop) = run_in(
        "x := 1\nwhile true\n{\n    x++\n}\nUntil (A_Index == 5)\nMsgBox x",
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["6"]);
}

#[test]
fn the_until_is_read_after_a_turn_that_continue_cut_short() {
    // Measured on the binary of v2.0.19: the `Until` is read
    // after the turn that `Continue` cut short as well, and the loop ends with
    // the variable at three. The v2.0.19 carries the `Until` of a `Loop`, and
    // the tree did not read it on that turn: the loop ran forever.
    let (report, _desktop) =
        run("z := 0\nLoop\n{\n    z++\n    continue\n}\nUntil z = 3\nMsgBox z");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
}

#[test]
fn the_word_until_is_reserved_in_v2_and_not_in_a_property() {
    // Measured on the binary of v2.0.19: the word is not the
    // name of a variable, and it is the name of a property.
    let error = parse("until := 2").expect_err("does not parse");
    assert_eq!(
        error.message,
        "The following reserved word must not be used as a variable name:\n\"until\""
    );
    let (report, _desktop) = run("o := {until: 5}\nMsgBox o.until");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["5"]);
}

#[test]
fn the_until_of_the_loop_of_a_kind_is_parsed() {
    // Measured on the binary of v2.0.19: the loops of a kind
    // take the until as well -- `Loop Parse "a,b,c", "," { } Until i >= 2`
    // ends on the second field. The parser attaches it; the runtime does
    // not read it for the kinds yet.
    assert!(parse("Loop Parse \"a,b,c\", \",\" {\n  i += 1\n} Until i >= 2").is_ok());
    assert!(parse("Loop Files \"*.txt\" {\n} Until true").is_ok());
}

#[test]
fn the_until_of_the_v1_is_still_the_statement_after_the_loop() {
    // Measured on the binary of AutoHotkey v1.1.37.02: the
    // `until` of the v1 is the condition of the loop, and the v1 reads it
    // from the statement after the body. The v2 change does not touch it.
    let (report, _desktop) = run_in(
        "i := 0\nLoop 3 {\ni := i + 1\n}\nUntil i == 2\nFileAppend, [%i%], *",
        ahk::dialect::v1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["[2]".to_string()]);
    // The `until` that no loop takes is still refused when the load is
    // over.
    let error = ahk::parse_with("Until 1", ahk::dialect::v1()).expect_err("does not parse");
    assert_eq!(error.message, "UNTIL with no matching LOOP");
}

#[test]
fn the_fields_of_the_loop_that_parses_are_the_characters_of_the_text() {
    // Measured on the binary of v2.0.19: the delimiters
    // that are not there make every character of the text a field of its
    // own, and the spaces are fields too.
    let (report, desktop) = run("Loop Parse \"a b\" {\n  Send \"%A_LoopField%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["a", " ", "b"]);
    // The characters that are not wanted at the ends of a field are taken
    // away from them.
    let (report, desktop) =
        run("Loop Parse \" a , b \", \",\", \" \" {\n  Send \"%A_LoopField%\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["a", "b"]);
}

#[test]
fn the_variables_of_the_loops_are_there_before_the_first_loop() {
    // Measured on the binary of v2.0.19: the index is zero
    // and the others are empty.
    let (report, _desktop) = run("MsgBox \"[\" A_Index \"]\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["[0]"]);
    let (report, _desktop) = run("MsgBox \"[\" A_LoopField \"]\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["[]"]);
    let (report, _desktop) = run("MsgBox \"[\" A_LoopReadLine \"]\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["[]"]);
}

#[test]
fn the_capacity_of_a_string_that_has_no_value_is_the_one_that_was_asked_for() {
    // Measured on the binary of v2.0.19: a variable that
    // has no value is a text that is empty, and the capacity of it is
    // the one that was asked for.
    let (report, _desktop) = run("MsgBox VarSetStrCapacity(&x, 100)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["100"]);
    // Measured on the binary of v2.0.19: the capacity of a
    // text is the size class AHK keeps for it, and the "hello" of five
    // characters is of the class of seven. The tree keeps the class as
    // well since the block of the string memory.
    let (report, _desktop) = run("x := \"hello\"\nMsgBox VarSetStrCapacity(&x)");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["7"]);
}

#[test]
fn an_object_is_not_written_as_a_text() {
    // Measured on the binary of v2.0.19: v2 reads the
    // property "Ptr" of the value that it writes, and an object has
    // none.
    let (report, _desktop) = run("FileAppend Map(), \"*\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Map\" has no property named \"Ptr\".")
    );
    let (report, _desktop) = run("FileAppend [1, 2], \"*\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Array\" has no property named \"Ptr\".")
    );
}

#[test]
fn a_class_is_a_class_of_its_own_kind_and_a_prototype_is_of_its_own() {
    // Measured on the binary of v2.0.19: the refusal of
    // v2 names the class "Class" and the prototype "Prototype".
    let (report, _desktop) = run("FileAppend Gui, \"*\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Class\" has no property named \"Ptr\".")
    );
    let (report, _desktop) = run("FileAppend Gui.Prototype, \"*\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Prototype\" has no property named \"Ptr\".")
    );
    // The parameter of a built-in names the kind of the value that
    // arrived the same way.
    let (report, _desktop) = run("MsgBox Gui");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Parameter #1 of MsgBox requires a String, but received a Class.")
    );
}

#[test]
fn the_stack_of_an_error_names_the_frames_of_the_call() {
    // Measured on the binary of v2.0.19: every frame is a
    // line of its own -- the file, the line, the name of the frame and
    // the source of the line -- and the last line names the section that
    // starts the script. The script of the runner has no file of its own,
    // and the file of every frame is empty.
    let (report, _desktop) = run(
        "f() {\n  Throw Error(\"boom\")\n}\ntry {\n  f()\n} catch Any as err {\n  MsgBox err.Stack\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![" (2) : [f] Throw Error(\"boom\")\n (5) : [] f()\n> Auto-execute"]
    );
}

#[test]
fn the_stack_of_an_error_of_the_section_that_starts_the_script() {
    // The section that starts the script is the last frame of the stack,
    // and it has no name.
    let (report, _desktop) =
        run("try {\n  Throw Error(\"boom\")\n} catch Any as err {\n  MsgBox err.Stack\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![" (2) : [] Throw Error(\"boom\")\n> Auto-execute"]
    );
}

#[test]
fn the_stack_of_a_failure_of_the_run_names_the_line_that_failed() {
    // A failure that the script did not throw has a stack too.
    let (report, _desktop) =
        run("try {\n  x := 1 / 0\n} catch Any as err {\n  MsgBox err.Stack\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec![" (2) : [] x := 1 / 0\n> Auto-execute"]);
}

#[test]
fn the_stack_names_every_frame_of_the_call() {
    // The frames come from the inside out, and the name of a method is
    // the one that v2 gives it.
    let (report, _desktop) = run(
        "g() {\n  f()\n}\nf() {\n  h()\n}\nh() {\n  Throw Error(\"boom\")\n}\ntry {\n  g()\n} catch Any as err {\n  MsgBox err.Stack\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            " (8) : [h] Throw Error(\"boom\")\n (5) : [f] h()\n (2) : [g] f()\n (11) : [] g()\n> Auto-execute"
        ]
    );
}

#[test]
fn the_member_of_the_answer_of_a_call_is_read_in_a_statement() {
    // Measured on the binary of v2.0.19: the answer of a call
    // is a value, and the member of it is read the way the member of any
    // other value is. The statement reads it and leaves the value behind.
    let (report, _desktop) = run("Map().Count");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn the_method_of_the_answer_of_a_call_is_called_in_a_statement() {
    // Measured on the binary of v2.0.19: the method of the
    // answer of a call is called when the statement is the call of it.
    let (report, _desktop) = run(r#"Map().HasOwnProp("a")"#);
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn the_write_to_a_member_of_a_call_is_refused_when_the_property_is_read_only() {
    // Measured on the binary of v2.0.19: the write of a
    // property that may not be written is refused at the run, with
    // "Property is read-only.", the name of the property in the Extra, and
    // nothing in the What.
    let (report, _desktop) = run(r#"try {
    Map().Count := 1
} catch Any as e {
    MsgBox e.Message " | " e.What " | " e.Extra
}"#);
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Property is read-only. |  | Count"]);
}

#[test]
fn the_call_itself_may_not_be_written() {
    // Measured on the binary of v2.0.19: the answer of a call
    // is not a value that may be written, and the refusal is at the load,
    // with the operator and the value named.
    let error = parse("Map() := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Invalid assignment.\n     Specifically: := 1"
    );
    let error = parse("Map()++").expect_err("does not parse");
    assert_eq!(error.message, "Invalid assignment.\n     Specifically: ++");
}

#[test]
fn the_item_of_the_answer_of_a_call_may_be_written() {
    // Measured on the binary of v2.0.19: the item of the
    // answer of a call is an item, and it may be written.
    let (report, _desktop) = run(r#"Map()["a"] := 1"#);
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn the_property_of_the_answer_of_a_constructor_may_be_written() {
    // Measured on the binary of v2.0.19: the property of the
    // answer of a constructor is a property of the instance, and it may be
    // written.
    let (report, _desktop) = run("class T {\n    P := 3\n}\nT().P := 1");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn the_hash_that_is_not_a_directive_is_an_illegal_character() {
    // Measured on the binary of v2.0.19: the hash is a
    // directive only at the start of a line, and anywhere else it is an
    // illegal character in an expression. The text that is named is the
    // expression, and the command of v2 carries the parenthesis that closes
    // it.
    let error = parse("x := 1 #y").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Illegal character in expression.\n     Specifically: #y"
    );
    let error = parse("MsgBox #y").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Illegal character in expression.\n     Specifically: #y)"
    );
}

#[test]
fn the_hash_that_follows_a_name_is_a_line_that_holds_no_action() {
    // Measured on the binary of v2.0.19: the hash that
    // follows a name is a part of it, and the line that holds it holds no
    // action.
    let error = parse("x#y").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: x#y"
    );
    let error = parse("x#y := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: x#y := 1"
    );
}

#[test]
fn the_directive_that_is_not_known_is_a_line_that_holds_no_action() {
    // Measured on the binary of v2.0.19: a name that the v2
    // does not know is not a directive at all, and the refusal is at the
    // load.
    let error = parse("#Foo").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: #Foo"
    );
    let error = parse("#Foo Bar").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: #Foo Bar"
    );
}

#[test]
fn the_double_deref_reads_the_variable_that_the_name_spells() {
    // Measured on the binary of v2.0.19: the double deref of
    // v2 is a name that the script builds -- the value of the expression is
    // the name of the variable whose value is the answer.
    let (report, _) = run("b := 42\nx := \"b\"\nMsgBox %x%");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["42"]);
}

#[test]
fn the_dynamic_name_reads_the_variable_whose_name_is_built() {
    // Measured on the binary of v2.0.19: the pieces of the
    // name are the text that is written and the values of the expressions,
    // and the space between two of them is what tells that the name is
    // over.
    let (report, _) = run("MyArray1 := \"A\"\ni := 1\nMsgBox MyArray%i%");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["A"]);
}

#[test]
fn the_dynamic_name_may_be_written() {
    // Measured on the binary of v2.0.19: the name that the
    // script builds may be written to, and the variable that it names is
    // the one that changes.
    let (report, _) = run("MyArray1 := \"A\"\ni := 1\nMyArray%i% := \"B\"\nMsgBox MyArray1");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["B"]);
}

#[test]
fn the_double_deref_may_be_written() {
    // Measured on the binary of v2.0.19: the name that the
    // script builds may be written to.
    let (report, _) = run("b := 1\nx := \"b\"\n%x% := 7\nMsgBox b");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["7"]);
}

#[test]
fn the_name_that_is_not_a_variable_fails_with_its_name() {
    // Measured on the binary of v2.0.19: the name of a
    // dynamic variable reference is the name of a variable, and the one
    // that is not there fails with "Variable not found." and the name in
    // the `Extra`.
    let (report, _) = run(
        "try {\n    x := \"nope\"\n    MsgBox %x%\n} catch as e {\n    MsgBox e.Message\n    MsgBox e.Extra\n    MsgBox e.__Class\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Variable not found.", "nope", "Error"]);
}

#[test]
fn the_name_of_a_dynamic_reference_is_not_a_path() {
    // Measured on the binary of v2.0.19: the name of a
    // dynamic variable reference is a name, and not a path of names:
    // `%m.a%` is the variable `m.a`, and it is not there.
    let (report, _) = run(
        "try {\n    m := Map()\n    x := \"m.a\"\n    MsgBox %x%\n} catch as e {\n    MsgBox e.Message\n    MsgBox e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Variable not found.", "m.a"]);
}

#[test]
fn the_dynamic_member_reads_the_property_that_the_name_spells() {
    // Measured on the binary of v2.0.19: the name of a
    // member may be built by the script: `m.%x%` is the member whose name
    // is the value of `x`.
    let (report, _) = run("m := {a: 1}\nx := \"a\"\nMsgBox m.%x%");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_dynamic_member_may_be_written() {
    // Measured on the binary of v2.0.19: the member whose
    // name the script builds may be written to.
    let (report, _) = run("m := {a: 1}\nx := \"a\"\nm.%x% := 7\nMsgBox m.a");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["7"]);
}

#[test]
fn the_dynamic_method_is_called_by_its_name() {
    // Measured on the binary of v2.0.19: `m.%x%()` is the
    // method whose name is the value of `x`, and the one that is not there
    // fails with the name of it.
    let (report, _) = run(
        "try {\n    m := {a: 1}\n    x := \"a\"\n    m.%x%()\n} catch as e {\n    MsgBox e.Message\n    MsgBox e.__Class\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            "This value of type \"Object\" has no method named \"a\".",
            "MethodError"
        ]
    );
}

#[test]
fn the_dynamic_write_of_a_global_from_a_function_wants_a_global_declaration() {
    // Measured on the binary of v2.0.19: a function that
    // writes a name of the script says that it wants a global declaration,
    // and the failure is of the class `Error`.
    let (report, _) = run(
        "b := 1\ntry {\n    Write()\n} catch as e {\n    MsgBox e.Message\n    MsgBox e.Extra\n    MsgBox e.__Class\n}\n\nWrite() {\n    x := \"b\"\n    %x% := 7\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            "This dynamic assignment requires a \"global\" declaration.",
            "b",
            "Error"
        ]
    );
}

#[test]
fn the_double_deref_that_does_not_close_is_refused_at_the_load() {
    // Measured on the binary of v2.0.19: the name that does
    // not close is refused at the load, and the text that is named is the
    // rest of the line, from the name.
    let error = parse("x := %a").expect_err("does not parse");
    assert_eq!(error.message, "Missing ending \"%\"\n     Specifically: %a");
    let error = parse("x := a%b").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing ending \"%\"\n     Specifically: a%b"
    );
}

#[test]
fn the_percent_sign_that_is_inside_a_name_is_a_syntax_error() {
    // Measured on the binary of v2.0.19: the percent sign
    // that is inside a name that the script builds is not a value, and v2
    // says "Syntax error." and the rest of the line, from it.
    let error = parse("x := %%y%%").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: %y%%");
}

#[test]
fn the_double_deref_alone_is_a_statement() {
    // Measured on the binary of v2.0.19: a statement that
    // begins with a double deref is a name that the script builds, and the
    // value it leaves behind is not used.
    let (report, _) = run("b := 42\nx := \"b\"\n%x%");
    assert!(report.ok, "{:?}", report.error);
    assert!(report.output.is_empty());
}

#[test]
fn the_dynamic_name_of_a_function_is_the_function() {
    // Measured on the binary of v2.0.19: the name of a
    // dynamic variable reference is looked up the way a name that is
    // written is looked up: a function of the script is a value too.
    let (report, _) = run("f() {\n    return 1\n}\nx := \"f\"\nMsgBox %x%.__Class");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Func"]);
}

#[test]
fn the_bracket_that_is_not_closed_is_the_failure() {
    // Measured on the binary of v2.0.19: the bracket that is
    // not closed is the failure, and not the operand that is not there
    // inside it: `x := f(1,` is a "Missing ")" and not a "Missing
    // operand.".
    let error = parse("x := f(1,").expect_err("does not parse");
    assert_eq!(error.message, "Missing \")\"\n     Specifically: x := f(1,");
    let error = parse("x := [").expect_err("does not parse");
    assert_eq!(error.message, "Missing \"]\"\n     Specifically: x := [");
    let error = parse("x := {").expect_err("does not parse");
    assert_eq!(error.message, "Missing \"}\"\n     Specifically: x := {");
    let error = parse("x := (1 +").expect_err("does not parse");
    assert_eq!(error.message, "Missing \")\"\n     Specifically: x := (1 +");
}

#[test]
fn the_brace_that_closes_the_block_is_named_before_the_bracket() {
    // Measured on the binary of v2.0.19: a brace that closes
    // a block is named before the bracket that is not closed, and the text
    // that is named is the statement and the line of the failure.
    let error = parse("try {\n    x := f(1\n} catch as err {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \")\" before \"}\"\n     Specifically: x := f(1 } catch as err {"
    );
}

#[test]
fn the_button_of_a_click_is_a_value_and_not_a_word() {
    // Measured on the binary of v2.0.19: `Click 10, 20,
    // right` reads the variable `right`. The v2 refuses the script at the
    // load when that variable has no value, and the tree refuses it at the
    // run.
    let (report, _desktop) = run("Click 10, 20, right");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This global variable has not been assigned a value.")
    );
}

#[test]
fn the_button_of_a_click_may_be_the_value_of_a_variable() {
    // Measured on the binary of v2.0.19: the value of the
    // variable is the name of the button, and not the name of the
    // variable.
    let (report, desktop) = run("right := \"left\"\nClick 10, 20, right");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("click".to_string(), "10,20,left,1".to_string())]
    );
}

#[test]
fn the_name_of_a_property_of_an_object_may_be_built() {
    // Measured on the binary of v2.0.19: `{%name%: 1}` is
    // the property whose name is the value of `name`.
    let (report, _desktop) = run("name := \"a\"\nx := {%name%: 1}\nFileAppend x.a, \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1".to_string()]);
}

#[test]
fn a_brace_that_opens_an_argument_of_a_command_opens_an_object() {
    // Measured on the binary of v2.0.19: `Send {Enter}` is
    // refused at the load, because the brace opens the literal of an
    // object and the name of a property is not there.
    let error = parse("Send {Enter}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Missing \"propertyname:\" in object literal."
    );
}

#[test]
fn the_button_of_a_mouse_click_is_the_first_parameter() {
    // Measured on the binary of v2.0.19: the button of a
    // `MouseClick` is the first parameter, and it is a text that is not
    // spread. `MouseClick 10, 20` is refused, and the "10" is the button.
    let (report, _desktop) = run("MouseClick 10, 20");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Parameter #1 of MouseClick is invalid.")
    );
}

#[test]
fn the_button_of_a_mouse_click_that_is_not_one_is_refused() {
    // Measured on the binary of v2.0.19.
    let (report, _desktop) = run("MouseClick \"nonsense\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Parameter #1 of MouseClick is invalid.")
    );
}

#[test]
fn the_button_of_a_mouse_click_is_not_spread() {
    // Measured on the binary of v2.0.19: the words of the
    // button are not the words of a click.
    let (report, _desktop) = run("MouseClick \"R D\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Parameter #1 of MouseClick is invalid.")
    );
}

#[test]
fn the_value_of_the_button_that_failed_is_in_the_extra() {
    // Measured on the binary of v2.0.19: the value that
    // failed is in the `Extra`, with the space at the end of it cut.
    let (report, _desktop) =
        run("try {\n    MouseClick \"Left \"\n} catch as err {\n    MsgBox err.Extra\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Left".to_string()]);
}

#[test]
fn a_mouse_click_takes_the_place_and_the_count() {
    let (report, desktop) = run("MouseClick \"Right\", 10, 20, 3");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("click".to_string(), "10,20,right,3".to_string())]
    );
}

#[test]
fn a_mouse_click_with_no_button_is_the_left_one() {
    // Measured on the binary of v2.0.19: a button that is
    // not there is the left one.
    let (report, desktop) = run("MouseClick");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("click".to_string(), "0,0,left,1".to_string())]
    );
}

#[test]
fn the_wheel_of_a_mouse_click_is_carried() {
    // Measured on the binary of v2.0.31: the `MouseClick
    // "WheelUp"` answers and the wheel turns.
    let (report, desktop) = run("MouseClick \"WheelUp\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("wheel".to_string(), "wheelup,1".to_string())]
    );
}

#[test]
fn the_fourth_button_of_a_mouse_click_is_carried() {
    // Measured on the binary of v2.0.31: the `MouseClick
    // "X1"` answers and the fourth button is the one that is pressed.
    let (report, desktop) = run("MouseClick \"X1\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        desktop.calls,
        vec![("click".to_string(), "0,0,x1,1".to_string())]
    );
}

#[test]
fn the_value_that_the_is_refuses_is_in_the_extra() {
    // Measured on the binary of v2.0.19: the value that
    // arrived goes in the `Extra` of the failure, and the `What` is empty.
    let (report, _desktop) =
        run("try {\n    x := 1 is 2\n} catch as err {\n    MsgBox err.Extra\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2".to_string()]);
}

#[test]
fn the_value_of_the_list_is_the_last_one() {
    // Measured on the binary of v2.0.19: the value of a
    // parenthesized list is the last of its items.
    let (report, _desktop) = run("x := (\"a\", \"b\")\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["b"]);
}

#[test]
fn every_item_of_the_list_runs() {
    // Measured on the binary of v2.0.19: every item of a
    // parenthesized list runs.
    let (report, _desktop) =
        run("i := 0\nf() {\n    global i\n    i := i + 1\n}\nx := (f(), f())\nMsgBox i");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_name_with_a_space_and_a_parenthesis_is_a_command() {
    // Measured on the binary of v2.0.19: the parenthesis
    // that is spaced is not the list of the arguments of a call, and the
    // group is one parameter of the command.
    let (report, _desktop) = run("One(a) {\n    MsgBox a\n}\nOne (\"a\", \"b\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["b"]);
}

#[test]
fn the_command_with_a_group_and_a_text_has_two_parameters() {
    // Measured on the binary of v2.0.19: the parameters of
    // a command are the expressions that the commas separate.
    let (report, _desktop) = run("Two(a, b) {\n    MsgBox a\n    MsgBox b\n}\nTwo (\"a\"), \"b\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a", "b"]);
}

#[test]
fn the_commas_at_the_end_of_a_command_add_nothing() {
    // Measured on the binary of v2.0.19: the commas at the
    // end of the parameters of a command add nothing.
    let (report, _desktop) = run("One(a) {\n    MsgBox a\n}\nOne (\"a\"),");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a"]);
}

#[test]
fn the_member_with_a_space_and_a_parenthesis_is_a_command() {
    // Measured on the binary of v2.0.19: the parenthesis
    // that is spaced is not the list of the arguments of a call, and the
    // group is one parameter of the command.
    let (report, _desktop) = run(
        "class Thing {\n    Method(a) {\n        MsgBox a\n    }\n}\nt := Thing()\nt.Method (\"a\", \"b\")",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["b"]);
}

#[test]
fn the_member_with_a_space_is_not_a_call_in_an_expression() {
    // Measured on the binary of v2.0.19: in an expression the
    // parenthesis that is spaced is the value that follows the member, and
    // the concatenation of v2 takes the two.
    let (report, _desktop) = run(
        "class Thing {\n    Method(a) {\n        MsgBox a\n    }\n}\nt := Thing()\nx := t.Method (\"a\")",
    );
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a String but got a Func.")
    );
}

#[test]
fn the_parameter_that_is_not_there_is_a_missing_one() {
    // Measured on the binary of v2.0.19: the parameter
    // that the command does not give a value to is a missing one, and the
    // call is refused with "Missing a required parameter.".
    let (report, _desktop) = run("Three(a, b, c) {\n    MsgBox a\n}\nThree (\"a\"),, \"c\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Missing a required parameter.")
    );
    assert_eq!(report.error_line, Some(4));
}

#[test]
fn the_name_that_the_script_builds_is_incremented_the_way_a_variable_is() {
    // Measured on the binary of v2.0.19: `y%x%++` answers the
    // old value and stores the new one, and `++y%x%` answers the new one.
    let (report, _desktop) = run(concat!(
        "x := 1\n",
        "y1 := 5\n",
        "a := y%x%++\n",
        "b := ++y%x%\n",
        "FileAppend(a \"|\" b \"|\" y1, \"*\")\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["5|7|7"]);
}

#[test]
fn the_hotkey_of_the_v2_is_a_line_of_its_own() {
    // Measured on the binary of v2.0.19: the hotkey of the v2
    // is a line whose name is followed by two colons, and the action of it is
    // the statement of the line, the block of the same line, the block of the
    // line that comes after it, or any statement (`return`). The `#` of the
    // Win modifier and the `$` of the hook are the first characters of the
    // line, and the fake desktop of the tests cannot watch the keyboard: the
    // script runs and says so in a note, because v2 has no failure of the load
    // for a hotkey.
    let (report, _desktop) = run(concat!(
        "F1::MsgBox \"one\"\n",
        "F2::{\n    MsgBox \"two\"\n}\n",
        "F3::\n{\n    MsgBox \"three\"\n}\n",
        "F4::return\n",
        "^!a::MsgBox \"chord\"\n",
        "F6 Up::MsgBox \"up\"\n",
        "#z::MsgBox \"hash\"\n",
        "$F5::MsgBox \"hook\"\n",
        "MsgBox \"after\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    // The action of a hotkey does not run in this runner, and the statement
    // after the definitions does.
    assert_eq!(report.output, vec!["after"]);
    assert!(
        report.notes.iter().any(|note| note.contains("not watched")),
        "{:?}",
        report.notes
    );
}

#[test]
fn the_hotkey_of_the_v2_refuses_the_shapes_the_binary_refuses() {
    // Measured on the binary of v2.0.19: the name that is
    // declared twice is "Duplicate hotkey.", the name that is not a key is
    // "Invalid key name.", and the braces that do not end the line are "This
    // line does not contain a recognized action.".
    let error = parse("F1::MsgBox \"a\"\nF1::MsgBox \"b\"\n").expect_err("does not parse");
    assert_eq!(error.message, "Duplicate hotkey.\n     Specifically: F1");
    let error = parse("zzz::MsgBox \"a\"\n").expect_err("does not parse");
    assert_eq!(error.message, "Invalid key name.\n     Specifically: zzz");
    let error = parse("F1::{}\n").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: {}"
    );
    let error = parse("F1::{ MsgBox \"x\" }\n").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: { MsgBox \"x\" }"
    );
}

/// The line of a hotkey of the v2 is the line of the hotkey, and not the
/// continuation of the expression that comes before it.
///
/// Measured on the binary of the v2.0.32: the `^` of the name of a hotkey
/// is the operator of the power, and a line that begins with an operator
/// continues the expression that comes before it -- but the line of a hotkey
/// is the line of the hotkey, and the two hotkeys of a script are two
/// hotkeys.
#[test]
fn two_hotkeys_of_the_v2_in_a_row_are_two_hotkeys() {
    let (report, _desktop) = run(concat!(
        "^j::MsgBox \"one\"\n",
        "^k::MsgBox \"two\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    // And the hotkey that follows a hotkey with a block is a hotkey too.
    let (report, _desktop) = run(concat!(
        "^j:: {\n",
        "    MsgBox \"one\"\n",
        "}\n",
        "^k::MsgBox \"two\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
}

/// The body of a hotkey is a scope of its own.
///
/// Measured on the binary of the v2.0.32: the body of a hotkey is the body
/// of a function, and the `static` is a declaration in it.
#[test]
fn the_body_of_a_hotkey_is_a_scope_of_its_own() {
    let (report, _desktop) = run(concat!(
        "F3:: {\n",
        "    static last := \"\"\n",
        "    FileAppend(last, \"*\")\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
}

/// The line of a command that ends with a comma goes on at the next line.
///
/// Measured on the binary of the v2.0.32: the command of the v2 is a call
/// whose parentheses are not written, and the line that ends with a comma
/// continues on the line that follows.
#[test]
fn the_line_of_a_command_that_ends_with_a_comma_goes_on() {
    let (report, _desktop) = run(concat!(
        "MsgBox \"a\",\n",
        "    \"b\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
}

/// The hotkey carries the condition of the `#HotIf` that comes before it.
///
/// Measured on the binary of the v2.0.32: the same name under two different
/// conditions is two hotkeys, and the same name under the same condition is
/// one hotkey written twice, which is refused with "Duplicate hotkey.".
#[test]
fn the_same_hotkey_under_two_conditions_is_two_hotkeys() {
    let (report, _desktop) = run(concat!(
        "#HotIf mode = \"normal\"\n",
        "F5::FileAppend(\"a\", \"*\")\n",
        "#HotIf\n",
        "#HotIf mode = \"edit\"\n",
        "F5::FileAppend(\"b\", \"*\")\n",
        "#HotIf\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    // And the same name under the same condition is a hotkey written twice.
    let error = parse(concat!(
        "#HotIf mode = \"normal\"\n",
        "F5::FileAppend(\"a\", \"*\")\n",
        "#HotIf\n",
        "#HotIf mode = \"normal\"\n",
        "F5::FileAppend(\"b\", \"*\")\n",
        "#HotIf\n",
    ))
    .expect_err("the hotkey written twice is refused");
    assert!(
        error.message.contains("Duplicate hotkey."),
        "{}",
        error.message
    );
}

/// The newline that is inside a bracket does not end the expression.
///
/// Measured on the binary of the v2.0.32: the values of the line that
/// follows are a part of the expression that is inside the bracket, and the
/// tree refused the call with "Missing ")"".
#[test]
fn the_values_of_the_line_that_follows_are_a_part_of_the_expression() {
    let (report, _desktop) = run(concat!(
        "FileAppend(\"a\"\n",
        "    \" b\", \"*\")\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a b"]);
}

/// The line that begins with the dot is the member of the value that came
/// before it.
///
/// Measured on the binary of the v2.0.32: the `x.Add("a")` of one line and
/// the `.OnEvent(...)` of the next are one chain, and the tree refused it
/// with "Unexpected "."".
#[test]
fn the_line_that_begins_with_the_dot_is_the_member_of_the_value_before_it() {
    let (report, _desktop) = run(concat!(
        "x := {A: 1}\n",
        "y := x\n",
        "    .A\n",
        "FileAppend(y, \"*\")\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

/// The sign of a value may begin the line that continues the one before.
///
/// Measured on the binary of the v2.0.32: the `StrLen(` of one line and the
/// `-2` of the next is one argument, -2, and the tree refused it with
/// "Missing operand.".
#[test]
fn the_sign_may_begin_the_line_that_continues_the_one_before() {
    let (report, _desktop) = run(concat!(
        "x := StrLen(\n",
        "    -2\n",
        ")\n",
        "FileAppend(x, \"*\")\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

/// The comma at the end of a list may be followed by the newlines of the
/// bracket that closes it.
///
/// Measured on the binary of the v2.0.32: the list may be written in more
/// than one line, and the tree refused the `Max(` of one line, the `1, 2,` of
/// the next and the `)` of the third with "Unexpected ")"".
#[test]
fn the_comma_at_the_end_of_a_list_may_be_followed_by_the_newlines() {
    let (report, _desktop) = run(concat!(
        "x := Max(\n",
        "    1, 2,\n",
        ")\n",
        "FileAppend(x, \"*\")\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

/// The name of a class of the dialect is a value of a concatenation.
///
/// Measured on the binary of the v2.0.32: the `"a" class` is one text, and
/// the tree refused it with "This Class cannot be used as an output
/// variable.".
#[test]
fn the_name_of_a_class_may_be_a_value_of_a_concatenation() {
    // The load is what is measured here: the run of the text and of the class
    // is a failure of the runtime, and the reference has it too.
    parse("x := \"a\" Class\n").expect("parses");
}

/// The body of a hotkey is a scope of its own, and a variable of it may be
/// named after a class of the dialect.
///
/// Measured on the binary of the v2.0.32: the `F2:: { class := 1 }` is a
/// script that loads, and the tree refused it with "This Class cannot be
/// used as an output variable." because the assignment of it was read as one
/// of the scope of the script.
#[test]
fn the_body_of_a_hotkey_may_hold_a_variable_named_after_a_class() {
    let (report, _desktop) = run(concat!(
        "F2:: {\n",
        "    class := 1\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
}

/// The list of the parameters of a declaration may be written in more than
/// one line.
///
/// Measured on the binary of the v2.0.32: the `F(a,` of one line and the
/// `b := 0)` of the next is one declaration with two parameters, and the tree
/// refused it with "Missing parameter name.".
#[test]
fn the_list_of_the_parameters_may_be_written_in_more_than_one_line() {
    let (report, _desktop) = run(concat!(
        "F(a,\n",
        "b := 0) {\n",
        "    return a + b\n",
        "}\n",
        "FileAppend(F(1), \"*\")\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

/// The name that the script builds may carry an index.
///
/// Measured on the binary of the v2.0.32: the `x.%a[1]%` is the variable
/// whose name is the "a[1]", and the tree refused it with "Missing ending
/// "%"" because the bracket was not a part of the operand of the percent
/// sign.
#[test]
fn the_name_that_the_script_builds_may_carry_an_index() {
    // The load is what is measured here: the run of a variable whose name
    // carries a bracket is not carried by the tree yet.
    parse(concat!(
        "a := [\"one\", \"two\"]\n",
        "x.%a[1]% := \"found\"\n",
    ))
    .expect("parses");
}

/// The switch of the v2 may carry the sense of the case of the comparison.
///
/// Measured on the binary of the v2.0.32: the second parameter of the switch
/// is the sense of the case: the `switch x, "On"` compares the texts with
/// the case of them, and the `switch x, "ZZZ"` is refused with "Parameter #2
/// invalid.".
#[test]
fn the_switch_may_carry_the_sense_of_the_case() {
    let (report, _desktop) = run(concat!(
        "x := \"On\"\n",
        "r := \"none\"\n",
        "switch x, \"On\" {\n",
        "    case \"on\":\n",
        "        r := \"on\"\n",
        "    default:\n",
        "        r := \"default\"\n",
        "}\n",
        "FileAppend(r, \"*\")\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    // The case of the texts is the one that the sense asks for: the "On" and
    // the "on" are not the same text.
    assert_eq!(report.output, vec!["default"]);
    // And the sense that is not one of them is refused at the run.
    let (report, _desktop) = run(concat!(
        "x := \"On\"\n",
        "switch x, \"ZZZ\" {\n",
        "    default:\n",
        "}\n",
    ));
    assert!(!report.ok, "the sense is refused");
    let error = report.error.as_deref().unwrap_or("");
    assert_eq!(error, "Parameter #2 invalid.");
}

/// The body of a hotstring may carry the blocks.
///
/// Measured on the binary of the v2.0.32: the body of a hotstring is a block
/// of statements, and the `try` of it is a block too. The tree cut the body
/// at the first `}` of it and read the rest of it as statements of the
/// script.
#[test]
fn the_body_of_a_hotstring_may_carry_the_blocks() {
    // The load is what is measured here: the runtime has no dispatcher of a
    // hotstring yet.
    parse(concat!(
        "::calc::{\n",
        "    try {\n",
        "    } catch {\n",
        "    }\n",
        "}\n",
    ))
    .expect("parses");
}

/// The function that the script writes inside another one hides the function
/// of the machine of the same name.
///
/// Measured on the binary of the v2.0.32: the `Log(m, l := "INFO")` of a
/// function and the `Log("a", "INFO")` of another one that is inside it are
/// one call with two parameters, and the tree refused the script with "Too
/// many parameters passed to function." because the call went to the
/// builtin.
#[test]
fn the_function_that_is_written_inside_another_one_hides_the_builtin() {
    let (report, _desktop) = run(concat!(
        "F() {\n",
        "    Log(m, l := \"INFO\") {\n",
        "        return l\n",
        "    }\n",
        "    return Log(\"a\", \"INFO\")\n",
        "}\n",
        "FileAppend(F(), \"*\")\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["INFO"]);
}

#[test]
fn the_hotstring_of_the_v2_reads_the_options_of_the_hotstring_directive() {
    // Measured on the binary of v2.0.19: `#Hotstring X0` turns
    // the code mode of the hotstrings off, so a replacement that ends with `{`
    // is text and not a block, and the statements after the definition are the
    // statements of the script.
    let (report, _desktop) = run(concat!(
        "#Hotstring X0\n",
        ":T:brace::{\n",
        "\n",
        "MsgBox \"after\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["after"]);
    // And `#Hotstring X` turns the code mode back on: the block is the action.
    let (report, _desktop) = run(concat!(
        "#Hotstring X0\n",
        "#Hotstring X\n",
        "::block::{\n",
        "    MsgBox \"in the block\"\n",
        "}\n",
        "MsgBox \"after\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["after"]);
}

#[test]
fn the_value_that_may_be_absent_is_the_absent_value_when_it_has_none() {
    // Measured on the binary of v2.0.19: `(v?)` is the value of
    // `v` when it has one and the absent value when it has none; a parameter
    // `v?` is left without a value when the call does not give it; a property
    // of an object literal is left out when its value is the absent value; and
    // `x := (v?)` leaves the variable without a value.
    let (report, _desktop) = run(concat!(
        "F(v?) {\n",
        "    MsgBox \"isset=\" IsSet(v)\n",
        "}\n",
        "F()\n",
        "F(7)\n",
        "LiteralOf(v?) => {a: 1, b: (v?)}\n",
        "MsgBox \"props0=\" ObjOwnPropCount(LiteralOf()) \" has_b0=\" LiteralOf().HasProp(\"b\")\n",
        "MsgBox \"props1=\" ObjOwnPropCount(LiteralOf(5)) \" b1=\" LiteralOf(5).b\n",
        "G(v?) {\n",
        "    x := (v?)\n",
        "    MsgBox \"g=\" IsSet(x)\n",
        "}\n",
        "G()\n",
        "G(1)\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            "isset=0",
            "isset=1",
            "props0=1 has_b0=0",
            "props1=2 b1=5",
            "g=0",
            "g=1",
        ]
    );
}

#[test]
fn the_absent_value_gives_a_parameter_no_value() {
    // Measured on the binary of v2.0.19: an argument that is
    // `unset` gives its parameter no value at all -- a required parameter is
    // missing, a parameter with a default takes the default, a property of an
    // object literal is left out, and an assignment leaves the variable
    // without a value.
    let (report, _desktop) = run(concat!(
        "F(a) {\n",
        "    MsgBox \"f=\" a\n",
        "}\n",
        "try {\n",
        "    F(unset)\n",
        "} catch as e {\n",
        "    MsgBox Type(e) \": \" e.Message\n",
        "}\n",
        "G(a := 7) {\n",
        "    MsgBox \"g=\" IsSet(a) \" \" a\n",
        "}\n",
        "G(unset)\n",
        "o := {a: unset}\n",
        "MsgBox \"obj=\" ObjOwnPropCount(o)\n",
        "x := unset\n",
        "MsgBox \"x=\" IsSet(x)\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            "Error: Missing a required parameter.",
            "g=1 7",
            "obj=0",
            "x=0"
        ]
    );
}

#[test]
fn the_body_of_a_property_may_begin_on_the_line_after_its_name() {
    // Measured on the binary of v2.0.19: the name of a property,
    // the brace of its body and the brace of its getter may each be a line of
    // their own.
    let (report, _desktop) = run(concat!(
        "class B {\n",
        "    c\n",
        "    {\n",
        "        get\n",
        "        {\n",
        "            return 1\n",
        "        }\n",
        "    }\n",
        "}\n",
        "MsgBox B().c\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_increment_of_a_property_reads_the_object_that_holds_it_once() {
    // Measured on the binary of v2.0.19: `a.b.c.d++` calls the
    // getter of `c` one time, and the write goes to the value that was read.
    let (report, _desktop) = run(concat!(
        "class Inner {\n",
        "    d := 5\n",
        "}\n",
        "class Outer {\n",
        "    _i := Inner()\n",
        "    n := 0\n",
        "    c {\n",
        "        get {\n",
        "            this.n += 1\n",
        "            return this._i\n",
        "        }\n",
        "    }\n",
        "}\n",
        "o := Outer()\n",
        "o.c.d++\n",
        "MsgBox o._i.d \" \" o.n\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["6 1"]);
}

#[test]
fn the_parse_loop_reads_a_comma_separated_text_the_way_a_csv_file_is() {
    // Measured on the binary of v2.0.19: the delimiters of the
    // text "csv" read the text as a comma-separated file. A field that begins
    // with a quote runs to the closing quote, a pair of quotes in it is one
    // quote, the text between the closing quote and the comma is dropped, the
    // quotes are not part of the field, and an empty text has no field at all
    // while a comma at the end of it is the empty field after it.
    let (report, _desktop) = run(concat!(
        "x := \"`\"first field`\",SecondField,`\"the word `\"`\"special`\"`\" is quoted literally`\",,`\"last field, has literal comma`\"\"\n",
        "z := \"\"\n",
        "Loop Parse x, \"csv\"\n",
        "    z .= A_LoopField\n",
        "MsgBox z\n",
        "s := \"\"\n",
        "Loop Parse \"\", \",\"\n",
        "    s .= \"x\"\n",
        "MsgBox \"empty=[\" s \"]\"\n",
        "t := \"\"\n",
        "Loop Parse \"a,,b\", \",\"\n",
        "    t .= \"[\" A_LoopField \"]\"\n",
        "MsgBox t\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            "first fieldSecondFieldthe word \"special\" is quoted literallylast field, has literal comma",
            "empty=[]",
            "[a][][b]",
        ]
    );
}

#[test]
fn a_function_that_reads_a_local_is_a_closure() {
    // Measured on the binary of v2.0.19: the arrow that reads a
    // local variable of the function that has it is a `Closure` -- and it is a
    // `Func` and an `Object` as well -- while the arrow that reads no such
    // variable, and the same arrow at the top of the script, is a `Func`.
    let (report, _desktop) = run(concat!(
        "f() => (x := 1, (*) => x)\n",
        "g() => ((*) => 9)\n",
        "h := (*) => 9\n",
        "MsgBox Type(f()) \" \" (f() is Closure) \" \" (f() is Func) \" \" (f() is Object)\n",
        "MsgBox Type(g()) \" \" (g() is Closure) \" \" (g() is Func)\n",
        "MsgBox Type(h) \" \" (h is Closure) \" \" (h is Func)\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Closure 1 1 1", "Func 0 1", "Func 0 1"]);
}

#[test]
fn the_index_of_a_turn_is_set_before_the_condition_of_the_while() {
    // Measured on the binary of v2.0.19: the condition of the
    // first turn of a `while` reads `A_Index` as one, and the body reads the
    // same number -- the index is set before the condition is read -- and the
    // index the loop had before it is given back when it ends.
    let (report, _desktop) = run(concat!(
        "x := [], t := \"\"\n",
        "while (x.Push(A_Index), t .= A_Index, A_Index <= 3)\n",
        "    continue\n",
        "MsgBox x.Length \" \" t\n",
        "MsgBox A_Index\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["4 1234", "0"]);
}

#[test]
fn the_equal_sign_compares_texts_as_texts_and_numbers_as_numbers() {
    // Measured on the binary of v2.0.19: two texts are compared
    // as texts -- "10" and "10.0" are not the same text -- and a text is
    // compared with a number as a number when it holds one, the hex of the
    // `0x` prefix included: `513 = "0x201"` and `"0x10" = 16` are one, and
    // `"0x10" = "16"` is zero. The `==` is the same rule with the case kept,
    // and two arrays are the same array and nothing else.
    let (report, _desktop) = run(concat!(
        "MsgBox (513 = \"0x201\") \" \" (\"0x10\" = 16) \" \" (\"10\" = \"10.0\") \" \" (\"010\" = \"10\") \" \" (\"0x10\" = \"16\") \" \" (\"abc\" = 0) \" \" (\"\" = 0)\n",
        "MsgBox (1 == \"1\") \" \" (1.0 == \"1\") \" \" (\"a\" == \"A\") \" \" (\"0x10\" == 16)\n",
        "x := [1, 2], y := [1, 2]\n",
        "MsgBox (x = y) \" \" (x = x) \" \" ([1, \"a\"] = [1, \"A\"])\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1 1 0 0 0 0 0", "1 1 0 1", "0 1 0"]);
}

#[test]
fn the_prototype_of_a_primitive_supplies_the_enumerator() {
    // Measured on the binary of v2.0.19: a script puts an
    // `__Enum` into the prototype of a class with
    // `({}.DefineProp)(String.Prototype, "__Enum", {Call: (this, n) => ...})`,
    // and the `for` over a text asks it -- the stub that answers a function
    // that gives no turn leaves the loop with no turn at all -- and the
    // `DeleteProp` takes it away.
    let (report, _desktop) = run(concat!(
        "({}.DefineProp)(String.Prototype, \"__Enum\", {Call: (this, n) => ((&c) => false)})\n",
        "n := 0\n",
        "for c in \"abc\"\n",
        "    n++\n",
        "MsgBox \"turns=\" n\n",
        "({}.DeleteProp)(String.Prototype, \"__Enum\")\n",
        "MsgBox \"deleted=\" (!String.Prototype.HasOwnProp(\"__Enum\"))\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["turns=0", "deleted=1"]);
}

#[test]
fn the_reference_of_a_variable_is_a_class_of_the_dialect() {
    // Measured on the binary of v2.0.19: `VarRef` is a class of
    // the dialect -- `Type(VarRef)` is Class and `IsObject(VarRef)` is one --
    // and a value that is a reference is one of it: `r := &x` then
    // `r is VarRef` is one, while a value that is not a reference is zero and
    // every value is of the class `Any`.
    let (report, _desktop) = run(concat!(
        "MsgBox Type(VarRef) \" \" IsObject(VarRef)\n",
        "x := 5\n",
        "r := &x\n",
        "MsgBox (r is VarRef) \" \" (x is VarRef) \" \" (x is Any)\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Class 1", "1 0 1"]);
}

#[test]
fn the_value_of_an_expression_is_called_by_the_parenthesis_after_it() {
    // Measured on the binary of v2.0.19: a parenthesis that is
    // not spaced after a value is the list of the arguments of the call of the
    // value. A function is called through itself, an object through its `Call`
    // method (a property that holds a function counts), and a value that is
    // neither is "This value of type "Integer" has no method named "Call"."
    // of the class `MethodError`.
    let (report, _desktop) = run(concat!(
        "f := (a, b) => a + b\n",
        "MsgBox (f)(2, 3)\n",
        "o := {Call: (this, x) => \"called \" x}\n",
        "MsgBox (o)(7)\n",
        "MsgBox ([10, 20])[1]\n",
        "m := Map(\"a\", (v) => v \"!\")\n",
        "MsgBox (m[\"a\"])(\"hi\")\n",
        "try {\n",
        "    x := (5)(1)\n",
        "} catch as e {\n",
        "    MsgBox Type(e) \": \" e.Message\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            "5",
            "called 7",
            "10",
            "hi!",
            "MethodError: This value of type \"Integer\" has no method named \"Call\".",
        ]
    );
}

#[test]
fn the_truth_of_a_text_is_the_truth_of_the_number_it_holds() {
    // Measured on the binary of v2.0.19: a text that is a number
    // is false when the number is zero -- "0", "00", "0x0", "0.0" and " 0" --
    // and a text that is not a number is true when it is not empty.
    let (report, _desktop) = run(concat!(
        "MsgBox (\"00\" || 2) \":\" (\"0x0\" || 2) \":\" (\"0x10\" || 2)\n",
        "MsgBox (\"\" || 2) \":\" (!\"0x0\") \":\" (\"0.0\" || 2) \":\" (\"abc\" || 2)\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2:2:0x10", "2:1:2:abc"]);
}

#[test]
fn the_goto_of_a_computed_label_is_resolved_when_it_runs() {
    // Measured on the binary of v2.0.19: `goto(name)` resolves
    // the label when the jump runs, among the labels of the blocks that
    // enclose it; a label that is not there is an Error with "Label not found
    // in current scope." and the name in the `Extra`; a label of a block that
    // does not enclose the jump is "A Goto must not jump into a block that
    // doesn't enclose it."; and a jump that leaves a `finally` block is
    // "Jumps cannot exit a FINALLY block.".
    let (report, _desktop) = run(concat!(
        "Jump(name) {\n",
        "    result := \"\"\n",
        "    if (name != \"\")\n",
        "        goto(name)\n",
        "    result .= \"a\"\n",
        "first:\n",
        "    result .= \"b\"\n",
        "Second:\n",
        "    result .= \"c\"\n",
        "    return result\n",
        "}\n",
        "MsgBox Jump(\"\")\n",
        "MsgBox Jump(\"first\")\n",
        "MsgBox Jump(\"SECOND\")\n",
        "try {\n",
        "    Jump(\"nosuch\")\n",
        "} catch as e {\n",
        "    MsgBox e.Message \"|\" e.Extra\n",
        "}\n",
        "Into(name) {\n",
        "    if (name != \"\")\n",
        "        goto(name)\n",
        "    if (name = \"\") {\n",
        "        inside:\n",
        "        return 1\n",
        "    }\n",
        "    return 0\n",
        "}\n",
        "try {\n",
        "    Into(\"inside\")\n",
        "} catch as e {\n",
        "    MsgBox e.Message\n",
        "}\n",
        "JumpFinally(name, leave := false) {\n",
        "    try {\n",
        "        goto(name)\n",
        "    } finally {\n",
        "        if leave\n",
        "            goto(name)\n",
        "    }\n",
        "done:\n",
        "    return 1\n",
        "}\n",
        "try {\n",
        "    JumpFinally(\"done\", true)\n",
        "} catch as e {\n",
        "    MsgBox e.Message\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            "abc",
            "bc",
            "c",
            "Label not found in current scope.|nosuch",
            "A Goto must not jump into a block that doesn't enclose it.",
            "Jumps cannot exit a FINALLY block.",
        ]
    );
}

#[test]
fn the_break_names_a_label_of_a_loop() {
    // Measured on the binary of v2.0.19: the break and the
    // continue may name a label that is written before a loop, and the level
    // is the number of the loops between the two. A label that no loop
    // follows is "Parameter #1 invalid.", and a label that is not there is
    // "Label not found in current scope." with the name in it.
    let (report, _desktop) = run(concat!(
        "outer:\n",
        "Loop 2\n",
        "{\n",
        "    Loop 3\n",
        "    {\n",
        "        if (A_Index == 2)\n",
        "            break outer\n",
        "    }\n",
        "    MsgBox \"the body of the outer loop is not run twice\"\n",
        "}\n",
        "MsgBox \"after\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["after"]);
    let failure = parse("Loop 3\n{\n    break nosuch\n}\n").expect_err("refused");
    assert_eq!(
        failure.message,
        "Label not found in current scope.\n     Specifically: nosuch"
    );
    let failure =
        parse("notloop:\nMsgBox \"x\"\nLoop 3\n{\n    break notloop\n}\n").expect_err("refused");
    assert_eq!(failure.message, "Parameter #1 invalid.");
    let failure = parse("l:\nLoop 2\n{\n    MsgBox \"x\"\n}\nbreak l\n").expect_err("refused");
    assert_eq!(
        failure.message,
        "Break/Continue must be enclosed by a Loop."
    );
}

#[test]
fn the_item_of_a_class_takes_more_than_one_parameter() {
    // Measured on the binary of v2.0.19: `obj[a, b]` is the
    // item of `obj` with two parameters, and a class of the script sees both
    // in its `__Item` -- the getter and the setter, the setter with the value
    // after them. A map and an array take one and answer "Too many parameters
    // passed to function." with the accessor in the `Extra`.
    let (report, _desktop) = run(concat!(
        "class Box {\n",
        "    __Item[a, b] {\n",
        "        get => a \"|\" b\n",
        "        set => MsgBox(\"set \" a \"|\" b \"=\" value)\n",
        "    }\n",
        "}\n",
        "obj := Box()\n",
        "MsgBox obj[1, 2]\n",
        "obj[3, 4] := 5\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1|2", "set 3|4=5"]);
    // A class type takes one parameter, and the failure is of v2.
    let (report, _desktop) = run("m := Map()\nm[\"a\", \"b\"] := 1\n");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Too many parameters passed to function.")
    );
}

#[test]
fn the_number_before_the_point_of_the_concat_assign_is_a_number() {
    // Measured on the binary of v2.0.19: `n.1.="D"` is the
    // property "1" of `n` written by `.=`, and the point of the operator is
    // not the point of the number: the number is 1 and the operator is `.=`.
    let (report, _desktop) = run(concat!(
        "n := {1: \"Y\"}\n",
        "n.1 .= \"D\"\n",
        "MsgBox n.1\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["YD"]);
}

#[test]
fn the_hole_of_a_loop_parameter_keeps_its_place() {
    // Measured on the binary of v2.0.19: `Loop Parse "hello", ,
    // "l"` leaves the delimiters out and the loop walks the letters, and the
    // hole is the comma that follows a comma.
    let (report, _desktop) = run(concat!(
        "Loop Parse \"hello\", , \"l\" {\n",
        "    MsgBox A_LoopField\n",
        "}\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["h", "e", "o"]);
}

#[test]
fn the_array_literal_takes_the_items_of_a_spread() {
    // Measured on the binary of v2.0.19: the last item of an
    // array literal may carry a star, and the items of the value it names take
    // its place -- `[a*]` is the items of `a`, the way a `for` loop walks
    // them: an array gives its items, a map its keys, and a class with an
    // `__Enum` its own. The star in the middle is "Missing operand." with the
    // text from the star, and so is a star after a trailing comma.
    let (report, _desktop) = run(concat!(
        "a := [1, 2]\n",
        "b := [a*]\n",
        "c := [0, a*]\n",
        "m := Map(\"k\", 1)\n",
        "d := [m*]\n",
        "class Empty {\n",
        "    static __Enum(n) => (&c) => false\n",
        "}\n",
        "none := [Empty*]\n",
        "MsgBox b.Length \":\" c.Length \":\" d.Length \":\" none.Length\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2:3:1:0"]);
    // The star in the middle is the refusal of the binary, and it is named
    // from the star.
    let failure = parse("a := [1, 2]\nb := [a*, 9]\n").expect_err("refused");
    assert_eq!(
        failure.message,
        "Missing operand.\n     Specifically: *, 9]"
    );
}

#[test]
fn the_hotif_directive_sets_the_condition_of_the_hotkeys() {
    // Measured on the binary of v2.0.19: `#HotIf` takes an
    // expression, and the hotkeys that follow it carry the condition until
    // the next `#HotIf`; the empty directive clears it. The forms this
    // runtime carries are the four window questions with a literal title, and
    // `#HotIfTimeout` is accepted (the question of this runtime answers at
    // once, so the timeout changes nothing).
    let (report, _desktop) = run(concat!(
        "#HotIf WinActive(\"a window\")\n",
        "F1::MsgBox \"one\"\n",
        "#HotIf WinExist(\"a window\")\n",
        "F2::MsgBox \"two\"\n",
        "#HotIf\n",
        "F3::MsgBox \"three\"\n",
        "#HotIfTimeout 100\n",
        "MsgBox \"after\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["after"]);
    // An expression is a condition: the `#HotIf 1` is carried, and the
    // hotkey is registered with it.
    let (report, _desktop) = run("#HotIf 1\nF1::MsgBox \"x\"\n");
    assert!(report.ok, "{:?}", report.error);
    // A condition that is not one expression is refused rather than
    // registered without its condition.
    let (report, _desktop) = run("#HotIf 1 +\nF1::MsgBox \"x\"\n");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some(
            "The condition of #HotIf is not one valid expression."
        )
    );
}

#[test]
fn the_exitapp_of_the_v2_takes_its_parentheses() {
    // Measured on the binary of v2.0.19: `ExitApp()` is a call
    // of the function with no code, and `ExitApp(3)` one that ends the script
    // with the three: the empty pair is not the syntax error of a group.
    let (report, _desktop) = run("MsgBox \"before\"\nExitApp()\nMsgBox \"after\"\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["before"]);
    assert_eq!(report.exit_code, Some(0));
    let (report, _desktop) = run("ExitApp(3)\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.exit_code, Some(3));
}

#[test]
fn the_hotstring_of_the_v2_is_a_line_of_raw_text() {
    // Measured on the binary of v2.0.19: `::btw::by the way` is
    // a hotstring, the text of it may hold the two colons again (`::1:::2`),
    // and the backtick escapes the colon (`::5`:::6`). The line without the
    // second pair of colons is "Invalid hotkey." with the line itself.
    let (report, _desktop) = run(concat!(
        "::btw::by the way\n",
        "::1:::2\n",
        "::5`:::6\n",
        ":*:st::starred\n",
        ":?:qm::question\n",
        ":*?:both::both of them\n",
        ":*:i\"q::I \"quote\"\n",
        ":?:; btu::; but\n",
        "::text1::\n",
        "(\n",
        "line one\n",
        "line two\n",
        ")\n",
        "::mf4::{\n",
        "    MsgBox \"x\"\n",
        "}\n",
        "::mf5::\n",
        "{\n",
        "    MsgBox \"y\"\n",
        "}\n",
        "MsgBox \"after\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["after"]);
    let error = parse("::btw\n").expect_err("does not parse");
    assert_eq!(error.message, "Invalid hotkey.\n     Specifically: ::btw");
    // Measured on the binary of v2.0.19: the line without the
    // second pair of colons and the one with an empty trigger are
    // "Invalid hotkey." with the line, and a replacement that is empty and has
    // no continuation section is "Hotkey or hotstring is missing its opening
    // brace.".
    let error = parse(":btw::x\n").expect_err("does not parse");
    assert_eq!(error.message, "Invalid hotkey.\n     Specifically: :btw::x");
    let error = parse(":*::x\n").expect_err("does not parse");
    assert_eq!(error.message, "Invalid hotkey.\n     Specifically: :*::x");
    let error = parse("::btw::\n").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Hotkey or hotstring is missing its opening brace."
    );
}

#[test]
fn a_text_that_holds_a_hexadecimal_number_is_a_number() {
    // Measured on the binary of v2.0.19: "0x1" + 0 is 1,
    // "0X1F" + 0 is 31, "-0x10" + 0 is -16, " 0x10 " + 0 is 16, `~"0x1"` is
    // -2, and "0x10" * 1 is 16. A text that holds a whole decimal number adds
    // as a whole number while "1e3" is a float. "0x" and "0xzz" hold no
    // number and are the "Expected a Number but got a String." of any other
    // text.
    let (report, _desktop) = run(concat!(
        "a := \"0x1\"\n",
        "b := \"0X1F\"\n",
        "c := \"-0x10\"\n",
        "d := \" 0x10 \"\n",
        "e := ~a\n",
        "f := b + 0\n",
        "g := c + 0\n",
        "h := d + 0\n",
        "i := a * 2\n",
        "t := \"2\"\n",
        "j := t + 1\n",
        "u := \"1e3\"\n",
        "k := u + 0\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    for (name, number) in [
        ("e", -2),
        ("f", 31),
        ("g", -16),
        ("h", 16),
        ("i", 2),
        ("j", 3),
    ] {
        assert_eq!(
            report.variables.get(name),
            Some(&Value::Int(number)),
            "{name}"
        );
    }
    assert_eq!(report.variables.get("k"), Some(&Value::Float(1000.0)));
}

#[test]
fn a_text_that_is_not_a_hexadecimal_number_is_not_a_number() {
    // Measured on the binary of v2.0.19: the prefix with no
    // digit and a digit that is not a hexadecimal one are not numbers.
    let (report, _desktop) = run("a := \"0x\"\nb := a + 0");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a Number but got a String.")
    );
    let (report, _desktop) = run("a := \"0xzz\"\nb := a + 0");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a Number but got a String.")
    );
}

#[test]
fn a_loop_may_carry_an_else_that_runs_when_it_took_no_turn() {
    // Measured on the binary of v2.0.19: every loop kind --
    // the counted one, the one without a count, `while`, `for`, `Files` and
    // `Parse` -- may carry an `else`, and the `else` runs exactly when the
    // loop took no turn. The `else` may come after a brace block or after the
    // one statement of the body.
    let (report, _desktop) = run(concat!(
        "seen := \"\"\n",
        "Loop 0 {\n",
        "    seen .= \"L0-body,\"\n",
        "} else {\n",
        "    seen .= \"L0-else,\"\n",
        "}\n",
        "Loop 1 {\n",
        "    seen .= \"L1-body,\"\n",
        "} else {\n",
        "    seen .= \"L1-else,\"\n",
        "}\n",
        "i := 0\n",
        "While (i < 0) {\n",
        "    seen .= \"W-body,\"\n",
        "} else {\n",
        "    seen .= \"W-else,\"\n",
        "}\n",
        "For x in [] {\n",
        "    seen .= \"F-body,\"\n",
        "} else {\n",
        "    seen .= \"F-else,\"\n",
        "}\n",
        "Loop Files \"zzz-nothing-*.txt\" {\n",
        "    seen .= \"LF-body,\"\n",
        "} else {\n",
        "    seen .= \"LF-else,\"\n",
        "}\n",
        "Loop Parse \"\" {\n",
        "    seen .= \"P-body,\"\n",
        "} else {\n",
        "    seen .= \"P-else,\"\n",
        "}\n",
        "Loop 0\n",
        "    seen .= \"S-body,\"\n",
        "else\n",
        "    seen .= \"S-else,\"\n",
        "FileAppend seen, \"*\"\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["L0-else,L1-body,W-else,F-else,LF-else,P-else,S-else,"]
    );
}

#[test]
fn the_optional_parameter_may_be_left_out() {
    // Measured on the binary of v2.0.19: `f(a, b?)` marks `b`
    // optional, and a call that leaves it out leaves it unset -- `IsSet(b)`
    // is zero -- while a call that gives it a value sets it. The method of a
    // class takes the optional parameter the same way.
    let (report, _desktop) = run(concat!(
        "f(a, b?) {\n",
        "    return IsSet(b) ? \"set:\" b : \"unset\"\n",
        "}\n",
        "class C {\n",
        "    m(a?, b?) {\n",
        "        return IsSet(a) \"|\" IsSet(b)\n",
        "    }\n",
        "}\n",
        "FileAppend(f(1) \"|\" f(1, 2) \"|\" C().m() \"|\" C().m(1, 2), \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["unset|set:2|0|0|1|1"]);
}

#[test]
fn a_reading_of_the_optional_parameter_that_is_left_out_is_unset() {
    // Measured on the binary of v2.0.19: a read of the
    // parameter that the call left out fails the way a read of an unset
    // local fails, and no warning is raised for it.
    let (report, _desktop) = run(concat!(
        "f(a?) {\n",
        "    try {\n",
        "        return a\n",
        "    } catch as e {\n",
        "        return Type(e) \": \" e.Message\n",
        "    }\n",
        "}\n",
        "FileAppend(f(), \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["UnsetError: This local variable has not been assigned a value."]
    );
}

#[test]
fn the_optional_parameter_has_the_refusals_of_v2() {
    // Measured on the binary of v2.0.19: the optional one may
    // not carry a default -- `f(a, b? := 2)` is "Missing comma" -- and a
    // required one may not come after it -- `f(a?, b)` is "Parameter default
    // required.", named after the required parameter.
    let error = parse("f(a, b? := 2) {\n    return b\n}\n").expect_err("does not parse");
    assert!(
        error.message.starts_with("Missing comma"),
        "{:?}",
        error.message
    );
    let error = parse("f(a?, b) {\n    return b\n}\n").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter default required.\n     Specifically: b"
    );
    // The arity of the call counts the required parameters alone.
    let error = parse("f(a, b?) {\n    return b\n}\nf()").expect_err("does not parse");
    assert!(
        error.message.starts_with("Too few parameters passed to function."),
        "{:?}",
        error.message
    );
}

#[test]
fn the_parameter_that_is_not_there_takes_its_default() {
    // Measured on the binary of v2.0.19: a parameter that
    // has a default takes it, and one that has none is a missing one.
    let (report, _desktop) =
        run("WithDefault(a, b := \"the default\") {\n    MsgBox b\n}\nWithDefault (\"a\"),,");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["the default"]);
}

#[test]
fn the_parameter_that_is_not_there_keeps_its_place() {
    // Measured on the binary of v2.0.19: the place of the
    // parameter that is not there is kept, and the number of the arguments
    // is checked before the parameters are bound.
    let (report, _desktop) = run("Two(a, b) {\n    MsgBox a\n}\nTwo (\"a\"),, \"b\"");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Too many parameters passed to function.\n     Specifically: Two")
    );
}

#[test]
fn the_value_that_is_not_there_in_a_group_is_refused_at_the_load() {
    // Measured on the binary of v2.0.19: the value that is
    // not there in the list of a group is a "Syntax error.", and the comma
    // that follows it is named.
    let error = parse("x := (\"a\",, \"c\")").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: , \"c\")");
}

#[test]
fn the_expression_of_a_statement_goes_on() {
    // Measured on the binary of v2.0.19: the expression of a
    // statement begins with the call, and the operators that follow it are
    // a part of it. The value of it is thrown away.
    let (report, _desktop) = run("f() {\n    MsgBox \"[f]\"\n    return 3\n}\nf() + 1");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["[f]"]);
}

#[test]
fn the_concatenation_of_a_statement_is_a_part_of_it() {
    // Measured on the binary of v2.0.19: the concatenation of
    // v2 is a part of the expression of a statement, and it wants a String.
    let (report, _desktop) = run(
        "class Thing {\n    Method() {\n        return \"m\"\n    }\n}\nt := Thing()\nf() {\n    return t.Method\n}\nf() \"a\"",
    );
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Expected a String but got a Func.")
    );
}

#[test]
fn the_comma_ends_the_expression_of_a_statement() {
    // Measured on the binary of v2.0.19: the expression of a
    // statement ends at the comma, and the rest of the line is the list of
    // v1: it is evaluated, and its value is thrown away.
    let (report, _desktop) =
        run("f() {\n    MsgBox \"[f]\"\n}\ng() {\n    MsgBox \"[g]\"\n}\nf(), g()");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["[f]", "[g]"]);
}

#[test]
fn the_assignment_that_is_followed_by_a_comma_keeps_its_value() {
    // Measured on the binary of v2.0.19: the expression of the
    // assignment ends at the comma, and the value of it is the one of the
    // expression.
    let (report, _desktop) = run("x := 1, 2\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_member_that_has_parentheses_is_an_expression() {
    // Measured on the binary of v2.0.19: the member that has
    // parentheses is an expression, and the comma ends it: the rest of the
    // line is the list of v1, and it is evaluated.
    let (report, _desktop) = run(
        "class Thing {\n    Method() {\n        MsgBox \"[m]\"\n    }\n}\nt := Thing()\ng() {\n    MsgBox \"[g]\"\n}\nt.Method(), g()",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["[m]", "[g]"]);
}

#[test]
fn the_member_that_is_a_command_carries_its_arguments() {
    // Measured on the binary of v2.0.19: the member that has
    // no parentheses is a command of v2, and the rest of the line is the
    // list of its arguments.
    let (report, _desktop) =
        run("class Thing {\n    Method(a) {\n        MsgBox a\n    }\n}\nt := Thing()\nt.Method 7");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["7"]);
}

#[test]
fn the_parameters_of_the_command_of_a_member_are_separated_by_the_comma() {
    // Measured on the binary of v2.0.19: the space is the
    // concatenation of v2, and the comma is what separates the arguments.
    let (report, _desktop) = run(
        "class Thing {\n    Method(a, b) {\n        MsgBox a \",\" b\n    }\n}\nt := Thing()\nt.Method 7, 8",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["7,8"]);
}

#[test]
fn the_parameter_that_is_not_there_in_a_method_is_a_missing_one() {
    // Measured on the binary of v2.0.19: the parameter that
    // the command does not give a value to keeps its place, and the method
    // is refused with "Missing a required parameter.".
    let (report, _desktop) = run(
        "class Thing {\n    Method(a, b) {\n        MsgBox a\n    }\n}\nt := Thing()\nt.Method , 7",
    );
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Missing a required parameter.")
    );
}

#[test]
fn the_line_that_ends_with_a_comma_continues_on_the_next_one() {
    // Measured on the binary of v2.0.19: the line that ends
    // with a comma continues on the next one, and the list of v1 goes on
    // with it.
    let (report, _desktop) = run("x := 1,\n2\nMsgBox x");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_number_of_the_arguments_is_checked_before_the_values() {
    // Measured on the binary of v2.0.19: the number of the
    // arguments of a call is checked before the arguments are evaluated: v2
    // refuses the script when it loads, and the refusal of the arity comes
    // before the failure of an argument.
    let error = parse("One(a) {\n    MsgBox a\n}\nOne(\"a\", nothingHere)").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Too many parameters passed to function.\n     Specifically: One"
    );
}

#[test]
fn the_number_of_the_arguments_of_a_method_is_checked_before_the_values() {
    // Measured on the binary of v2.0.19: the number of the
    // arguments of a method is checked before the arguments are evaluated,
    // and the failure carries the name of the method in the \"Extra\".
    let (report, _desktop) = run(
        "class Thing {\n    Method(a) {\n        MsgBox a\n    }\n}\nt := Thing()\nt.Method(\"a\", nothingHere)",
    );
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Too many parameters passed to function.")
    );
}

#[test]
fn the_built_in_that_takes_the_parameter_that_is_not_there() {
    // Measured on the binary of v2.0.19: the parameter that
    // is not there keeps its place in the list of the arguments of a call,
    // and the built-in takes it for the value that is not there.
    let (report, _desktop) = run("FileAppend Format(\"{}{}\", 1,, 2), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_built_in_with_the_parameter_that_is_not_there_at_the_end() {
    // Measured on the binary of v2.0.19: the parameter that
    // is not there and that is the last one is dropped: the commas at the
    // end of the list add nothing.
    let (report, _desktop) = run("FileAppend Format(\"{}{}\", 1,), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1{}"]);
}

#[test]
fn the_built_in_with_the_parameter_that_is_not_there_alone_is_refused() {
    // Measured on the binary of v2.0.19: the parameter that
    // is not there and that is the last one is dropped, and the built-in
    // that wants one is refused with the number of the arguments.
    let error = parse("FileAppend StrLen(,), \"*\"").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Too few parameters passed to function.\n     Specifically: StrLen"
    );
}

#[test]
fn the_comma_at_the_end_of_the_list_of_the_arguments_adds_nothing_in_the_v2() {
    // Measured on the binary of AutoHotkey v2.0.19: the comma
    // at the end of the list of the arguments adds nothing: the `Add(1,)` is
    // a call of one argument. The v1 counts it, and refuses the call when
    // the list is full.
    let (report, _desktop) = run("Add(a) {\n  return a\n}\nFileAppend Add(1,), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1".to_string()]);
}

#[test]
fn the_object_literal_that_is_written_in_more_than_one_line() {
    // Measured on the binary of v2.0.19: the literal of an
    // object may be written in more than one line: the property may be on
    // the line that follows the brace that opens it, and the brace that
    // closes it may be on a line of its own.
    let (report, _desktop) = run("x := {a: 1\n}\nFileAppend x.a, \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    let (report, _desktop) = run("x := {a: 1\n}\nFileAppend x.a, \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn the_block_that_opens_a_line_runs_its_statements() {
    // Measured on the binary of v2.0.19: the brace that opens
    // a line is a block of its own, and the statements between the braces
    // run.
    let (report, _desktop) = run("{\nx := 1\n}\nFileAppend x, \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    // The content may be on the line of the brace that opens it.
    let (report, _desktop) = run("{x := 2\n}\nFileAppend x, \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_member_of_a_number_is_read_at_the_run() {
    // Measured on the binary of v2.0.19: `1.abc` is the
    // number 1 and the member that follows it. The script loads, and it
    // fails at the run.
    let (report, _desktop) = run("x := 1.abc");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Integer\" has no property named \"abc\".")
    );
}

#[test]
fn the_definition_may_be_written_with_the_fat_arrow() {
    // Measured on the binary of v2.0.19: the body of a
    // definition may be an expression, and the fat arrow is what says so:
    // `f(a) => a + 1` is a function that answers the expression of it.
    let (report, _desktop) = run("f(a) => a + 1\nFileAppend f(1), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
    // The parameters are the ones of a function, and the default of one of
    // them is a default.
    let (report, _desktop) = run("f(a, b := 2) => a + b\nFileAppend f(1), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
    // The method of a class may be written the same way.
    let (report, _desktop) = run("class X {\n    m() => 1\n}\nFileAppend X().m(), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
    // The arrow may be on the line of the parameters or on the one after it.
    let (report, _desktop) = run("f(a)\n=> a + 1\nFileAppend f(1), \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["2"]);
}

#[test]
fn the_case_of_a_switch_may_name_many_values() {
    // Measured on the binary of v2.0.19: a case may name
    // many values, and it matches when the subject of the switch is one of
    // them.
    let (report, _desktop) = run(
        "x := 2\nswitch (x) {\n    case 1, 2:\n        FileAppend \"one_or_two=\", \"*\"\n    default:\n        FileAppend \"other=\", \"*\"\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["one_or_two="]);
    let (report, _desktop) = run(
        "x := 9\nswitch (x) {\n    case 1, 2:\n        FileAppend \"one_or_two=\", \"*\"\n    default:\n        FileAppend \"other=\", \"*\"\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["other="]);
    // The comma at the end of the list adds nothing.
    let (report, _desktop) =
        run("x := 2\nswitch (x) {\n    case 1, 2,:\n        FileAppend \"one_or_two=\", \"*\"\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["one_or_two="]);
}

// -- the words of the v2 ---------------------------------------------------------

#[test]
fn the_reserved_word_refuses_with_the_reason_of_the_word() {
    // Measured on the binary of v2.0.19: the reason belongs
    // to the word, and it is the same in every place where the word is
    // refused.
    let error = parse("if := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "The following reserved word must not be used as a variable name:\n\"if\""
    );
    let error = parse("true := 1").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: true");
    let error = parse("in := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Unexpected reserved word.\n     Specifically: in"
    );
}

#[test]
fn the_value_of_a_reserved_word_refuses_with_the_reason_of_the_word() {
    // Measured on the binary of v2.0.19: the reason is the
    // one of the word, in the value as well as in the name.
    let error = parse("x := if").expect_err("does not parse");
    assert_eq!(
        error.message,
        "The following reserved word must not be used as a variable name:\n\"if\""
    );
    let error = parse("x := and").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: and");
    let error = parse("x := in").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Unexpected reserved word.\n     Specifically: in"
    );
}

#[test]
fn the_name_of_a_function_may_not_be_a_reserved_word() {
    // Measured on the binary of v2.0.19: the name of a
    // function may not be a word that the language reserves, and the
    // refusal names the function.
    let error = parse("true() {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "The following reserved word must not be used as a function name:\n\"true\""
    );
    let error = parse("in() {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "The following reserved word must not be used as a function name:\n\"in\""
    );
    // The words that the v2 does not reserve are names.
    assert!(parse("until() {\n}").is_ok());
    assert!(parse("catch() {\n}").is_ok());
    assert!(parse("exitapp() {\n}").is_ok());
}

#[test]
fn the_words_that_the_v2_does_not_reserve_are_names() {
    // Measured on the binary of v2.0.19: the `elseif`, the
    // `default` and the `extends` are names in v2, and the branch of an `if`
    // is written with two words.
    assert!(parse("elseif := 1").is_ok());
    assert!(parse("default := 1").is_ok());
    assert!(parse("extends := 1").is_ok());
    assert!(parse("class default {\n}").is_ok());
    assert!(parse("class elseif {\n}").is_ok());
    assert!(parse("class extends {\n}").is_ok());
    assert!(parse("if 0 {\n} else if 1 {\n}").is_ok());
}

#[test]
fn the_output_variable_of_an_assignment_may_not_be_a_function() {
    // Measured on the binary of v2.0.19: the name of a
    // function of the machine may not be the output variable of an
    // assignment, and the plain assignment and the compound ones say it in
    // their own words.
    let error = parse("MsgBox := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This Func cannot be used as an output variable.\n     Specifically: MsgBox"
    );
    let error = parse("MsgBox += 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This Func cannot be assigned a value.\n     Specifically: MsgBox"
    );
    let error = parse("exitapp := 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This Func cannot be used as an output variable.\n     Specifically: exitapp"
    );
}

#[test]
fn a_local_may_be_named_after_a_function_of_the_machine() {
    // Measured on the binary of v2.0.19: the refusal is the
    // one of the global scope, and a local one may carry the name.
    assert!(parse("f() {\nMsgBox := 1\n}").is_ok());
    assert!(parse("f() {\nclass := 1\n}").is_ok());
    // The name that is declared global is a global one.
    let error = parse("f() {\nglobal MsgBox\nMsgBox := 1\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This Func cannot be used as an output variable.\n     Specifically: MsgBox"
    );
}

#[test]
fn a_parameter_may_be_named_after_a_function_or_a_class() {
    // Measured on the binary of v2.0.19: `f(exitapp)` and
    // `f(class)` are both of them.
    assert!(parse("f(exitapp) {\n}").is_ok());
    assert!(parse("f(class) {\n}").is_ok());
}

#[test]
fn the_name_of_a_class_may_be_the_name_of_a_function() {
    // Measured on the binary of v2.0.19: `class ExitApp { }`
    // is a class, and the `Class` is a class of the dialect and a class of
    // the script may not have its name.
    assert!(parse("class exitapp {\n}").is_ok());
    let error = parse("class class {\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This class declaration conflicts with an existing Class.\n     Specifically: Class"
    );
}

#[test]
fn the_default_of_a_switch_is_a_name_and_a_colon() {
    // Measured on the binary of v2.0.19: the label of the
    // default case is the name `default` and a colon, and the name alone in
    // the body of the switch is not a label.
    let statements = parse("switch 1 {\ndefault:\n}").expect("parses");
    let Node::Switch { cases, .. } = &statements[0] else {
        panic!("a switch")
    };
    assert_eq!(cases.len(), 1);
    assert!(cases[0].values.is_empty());
    let error = parse("switch 1 {\ndefault\n}").expect_err("does not parse");
    assert_eq!(error.message, "Expected Case/Default.");
}

#[test]
fn a_case_may_name_no_value() {
    // Measured on the binary of v2.0.19: the values of a case
    // may be none, and the colon is what says where the label of the case
    // ends.
    let statements = parse("switch 1 {\ncase:\n}").expect("parses");
    let Node::Switch { cases, .. } = &statements[0] else {
        panic!("a switch")
    };
    assert_eq!(cases.len(), 1);
    assert!(cases[0].values.is_empty());
    let error = parse("switch 1 {\ncase\n}").expect_err("does not parse");
    assert_eq!(error.message, "Missing \":\"\n     Specifically: case");
}

#[test]
fn the_elseif_is_a_name_and_not_a_branch() {
    // Measured on the binary of v2.0.19: the `elseif` is a
    // name in v2, and a script may write to it.
    let (report, desktop) = run("elseif := \"one\"\nSend elseif");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["one"]);
}

#[test]
fn the_default_in_the_body_of_a_case_is_a_statement() {
    // Measured on the binary of v2.0.19: the label of the
    // default case is the name and a colon, and the name alone in the body
    // of a case is a statement.
    let (report, desktop) = run("default := \"one\"\nswitch 1 {\ncase 1:\nSend default\n}");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["one"]);
}

#[test]
fn a_return_that_carries_a_value_outside_a_function_is_refused() {
    // Measured on the binary of v2.0.19: a return that carries
    // a value outside a function is refused at the load, and the reason
    // comes before the one of the expression that it carries.
    let error = parse("return 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Return's parameter should be blank except inside a function."
    );
    assert_eq!(error.line, 1);
}

#[test]
fn a_return_inside_a_function_carries_the_value() {
    // Measured on the binary of v2.0.19: a return inside a
    // function may carry a value.
    let (report, _) = run("f() {\n    return 1\n}\nf()");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn a_break_that_carries_a_parenthesis_is_a_break() {
    // Measured on the binary of v2.0.19: the v2 takes the
    // parenthesis of a break and of a continue, and it ignores what is
    // inside it.
    let (report, _) = run("Loop 2 {\n    break(1)\n}");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn a_break_with_a_parenthesis_outside_a_loop_is_refused() {
    // Measured on the binary of v2.0.19: the refusal of a break
    // that is not inside a loop comes after the parenthesis.
    let error = parse("break()").expect_err("does not parse");
    assert_eq!(error.message, "Break/Continue must be enclosed by a Loop.");
    assert_eq!(error.line, 1);
}

#[test]
fn a_try_that_carries_a_parenthesis_is_refused() {
    // Measured on the binary of v2.0.19: the try takes no
    // parenthesis, and the dialect names the statement that carries it.
    let error = parse("try() {\n}").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: try() {");
    assert_eq!(error.line, 1);
}

#[test]
fn a_finally_that_carries_a_parenthesis_is_refused() {
    // Measured on the binary of v2.0.19: the finally takes no
    // parenthesis, and the dialect names the statement that carries it.
    let error = parse("try {\n}\nfinally()").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: finally()");
    assert_eq!(error.line, 3);
}

#[test]
fn an_else_that_carries_a_parenthesis_is_refused() {
    // Measured on the binary of v2.0.19: the else takes no
    // parenthesis, and the dialect names the statement that carries it.
    let error = parse("if (1) {\n}\nelse()").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: else()");
    assert_eq!(error.line, 3);
}

#[test]
fn a_switch_without_a_subject_is_refused() {
    // Measured on the binary of v2.0.19: the switch needs a
    // subject, and it names itself when there is none.
    let error = parse("switch").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Switch\"");
    assert_eq!(error.line, 1);
}

#[test]
fn a_condition_that_is_empty_is_refused_after_the_body() {
    // Measured on the binary of v2.0.19: the v2 reads the body
    // of the construct before it reads the condition, and the refusal of a
    // condition that is empty comes after the one of the body.
    let error = parse("if()").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"If\"");
    let error = parse("if()\n{\n}").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: )");
}

#[test]
fn a_for_whose_list_is_empty_names_the_text_that_it_read() {
    // Measured on the binary of v2.0.19: the v2 names the text
    // that it read, and the text of the for ends at the parenthesis that
    // closes the list.
    let error = parse("for() {\n}").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: for(");
    let error = parse("for(1) {\n}").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: for(1");
}

#[test]
fn the_values_of_a_case_that_are_empty_name_the_closing_parenthesis() {
    // Measured on the binary of v2.0.19: the text that is
    // named is the closing parenthesis and not the rest of the line.
    let error = parse("switch 1 {\n    case(): x := 1\n}").expect_err("does not parse");
    assert_eq!(error.message, "Syntax error.\n     Specifically: )");
    assert_eq!(error.line, 2);
}

#[test]
fn the_catch_that_has_no_body_names_itself() {
    // Measured on the binary of v2.0.19: the catch needs a
    // body, and the construct that has none says so in its own words.
    let error = parse("try {\n}\ncatch").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Catch\"");
    assert_eq!(error.line, 3);
}

#[test]
fn the_finally_that_has_no_body_names_itself() {
    // Measured on the binary of v2.0.19: the finally needs a
    // body too.
    let error = parse("try {\n}\nfinally").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Finally\"");
    assert_eq!(error.line, 3);
}

#[test]
fn the_else_that_has_no_body_names_itself() {
    // Measured on the binary of v2.0.19: the else needs a
    // body as well.
    let error = parse("if (1) {\n}\nelse").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Else\"");
    assert_eq!(error.line, 3);
}

#[test]
fn the_else_of_a_try_needs_a_catch_before_it() {
    // Measured on the binary of v2.0.19: the else of a try
    // needs a catch before it, and it names itself when there is none.
    let error = parse("try {\n}\nelse {\n}").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"Else\"");
    // The catch before it is what makes the difference.
    let statements = parse("try {\n}\ncatch {\n}\nelse {\n}").expect("parses");
    assert_eq!(statements.len(), 1);
}

#[test]
fn the_content_of_a_block_may_be_on_the_line_of_the_brace_that_opens_it() {
    // Measured on the binary of v2.0.19: the content may be
    // there when the brace is at the start of its line.
    let statements = parse("if (1)\n{ x := 1\n}").expect("parses");
    assert_eq!(statements.len(), 1);
    // And the construct refuses it when the brace is not at the start of
    // its line.
    let error = parse("if (1) { x := 1 }").expect_err("does not parse");
    assert_eq!(error.message, "Unexpected \"If\"");
}

#[test]
fn the_body_of_a_try_that_is_on_one_line_says_that_the_brace_is_missing() {
    // Measured on the binary of v2.0.19: the block that is on
    // the line of the construct says that the brace is missing.
    let error = parse("try { x := 1 }").expect_err("does not parse");
    assert_eq!(error.message, "Missing \"}\"");
    // And the block that is empty is not a block at all.
    let error = parse("try {}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: }"
    );
}

#[test]
fn the_block_that_is_not_closed_names_the_construct_that_opened_it() {
    // Measured on the binary of v2.0.19: the block that is
    // empty names the construct that opened it, and the block that has
    // content names nothing.
    let error = parse("if (1) {").expect_err("does not parse");
    assert_eq!(error.message, "Missing \"}\"\n     Specifically: if (1) {");
    let error = parse("if (1) {\n    x := 1").expect_err("does not parse");
    assert_eq!(error.message, "Missing \"}\"");
}

#[test]
fn the_goto_whose_parenthesis_is_spaced_looks_for_the_whole_text() {
    // Measured on the binary of v2.0.19: the parenthesis
    // that is spaced is a part of the text of the name of the label.
    let error = parse("Goto (nowhere)").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Label not found in current scope.\n     Specifically: (nowhere)"
    );
}

#[test]
fn the_name_of_a_member_that_is_a_text_is_an_illegal_character() {
    // Measured on the binary of v2.0.19: the name of a member
    // may not be a text: the `a.'b'` is refused with "Illegal character in
    // expression." and the text is named as it was written.
    let error = parse("a := 1\nx := a.'b'").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Illegal character in expression.\n     Specifically: 'b'"
    );
}

#[test]
fn the_statement_that_begins_with_a_member_whose_name_is_not_a_name_holds_no_action() {
    // Measured on the binary of v2.0.19: the statement that
    // begins with a member whose name is not a name holds no action: the
    // `a.'b' := 5` is refused with "This line does not contain a recognized
    // action." and the whole line is named.
    let error = parse("a := 1\na.'b' := 5").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: a.'b' := 5"
    );
}

#[test]
fn the_statement_that_begins_with_a_member_whose_name_is_not_a_name_and_a_sum_holds_no_action() {
    // Measured on the binary of v2.0.19: the `a.'b' + 1` is a
    // statement that holds no action, and the whole line is named.
    let error = parse("a := 1\na.'b' + 1").expect_err("does not parse");
    assert_eq!(
        error.message,
        "This line does not contain a recognized action.\n     Specifically: a.'b' + 1"
    );
}

/// The mode of the loop of the files is a set of letters, and the letter that
/// is not one of them is refused when the loop runs.
///
/// Measured on the binary of AutoHotkey v2.0.19: the mode is the
/// "R" that walks into the folders, the "D" and the "K" that give the
/// folders, the "F" and the "V" that give the files, and the spaces between
/// them are ignored. The "A" is not one of them.
#[test]
fn the_mode_of_the_loop_of_the_files_carries_the_letters_of_v2() {
    let (report, _desktop) = run("Loop Files \"*.txt\", \"A\" {\n}\n");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some(
            "the mode of a `Loop Files` is the \"R\", the \"D\", the \"F\", the \"V\" or the \"K\", and the letter \"A\" is not one of them."
        )
    );
    assert_eq!(report.error_line, Some(1));
    // The spaces between the letters are ignored.
    let (report, desktop) =
        run("Loop Files \"*.txt\", \"D R\" {\n  Send \"%A_LoopFileName%\"\n}\n");
    assert!(report.ok, "{:?}", report.error);
    assert!(
        desktop
            .calls
            .contains(&("list_files".to_string(), "/work|*.txt|D R".to_string())),
        "{:?}",
        desktop.calls
    );
}

/// The `#Warn` takes the type of the warning and the mode of it.
///
/// Measured on the binary of AutoHotkey v2.0.19: the type that
/// is not one of them is refused at the load with "Parameter #1 invalid." and
/// the source of the line up to the comma, and the mode that is not one of
/// them with "Parameter #2 invalid." and the text that was written.
#[test]
fn the_type_of_the_warn_that_is_not_one_of_them_is_refused() {
    let error = parse("#Warn Nope").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 invalid.\n     Specifically: #Warn Nope"
    );
    assert_eq!(error.line, 1);
}

/// The text that the refusal of the type names is the source of the line.
///
/// Measured on the binary of AutoHotkey v2.0.19: the case of
/// the script is the case of the text -- "#warn nope" -- and the spaces of
/// the line are the spaces of the text -- "#Warn    Nope".
#[test]
fn the_type_of_the_warn_is_named_with_the_source_of_the_line() {
    let error = parse("#warn nope").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 invalid.\n     Specifically: #warn nope"
    );
    let error = parse("#WARN NOPE").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 invalid.\n     Specifically: #WARN NOPE"
    );
    let error = parse("#Warn    Nope").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 invalid.\n     Specifically: #Warn    Nope"
    );
    // The text stops at the comma.
    let error = parse("#Warn Nope , StdOut").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 invalid.\n     Specifically: #Warn Nope"
    );
}

/// The mode of the `#Warn` that is not one of them is refused.
///
/// Measured on the binary of AutoHotkey v2.0.19: the text that
/// the refusal names is the text that comes after the comma, and it is the
/// whole of it -- "StdOut, Extra".
#[test]
fn the_mode_of_the_warn_that_is_not_one_of_them_is_refused() {
    let error = parse("#Warn All, StdOut, Extra").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #2 invalid.\n     Specifically: StdOut, Extra"
    );
    let error = parse("#Warn All, StdOut Extra").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #2 invalid.\n     Specifically: StdOut Extra"
    );
}

/// A type that is empty is not a type, and the mode is the text that comes
/// after the comma.
///
/// Measured on the binary of AutoHotkey v2.0.19: the "#Warn ,
/// StdOut" is not a failure, and the "#Warn ,Nope" is a refusal of the mode.
#[test]
fn the_empty_type_of_the_warn_is_not_a_type() {
    parse("#Warn , StdOut").expect("parses");
    parse("#Warn All").expect("parses");
    let error = parse("#Warn ,Nope").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #2 invalid.\n     Specifically: Nope"
    );
}

/// The line that never runs is warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the warning
/// is of the line that comes right after the statement that leaves the level,
/// and the mode of the "StdOut" writes it to the standard output.
#[test]
fn the_line_that_never_runs_is_warned_about() {
    let (report, _desktop) = run("#Warn Unreachable, StdOut\nreturn\nx := 1\ny := 2\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 3);
    assert_eq!(
        report.warnings[0].message,
        "This line will never execute, due to Return preceding it."
    );
    assert_eq!(
        report.output,
        vec![
            "(3) : ==> Warning: This line will never execute, due to Return preceding it."
                .to_string()
        ]
    );
}

/// The warning is of the level of the statement that leaves it.
///
/// Measured on the binary of AutoHotkey v2.0.19: the break of
/// a loop warns the line that comes after it in the body of the loop, and the
/// return of a block does not warn the line that comes after the block.
#[test]
fn the_warning_of_the_unreachable_line_is_of_its_own_level() {
    let (report, _desktop) = run("#Warn Unreachable, StdOut\nloop 3 {\nbreak\nx := 1\n}\n");
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 4);
    assert_eq!(
        report.warnings[0].message,
        "This line will never execute, due to Break preceding it."
    );
    let (report, _desktop) = run("#Warn Unreachable, StdOut\nif 1 {\nreturn\n}\nx := 1\n");
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
}

/// The label that is the target of the jump is not warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the `Goto`
/// warns the line that comes after it, and the label is the line that the
/// jump comes to.
#[test]
fn the_label_that_is_the_target_of_the_jump_is_not_warned_about() {
    let (report, _desktop) = run("#Warn Unreachable, StdOut\ngoto later\nx := 1\nlater:\ny := 2\n");
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 3);
    assert_eq!(
        report.warnings[0].message,
        "This line will never execute, due to Goto preceding it."
    );
}

/// The warning of the unreachable line is off when the directive says so.
///
/// Measured on the binary of AutoHotkey v2.0.19: the mode of
/// the "Off" is no warning at all.
#[test]
fn the_warning_of_the_unreachable_line_is_off_when_the_directive_says_so() {
    let (report, _desktop) = run("#Warn Unreachable, Off\nreturn\nx := 1\n");
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
    assert!(report.output.is_empty(), "{:?}", report.output);
}

/// The variable that is read and never written is warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the warning
/// is given before the failure of the run, and the text of it names the kind
/// of the variable.
#[test]
fn the_variable_that_is_read_and_never_written_is_warned_about() {
    let (report, _desktop) = run("#Warn VarUnset, StdOut\nx := nope\n");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This global variable has not been assigned a value.")
    );
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 2);
    assert_eq!(
        report.warnings[0].message,
        "This global variable appears to never be assigned a value.\n     Specifically: nope"
    );
    assert_eq!(
        report.output,
        vec!["(2) : ==> Warning: This global variable appears to never be assigned a value.\n     Specifically: nope".to_string()]
    );
}

/// The local variable that is read and never written is warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the text of
/// the warning of a local says "This local variable appears to never be
/// assigned a value.".
#[test]
fn the_local_variable_that_is_read_and_never_written_is_warned_about() {
    let (report, _desktop) = run("#Warn VarUnset, StdOut\nf() {\n  x := nope\n}\nf()\n");
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 3);
    assert_eq!(
        report.warnings[0].message,
        "This local variable appears to never be assigned a value.\n     Specifically: nope"
    );
}

/// The variable that is read and never written is warned about when the
/// script is read.
///
/// Measured on the binary of AutoHotkey v2.0.19: the warning is
/// given when the script is read, and not when the variable is read: the
/// `if 0 { x := nope }` is warned about, and the read of it never happens.
#[test]
fn the_variable_that_is_never_run_is_warned_about() {
    let (report, _desktop) = run("#Warn VarUnset, StdOut\nif 0 {\n  x := nope\n}\n");
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 3);
    assert_eq!(
        report.warnings[0].message,
        "This global variable appears to never be assigned a value.\n     Specifically: nope"
    );
}

/// The variable that is written after the read is not warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the name that
/// the scope writes is a local of it, and the warning is not given for it.
#[test]
fn the_variable_that_is_written_after_the_read_is_not_warned_about() {
    let (report, _desktop) = run("#Warn VarUnset, StdOut\nx := nope\nnope := 5\n");
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
}

/// The name that the script has seen is a global one.
///
/// Measured on the binary of AutoHotkey v2.0.19: the script reads
/// the name and the function reads it after, and there is one warning only.
#[test]
fn the_name_that_the_script_has_seen_is_not_warned_about_again() {
    let (report, _desktop) = run("#Warn VarUnset, StdOut\nx := nope\nf() {\n  y := x\n}\nf()\n");
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 2);
}

/// The variable of a class that is read and never written is warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the body of the
/// class is a scope of its own, and the name that it reads and that the script
/// does not write is a local of it.
#[test]
fn the_variable_of_a_class_that_is_never_written_is_warned_about() {
    let (report, _desktop) = run("#Warn VarUnset, StdOut\nclass A {\n  y := nope\n}\n");
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 3);
    assert_eq!(
        report.warnings[0].message,
        "This local variable appears to never be assigned a value.\n     Specifically: nope"
    );
}

/// The type of the warning that the v2 does not know is refused.
///
/// Measured on the binary of AutoHotkey v2.0.19: a type that
/// is not one of the v2 is refused at the load. The `UseUnsetGlobal` is a
/// type of the v2, and it is carried.
#[test]
fn the_type_of_the_warn_that_the_v2_does_not_know_is_refused() {
    let error = parse("#Warn NotAWarningType, StdOut").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Parameter #1 invalid.\n     Specifically: #Warn NotAWarningType"
    );
}

/// The type of the warning that the tree carries is not named in the report.
///
/// The report names the types of the warning that the tree does not carry, and
/// the tree carries them all: the note is only there for the types that are
/// missing.
#[test]
fn the_type_of_the_warn_that_the_tree_carries_is_not_named_in_the_report() {
    let (report, _desktop) = run("#Warn LocalSameAsGlobal, StdOut\nx := 1\n");
    assert!(report.ok, "{:?}", report.error);
    assert!(
        !report
            .notes
            .iter()
            .any(|note| note.contains("LocalSameAsGlobal")),
        "{:?}",
        report.notes
    );
}

/// The local variable that has the name of a global one is warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the warning
/// is given once, when the script is read, and the text of it names the
/// variable and the function of it -- "x  (in function f)". The line of it is
/// the line of the write of the local.
#[test]
fn the_local_that_has_the_name_of_a_global_is_warned_about() {
    let (report, _desktop) =
        run("#Warn LocalSameAsGlobal, StdOut\nx := 1\nf() {\n  x := 2\n}\nf()\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 4);
    assert_eq!(
        report.warnings[0].message,
        "This local variable has the same name as a global variable.\n     Specifically: x  (in function f)"
    );
    assert_eq!(
        report.output,
        vec!["(4) : ==> Warning: This local variable has the same name as a global variable.\n     Specifically: x  (in function f)".to_string()]
    );
}

/// The name that is only read is not a local of the v2.
///
/// Measured on the binary of AutoHotkey v2.0.19: the v2 reads
/// the scope of the script when the name is only read, and the name that it
/// only reads is not a local of it.
#[test]
fn the_local_that_is_only_read_is_not_warned_about() {
    let (report, _desktop) =
        run("#Warn LocalSameAsGlobal, StdOut\nx := 1\nf() {\n  y := x\n}\nf()\n");
    assert!(report.ok, "{:?}", report.error);
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
}

/// The local of a method that has the name of a global one is warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the name of the
/// method is "A.Prototype.m" -- the class, the prototype and the method -- and
/// the line of the warning is the line of the write of the local.
#[test]
fn the_local_of_a_method_that_has_the_name_of_a_global_is_warned_about() {
    let (report, _desktop) =
        run("#Warn LocalSameAsGlobal, StdOut\nx := 1\nclass A {\n  m() {\n    x := 2\n  }\n}\n");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 5);
    assert_eq!(
        report.warnings[0].message,
        "This local variable has the same name as a global variable.\n     Specifically: x  (in function A.Prototype.m)"
    );
}

/// The local of a getter that has the name of a global one is warned about.
///
/// Measured on the binary of AutoHotkey v2.0.19: the name of the
/// getter is "A.Prototype.p.get", and the line of the warning is the line of
/// the write of the local.
#[test]
fn the_local_of_a_getter_that_has_the_name_of_a_global_is_warned_about() {
    let (report, _desktop) = run(
        "#Warn LocalSameAsGlobal, StdOut\nx := 1\nclass A {\n  p {\n    get {\n      x := 2\n    }\n  }\n}\n",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(report.warnings[0].line, 6);
    assert_eq!(
        report.warnings[0].message,
        "This local variable has the same name as a global variable.\n     Specifically: x  (in function A.Prototype.p.get)"
    );
}

/// The local that is declared global is not a local.
///
/// Measured on the binary of AutoHotkey v2.0.19: the local that
/// the function declares global is not warned about.
#[test]
fn the_local_that_is_declared_global_is_not_warned_about() {
    let (report, _desktop) =
        run("#Warn LocalSameAsGlobal, StdOut\nx := 1\nf() {\n  global x\n  x := 2\n}\nf()\n");
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
    // The local without a global is not warned about either.
    let (report, _desktop) = run("#Warn LocalSameAsGlobal, StdOut\nf() {\n  x := 2\n}\nf()\n");
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
}

/// The body of the getter is a scope of the v2.
///
/// Measured on the binary of v2.0.19: the `return` of the body
/// of the getter is the value of the property, and the name that the body
/// writes is a local of it.
#[test]
fn the_body_of_the_getter_is_a_scope_of_the_v2() {
    let (report, desktop) = run(
        "class Thing {\n  p {\n    get {\n      return 1\n    }\n  }\n}\ninst := Thing()\nSend inst.p",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["1"]);
}

/// The getter that is on the same line as the name is refused.
///
/// Measured on the binary of v2.0.19: the script does not load,
/// and the member is named whole: "p { get { return 1 } }".
#[test]
fn the_getter_that_is_on_the_same_line_as_the_name_is_refused() {
    let error =
        parse("class Thing {\n  p { get {\n      return 1\n  } }\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Not a valid method, class or property definition.\n     Specifically: p { get { return 1 } }"
    );
    assert_eq!(error.line, 2);
}

/// The body of the getter that is on the line of the member is refused.
///
/// Measured on the binary of v2.0.19: the script does not load,
/// and the line of the member is named: "get { return 1 }".
#[test]
fn the_body_of_the_getter_that_is_on_the_line_of_the_member_is_refused() {
    let error =
        parse("class Thing {\n  p {\n    get { return 1 }\n  }\n}").expect_err("does not parse");
    assert_eq!(
        error.message,
        "Not a valid property getter/setter.\n     Specifically: get { return 1 }"
    );
    assert_eq!(error.line, 3);
}

/// The setter of the v2 writes the value that is given to it.
///
/// Measured on the binary of v2.0.19: the body of the setter is
/// a scope, and the name "value" is the value that is written.
#[test]
fn the_setter_of_the_v2_writes_the_value_that_is_given_to_it() {
    let (report, desktop) = run(
        "class Thing {\n  p {\n    set {\n      this.stored := value\n    }\n  }\n}\ninst := Thing()\ninst.p := 7\nSend inst.stored",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(desktop.types(), vec!["7"]);
}

/// The value of the setter is a parameter of it, and it is not warned about.
///
/// Measured on the binary of v2.0.19: the name "value" is the
/// value that is written, and the v2 does not warn about it.
#[test]
fn the_value_of_the_setter_is_not_warned_about_in_the_v2() {
    let (report, _desktop) = run(
        "#Warn VarUnset, StdOut\nclass Thing {\n  p {\n    set {\n      this.stored := value\n    }\n  }\n}\ninst := Thing()\ninst.p := 7\n",
    );
    assert!(report.ok, "{:?}", report.error);
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
}

/// The `this` of a class is not a variable.
///
/// Measured on the binary of v2.0.19: the `this` of the body of
/// a class, the one of a method, the one of a static method and the one of a
/// getter are not warned about, and the `this` of a function is.
#[test]
fn the_this_of_a_class_is_not_a_variable_of_the_v2() {
    let (report, _desktop) =
        run("#Warn VarUnset, StdOut\nclass Thing {\n  m() {\n    this.x := 1\n  }\n}\n");
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
    let (report, _desktop) = run(
        "#Warn VarUnset, StdOut\nclass Thing {\n  p {\n    get {\n      this.x := 1\n    }\n  }\n}\n",
    );
    assert!(report.warnings.is_empty(), "{:?}", report.warnings);
    // The `this` of a function is a variable that has no value.
    let (report, _desktop) = run("#Warn VarUnset, StdOut\nf() {\n  y := this\n}\n");
    assert_eq!(report.warnings.len(), 1);
    assert_eq!(
        report.warnings[0].message,
        "This local variable appears to never be assigned a value.\n     Specifically: this"
    );
}

// -- break N, continue N, and the spread of a call ---------------------------
//
// Measured on the binary of v2.0.19: `break N`/`continue N`
// name the level of the loop that the flow leaves or continues, the level is
// an integer literal that is refused at the load when it is zero or above the
// count of the loops around it, and a `switch` is transparent to a break and
// to a continue. `f(arr*)` spreads every item of the value of the last
// argument into the arguments of the call; the spread is enumerated the way a
// `for` loop enumerates, and a middle spread is refused at the load with
// "Missing operand.".

#[test]
fn a_break_of_two_leaves_the_two_loops() {
    let (report, _desktop) = run(concat!(
        "out := \"\"\n",
        "Loop 2 {\n",
        "    Loop 2 {\n",
        "        out .= A_Index\n",
        "        break 2\n",
        "    }\n",
        "    out .= \"x\"\n",
        "}\n",
        "FileAppend(out, \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1"]);
}

#[test]
fn a_continue_of_two_continues_the_outer_loop() {
    let (report, _desktop) = run(concat!(
        "out := \"\"\n",
        "Loop 3 {\n",
        "    Loop 3 {\n",
        "        If A_Index = 2\n",
        "            continue 2\n",
        "        out .= \"in\" A_Index \" \"\n",
        "    }\n",
        "    out .= \"after\" A_Index \" \"\n",
        "}\n",
        "FileAppend(out, \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["in1 in1 in1 "]);
}

#[test]
fn a_plain_break_and_a_plain_continue_are_the_level_one() {
    let (report, _desktop) = run(concat!(
        "out := \"\"\n",
        "Loop 2 {\n",
        "    Loop 2 {\n",
        "        out .= A_Index\n",
        "        break 1\n",
        "    }\n",
        "    out .= \"-\"\n",
        "}\n",
        "FileAppend(out, \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1-1-"]);
}

#[test]
fn a_level_of_zero_is_refused() {
    let error = parse("Loop 2 {\n    break 0\n}").expect_err("does not parse");
    assert_eq!(error.message, "Parameter #1 invalid.");
    assert_eq!(error.line, 2);
    let error = parse("Loop 2 {\n    continue 0\n}").expect_err("does not parse");
    assert_eq!(error.message, "Parameter #1 invalid.");
    assert_eq!(error.line, 2);
}

#[test]
fn a_level_above_the_loops_around_it_is_refused() {
    let error = parse("Loop 2 {\n    break 2\n}").expect_err("does not parse");
    assert_eq!(error.message, "Parameter #1 invalid.");
    assert_eq!(error.line, 2);
    let error =
        parse("Loop 2 {\n    Loop 2 {\n        continue 3\n    }\n}").expect_err("does not parse");
    assert_eq!(error.message, "Parameter #1 invalid.");
    assert_eq!(error.line, 3);
}

#[test]
fn a_level_outside_a_loop_names_the_enclosure() {
    // The refusal of the enclosure comes before the level: the real binary
    // answers "Break/Continue must be enclosed by a Loop." for a break of any
    // level that is not inside one.
    let error = parse("break 2").expect_err("does not parse");
    assert_eq!(error.message, "Break/Continue must be enclosed by a Loop.");
}

#[test]
fn a_break_of_a_level_inside_a_switch_leaves_the_loop() {
    // Measured on the binary of v2.0.19: the switch does not
    // take a break of its own -- a case body ends at the next case -- and the
    // break goes on to the loop that holds the switch.
    let (report, _desktop) = run(concat!(
        "out := \"\"\n",
        "Loop 2 {\n",
        "    switch A_Index {\n",
        "        case 1:\n",
        "            out .= \"one-\"\n",
        "            break\n",
        "        default:\n",
        "            out .= \"other-\"\n",
        "    }\n",
        "    out .= \"after\" A_Index \"-\"\n",
        "}\n",
        "FileAppend(out, \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["one-"]);
    // A level of two inside a switch inside one loop is above the one loop
    // around it: the switch does not count as a level.
    let error = parse("Loop 2 {\n    switch 1 {\n        case 1:\n            break 2\n    }\n}")
        .expect_err("does not parse");
    assert_eq!(error.message, "Parameter #1 invalid.");
}

#[test]
fn a_level_comes_out_of_a_parse_loop_and_a_while_loop() {
    let (report, _desktop) = run(concat!(
        "out := \"\"\n",
        "Loop 2 {\n",
        "    Loop Parse \"a,b,c\", \",\" {\n",
        "        out .= A_LoopField\n",
        "        If A_LoopField = \"b\"\n",
        "            break 2\n",
        "    }\n",
        "    out .= \"x\"\n",
        "}\n",
        "FileAppend(out, \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ab"]);
    let (report, _desktop) = run(concat!(
        "out := \"\"\n",
        "i := 0\n",
        "Loop 3 {\n",
        "    i += 1\n",
        "    j := 0\n",
        "    while (j < 3) {\n",
        "        j += 1\n",
        "        If j = 2\n",
        "            continue 2\n",
        "        out .= i \".\" j \" \"\n",
        "    }\n",
        "    out .= \"after-\"\n",
        "}\n",
        "FileAppend(out, \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1.1 2.1 3.1 "]);
}

#[test]
fn the_spread_of_a_call_spreads_the_array() {
    let (report, _desktop) = run(concat!(
        "F(a, b) {\n",
        "    return a \"/\" b\n",
        "}\n",
        "arr := [1, 2]\n",
        "FileAppend(F(arr*), \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1/2"]);
}

#[test]
fn the_spread_of_a_call_goes_through_a_value_and_a_variadic() {
    let (report, _desktop) = run(concat!(
        "F(a, b) {\n",
        "    return a \"/\" b\n",
        "}\n",
        "Sum(rest*) {\n",
        "    s := 0\n",
        "    for v in rest\n",
        "        s += v\n",
        "    return s\n",
        "}\n",
        "arr := [1, 2]\n",
        "g := F\n",
        "FileAppend(g(arr*) \"|\" Sum(arr*) \"|\" Abs([-3]*), \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1/2|3|3"]);
}

#[test]
fn the_spread_of_a_map_gives_its_keys() {
    // Measured on the binary of v2.0.19: the spread enumerates
    // the value the way a `for` loop does, and the `for` over a map walks its
    // keys.
    let (report, _desktop) = run(concat!(
        "F(a, b) {\n",
        "    return a \"/\" b\n",
        "}\n",
        "m := Map(\"a\", 1, \"b\", 2)\n",
        "FileAppend(F(m*), \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a/b"]);
}

#[test]
fn the_spread_of_an_empty_array_leaves_the_call_without_a_value() {
    let (report, _desktop) = run(concat!(
        "F(a, b) {\n",
        "    return a \"/\" b\n",
        "}\n",
        "arr := []\n",
        "FileAppend(F(arr*), \"*\")",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Missing a required parameter.")
    );
}

#[test]
fn a_spread_of_a_value_that_cannot_be_enumerated_is_refused() {
    let (report, _desktop) = run(concat!(
        "F(a, b) {\n",
        "    return a \"/\" b\n",
        "}\n",
        "x := 5\n",
        "FileAppend(F(x*), \"*\")",
    ));
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("Value not enumerable."));
}

#[test]
fn a_middle_spread_is_refused() {
    // Measured on the binary of v2.0.19: only the last
    // argument may carry the star, and the refusal names the rest of the
    // line from the star -- also inside the list of another call.
    let error =
        parse("F(a, b) {\n    return a\n}\narr := [1, 2]\nF(arr*, 9)").expect_err("does not parse");
    assert!(
        error
            .message
            .starts_with("Missing operand.\n     Specifically: *, 9)"),
        "{:?}",
        error.message
    );
    let error = parse("F(a, b) {\n    return a\n}\narr := [1, 2]\nEmit(F(arr*, 9))")
        .expect_err("does not parse");
    assert!(
        error.message.starts_with("Missing operand."),
        "{:?}",
        error.message
    );
}

#[test]
fn the_spread_of_a_method_and_of_a_constructor_works() {
    let (report, _desktop) = run(concat!(
        "class C {\n",
        "    __New(a, b) {\n",
        "        this.v := a \"-\" b\n",
        "    }\n",
        "    m(a, b) {\n",
        "        return a \"+\" b\n",
        "    }\n",
        "}\n",
        "arr := [4, 5]\n",
        "inst := C(arr*)\n",
        "FileAppend(inst.v \"|\" inst.m(arr*), \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["4-5|4+5"]);
}

#[test]
fn a_spread_is_a_value_where_a_reference_is_wanted() {
    // Measured on the binary of v2.0.19: an item of an array is
    // not a variable reference, and a spread argument is a value.
    let (report, _desktop) = run(concat!(
        "Swap(&x, &y) {\n",
        "    t := x\n",
        "    x := y\n",
        "    y := t\n",
        "}\n",
        "a := 1\n",
        "b := 2\n",
        "arr := [a, b]\n",
        "Swap(arr*)",
    ));
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Parameter #1 of Swap requires a variable reference, but received an Integer.")
    );
}

#[test]
fn the_spread_of_a_class_enumerates_its_values() {
    let (report, _desktop) = run(concat!(
        "class E {\n",
        "    __Enum(n) {\n",
        "        return [7, 8].__Enum(n)\n",
        "    }\n",
        "}\n",
        "F(a, b) {\n",
        "    return a \"/\" b\n",
        "}\n",
        "FileAppend(F(E()*), \"*\")",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["7/8"]);
}
#[test]
fn the_mode_of_the_title_is_a_setting_of_the_script() {
    let _turn = THE_TITLE_MODE_TURN
        .lock()
        .unwrap_or_else(|error| error.into_inner());
    // Measured on the binary of v2.0.19: the command may be
    // written as a statement without the parentheses, and the mode that it
    // sets is the one that the built-in variable `A_TitleMatchMode` answers.
    let (report, _desktop) = run(concat!(
        "SetTitleMatchMode 3\n",
        "m := A_TitleMatchMode\n",
        "SetTitleMatchMode 2\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("m"), Some(&Value::Int(3)));
}

#[test]
fn the_mode_of_the_title_may_be_the_word_of_the_patterns() {
    let _turn = THE_TITLE_MODE_TURN
        .lock()
        .unwrap_or_else(|error| error.into_inner());
    // Measured on the binary of v2.0.19: the word "RegEx" is the
    // mode of the patterns, and the built-in variable answers the word.
    let (report, _desktop) = run(concat!(
        "SetTitleMatchMode \"RegEx\"\n",
        "m := A_TitleMatchMode\n",
        "SetTitleMatchMode 2\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.variables.get("m"),
        Some(&Value::Text("RegEx".into()))
    );
}
#[test]
fn a_hotkey_of_a_suspend_exempt_section_is_exempt() {
    // Measured on the binary of v2.0.19: `#SuspendExempt` covers
    // the hotkeys that come after it, and `#SuspendExempt False` stops
    // covering them; `Suspend 1` turns the state on and `Suspend 0` turns it
    // off. The runner has no keyboard, so the state is what is observable.
    let (report, _desktop) = run(concat!(
        "#SuspendExempt\n",
        "F1::return\n",
        "#SuspendExempt False\n",
        "F2::return\n",
        "Suspend 1\n",
        "a := A_IsSuspended\n",
        "Suspend 0\n",
        "b := A_IsSuspended\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("a"), Some(&Value::Int(1)));
    assert_eq!(report.variables.get("b"), Some(&Value::Int(0)));
}

#[test]
fn a_suspend_exempt_that_is_not_a_boolean_is_refused() {
    // Measured on the binary of v2.0.19: a value that is not a
    // boolean is the load failure "Parameter #1 invalid.".
    let (report, _desktop) = run("#SuspendExempt Bogus\n");
    assert_eq!(report.error.as_deref(), Some("Parameter #1 invalid."));
}
#[test]
fn the_two_kinds_of_absence_are_told_apart() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: the result of an
    // access that the mark of the chain passed over is passed over by every
    // access after it -- `holder.Prop?.Inner` has no value and does not fail --
    // and the absent value that the script holds is a failure when it is used:
    // `holder.Prop.Inner` is an `UnsetError`, and so is `x.Inner` of a variable
    // that holds the absent value. The mark turns the absent value of the
    // access it follows into the one that is passed over, so
    // `(holder.Prop?)?.Inner` has no value either.
    let (report, _desktop) = run_in(
        concat!(
            "class Holder {\n",
            "    Prop => unset\n",
            "}\n",
            "h := Holder()\n",
            "v := h.Prop?.Inner\n",
            "MsgBox \"a\" IsSet(v)\n",
            "try {\n",
            "    v := h.Prop.Inner\n",
            "    MsgBox \"no throw 1\"\n",
            "} catch as e {\n",
            "    MsgBox \"b\" Type(e)\n",
            "}\n",
            "v := (h.Prop?)?.Inner\n",
            "MsgBox \"c\" IsSet(v)\n",
            "MsgBox \"d\" (h.Prop?.Inner ?? 12)\n",
            "x := unset\n",
            "try {\n",
            "    v := x.Inner\n",
            "    MsgBox \"no throw 2\"\n",
            "} catch as e {\n",
            "    MsgBox \"e\" Type(e)\n",
            "}\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["a0", "bUnsetError", "c0", "d12", "eUnsetError"]
    );
}
#[test]
fn the_parenthesis_is_what_makes_the_mark_a_chain_on_a_value() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: the mark that
    // follows an access tolerates the absent value of that access --
    // `h.Prop?.Inner` has no value -- and the mark that follows a parenthesis is
    // the chain on a value: `(h?.Prop)?.Inner` is an `UnsetError`, because the
    // value of the group has no value and the access after it is not the one
    // the mark tolerates.
    let (report, _desktop) = run_in(
        concat!(
            "h := { Prop: unset }\n",
            "v := h.Prop?.Inner\n",
            "MsgBox \"a\" IsSet(v)\n",
            "try {\n",
            "    v := (h?.Prop)?.Inner\n",
            "    MsgBox \"no throw\"\n",
            "} catch as e {\n",
            "    MsgBox \"b\" Type(e)\n",
            "}\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a0", "bUnsetError"]);
}

#[test]
fn the_line_of_the_arguments_of_a_call_is_the_line_of_the_call() {
    // Measured on the test suite of KeySharp (the corpus script
    // `op-null-assign.ahk`) and on the notes of v2.1-alpha: the `A_LineNumber`
    // of the arguments of a call is the line of the call, and not a line of the
    // body that the call ran -- the body leaves its own line behind, and the
    // argument after it has to see the line of the caller.
    let (report, _desktop) = run(concat!(
        "F(a) {\n",
        "    b := a + 1\n",
        "    return b\n",
        "}\n",
        "MsgBox \"b=\" F(1) \" \" A_LineNumber\n",
        "MsgBox \"c=\" A_LineNumber\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["b=2 5", "c=6"]);
}
#[test]
fn a_property_that_holds_a_function_is_called_as_a_method() {
    // Measured on the test suite of KeySharp (the corpus script
    // `class-ownprops.ahk`) and on the notes of v2.1-alpha: a property that holds
    // a function is called as the method of its object -- the descriptor of
    // `DefineProp` holds the getter in `get`, and `o.get()` calls it with `o` as
    // the `this` of the function, so `(this) => 456` answers 456 and a setter
    // that writes a property of `this` writes the object. The v2.0.19 refuses
    // the call with "This value of type ... has no method named get.".
    let (report, _desktop) = run_in(
        concat!(
            "o := { get: (this) => 456, set: (this) => this.b := 789 }\n",
            "MsgBox \"a\" o.get()\n",
            "o.set()\n",
            "MsgBox \"b\" o.b\n",
            "class Kls {\n",
            "    x := 1\n",
            "}\n",
            "k := Kls()\n",
            "k.DefineProp(\"p\", { get: (this) => this.x * 2 })\n",
            "MsgBox \"c\" k.p\n",
        ),
        ahk::dialect::v2_1(),
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a456", "b789", "c2"]);
}

#[test]
fn the_property_that_holds_a_function_is_called_by_the_v2() {
    // Measured on the binary of v2.0.31: a property that holds
    // a function is called as the method of its object -- `o := { get: (this)
    // => 456 }` and then `o.get()` answers 456. The v2.0.19 refused the call
    // with "This value of type ... has no method named get."; the v2.0.31
    // calls it.
    let (report, _desktop) = run(concat!("o := { get: (this) => 456 }\n", "MsgBox o.get()\n",));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["456"]);
}
#[test]
fn the_comma_at_the_level_of_a_statement_separates_statements() {
    // Measured on the binary of v2.0.19: the comma at the level
    // of a statement separates statements and not expressions -- `x++, y++`
    // runs the two increments, and `x := 3, y := 4` runs the two assignments
    // -- and the value of the whole is the value of the last one.
    let (report, _desktop) = run(concat!(
        "x := 1\n",
        "x++, x++\n",
        "MsgBox \"a\" x\n",
        "y := 0\n",
        "y := 3, y := 4\n",
        "MsgBox \"b\" y\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a3", "b4"]);
}

#[test]
fn the_line_that_begins_with_a_comma_continues_the_statement() {
    // Measured on the binary of v2.0.19: a line that begins
    // with a comma continues the statement of the line before it -- `x := 1`
    // and then `, ++x` is one line of two statements -- and a line that begins
    // with a comma at the start of the script is refused.
    let (report, _desktop) = run(concat!(
        "x := 1\n",
        ", ++x\n",
        ", y := x\n",
        "MsgBox \"a\" y\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a2"]);
}

#[test]
fn a_declaration_may_carry_an_operator_of_the_write() {
    // Measured on the binary of v2.0.19: a declaration may
    // carry any operator of the write -- `global n += 1` adds the one to the
    // value that is there -- and the increment and the null assignment of the
    // v2.1 are not among them.
    let (report, _desktop) = run(concat!(
        "n := 10\n",
        "F() {\n",
        "    global n += 1\n",
        "}\n",
        "F()\n",
        "MsgBox \"a\" n\n",
        "G() {\n",
        "    local m := 4\n",
        "    local m *= 2\n",
        "    MsgBox \"b\" m\n",
        "}\n",
        "G()\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["a11", "b8"]);
}

#[test]
fn the_dot_that_ends_the_line_is_the_operator_of_the_concat() {
    // Measured on the binary of v2.0.19: the dot that ends the
    // line and that has a space before it is the operator of the concat, and
    // the operand of it is on the line below -- `z := x .` and then `"qwer"`
    // is the text of x and the text of qwer.
    let (report, _desktop) = run(concat!(
        "x := \"asdf\"\n",
        "z := x .\n",
        "\"qwer\"\n",
        "MsgBox \"a\" z\n",
    ));
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["aasdfqwer"]);
}

#[test]
fn a_sub_version_is_met_when_it_is_the_least_one_that_runs() {
    // Measured on the binary of v2.0.19: `#Requires AutoHotkey
    // v2.0` and `#Requires AutoHotkey v2.0.19` both run, because the version
    // of the requirement is the least one that runs the script. The tree
    // refused both of them until this was fixed.
    let (report, _) = run("#Requires AutoHotkey v2.0\nFileAppend \"ok\", \"*\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["ok"]);
    let (report, _) = run("#Requires AutoHotkey v2.0.32\nFileAppend \"ok\", \"*\"");
    assert!(report.ok, "{:?}", report.error);
}

#[test]
fn a_version_later_than_this_runtime_is_refused() {
    // Measured on the binary of v2.0.19: `#Requires AutoHotkey
    // v2.0.20` does not run, because that version is later than the one that
    // is running.
    //
    // The release of the tree is the 2.0.32, and the version that is later
    // than it is the 2.0.33.
    let (report, _) = run("#Requires AutoHotkey v2.0.33");
    assert!(!report.ok);
    let error = report.error.as_deref().unwrap_or("");
    assert!(
        error.contains("This script requires AutoHotkey v2.0.33."),
        "{error}"
    );
}

#[test]
fn a_sub_version_with_no_interpreter_named_is_refused() {
    // Measured on the binary of v2.0.19: `#Requires v2.0` does
    // not run there, because the first word is read as the name of the
    // interpreter and `v2.0` is not one.
    let (report, _) = run("#Requires v2.0");
    assert!(!report.ok);
}