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
|
-- Test suite for testing interactions with API bindings
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
local nvim_prog = n.nvim_prog
local fn = n.fn
local api = n.api
local command = n.command
local dedent = t.dedent
local insert = n.insert
local clear = n.clear
local eq = t.eq
local ok = t.ok
local pesc = vim.pesc
local eval = n.eval
local feed = n.feed
local pcall_err = t.pcall_err
local exec_lua = n.exec_lua
local matches = t.matches
local exec = n.exec
local NIL = vim.NIL
local retry = t.retry
local next_msg = n.next_msg
local remove_trace = t.remove_trace
local mkdir_p = n.mkdir_p
local rmdir = n.rmdir
local write_file = t.write_file
local poke_eventloop = n.poke_eventloop
local assert_alive = n.assert_alive
describe('lua stdlib', function()
before_each(clear)
-- İ: `tolower("İ")` is `i` which has length 1 while `İ` itself has
-- length 2 (in bytes).
-- Ⱥ: `tolower("Ⱥ")` is `ⱥ` which has length 2 while `Ⱥ` itself has
-- length 3 (in bytes).
--
-- Note: 'i' !=? 'İ' and 'ⱥ' !=? 'Ⱥ' on some systems.
-- Note: Built-in Nvim comparison (on systems lacking `strcasecmp`) works
-- only on ASCII characters.
it('vim.stricmp', function()
eq(0, fn.luaeval('vim.stricmp("a", "A")'))
eq(0, fn.luaeval('vim.stricmp("A", "a")'))
eq(0, fn.luaeval('vim.stricmp("a", "a")'))
eq(0, fn.luaeval('vim.stricmp("A", "A")'))
eq(0, fn.luaeval('vim.stricmp("", "")'))
eq(0, fn.luaeval('vim.stricmp("\\0", "\\0")'))
eq(0, fn.luaeval('vim.stricmp("\\0\\0", "\\0\\0")'))
eq(0, fn.luaeval('vim.stricmp("\\0\\0\\0", "\\0\\0\\0")'))
eq(0, fn.luaeval('vim.stricmp("\\0\\0\\0A", "\\0\\0\\0a")'))
eq(0, fn.luaeval('vim.stricmp("\\0\\0\\0a", "\\0\\0\\0A")'))
eq(0, fn.luaeval('vim.stricmp("\\0\\0\\0a", "\\0\\0\\0a")'))
eq(0, fn.luaeval('vim.stricmp("a\\0", "A\\0")'))
eq(0, fn.luaeval('vim.stricmp("A\\0", "a\\0")'))
eq(0, fn.luaeval('vim.stricmp("a\\0", "a\\0")'))
eq(0, fn.luaeval('vim.stricmp("A\\0", "A\\0")'))
eq(0, fn.luaeval('vim.stricmp("\\0a", "\\0A")'))
eq(0, fn.luaeval('vim.stricmp("\\0A", "\\0a")'))
eq(0, fn.luaeval('vim.stricmp("\\0a", "\\0a")'))
eq(0, fn.luaeval('vim.stricmp("\\0A", "\\0A")'))
eq(0, fn.luaeval('vim.stricmp("\\0a\\0", "\\0A\\0")'))
eq(0, fn.luaeval('vim.stricmp("\\0A\\0", "\\0a\\0")'))
eq(0, fn.luaeval('vim.stricmp("\\0a\\0", "\\0a\\0")'))
eq(0, fn.luaeval('vim.stricmp("\\0A\\0", "\\0A\\0")'))
eq(-1, fn.luaeval('vim.stricmp("a", "B")'))
eq(-1, fn.luaeval('vim.stricmp("A", "b")'))
eq(-1, fn.luaeval('vim.stricmp("a", "b")'))
eq(-1, fn.luaeval('vim.stricmp("A", "B")'))
eq(-1, fn.luaeval('vim.stricmp("", "\\0")'))
eq(-1, fn.luaeval('vim.stricmp("\\0", "\\0\\0")'))
eq(-1, fn.luaeval('vim.stricmp("\\0\\0", "\\0\\0\\0")'))
eq(-1, fn.luaeval('vim.stricmp("\\0\\0\\0A", "\\0\\0\\0b")'))
eq(-1, fn.luaeval('vim.stricmp("\\0\\0\\0a", "\\0\\0\\0B")'))
eq(-1, fn.luaeval('vim.stricmp("\\0\\0\\0a", "\\0\\0\\0b")'))
eq(-1, fn.luaeval('vim.stricmp("a\\0", "B\\0")'))
eq(-1, fn.luaeval('vim.stricmp("A\\0", "b\\0")'))
eq(-1, fn.luaeval('vim.stricmp("a\\0", "b\\0")'))
eq(-1, fn.luaeval('vim.stricmp("A\\0", "B\\0")'))
eq(-1, fn.luaeval('vim.stricmp("\\0a", "\\0B")'))
eq(-1, fn.luaeval('vim.stricmp("\\0A", "\\0b")'))
eq(-1, fn.luaeval('vim.stricmp("\\0a", "\\0b")'))
eq(-1, fn.luaeval('vim.stricmp("\\0A", "\\0B")'))
eq(-1, fn.luaeval('vim.stricmp("\\0a\\0", "\\0B\\0")'))
eq(-1, fn.luaeval('vim.stricmp("\\0A\\0", "\\0b\\0")'))
eq(-1, fn.luaeval('vim.stricmp("\\0a\\0", "\\0b\\0")'))
eq(-1, fn.luaeval('vim.stricmp("\\0A\\0", "\\0B\\0")'))
eq(1, fn.luaeval('vim.stricmp("c", "B")'))
eq(1, fn.luaeval('vim.stricmp("C", "b")'))
eq(1, fn.luaeval('vim.stricmp("c", "b")'))
eq(1, fn.luaeval('vim.stricmp("C", "B")'))
eq(1, fn.luaeval('vim.stricmp("\\0", "")'))
eq(1, fn.luaeval('vim.stricmp("\\0\\0", "\\0")'))
eq(1, fn.luaeval('vim.stricmp("\\0\\0\\0", "\\0\\0")'))
eq(1, fn.luaeval('vim.stricmp("\\0\\0\\0\\0", "\\0\\0\\0")'))
eq(1, fn.luaeval('vim.stricmp("\\0\\0\\0C", "\\0\\0\\0b")'))
eq(1, fn.luaeval('vim.stricmp("\\0\\0\\0c", "\\0\\0\\0B")'))
eq(1, fn.luaeval('vim.stricmp("\\0\\0\\0c", "\\0\\0\\0b")'))
eq(1, fn.luaeval('vim.stricmp("c\\0", "B\\0")'))
eq(1, fn.luaeval('vim.stricmp("C\\0", "b\\0")'))
eq(1, fn.luaeval('vim.stricmp("c\\0", "b\\0")'))
eq(1, fn.luaeval('vim.stricmp("C\\0", "B\\0")'))
eq(1, fn.luaeval('vim.stricmp("c\\0", "B")'))
eq(1, fn.luaeval('vim.stricmp("C\\0", "b")'))
eq(1, fn.luaeval('vim.stricmp("c\\0", "b")'))
eq(1, fn.luaeval('vim.stricmp("C\\0", "B")'))
eq(1, fn.luaeval('vim.stricmp("\\0c", "\\0B")'))
eq(1, fn.luaeval('vim.stricmp("\\0C", "\\0b")'))
eq(1, fn.luaeval('vim.stricmp("\\0c", "\\0b")'))
eq(1, fn.luaeval('vim.stricmp("\\0C", "\\0B")'))
eq(1, fn.luaeval('vim.stricmp("\\0c\\0", "\\0B\\0")'))
eq(1, fn.luaeval('vim.stricmp("\\0C\\0", "\\0b\\0")'))
eq(1, fn.luaeval('vim.stricmp("\\0c\\0", "\\0b\\0")'))
eq(1, fn.luaeval('vim.stricmp("\\0C\\0", "\\0B\\0")'))
end)
--- @param prerel string | nil
local function test_vim_deprecate(prerel)
-- vim.deprecate(name, alternative, version, plugin, backtrace)
-- See MAINTAIN.md for the soft/hard deprecation policy
describe(('vim.deprecate prerel=%s,'):format(prerel or 'nil'), function()
it('plugin=nil', function()
local curver = exec_lua('return vim.version()') --[[@as {major:number, minor:number}]]
-- "0.10" or "0.10-dev+xxx"
local curstr = ('%s.%s%s'):format(curver.major, curver.minor, prerel or '')
-- "0.10" or "0.11"
local nextver = ('%s.%s'):format(curver.major, curver.minor + (prerel and 0 or 1))
local was_removed = prerel and 'was removed' or 'will be removed'
eq(
dedent([[
foo.bar() is deprecated, use zub.wooo{ok=yay} instead. :help deprecated
Feature was removed in Nvim %s]]):format(curstr),
exec_lua('return vim.deprecate(...)', 'foo.bar()', 'zub.wooo{ok=yay}', curstr)
)
-- Same message as above; skipped this time.
eq(vim.NIL, exec_lua('return vim.deprecate(...)', 'foo.bar()', 'zub.wooo{ok=yay}', curstr))
-- No error if soft-deprecated.
eq(
vim.NIL,
exec_lua('return vim.deprecate(...)', 'foo.baz()', 'foo.better_baz()', '0.99.0')
)
-- Show error if hard-deprecated.
eq(
dedent([[
foo.hard_dep() is deprecated, use vim.new_api() instead. :help deprecated
Feature %s in Nvim %s]]):format(was_removed, nextver),
exec_lua('return vim.deprecate(...)', 'foo.hard_dep()', 'vim.new_api()', nextver)
)
-- To be deleted in the next major version (1.0)
eq(
dedent [[
foo.baz() is deprecated. :help deprecated
Feature will be removed in Nvim 1.0]],
exec_lua [[ return vim.deprecate('foo.baz()', nil, '1.0') ]]
)
end)
it('plugin specified', function()
-- When `plugin` is specified, don't show ":help deprecated". #22235
eq(
dedent [[
foo.bar() is deprecated, use zub.wooo{ok=yay} instead.
Feature will be removed in my-plugin.nvim 0.3.0]],
exec_lua(
'return vim.deprecate(...)',
'foo.bar()',
'zub.wooo{ok=yay}',
'0.3.0',
'my-plugin.nvim',
false
)
)
-- plugins: no soft deprecation period
eq(
dedent [[
foo.bar() is deprecated, use zub.wooo{ok=yay} instead.
Feature will be removed in my-plugin.nvim 0.11.0]],
exec_lua(
'return vim.deprecate(...)',
'foo.bar()',
'zub.wooo{ok=yay}',
'0.11.0',
'my-plugin.nvim',
false
)
)
end)
end)
end
test_vim_deprecate()
test_vim_deprecate('-dev+g0000000')
it('vim.startswith', function()
eq(true, fn.luaeval('vim.startswith("123", "1")'))
eq(true, fn.luaeval('vim.startswith("123", "")'))
eq(true, fn.luaeval('vim.startswith("123", "123")'))
eq(true, fn.luaeval('vim.startswith("", "")'))
eq(false, fn.luaeval('vim.startswith("123", " ")'))
eq(false, fn.luaeval('vim.startswith("123", "2")'))
eq(false, fn.luaeval('vim.startswith("123", "1234")'))
matches(
'prefix: expected string, got nil',
pcall_err(exec_lua, 'return vim.startswith("123", nil)')
)
matches('s: expected string, got nil', pcall_err(exec_lua, 'return vim.startswith(nil, "123")'))
end)
it('vim.endswith', function()
eq(true, fn.luaeval('vim.endswith("123", "3")'))
eq(true, fn.luaeval('vim.endswith("123", "")'))
eq(true, fn.luaeval('vim.endswith("123", "123")'))
eq(true, fn.luaeval('vim.endswith("", "")'))
eq(false, fn.luaeval('vim.endswith("123", " ")'))
eq(false, fn.luaeval('vim.endswith("123", "2")'))
eq(false, fn.luaeval('vim.endswith("123", "1234")'))
matches(
'suffix: expected string, got nil',
pcall_err(exec_lua, 'return vim.endswith("123", nil)')
)
matches('s: expected string, got nil', pcall_err(exec_lua, 'return vim.endswith(nil, "123")'))
end)
it('vim.str_utfindex/str_byteindex', function()
exec_lua([[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ\000ъ"]])
local indices32 = {
[0] = 0,
1,
2,
3,
5,
7,
9,
10,
12,
13,
16,
19,
20,
23,
24,
28,
29,
33,
34,
35,
37,
38,
40,
42,
44,
46,
48,
49,
51,
}
local indices16 = {
[0] = 0,
1,
2,
3,
5,
7,
9,
10,
12,
13,
16,
19,
20,
23,
24,
28,
28,
29,
33,
33,
34,
35,
37,
38,
40,
42,
44,
46,
48,
49,
51,
}
for i, k in pairs(indices32) do
eq(k, exec_lua('return vim.str_byteindex(_G.test_text, ...)', i), i)
end
for i, k in pairs(indices16) do
eq(k, exec_lua('return vim.str_byteindex(_G.test_text, ..., true)', i), i)
end
eq(
'index out of range',
pcall_err(exec_lua, 'return vim.str_byteindex(_G.test_text, ...)', #indices32 + 1)
)
eq(
'index out of range',
pcall_err(exec_lua, 'return vim.str_byteindex(_G.test_text, ..., true)', #indices16 + 1)
)
local i32, i16 = 0, 0
local len = 51
for k = 0, len do
if indices32[i32] < k then
i32 = i32 + 1
end
if indices16[i16] < k then
i16 = i16 + 1
if indices16[i16 + 1] == indices16[i16] then
i16 = i16 + 1
end
end
eq({ i32, i16 }, exec_lua('return {vim.str_utfindex(_G.test_text, ...)}', k), k)
end
eq(
'index out of range',
pcall_err(exec_lua, 'return vim.str_utfindex(_G.test_text, ...)', len + 1)
)
end)
it('vim.str_utf_start', function()
exec_lua([[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]])
local expected_positions = {
0,
0,
0,
0,
-1,
0,
-1,
0,
-1,
0,
0,
-1,
0,
0,
-1,
-2,
0,
-1,
-2,
0,
0,
-1,
-2,
0,
0,
-1,
-2,
-3,
0,
0,
-1,
-2,
-3,
0,
0,
0,
-1,
0,
0,
-1,
0,
-1,
0,
-1,
0,
-1,
0,
-1,
}
eq(
expected_positions,
exec_lua([[
local start_codepoint_positions = {}
for idx = 1, #_G.test_text do
table.insert(start_codepoint_positions, vim.str_utf_start(_G.test_text, idx))
end
return start_codepoint_positions
]])
)
end)
it('vim.str_utf_end', function()
exec_lua([[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]])
local expected_positions = {
0,
0,
0,
1,
0,
1,
0,
1,
0,
0,
1,
0,
0,
2,
1,
0,
2,
1,
0,
0,
2,
1,
0,
0,
3,
2,
1,
0,
0,
3,
2,
1,
0,
0,
0,
1,
0,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
}
eq(
expected_positions,
exec_lua([[
local end_codepoint_positions = {}
for idx = 1, #_G.test_text do
table.insert(end_codepoint_positions, vim.str_utf_end(_G.test_text, idx))
end
return end_codepoint_positions
]])
)
end)
it('vim.str_utf_pos', function()
exec_lua([[_G.test_text = "xy åäö ɧ 汉语 ↥ 🤦x🦄 å بِيَّ"]])
local expected_positions = {
1,
2,
3,
4,
6,
8,
10,
11,
13,
14,
17,
20,
21,
24,
25,
29,
30,
34,
35,
36,
38,
39,
41,
43,
45,
47,
}
eq(expected_positions, exec_lua('return vim.str_utf_pos(_G.test_text)'))
end)
it('vim.schedule', function()
exec_lua([[
test_table = {}
vim.schedule(function()
table.insert(test_table, "xx")
end)
table.insert(test_table, "yy")
]])
eq({ 'yy', 'xx' }, exec_lua('return test_table'))
-- Validates args.
matches('vim.schedule: expected function', pcall_err(exec_lua, "vim.schedule('stringly')"))
matches('vim.schedule: expected function', pcall_err(exec_lua, 'vim.schedule()'))
exec_lua([[
vim.schedule(function()
error("big failure\nvery async")
end)
]])
feed('<cr>')
matches('big failure\nvery async', remove_trace(eval('v:errmsg')))
local screen = Screen.new(60, 5)
screen:set_default_attr_ids({
[1] = { bold = true, foreground = Screen.colors.Blue1 },
[2] = { bold = true, reverse = true },
[3] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
[4] = { bold = true, foreground = Screen.colors.SeaGreen4 },
})
screen:attach()
screen:expect {
grid = [[
^ |
{1:~ }|*3
|
]],
}
-- nvim_command causes a Vimscript exception, check that it is properly caught
-- and propagated as an error message in async contexts.. #10809
exec_lua([[
vim.schedule(function()
vim.api.nvim_command(":echo 'err")
end)
]])
screen:expect {
grid = [[
{3:stack traceback:} |
{3: [C]: in function 'nvim_command'} |
{3: [string "<nvim>"]:2: in function <[string "<nvim>"]:}|
{3:1>} |
{4:Press ENTER or type command to continue}^ |
]],
}
end)
it('vim.gsplit, vim.split', function()
local tests = {
-- plain trimempty
{ 'a,b', ',', false, false, { 'a', 'b' } },
{ ':aa::::bb:', ':', false, false, { '', 'aa', '', '', '', 'bb', '' } },
{ ':aa::::bb:', ':', false, true, { 'aa', '', '', '', 'bb' } },
{ 'aa::::bb:', ':', false, true, { 'aa', '', '', '', 'bb' } },
{ ':aa::bb:', ':', false, true, { 'aa', '', 'bb' } },
{ '/a/b:/b/\n', '[:\n]', false, true, { '/a/b', '/b/' } },
{ '::ee::ff:', ':', false, false, { '', '', 'ee', '', 'ff', '' } },
{ '::ee::ff::', ':', false, true, { 'ee', '', 'ff' } },
{ 'ab', '.', false, false, { '', '', '' } },
{ 'a1b2c', '[0-9]', false, false, { 'a', 'b', 'c' } },
{ 'xy', '', false, false, { 'x', 'y' } },
{ 'here be dragons', ' ', false, false, { 'here', 'be', 'dragons' } },
{ 'axaby', 'ab?', false, false, { '', 'x', 'y' } },
{ 'f v2v v3v w2w ', '([vw])2%1', false, false, { 'f ', ' v3v ', ' ' } },
{ '', '', false, false, {} },
{ '', '', false, true, {} },
{ '\n', '[:\n]', false, true, {} },
{ '', 'a', false, false, { '' } },
{ 'x*yz*oo*l', '*', true, false, { 'x', 'yz', 'oo', 'l' } },
}
for _, q in ipairs(tests) do
eq(q[5], vim.split(q[1], q[2], { plain = q[3], trimempty = q[4] }), q[1])
end
-- Test old signature
eq({ 'x', 'yz', 'oo', 'l' }, vim.split('x*yz*oo*l', '*', true))
local loops = {
{ 'abc', '.-' },
}
for _, q in ipairs(loops) do
matches('Infinite loop detected', pcall_err(vim.split, q[1], q[2]))
end
-- Validates args.
eq(true, pcall(vim.split, 'string', 'string'))
matches('s: expected string, got number', pcall_err(vim.split, 1, 'string'))
matches('sep: expected string, got number', pcall_err(vim.split, 'string', 1))
matches('opts: expected table, got number', pcall_err(vim.split, 'string', 'string', 1))
end)
it('vim.trim', function()
local trim = function(s)
return exec_lua('return vim.trim(...)', s)
end
local trims = {
{ ' a', 'a' },
{ ' b ', 'b' },
{ '\tc', 'c' },
{ 'r\n', 'r' },
}
for _, q in ipairs(trims) do
assert(q[2], trim(q[1]))
end
-- Validates args.
matches('s: expected string, got number', pcall_err(trim, 2))
end)
it('vim.inspect', function()
-- just make sure it basically works, it has its own test suite
local inspect = function(q, opts)
return exec_lua('return vim.inspect(...)', q, opts)
end
eq('2', inspect(2))
eq('{+a = {+b = 1+}+}', inspect({ a = { b = 1 } }, { newline = '+', indent = '' }))
-- special value vim.inspect.KEY works
eq(
'{ KEY_a = "x", KEY_b = "y"}',
exec_lua([[
return vim.inspect({a="x", b="y"}, {newline = '', process = function(item, path)
if path[#path] == vim.inspect.KEY then
return 'KEY_'..item
end
return item
end})
]])
)
end)
it('vim.deepcopy', function()
ok(exec_lua([[
local a = { x = { 1, 2 }, y = 5}
local b = vim.deepcopy(a)
return b.x[1] == 1 and b.x[2] == 2 and b.y == 5 and vim.tbl_count(b) == 2
and tostring(a) ~= tostring(b)
]]))
ok(exec_lua([[
local a = {}
local b = vim.deepcopy(a)
return vim.islist(b) and vim.tbl_count(b) == 0 and tostring(a) ~= tostring(b)
]]))
ok(exec_lua([[
local a = vim.empty_dict()
local b = vim.deepcopy(a)
return not vim.islist(b) and vim.tbl_count(b) == 0
]]))
ok(exec_lua([[
local a = {x = vim.empty_dict(), y = {}}
local b = vim.deepcopy(a)
return not vim.islist(b.x) and vim.islist(b.y)
and vim.tbl_count(b) == 2
and tostring(a) ~= tostring(b)
]]))
ok(exec_lua([[
local f1 = function() return 1 end
local f2 = function() return 2 end
local t1 = {f = f1}
local t2 = vim.deepcopy(t1)
t1.f = f2
return t1.f() ~= t2.f()
]]))
ok(exec_lua([[
local t1 = {a = 5}
t1.self = t1
local t2 = vim.deepcopy(t1)
return t2.self == t2 and t2.self ~= t1
]]))
ok(exec_lua([[
local mt = {mt=true}
local t1 = setmetatable({a = 5}, mt)
local t2 = vim.deepcopy(t1)
return getmetatable(t2) == mt
]]))
ok(exec_lua([[
local t1 = {a = vim.NIL}
local t2 = vim.deepcopy(t1)
return t2.a == vim.NIL
]]))
matches(
'Cannot deepcopy object of type thread',
pcall_err(
exec_lua,
[[
local thread = coroutine.create(function () return 0 end)
local t = {thr = thread}
vim.deepcopy(t)
]]
)
)
end)
it('vim.pesc', function()
eq('foo%-bar', exec_lua([[return vim.pesc('foo-bar')]]))
eq('foo%%%-bar', exec_lua([[return vim.pesc(vim.pesc('foo-bar'))]]))
-- pesc() returns one result. #20751
eq({ 'x' }, exec_lua([[return {vim.pesc('x')}]]))
-- Validates args.
matches('s: expected string, got number', pcall_err(exec_lua, [[return vim.pesc(2)]]))
end)
it('vim.list_contains', function()
eq(true, exec_lua("return vim.list_contains({'a','b','c'}, 'c')"))
eq(false, exec_lua("return vim.list_contains({'a','b','c'}, 'd')"))
end)
it('vim.tbl_contains', function()
eq(true, exec_lua("return vim.tbl_contains({'a','b','c'}, 'c')"))
eq(false, exec_lua("return vim.tbl_contains({'a','b','c'}, 'd')"))
eq(true, exec_lua("return vim.tbl_contains({[2]='a',foo='b',[5] = 'c'}, 'c')"))
eq(
true,
exec_lua([[
return vim.tbl_contains({ 'a', { 'b', 'c' } }, function(v)
return vim.deep_equal(v, { 'b', 'c' })
end, { predicate = true })
]])
)
end)
it('vim.tbl_keys', function()
eq({}, exec_lua('return vim.tbl_keys({})'))
for _, v in pairs(exec_lua("return vim.tbl_keys({'a', 'b', 'c'})")) do
eq(true, exec_lua('return vim.tbl_contains({ 1, 2, 3 }, ...)', v))
end
for _, v in pairs(exec_lua('return vim.tbl_keys({a=1, b=2, c=3})')) do
eq(true, exec_lua("return vim.tbl_contains({ 'a', 'b', 'c' }, ...)", v))
end
end)
it('vim.tbl_values', function()
eq({}, exec_lua('return vim.tbl_values({})'))
for _, v in pairs(exec_lua("return vim.tbl_values({'a', 'b', 'c'})")) do
eq(true, exec_lua("return vim.tbl_contains({ 'a', 'b', 'c' }, ...)", v))
end
for _, v in pairs(exec_lua('return vim.tbl_values({a=1, b=2, c=3})')) do
eq(true, exec_lua('return vim.tbl_contains({ 1, 2, 3 }, ...)', v))
end
end)
it('vim.tbl_map', function()
eq(
{},
exec_lua([[
return vim.tbl_map(function(v) return v * 2 end, {})
]])
)
eq(
{ 2, 4, 6 },
exec_lua([[
return vim.tbl_map(function(v) return v * 2 end, {1, 2, 3})
]])
)
eq(
{ { i = 2 }, { i = 4 }, { i = 6 } },
exec_lua([[
return vim.tbl_map(function(v) return { i = v.i * 2 } end, {{i=1}, {i=2}, {i=3}})
]])
)
end)
it('vim.tbl_filter', function()
eq(
{},
exec_lua([[
return vim.tbl_filter(function(v) return (v % 2) == 0 end, {})
]])
)
eq(
{ 2 },
exec_lua([[
return vim.tbl_filter(function(v) return (v % 2) == 0 end, {1, 2, 3})
]])
)
eq(
{ { i = 2 } },
exec_lua([[
return vim.tbl_filter(function(v) return (v.i % 2) == 0 end, {{i=1}, {i=2}, {i=3}})
]])
)
end)
it('vim.isarray', function()
eq(true, exec_lua('return vim.isarray({})'))
eq(false, exec_lua('return vim.isarray(vim.empty_dict())'))
eq(true, exec_lua("return vim.isarray({'a', 'b', 'c'})"))
eq(false, exec_lua("return vim.isarray({'a', '32', a='hello', b='baz'})"))
eq(false, exec_lua("return vim.isarray({1, a='hello', b='baz'})"))
eq(false, exec_lua("return vim.isarray({a='hello', b='baz', 1})"))
eq(false, exec_lua("return vim.isarray({1, 2, nil, a='hello'})"))
eq(true, exec_lua('return vim.isarray({1, 2, nil, 4})'))
eq(true, exec_lua('return vim.isarray({nil, 2, 3, 4})'))
eq(false, exec_lua('return vim.isarray({1, [1.5]=2, [3]=3})'))
end)
it('vim.islist', function()
eq(true, exec_lua('return vim.islist({})'))
eq(false, exec_lua('return vim.islist(vim.empty_dict())'))
eq(true, exec_lua("return vim.islist({'a', 'b', 'c'})"))
eq(false, exec_lua("return vim.islist({'a', '32', a='hello', b='baz'})"))
eq(false, exec_lua("return vim.islist({1, a='hello', b='baz'})"))
eq(false, exec_lua("return vim.islist({a='hello', b='baz', 1})"))
eq(false, exec_lua("return vim.islist({1, 2, nil, a='hello'})"))
eq(false, exec_lua('return vim.islist({1, 2, nil, 4})'))
eq(false, exec_lua('return vim.islist({nil, 2, 3, 4})'))
eq(false, exec_lua('return vim.islist({1, [1.5]=2, [3]=3})'))
end)
it('vim.tbl_isempty', function()
eq(true, exec_lua('return vim.tbl_isempty({})'))
eq(false, exec_lua('return vim.tbl_isempty({ 1, 2, 3 })'))
eq(false, exec_lua('return vim.tbl_isempty({a=1, b=2, c=3})'))
end)
it('vim.tbl_get', function()
eq(
true,
exec_lua("return vim.tbl_get({ test = { nested_test = true }}, 'test', 'nested_test')")
)
eq(NIL, exec_lua("return vim.tbl_get({ unindexable = true }, 'unindexable', 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({ unindexable = 1 }, 'unindexable', 'missing_key')"))
eq(
NIL,
exec_lua(
"return vim.tbl_get({ unindexable = coroutine.create(function () end) }, 'unindexable', 'missing_key')"
)
)
eq(
NIL,
exec_lua(
"return vim.tbl_get({ unindexable = function () end }, 'unindexable', 'missing_key')"
)
)
eq(NIL, exec_lua("return vim.tbl_get({}, 'missing_key')"))
eq(NIL, exec_lua('return vim.tbl_get({})'))
eq(1, exec_lua("return select('#', vim.tbl_get({}))"))
eq(1, exec_lua("return select('#', vim.tbl_get({ nested = {} }, 'nested', 'missing_key'))"))
end)
it('vim.tbl_extend', function()
ok(exec_lua([[
local a = {x = 1}
local b = {y = 2}
local c = vim.tbl_extend("keep", a, b)
return c.x == 1 and b.y == 2 and vim.tbl_count(c) == 2
]]))
ok(exec_lua([[
local a = {x = 1}
local b = {y = 2}
local c = {z = 3}
local d = vim.tbl_extend("keep", a, b, c)
return d.x == 1 and d.y == 2 and d.z == 3 and vim.tbl_count(d) == 3
]]))
ok(exec_lua([[
local a = {x = 1}
local b = {x = 3}
local c = vim.tbl_extend("keep", a, b)
return c.x == 1 and vim.tbl_count(c) == 1
]]))
ok(exec_lua([[
local a = {x = 1}
local b = {x = 3}
local c = vim.tbl_extend("force", a, b)
return c.x == 3 and vim.tbl_count(c) == 1
]]))
ok(exec_lua([[
local a = vim.empty_dict()
local b = {}
local c = vim.tbl_extend("keep", a, b)
return not vim.islist(c) and vim.tbl_count(c) == 0
]]))
ok(exec_lua([[
local a = {}
local b = vim.empty_dict()
local c = vim.tbl_extend("keep", a, b)
return vim.islist(c) and vim.tbl_count(c) == 0
]]))
ok(exec_lua([[
local a = {x = {a = 1, b = 2}}
local b = {x = {a = 2, c = {y = 3}}}
local c = vim.tbl_extend("keep", a, b)
local count = 0
for _ in pairs(c) do count = count + 1 end
return c.x.a == 1 and c.x.b == 2 and c.x.c == nil and count == 1
]]))
matches(
'invalid "behavior": nil',
pcall_err(
exec_lua,
[[
return vim.tbl_extend()
]]
)
)
matches(
'wrong number of arguments %(given 1, expected at least 3%)',
pcall_err(
exec_lua,
[[
return vim.tbl_extend("keep")
]]
)
)
matches(
'wrong number of arguments %(given 2, expected at least 3%)',
pcall_err(
exec_lua,
[[
return vim.tbl_extend("keep", {})
]]
)
)
end)
it('vim.tbl_deep_extend', function()
ok(exec_lua([[
local a = {x = {a = 1, b = 2}}
local b = {x = {a = 2, c = {y = 3}}}
local c = vim.tbl_deep_extend("keep", a, b)
local count = 0
for _ in pairs(c) do count = count + 1 end
return c.x.a == 1 and c.x.b == 2 and c.x.c.y == 3 and count == 1
]]))
ok(exec_lua([[
local a = {x = {a = 1, b = 2}}
local b = {x = {a = 2, c = {y = 3}}}
local c = vim.tbl_deep_extend("force", a, b)
local count = 0
for _ in pairs(c) do count = count + 1 end
return c.x.a == 2 and c.x.b == 2 and c.x.c.y == 3 and count == 1
]]))
ok(exec_lua([[
local a = {x = {a = 1, b = 2}}
local b = {x = {a = 2, c = {y = 3}}}
local c = {x = {c = 4, d = {y = 4}}}
local d = vim.tbl_deep_extend("keep", a, b, c)
local count = 0
for _ in pairs(c) do count = count + 1 end
return d.x.a == 1 and d.x.b == 2 and d.x.c.y == 3 and d.x.d.y == 4 and count == 1
]]))
ok(exec_lua([[
local a = {x = {a = 1, b = 2}}
local b = {x = {a = 2, c = {y = 3}}}
local c = {x = {c = 4, d = {y = 4}}}
local d = vim.tbl_deep_extend("force", a, b, c)
local count = 0
for _ in pairs(c) do count = count + 1 end
return d.x.a == 2 and d.x.b == 2 and d.x.c == 4 and d.x.d.y == 4 and count == 1
]]))
ok(exec_lua([[
local a = vim.empty_dict()
local b = {}
local c = vim.tbl_deep_extend("keep", a, b)
local count = 0
for _ in pairs(c) do count = count + 1 end
return not vim.islist(c) and count == 0
]]))
ok(exec_lua([[
local a = {}
local b = vim.empty_dict()
local c = vim.tbl_deep_extend("keep", a, b)
local count = 0
for _ in pairs(c) do count = count + 1 end
return vim.islist(c) and count == 0
]]))
eq(
{ a = { b = 1 } },
exec_lua([[
local a = { a = { b = 1 } }
local b = { a = {} }
return vim.tbl_deep_extend("force", a, b)
]])
)
eq(
{ a = { b = 1 } },
exec_lua([[
local a = { a = 123 }
local b = { a = { b = 1} }
return vim.tbl_deep_extend("force", a, b)
]])
)
ok(exec_lua([[
local a = { a = {[2] = 3} }
local b = { a = {[3] = 3} }
local c = vim.tbl_deep_extend("force", a, b)
return vim.deep_equal(c, {a = {[3] = 3}})
]]))
eq(
{ a = 123 },
exec_lua([[
local a = { a = { b = 1} }
local b = { a = 123 }
return vim.tbl_deep_extend("force", a, b)
]])
)
matches(
'invalid "behavior": nil',
pcall_err(
exec_lua,
[[
return vim.tbl_deep_extend()
]]
)
)
matches(
'wrong number of arguments %(given 1, expected at least 3%)',
pcall_err(
exec_lua,
[[
return vim.tbl_deep_extend("keep")
]]
)
)
matches(
'wrong number of arguments %(given 2, expected at least 3%)',
pcall_err(
exec_lua,
[[
return vim.tbl_deep_extend("keep", {})
]]
)
)
end)
it('vim.tbl_count', function()
eq(0, exec_lua [[ return vim.tbl_count({}) ]])
eq(0, exec_lua [[ return vim.tbl_count(vim.empty_dict()) ]])
eq(0, exec_lua [[ return vim.tbl_count({nil}) ]])
eq(0, exec_lua [[ return vim.tbl_count({a=nil}) ]])
eq(1, exec_lua [[ return vim.tbl_count({1}) ]])
eq(2, exec_lua [[ return vim.tbl_count({1, 2}) ]])
eq(2, exec_lua [[ return vim.tbl_count({1, nil, 3}) ]])
eq(1, exec_lua [[ return vim.tbl_count({a=1}) ]])
eq(2, exec_lua [[ return vim.tbl_count({a=1, b=2}) ]])
eq(2, exec_lua [[ return vim.tbl_count({a=1, b=nil, c=3}) ]])
end)
it('vim.deep_equal', function()
eq(true, exec_lua [[ return vim.deep_equal({a=1}, {a=1}) ]])
eq(true, exec_lua [[ return vim.deep_equal({a={b=1}}, {a={b=1}}) ]])
eq(true, exec_lua [[ return vim.deep_equal({a={b={nil}}}, {a={b={}}}) ]])
eq(true, exec_lua [[ return vim.deep_equal({a=1, [5]=5}, {nil,nil,nil,nil,5,a=1}) ]])
eq(false, exec_lua [[ return vim.deep_equal(1, {nil,nil,nil,nil,5,a=1}) ]])
eq(false, exec_lua [[ return vim.deep_equal(1, 3) ]])
eq(false, exec_lua [[ return vim.deep_equal(nil, 3) ]])
eq(false, exec_lua [[ return vim.deep_equal({a=1}, {a=2}) ]])
end)
it('vim.list_extend', function()
eq({ 1, 2, 3 }, exec_lua [[ return vim.list_extend({1}, {2,3}) ]])
matches(
'src: expected table, got nil',
pcall_err(exec_lua, [[ return vim.list_extend({1}, nil) ]])
)
eq({ 1, 2 }, exec_lua [[ return vim.list_extend({1}, {2;a=1}) ]])
eq(true, exec_lua [[ local a = {1} return vim.list_extend(a, {2;a=1}) == a ]])
eq({ 2 }, exec_lua [[ return vim.list_extend({}, {2;a=1}, 1) ]])
eq({}, exec_lua [[ return vim.list_extend({}, {2;a=1}, 2) ]])
eq({}, exec_lua [[ return vim.list_extend({}, {2;a=1}, 1, -1) ]])
eq({ 2 }, exec_lua [[ return vim.list_extend({}, {2;a=1}, -1, 2) ]])
end)
it('vim.tbl_add_reverse_lookup', function()
eq(
true,
exec_lua [[
local a = { A = 1 }
vim.tbl_add_reverse_lookup(a)
return vim.deep_equal(a, { A = 1; [1] = 'A'; })
]]
)
-- Throw an error for trying to do it twice (run into an existing key)
local code = [[
local res = {}
local a = { A = 1 }
vim.tbl_add_reverse_lookup(a)
assert(vim.deep_equal(a, { A = 1; [1] = 'A'; }))
vim.tbl_add_reverse_lookup(a)
]]
matches(
'The reverse lookup found an existing value for "[1A]" while processing key "[1A]"$',
pcall_err(exec_lua, code)
)
end)
it('vim.spairs', function()
local res = ''
local table = {
ccc = 1,
bbb = 2,
ddd = 3,
aaa = 4,
}
for key, _ in vim.spairs(table) do
res = res .. key
end
matches('aaabbbcccddd', res)
end)
it('vim.call, vim.fn', function()
eq(true, exec_lua([[return vim.call('sin', 0.0) == 0.0 ]]))
eq(true, exec_lua([[return vim.fn.sin(0.0) == 0.0 ]]))
-- compat: nvim_call_function uses "special" value for Vimscript float
eq(false, exec_lua([[return vim.api.nvim_call_function('sin', {0.0}) == 0.0 ]]))
exec([[
func! FooFunc(test)
let g:test = a:test
return {}
endfunc
func! VarArg(...)
return a:000
endfunc
func! Nilly()
return [v:null, v:null]
endfunc
]])
eq(true, exec_lua([[return next(vim.fn.FooFunc(3)) == nil ]]))
eq(3, eval('g:test'))
-- compat: nvim_call_function uses "special" value for empty dict
eq(true, exec_lua([[return next(vim.api.nvim_call_function("FooFunc", {5})) == true ]]))
eq(5, eval('g:test'))
eq({ 2, 'foo', true }, exec_lua([[return vim.fn.VarArg(2, "foo", true)]]))
eq(
true,
exec_lua([[
local x = vim.fn.Nilly()
return #x == 2 and x[1] == vim.NIL and x[2] == vim.NIL
]])
)
eq({ NIL, NIL }, exec_lua([[return vim.fn.Nilly()]]))
-- error handling
eq(
{ false, 'Vim:E897: List or Blob required' },
exec_lua([[return {pcall(vim.fn.add, "aa", "bb")}]])
)
-- conversion between LuaRef and Vim Funcref
eq(
true,
exec_lua([[
local x = vim.fn.VarArg(function() return 'foo' end, function() return 'bar' end)
return #x == 2 and x[1]() == 'foo' and x[2]() == 'bar'
]])
)
-- Test for #20211
eq(
'a (b) c',
exec_lua([[
return vim.fn.substitute('a b c', 'b', function(m) return '(' .. m[1] .. ')' end, 'g')
]])
)
end)
it('vim.fn should error when calling API function', function()
matches(
'Tried to call API function with vim.fn: use vim.api.nvim_get_current_line instead',
pcall_err(exec_lua, 'vim.fn.nvim_get_current_line()')
)
end)
it('vim.fn is allowed in "fast" context by some functions #18306', function()
exec_lua([[
local timer = vim.uv.new_timer()
timer:start(0, 0, function()
timer:close()
assert(vim.in_fast_event())
vim.g.fnres = vim.fn.iconv('hello', 'utf-8', 'utf-8')
end)
]])
poke_eventloop()
eq('hello', exec_lua [[return vim.g.fnres]])
end)
it('vim.rpcrequest and vim.rpcnotify', function()
exec_lua([[
chan = vim.fn.jobstart({'cat'}, {rpc=true})
vim.rpcrequest(chan, 'nvim_set_current_line', 'meow')
]])
eq('meow', api.nvim_get_current_line())
command("let x = [3, 'aa', v:true, v:null]")
eq(
true,
exec_lua([[
ret = vim.rpcrequest(chan, 'nvim_get_var', 'x')
return #ret == 4 and ret[1] == 3 and ret[2] == 'aa' and ret[3] == true and ret[4] == vim.NIL
]])
)
eq({ 3, 'aa', true, NIL }, exec_lua([[return ret]]))
eq(
{ {}, {}, false, true },
exec_lua([[
vim.rpcrequest(chan, 'nvim_exec', 'let xx = {}\nlet yy = []', false)
local dict = vim.rpcrequest(chan, 'nvim_eval', 'xx')
local list = vim.rpcrequest(chan, 'nvim_eval', 'yy')
return {dict, list, vim.islist(dict), vim.islist(list)}
]])
)
exec_lua([[
vim.rpcrequest(chan, 'nvim_set_var', 'aa', {})
vim.rpcrequest(chan, 'nvim_set_var', 'bb', vim.empty_dict())
]])
eq({ 1, 1 }, eval('[type(g:aa) == type([]), type(g:bb) == type({})]'))
-- error handling
eq({ false, 'Invalid channel: 23' }, exec_lua([[return {pcall(vim.rpcrequest, 23, 'foo')}]]))
eq({ false, 'Invalid channel: 23' }, exec_lua([[return {pcall(vim.rpcnotify, 23, 'foo')}]]))
eq(
{ false, 'Vim:E121: Undefined variable: foobar' },
exec_lua([[return {pcall(vim.rpcrequest, chan, 'nvim_eval', "foobar")}]])
)
-- rpcnotify doesn't wait on request
eq(
'meow',
exec_lua([[
vim.rpcnotify(chan, 'nvim_set_current_line', 'foo')
return vim.api.nvim_get_current_line()
]])
)
retry(10, nil, function()
eq('foo', api.nvim_get_current_line())
end)
local screen = Screen.new(50, 7)
screen:set_default_attr_ids({
[1] = { bold = true, foreground = Screen.colors.Blue1 },
[2] = { bold = true, reverse = true },
[3] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
[4] = { bold = true, foreground = Screen.colors.SeaGreen4 },
})
screen:attach()
exec_lua([[
timer = vim.uv.new_timer()
timer:start(20, 0, function ()
-- notify ok (executed later when safe)
vim.rpcnotify(chan, 'nvim_set_var', 'yy', {3, vim.NIL})
-- rpcrequest an error
vim.rpcrequest(chan, 'nvim_set_current_line', 'bork')
end)
]])
screen:expect {
grid = [[
{3:[string "<nvim>"]:6: E5560: rpcrequest must not be}|
{3: called in a lua loop callback} |
{3:stack traceback:} |
{3: [C]: in function 'rpcrequest'} |
{3: [string "<nvim>"]:6: in function <[string }|
{3:"<nvim>"]:2>} |
{4:Press ENTER or type command to continue}^ |
]],
}
feed('<cr>')
retry(10, nil, function()
eq({ 3, NIL }, api.nvim_get_var('yy'))
end)
exec_lua([[timer:close()]])
end)
it('vim.empty_dict()', function()
eq(
{ true, false, true, true },
exec_lua([[
vim.api.nvim_set_var('listy', {})
vim.api.nvim_set_var('dicty', vim.empty_dict())
local listy = vim.fn.eval("listy")
local dicty = vim.fn.eval("dicty")
return {vim.islist(listy), vim.islist(dicty), next(listy) == nil, next(dicty) == nil}
]])
)
-- vim.empty_dict() gives new value each time
-- equality is not overridden (still by ref)
-- non-empty table uses the usual heuristics (ignores the tag)
eq(
{ false, { 'foo' }, { namey = 'bar' } },
exec_lua([[
local aa = vim.empty_dict()
local bb = vim.empty_dict()
local equally = (aa == bb)
aa[1] = "foo"
bb["namey"] = "bar"
return {equally, aa, bb}
]])
)
eq('{ {}, vim.empty_dict() }', exec_lua('return vim.inspect({{}, vim.empty_dict()})'))
eq('{}', exec_lua([[ return vim.fn.json_encode(vim.empty_dict()) ]]))
eq('{"a": {}, "b": []}', exec_lua([[ return vim.fn.json_encode({a=vim.empty_dict(), b={}}) ]]))
end)
it('vim.validate', function()
exec_lua("vim.validate{arg1={{}, 'table' }}")
exec_lua("vim.validate{arg1={{}, 't' }}")
exec_lua("vim.validate{arg1={nil, 't', true }}")
exec_lua("vim.validate{arg1={{ foo='foo' }, 't' }}")
exec_lua("vim.validate{arg1={{ 'foo' }, 't' }}")
exec_lua("vim.validate{arg1={'foo', 'string' }}")
exec_lua("vim.validate{arg1={'foo', 's' }}")
exec_lua("vim.validate{arg1={'', 's' }}")
exec_lua("vim.validate{arg1={nil, 's', true }}")
exec_lua("vim.validate{arg1={1, 'number' }}")
exec_lua("vim.validate{arg1={1, 'n' }}")
exec_lua("vim.validate{arg1={0, 'n' }}")
exec_lua("vim.validate{arg1={0.1, 'n' }}")
exec_lua("vim.validate{arg1={nil, 'n', true }}")
exec_lua("vim.validate{arg1={true, 'boolean' }}")
exec_lua("vim.validate{arg1={true, 'b' }}")
exec_lua("vim.validate{arg1={false, 'b' }}")
exec_lua("vim.validate{arg1={nil, 'b', true }}")
exec_lua("vim.validate{arg1={function()end, 'function' }}")
exec_lua("vim.validate{arg1={function()end, 'f' }}")
exec_lua("vim.validate{arg1={nil, 'f', true }}")
exec_lua("vim.validate{arg1={nil, 'nil' }}")
exec_lua("vim.validate{arg1={nil, 'nil', true }}")
exec_lua("vim.validate{arg1={coroutine.create(function()end), 'thread' }}")
exec_lua("vim.validate{arg1={nil, 'thread', true }}")
exec_lua("vim.validate{arg1={{}, 't' }, arg2={ 'foo', 's' }}")
exec_lua("vim.validate{arg1={2, function(a) return (a % 2) == 0 end, 'even number' }}")
exec_lua("vim.validate{arg1={5, {'n', 's'} }, arg2={ 'foo', {'n', 's'} }}")
matches('expected table, got number', pcall_err(exec_lua, "vim.validate{ 1, 'x' }"))
matches('invalid type name: x', pcall_err(exec_lua, "vim.validate{ arg1={ 1, 'x' }}"))
matches('invalid type name: 1', pcall_err(exec_lua, 'vim.validate{ arg1={ 1, 1 }}'))
matches('invalid type name: nil', pcall_err(exec_lua, 'vim.validate{ arg1={ 1 }}'))
-- Validated parameters are required by default.
matches(
'arg1: expected string, got nil',
pcall_err(exec_lua, "vim.validate{ arg1={ nil, 's' }}")
)
-- Explicitly required.
matches(
'arg1: expected string, got nil',
pcall_err(exec_lua, "vim.validate{ arg1={ nil, 's', false }}")
)
matches('arg1: expected table, got number', pcall_err(exec_lua, "vim.validate{arg1={1, 't'}}"))
matches(
'arg2: expected string, got number',
pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={1, 's'}}")
)
matches(
'arg2: expected string, got nil',
pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}")
)
matches(
'arg2: expected string, got nil',
pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}")
)
matches(
'arg1: expected even number, got 3',
pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end, 'even number'}}")
)
matches(
'arg1: expected %?, got 3',
pcall_err(exec_lua, 'vim.validate{arg1={3, function(a) return a == 1 end}}')
)
matches(
'arg1: expected number|string, got nil',
pcall_err(exec_lua, "vim.validate{ arg1={ nil, {'n', 's'} }}")
)
-- Pass an additional message back.
matches(
'arg1: expected %?, got 3. Info: TEST_MSG',
pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1, 'TEST_MSG' end}}")
)
end)
it('vim.is_callable', function()
eq(true, exec_lua('return vim.is_callable(function()end)'))
eq(
true,
exec_lua([[
local meta = { __call = function()end }
local function new_callable()
return setmetatable({}, meta)
end
local callable = new_callable()
return vim.is_callable(callable)
]])
)
eq(false, exec_lua('return vim.is_callable(1)'))
eq(false, exec_lua("return vim.is_callable('foo')"))
eq(false, exec_lua('return vim.is_callable({})'))
end)
it('vim.g', function()
exec_lua [[
vim.api.nvim_set_var("testing", "hi")
vim.api.nvim_set_var("other", 123)
vim.api.nvim_set_var("floaty", 5120.1)
vim.api.nvim_set_var("nullvar", vim.NIL)
vim.api.nvim_set_var("to_delete", {hello="world"})
]]
eq('hi', fn.luaeval 'vim.g.testing')
eq(123, fn.luaeval 'vim.g.other')
eq(5120.1, fn.luaeval 'vim.g.floaty')
eq(NIL, fn.luaeval 'vim.g.nonexistent')
eq(NIL, fn.luaeval 'vim.g.nullvar')
-- lost over RPC, so test locally:
eq(
{ false, true },
exec_lua [[
return {vim.g.nonexistent == vim.NIL, vim.g.nullvar == vim.NIL}
]]
)
eq({ hello = 'world' }, fn.luaeval 'vim.g.to_delete')
exec_lua [[
vim.g.to_delete = nil
]]
eq(NIL, fn.luaeval 'vim.g.to_delete')
matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.g[0].testing'))
exec_lua [[
local counter = 0
local function add_counter() counter = counter + 1 end
local function get_counter() return counter end
vim.g.AddCounter = add_counter
vim.g.GetCounter = get_counter
vim.g.fn = {add = add_counter, get = get_counter}
vim.g.AddParens = function(s) return '(' .. s .. ')' end
]]
eq(0, eval('g:GetCounter()'))
eval('g:AddCounter()')
eq(1, eval('g:GetCounter()'))
eval('g:AddCounter()')
eq(2, eval('g:GetCounter()'))
exec_lua([[vim.g.AddCounter()]])
eq(3, exec_lua([[return vim.g.GetCounter()]]))
exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]]))
exec_lua([[vim.g.fn.add()]])
eq(5, exec_lua([[return vim.g.fn.get()]]))
exec_lua([[vim.api.nvim_get_var('fn').add()]])
eq(6, exec_lua([[return vim.api.nvim_get_var('fn').get()]]))
eq('((foo))', eval([['foo'->AddParens()->AddParens()]]))
exec_lua [[
local counter = 0
local function add_counter() counter = counter + 1 end
local function get_counter() return counter end
vim.api.nvim_set_var('AddCounter', add_counter)
vim.api.nvim_set_var('GetCounter', get_counter)
vim.api.nvim_set_var('fn', {add = add_counter, get = get_counter})
vim.api.nvim_set_var('AddParens', function(s) return '(' .. s .. ')' end)
]]
eq(0, eval('g:GetCounter()'))
eval('g:AddCounter()')
eq(1, eval('g:GetCounter()'))
eval('g:AddCounter()')
eq(2, eval('g:GetCounter()'))
exec_lua([[vim.g.AddCounter()]])
eq(3, exec_lua([[return vim.g.GetCounter()]]))
exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]]))
exec_lua([[vim.g.fn.add()]])
eq(5, exec_lua([[return vim.g.fn.get()]]))
exec_lua([[vim.api.nvim_get_var('fn').add()]])
eq(6, exec_lua([[return vim.api.nvim_get_var('fn').get()]]))
eq('((foo))', eval([['foo'->AddParens()->AddParens()]]))
exec([[
function Test()
endfunction
function s:Test()
endfunction
let g:Unknown_func = function('Test')
let g:Unknown_script_func = function('s:Test')
]])
eq(NIL, exec_lua([[return vim.g.Unknown_func]]))
eq(NIL, exec_lua([[return vim.g.Unknown_script_func]]))
-- Check if autoload works properly
local pathsep = n.get_pathsep()
local xconfig = 'Xhome' .. pathsep .. 'Xconfig'
local xdata = 'Xhome' .. pathsep .. 'Xdata'
local autoload_folder = table.concat({ xconfig, 'nvim', 'autoload' }, pathsep)
local autoload_file = table.concat({ autoload_folder, 'testload.vim' }, pathsep)
mkdir_p(autoload_folder)
write_file(autoload_file, [[let testload#value = 2]])
clear { args_rm = { '-u' }, env = { XDG_CONFIG_HOME = xconfig, XDG_DATA_HOME = xdata } }
eq(2, exec_lua("return vim.g['testload#value']"))
rmdir('Xhome')
end)
it('vim.b', function()
exec_lua [[
vim.api.nvim_buf_set_var(0, "testing", "hi")
vim.api.nvim_buf_set_var(0, "other", 123)
vim.api.nvim_buf_set_var(0, "floaty", 5120.1)
vim.api.nvim_buf_set_var(0, "nullvar", vim.NIL)
vim.api.nvim_buf_set_var(0, "to_delete", {hello="world"})
BUF = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_var(BUF, "testing", "bye")
]]
eq('hi', fn.luaeval 'vim.b.testing')
eq('bye', fn.luaeval 'vim.b[BUF].testing')
eq(123, fn.luaeval 'vim.b.other')
eq(5120.1, fn.luaeval 'vim.b.floaty')
eq(NIL, fn.luaeval 'vim.b.nonexistent')
eq(NIL, fn.luaeval 'vim.b[BUF].nonexistent')
eq(NIL, fn.luaeval 'vim.b.nullvar')
-- lost over RPC, so test locally:
eq(
{ false, true },
exec_lua [[
return {vim.b.nonexistent == vim.NIL, vim.b.nullvar == vim.NIL}
]]
)
matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.b[BUF][0].testing'))
eq({ hello = 'world' }, fn.luaeval 'vim.b.to_delete')
exec_lua [[
vim.b.to_delete = nil
]]
eq(NIL, fn.luaeval 'vim.b.to_delete')
exec_lua [[
local counter = 0
local function add_counter() counter = counter + 1 end
local function get_counter() return counter end
vim.b.AddCounter = add_counter
vim.b.GetCounter = get_counter
vim.b.fn = {add = add_counter, get = get_counter}
vim.b.AddParens = function(s) return '(' .. s .. ')' end
]]
eq(0, eval('b:GetCounter()'))
eval('b:AddCounter()')
eq(1, eval('b:GetCounter()'))
eval('b:AddCounter()')
eq(2, eval('b:GetCounter()'))
exec_lua([[vim.b.AddCounter()]])
eq(3, exec_lua([[return vim.b.GetCounter()]]))
exec_lua([[vim.api.nvim_buf_get_var(0, 'AddCounter')()]])
eq(4, exec_lua([[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]]))
exec_lua([[vim.b.fn.add()]])
eq(5, exec_lua([[return vim.b.fn.get()]]))
exec_lua([[vim.api.nvim_buf_get_var(0, 'fn').add()]])
eq(6, exec_lua([[return vim.api.nvim_buf_get_var(0, 'fn').get()]]))
eq('((foo))', eval([['foo'->b:AddParens()->b:AddParens()]]))
exec_lua [[
local counter = 0
local function add_counter() counter = counter + 1 end
local function get_counter() return counter end
vim.api.nvim_buf_set_var(0, 'AddCounter', add_counter)
vim.api.nvim_buf_set_var(0, 'GetCounter', get_counter)
vim.api.nvim_buf_set_var(0, 'fn', {add = add_counter, get = get_counter})
vim.api.nvim_buf_set_var(0, 'AddParens', function(s) return '(' .. s .. ')' end)
]]
eq(0, eval('b:GetCounter()'))
eval('b:AddCounter()')
eq(1, eval('b:GetCounter()'))
eval('b:AddCounter()')
eq(2, eval('b:GetCounter()'))
exec_lua([[vim.b.AddCounter()]])
eq(3, exec_lua([[return vim.b.GetCounter()]]))
exec_lua([[vim.api.nvim_buf_get_var(0, 'AddCounter')()]])
eq(4, exec_lua([[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]]))
exec_lua([[vim.b.fn.add()]])
eq(5, exec_lua([[return vim.b.fn.get()]]))
exec_lua([[vim.api.nvim_buf_get_var(0, 'fn').add()]])
eq(6, exec_lua([[return vim.api.nvim_buf_get_var(0, 'fn').get()]]))
eq('((foo))', eval([['foo'->b:AddParens()->b:AddParens()]]))
exec([[
function Test()
endfunction
function s:Test()
endfunction
let b:Unknown_func = function('Test')
let b:Unknown_script_func = function('s:Test')
]])
eq(NIL, exec_lua([[return vim.b.Unknown_func]]))
eq(NIL, exec_lua([[return vim.b.Unknown_script_func]]))
exec_lua [[
vim.cmd "vnew"
]]
eq(NIL, fn.luaeval 'vim.b.testing')
eq(NIL, fn.luaeval 'vim.b.other')
eq(NIL, fn.luaeval 'vim.b.nonexistent')
end)
it('vim.w', function()
exec_lua [[
vim.api.nvim_win_set_var(0, "testing", "hi")
vim.api.nvim_win_set_var(0, "other", 123)
vim.api.nvim_win_set_var(0, "to_delete", {hello="world"})
BUF = vim.api.nvim_create_buf(false, true)
WIN = vim.api.nvim_open_win(BUF, false, {
width=10, height=10,
relative='win', row=0, col=0
})
vim.api.nvim_win_set_var(WIN, "testing", "bye")
]]
eq('hi', fn.luaeval 'vim.w.testing')
eq('bye', fn.luaeval 'vim.w[WIN].testing')
eq(123, fn.luaeval 'vim.w.other')
eq(NIL, fn.luaeval 'vim.w.nonexistent')
eq(NIL, fn.luaeval 'vim.w[WIN].nonexistent')
matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.w[WIN][0].testing'))
eq({ hello = 'world' }, fn.luaeval 'vim.w.to_delete')
exec_lua [[
vim.w.to_delete = nil
]]
eq(NIL, fn.luaeval 'vim.w.to_delete')
exec_lua [[
local counter = 0
local function add_counter() counter = counter + 1 end
local function get_counter() return counter end
vim.w.AddCounter = add_counter
vim.w.GetCounter = get_counter
vim.w.fn = {add = add_counter, get = get_counter}
vim.w.AddParens = function(s) return '(' .. s .. ')' end
]]
eq(0, eval('w:GetCounter()'))
eval('w:AddCounter()')
eq(1, eval('w:GetCounter()'))
eval('w:AddCounter()')
eq(2, eval('w:GetCounter()'))
exec_lua([[vim.w.AddCounter()]])
eq(3, exec_lua([[return vim.w.GetCounter()]]))
exec_lua([[vim.api.nvim_win_get_var(0, 'AddCounter')()]])
eq(4, exec_lua([[return vim.api.nvim_win_get_var(0, 'GetCounter')()]]))
exec_lua([[vim.w.fn.add()]])
eq(5, exec_lua([[return vim.w.fn.get()]]))
exec_lua([[vim.api.nvim_win_get_var(0, 'fn').add()]])
eq(6, exec_lua([[return vim.api.nvim_win_get_var(0, 'fn').get()]]))
eq('((foo))', eval([['foo'->w:AddParens()->w:AddParens()]]))
exec_lua [[
local counter = 0
local function add_counter() counter = counter + 1 end
local function get_counter() return counter end
vim.api.nvim_win_set_var(0, 'AddCounter', add_counter)
vim.api.nvim_win_set_var(0, 'GetCounter', get_counter)
vim.api.nvim_win_set_var(0, 'fn', {add = add_counter, get = get_counter})
vim.api.nvim_win_set_var(0, 'AddParens', function(s) return '(' .. s .. ')' end)
]]
eq(0, eval('w:GetCounter()'))
eval('w:AddCounter()')
eq(1, eval('w:GetCounter()'))
eval('w:AddCounter()')
eq(2, eval('w:GetCounter()'))
exec_lua([[vim.w.AddCounter()]])
eq(3, exec_lua([[return vim.w.GetCounter()]]))
exec_lua([[vim.api.nvim_win_get_var(0, 'AddCounter')()]])
eq(4, exec_lua([[return vim.api.nvim_win_get_var(0, 'GetCounter')()]]))
exec_lua([[vim.w.fn.add()]])
eq(5, exec_lua([[return vim.w.fn.get()]]))
exec_lua([[vim.api.nvim_win_get_var(0, 'fn').add()]])
eq(6, exec_lua([[return vim.api.nvim_win_get_var(0, 'fn').get()]]))
eq('((foo))', eval([['foo'->w:AddParens()->w:AddParens()]]))
exec([[
function Test()
endfunction
function s:Test()
endfunction
let w:Unknown_func = function('Test')
let w:Unknown_script_func = function('s:Test')
]])
eq(NIL, exec_lua([[return vim.w.Unknown_func]]))
eq(NIL, exec_lua([[return vim.w.Unknown_script_func]]))
exec_lua [[
vim.cmd "vnew"
]]
eq(NIL, fn.luaeval 'vim.w.testing')
eq(NIL, fn.luaeval 'vim.w.other')
eq(NIL, fn.luaeval 'vim.w.nonexistent')
end)
it('vim.t', function()
exec_lua [[
vim.api.nvim_tabpage_set_var(0, "testing", "hi")
vim.api.nvim_tabpage_set_var(0, "other", 123)
vim.api.nvim_tabpage_set_var(0, "to_delete", {hello="world"})
]]
eq('hi', fn.luaeval 'vim.t.testing')
eq(123, fn.luaeval 'vim.t.other')
eq(NIL, fn.luaeval 'vim.t.nonexistent')
eq('hi', fn.luaeval 'vim.t[0].testing')
eq(123, fn.luaeval 'vim.t[0].other')
eq(NIL, fn.luaeval 'vim.t[0].nonexistent')
matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.t[0][0].testing'))
eq({ hello = 'world' }, fn.luaeval 'vim.t.to_delete')
exec_lua [[
vim.t.to_delete = nil
]]
eq(NIL, fn.luaeval 'vim.t.to_delete')
exec_lua [[
local counter = 0
local function add_counter() counter = counter + 1 end
local function get_counter() return counter end
vim.t.AddCounter = add_counter
vim.t.GetCounter = get_counter
vim.t.fn = {add = add_counter, get = get_counter}
vim.t.AddParens = function(s) return '(' .. s .. ')' end
]]
eq(0, eval('t:GetCounter()'))
eval('t:AddCounter()')
eq(1, eval('t:GetCounter()'))
eval('t:AddCounter()')
eq(2, eval('t:GetCounter()'))
exec_lua([[vim.t.AddCounter()]])
eq(3, exec_lua([[return vim.t.GetCounter()]]))
exec_lua([[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]])
eq(4, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]]))
exec_lua([[vim.t.fn.add()]])
eq(5, exec_lua([[return vim.t.fn.get()]]))
exec_lua([[vim.api.nvim_tabpage_get_var(0, 'fn').add()]])
eq(6, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'fn').get()]]))
eq('((foo))', eval([['foo'->t:AddParens()->t:AddParens()]]))
exec_lua [[
local counter = 0
local function add_counter() counter = counter + 1 end
local function get_counter() return counter end
vim.api.nvim_tabpage_set_var(0, 'AddCounter', add_counter)
vim.api.nvim_tabpage_set_var(0, 'GetCounter', get_counter)
vim.api.nvim_tabpage_set_var(0, 'fn', {add = add_counter, get = get_counter})
vim.api.nvim_tabpage_set_var(0, 'AddParens', function(s) return '(' .. s .. ')' end)
]]
eq(0, eval('t:GetCounter()'))
eval('t:AddCounter()')
eq(1, eval('t:GetCounter()'))
eval('t:AddCounter()')
eq(2, eval('t:GetCounter()'))
exec_lua([[vim.t.AddCounter()]])
eq(3, exec_lua([[return vim.t.GetCounter()]]))
exec_lua([[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]])
eq(4, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]]))
exec_lua([[vim.t.fn.add()]])
eq(5, exec_lua([[return vim.t.fn.get()]]))
exec_lua([[vim.api.nvim_tabpage_get_var(0, 'fn').add()]])
eq(6, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'fn').get()]]))
eq('((foo))', eval([['foo'->t:AddParens()->t:AddParens()]]))
exec_lua [[
vim.cmd "tabnew"
]]
eq(NIL, fn.luaeval 'vim.t.testing')
eq(NIL, fn.luaeval 'vim.t.other')
eq(NIL, fn.luaeval 'vim.t.nonexistent')
end)
it('vim.env', function()
exec_lua([[vim.fn.setenv('A', 123)]])
eq('123', fn.luaeval('vim.env.A'))
exec_lua([[vim.env.A = 456]])
eq('456', fn.luaeval('vim.env.A'))
exec_lua([[vim.env.A = nil]])
eq(NIL, fn.luaeval('vim.env.A'))
eq(true, fn.luaeval('vim.env.B == nil'))
command([[let $HOME = 'foo']])
eq('foo', fn.expand('~'))
eq('foo', fn.luaeval('vim.env.HOME'))
exec_lua([[vim.env.HOME = nil]])
eq('foo', fn.expand('~'))
exec_lua([[vim.env.HOME = 'bar']])
eq('bar', fn.expand('~'))
eq('bar', fn.luaeval('vim.env.HOME'))
end)
it('vim.v', function()
eq(fn.luaeval "vim.api.nvim_get_vvar('progpath')", fn.luaeval 'vim.v.progpath')
eq(false, fn.luaeval "vim.v['false']")
eq(NIL, fn.luaeval 'vim.v.null')
matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.v[0].progpath'))
eq('Key is read-only: count', pcall_err(exec_lua, [[vim.v.count = 42]]))
eq('Dictionary is locked', pcall_err(exec_lua, [[vim.v.nosuchvar = 42]]))
eq('Key is fixed: errmsg', pcall_err(exec_lua, [[vim.v.errmsg = nil]]))
exec_lua([[vim.v.errmsg = 'set by Lua']])
eq('set by Lua', eval('v:errmsg'))
exec_lua([[vim.v.errmsg = 42]])
eq('42', eval('v:errmsg'))
exec_lua([[vim.v.oldfiles = { 'one', 'two' }]])
eq({ 'one', 'two' }, eval('v:oldfiles'))
exec_lua([[vim.v.oldfiles = {}]])
eq({}, eval('v:oldfiles'))
eq('Setting v:oldfiles to value with wrong type', pcall_err(exec_lua, [[vim.v.oldfiles = 'a']]))
eq({}, eval('v:oldfiles'))
feed('i foo foo foo<Esc>0/foo<CR>')
eq({ 1, 1 }, api.nvim_win_get_cursor(0))
eq(1, eval('v:searchforward'))
feed('n')
eq({ 1, 5 }, api.nvim_win_get_cursor(0))
exec_lua([[vim.v.searchforward = 0]])
eq(0, eval('v:searchforward'))
feed('n')
eq({ 1, 1 }, api.nvim_win_get_cursor(0))
exec_lua([[vim.v.searchforward = 1]])
eq(1, eval('v:searchforward'))
feed('n')
eq({ 1, 5 }, api.nvim_win_get_cursor(0))
local screen = Screen.new(60, 3)
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue },
[1] = { background = Screen.colors.Yellow },
})
screen:attach()
eq(1, eval('v:hlsearch'))
screen:expect {
grid = [[
{1:foo} {1:^foo} {1:foo} |
{0:~ }|
|
]],
}
exec_lua([[vim.v.hlsearch = 0]])
eq(0, eval('v:hlsearch'))
screen:expect {
grid = [[
foo ^foo foo |
{0:~ }|
|
]],
}
exec_lua([[vim.v.hlsearch = 1]])
eq(1, eval('v:hlsearch'))
screen:expect {
grid = [[
{1:foo} {1:^foo} {1:foo} |
{0:~ }|
|
]],
}
end)
it('vim.bo', function()
eq('', fn.luaeval 'vim.bo.filetype')
exec_lua [[
vim.api.nvim_set_option_value("filetype", "markdown", {})
BUF = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value("modifiable", false, {buf = BUF})
]]
eq(false, fn.luaeval 'vim.bo.modified')
eq('markdown', fn.luaeval 'vim.bo.filetype')
eq(false, fn.luaeval 'vim.bo[BUF].modifiable')
exec_lua [[
vim.bo.filetype = ''
vim.bo[BUF].modifiable = true
]]
eq('', fn.luaeval 'vim.bo.filetype')
eq(true, fn.luaeval 'vim.bo[BUF].modifiable')
matches("Unknown option 'nosuchopt'$", pcall_err(exec_lua, 'return vim.bo.nosuchopt'))
matches('Expected Lua string$', pcall_err(exec_lua, 'return vim.bo[0][0].autoread'))
matches('Invalid buffer id: %-1$', pcall_err(exec_lua, 'return vim.bo[-1].filetype'))
end)
it('vim.wo', function()
exec_lua [[
vim.api.nvim_set_option_value("cole", 2, {})
vim.cmd "split"
vim.api.nvim_set_option_value("cole", 2, {})
]]
eq(2, fn.luaeval 'vim.wo.cole')
exec_lua [[
vim.wo.conceallevel = 0
]]
eq(0, fn.luaeval 'vim.wo.cole')
eq(0, fn.luaeval 'vim.wo[0].cole')
eq(0, fn.luaeval 'vim.wo[1001].cole')
matches("Unknown option 'notanopt'$", pcall_err(exec_lua, 'return vim.wo.notanopt'))
matches('Invalid window id: %-1$', pcall_err(exec_lua, 'return vim.wo[-1].list'))
eq(2, fn.luaeval 'vim.wo[1000].cole')
exec_lua [[
vim.wo[1000].cole = 0
]]
eq(0, fn.luaeval 'vim.wo[1000].cole')
-- Can handle global-local values
exec_lua [[vim.o.scrolloff = 100]]
exec_lua [[vim.wo.scrolloff = 200]]
eq(200, fn.luaeval 'vim.wo.scrolloff')
exec_lua [[vim.wo.scrolloff = -1]]
eq(100, fn.luaeval 'vim.wo.scrolloff')
exec_lua [[
vim.wo[0][0].scrolloff = 200
vim.cmd "enew"
]]
eq(100, fn.luaeval 'vim.wo.scrolloff')
end)
describe('vim.opt', function()
-- TODO: We still need to write some tests for optlocal, opt and then getting the options
-- Probably could also do some stuff with getting things from viml side as well to confirm behavior is the same.
it('should allow setting number values', function()
local scrolloff = exec_lua [[
vim.opt.scrolloff = 10
return vim.o.scrolloff
]]
eq(10, scrolloff)
end)
pending('should handle STUPID window things', function()
local result = exec_lua [[
local result = {}
table.insert(result, vim.api.nvim_get_option_value('scrolloff', {scope='global'}))
table.insert(result, vim.api.nvim_get_option_value('scrolloff', {win=0}))
return result
]]
eq({}, result)
end)
it('should allow setting tables', function()
local wildignore = exec_lua [[
vim.opt.wildignore = { 'hello', 'world' }
return vim.o.wildignore
]]
eq('hello,world', wildignore)
end)
it('should allow setting tables with shortnames', function()
local wildignore = exec_lua [[
vim.opt.wig = { 'hello', 'world' }
return vim.o.wildignore
]]
eq('hello,world', wildignore)
end)
it('should error when you attempt to set string values to numeric options', function()
local result = exec_lua [[
return {
pcall(function() vim.opt.textwidth = 'hello world' end)
}
]]
eq(false, result[1])
end)
it('should error when you attempt to setlocal a global value', function()
local result = exec_lua [[
return pcall(function() vim.opt_local.clipboard = "hello" end)
]]
eq(false, result)
end)
it('should allow you to set boolean values', function()
eq(
{ true, false, true },
exec_lua [[
local results = {}
vim.opt.autoindent = true
table.insert(results, vim.bo.autoindent)
vim.opt.autoindent = false
table.insert(results, vim.bo.autoindent)
vim.opt.autoindent = not vim.opt.autoindent:get()
table.insert(results, vim.bo.autoindent)
return results
]]
)
end)
it('should change current buffer values and defaults for global local values', function()
local result = exec_lua [[
local result = {}
vim.opt.makeprg = "global-local"
table.insert(result, vim.go.makeprg)
table.insert(result, vim.api.nvim_get_option_value('makeprg', {buf=0}))
vim.opt_local.mp = "only-local"
table.insert(result, vim.go.makeprg)
table.insert(result, vim.api.nvim_get_option_value('makeprg', {buf=0}))
vim.opt_global.makeprg = "only-global"
table.insert(result, vim.go.makeprg)
table.insert(result, vim.api.nvim_get_option_value('makeprg', {buf=0}))
vim.opt.makeprg = "global-local"
table.insert(result, vim.go.makeprg)
table.insert(result, vim.api.nvim_get_option_value('makeprg', {buf=0}))
return result
]]
-- Set -> global & local
eq('global-local', result[1])
eq('', result[2])
-- Setlocal -> only local
eq('global-local', result[3])
eq('only-local', result[4])
-- Setglobal -> only global
eq('only-global', result[5])
eq('only-local', result[6])
-- Set -> sets global value and resets local value
eq('global-local', result[7])
eq('', result[8])
end)
it('should allow you to retrieve window opts even if they have not been set', function()
local result = exec_lua [[
local result = {}
table.insert(result, vim.opt.number:get())
table.insert(result, vim.opt_local.number:get())
vim.opt_local.number = true
table.insert(result, vim.opt.number:get())
table.insert(result, vim.opt_local.number:get())
return result
]]
eq({ false, false, true, true }, result)
end)
it('should allow all sorts of string manipulation', function()
eq(
{ 'hello', 'hello world', 'start hello world' },
exec_lua [[
local results = {}
vim.opt.makeprg = "hello"
table.insert(results, vim.o.makeprg)
vim.opt.makeprg = vim.opt.makeprg + " world"
table.insert(results, vim.o.makeprg)
vim.opt.makeprg = vim.opt.makeprg ^ "start "
table.insert(results, vim.o.makeprg)
return results
]]
)
end)
describe('option:get()', function()
it('should work for boolean values', function()
eq(
false,
exec_lua [[
vim.opt.number = false
return vim.opt.number:get()
]]
)
end)
it('should work for number values', function()
local tabstop = exec_lua [[
vim.opt.tabstop = 10
return vim.opt.tabstop:get()
]]
eq(10, tabstop)
end)
it('should work for string values', function()
eq(
'hello world',
exec_lua [[
vim.opt.makeprg = "hello world"
return vim.opt.makeprg:get()
]]
)
end)
it('should work for set type flaglists', function()
local formatoptions = exec_lua [[
vim.opt.formatoptions = 'tcro'
return vim.opt.formatoptions:get()
]]
eq(true, formatoptions.t)
eq(true, not formatoptions.q)
end)
it('should work for set type flaglists', function()
local formatoptions = exec_lua [[
vim.opt.formatoptions = { t = true, c = true, r = true, o = true }
return vim.opt.formatoptions:get()
]]
eq(true, formatoptions.t)
eq(true, not formatoptions.q)
end)
it('should work for array list type options', function()
local wildignore = exec_lua [[
vim.opt.wildignore = "*.c,*.o,__pycache__"
return vim.opt.wildignore:get()
]]
eq(3, #wildignore)
eq('*.c', wildignore[1])
end)
it('should work for options that are both commalist and flaglist', function()
local result = exec_lua [[
vim.opt.whichwrap = "b,s"
return vim.opt.whichwrap:get()
]]
eq({ b = true, s = true }, result)
result = exec_lua [[
vim.opt.whichwrap = { b = true, s = false, h = true }
return vim.opt.whichwrap:get()
]]
eq({ b = true, h = true }, result)
end)
it('should work for key-value pair options', function()
local listchars = exec_lua [[
vim.opt.listchars = "tab:> ,space:_"
return vim.opt.listchars:get()
]]
eq({
tab = '> ',
space = '_',
}, listchars)
end)
it('should allow you to add numeric options', function()
eq(
16,
exec_lua [[
vim.opt.tabstop = 12
vim.opt.tabstop = vim.opt.tabstop + 4
return vim.bo.tabstop
]]
)
end)
it('should allow you to subtract numeric options', function()
eq(
2,
exec_lua [[
vim.opt.tabstop = 4
vim.opt.tabstop = vim.opt.tabstop - 2
return vim.bo.tabstop
]]
)
end)
end)
describe('key:value style options', function()
it('should handle dictionary style', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
return vim.o.listchars
]]
eq('eol:~,space:.', listchars)
end)
it('should allow adding dictionary style', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
vim.opt.listchars = vim.opt.listchars + { space = "-" }
return vim.o.listchars
]]
eq('eol:~,space:-', listchars)
end)
it('should allow adding dictionary style', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
vim.opt.listchars = vim.opt.listchars + { space = "-" } + { space = "_" }
return vim.o.listchars
]]
eq('eol:~,space:_', listchars)
end)
it('should allow completely new keys', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
vim.opt.listchars = vim.opt.listchars + { tab = ">>>" }
return vim.o.listchars
]]
eq('eol:~,space:.,tab:>>>', listchars)
end)
it('should allow subtracting dictionary style', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
vim.opt.listchars = vim.opt.listchars - "space"
return vim.o.listchars
]]
eq('eol:~', listchars)
end)
it('should allow subtracting dictionary style', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
vim.opt.listchars = vim.opt.listchars - "space" - "eol"
return vim.o.listchars
]]
eq('', listchars)
end)
it('should allow subtracting dictionary style multiple times', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
vim.opt.listchars = vim.opt.listchars - "space" - "space"
return vim.o.listchars
]]
eq('eol:~', listchars)
end)
it('should allow adding a key:value string to a listchars', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
vim.opt.listchars = vim.opt.listchars + "tab:>~"
return vim.o.listchars
]]
eq('eol:~,space:.,tab:>~', listchars)
end)
it('should allow prepending a key:value string to a listchars', function()
local listchars = exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
vim.opt.listchars = vim.opt.listchars ^ "tab:>~"
return vim.o.listchars
]]
eq('eol:~,space:.,tab:>~', listchars)
end)
end)
it('should automatically set when calling remove', function()
eq(
'foo,baz',
exec_lua [[
vim.opt.wildignore = "foo,bar,baz"
vim.opt.wildignore:remove("bar")
return vim.o.wildignore
]]
)
end)
it('should automatically set when calling remove with a table', function()
eq(
'foo',
exec_lua [[
vim.opt.wildignore = "foo,bar,baz"
vim.opt.wildignore:remove { "bar", "baz" }
return vim.o.wildignore
]]
)
end)
it('should automatically set when calling append', function()
eq(
'foo,bar,baz,bing',
exec_lua [[
vim.opt.wildignore = "foo,bar,baz"
vim.opt.wildignore:append("bing")
return vim.o.wildignore
]]
)
end)
it('should automatically set when calling append with a table', function()
eq(
'foo,bar,baz,bing,zap',
exec_lua [[
vim.opt.wildignore = "foo,bar,baz"
vim.opt.wildignore:append { "bing", "zap" }
return vim.o.wildignore
]]
)
end)
it('should allow adding tables', function()
local wildignore = exec_lua [[
vim.opt.wildignore = 'foo'
return vim.o.wildignore
]]
eq('foo', wildignore)
wildignore = exec_lua [[
vim.opt.wildignore = vim.opt.wildignore + { 'bar', 'baz' }
return vim.o.wildignore
]]
eq('foo,bar,baz', wildignore)
end)
it('should handle adding duplicates', function()
local wildignore = exec_lua [[
vim.opt.wildignore = 'foo'
return vim.o.wildignore
]]
eq('foo', wildignore)
wildignore = exec_lua [[
vim.opt.wildignore = vim.opt.wildignore + { 'bar', 'baz' }
return vim.o.wildignore
]]
eq('foo,bar,baz', wildignore)
wildignore = exec_lua [[
vim.opt.wildignore = vim.opt.wildignore + { 'bar', 'baz' }
return vim.o.wildignore
]]
eq('foo,bar,baz', wildignore)
end)
it('should allow adding multiple times', function()
local wildignore = exec_lua [[
vim.opt.wildignore = 'foo'
vim.opt.wildignore = vim.opt.wildignore + 'bar' + 'baz'
return vim.o.wildignore
]]
eq('foo,bar,baz', wildignore)
end)
it('should remove values when you use minus', function()
local wildignore = exec_lua [[
vim.opt.wildignore = 'foo'
return vim.o.wildignore
]]
eq('foo', wildignore)
wildignore = exec_lua [[
vim.opt.wildignore = vim.opt.wildignore + { 'bar', 'baz' }
return vim.o.wildignore
]]
eq('foo,bar,baz', wildignore)
wildignore = exec_lua [[
vim.opt.wildignore = vim.opt.wildignore - 'bar'
return vim.o.wildignore
]]
eq('foo,baz', wildignore)
end)
it('should prepend values when using ^', function()
local wildignore = exec_lua [[
vim.opt.wildignore = 'foo'
vim.opt.wildignore = vim.opt.wildignore ^ 'first'
return vim.o.wildignore
]]
eq('first,foo', wildignore)
wildignore = exec_lua [[
vim.opt.wildignore = vim.opt.wildignore ^ 'super_first'
return vim.o.wildignore
]]
eq('super_first,first,foo', wildignore)
end)
it('should not remove duplicates from wildmode: #14708', function()
local wildmode = exec_lua [[
vim.opt.wildmode = {"full", "list", "full"}
return vim.o.wildmode
]]
eq('full,list,full', wildmode)
end)
describe('option types', function()
it('should allow to set option with numeric value', function()
eq(
4,
exec_lua [[
vim.opt.tabstop = 4
return vim.bo.tabstop
]]
)
matches(
"Invalid option type 'string' for 'tabstop'",
pcall_err(
exec_lua,
[[
vim.opt.tabstop = '4'
]]
)
)
matches(
"Invalid option type 'boolean' for 'tabstop'",
pcall_err(
exec_lua,
[[
vim.opt.tabstop = true
]]
)
)
matches(
"Invalid option type 'table' for 'tabstop'",
pcall_err(
exec_lua,
[[
vim.opt.tabstop = {4, 2}
]]
)
)
matches(
"Invalid option type 'function' for 'tabstop'",
pcall_err(
exec_lua,
[[
vim.opt.tabstop = function()
return 4
end
]]
)
)
end)
it('should allow to set option with boolean value', function()
eq(
true,
exec_lua [[
vim.opt.undofile = true
return vim.bo.undofile
]]
)
matches(
"Invalid option type 'number' for 'undofile'",
pcall_err(
exec_lua,
[[
vim.opt.undofile = 0
]]
)
)
matches(
"Invalid option type 'table' for 'undofile'",
pcall_err(
exec_lua,
[[
vim.opt.undofile = {true}
]]
)
)
matches(
"Invalid option type 'string' for 'undofile'",
pcall_err(
exec_lua,
[[
vim.opt.undofile = 'true'
]]
)
)
matches(
"Invalid option type 'function' for 'undofile'",
pcall_err(
exec_lua,
[[
vim.opt.undofile = function()
return true
end
]]
)
)
end)
it('should allow to set option with array or string value', function()
eq(
'indent,eol,start',
exec_lua [[
vim.opt.backspace = {'indent','eol','start'}
return vim.go.backspace
]]
)
eq(
'indent,eol,start',
exec_lua [[
vim.opt.backspace = 'indent,eol,start'
return vim.go.backspace
]]
)
matches(
"Invalid option type 'boolean' for 'backspace'",
pcall_err(
exec_lua,
[[
vim.opt.backspace = true
]]
)
)
matches(
"Invalid option type 'number' for 'backspace'",
pcall_err(
exec_lua,
[[
vim.opt.backspace = 2
]]
)
)
matches(
"Invalid option type 'function' for 'backspace'",
pcall_err(
exec_lua,
[[
vim.opt.backspace = function()
return 'indent,eol,start'
end
]]
)
)
end)
it('should allow set option with map or string value', function()
eq(
'eol:~,space:.',
exec_lua [[
vim.opt.listchars = {
eol = "~",
space = ".",
}
return vim.o.listchars
]]
)
eq(
'eol:~,space:.,tab:>~',
exec_lua [[
vim.opt.listchars = "eol:~,space:.,tab:>~"
return vim.o.listchars
]]
)
matches(
"Invalid option type 'boolean' for 'listchars'",
pcall_err(
exec_lua,
[[
vim.opt.listchars = true
]]
)
)
matches(
"Invalid option type 'number' for 'listchars'",
pcall_err(
exec_lua,
[[
vim.opt.listchars = 2
]]
)
)
matches(
"Invalid option type 'function' for 'listchars'",
pcall_err(
exec_lua,
[[
vim.opt.listchars = function()
return "eol:~,space:.,tab:>~"
end
]]
)
)
end)
it('should allow set option with set or string value', function()
local ww = exec_lua [[
vim.opt.whichwrap = {
b = true,
s = 1,
}
return vim.go.whichwrap
]]
eq('b,s', ww)
eq(
'b,s,<,>,[,]',
exec_lua [[
vim.opt.whichwrap = "b,s,<,>,[,]"
return vim.go.whichwrap
]]
)
matches(
"Invalid option type 'boolean' for 'whichwrap'",
pcall_err(
exec_lua,
[[
vim.opt.whichwrap = true
]]
)
)
matches(
"Invalid option type 'number' for 'whichwrap'",
pcall_err(
exec_lua,
[[
vim.opt.whichwrap = 2
]]
)
)
matches(
"Invalid option type 'function' for 'whichwrap'",
pcall_err(
exec_lua,
[[
vim.opt.whichwrap = function()
return "b,s,<,>,[,]"
end
]]
)
)
end)
end)
-- isfname=a,b,c,,,d,e,f
it('can handle isfname ,,,', function()
local result = exec_lua [[
vim.opt.isfname = "a,b,,,c"
return { vim.opt.isfname:get(), vim.go.isfname }
]]
eq({ { ',', 'a', 'b', 'c' }, 'a,b,,,c' }, result)
end)
-- isfname=a,b,c,^,,def
it('can handle isfname ,^,,', function()
local result = exec_lua [[
vim.opt.isfname = "a,b,^,,c"
return { vim.opt.isfname:get(), vim.go.isfname }
]]
eq({ { '^,', 'a', 'b', 'c' }, 'a,b,^,,c' }, result)
end)
describe('https://github.com/neovim/neovim/issues/14828', function()
it('gives empty list when item is empty:array', function()
eq(
{},
exec_lua [[
vim.cmd("set wildignore=")
return vim.opt.wildignore:get()
]]
)
eq(
{},
exec_lua [[
vim.opt.wildignore = {}
return vim.opt.wildignore:get()
]]
)
end)
it('gives empty list when item is empty:set', function()
eq(
{},
exec_lua [[
vim.cmd("set formatoptions=")
return vim.opt.formatoptions:get()
]]
)
eq(
{},
exec_lua [[
vim.opt.formatoptions = {}
return vim.opt.formatoptions:get()
]]
)
end)
it('does not append to empty item', function()
eq(
{ '*.foo', '*.bar' },
exec_lua [[
vim.opt.wildignore = {}
vim.opt.wildignore:append { "*.foo", "*.bar" }
return vim.opt.wildignore:get()
]]
)
end)
it('does not prepend to empty item', function()
eq(
{ '*.foo', '*.bar' },
exec_lua [[
vim.opt.wildignore = {}
vim.opt.wildignore:prepend { "*.foo", "*.bar" }
return vim.opt.wildignore:get()
]]
)
end)
it('append to empty set', function()
eq(
{ t = true },
exec_lua [[
vim.opt.formatoptions = {}
vim.opt.formatoptions:append("t")
return vim.opt.formatoptions:get()
]]
)
end)
it('prepend to empty set', function()
eq(
{ t = true },
exec_lua [[
vim.opt.formatoptions = {}
vim.opt.formatoptions:prepend("t")
return vim.opt.formatoptions:get()
]]
)
end)
end)
end) -- vim.opt
describe('vim.opt_local', function()
it('appends into global value when changing local option value', function()
eq(
{ 'foo,bar,baz,qux' },
exec_lua [[
local result = {}
vim.opt.tags = "foo,bar"
vim.opt_local.tags:append("baz")
vim.opt_local.tags:append("qux")
table.insert(result, vim.bo.tags)
return result
]]
)
end)
end)
describe('vim.opt_global', function()
it('gets current global option value', function()
eq(
{ 'yes' },
exec_lua [[
local result = {}
vim.cmd "setglobal signcolumn=yes"
table.insert(result, vim.opt_global.signcolumn:get())
return result
]]
)
end)
end)
it('vim.cmd', function()
exec_lua [[
vim.cmd "autocmd BufNew * ++once lua BUF = vim.fn.expand('<abuf>')"
vim.cmd "new"
]]
eq('2', fn.luaeval 'BUF')
eq(2, fn.luaeval '#vim.api.nvim_list_bufs()')
-- vim.cmd can be indexed with a command name
exec_lua [[
vim.cmd.let 'g:var = 2'
]]
eq(2, fn.luaeval 'vim.g.var')
end)
it('vim.regex', function()
exec_lua [[
re1 = vim.regex"ab\\+c"
vim.cmd "set nomagic ignorecase"
re2 = vim.regex"xYz"
]]
eq({}, exec_lua [[return {re1:match_str("x ac")}]])
eq({ 3, 7 }, exec_lua [[return {re1:match_str("ac abbc")}]])
api.nvim_buf_set_lines(0, 0, -1, true, { 'yy', 'abc abbc' })
eq({}, exec_lua [[return {re1:match_line(0, 0)}]])
eq({ 0, 3 }, exec_lua [[return {re1:match_line(0, 1)}]])
eq({ 3, 7 }, exec_lua [[return {re1:match_line(0, 1, 1)}]])
eq({ 3, 7 }, exec_lua [[return {re1:match_line(0, 1, 1, 8)}]])
eq({}, exec_lua [[return {re1:match_line(0, 1, 1, 7)}]])
eq({ 0, 3 }, exec_lua [[return {re1:match_line(0, 1, 0, 7)}]])
-- vim.regex() error inside :silent! should not crash. #20546
command([[silent! lua vim.regex('\\z')]])
assert_alive()
end)
it('vim.defer_fn', function()
eq(
false,
exec_lua [[
vim.g.test = false
vim.defer_fn(function() vim.g.test = true end, 150)
return vim.g.test
]]
)
exec_lua [[vim.wait(1000, function() return vim.g.test end)]]
eq(true, exec_lua [[return vim.g.test]])
end)
describe('vim.region', function()
it('charwise', function()
insert(dedent([[
text tααt tααt text
text tαxt txtα tex
text tαxt tαxt
]]))
eq({ 5, 13 }, exec_lua [[ return vim.region(0,{0,5},{0,13},'v',false)[0] ]])
eq({ 5, 15 }, exec_lua [[ return vim.region(0,{0,5},{0,13},'v',true)[0] ]])
eq({ 5, 15 }, exec_lua [[ return vim.region(0,{0,5},{0,14},'v',true)[0] ]])
eq({ 5, 15 }, exec_lua [[ return vim.region(0,{0,5},{0,15},'v',false)[0] ]])
eq({ 5, 17 }, exec_lua [[ return vim.region(0,{0,5},{0,15},'v',true)[0] ]])
eq({ 5, 17 }, exec_lua [[ return vim.region(0,{0,5},{0,16},'v',true)[0] ]])
eq({ 5, 17 }, exec_lua [[ return vim.region(0,{0,5},{0,17},'v',false)[0] ]])
eq({ 5, 18 }, exec_lua [[ return vim.region(0,{0,5},{0,17},'v',true)[0] ]])
end)
it('blockwise', function()
insert([[αα]])
eq({ 0, 5 }, exec_lua [[ return vim.region(0,{0,0},{0,4},'3',true)[0] ]])
end)
it('linewise', function()
insert(dedent([[
text tααt tααt text
text tαxt txtα tex
text tαxt tαxt
]]))
eq({ 0, -1 }, exec_lua [[ return vim.region(0,{1,5},{1,14},'V',true)[1] ]])
end)
it('getpos() input', function()
insert('getpos')
eq({ 0, 6 }, exec_lua [[ return vim.region(0,{0,0},'.','v',true)[0] ]])
end)
end)
describe('vim.on_key', function()
it('tracks Unicode input', function()
insert([[hello world ]])
exec_lua [[
keys = {}
typed = {}
vim.on_key(function(buf, typed_buf)
if buf:byte() == 27 then
buf = "<ESC>"
end
if typed_buf:byte() == 27 then
typed_buf = "<ESC>"
end
table.insert(keys, buf)
table.insert(typed, typed_buf)
end)
]]
insert([[next 🤦 lines å …]])
-- It has escape in the keys pressed
eq('inext 🤦 lines å …<ESC>', exec_lua [[return table.concat(keys, '')]])
eq('inext 🤦 lines å …<ESC>', exec_lua [[return table.concat(typed, '')]])
end)
it('tracks input with modifiers', function()
exec_lua [[
keys = {}
typed = {}
vim.on_key(function(buf, typed_buf)
table.insert(keys, vim.fn.keytrans(buf))
table.insert(typed, vim.fn.keytrans(typed_buf))
end)
]]
feed([[i<C-V><C-;><C-V><C-…><Esc>]])
eq('i<C-V><C-;><C-V><C-…><Esc>', exec_lua [[return table.concat(keys, '')]])
eq('i<C-V><C-;><C-V><C-…><Esc>', exec_lua [[return table.concat(typed, '')]])
end)
it('works with character find and Select mode', function()
insert('12345')
exec_lua [[
typed = {}
vim.cmd('snoremap # @')
vim.on_key(function(buf, typed_buf)
table.insert(typed, vim.fn.keytrans(typed_buf))
end)
]]
feed('F3gHβγδεζ<Esc>gH…<Esc>gH#$%^')
eq('F3gHβγδεζ<Esc>gH…<Esc>gH#$%^', exec_lua [[return table.concat(typed, '')]])
end)
it('allows removing on_key listeners', function()
-- Create some unused namespaces
api.nvim_create_namespace('unused1')
api.nvim_create_namespace('unused2')
api.nvim_create_namespace('unused3')
api.nvim_create_namespace('unused4')
insert([[hello world]])
exec_lua [[
keys = {}
return vim.on_key(function(buf)
if buf:byte() == 27 then
buf = "<ESC>"
end
table.insert(keys, buf)
end, vim.api.nvim_create_namespace("logger"))
]]
insert([[next lines]])
eq(1, exec_lua('return vim.on_key()'))
exec_lua("vim.on_key(nil, vim.api.nvim_create_namespace('logger'))")
eq(0, exec_lua('return vim.on_key()'))
insert([[more lines]])
-- It has escape in the keys pressed
eq('inext lines<ESC>', exec_lua [[return table.concat(keys, '')]])
end)
it('skips any function that caused an error', function()
insert([[hello world]])
exec_lua [[
keys = {}
return vim.on_key(function(buf)
if buf:byte() == 27 then
buf = "<ESC>"
end
table.insert(keys, buf)
if buf == 'l' then
error("Dumb Error")
end
end)
]]
insert([[next lines]])
insert([[more lines]])
-- Only the first letter gets added. After that we remove the callback
eq('inext l', exec_lua [[ return table.concat(keys, '') ]])
end)
it('argument 1 is keys after mapping, argument 2 is typed keys', function()
exec_lua [[
keys = {}
typed = {}
vim.cmd("inoremap hello world")
vim.on_key(function(buf, typed_buf)
if buf:byte() == 27 then
buf = "<ESC>"
end
if typed_buf:byte() == 27 then
typed_buf = "<ESC>"
end
table.insert(keys, buf)
table.insert(typed, typed_buf)
end)
]]
insert('hello')
eq('iworld<ESC>', exec_lua [[return table.concat(keys, '')]])
eq('ihello<ESC>', exec_lua [[return table.concat(typed, '')]])
end)
it('can call vim.fn functions on Ctrl-C #17273', function()
exec_lua([[
_G.ctrl_c_cmdtype = ''
vim.on_key(function(c)
if c == '\3' then
_G.ctrl_c_cmdtype = vim.fn.getcmdtype()
end
end)
]])
feed('/')
poke_eventloop() -- This is needed because Ctrl-C flushes input
feed('<C-C>')
eq('/', exec_lua([[return _G.ctrl_c_cmdtype]]))
end)
end)
describe('vim.wait', function()
before_each(function()
exec_lua [[
-- high precision timer
get_time = function()
return vim.fn.reltimefloat(vim.fn.reltime())
end
]]
end)
it('should run from lua', function()
exec_lua [[vim.wait(100, function() return true end)]]
end)
it('should wait the expected time if false', function()
eq(
{ time = true, wait_result = { false, -1 } },
exec_lua [[
start_time = get_time()
wait_succeed, wait_fail_val = vim.wait(200, function() return false end)
return {
-- 150ms waiting or more results in true. Flaky tests are bad.
time = (start_time + 0.15) < get_time(),
wait_result = {wait_succeed, wait_fail_val}
}
]]
)
end)
it('should not block other events', function()
eq(
{ time = true, wait_result = true },
exec_lua [[
start_time = get_time()
vim.g.timer_result = false
timer = vim.uv.new_timer()
timer:start(100, 0, vim.schedule_wrap(function()
vim.g.timer_result = true
end))
-- Would wait ten seconds if results blocked.
wait_result = vim.wait(10000, function() return vim.g.timer_result end)
timer:close()
return {
time = (start_time + 5) > get_time(),
wait_result = wait_result,
}
]]
)
end)
it('should not process non-fast events when commanded', function()
eq(
{ wait_result = false },
exec_lua [[
start_time = get_time()
vim.g.timer_result = false
timer = vim.uv.new_timer()
timer:start(100, 0, vim.schedule_wrap(function()
vim.g.timer_result = true
end))
wait_result = vim.wait(300, function() return vim.g.timer_result end, nil, true)
timer:close()
return {
wait_result = wait_result,
}
]]
)
end)
it('should work with vim.defer_fn', function()
eq(
{ time = true, wait_result = true },
exec_lua [[
start_time = get_time()
vim.defer_fn(function() vim.g.timer_result = true end, 100)
wait_result = vim.wait(10000, function() return vim.g.timer_result end)
return {
time = (start_time + 5) > get_time(),
wait_result = wait_result,
}
]]
)
end)
it('should not crash when callback errors', function()
local result = exec_lua [[
return {pcall(function() vim.wait(1000, function() error("As Expected") end) end)}
]]
eq({ false, '[string "<nvim>"]:1: As Expected' }, { result[1], remove_trace(result[2]) })
end)
it('if callback is passed, it must be a function', function()
eq(
{ false, 'vim.wait: if passed, condition must be a function' },
exec_lua [[
return {pcall(function() vim.wait(1000, 13) end)}
]]
)
end)
it('should allow waiting with no callback, explicit', function()
eq(
true,
exec_lua [[
local start_time = vim.uv.hrtime()
vim.wait(50, nil)
return vim.uv.hrtime() - start_time > 25000
]]
)
end)
it('should allow waiting with no callback, implicit', function()
eq(
true,
exec_lua [[
local start_time = vim.uv.hrtime()
vim.wait(50)
return vim.uv.hrtime() - start_time > 25000
]]
)
end)
it('should call callbacks exactly once if they return true immediately', function()
eq(
true,
exec_lua [[
vim.g.wait_count = 0
vim.wait(1000, function()
vim.g.wait_count = vim.g.wait_count + 1
return true
end, 20)
return vim.g.wait_count == 1
]]
)
end)
it('should call callbacks few times with large `interval`', function()
eq(
true,
exec_lua [[
vim.g.wait_count = 0
vim.wait(50, function() vim.g.wait_count = vim.g.wait_count + 1 end, 200)
return vim.g.wait_count < 5
]]
)
end)
it('should play nice with `not` when fails', function()
eq(
true,
exec_lua [[
if not vim.wait(50, function() end) then
return true
end
return false
]]
)
end)
it('should play nice with `if` when success', function()
eq(
true,
exec_lua [[
if vim.wait(50, function() return true end) then
return true
end
return false
]]
)
end)
it('should return immediately with false if timeout is 0', function()
eq(
{ false, -1 },
exec_lua [[
return {
vim.wait(0, function() return false end)
}
]]
)
end)
it('should work with tables with __call', function()
eq(
true,
exec_lua [[
local t = setmetatable({}, {__call = function(...) return true end})
return vim.wait(100, t, 10)
]]
)
end)
it('should work with tables with __call that change', function()
eq(
true,
exec_lua [[
local t = {count = 0}
setmetatable(t, {
__call = function()
t.count = t.count + 1
return t.count > 3
end
})
return vim.wait(1000, t, 10)
]]
)
end)
it('should not work with negative intervals', function()
local pcall_result = exec_lua [[
return pcall(function() vim.wait(1000, function() return false end, -1) end)
]]
eq(false, pcall_result)
end)
it('should not work with weird intervals', function()
local pcall_result = exec_lua [[
return pcall(function() vim.wait(1000, function() return false end, 'a string value') end)
]]
eq(false, pcall_result)
end)
describe('returns -2 when interrupted', function()
before_each(function()
local channel = api.nvim_get_chan_info(0).id
api.nvim_set_var('channel', channel)
end)
it('without callback', function()
exec_lua([[
function _G.Wait()
vim.rpcnotify(vim.g.channel, 'ready')
local _, interrupted = vim.wait(4000)
vim.rpcnotify(vim.g.channel, 'wait', interrupted)
end
]])
feed(':lua _G.Wait()<CR>')
eq({ 'notification', 'ready', {} }, next_msg(500))
feed('<C-C>')
eq({ 'notification', 'wait', { -2 } }, next_msg(500))
end)
it('with callback', function()
exec_lua([[
function _G.Wait()
vim.rpcnotify(vim.g.channel, 'ready')
local _, interrupted = vim.wait(4000, function() end)
vim.rpcnotify(vim.g.channel, 'wait', interrupted)
end
]])
feed(':lua _G.Wait()<CR>')
eq({ 'notification', 'ready', {} }, next_msg(500))
feed('<C-C>')
eq({ 'notification', 'wait', { -2 } }, next_msg(500))
end)
end)
it('should not run in fast callbacks #26122', function()
local screen = Screen.new(80, 10)
screen:attach()
exec_lua([[
local timer = vim.uv.new_timer()
timer:start(0, 0, function()
timer:close()
vim.wait(100, function() end)
end)
]])
screen:expect({
any = pesc('E5560: vim.wait must not be called in a lua loop callback'),
})
feed('<CR>')
assert_alive()
end)
end)
it('vim.notify_once', function()
local screen = Screen.new(60, 5)
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue },
[1] = { foreground = Screen.colors.Red },
})
screen:attach()
screen:expect {
grid = [[
^ |
{0:~ }|*3
|
]],
}
exec_lua [[vim.notify_once("I'll only tell you this once...", vim.log.levels.WARN)]]
screen:expect {
grid = [[
^ |
{0:~ }|*3
{1:I'll only tell you this once...} |
]],
}
feed('<C-l>')
screen:expect {
grid = [[
^ |
{0:~ }|*3
|
]],
}
exec_lua [[vim.notify_once("I'll only tell you this once...")]]
screen:expect_unchanged()
end)
describe('vim.schedule_wrap', function()
it('preserves argument lists', function()
exec_lua [[
local fun = vim.schedule_wrap(function(kling, klang, klonk)
vim.rpcnotify(1, 'mayday_mayday', {a=kling, b=klang, c=klonk})
end)
fun("BOB", nil, "MIKE")
]]
eq({ 'notification', 'mayday_mayday', { { a = 'BOB', c = 'MIKE' } } }, next_msg())
-- let's gooooo
exec_lua [[
vim.schedule_wrap(function(...) vim.rpcnotify(1, 'boogalo', select('#', ...)) end)(nil,nil,nil,nil)
]]
eq({ 'notification', 'boogalo', { 4 } }, next_msg())
end)
end)
describe('vim.api.nvim_buf_call', function()
it('can access buf options', function()
local buf1 = api.nvim_get_current_buf()
local buf2 = exec_lua [[
buf2 = vim.api.nvim_create_buf(false, true)
return buf2
]]
eq(false, api.nvim_get_option_value('autoindent', { buf = buf1 }))
eq(false, api.nvim_get_option_value('autoindent', { buf = buf2 }))
local val = exec_lua [[
return vim.api.nvim_buf_call(buf2, function()
vim.cmd "set autoindent"
return vim.api.nvim_get_current_buf()
end)
]]
eq(false, api.nvim_get_option_value('autoindent', { buf = buf1 }))
eq(true, api.nvim_get_option_value('autoindent', { buf = buf2 }))
eq(buf1, api.nvim_get_current_buf())
eq(buf2, val)
end)
it('does not cause ml_get errors with invalid visual selection', function()
-- Should be fixed by vim-patch:8.2.4028.
exec_lua [[
local api = vim.api
local t = function(s) return api.nvim_replace_termcodes(s, true, true, true) end
api.nvim_buf_set_lines(0, 0, -1, true, {"a", "b", "c"})
api.nvim_feedkeys(t "G<C-V>", "txn", false)
api.nvim_buf_call(api.nvim_create_buf(false, true), function() vim.cmd "redraw" end)
]]
end)
it('can be nested crazily with hidden buffers', function()
eq(
true,
exec_lua([[
local function scratch_buf_call(fn)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value('cindent', true, {buf = buf})
return vim.api.nvim_buf_call(buf, function()
return vim.api.nvim_get_current_buf() == buf
and vim.api.nvim_get_option_value('cindent', {buf = buf})
and fn()
end) and vim.api.nvim_buf_delete(buf, {}) == nil
end
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return scratch_buf_call(function()
return true
end)
end)
end)
end)
end)
end)
end)
end)
end)
end)
end)
end)
]])
)
end)
it('can return values by reference', function()
eq(
{ 4, 7 },
exec_lua [[
local val = {4, 10}
local ref = vim.api.nvim_buf_call(0, function() return val end)
ref[2] = 7
return val
]]
)
end)
end)
describe('vim.api.nvim_win_call', function()
it('can access window options', function()
command('vsplit')
local win1 = api.nvim_get_current_win()
command('wincmd w')
local win2 = exec_lua [[
win2 = vim.api.nvim_get_current_win()
return win2
]]
command('wincmd p')
eq('', api.nvim_get_option_value('winhighlight', { win = win1 }))
eq('', api.nvim_get_option_value('winhighlight', { win = win2 }))
local val = exec_lua [[
return vim.api.nvim_win_call(win2, function()
vim.cmd "setlocal winhighlight=Normal:Normal"
return vim.api.nvim_get_current_win()
end)
]]
eq('', api.nvim_get_option_value('winhighlight', { win = win1 }))
eq('Normal:Normal', api.nvim_get_option_value('winhighlight', { win = win2 }))
eq(win1, api.nvim_get_current_win())
eq(win2, val)
end)
it('does not cause ml_get errors with invalid visual selection', function()
-- Add lines to the current buffer and make another window looking into an empty buffer.
exec_lua [[
_G.api = vim.api
_G.t = function(s) return api.nvim_replace_termcodes(s, true, true, true) end
_G.win_lines = api.nvim_get_current_win()
vim.cmd "new"
_G.win_empty = api.nvim_get_current_win()
api.nvim_set_current_win(win_lines)
api.nvim_buf_set_lines(0, 0, -1, true, {"a", "b", "c"})
]]
-- Start Visual in current window, redraw in other window with fewer lines.
-- Should be fixed by vim-patch:8.2.4018.
exec_lua [[
api.nvim_feedkeys(t "G<C-V>", "txn", false)
api.nvim_win_call(win_empty, function() vim.cmd "redraw" end)
]]
-- Start Visual in current window, extend it in other window with more lines.
-- Fixed for win_execute by vim-patch:8.2.4026, but nvim_win_call should also not be affected.
exec_lua [[
api.nvim_feedkeys(t "<Esc>gg", "txn", false)
api.nvim_set_current_win(win_empty)
api.nvim_feedkeys(t "gg<C-V>", "txn", false)
api.nvim_win_call(win_lines, function() api.nvim_feedkeys(t "G<C-V>", "txn", false) end)
vim.cmd "redraw"
]]
end)
it('updates ruler if cursor moved', function()
-- Fixed for win_execute in vim-patch:8.1.2124, but should've applied to nvim_win_call too!
local screen = Screen.new(30, 5)
screen:set_default_attr_ids {
[1] = { reverse = true },
[2] = { bold = true, reverse = true },
}
screen:attach()
exec_lua [[
_G.api = vim.api
vim.opt.ruler = true
local lines = {}
for i = 0, 499 do lines[#lines + 1] = tostring(i) end
api.nvim_buf_set_lines(0, 0, -1, true, lines)
api.nvim_win_set_cursor(0, {20, 0})
vim.cmd "split"
_G.win = api.nvim_get_current_win()
vim.cmd "wincmd w | redraw"
]]
screen:expect [[
19 |
{1:[No Name] [+] 20,1 3%}|
^19 |
{2:[No Name] [+] 20,1 3%}|
|
]]
exec_lua [[
api.nvim_win_call(win, function() api.nvim_win_set_cursor(0, {100, 0}) end)
vim.cmd "redraw"
]]
screen:expect [[
99 |
{1:[No Name] [+] 100,1 19%}|
^19 |
{2:[No Name] [+] 20,1 3%}|
|
]]
end)
it('can return values by reference', function()
eq(
{ 7, 10 },
exec_lua [[
local val = {4, 10}
local ref = vim.api.nvim_win_call(0, function() return val end)
ref[1] = 7
return val
]]
)
end)
it('layout in current tabpage does not affect windows in others', function()
command('tab split')
local t2_move_win = api.nvim_get_current_win()
command('vsplit')
local t2_other_win = api.nvim_get_current_win()
command('tabprevious')
matches('E36: Not enough room$', pcall_err(command, 'execute "split|"->repeat(&lines)'))
command('vsplit')
-- Without vim-patch:8.2.3862, this gives E36, despite just the 1st tabpage being full.
exec_lua('vim.api.nvim_win_call(..., function() vim.cmd.wincmd "J" end)', t2_move_win)
eq({ 'col', { { 'leaf', t2_other_win }, { 'leaf', t2_move_win } } }, fn.winlayout(2))
end)
end)
describe('vim.iconv', function()
it('can convert strings', function()
eq(
'hello',
exec_lua [[
return vim.iconv('hello', 'latin1', 'utf-8')
]]
)
end)
it('can validate arguments', function()
eq(
{ false, 'Expected at least 3 arguments' },
exec_lua [[
return {pcall(vim.iconv, 'hello')}
]]
)
eq(
{ false, "bad argument #3 to '?' (expected string)" },
exec_lua [[
return {pcall(vim.iconv, 'hello', 'utf-8', true)}
]]
)
end)
it('can handle bad encodings', function()
eq(
NIL,
exec_lua [[
return vim.iconv('hello', 'foo', 'bar')
]]
)
end)
it('can handle strings with NUL bytes', function()
eq(
7,
exec_lua [[
local a = string.char(97, 98, 99, 0, 100, 101, 102) -- abc\0def
return string.len(vim.iconv(a, 'latin1', 'utf-8'))
]]
)
end)
end)
describe('vim.defaulttable', function()
it('creates nested table by default', function()
eq(
{ b = { c = 1 } },
exec_lua [[
local a = vim.defaulttable()
a.b.c = 1
return a
]]
)
end)
it('allows to create default objects', function()
eq(
{ b = 1 },
exec_lua [[
local a = vim.defaulttable(function() return 0 end)
a.b = a.b + 1
return a
]]
)
end)
it('accepts the key name', function()
eq(
{ b = 'b', c = 'c' },
exec_lua [[
local a = vim.defaulttable(function(k) return k end)
local _ = a.b
local _ = a.c
return a
]]
)
end)
end)
it('vim.lua_omnifunc', function()
local screen = Screen.new(60, 5)
screen:set_default_attr_ids {
[1] = { foreground = Screen.colors.Blue1, bold = true },
[2] = { background = Screen.colors.WebGray },
[3] = { background = Screen.colors.LightMagenta },
[4] = { bold = true },
[5] = { foreground = Screen.colors.SeaGreen, bold = true },
}
screen:attach()
command [[ set omnifunc=v:lua.vim.lua_omnifunc ]]
-- Note: the implementation is shared with lua command line completion.
-- More tests for completion in lua/command_line_completion_spec.lua
feed [[ivim.insp<c-x><c-o>]]
screen:expect {
grid = [[
vim.inspect^ |
{1:~ }{2: inspect }{1: }|
{1:~ }{3: inspect_pos }{1: }|
{1:~ }|
{4:-- Omni completion (^O^N^P) }{5:match 1 of 2} |
]],
}
end)
it('vim.print', function()
-- vim.print() returns its args.
eq(
{ 42, 'abc', { a = { b = 77 } } },
exec_lua [[return {vim.print(42, 'abc', { a = { b = 77 }})}]]
)
-- vim.print() pretty-prints the args.
eq(
dedent [[
42
abc
{
a = {
b = 77
}
}]],
eval [[execute('lua vim.print(42, "abc", { a = { b = 77 }})')]]
)
end)
it('vim.F.if_nil', function()
local function if_nil(...)
return exec_lua(
[[
local args = {...}
local nargs = select('#', ...)
for i = 1, nargs do
if args[i] == vim.NIL then
args[i] = nil
end
end
return vim.F.if_nil(unpack(args, 1, nargs))
]],
...
)
end
local a = NIL
local b = NIL
local c = 42
local d = false
eq(42, if_nil(a, c))
eq(false, if_nil(d, b))
eq(42, if_nil(a, b, c, d))
eq(false, if_nil(d))
eq(false, if_nil(d, c))
eq(NIL, if_nil(a))
end)
it('lpeg', function()
eq(
5,
exec_lua [[
local m = vim.lpeg
return m.match(m.R'09'^1, '4504ab')
]]
)
eq(4, exec_lua [[ return vim.re.match("abcde", '[a-c]+') ]])
end)
it('vim.ringbuf', function()
local results = exec_lua([[
local ringbuf = vim.ringbuf(3)
ringbuf:push("a") -- idx: 0
local peeka1 = ringbuf:peek()
local peeka2 = ringbuf:peek()
local popa = ringbuf:pop()
local popnil = ringbuf:pop()
ringbuf:push("a") -- idx: 1
ringbuf:push("b") -- idx: 2
-- doesn't read last added item, but uses separate read index
local pop_after_add_b = ringbuf:pop()
ringbuf:push("c") -- idx: 3 wraps around, overrides idx: 0 "a"
ringbuf:push("d") -- idx: 4 wraps around, overrides idx: 1 "a"
return {
peeka1 = peeka1,
peeka2 = peeka2,
pop1 = popa,
pop2 = popnil,
pop3 = ringbuf:pop(),
pop4 = ringbuf:pop(),
pop5 = ringbuf:pop(),
pop_after_add_b = pop_after_add_b,
}
]])
local expected = {
peeka1 = 'a',
peeka2 = 'a',
pop1 = 'a',
pop2 = nil,
pop3 = 'b',
pop4 = 'c',
pop5 = 'd',
pop_after_add_b = 'a',
}
eq(expected, results)
end)
end)
describe('lua: builtin modules', function()
local function do_tests()
eq(2, exec_lua [[return vim.tbl_count {x=1,y=2}]])
eq('{ 10, "spam" }', exec_lua [[return vim.inspect {10, 'spam'}]])
end
it('works', function()
clear()
do_tests()
end)
it('works when disabled', function()
clear('--luamod-dev')
do_tests()
end)
it('works without runtime', function()
clear { env = { VIMRUNTIME = 'fixtures/a' } }
do_tests()
end)
it('fails when disabled without runtime', function()
clear()
command("let $VIMRUNTIME='fixtures/a'")
-- Use system([nvim,…]) instead of clear() to avoid stderr noise. #21844
local out = fn.system({
nvim_prog,
'--clean',
'--luamod-dev',
[[+call nvim_exec_lua('return vim.tbl_count {x=1,y=2}')]],
'+qa!',
}):gsub('\r\n', '\n')
eq(1, eval('v:shell_error'))
matches("'vim%.shared' not found", out)
end)
end)
describe('lua: require("mod") from packages', function()
before_each(function()
clear('--cmd', 'set rtp+=test/functional/fixtures pp+=test/functional/fixtures')
end)
it('propagates syntax error', function()
local syntax_error_msg = exec_lua [[
local _, err = pcall(require, "syntax_error")
return err
]]
matches('unexpected symbol', syntax_error_msg)
end)
it('uses the right order of mod.lua vs mod/init.lua', function()
-- lua/fancy_x.lua takes precedence over lua/fancy_x/init.lua
eq('I am fancy_x.lua', exec_lua [[ return require'fancy_x' ]])
-- but lua/fancy_y/init.lua takes precedence over after/lua/fancy_y.lua
eq('I am init.lua of fancy_y!', exec_lua [[ return require'fancy_y' ]])
-- safety check: after/lua/fancy_z.lua is still loaded
eq('I am fancy_z.lua', exec_lua [[ return require'fancy_z' ]])
end)
end)
describe('vim.keymap', function()
before_each(clear)
it('can make a mapping', function()
eq(
0,
exec_lua [[
GlobalCount = 0
vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end)
return GlobalCount
]]
)
feed('asdf\n')
eq(1, exec_lua [[return GlobalCount]])
end)
it('can make an expr mapping', function()
exec_lua [[
vim.keymap.set('n', 'aa', function() return '<Insert>π<C-V><M-π>foo<lt><Esc>' end, {expr = true})
]]
feed('aa')
eq({ 'π<M-π>foo<' }, api.nvim_buf_get_lines(0, 0, -1, false))
end)
it('can overwrite a mapping', function()
eq(
0,
exec_lua [[
GlobalCount = 0
vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end)
return GlobalCount
]]
)
feed('asdf\n')
eq(1, exec_lua [[return GlobalCount]])
exec_lua [[
vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount - 1 end)
]]
feed('asdf\n')
eq(0, exec_lua [[return GlobalCount]])
end)
it('can unmap a mapping', function()
eq(
0,
exec_lua [[
GlobalCount = 0
vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end)
return GlobalCount
]]
)
feed('asdf\n')
eq(1, exec_lua [[return GlobalCount]])
exec_lua [[
vim.keymap.del('n', 'asdf')
]]
feed('asdf\n')
eq(1, exec_lua [[return GlobalCount]])
eq('\nNo mapping found', n.exec_capture('nmap asdf'))
end)
it('works with buffer-local mappings', function()
eq(
0,
exec_lua [[
GlobalCount = 0
vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end, {buffer=true})
return GlobalCount
]]
)
feed('asdf\n')
eq(1, exec_lua [[return GlobalCount]])
exec_lua [[
vim.keymap.del('n', 'asdf', {buffer=true})
]]
feed('asdf\n')
eq(1, exec_lua [[return GlobalCount]])
eq('\nNo mapping found', n.exec_capture('nmap asdf'))
end)
it('does not mutate the opts parameter', function()
eq(
true,
exec_lua [[
opts = {buffer=true}
vim.keymap.set('n', 'asdf', function() end, opts)
return opts.buffer
]]
)
eq(
true,
exec_lua [[
vim.keymap.del('n', 'asdf', opts)
return opts.buffer
]]
)
end)
it('can do <Plug> mappings', function()
eq(
0,
exec_lua [[
GlobalCount = 0
vim.keymap.set('n', '<plug>(asdf)', function() GlobalCount = GlobalCount + 1 end)
vim.keymap.set('n', 'ww', '<plug>(asdf)')
return GlobalCount
]]
)
feed('ww\n')
eq(1, exec_lua [[return GlobalCount]])
end)
end)
describe('Vimscript function exists()', function()
it('can check a lua function', function()
eq(
1,
exec_lua [[
_G.test = function() print("hello") end
return vim.fn.exists('*v:lua.test')
]]
)
eq(1, fn.exists('*v:lua.require("mpack").decode'))
eq(1, fn.exists("*v:lua.require('mpack').decode"))
eq(1, fn.exists('*v:lua.require"mpack".decode'))
eq(1, fn.exists("*v:lua.require'mpack'.decode"))
eq(1, fn.exists("*v:lua.require('vim.lsp').start"))
eq(1, fn.exists('*v:lua.require"vim.lsp".start'))
eq(1, fn.exists("*v:lua.require'vim.lsp'.start"))
eq(0, fn.exists("*v:lua.require'vim.lsp'.unknown"))
eq(0, fn.exists('*v:lua.?'))
end)
end)
|