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
|
" Test various aspects of the Vim9 script language.
source check.vim
source term_util.vim
source view_util.vim
import './vim9.vim' as v9
source screendump.vim
func Test_def_basic()
def SomeFunc(): string
return 'yes'
enddef
call SomeFunc()->assert_equal('yes')
endfunc
func Test_compiling_error()
" use a terminal to see the whole error message
CheckRunVimInTerminal
call TestCompilingError()
call TestCompilingErrorInTry()
endfunc
def TestCompilingError()
var lines =<< trim END
vim9script
def Fails()
echo nothing
enddef
defcompile
END
writefile(lines, 'XTest_compile_error', 'D')
var buf = g:RunVimInTerminal('-S XTest_compile_error',
{rows: 10, wait_for_ruler: 0})
g:WaitForAssert(() => assert_match('Error detected while compiling command line.*Fails.*Variable not found: nothing',
g:Term_getlines(buf, range(1, 9))))
# clean up
g:StopVimInTerminal(buf)
enddef
def TestCompilingErrorInTry()
var dir = 'Xcompdir/autoload'
mkdir(dir, 'pR')
var lines =<< trim END
vim9script
export def OnlyCompiled()
g:runtime = 'yes'
invalid
enddef
END
writefile(lines, dir .. '/script.vim')
lines =<< trim END
vim9script
todo
try
script#OnlyCompiled()
catch /nothing/
endtry
END
lines[1] = 'set rtp=' .. getcwd() .. '/Xcompdir'
writefile(lines, 'XTest_compile_error', 'D')
var buf = g:RunVimInTerminal('-S XTest_compile_error', {rows: 10, wait_for_ruler: 0})
g:WaitForAssert(() => assert_match('Error detected while compiling command line.*function script#OnlyCompiled.*Invalid command: invalid',
g:Term_getlines(buf, range(1, 9))))
# clean up
g:StopVimInTerminal(buf)
enddef
def Test_comment_error()
v9.CheckDefFailure(['#{ comment'], 'E1170:')
enddef
def Test_compile_error_in_called_function()
var lines =<< trim END
vim9script
var n: number
def Foo()
&hls = n
enddef
def Bar()
Foo()
enddef
silent! Foo()
Bar()
END
v9.CheckScriptFailureList(lines, ['E1012:', 'E1191:'])
enddef
def Test_wrong_function_name()
var lines =<< trim END
vim9script
func _Foo()
echo 'foo'
endfunc
END
v9.CheckScriptFailure(lines, 'E1267:')
lines =<< trim END
vim9script
def _Foo()
echo 'foo'
enddef
END
v9.CheckScriptFailure(lines, 'E1267:')
lines =<< trim END
vim9script
var Object = {}
function Object.Method()
endfunction
END
v9.CheckScriptFailure(lines, 'E1182:')
lines =<< trim END
vim9script
var Object = {}
def Object.Method()
enddef
END
v9.CheckScriptFailure(lines, 'E1182:')
lines =<< trim END
vim9script
g:Object = {}
function g:Object.Method()
endfunction
END
v9.CheckScriptFailure(lines, 'E1182:')
lines =<< trim END
let s:Object = {}
def Define()
function s:Object.Method()
endfunction
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1182:')
delfunc g:Define
lines =<< trim END
let s:Object = {}
def Define()
def Object.Method()
enddef
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1182:')
delfunc g:Define
lines =<< trim END
let g:Object = {}
def Define()
function g:Object.Method()
endfunction
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1182:')
delfunc g:Define
enddef
def Test_break_in_skipped_block()
var lines =<< trim END
vim9script
def FixStackFrame(): string
for _ in [2]
var path = 'xxx'
if !!path
if false
break
else
return 'foo'
endif
endif
endfor
return 'xxx'
enddef
disas FixStackFrame
FixStackFrame()
END
v9.CheckScriptSuccess(lines)
enddef
def Test_autoload_name_mismatch()
var dir = 'Xnamedir/autoload'
mkdir(dir, 'pR')
var lines =<< trim END
vim9script
export def NoFunction()
# comment
g:runtime = 'yes'
enddef
END
writefile(lines, dir .. '/script.vim')
var save_rtp = &rtp
exe 'set rtp=' .. getcwd() .. '/Xnamedir'
lines =<< trim END
call script#Function()
END
v9.CheckScriptFailure(lines, 'E117:', 1)
&rtp = save_rtp
enddef
def Test_autoload_names()
var dir = 'Xandir/autoload'
mkdir(dir, 'pR')
var lines =<< trim END
func foobar#function()
return 'yes'
endfunc
let foobar#var = 'no'
END
writefile(lines, dir .. '/foobar.vim')
var save_rtp = &rtp
exe 'set rtp=' .. getcwd() .. '/Xandir'
lines =<< trim END
assert_equal('yes', foobar#function())
var Function = foobar#function
assert_equal('yes', Function())
assert_equal('no', foobar#var)
END
v9.CheckDefAndScriptSuccess(lines)
&rtp = save_rtp
enddef
def Test_autoload_error_in_script()
var dir = 'Xaedir/autoload'
mkdir(dir, 'pR')
var lines =<< trim END
func scripterror#function()
let g:called_function = 'yes'
endfunc
let 0 = 1
END
writefile(lines, dir .. '/scripterror.vim')
var save_rtp = &rtp
exe 'set rtp=' .. getcwd() .. '/Xaedir'
g:called_function = 'no'
# The error in the autoload script cannot be checked with assert_fails(), use
# CheckDefSuccess() instead of CheckDefFailure()
try
v9.CheckDefSuccess(['scripterror#function()'])
catch
assert_match('E121: Undefined variable: 0', v:exception)
endtry
assert_equal('no', g:called_function)
lines =<< trim END
func scriptcaught#function()
let g:called_function = 'yes'
endfunc
try
let 0 = 1
catch
let g:caught = v:exception
endtry
END
writefile(lines, dir .. '/scriptcaught.vim')
g:called_function = 'no'
v9.CheckDefSuccess(['scriptcaught#function()'])
assert_match('E121: Undefined variable: 0', g:caught)
assert_equal('yes', g:called_function)
&rtp = save_rtp
enddef
def s:CallRecursive(n: number): number
return CallRecursive(n + 1)
enddef
def s:CallMapRecursive(l: list<number>): number
return map(l, (_, v) => CallMapRecursive([v]))[0]
enddef
def Test_funcdepth_error()
set maxfuncdepth=10
var caught = false
try
CallRecursive(1)
catch /E132:/
caught = true
endtry
assert_true(caught)
caught = false
try
CallMapRecursive([1])
catch /E132:/
caught = true
endtry
assert_true(caught)
set maxfuncdepth&
enddef
def Test_endfunc_enddef()
var lines =<< trim END
def Test()
echo 'test'
endfunc
enddef
END
v9.CheckScriptFailure(lines, 'E1151:', 3)
lines =<< trim END
def Test()
func Nested()
echo 'test'
enddef
enddef
END
v9.CheckScriptFailure(lines, 'E1152:', 4)
lines =<< trim END
def Ok()
echo 'hello'
enddef | echo 'there'
def Bad()
echo 'hello'
enddef there
END
v9.CheckScriptFailure(lines, 'E1173: Text found after enddef: there', 6)
enddef
def Test_missing_endfunc_enddef()
var lines =<< trim END
vim9script
def Test()
echo 'test'
endef
END
v9.CheckScriptFailure(lines, 'E1057:', 2)
lines =<< trim END
vim9script
func Some()
echo 'test'
enfffunc
END
v9.CheckScriptFailure(lines, 'E126:', 2)
enddef
def Test_white_space_before_paren()
var lines =<< trim END
vim9script
def Test ()
echo 'test'
enddef
END
v9.CheckScriptFailure(lines, 'E1068:', 2)
lines =<< trim END
vim9script
func Test ()
echo 'test'
endfunc
END
v9.CheckScriptFailure(lines, 'E1068:', 2)
lines =<< trim END
def Test ()
echo 'test'
enddef
END
v9.CheckScriptFailure(lines, 'E1068:', 1)
lines =<< trim END
func Test ()
echo 'test'
endfunc
END
v9.CheckScriptSuccess(lines)
enddef
def Test_enddef_dict_key()
var d = {
enddef: 'x',
endfunc: 'y',
}
assert_equal({enddef: 'x', endfunc: 'y'}, d)
enddef
def ReturnString(): string
return 'string'
enddef
def ReturnNumber(): number
return 123
enddef
let g:notNumber = 'string'
def ReturnGlobal(): number
return g:notNumber
enddef
def Test_return_something()
g:ReturnString()->assert_equal('string')
g:ReturnNumber()->assert_equal(123)
assert_fails('g:ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
var lines =<< trim END
vim9script
def Msg()
echomsg 'in Msg()...'
enddef
def Func()
return Msg()
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1096:')
enddef
def Test_check_argument_type()
var lines =<< trim END
vim9script
def Val(a: number, b: number): number
return 0
enddef
def Func()
var x: any = true
Val(0, x)
enddef
disass Func
Func()
END
v9.CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got bool', 2)
lines =<< trim END
vim9script
def Foobar(Fn: func(any, ?string): any)
enddef
Foobar((t) => 0)
END
v9.CheckScriptSuccess(lines)
enddef
def Test_missing_return()
v9.CheckDefFailure(['def Missing(): number',
' if g:cond',
' echo "no return"',
' else',
' return 0',
' endif',
'enddef'], 'E1027:')
v9.CheckDefFailure(['def Missing(): number',
' if g:cond',
' return 1',
' else',
' echo "no return"',
' endif',
'enddef'], 'E1027:')
v9.CheckDefFailure(['def Missing(): number',
' if g:cond',
' return 1',
' else',
' return 2',
' endif',
' return 3',
'enddef'], 'E1095:')
enddef
def Test_return_bool()
var lines =<< trim END
vim9script
def MenuFilter(id: number, key: string): bool
return popup_filter_menu(id, key)
enddef
def YesnoFilter(id: number, key: string): bool
return popup_filter_yesno(id, key)
enddef
defcompile
END
v9.CheckScriptSuccess(lines)
enddef
def Test_return_void_comment_follows()
var lines =<< trim END
vim9script
def ReturnCommentFollows(): void
return # Some comment
enddef
defcompile
END
v9.CheckScriptSuccess(lines)
enddef
let s:nothing = 0
def ReturnNothing()
s:nothing = 1
if true
return
endif
s:nothing = 2
enddef
def Test_return_nothing()
g:ReturnNothing()
s:nothing->assert_equal(1)
enddef
def Test_return_invalid()
var lines =<< trim END
vim9script
def Func(): invalid
return xxx
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1010:', 2)
lines =<< trim END
vim9script
def Test(Fun: func(number): number): list<number>
return map([1, 2, 3], (_, i) => Fun(i))
enddef
defcompile
def Inc(nr: number): nr
return nr + 2
enddef
echo Test(Inc)
END
# doing this twice was leaking memory
v9.CheckScriptFailure(lines, 'E1010:')
v9.CheckScriptFailure(lines, 'E1010:')
enddef
def Test_return_list_any()
# This used to fail but now the actual list type is checked, and since it has
# an item of type string it can be used as list<string>.
var lines =<< trim END
vim9script
def Func(): list<string>
var l: list<any>
l->add('string')
return l
enddef
echo Func()
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Func(): list<string>
var l: list<any>
l += ['string']
return l
enddef
echo Func()
END
v9.CheckScriptSuccess(lines)
enddef
def Test_return_any_two_types()
var lines =<< trim END
vim9script
def G(Fn: func(string): any)
g:result = Fn("hello")
enddef
def F(a: number, b: string): any
echo b
if a > 0
return 1
else
return []
endif
enddef
G(function(F, [1]))
END
v9.CheckScriptSuccess(lines)
assert_equal(1, g:result)
unlet g:result
enddef
func s:Increment()
let g:counter += 1
endfunc
def Test_call_ufunc_count()
g:counter = 1
Increment()
Increment()
Increment()
# works with and without :call
g:counter->assert_equal(4)
eval g:counter->assert_equal(4)
unlet g:counter
enddef
def Test_call_ufunc_failure()
var lines =<< trim END
vim9script
def Tryit()
g:Global(1, 2, 3)
enddef
func g:Global(a, b, c)
echo a:a a:b a:c
endfunc
defcompile
func! g:Global(a, b)
echo a:a a:b
endfunc
Tryit()
END
v9.CheckScriptFailure(lines, 'E118: Too many arguments for function: Global')
delfunc g:Global
lines =<< trim END
vim9script
g:Ref = function('len')
def Tryit()
g:Ref('x')
enddef
defcompile
g:Ref = function('add')
Tryit()
END
v9.CheckScriptFailure(lines, 'E119: Not enough arguments for function: add')
unlet g:Ref
enddef
def s:MyVarargs(arg: string, ...rest: list<string>): string
var res = arg
for s in rest
res ..= ',' .. s
endfor
return res
enddef
def Test_call_varargs()
MyVarargs('one')->assert_equal('one')
MyVarargs('one', 'two')->assert_equal('one,two')
MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
enddef
def Test_call_white_space()
v9.CheckDefAndScriptFailure(["call Test ('text')"], ['E476:', 'E1068:'])
enddef
def MyDefaultArgs(name = 'string'): string
return name
enddef
def s:MyDefaultSecond(name: string, second: bool = true): string
return second ? name : 'none'
enddef
def Test_call_default_args()
g:MyDefaultArgs()->assert_equal('string')
g:MyDefaultArgs(v:none)->assert_equal('string')
g:MyDefaultArgs('one')->assert_equal('one')
assert_fails('g:MyDefaultArgs("one", "two")', 'E118:', '', 4, 'Test_call_default_args')
MyDefaultSecond('test')->assert_equal('test')
MyDefaultSecond('test', true)->assert_equal('test')
MyDefaultSecond('test', false)->assert_equal('none')
var lines =<< trim END
def MyDefaultThird(name: string, aa = 'aa', bb = 'bb'): string
return name .. aa .. bb
enddef
MyDefaultThird('->')->assert_equal('->aabb')
MyDefaultThird('->', v:none)->assert_equal('->aabb')
MyDefaultThird('->', 'xx')->assert_equal('->xxbb')
MyDefaultThird('->', v:none, v:none)->assert_equal('->aabb')
MyDefaultThird('->', 'xx', v:none)->assert_equal('->xxbb')
MyDefaultThird('->', v:none, 'yy')->assert_equal('->aayy')
MyDefaultThird('->', 'xx', 'yy')->assert_equal('->xxyy')
def DefArg(mandatory: any, optional = mandatory): string
return mandatory .. optional
enddef
DefArg(1234)->assert_equal('12341234')
DefArg("ok")->assert_equal('okok')
END
v9.CheckDefAndScriptSuccess(lines)
v9.CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
delfunc g:Func
v9.CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
delfunc g:Func
v9.CheckDefFailure(['def Func(x: number = )', 'enddef'], 'E15:')
lines =<< trim END
vim9script
def Func(a = b == 0 ? 1 : 2, b = 0)
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1001: Variable not found: b')
# using script variable requires matching type or type cast when executed
lines =<< trim END
vim9script
var a: any
def Func(arg: string = a)
echo arg
enddef
defcompile
END
v9.CheckScriptSuccess(lines + ['a = "text"', 'Func()'])
v9.CheckScriptFailure(lines + ['a = 123', 'Func()'], 'E1013: Argument 1: type mismatch, expected string but got number')
# using global variable does not require type cast
lines =<< trim END
vim9script
def Func(arg: string = g:str)
echo arg
enddef
g:str = 'works'
Func()
END
v9.CheckScriptSuccess(lines)
enddef
def Test_convert_number_to_float()
var lines =<< trim END
vim9script
def Foo(a: float, b: float): float
return a + b
enddef
assert_equal(5.3, Foo(3.3, 2))
END
v9.CheckScriptSuccess(lines)
enddef
def s:FuncWithComment( # comment
a: number, #comment
b: bool, # comment
c: string) #comment
assert_equal(4, a)
assert_equal(true, b)
assert_equal('yes', c)
enddef
def Test_func_with_comments()
FuncWithComment(4, true, 'yes')
var lines =<< trim END
def Func(# comment
arg: string)
enddef
END
v9.CheckScriptFailure(lines, 'E125:', 1)
lines =<< trim END
def Func(
arg: string# comment
)
enddef
END
v9.CheckScriptFailure(lines, 'E475:', 2)
lines =<< trim END
def Func(
arg: string
)# comment
enddef
END
v9.CheckScriptFailure(lines, 'E488:', 3)
enddef
def Test_nested_function()
def NestedDef(arg: string): string
return 'nested ' .. arg
enddef
NestedDef(':def')->assert_equal('nested :def')
func NestedFunc(arg)
return 'nested ' .. a:arg
endfunc
NestedFunc(':func')->assert_equal('nested :func')
v9.CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
v9.CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
v9.CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
v9.CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
var lines =<< trim END
def Outer()
def Inner()
# comment
enddef
def Inner()
enddef
enddef
END
v9.CheckDefFailure(lines, 'E1073:')
lines =<< trim END
def Outer()
def Inner()
# comment
enddef
def! Inner()
enddef
enddef
END
v9.CheckDefFailure(lines, 'E1117:')
lines =<< trim END
vim9script
def Outer()
def Inner()
g:result = 'ok'
enddef
Inner()
enddef
Outer()
Inner()
END
v9.CheckScriptFailure(lines, 'E117: Unknown function: Inner')
assert_equal('ok', g:result)
unlet g:result
lines =<< trim END
vim9script
def Outer()
def _Inner()
echo 'bad'
enddef
_Inner()
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1267:')
lines =<< trim END
vim9script
def Outer()
def g:inner()
echo 'bad'
enddef
g:inner()
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1267:')
lines =<< trim END
vim9script
def g:_Func()
echo 'bad'
enddef
END
v9.CheckScriptFailure(lines, 'E1267:')
lines =<< trim END
vim9script
def _Func()
echo 'bad'
enddef
END
v9.CheckScriptFailure(lines, 'E1267:')
# nested function inside conditional
lines =<< trim END
vim9script
var thecount = 0
if true
def Test(): number
def TheFunc(): number
thecount += 1
return thecount
enddef
return TheFunc()
enddef
endif
defcompile
assert_equal(1, Test())
assert_equal(2, Test())
END
v9.CheckScriptSuccess(lines)
# also works when "thecount" is inside the "if" block
lines =<< trim END
vim9script
if true
var thecount = 0
def Test(): number
def TheFunc(): number
thecount += 1
return thecount
enddef
return TheFunc()
enddef
endif
defcompile
assert_equal(1, Test())
assert_equal(2, Test())
END
v9.CheckScriptSuccess(lines)
# nested function with recursive call
lines =<< trim END
vim9script
def MyFunc(): number
def Fib(n: number): number
if n < 2
return 1
endif
return Fib(n - 2) + Fib(n - 1)
enddef
return Fib(5)
enddef
assert_equal(8, MyFunc())
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Outer()
def Inner()
echo 'hello'
enddef burp
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1173: Text found after enddef: burp', 3)
enddef
def Test_nested_function_fails()
var lines =<< trim END
def T()
def Func(g: string):string
enddef
Func()
enddef
silent! defcompile
END
v9.CheckScriptFailure(lines, 'E1069:')
enddef
def Test_not_nested_function()
echo printf('%d',
function('len')('xxx'))
enddef
func Test_call_default_args_from_func()
call MyDefaultArgs()->assert_equal('string')
call MyDefaultArgs('one')->assert_equal('one')
call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
endfunc
def Test_nested_global_function()
var lines =<< trim END
vim9script
def Outer()
def g:Inner(): string
return 'inner'
enddef
enddef
defcompile
Outer()
g:Inner()->assert_equal('inner')
delfunc g:Inner
Outer()
g:Inner()->assert_equal('inner')
delfunc g:Inner
Outer()
g:Inner()->assert_equal('inner')
delfunc g:Inner
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Outer()
func g:Inner()
return 'inner'
endfunc
enddef
defcompile
Outer()
g:Inner()->assert_equal('inner')
delfunc g:Inner
Outer()
g:Inner()->assert_equal('inner')
delfunc g:Inner
Outer()
g:Inner()->assert_equal('inner')
delfunc g:Inner
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Outer()
def g:Inner(): string
return 'inner'
enddef
enddef
defcompile
Outer()
Outer()
END
v9.CheckScriptFailure(lines, "E122:")
delfunc g:Inner
lines =<< trim END
vim9script
def Outer()
def g:Inner()
echo map([1, 2, 3], (_, v) => v + 1)
enddef
g:Inner()
enddef
Outer()
END
v9.CheckScriptSuccess(lines)
delfunc g:Inner
lines =<< trim END
vim9script
def Func()
echo 'script'
enddef
def Outer()
def Func()
echo 'inner'
enddef
enddef
defcompile
END
v9.CheckScriptFailure(lines, "E1073:", 1)
lines =<< trim END
vim9script
def Func()
echo 'script'
enddef
def Func()
echo 'script'
enddef
END
v9.CheckScriptFailure(lines, "E1073:", 5)
enddef
def DefListAll()
def
enddef
def DefListOne()
def DefListOne
enddef
def DefListMatches()
def /DefList
enddef
def Test_nested_def_list()
var funcs = split(execute('call DefListAll()'), "\n")
assert_true(len(funcs) > 10)
assert_true(funcs->index('def DefListAll()') >= 0)
funcs = split(execute('call DefListOne()'), "\n")
assert_equal([' def DefListOne()', '1 def DefListOne', ' enddef'], funcs)
funcs = split(execute('call DefListMatches()'), "\n")
assert_true(len(funcs) >= 3)
assert_true(funcs->index('def DefListAll()') >= 0)
assert_true(funcs->index('def DefListOne()') >= 0)
assert_true(funcs->index('def DefListMatches()') >= 0)
var lines =<< trim END
vim9script
def Func()
def +Func+
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E476:', 1)
enddef
def Test_global_function_not_found()
var lines =<< trim END
g:Ref = 123
call g:Ref()
END
v9.CheckDefExecAndScriptFailure(lines, ['E117:', 'E1085:'], 2)
enddef
def Test_global_local_function()
var lines =<< trim END
vim9script
def g:Func(): string
return 'global'
enddef
def Func(): string
return 'local'
enddef
g:Func()->assert_equal('global')
Func()->assert_equal('local')
delfunc g:Func
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def g:Funcy()
echo 'funcy'
enddef
Funcy()
END
v9.CheckScriptFailure(lines, 'E117:')
enddef
def Test_local_function_shadows_global()
var lines =<< trim END
vim9script
def g:Gfunc(): string
return 'global'
enddef
def AnotherFunc(): number
var Gfunc = function('len')
return Gfunc('testing')
enddef
g:Gfunc()->assert_equal('global')
AnotherFunc()->assert_equal(7)
delfunc g:Gfunc
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def g:Func(): string
return 'global'
enddef
def AnotherFunc()
g:Func = function('len')
enddef
AnotherFunc()
END
v9.CheckScriptFailure(lines, 'E705:')
delfunc g:Func
# global function is not found with g: prefix
lines =<< trim END
vim9script
def g:Func(): string
return 'global'
enddef
def AnotherFunc(): string
return Func()
enddef
assert_equal('global', AnotherFunc())
END
v9.CheckScriptFailure(lines, 'E117:')
delfunc g:Func
lines =<< trim END
vim9script
def g:Func(): string
return 'global'
enddef
assert_equal('global', g:Func())
delfunc g:Func
END
v9.CheckScriptSuccess(lines)
# This does not shadow "i" which is visible only inside the for loop
lines =<< trim END
vim9script
def Foo(i: number)
echo i
enddef
for i in range(3)
# Foo() is compiled here
Foo(i)
endfor
END
v9.CheckScriptSuccess(lines)
enddef
func TakesOneArg(arg)
echo a:arg
endfunc
def Test_call_wrong_args()
v9.CheckDefFailure(['g:TakesOneArg()'], 'E119:')
v9.CheckDefFailure(['g:TakesOneArg(11, 22)'], 'E118:')
v9.CheckDefFailure(['bufnr(xxx)'], 'E1001:')
v9.CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
var lines =<< trim END
vim9script
def Func(s: string)
echo s
enddef
Func([])
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
# argument name declared earlier is found when declaring a function
lines =<< trim END
vim9script
var name = 'piet'
def FuncOne(name: string)
echo name
enddef
END
v9.CheckScriptFailure(lines, 'E1168:')
# same, inside the same block
lines =<< trim END
vim9script
if true
var name = 'piet'
def FuncOne(name: string)
echo name
enddef
endif
END
v9.CheckScriptFailure(lines, 'E1168:')
# variable in other block is OK
lines =<< trim END
vim9script
if true
var name = 'piet'
endif
def FuncOne(name: string)
echo name
enddef
END
v9.CheckScriptSuccess(lines)
# with another variable in another block
lines =<< trim END
vim9script
if true
var name = 'piet'
# define a function so that the variable isn't cleared
def GetItem(): string
return item
enddef
endif
if true
var name = 'peter'
def FuncOne(name: string)
echo name
enddef
endif
END
v9.CheckScriptFailure(lines, 'E1168:')
# only variable in another block is OK
lines =<< trim END
vim9script
if true
var name = 'piet'
# define a function so that the variable isn't cleared
def GetItem(): string
return item
enddef
endif
if true
def FuncOne(name: string)
echo name
enddef
endif
END
v9.CheckScriptSuccess(lines)
# argument name declared later is only found when compiling
lines =<< trim END
vim9script
def FuncOne(name: string)
echo nr
enddef
var name = 'piet'
END
v9.CheckScriptSuccess(lines)
v9.CheckScriptFailure(lines + ['defcompile'], 'E1168:')
lines =<< trim END
vim9script
def FuncOne(nr: number)
echo nr
enddef
def FuncTwo()
FuncOne()
enddef
defcompile
END
writefile(lines, 'Xscript')
var didCatch = false
try
source Xscript
catch
assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
didCatch = true
endtry
assert_true(didCatch)
lines =<< trim END
vim9script
def FuncOne(nr: number)
echo nr
enddef
def FuncTwo()
FuncOne(1, 2)
enddef
defcompile
END
writefile(lines, 'Xscript', 'D')
didCatch = false
try
source Xscript
catch
assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
didCatch = true
endtry
assert_true(didCatch)
enddef
def Test_call_funcref_wrong_args()
var head =<< trim END
vim9script
def Func3(a1: string, a2: number, a3: list<number>)
echo a1 .. a2 .. a3[0]
enddef
def Testme()
var funcMap: dict<func> = {func: Func3}
END
var tail =<< trim END
enddef
Testme()
END
v9.CheckScriptSuccess(head + ["funcMap['func']('str', 123, [1, 2, 3])"] + tail)
v9.CheckScriptFailure(head + ["funcMap['func']('str', 123)"] + tail, 'E119:')
v9.CheckScriptFailure(head + ["funcMap['func']('str', 123, [1], 4)"] + tail, 'E118:')
var lines =<< trim END
vim9script
var Ref: func(number): any
Ref = (j) => !j
echo Ref(false)
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
lines =<< trim END
vim9script
var Ref: func(number): any
Ref = (j) => !j
call Ref(false)
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got bool', 4)
enddef
def Test_call_lambda_args()
var lines =<< trim END
var Callback = (..._) => 'anything'
assert_equal('anything', Callback())
assert_equal('anything', Callback(1))
assert_equal('anything', Callback('a', 2))
assert_equal('xyz', ((a: string): string => a)('xyz'))
END
v9.CheckDefAndScriptSuccess(lines)
v9.CheckDefFailure(['echo ((i) => 0)()'],
'E119: Not enough arguments for function: ((i) => 0)()')
lines =<< trim END
var Ref = (x: number, y: number) => x + y
echo Ref(1, 'x')
END
v9.CheckDefFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string')
lines =<< trim END
var Ref: func(job, string, number)
Ref = (x, y) => 0
END
v9.CheckDefAndScriptFailure(lines, 'E1012:')
lines =<< trim END
var Ref: func(job, string)
Ref = (x, y, z) => 0
END
v9.CheckDefAndScriptFailure(lines, 'E1012:')
lines =<< trim END
var one = 1
var l = [1, 2, 3]
echo map(l, (one) => one)
END
v9.CheckDefFailure(lines, 'E1167:')
v9.CheckScriptFailure(['vim9script'] + lines, 'E1168:')
lines =<< trim END
var Ref: func(any, ?any): bool
Ref = (_, y = 1) => false
END
v9.CheckDefAndScriptFailure(lines, 'E1172:')
lines =<< trim END
var a = 0
var b = (a == 0 ? 1 : 2)
assert_equal(1, b)
var txt = 'a'
b = (txt =~ 'x' ? 1 : 2)
assert_equal(2, b)
END
v9.CheckDefAndScriptSuccess(lines)
lines =<< trim END
def ShadowLocal()
var one = 1
var l = [1, 2, 3]
echo map(l, (one) => one)
enddef
END
v9.CheckDefFailure(lines, 'E1167:')
lines =<< trim END
def Shadowarg(one: number)
var l = [1, 2, 3]
echo map(l, (one) => one)
enddef
END
v9.CheckDefFailure(lines, 'E1167:')
lines =<< trim END
echo ((a) => a)('aa', 'bb')
END
v9.CheckDefAndScriptFailure(lines, 'E118:', 1)
lines =<< trim END
echo 'aa'->((a) => a)('bb')
END
v9.CheckDefFailure(lines, 'E118: Too many arguments for function: ->((a) => a)(''bb'')', 1)
v9.CheckScriptFailure(['vim9script'] + lines, 'E118: Too many arguments for function: <lambda>', 2)
enddef
def Test_lambda_line_nr()
var lines =<< trim END
vim9script
# comment
# comment
var id = timer_start(1'000, (_) => 0)
var out = execute('verbose ' .. timer_info(id)[0].callback
->string()
->substitute("('\\|')", ' ', 'g'))
assert_match('Last set from .* line 4', out)
END
v9.CheckScriptSuccess(lines)
enddef
def FilterWithCond(x: string, Cond: func(string): bool): bool
return Cond(x)
enddef
def Test_lambda_return_type()
var lines =<< trim END
var Ref = (): => 123
END
v9.CheckDefAndScriptFailure(lines, 'E1157:', 1)
# no space before the return type
lines =<< trim END
var Ref = (x):number => x + 1
END
v9.CheckDefAndScriptFailure(lines, 'E1069:', 1)
# this works
for x in ['foo', 'boo']
echo g:FilterWithCond(x, (v) => v =~ '^b')
endfor
# this fails
lines =<< trim END
echo g:FilterWithCond('foo', (v) => v .. '^b')
END
v9.CheckDefAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected func(string): bool but got func(any): string', 1)
lines =<< trim END
var Lambda1 = (x) => {
return x
}
assert_equal('asdf', Lambda1('asdf'))
var Lambda2 = (x): string => {
return x
}
assert_equal('foo', Lambda2('foo'))
END
v9.CheckDefAndScriptSuccess(lines)
lines =<< trim END
var Lambda = (x): string => {
return x
}
echo Lambda(['foo'])
END
v9.CheckDefExecAndScriptFailure(lines, 'E1012:')
enddef
def Test_lambda_uses_assigned_var()
v9.CheckDefSuccess([
'var x: any = "aaa"',
'x = filter(["bbb"], (_, v) => v =~ x)'])
enddef
def Test_lambda_invalid_block()
var lines =<< trim END
timer_start(0, (_) => { # echo
echo 'yes'
})
END
v9.CheckDefAndScriptSuccess(lines)
lines =<< trim END
timer_start(0, (_) => { " echo
echo 'yes'
})
END
v9.CheckDefAndScriptFailure(lines, 'E488: Trailing characters: " echo')
lines =<< trim END
timer_start(0, (_) => { | echo
echo 'yes'
})
END
v9.CheckDefAndScriptFailure(lines, 'E488: Trailing characters: | echo')
enddef
def Test_lambda_with_following_cmd()
var lines =<< trim END
set ts=2
var Lambda = () => {
set ts=4
} | set ts=3
assert_equal(3, &ts)
Lambda()
assert_equal(4, &ts)
END
v9.CheckDefAndScriptSuccess(lines)
set ts=8
enddef
def Test_pass_legacy_lambda_to_def_func()
var lines =<< trim END
vim9script
func Foo()
eval s:Bar({x -> 0})
endfunc
def Bar(y: any)
enddef
Foo()
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def g:TestFunc(F: func)
enddef
legacy call g:TestFunc({-> 0})
delfunc g:TestFunc
def g:TestFunc(F: func(number))
enddef
legacy call g:TestFunc({nr -> 0})
delfunc g:TestFunc
END
v9.CheckScriptSuccess(lines)
enddef
def Test_lambda_in_reduce_line_break()
# this was using freed memory
var lines =<< trim END
vim9script
const result: dict<number> =
['Bob', 'Sam', 'Cat', 'Bob', 'Cat', 'Cat']
->reduce((acc, val) => {
if has_key(acc, val)
acc[val] += 1
return acc
else
acc[val] = 1
return acc
endif
}, {})
assert_equal({Bob: 2, Sam: 1, Cat: 3}, result)
END
v9.CheckScriptSuccess(lines)
enddef
def Test_set_opfunc_to_lambda()
var lines =<< trim END
vim9script
nnoremap <expr> <F4> <SID>CountSpaces() .. '_'
def CountSpaces(type = ''): string
if type == ''
&operatorfunc = (t) => CountSpaces(t)
return 'g@'
endif
normal! '[V']y
g:result = getreg('"')->count(' ')
return ''
enddef
new
'a b c d e'->setline(1)
feedkeys("\<F4>", 'x')
assert_equal(4, g:result)
bwipe!
END
v9.CheckScriptSuccess(lines)
enddef
def Test_set_opfunc_to_global_function()
var lines =<< trim END
vim9script
def g:CountSpaces(type = ''): string
normal! '[V']y
g:result = getreg('"')->count(' ')
return ''
enddef
# global function works at script level
&operatorfunc = g:CountSpaces
new
'a b c d e'->setline(1)
feedkeys("g@_", 'x')
assert_equal(4, g:result)
&operatorfunc = ''
g:result = 0
# global function works in :def function
def Func()
&operatorfunc = g:CountSpaces
enddef
Func()
feedkeys("g@_", 'x')
assert_equal(4, g:result)
bwipe!
END
v9.CheckScriptSuccess(lines)
&operatorfunc = ''
enddef
def Test_use_script_func_name_with_prefix()
var lines =<< trim END
vim9script
func g:Getit()
return 'it'
endfunc
var Fn = g:Getit
assert_equal('it', Fn())
END
v9.CheckScriptSuccess(lines)
enddef
def Test_lambda_type_allocated()
# Check that unreferencing a partial using a lambda can use the variable type
# after the lambda has been freed and does not leak memory.
var lines =<< trim END
vim9script
func MyomniFunc1(val, findstart, base)
return a:findstart ? 0 : []
endfunc
var Lambda = (a, b) => MyomniFunc1(19, a, b)
&omnifunc = Lambda
Lambda = (a, b) => MyomniFunc1(20, a, b)
&omnifunc = string(Lambda)
Lambda = (a, b) => strlen(a)
END
v9.CheckScriptSuccess(lines)
enddef
def Test_define_lambda_in_execute()
var lines =<< trim [CODE]
vim9script
def BuildFuncMultiLine(): func
var x =<< trim END
g:SomeRandomFunc = (d: dict<any>) => {
return d.k1 + d.k2
}
END
execute(x)
return g:SomeRandomFunc
enddef
var ResultPlus = BuildFuncMultiLine()
assert_equal(7, ResultPlus({k1: 3, k2: 4}))
[CODE]
v9.CheckScriptSuccess(lines)
unlet g:SomeRandomFunc
enddef
" Default arg and varargs
def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
var res = one .. ',' .. two
for s in rest
res ..= ',' .. s
endfor
return res
enddef
def Test_call_def_varargs()
assert_fails('g:MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
g:MyDefVarargs('one')->assert_equal('one,foo')
g:MyDefVarargs('one', 'two')->assert_equal('one,two')
g:MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
v9.CheckDefFailure(['g:MyDefVarargs("one", 22)'],
'E1013: Argument 2: type mismatch, expected string but got number')
v9.CheckDefFailure(['g:MyDefVarargs("one", "two", 123)'],
'E1013: Argument 3: type mismatch, expected string but got number')
var lines =<< trim END
vim9script
def Func(...l: list<string>)
echo l
enddef
Func('a', 'b', 'c')
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Func(...l: list<string>)
echo l
enddef
Func()
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Func(...l: list<any>)
echo l
enddef
Func(0)
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Func(...l: any)
echo l
enddef
Func(0)
END
v9.CheckScriptFailure(lines, 'E1180:', 2)
lines =<< trim END
vim9script
def Func(..._l: list<string>)
echo _l
enddef
Func('a', 'b', 'c')
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Func(...l: list<string>)
echo l
enddef
Func(1, 2, 3)
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
lines =<< trim END
vim9script
def Func(...l: list<string>)
echo l
enddef
Func('a', 9)
END
v9.CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
lines =<< trim END
vim9script
def Func(...l: list<string>)
echo l
enddef
Func(1, 'a')
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
lines =<< trim END
vim9script
def Func( # some comment
...l = []
)
echo l
enddef
END
v9.CheckScriptFailure(lines, 'E1160:')
lines =<< trim END
vim9script
def DoIt()
g:Later('')
enddef
defcompile
def g:Later(...l: list<number>)
enddef
DoIt()
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected number but got string')
enddef
let s:value = ''
def FuncOneDefArg(opt = 'text')
s:value = opt
enddef
def FuncTwoDefArg(nr = 123, opt = 'text'): string
return nr .. opt
enddef
def FuncVarargs(...arg: list<string>): string
return join(arg, ',')
enddef
def Test_func_type_varargs()
var RefDefArg: func(?string)
RefDefArg = g:FuncOneDefArg
RefDefArg()
s:value->assert_equal('text')
RefDefArg('some')
s:value->assert_equal('some')
var RefDef2Arg: func(?number, ?string): string
RefDef2Arg = g:FuncTwoDefArg
RefDef2Arg()->assert_equal('123text')
RefDef2Arg(99)->assert_equal('99text')
RefDef2Arg(77, 'some')->assert_equal('77some')
v9.CheckDefFailure(['var RefWrong: func(string?)'], 'E1010:')
v9.CheckDefFailure(['var RefWrong: func(?string, string)'], 'E1007:')
var RefVarargs: func(...list<string>): string
RefVarargs = g:FuncVarargs
RefVarargs()->assert_equal('')
RefVarargs('one')->assert_equal('one')
RefVarargs('one', 'two')->assert_equal('one,two')
v9.CheckDefFailure(['var RefWrong: func(...list<string>, string)'], 'E110:')
v9.CheckDefFailure(['var RefWrong: func(...list<string>, ?string)'], 'E110:')
enddef
" Only varargs
def MyVarargsOnly(...args: list<string>): string
return join(args, ',')
enddef
def Test_call_varargs_only()
g:MyVarargsOnly()->assert_equal('')
g:MyVarargsOnly('one')->assert_equal('one')
g:MyVarargsOnly('one', 'two')->assert_equal('one,two')
v9.CheckDefFailure(['g:MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
v9.CheckDefFailure(['g:MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
enddef
def Test_varargs_mismatch()
var lines =<< trim END
vim9script
def Map(Fn: func(...any): number): number
return Fn('12')
enddef
var res = Map((v) => str2nr(v))
assert_equal(12, res)
END
v9.CheckScriptSuccess(lines)
enddef
def Test_using_var_as_arg()
var lines =<< trim END
def Func(x: number)
var x = 234
enddef
END
v9.CheckDefFailure(lines, 'E1006:')
lines =<< trim END
def Func(Ref: number)
def Ref()
enddef
enddef
END
v9.CheckDefFailure(lines, 'E1073:')
enddef
def s:DictArg(arg: dict<string>)
arg['key'] = 'value'
enddef
def s:ListArg(arg: list<string>)
arg[0] = 'value'
enddef
def Test_assign_to_argument()
# works for dict and list
var d: dict<string> = {}
DictArg(d)
d['key']->assert_equal('value')
var l: list<string> = []
ListArg(l)
l[0]->assert_equal('value')
v9.CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
delfunc! g:Func
enddef
" These argument names are reserved in legacy functions.
def s:WithReservedNames(firstline: string, lastline: string): string
return firstline .. lastline
enddef
def Test_argument_names()
assert_equal('OK', WithReservedNames('O', 'K'))
enddef
def Test_call_func_defined_later()
g:DefinedLater('one')->assert_equal('one')
assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
enddef
func DefinedLater(arg)
return a:arg
endfunc
def Test_call_funcref()
g:SomeFunc('abc')->assert_equal(3)
assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
assert_fails('g:NotAFunc()', 'E1085:', '', 3, 'Test_call_funcref')
var lines =<< trim END
vim9script
def RetNumber(): number
return 123
enddef
var Funcref: func: number = function('RetNumber')
Funcref()->assert_equal(123)
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def RetNumber(): number
return 123
enddef
def Bar(F: func: number): number
return F()
enddef
var Funcref = function('RetNumber')
Bar(Funcref)->assert_equal(123)
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def UseNumber(nr: number)
echo nr
enddef
var Funcref: func(number) = function('UseNumber')
Funcref(123)
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def UseNumber(nr: number)
echo nr
enddef
var Funcref: func(string) = function('UseNumber')
END
v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
lines =<< trim END
vim9script
def EchoNr(nr = 34)
g:echo = nr
enddef
var Funcref: func(?number) = function('EchoNr')
Funcref()
g:echo->assert_equal(34)
Funcref(123)
g:echo->assert_equal(123)
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def EchoList(...l: list<number>)
g:echo = l
enddef
var Funcref: func(...list<number>) = function('EchoList')
Funcref()
g:echo->assert_equal([])
Funcref(1, 2, 3)
g:echo->assert_equal([1, 2, 3])
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
g:optarg = opt
g:listarg = l
return nr
enddef
var Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
Funcref(10)->assert_equal(10)
g:optarg->assert_equal(12)
g:listarg->assert_equal([])
Funcref(11, 22)->assert_equal(11)
g:optarg->assert_equal(22)
g:listarg->assert_equal([])
Funcref(17, 18, 1, 2, 3)->assert_equal(17)
g:optarg->assert_equal(18)
g:listarg->assert_equal([1, 2, 3])
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
function s:func(num)
return a:num * 2
endfunction
def s:CallFuncref()
var Funcref = function('s:func')
Funcref(3)->assert_equal(6)
enddef
call s:CallFuncref()
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
function s:func(num)
return a:num * 2
endfunction
def s:CallFuncref()
var Funcref = function(s:func)
Funcref(3)->assert_equal(6)
enddef
call s:CallFuncref()
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
function s:func(num)
return a:num * 2
endfunction
def s:CallFuncref()
var Funcref = s:func
Funcref(3)->assert_equal(6)
enddef
call s:CallFuncref()
END
v9.CheckScriptSuccess(lines)
enddef
let SomeFunc = function('len')
let NotAFunc = 'text'
def CombineFuncrefTypes()
# same arguments, different return type
var Ref1: func(bool): string
var Ref2: func(bool): number
var Ref3: func(bool): any
Ref3 = g:cond ? Ref1 : Ref2
# different number of arguments
var Refa1: func(bool): number
var Refa2: func(bool, number): number
var Refa3: func: number
Refa3 = g:cond ? Refa1 : Refa2
# different argument types
var Refb1: func(bool, string): number
var Refb2: func(string, number): number
var Refb3: func(any, any): number
Refb3 = g:cond ? Refb1 : Refb2
enddef
def FuncWithForwardCall()
return g:DefinedEvenLater("yes")
enddef
def DefinedEvenLater(arg: string): string
return arg
enddef
def Test_error_in_nested_function()
# Error in called function requires unwinding the call stack.
assert_fails('g:FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
enddef
def Test_nested_function_with_nextcmd()
var lines =<< trim END
vim9script
# Define an outer function
def FirstFunction()
# Define an inner function
def SecondFunction()
# the function has a body, a double free is detected.
AAAAA
# enddef followed by | or } followed by # one or more characters
enddef|BBBB
enddef
# Compile all functions
defcompile
END
v9.CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
enddef
def Test_nested_function_with_args_split()
var lines =<< trim END
vim9script
def FirstFunction()
def SecondFunction(
)
# had a double free if the right parenthesis of the nested function is
# on the next line
enddef|BBBB
enddef
# Compile all functions
defcompile
END
v9.CheckScriptFailure(lines, 'E1173: Text found after enddef: BBBB')
lines =<< trim END
vim9script
def FirstFunction()
func SecondFunction()
endfunc|BBBB
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1173: Text found after endfunction: BBBB')
enddef
def Test_error_in_function_args()
var lines =<< trim END
def FirstFunction()
def SecondFunction(J =
# Nois
# one
enddef|BBBB
enddef
# Compile all functions
defcompile
END
v9.CheckScriptFailure(lines, 'E488:')
enddef
def Test_return_type_wrong()
v9.CheckScriptFailure([
'def Func(): number',
'return "a"',
'enddef',
'defcompile'], 'expected number but got string')
delfunc! g:Func
v9.CheckScriptFailure([
'def Func(): string',
'return 1',
'enddef',
'defcompile'], 'expected string but got number')
delfunc! g:Func
v9.CheckScriptFailure([
'def Func(): void',
'return "a"',
'enddef',
'defcompile'],
'E1096: Returning a value in a function without a return type')
delfunc! g:Func
v9.CheckScriptFailure([
'def Func()',
'return "a"',
'enddef',
'defcompile'],
'E1096: Returning a value in a function without a return type')
delfunc! g:Func
v9.CheckScriptFailure([
'def Func(): number',
'return',
'enddef',
'defcompile'], 'E1003:')
delfunc! g:Func
v9.CheckScriptFailure([
'def Func():number',
'return 123',
'enddef',
'defcompile'], 'E1069:')
delfunc! g:Func
v9.CheckScriptFailure([
'def Func() :number',
'return 123',
'enddef',
'defcompile'], 'E1059:')
delfunc! g:Func
v9.CheckScriptFailure([
'def Func() : number',
'return 123',
'enddef',
'defcompile'], 'E1059:')
delfunc! g:Func
v9.CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008: Missing <type> after list')
delfunc! g:Func
v9.CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008: Missing <type> after dict')
delfunc! g:Func
v9.CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
delfunc! g:Func
v9.CheckScriptFailure([
'vim9script',
'def FuncB()',
' return 123',
'enddef',
'def FuncA()',
' FuncB()',
'enddef',
'defcompile'], 'E1096:')
enddef
def Test_arg_type_wrong()
v9.CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type> after list')
v9.CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
v9.CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
v9.CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
v9.CheckScriptFailure(['def Func6(...x:list<number>)', 'echo "a"', 'enddef'], 'E1069:')
v9.CheckScriptFailure(['def Func7(...x: int)', 'echo "a"', 'enddef'], 'E1010:')
enddef
def Test_white_space_before_comma()
var lines =<< trim END
vim9script
def Func(a: number , b: number)
enddef
END
v9.CheckScriptFailure(lines, 'E1068:')
call assert_fails('vim9cmd echo stridx("a" .. "b" , "a")', 'E1068:')
enddef
def Test_white_space_after_comma()
var lines =<< trim END
vim9script
def Func(a: number,b: number)
enddef
END
v9.CheckScriptFailure(lines, 'E1069:')
# OK in legacy function
lines =<< trim END
vim9script
func Func(a,b)
endfunc
END
v9.CheckScriptSuccess(lines)
enddef
def Test_vim9script_call()
var lines =<< trim END
vim9script
var name = ''
def MyFunc(arg: string)
name = arg
enddef
MyFunc('foobar')
name->assert_equal('foobar')
var str = 'barfoo'
str->MyFunc()
name->assert_equal('barfoo')
g:value = 'value'
g:value->MyFunc()
name->assert_equal('value')
var listvar = []
def ListFunc(arg: list<number>)
listvar = arg
enddef
[1, 2, 3]->ListFunc()
listvar->assert_equal([1, 2, 3])
var dictvar = {}
def DictFunc(arg: dict<number>)
dictvar = arg
enddef
{a: 1, b: 2}->DictFunc()
dictvar->assert_equal({a: 1, b: 2})
def CompiledDict()
{a: 3, b: 4}->DictFunc()
enddef
CompiledDict()
dictvar->assert_equal({a: 3, b: 4})
{a: 3, b: 4}->DictFunc()
dictvar->assert_equal({a: 3, b: 4})
('text')->MyFunc()
name->assert_equal('text')
("some")->MyFunc()
name->assert_equal('some')
# line starting with single quote is not a mark
# line starting with double quote can be a method call
'asdfasdf'->MyFunc()
name->assert_equal('asdfasdf')
"xyz"->MyFunc()
name->assert_equal('xyz')
def UseString()
'xyork'->MyFunc()
enddef
UseString()
name->assert_equal('xyork')
def UseString2()
"knife"->MyFunc()
enddef
UseString2()
name->assert_equal('knife')
# prepending a colon makes it a mark
new
setline(1, ['aaa', 'bbb', 'ccc'])
normal! 3Gmt1G
:'t
getcurpos()[1]->assert_equal(3)
bwipe!
MyFunc(
'continued'
)
assert_equal('continued',
name
)
call MyFunc(
'more'
..
'lines'
)
assert_equal(
'morelines',
name)
END
writefile(lines, 'Xcall.vim', 'D')
source Xcall.vim
enddef
def Test_vim9script_call_fail_decl()
var lines =<< trim END
vim9script
var name = ''
def MyFunc(arg: string)
var name = 123
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1054:')
enddef
def Test_vim9script_call_fail_type()
var lines =<< trim END
vim9script
def MyFunc(arg: string)
echo arg
enddef
MyFunc(1234)
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
enddef
def Test_vim9script_call_fail_const()
var lines =<< trim END
vim9script
const var = ''
def MyFunc(arg: string)
var = 'asdf'
enddef
defcompile
END
writefile(lines, 'Xcall_const.vim', 'D')
assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
lines =<< trim END
const g:Aconst = 77
def Change()
# comment
g:Aconst = 99
enddef
call Change()
unlet g:Aconst
END
v9.CheckScriptFailure(lines, 'E741: Value is locked: Aconst', 2)
enddef
" Test that inside :function a Python function can be defined, :def is not
" recognized.
func Test_function_python()
CheckFeature python3
let py = 'python3'
execute py "<< EOF"
def do_something():
return 1
EOF
endfunc
def Test_delfunc()
var lines =<< trim END
vim9script
def g:GoneSoon()
echo 'hello'
enddef
def CallGoneSoon()
g:GoneSoon()
enddef
defcompile
delfunc g:GoneSoon
CallGoneSoon()
END
writefile(lines, 'XToDelFunc', 'D')
assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
enddef
func Test_free_dict_while_in_funcstack()
" relies on the sleep command
CheckUnix
call Run_Test_free_dict_while_in_funcstack()
endfunc
def Run_Test_free_dict_while_in_funcstack()
# this was freeing the TermRun() default argument dictionary while it was
# still referenced in a funcstack_T
var lines =<< trim END
vim9script
&updatetime = 400
def TermRun(_ = {})
def Post()
enddef
def Exec()
term_start('sleep 1', {
term_finish: 'close',
exit_cb: (_, _) => Post(),
})
enddef
Exec()
enddef
nnoremap <F4> <Cmd>call <SID>TermRun()<CR>
timer_start(100, (_) => feedkeys("\<F4>"))
timer_start(1000, (_) => feedkeys("\<F4>"))
sleep 1500m
END
v9.CheckScriptSuccess(lines)
nunmap <F4>
set updatetime&
enddef
def Test_redef_failure()
writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
so Xdef
writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
so Xdef
writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
so Xdef
delete('Xdef')
assert_fails('g:Func0()', 'E1091:')
g:Func1()->assert_equal('Func1')
g:Func2()->assert_equal('Func2')
delfunc! Func0
delfunc! Func1
delfunc! Func2
enddef
def Test_vim9script_func()
var lines =<< trim END
vim9script
func Func(arg)
echo a:arg
endfunc
Func('text')
END
writefile(lines, 'XVim9Func', 'D')
so XVim9Func
enddef
let s:funcResult = 0
def FuncNoArgNoRet()
s:funcResult = 11
enddef
def FuncNoArgRetNumber(): number
s:funcResult = 22
return 1234
enddef
def FuncNoArgRetString(): string
s:funcResult = 45
return 'text'
enddef
def FuncOneArgNoRet(arg: number)
s:funcResult = arg
enddef
def FuncOneArgRetNumber(arg: number): number
s:funcResult = arg
return arg
enddef
def FuncTwoArgNoRet(one: bool, two: number)
s:funcResult = two
enddef
def s:FuncOneArgRetString(arg: string): string
return arg
enddef
def s:FuncOneArgRetAny(arg: any): any
return arg
enddef
def Test_func_type()
var Ref1: func()
s:funcResult = 0
Ref1 = g:FuncNoArgNoRet
Ref1()
s:funcResult->assert_equal(11)
var Ref2: func
s:funcResult = 0
Ref2 = g:FuncNoArgNoRet
Ref2()
s:funcResult->assert_equal(11)
s:funcResult = 0
Ref2 = g:FuncOneArgNoRet
Ref2(12)
s:funcResult->assert_equal(12)
s:funcResult = 0
Ref2 = g:FuncNoArgRetNumber
Ref2()->assert_equal(1234)
s:funcResult->assert_equal(22)
s:funcResult = 0
Ref2 = g:FuncOneArgRetNumber
Ref2(13)->assert_equal(13)
s:funcResult->assert_equal(13)
enddef
def Test_repeat_return_type()
var res = 0
for n in repeat([1], 3)
res += n
endfor
res->assert_equal(3)
res = 0
for n in repeat(0z01, 3)->blob2list()
res += n
endfor
res->assert_equal(3)
res = 0
for n in add([1, 2], 3)
res += n
endfor
res->assert_equal(6)
enddef
def Test_argv_return_type()
next fileone filetwo
var res = ''
for name in argv()
res ..= name
endfor
res->assert_equal('fileonefiletwo')
enddef
def Test_func_type_part()
var RefVoid: func: void
RefVoid = g:FuncNoArgNoRet
RefVoid = g:FuncOneArgNoRet
v9.CheckDefFailure(['var RefVoid: func: void', 'RefVoid = g:FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
v9.CheckDefFailure(['var RefVoid: func: void', 'RefVoid = g:FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
var RefAny: func(): any
RefAny = g:FuncNoArgRetNumber
RefAny = g:FuncNoArgRetString
v9.CheckDefFailure(['var RefAny: func(): any', 'RefAny = g:FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
v9.CheckDefFailure(['var RefAny: func(): any', 'RefAny = g:FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
var RefAnyNoArgs: func: any = RefAny
var RefNr: func: number
RefNr = g:FuncNoArgRetNumber
RefNr = g:FuncOneArgRetNumber
v9.CheckDefFailure(['var RefNr: func: number', 'RefNr = g:FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
v9.CheckDefFailure(['var RefNr: func: number', 'RefNr = g:FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
var RefStr: func: string
RefStr = g:FuncNoArgRetString
RefStr = FuncOneArgRetString
v9.CheckDefFailure(['var RefStr: func: string', 'RefStr = g:FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
v9.CheckDefFailure(['var RefStr: func: string', 'RefStr = g:FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
enddef
def Test_func_type_fails()
v9.CheckDefFailure(['var ref1: func()'], 'E704:')
v9.CheckDefFailure(['var Ref1: func()', 'Ref1 = g:FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
v9.CheckDefFailure(['var Ref1: func()', 'Ref1 = g:FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
v9.CheckDefFailure(['var Ref1: func()', 'Ref1 = g:FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
v9.CheckDefFailure(['var Ref1: func(bool)', 'Ref1 = g:FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
v9.CheckDefFailure(['var Ref1: func(?bool)', 'Ref1 = g:FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
v9.CheckDefFailure(['var Ref1: func(...bool)', 'Ref1 = g:FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(...bool) but got func(bool, number)')
v9.CheckDefFailure(['var RefWrong: func(string ,number)'], 'E1068:')
v9.CheckDefFailure(['var RefWrong: func(string,number)'], 'E1069:')
v9.CheckDefFailure(['var RefWrong: func(bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool)'], 'E1005:')
v9.CheckDefFailure(['var RefWrong: func(bool):string'], 'E1069:')
enddef
def Test_func_return_type()
var nr: number
nr = g:FuncNoArgRetNumber()
nr->assert_equal(1234)
nr = FuncOneArgRetAny(122)
nr->assert_equal(122)
var str: string
str = FuncOneArgRetAny('yes')
str->assert_equal('yes')
v9.CheckDefFailure(['var str: string', 'str = g:FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
enddef
def Test_func_common_type()
def FuncOne(n: number): number
return n
enddef
def FuncTwo(s: string): number
return len(s)
enddef
def FuncThree(n: number, s: string): number
return n + len(s)
enddef
var list = [FuncOne, FuncTwo, FuncThree]
assert_equal(8, list[0](8))
assert_equal(4, list[1]('word'))
assert_equal(7, list[2](3, 'word'))
enddef
def s:MultiLine(
arg1: string,
arg2 = 1234,
...rest: list<string>
): string
return arg1 .. arg2 .. join(rest, '-')
enddef
def MultiLineComment(
arg1: string, # comment
arg2 = 1234, # comment
...rest: list<string> # comment
): string # comment
return arg1 .. arg2 .. join(rest, '-')
enddef
def Test_multiline()
MultiLine('text')->assert_equal('text1234')
MultiLine('text', 777)->assert_equal('text777')
MultiLine('text', 777, 'one')->assert_equal('text777one')
MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
enddef
func Test_multiline_not_vim9()
call s:MultiLine('text')->assert_equal('text1234')
call s:MultiLine('text', 777)->assert_equal('text777')
call s:MultiLine('text', 777, 'one')->assert_equal('text777one')
call s:MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
endfunc
" When using CheckScriptFailure() for the below test, E1010 is generated instead
" of E1056.
func Test_E1056_1059()
let caught_1056 = 0
try
def F():
return 1
enddef
catch /E1056:/
let caught_1056 = 1
endtry
eval caught_1056->assert_equal(1)
let caught_1059 = 0
try
def F5(items : list)
echo 'a'
enddef
catch /E1059:/
let caught_1059 = 1
endtry
eval caught_1059->assert_equal(1)
endfunc
func DelMe()
echo 'DelMe'
endfunc
def Test_error_reporting()
# comment lines at the start of the function
var lines =<< trim END
" comment
def Func()
# comment
# comment
invalid
enddef
defcompile
END
writefile(lines, 'Xdef', 'D')
try
source Xdef
assert_report('should have failed')
catch /E476:/
v:exception->assert_match('Invalid command: invalid')
v:throwpoint->assert_match(', line 3$')
endtry
delfunc! g:Func
# comment lines after the start of the function
lines =<< trim END
" comment
def Func()
var x = 1234
# comment
# comment
invalid
enddef
defcompile
END
writefile(lines, 'Xdef')
try
source Xdef
assert_report('should have failed')
catch /E476:/
v:exception->assert_match('Invalid command: invalid')
v:throwpoint->assert_match(', line 4$')
endtry
delfunc! g:Func
lines =<< trim END
vim9script
def Func()
var db = {foo: 1, bar: 2}
# comment
var x = db.asdf
enddef
defcompile
Func()
END
writefile(lines, 'Xdef')
try
source Xdef
assert_report('should have failed')
catch /E716:/
v:throwpoint->assert_match('_Func, line 3$')
endtry
delfunc! g:Func
enddef
def Test_deleted_function()
v9.CheckDefExecFailure([
'var RefMe: func = function("g:DelMe")',
'delfunc g:DelMe',
'echo RefMe()'], 'E117:')
enddef
def Test_unknown_function()
v9.CheckDefExecFailure([
'var Ref: func = function("NotExist")',
'delfunc g:NotExist'], 'E700:')
enddef
def s:RefFunc(Ref: func(any): any): string
return Ref('more')
enddef
def Test_closure_simple()
var local = 'some '
RefFunc((s) => local .. s)->assert_equal('some more')
enddef
def s:MakeRef()
var local = 'some '
g:Ref = (s) => local .. s
enddef
def Test_closure_ref_after_return()
MakeRef()
g:Ref('thing')->assert_equal('some thing')
unlet g:Ref
enddef
def s:MakeTwoRefs()
var local = ['some']
g:Extend = (s) => local->add(s)
g:Read = () => local
enddef
def Test_closure_two_refs()
MakeTwoRefs()
join(g:Read(), ' ')->assert_equal('some')
g:Extend('more')
join(g:Read(), ' ')->assert_equal('some more')
g:Extend('even')
join(g:Read(), ' ')->assert_equal('some more even')
unlet g:Extend
unlet g:Read
enddef
def s:ReadRef(Ref: func(): list<string>): string
return join(Ref(), ' ')
enddef
def s:ExtendRef(Ref: func(string): list<string>, add: string)
Ref(add)
enddef
def Test_closure_two_indirect_refs()
MakeTwoRefs()
ReadRef(g:Read)->assert_equal('some')
ExtendRef(g:Extend, 'more')
ReadRef(g:Read)->assert_equal('some more')
ExtendRef(g:Extend, 'even')
ReadRef(g:Read)->assert_equal('some more even')
unlet g:Extend
unlet g:Read
enddef
def s:MakeArgRefs(theArg: string)
var local = 'loc_val'
g:UseArg = (s) => theArg .. '/' .. local .. '/' .. s
enddef
def s:MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
var local = 'the_loc'
g:UseVararg = (s) => theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)
enddef
def Test_closure_using_argument()
MakeArgRefs('arg_val')
g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
MakeArgRefsVarargs('arg_val', 'one', 'two')
g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
unlet g:UseArg
unlet g:UseVararg
var lines =<< trim END
vim9script
def Test(Fun: func(number): number): list<number>
return map([1, 2, 3], (_, i) => Fun(i))
enddef
def Inc(nr: number): number
return nr + 2
enddef
assert_equal([3, 4, 5], Test(Inc))
END
v9.CheckScriptSuccess(lines)
enddef
def s:MakeGetAndAppendRefs()
var local = 'a'
def Append(arg: string)
local ..= arg
enddef
g:Append = Append
def Get(): string
return local
enddef
g:Get = Get
enddef
def Test_closure_append_get()
MakeGetAndAppendRefs()
g:Get()->assert_equal('a')
g:Append('-b')
g:Get()->assert_equal('a-b')
g:Append('-c')
g:Get()->assert_equal('a-b-c')
unlet g:Append
unlet g:Get
enddef
def Test_nested_closure()
var local = 'text'
def Closure(arg: string): string
return local .. arg
enddef
Closure('!!!')->assert_equal('text!!!')
enddef
func s:GetResult(Ref)
return a:Ref('some')
endfunc
def Test_call_closure_not_compiled()
var text = 'text'
g:Ref = (s) => s .. text
GetResult(g:Ref)->assert_equal('sometext')
enddef
def Test_double_closure_fails()
var lines =<< trim END
vim9script
def Func()
var name = 0
for i in range(2)
timer_start(0, () => name)
endfor
enddef
Func()
END
v9.CheckScriptSuccess(lines)
enddef
def Test_nested_closure_used()
var lines =<< trim END
vim9script
def Func()
var x = 'hello'
var Closure = () => x
g:Myclosure = () => Closure()
enddef
Func()
assert_equal('hello', g:Myclosure())
END
v9.CheckScriptSuccess(lines)
enddef
def Test_nested_closure_fails()
var lines =<< trim END
vim9script
def FuncA()
FuncB(0)
enddef
def FuncB(n: number): list<string>
return map([0], (_, v) => n)
enddef
FuncA()
END
v9.CheckScriptFailure(lines, 'E1012:')
enddef
def Run_Test_closure_in_for_loop_fails()
var lines =<< trim END
vim9script
redraw
for n in [0]
# time should be enough for startup to finish
timer_start(200, (_) => {
echo n
})
endfor
END
writefile(lines, 'XTest_closure_fails', 'D')
# Check that an error shows
var buf = g:RunVimInTerminal('-S XTest_closure_fails', {rows: 6, wait_for_ruler: 0})
g:VerifyScreenDump(buf, 'Test_vim9_closure_fails', {wait: 3000})
# clean up
g:StopVimInTerminal(buf)
enddef
func Test_closure_in_for_loop_fails()
CheckScreendump
call Run_Test_closure_in_for_loop_fails()
endfunc
def Test_global_closure()
var lines =<< trim END
vim9script
def ReverseEveryNLines(n: number, line1: number, line2: number)
var mods = 'sil keepj keepp lockm '
var range = ':' .. line1 .. ',' .. line2
def g:Offset(): number
var offset = (line('.') - line1 + 1) % n
return offset != 0 ? offset : n
enddef
exe mods .. range .. 'g/^/exe "m .-" .. g:Offset()'
enddef
new
repeat(['aaa', 'bbb', 'ccc'], 3)->setline(1)
ReverseEveryNLines(3, 1, 9)
END
v9.CheckScriptSuccess(lines)
var expected = repeat(['ccc', 'bbb', 'aaa'], 3)
assert_equal(expected, getline(1, 9))
bwipe!
enddef
def Test_global_closure_called_directly()
var lines =<< trim END
vim9script
def Outer()
var x = 1
def g:Inner()
var y = x
x += 1
assert_equal(1, y)
enddef
g:Inner()
assert_equal(2, x)
enddef
Outer()
END
v9.CheckScriptSuccess(lines)
delfunc g:Inner
enddef
def Test_closure_called_from_legacy()
var lines =<< trim END
vim9script
def Func()
var outer = 'foo'
var F = () => {
outer = 'bar'
}
execute printf('call %s()', string(F))
enddef
Func()
END
v9.CheckScriptFailure(lines, 'E1248')
enddef
def Test_failure_in_called_function()
# this was using the frame index as the return value
var lines =<< trim END
vim9script
au TerminalWinOpen * eval [][0]
def PopupTerm(a: any)
# make sure typvals on stack are string
['a', 'b', 'c', 'd', 'e', 'f', 'g']->join()
FireEvent()
enddef
def FireEvent()
do TerminalWinOpen
enddef
# use try/catch to make eval fail
try
call PopupTerm(0)
catch
endtry
au! TerminalWinOpen
END
v9.CheckScriptSuccess(lines)
enddef
def Test_nested_lambda()
var lines =<< trim END
vim9script
def Func()
var x = 4
var Lambda1 = () => 7
var Lambda2 = () => [Lambda1(), x]
var res = Lambda2()
assert_equal([7, 4], res)
enddef
Func()
END
v9.CheckScriptSuccess(lines)
enddef
def Test_double_nested_lambda()
var lines =<< trim END
vim9script
def F(head: string): func(string): func(string): string
return (sep: string): func(string): string => ((tail: string): string => {
return head .. sep .. tail
})
enddef
assert_equal('hello-there', F('hello')('-')('there'))
END
v9.CheckScriptSuccess(lines)
enddef
def Test_nested_inline_lambda()
var lines =<< trim END
vim9script
def F(text: string): func(string): func(string): string
return (arg: string): func(string): string => ((sep: string): string => {
return sep .. arg .. text
})
enddef
assert_equal('--there++', F('++')('there')('--'))
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
echo range(4)->mapnew((_, v) => {
return range(v) ->mapnew((_, s) => {
return string(s)
})
})
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def Func()
range(10)
->mapnew((_, _) => ({
key: range(10)->mapnew((_, _) => {
return ' '
}),
}))
enddef
defcomp
END
v9.CheckScriptSuccess(lines)
enddef
def Shadowed(): list<number>
var FuncList: list<func: number> = [() => 42]
return FuncList->mapnew((_, Shadowed) => Shadowed())
enddef
def Test_lambda_arg_shadows_func()
assert_equal([42], g:Shadowed())
enddef
def Test_compiling_referenced_func_no_shadow()
var lines =<< trim END
vim9script
def InitializeReply(lspserver: dict<any>)
enddef
def ProcessReply(lspserver: dict<any>)
var lsp_reply_handlers: dict<func> =
{ 'initialize': InitializeReply }
lsp_reply_handlers['initialize'](lspserver)
enddef
call ProcessReply({})
END
v9.CheckScriptSuccess(lines)
enddef
def s:Line_continuation_in_def(dir: string = ''): string
var path: string = empty(dir)
\ ? 'empty'
\ : 'full'
return path
enddef
def Test_line_continuation_in_def()
Line_continuation_in_def('.')->assert_equal('full')
enddef
def Test_script_var_in_lambda()
var lines =<< trim END
vim9script
var script = 'test'
assert_equal(['test'], map(['one'], (_, _) => script))
END
v9.CheckScriptSuccess(lines)
enddef
def s:Line_continuation_in_lambda(): list<string>
var x = range(97, 100)
->mapnew((_, v) => nr2char(v)
->toupper())
->reverse()
return x
enddef
def Test_line_continuation_in_lambda()
Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
var lines =<< trim END
vim9script
var res = [{n: 1, m: 2, s: 'xxx'}]
->mapnew((_, v: dict<any>): string => printf('%d:%d:%s',
v.n,
v.m,
substitute(v.s, '.*', 'yyy', '')
))
assert_equal(['1:2:yyy'], res)
END
v9.CheckScriptSuccess(lines)
enddef
def Test_list_lambda()
timer_start(1000, (_) => 0)
var body = execute(timer_info()[0].callback
->string()
->substitute("('", ' ', '')
->substitute("')", '', '')
->substitute('function\zs', ' ', ''))
assert_match('def <lambda>\d\+(_: any): number\n1 return 0\n enddef', body)
enddef
def Test_lambda_block_variable()
var lines =<< trim END
vim9script
var flist: list<func>
for i in range(10)
var inloop = i
flist[i] = () => inloop
endfor
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
if true
var outloop = 5
var flist: list<func>
for i in range(10)
flist[i] = () => outloop
endfor
endif
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
if true
var outloop = 5
endif
var flist: list<func>
for i in range(10)
flist[i] = () => outloop
endfor
END
v9.CheckScriptFailure(lines, 'E1001: Variable not found: outloop', 1)
lines =<< trim END
vim9script
for i in range(10)
var Ref = () => 0
endfor
assert_equal(0, ((i) => 0)(0))
END
v9.CheckScriptSuccess(lines)
enddef
def Test_legacy_lambda()
legacy echo {x -> 'hello ' .. x}('foo')
var lines =<< trim END
echo {x -> 'hello ' .. x}('foo')
END
v9.CheckDefAndScriptFailure(lines, 'E720:')
lines =<< trim END
vim9script
def Func()
echo (() => 'no error')()
enddef
legacy call s:Func()
END
v9.CheckScriptSuccess(lines)
enddef
def Test_legacy()
var lines =<< trim END
vim9script
func g:LegacyFunction()
let g:legacyvar = 1
endfunc
def Testit()
legacy call g:LegacyFunction()
enddef
Testit()
assert_equal(1, g:legacyvar)
unlet g:legacyvar
delfunc g:LegacyFunction
END
v9.CheckScriptSuccess(lines)
enddef
def Test_legacy_errors()
for cmd in ['if', 'elseif', 'else', 'endif',
'for', 'endfor', 'continue', 'break',
'while', 'endwhile',
'try', 'catch', 'finally', 'endtry']
v9.CheckDefFailure(['legacy ' .. cmd .. ' expr'], 'E1189:')
endfor
enddef
def Test_call_legacy_with_dict()
var lines =<< trim END
vim9script
func Legacy() dict
let g:result = self.value
endfunc
def TestDirect()
var d = {value: 'yes', func: Legacy}
d.func()
enddef
TestDirect()
assert_equal('yes', g:result)
unlet g:result
def TestIndirect()
var d = {value: 'foo', func: Legacy}
var Fi = d.func
Fi()
enddef
TestIndirect()
assert_equal('foo', g:result)
unlet g:result
var d = {value: 'bar', func: Legacy}
d.func()
assert_equal('bar', g:result)
unlet g:result
END
v9.CheckScriptSuccess(lines)
enddef
def s:DoFilterThis(a: string): list<string>
# closure nested inside another closure using argument
var Filter = (l) => filter(l, (_, v) => stridx(v, a) == 0)
return ['x', 'y', 'a', 'x2', 'c']->Filter()
enddef
def Test_nested_closure_using_argument()
assert_equal(['x', 'x2'], DoFilterThis('x'))
enddef
def Test_triple_nested_closure()
var what = 'x'
var Match = (val: string, cmp: string): bool => stridx(val, cmp) == 0
var Filter = (l) => filter(l, (_, v) => Match(v, what))
assert_equal(['x', 'x2'], ['x', 'y', 'a', 'x2', 'c']->Filter())
enddef
func Test_silent_echo()
CheckScreendump
call Run_Test_silent_echo()
endfunc
def Run_Test_silent_echo()
var lines =<< trim END
vim9script
def EchoNothing()
silent echo ''
enddef
defcompile
END
writefile(lines, 'XTest_silent_echo', 'D')
# Check that the balloon shows up after a mouse move
var buf = g:RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
term_sendkeys(buf, ":abc")
g:VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
# clean up
g:StopVimInTerminal(buf)
enddef
def SilentlyError()
execute('silent! invalid')
g:did_it = 'yes'
enddef
func s:UserError()
silent! invalid
endfunc
def SilentlyUserError()
UserError()
g:did_it = 'yes'
enddef
" This can't be a :def function, because the assert would not be reached.
func Test_ignore_silent_error()
let g:did_it = 'no'
call SilentlyError()
call assert_equal('yes', g:did_it)
let g:did_it = 'no'
call SilentlyUserError()
call assert_equal('yes', g:did_it)
unlet g:did_it
endfunc
def Test_ignore_silent_error_in_filter()
var lines =<< trim END
vim9script
def Filter(winid: number, key: string): bool
if key == 'o'
silent! eval [][0]
return true
endif
return popup_filter_menu(winid, key)
enddef
popup_create('popup', {filter: Filter})
feedkeys("o\r", 'xnt')
END
v9.CheckScriptSuccess(lines)
enddef
def s:Fibonacci(n: number): number
if n < 2
return n
else
return Fibonacci(n - 1) + Fibonacci(n - 2)
endif
enddef
def Test_recursive_call()
Fibonacci(20)->assert_equal(6765)
enddef
def s:TreeWalk(dir: string): list<any>
return readdir(dir)->mapnew((_, val) =>
fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
? {[val]: TreeWalk(dir .. '/' .. val)}
: val
)
enddef
def Test_closure_in_map()
mkdir('XclosureDir/tdir', 'pR')
writefile(['111'], 'XclosureDir/file1')
writefile(['222'], 'XclosureDir/file2')
writefile(['333'], 'XclosureDir/tdir/file3')
TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {tdir: ['file3']}])
enddef
def Test_invalid_function_name()
var lines =<< trim END
vim9script
def s: list<string>
END
v9.CheckScriptFailure(lines, 'E1268:')
lines =<< trim END
vim9script
def g: list<string>
END
v9.CheckScriptFailure(lines, 'E129:')
lines =<< trim END
vim9script
def <SID>: list<string>
END
v9.CheckScriptFailure(lines, 'E884:')
lines =<< trim END
vim9script
def F list<string>
END
v9.CheckScriptFailure(lines, 'E488:')
enddef
def Test_partial_call()
var lines =<< trim END
var Xsetlist: func
Xsetlist = function('setloclist', [0])
Xsetlist([], ' ', {title: 'test'})
getloclist(0, {title: 1})->assert_equal({title: 'test'})
Xsetlist = function('setloclist', [0, [], ' '])
Xsetlist({title: 'test'})
getloclist(0, {title: 1})->assert_equal({title: 'test'})
Xsetlist = function('setqflist')
Xsetlist([], ' ', {title: 'test'})
getqflist({title: 1})->assert_equal({title: 'test'})
Xsetlist = function('setqflist', [[], ' '])
Xsetlist({title: 'test'})
getqflist({title: 1})->assert_equal({title: 'test'})
var Len: func: number = function('len', ['word'])
assert_equal(4, Len())
var RepeatFunc = function('repeat', ['o'])
assert_equal('ooooo', RepeatFunc(5))
END
v9.CheckDefAndScriptSuccess(lines)
lines =<< trim END
vim9script
def Foo(Parser: any)
enddef
var Expr: func(dict<any>): dict<any>
const Call = Foo(Expr)
END
v9.CheckScriptFailure(lines, 'E1031:')
enddef
def Test_partial_double_nested()
var idx = 123
var Get = () => idx
var Ref = function(Get, [])
var RefRef = function(Ref, [])
assert_equal(123, RefRef())
enddef
def Test_partial_null_function()
var lines =<< trim END
var d: dict<func> = {f: null_function}
var Ref = d.f
assert_equal('func(...): unknown', typename(Ref))
END
v9.CheckDefAndScriptSuccess(lines)
enddef
def Test_cmd_modifier()
tab echo '0'
v9.CheckDefFailure(['5tab echo 3'], 'E16:')
enddef
def Test_restore_modifiers()
# check that when compiling a :def function command modifiers are not messed
# up.
var lines =<< trim END
vim9script
set eventignore=
autocmd QuickFixCmdPost * copen
def AutocmdsDisabled()
eval 1 + 2
enddef
func Func()
noautocmd call s:AutocmdsDisabled()
let g:ei_after = &eventignore
endfunc
Func()
END
v9.CheckScriptSuccess(lines)
g:ei_after->assert_equal('')
enddef
def StackTop()
eval 1 + 2
eval 2 + 3
# call not on fourth line
g:StackBot()
enddef
def StackBot()
# throw an error
eval [][0]
enddef
def Test_callstack_def()
try
g:StackTop()
catch
v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
endtry
enddef
" Re-using spot for variable used in block
def Test_block_scoped_var()
var lines =<< trim END
vim9script
def Func()
var x = ['a', 'b', 'c']
if 1
var y = 'x'
map(x, (_, _) => y)
endif
var z = x
assert_equal(['x', 'x', 'x'], z)
enddef
Func()
END
v9.CheckScriptSuccess(lines)
enddef
def Test_reset_did_emsg()
var lines =<< trim END
@s = 'blah'
au BufWinLeave * #
def Func()
var winid = popup_create('popup', {})
exe '*s'
popup_close(winid)
enddef
Func()
END
v9.CheckScriptFailure(lines, 'E492:', 8)
delfunc! g:Func
enddef
def Test_did_emsg_reset()
# executing an autocommand resets did_emsg, this should not result in a
# builtin function considered failing
var lines =<< trim END
vim9script
au BufWinLeave * #
def Func()
popup_menu('', {callback: (a, b) => popup_create('', {})->popup_close()})
eval [][0]
enddef
nno <F3> <cmd>call <sid>Func()<cr>
feedkeys("\<F3>\e", 'xt')
END
writefile(lines, 'XemsgReset', 'D')
assert_fails('so XemsgReset', ['E684:', 'E684:'], lines, 2)
nunmap <F3>
au! BufWinLeave
enddef
def Test_abort_with_silent_call()
var lines =<< trim END
vim9script
g:result = 'none'
def Func()
g:result += 3
g:result = 'yes'
enddef
# error is silenced, but function aborts on error
silent! Func()
assert_equal('none', g:result)
unlet g:result
END
v9.CheckScriptSuccess(lines)
enddef
def Test_continues_with_silent_error()
var lines =<< trim END
vim9script
g:result = 'none'
def Func()
silent! g:result += 3
g:result = 'yes'
enddef
# error is silenced, function does not abort
Func()
assert_equal('yes', g:result)
unlet g:result
END
v9.CheckScriptSuccess(lines)
enddef
def Test_abort_even_with_silent()
var lines =<< trim END
vim9script
g:result = 'none'
def Func()
eval {-> ''}() .. '' .. {}['X']
g:result = 'yes'
enddef
silent! Func()
assert_equal('none', g:result)
unlet g:result
END
v9.CheckScriptSuccess(lines)
enddef
def Test_cmdmod_silent_restored()
var lines =<< trim END
vim9script
def Func()
g:result = 'none'
silent! g:result += 3
g:result = 'none'
g:result += 3
enddef
Func()
END
# can't use CheckScriptFailure, it ignores the :silent!
var fname = 'Xdefsilent'
writefile(lines, fname, 'D')
var caught = 'no'
try
exe 'source ' .. fname
catch /E1030:/
caught = 'yes'
assert_match('Func, line 4', v:throwpoint)
endtry
assert_equal('yes', caught)
enddef
def Test_cmdmod_silent_nested()
var lines =<< trim END
vim9script
var result = ''
def Error()
result ..= 'Eb'
eval [][0]
result ..= 'Ea'
enddef
def Crash()
result ..= 'Cb'
sil! Error()
result ..= 'Ca'
enddef
Crash()
assert_equal('CbEbEaCa', result)
END
v9.CheckScriptSuccess(lines)
enddef
def Test_dict_member_with_silent()
var lines =<< trim END
vim9script
g:result = 'none'
var d: dict<any>
def Func()
try
g:result = map([], (_, v) => ({}[v]))->join() .. d['']
catch
endtry
enddef
silent! Func()
assert_equal('0', g:result)
unlet g:result
END
v9.CheckScriptSuccess(lines)
enddef
def Test_skip_cmds_with_silent()
var lines =<< trim END
vim9script
def Func(b: bool)
Crash()
enddef
def Crash()
sil! :/not found/d _
sil! :/not found/put _
enddef
Func(true)
END
v9.CheckScriptSuccess(lines)
enddef
def Test_opfunc()
nnoremap <F3> <cmd>set opfunc=g:Opfunc<cr>g@
def g:Opfunc(_: any): string
setline(1, 'ASDF')
return ''
enddef
new
setline(1, 'asdf')
feedkeys("\<F3>$", 'x')
assert_equal('ASDF', getline(1))
bwipe!
nunmap <F3>
enddef
func Test_opfunc_error()
CheckScreendump
call Run_Test_opfunc_error()
endfunc
def Run_Test_opfunc_error()
# test that the error from Opfunc() is displayed right away
var lines =<< trim END
vim9script
def Opfunc(type: string)
try
eval [][0]
catch /nothing/ # error not caught
endtry
enddef
&operatorfunc = Opfunc
nnoremap <expr> l <SID>L()
def L(): string
return 'l'
enddef
'x'->repeat(10)->setline(1)
feedkeys('g@l', 'n')
feedkeys('llll')
END
call writefile(lines, 'XTest_opfunc_error', 'D')
var buf = g:RunVimInTerminal('-S XTest_opfunc_error', {rows: 6, wait_for_ruler: 0})
g:WaitForAssert(() => assert_match('Press ENTER', term_getline(buf, 6)))
g:WaitForAssert(() => assert_match('E684: List index out of range: 0', term_getline(buf, 5)))
# clean up
g:StopVimInTerminal(buf)
enddef
" this was crashing on exit
def Test_nested_lambda_in_closure()
var lines =<< trim END
vim9script
command WriteDone writefile(['Done'], 'XnestedDone')
def Outer()
def g:Inner()
echo map([1, 2, 3], {_, v -> v + 1})
enddef
g:Inner()
enddef
defcompile
# not reached
END
if !g:RunVim([], lines, '--clean -c WriteDone -c quit')
return
endif
assert_equal(['Done'], readfile('XnestedDone'))
delete('XnestedDone')
enddef
def Test_nested_closure_funcref()
var lines =<< trim END
vim9script
def Func()
var n: number
def Nested()
++n
enddef
Nested()
g:result_one = n
var Ref = function(Nested)
Ref()
g:result_two = n
enddef
Func()
END
v9.CheckScriptSuccess(lines)
assert_equal(1, g:result_one)
assert_equal(2, g:result_two)
unlet g:result_one g:result_two
enddef
def Test_nested_closure_in_dict()
var lines =<< trim END
vim9script
def Func(): dict<any>
var n: number
def Inc(): number
++n
return n
enddef
return {inc: function(Inc)}
enddef
disas Func
var d = Func()
assert_equal(1, d.inc())
assert_equal(2, d.inc())
END
v9.CheckScriptSuccess(lines)
enddef
def Test_script_local_other_script()
var lines =<< trim END
function LegacyJob()
let FuncRef = function('s:close_cb')
endfunction
function s:close_cb(...)
endfunction
END
lines->writefile('Xlegacy.vim', 'D')
source Xlegacy.vim
g:LegacyJob()
g:LegacyJob()
g:LegacyJob()
delfunc g:LegacyJob
enddef
def Test_check_func_arg_types()
var lines =<< trim END
vim9script
def F1(x: string): string
return x
enddef
def F2(x: number): number
return x + 1
enddef
def G(Fg: func): dict<func>
return {f: Fg}
enddef
def H(d: dict<func>): string
return d.f('a')
enddef
END
v9.CheckScriptSuccess(lines + ['echo H(G(F1))'])
v9.CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
v9.CheckScriptFailure(lines + ['def SomeFunc(ff: func)', 'enddef'], 'E704:')
enddef
def Test_call_func_with_null()
var lines =<< trim END
def Fstring(v: string)
assert_equal(null_string, v)
enddef
Fstring(null_string)
def Fblob(v: blob)
assert_equal(null_blob, v)
enddef
Fblob(null_blob)
def Flist(v: list<number>)
assert_equal(null_list, v)
enddef
Flist(null_list)
def Fdict(v: dict<number>)
assert_equal(null_dict, v)
enddef
Fdict(null_dict)
def Ffunc(Fv: func(number): number)
assert_equal(null_function, Fv)
enddef
Ffunc(null_function)
if has('channel')
def Fchannel(v: channel)
assert_equal(null_channel, v)
enddef
Fchannel(null_channel)
def Fjob(v: job)
assert_equal(null_job, v)
enddef
Fjob(null_job)
endif
END
v9.CheckDefAndScriptSuccess(lines)
enddef
def Test_null_default_argument()
var lines =<< trim END
def Fstring(v: string = null_string)
assert_equal(null_string, v)
enddef
Fstring()
def Fblob(v: blob = null_blob)
assert_equal(null_blob, v)
enddef
Fblob()
def Flist(v: list<number> = null_list)
assert_equal(null_list, v)
enddef
Flist()
def Fdict(v: dict<number> = null_dict)
assert_equal(null_dict, v)
enddef
Fdict()
def Ffunc(Fv: func(number): number = null_function)
assert_equal(null_function, Fv)
enddef
Ffunc()
if has('channel')
def Fchannel(v: channel = null_channel)
assert_equal(null_channel, v)
enddef
Fchannel()
def Fjob(v: job = null_job)
assert_equal(null_job, v)
enddef
Fjob()
endif
END
v9.CheckDefAndScriptSuccess(lines)
enddef
def Test_null_return()
var lines =<< trim END
def Fstring(): string
return null_string
enddef
assert_equal(null_string, Fstring())
def Fblob(): blob
return null_blob
enddef
assert_equal(null_blob, Fblob())
def Flist(): list<number>
return null_list
enddef
assert_equal(null_list, Flist())
def Fdict(): dict<number>
return null_dict
enddef
assert_equal(null_dict, Fdict())
def Ffunc(): func(number): number
return null_function
enddef
assert_equal(null_function, Ffunc())
if has('channel')
def Fchannel(): channel
return null_channel
enddef
assert_equal(null_channel, Fchannel())
def Fjob(): job
return null_job
enddef
assert_equal(null_job, Fjob())
endif
END
v9.CheckDefAndScriptSuccess(lines)
enddef
def Test_list_any_type_checked()
var lines =<< trim END
vim9script
def Foo()
--decl--
Bar(l)
enddef
def Bar(ll: list<dict<any>>)
enddef
Foo()
END
# "any" could be "dict<any>", thus OK
lines[2] = 'var l: list<any>'
v9.CheckScriptSuccess(lines)
lines[2] = 'var l: list<any> = []'
v9.CheckScriptSuccess(lines)
lines[2] = 'var l: list<any> = [11]'
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected list<dict<any>> but got list<number>', 2)
enddef
def Test_compile_error()
var lines =<< trim END
def g:Broken()
echo 'a' + {}
enddef
call g:Broken()
END
# First call: compilation error
v9.CheckScriptFailure(lines, 'E1051: Wrong argument type for +')
# Second call won't try compiling again
assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
delfunc g:Broken
# No error when compiling with :silent!
lines =<< trim END
def g:Broken()
echo 'a' + []
enddef
silent! defcompile
END
v9.CheckScriptSuccess(lines)
# Calling the function won't try compiling again
assert_fails('call g:Broken()', 'E1091: Function is not compiled: Broken')
delfunc g:Broken
enddef
def Test_ignored_argument()
var lines =<< trim END
vim9script
def Ignore(_, _): string
return 'yes'
enddef
assert_equal('yes', Ignore(1, 2))
func Ok(_)
return a:_
endfunc
assert_equal('ok', Ok('ok'))
func Oktoo()
let _ = 'too'
return _
endfunc
assert_equal('too', Oktoo())
assert_equal([[1], [2], [3]], range(3)->mapnew((_, v) => [v]->map((_, w) => w + 1)))
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
def Ignore(_: string): string
return _
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E1181:', 1)
lines =<< trim END
var _ = 1
END
v9.CheckDefAndScriptFailure(lines, 'E1181:', 1)
lines =<< trim END
var x = _
END
v9.CheckDefAndScriptFailure(lines, 'E1181:', 1)
enddef
def Test_too_many_arguments()
var lines =<< trim END
echo [0, 1, 2]->map(() => 123)
END
v9.CheckDefAndScriptFailure(lines, ['E176:', 'E1106: 2 arguments too many'], 1)
lines =<< trim END
echo [0, 1, 2]->map((_) => 123)
END
v9.CheckDefAndScriptFailure(lines, ['E176', 'E1106: One argument too many'], 1)
lines =<< trim END
vim9script
def OneArgument(arg: string)
echo arg
enddef
var Ref = OneArgument
Ref('a', 'b')
END
v9.CheckScriptFailure(lines, 'E118:')
enddef
def Test_funcref_with_base()
var lines =<< trim END
vim9script
def TwoArguments(str: string, nr: number)
echo str nr
enddef
var Ref = TwoArguments
Ref('a', 12)
'b'->Ref(34)
END
v9.CheckScriptSuccess(lines)
lines =<< trim END
vim9script
def TwoArguments(str: string, nr: number)
echo str nr
enddef
var Ref = TwoArguments
'a'->Ref('b')
END
v9.CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected number but got string', 6)
lines =<< trim END
vim9script
def TwoArguments(str: string, nr: number)
echo str nr
enddef
var Ref = TwoArguments
123->Ref(456)
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
lines =<< trim END
vim9script
def TwoArguments(nr: number, str: string)
echo str nr
enddef
var Ref = TwoArguments
123->Ref('b')
def AndNowCompiled()
456->Ref('x')
enddef
AndNowCompiled()
END
v9.CheckScriptSuccess(lines)
enddef
def Test_closing_brace_at_start_of_line()
var lines =<< trim END
def Func()
enddef
Func(
)
END
v9.CheckDefAndScriptSuccess(lines)
enddef
func s:CreateMydict()
let g:mydict = {}
func g:mydict.afunc()
let g:result = self.key
endfunc
endfunc
def Test_numbered_function_reference()
CreateMydict()
var output = execute('legacy func g:mydict.afunc')
var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
execute 'function(' .. funcName .. ', [], {key: 42})()'
# check that the function still exists
assert_equal(output, execute('legacy func g:mydict.afunc'))
unlet g:mydict
enddef
def Test_numbered_function_call()
var lines =<< trim END
let s:legacyscript = {}
func s:legacyscript.Helper() abort
return "Success"
endfunc
let g:legacyscript = deepcopy(s:legacyscript)
let g:legacy_result = eval("g:legacyscript.Helper()")
vim9cmd g:vim9_result = eval("g:legacyscript.Helper()")
END
v9.CheckScriptSuccess(lines)
assert_equal('Success', g:legacy_result)
assert_equal('Success', g:vim9_result)
unlet g:legacy_result
unlet g:vim9_result
enddef
def Test_go_beyond_end_of_cmd()
# this was reading the byte after the end of the line
var lines =<< trim END
def F()
cal
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E476:')
enddef
" Test for memory allocation failure when defining a new lambda
func Test_lambda_allocation_failure()
new
let lines =<< trim END
vim9script
g:Xlambda = (x): number => {
return x + 1
}
END
call setline(1, lines)
call test_alloc_fail(GetAllocId('get_func'), 0, 0)
call assert_fails('source', 'E342:')
call assert_false(exists('g:Xlambda'))
bw!
endfunc
def Test_lambda_argument_type_check()
var lines =<< trim END
vim9script
def Scan(ll: list<any>): func(func(any))
return (Emit: func(any)) => {
for e in ll
Emit(e)
endfor
}
enddef
def Sum(Cont: func(func(any))): any
var sum = 0.0
Cont((v: float) => { # <== NOTE: the lambda expects a float
sum += v
})
return sum
enddef
const ml = [3.0, 2, '7']
echo Scan(ml)->Sum()
END
v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected float but got string')
enddef
def Test_multiple_funcref()
# This was using a NULL pointer
var lines =<< trim END
vim9script
def A(F: func, ...args: list<any>): func
return funcref(F, args)
enddef
def B(F: func): func
return funcref(A, [F])
enddef
def Test(n: number)
enddef
const X = B(Test)
X(1)
END
v9.CheckScriptSuccess(lines)
# slightly different case
lines =<< trim END
vim9script
def A(F: func, ...args: list<any>): any
return call(F, args)
enddef
def B(F: func): func
return funcref(A, [F])
enddef
def Test(n: number)
enddef
const X = B(Test)
X(1)
END
v9.CheckScriptSuccess(lines)
enddef
def Test_cexpr_errmsg_line_number()
var lines =<< trim END
vim9script
def Func()
var qfl = {}
cexpr qfl
enddef
Func()
END
v9.CheckScriptFailure(lines, 'E777', 2)
enddef
def AddDefer(s: string)
g:deferred->extend([s])
enddef
def DeferTwo()
g:deferred->extend(['in Two'])
for n in range(3)
defer g:AddDefer('two' .. n)
endfor
g:deferred->extend(['end Two'])
enddef
def DeferOne()
g:deferred->extend(['in One'])
defer g:AddDefer('one')
g:DeferTwo()
g:deferred->extend(['end One'])
writefile(['text'], 'XdeferFile')
defer delete('XdeferFile')
enddef
def Test_defer()
g:deferred = []
g:DeferOne()
assert_equal(['in One', 'in Two', 'end Two', 'two2', 'two1', 'two0', 'end One', 'one'], g:deferred)
unlet g:deferred
assert_equal('', glob('XdeferFile'))
enddef
def Test_invalid_redir()
var lines =<< trim END
def Tone()
if 1
redi =>@ 0
redi END
endif
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E354:')
delfunc g:Tone
# this was reading past the end of the line
lines =<< trim END
def Ttwo()
if 0
redi =>@ 0
redi END
endif
enddef
defcompile
END
v9.CheckScriptFailure(lines, 'E354:')
delfunc g:Ttwo
enddef
func Test_keytyped_in_nested_function()
CheckRunVimInTerminal
call Run_Test_keytyped_in_nested_function()
endfunc
def Run_Test_keytyped_in_nested_function()
var lines =<< trim END
vim9script
autocmd CmdlineEnter * sample#Init()
exe 'set rtp=' .. getcwd() .. '/Xrtpdir'
END
writefile(lines, 'Xkeytyped', 'D')
var dir = 'Xrtpdir/autoload'
mkdir(dir, 'pR')
lines =<< trim END
vim9script
export def Init(): void
cnoremap <expr>" <SID>Quote('"')
enddef
def Quote(str: string): string
def InPair(): number
return 0
enddef
return str
enddef
END
writefile(lines, dir .. '/sample.vim')
var buf = g:RunVimInTerminal('-S Xkeytyped', {rows: 6})
term_sendkeys(buf, ':"')
g:VerifyScreenDump(buf, 'Test_keytyped_in_nested_func', {})
# clean up
term_sendkeys(buf, "\<Esc>")
g:StopVimInTerminal(buf)
enddef
" The following messes up syntax highlight, keep near the end.
if has('python3')
def Test_python3_command()
py3 import vim
py3 vim.command("g:done = 'yes'")
assert_equal('yes', g:done)
unlet g:done
enddef
def Test_python3_heredoc()
py3 << trim EOF
import vim
vim.vars['didit'] = 'yes'
EOF
assert_equal('yes', g:didit)
python3 << trim EOF
import vim
vim.vars['didit'] = 'again'
EOF
assert_equal('again', g:didit)
enddef
endif
if has('lua')
def Test_lua_heredoc()
g:d = {}
lua << trim EOF
x = vim.eval('g:d')
x['key'] = 'val'
EOF
assert_equal('val', g:d.key)
enddef
def Test_lua_heredoc_fails()
var lines = [
'vim9script',
'def ExeLua()',
'lua << trim EOLUA',
"x = vim.eval('g:nodict')",
'EOLUA',
'enddef',
'ExeLua()',
]
v9.CheckScriptFailure(lines, 'E121: Undefined variable: g:nodict')
enddef
endif
if has('perl')
def Test_perl_heredoc_nested()
var lines =<< trim END
vim9script
def F(): string
def G(): string
perl << EOF
EOF
return 'done'
enddef
return G()
enddef
assert_equal('done', F())
END
v9.CheckScriptSuccess(lines)
enddef
endif
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|