1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573
|
Description: restore tests and rake_tasks v1.1.2
Author: HIGUCHI Daisuke (VDR dai) <dai@debian.org>
Origin: vendor
Forwarded: not-needed
Last-Update: 2017-10-22
diff --git a/Rakefile b/Rakefile
new file mode 100644
index 0000000..c9b1e8a
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,37 @@
+require 'bundler/gem_tasks'
+
+$:.unshift File.dirname(__FILE__) unless $:.include? '.'
+
+ROOT = '.'
+LIB_ROOT = File.join ROOT, 'lib'
+
+task :default => :test
+
+if File.directory? 'rake_tasks'
+
+ # load rake tasks from subfolder
+ for task_file in Dir['rake_tasks/*.rake'].sort
+ load task_file
+ end
+
+else
+
+ # fallback tasks when rake_tasks folder is not present (eg. in the distribution package)
+ desc 'Run CodeRay tests (basic)'
+ task :test do
+ ruby './test/functional/suite.rb'
+ ruby './test/functional/for_redcloth.rb'
+ end
+
+ gem 'rdoc' if defined? gem
+ require 'rdoc/task'
+ desc 'Generate documentation for CodeRay'
+ Rake::RDocTask.new :doc do |rd|
+ rd.title = 'CodeRay Documentation'
+ rd.main = 'README_INDEX.rdoc'
+ rd.rdoc_files.add Dir['lib']
+ rd.rdoc_files.add rd.main
+ rd.rdoc_dir = 'doc'
+ end
+
+end
diff --git a/rake_tasks/benchmark.rake b/rake_tasks/benchmark.rake
new file mode 100644
index 0000000..8edeffb
--- /dev/null
+++ b/rake_tasks/benchmark.rake
@@ -0,0 +1,6 @@
+desc 'Do a benchmark'
+task :benchmark do
+ ruby 'bench/bench.rb ruby html'
+end
+
+task :bench => :benchmark
diff --git a/rake_tasks/code_statistics.rb b/rake_tasks/code_statistics.rb
new file mode 100644
index 0000000..0a2016b
--- /dev/null
+++ b/rake_tasks/code_statistics.rb
@@ -0,0 +1,171 @@
+# From rails (http://rubyonrails.com)
+#
+# Improved by murphy
+class CodeStatistics
+
+ TEST_TYPES = /\btest/i
+
+ # Create a new Code Statistic.
+ #
+ # Rakefile Example:
+ #
+ # desc 'Report code statistics (LOC) from the application'
+ # task :stats => :copy_files do
+ # require 'rake_helpers/code_statistics'
+ # CodeStatistics.new(
+ # ["Main", "lib"],
+ # ["Tests", "test"],
+ # ["Demos", "demo"]
+ # ).to_s
+ # end
+ def initialize(*pairs)
+ @pairs = pairs
+ @statistics = calculate_statistics
+ @total = if pairs.empty? then nil else calculate_total end
+ end
+
+ # Print a textual table viewing the stats
+ #
+ # Intended for console output.
+ def print
+ print_header
+ @pairs.each { |name, path| print_line name, @statistics[name] }
+ print_splitter
+
+ if @total
+ print_line 'Total', @total
+ print_splitter
+ end
+
+ print_code_test_stats
+ end
+
+private
+
+ DEFAULT_FILE_PATTERN = /\.rb$/
+
+ def calculate_statistics
+ @pairs.inject({}) do |stats, (name, path, pattern, is_ruby_code)|
+ pattern ||= DEFAULT_FILE_PATTERN
+ path = File.join path, '*.rb'
+ stats[name] = calculate_directory_statistics path, pattern, is_ruby_code
+ stats
+ end
+ end
+
+ def calculate_directory_statistics directory, pattern = DEFAULT_FILE_PATTERN, is_ruby_code = true
+ is_ruby_code = true if is_ruby_code.nil?
+ stats = Hash.new 0
+
+ Dir[directory].each do |file_name|
+ p "Scanning #{file_name}..." if $VERBOSE
+ next unless file_name =~ pattern
+
+ lines = codelines = classes = modules = methods = 0
+ empty_lines = comment_lines = 0
+ in_comment_block = false
+
+ File.readlines(file_name).each do |line|
+ lines += 1
+ if line[/^\s*$/]
+ empty_lines += 1
+ elsif is_ruby_code
+ case line
+ when /^=end\b/
+ comment_lines += 1
+ in_comment_block = false
+ when in_comment_block
+ comment_lines += 1
+ when /^\s*class\b/
+ classes += 1
+ when /^\s*module\b/
+ modules += 1
+ when /^\s*def\b/
+ methods += 1
+ when /^\s*#/
+ comment_lines += 1
+ when /^=begin\b/
+ in_comment_block = false
+ comment_lines += 1
+ when /^__END__$/
+ in_comment_block = true
+ end
+ end
+ end
+
+ codelines = lines - comment_lines - empty_lines
+
+ stats[:lines] += lines
+ stats[:comments] += comment_lines
+ stats[:codelines] += codelines
+ stats[:classes] += classes
+ stats[:modules] += modules
+ stats[:methods] += methods
+ stats[:files] += 1
+ end
+
+ stats
+ end
+
+ def calculate_total
+ total = Hash.new 0
+ @statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
+ total
+ end
+
+ def calculate_code
+ code_loc = 0
+ @statistics.each { |k, v| code_loc += v[:codelines] unless k[TEST_TYPES] }
+ code_loc
+ end
+
+ def calculate_tests
+ test_loc = 0
+ @statistics.each { |k, v| test_loc += v[:codelines] if k[TEST_TYPES] }
+ test_loc
+ end
+
+ def print_header
+ print_splitter
+ puts "| T=Test Name | Files | Lines | LOC | Comments | Classes | Modules | Methods | M/C | LOC/M |"
+ print_splitter
+ end
+
+ def print_splitter
+ puts "+---------------------------+-------+-------+-------+----------+---------+---------+---------+-----+-------+"
+ end
+
+ def print_line name, statistics
+ m_over_c = (statistics[:methods] / (statistics[:classes] + statistics[:modules])) rescue m_over_c = 0
+ loc_over_m = (statistics[:codelines] / statistics[:methods]) - 2 rescue loc_over_m = 0
+
+ if name[TEST_TYPES]
+ name = "T #{name}"
+ else
+ name = " #{name}"
+ end
+
+ line = "| %-25s | %5d | %5d | %5d | %8d | %7d | %7d | %7d | %3d | %5d |" % (
+ [name, *statistics.values_at(:files, :lines, :codelines, :comments, :classes, :modules, :methods)] +
+ [m_over_c, loc_over_m] )
+
+ puts line
+ end
+
+ def print_code_test_stats
+ code = calculate_code
+ tests = calculate_tests
+
+ puts " Code LOC = #{code} Test LOC = #{tests} Code:Test Ratio = [1 : #{sprintf("%.2f", tests.to_f/code)}]"
+ puts ""
+ end
+
+end
+
+# Run a test script.
+if $0 == __FILE__
+ $VERBOSE = true
+ CodeStatistics.new(
+ ['This dir', File.dirname(__FILE__)]
+ ).print
+end
diff --git a/rake_tasks/documentation.rake b/rake_tasks/documentation.rake
new file mode 100644
index 0000000..4f7cef7
--- /dev/null
+++ b/rake_tasks/documentation.rake
@@ -0,0 +1,23 @@
+begin
+ if RUBY_VERSION >= '1.8.7'
+ gem 'rdoc' if defined? gem
+ require 'rdoc/task'
+ else
+ require 'rake/rdoctask'
+ end
+rescue LoadError
+ warn 'Please gem install rdoc.'
+end
+
+desc 'Generate documentation for CodeRay'
+Rake::RDocTask.new :doc do |rd|
+ rd.main = 'lib/README'
+ rd.title = 'CodeRay Documentation'
+
+ rd.options << '--line-numbers' << '--tab-width' << '2'
+
+ rd.main = 'README_INDEX.rdoc'
+ rd.rdoc_files.add 'README_INDEX.rdoc'
+ rd.rdoc_files.add Dir['lib']
+ rd.rdoc_dir = 'doc'
+end if defined? Rake::RDocTask
diff --git a/rake_tasks/generator.rake b/rake_tasks/generator.rake
new file mode 100644
index 0000000..284adcb
--- /dev/null
+++ b/rake_tasks/generator.rake
@@ -0,0 +1,72 @@
+namespace :generate do
+ desc 'generates a new scanner NAME=lang [ALT=alternative,plugin,ids] [EXT=file,extensions] [BASE=base lang]'
+ task :scanner do
+ raise 'I need a scanner name; use NAME=lang' unless scanner_class_name = ENV['NAME']
+ raise "Invalid lang: #{scanner_class_name}; use NAME=lang." unless /\A\w+\z/ === scanner_class_name
+ require 'active_support/all'
+ lang = scanner_class_name.underscore
+ class_name = scanner_class_name.camelize
+
+ def scanner_file_for_lang lang
+ File.join(LIB_ROOT, 'coderay', 'scanners', lang + '.rb')
+ end
+
+ scanner_file = scanner_file_for_lang lang
+ if File.exist? scanner_file
+ print "#{scanner_file} already exists. Overwrite? [y|N] "
+ exit unless $stdin.gets.chomp.downcase == 'y'
+ end
+
+ base_lang = ENV.fetch('BASE', 'json')
+ base_scanner_file = scanner_file_for_lang(base_lang)
+ puts "Reading base scanner #{base_scanner_file}..."
+ base_scanner = File.read base_scanner_file
+ puts "Writing new scanner #{scanner_file}..."
+ File.open(scanner_file, 'w') do |file|
+ file.write base_scanner.
+ sub(/class \w+ < Scanner/, "class #{class_name} < Scanner").
+ sub('# Scanner for JSON (JavaScript Object Notation).', "# A scanner for #{scanner_class_name}.").
+ sub(/register_for :\w+/, "register_for :#{lang}").
+ sub(/file_extension '\S+'/, "file_extension '#{ENV.fetch('EXT', lang).split(',').first}'")
+ end
+
+ test_dir = File.join(ROOT, 'test', 'scanners', lang)
+ unless File.exist? test_dir
+ puts "Creating test folder #{test_dir}..."
+ sh "mkdir #{test_dir}"
+ end
+ test_suite_file = File.join(test_dir, 'suite.rb')
+ unless File.exist? test_suite_file
+ puts "Creating test suite file #{test_suite_file}..."
+ base_suite = File.read File.join(test_dir, '..', 'ruby', 'suite.rb')
+ File.open(test_suite_file, 'w') do |file|
+ file.write base_suite.sub(/class Ruby/, "class #{class_name}")
+ end
+ end
+
+ if extensions = ENV['EXT']
+ file_type_file = File.join(LIB_ROOT, 'coderay', 'helpers', 'filetype.rb')
+ puts "Not automated. Remember to add your extensions to #{file_type_file}:"
+ for ext in extensions.split(',')
+ puts " '#{ext}' => :#{lang},"
+ end
+ end
+
+ if alternative_ids = ENV['ALT'] && alternative_ids != lang
+ map_file = File.join(LIB_ROOT, 'coderay', 'scanners', '_map.rb')
+ puts "Not automated. Remember to add your alternative plugin ids to #{map_file}:"
+ for id in alternative_ids.split(',')
+ puts " :#{id} => :#{lang},"
+ end
+ end
+
+ print 'Add to git? [Y|n] '
+ answer = $stdin.gets.chomp.downcase
+ if answer.empty? || answer == 'y'
+ sh "git add #{scanner_file}"
+ cd File.join('test', 'scanners') do
+ sh "git add #{lang}"
+ end
+ end
+ end
+end
diff --git a/rake_tasks/statistic.rake b/rake_tasks/statistic.rake
new file mode 100644
index 0000000..d30e9b1
--- /dev/null
+++ b/rake_tasks/statistic.rake
@@ -0,0 +1,19 @@
+desc 'Report code statistics (LOC) from the application'
+task :stats do
+ require './rake_tasks/code_statistics'
+ CodeStatistics.new(
+ ['Main', 'lib', /coderay.rb$/],
+ ['CodeRay', 'lib/coderay/'],
+ [' Scanners', 'lib/coderay/scanners/**'],
+ [' Encoders', 'lib/coderay/encoders/**'],
+ [' Helpers', 'lib/coderay/helpers/**'],
+ [' Styles', 'lib/coderay/styles/**'],
+ ['Executable', 'bin', /coderay$/],
+ ['Executable Tests', 'test/executable/**'],
+ ['Functional Tests', 'test/functional/**'],
+ ['Scanner Tests', 'test/scanners/**', /suite\.rb$/],
+ ['Unit Tests', 'test/unit/**'],
+ # [' Test Data', 'test/scanners/**', /\.in\./, false],
+ ['Demos', 'sample/**']
+ ).print
+end
diff --git a/rake_tasks/test.rake b/rake_tasks/test.rake
new file mode 100644
index 0000000..1a23a5b
--- /dev/null
+++ b/rake_tasks/test.rake
@@ -0,0 +1,82 @@
+namespace :test do
+ desc 'run functional tests'
+ task :functional do
+ ruby './test/functional/suite.rb'
+ ruby './test/functional/for_redcloth.rb' unless (''.chop! rescue true)
+ end
+
+ desc 'run unit tests'
+ task :units do
+ ruby './test/unit/suite.rb'
+ end
+
+ scanner_suite = 'test/scanners/suite.rb'
+ desc 'run all scanner tests'
+ task :scanners => :update_scanner_suite do
+ ruby scanner_suite
+ end
+
+ desc 'update scanner test suite from GitHub'
+ task :update_scanner_suite do
+ if File.exist? scanner_suite
+ Dir.chdir File.dirname(scanner_suite) do
+ if File.directory? '.git'
+ puts 'Updating scanner test suite...'
+ sh 'git pull'
+ elsif File.directory? '.svn'
+ raise <<-ERROR
+Found the deprecated Subversion scanner test suite in ./#{File.dirname(scanner_suite)}.
+Please rename or remove it and run again to use the GitHub repository:
+
+ mv test/scanners test/scanners-old
+ ERROR
+ else
+ raise 'No scanner test suite found.'
+ end
+ end
+ else
+ puts 'Downloading scanner test suite...'
+ sh 'git clone https://github.com/rubychan/coderay-scanner-tests.git test/scanners/'
+ end
+ end
+
+ namespace :scanner do
+ Dir['./test/scanners/*'].each do |scanner|
+ next unless File.directory? scanner
+ lang = File.basename(scanner)
+ desc "run all scanner tests for #{lang}"
+ task lang => :update_scanner_suite do
+ ruby "./test/scanners/suite.rb #{lang}"
+ end
+ end
+ end
+
+ desc 'clean test output files'
+ task :clean do
+ for file in Dir['test/scanners/**/*.actual.*']
+ rm file
+ end
+ for file in Dir['test/scanners/**/*.debug.diff']
+ rm file
+ end
+ for file in Dir['test/scanners/**/*.debug.diff.html']
+ rm file
+ end
+ for file in Dir['test/scanners/**/*.expected.html']
+ rm file
+ end
+ end
+
+ desc 'test the CodeRay executable'
+ task :exe do
+ if RUBY_VERSION >= '1.8.7'
+ ruby './test/executable/suite.rb'
+ else
+ puts
+ puts "Can't run executable tests because shoulda-context requires Ruby 1.8.7+."
+ puts "Skipping."
+ end
+ end
+end
+
+task :test => %w(test:functional test:units test:exe)
diff --git a/test/executable/source.py b/test/executable/source.py
new file mode 100644
index 0000000..1bb2c00
--- /dev/null
+++ b/test/executable/source.py
@@ -0,0 +1 @@
+class ClassName(): pass
\ No newline at end of file
diff --git a/test/executable/source.rb b/test/executable/source.rb
new file mode 100644
index 0000000..226f15f
--- /dev/null
+++ b/test/executable/source.rb
@@ -0,0 +1 @@
+class ClassName; end
\ No newline at end of file
diff --git a/test/executable/source_with_comments.rb b/test/executable/source_with_comments.rb
new file mode 100644
index 0000000..ec79358
--- /dev/null
+++ b/test/executable/source_with_comments.rb
@@ -0,0 +1,3 @@
+# a class
+class ClassName
+end
diff --git a/test/executable/suite.rb b/test/executable/suite.rb
new file mode 100644
index 0000000..997405c
--- /dev/null
+++ b/test/executable/suite.rb
@@ -0,0 +1,226 @@
+require 'test/unit'
+require 'rubygems' unless defined? Gem
+require 'shoulda-context'
+
+require 'pathname'
+require 'json'
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
+require 'coderay'
+
+puts "Running CodeRay #{CodeRay::VERSION} executable tests..."
+
+class TestCodeRayExecutable < Test::Unit::TestCase
+
+ ROOT_DIR = Pathname.new(File.dirname(__FILE__)) + '..' + '..'
+ EXECUTABLE = ROOT_DIR + 'bin' + 'coderay'
+ RUBY_COMMAND = 'ruby'
+ EXE_COMMAND =
+ if RUBY_PLATFORM === 'java' && `ruby --ng -e '' 2> /dev/null` && $?.success?
+ # use Nailgun
+ "#{RUBY_COMMAND}--ng -I%s %s"
+ else
+ "#{RUBY_COMMAND} -I%s %s"
+ end % [ROOT_DIR + 'lib', EXECUTABLE]
+
+ def coderay args, options = {}
+ if options[:fake_tty]
+ command = "#{EXE_COMMAND} #{args} --tty"
+ else
+ command = "#{EXE_COMMAND} #{args}"
+ end
+
+ puts command if $DEBUG
+
+ if options[:input]
+ output = IO.popen "#{command} 2>&1", "r+" do |io|
+ io.write options[:input]
+ io.close_write
+ io.read
+ end
+ else
+ output = `#{command} 2>&1`
+ end
+
+ if output[EXECUTABLE.to_s]
+ raise output
+ else
+ output
+ end
+ end
+
+ context 'a simple call with no arguments' do
+ should 'work' do
+ assert_nothing_raised { coderay('') }
+ end
+ should 'print version and help' do
+ assert_match(/CodeRay #{CodeRay::VERSION}/, coderay(''))
+ assert_match(/usage:/, coderay(''))
+ end
+ end
+
+ context 'version' do
+ should 'be printed with -v' do
+ assert_match(/\ACodeRay #{CodeRay::VERSION}\Z/, coderay('-v'))
+ end
+ should 'be printed with --version' do
+ assert_match(/\ACodeRay #{CodeRay::VERSION}\Z/, coderay('--version'))
+ end
+ end
+
+ context 'help' do
+ should 'be printed with -h' do
+ assert_match(/^usage:/, coderay('-h'))
+ end
+ should 'be printed with --help' do
+ assert_match(/^usage:/, coderay('--help'))
+ end
+ should 'be printed with subcommand help' do
+ assert_match(/^usage:/, coderay('help'))
+ end
+ end
+
+ context 'commands' do
+ should 'be printed with subcommand commands' do
+ assert_match(/^ +help/, coderay('commands'))
+ assert_match(/^ +version/, coderay('commands'))
+ end
+ end
+
+ context 'highlighting a file to the terminal' do
+ source_file = ROOT_DIR + 'test/executable/source.py'
+
+ source = File.read source_file
+
+ ansi_seq = /\e\[[0-9;]+m/
+
+ should 'not throw an error' do
+ assert_nothing_raised { coderay(source_file, :fake_tty => true) }
+ end
+ should 'output its contents to stdout' do
+ target = coderay(source_file, :fake_tty => true)
+ assert_equal source, target.chomp.gsub(ansi_seq, '')
+ end
+ should 'output ANSI-colored text' do
+ target = coderay(source_file, :fake_tty => true)
+ assert_not_equal source, target.chomp
+ assert_equal 6, target.scan(ansi_seq).size
+ end
+ end
+
+ context 'highlighting a file into a pipe (source.rb -html > source.rb.html)' do
+ source_file = ROOT_DIR + 'test/executable/source.rb'
+ target_file = "#{source_file}.html"
+ command = "#{source_file} -html > #{target_file}"
+
+ source = File.read source_file
+
+ pre = %r{<td class="code"><pre>(.*?)</pre>}m
+ tag = /<[^>]*>/
+
+ should 'not throw an error' do
+ assert_nothing_raised { coderay(command) }
+ end
+ should 'output its contents to the pipe' do
+ coderay(command)
+ target = File.read(target_file)
+ if target = target[pre, 1]
+ assert_equal source, target.gsub(tag, '').strip
+ else
+ flunk "target code has no <pre> tag: #{target}"
+ end
+ end
+ should 'output valid HTML' do
+ coderay(command)
+ target = File.read(target_file)
+ assert_not_equal source, target[pre, 1]
+ assert_equal 6, target[pre, 1].scan(tag).size
+ assert_match %r{\A<!DOCTYPE html>\n<html>\n<head>}, target
+ end
+ end
+
+ context 'highlighting a file into another file (source.rb source.rb.json)' do
+ source_file = ROOT_DIR + 'test/executable/source.rb'
+ target_file = "#{source_file}.json"
+ command = "#{source_file} #{target_file}"
+
+ source = File.read source_file
+
+ text = /"text":"([^"]*)"/
+
+ should 'not throw an error' do
+ assert_nothing_raised { coderay(command) }
+ end
+ should 'output its contents to the file' do
+ coderay(command)
+ target = File.read(target_file)
+ assert_equal source, target.scan(text).join
+ end
+ should 'output JSON' do
+ coderay(command)
+ target = File.read(target_file)
+ assert_not_equal source, target
+ assert_equal 6, target.scan(text).size
+ end
+ end
+
+ context 'highlighting a file without explicit input type (source.py)' do
+ source_file = ROOT_DIR + 'test/executable/source.py'
+ command = "#{source_file} -html"
+
+ source = File.read source_file
+
+ pre = %r{<td class="code"><pre>(.*?)</pre>}m
+ tag_class = /<span class="([^>"]*)"?[^>]*>/
+
+ should 'respect the file extension and highlight the input as Python' do
+ target = coderay(command)
+ assert_equal %w(keyword class keyword), target[pre, 1].scan(tag_class).flatten
+ end
+ end
+
+ context 'highlighting a file with explicit input type (-ruby source.py)' do
+ source_file = ROOT_DIR + 'test/executable/source.py'
+ command = "-ruby #{source_file} -html"
+
+ source = File.read source_file
+
+ pre = %r{<td class="code"><pre>(.*?)</pre>}m
+ tag_class = /<span class="([^>"]*)"?[^>]*>/
+
+ should 'ignore the file extension and highlight the input as Ruby' do
+ target = coderay(command)
+ assert_equal %w(keyword class), target[pre, 1].scan(tag_class).flatten
+ end
+ end
+
+ context 'highlighting a file with explicit input and output type (-ruby source.py -span)' do
+ source_file = ROOT_DIR + 'test/executable/source.py'
+ command = "-ruby #{source_file} -span"
+
+ source = File.read source_file
+
+ span_tags = /<\/?span[^>]*>/
+
+ should 'just respect the output type and include span tags' do
+ target = coderay(command)
+ assert_equal source, target.chomp.gsub(span_tags, '')
+ end
+ end
+
+ context 'the LOC counter' do
+ source_file = ROOT_DIR + 'test/executable/source_with_comments.rb'
+ command = "-ruby -loc"
+
+ should 'work' do
+ output = coderay(command, :input => <<-CODE)
+# test
+=begin
+=end
+test
+ CODE
+ assert_equal "1\n", output
+ end
+ end
+
+end
diff --git a/test/functional/basic.rb b/test/functional/basic.rb
new file mode 100644
index 0000000..752d4ba
--- /dev/null
+++ b/test/functional/basic.rb
@@ -0,0 +1,318 @@
+# encoding: utf-8
+require 'test/unit'
+require File.expand_path('../../lib/assert_warning', __FILE__)
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
+require 'coderay'
+
+class BasicTest < Test::Unit::TestCase
+
+ def test_version
+ assert_nothing_raised do
+ assert_match(/\A\d\.\d\.\d?\z/, CodeRay::VERSION)
+ end
+ end
+
+ def with_empty_load_path
+ old_load_path = $:.dup
+ $:.clear
+ yield
+ ensure
+ $:.replace old_load_path
+ end
+
+ def test_autoload
+ with_empty_load_path do
+ assert_nothing_raised do
+ CodeRay::Scanners::Java::BuiltinTypes
+ end
+ end
+ end
+
+ RUBY_TEST_CODE = 'puts "Hello, World!"'
+
+ RUBY_TEST_TOKENS = [
+ ['puts', :ident],
+ [' ', :space],
+ [:begin_group, :string],
+ ['"', :delimiter],
+ ['Hello, World!', :content],
+ ['"', :delimiter],
+ [:end_group, :string]
+ ].flatten
+ def test_simple_scan
+ assert_nothing_raised do
+ assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).tokens
+ end
+ end
+
+ RUBY_TEST_HTML = 'puts <span class="string"><span class="delimiter">"</span>' +
+ '<span class="content">Hello, World!</span><span class="delimiter">"</span></span>'
+ def test_simple_highlight
+ assert_nothing_raised do
+ assert_equal RUBY_TEST_HTML, CodeRay.scan(RUBY_TEST_CODE, :ruby).html
+ end
+ end
+
+ def test_scan_file
+ CodeRay.scan_file __FILE__
+ end
+
+ def test_encode
+ assert_equal 1, CodeRay.encode('test', :python, :count)
+ end
+
+ def test_encode_tokens
+ assert_equal 1, CodeRay.encode_tokens(CodeRay::Tokens['test', :string], :count)
+ end
+
+ def test_encode_file
+ assert_equal File.read(__FILE__), CodeRay.encode_file(__FILE__, :text)
+ end
+
+ def test_highlight
+ assert_match '<pre>test</pre>', CodeRay.highlight('test', :python)
+ end
+
+ def test_highlight_file
+ assert_match "require <span class=\"string\"><span class=\"delimiter\">'</span><span class=\"content\">test/unit</span><span class=\"delimiter\">'</span></span>\n", CodeRay.highlight_file(__FILE__)
+ end
+
+ def test_duo
+ assert_equal(RUBY_TEST_CODE,
+ CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE))
+ assert_equal(RUBY_TEST_CODE,
+ CodeRay::Duo[:plain => :text].highlight(RUBY_TEST_CODE))
+ end
+
+ def test_duo_stream
+ assert_equal(RUBY_TEST_CODE,
+ CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE, :stream => true))
+ end
+
+ def test_comment_filter
+ assert_equal <<-EXPECTED, CodeRay.scan(<<-INPUT, :ruby).comment_filter.text
+#!/usr/bin/env ruby
+
+code
+
+more code
+ EXPECTED
+#!/usr/bin/env ruby
+=begin
+A multi-line comment.
+=end
+code
+# A single-line comment.
+more code # and another comment, in-line.
+ INPUT
+ end
+
+ def test_lines_of_code
+ assert_equal 2, CodeRay.scan(<<-INPUT, :ruby).lines_of_code
+#!/usr/bin/env ruby
+=begin
+A multi-line comment.
+=end
+code
+# A single-line comment.
+more code # and another comment, in-line.
+ INPUT
+ rHTML = <<-RHTML
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <title><%= controller.controller_name.titleize %>: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<div id="main">
+ <%= yield %>
+</div>
+
+</body>
+</html>
+ RHTML
+ assert_equal 0, CodeRay.scan(rHTML, :html).lines_of_code
+ assert_equal 0, CodeRay.scan(rHTML, :php).lines_of_code
+ assert_equal 0, CodeRay.scan(rHTML, :yaml).lines_of_code
+ assert_equal 4, CodeRay.scan(rHTML, :erb).lines_of_code
+ end
+
+ def test_list_of_encoders
+ assert_kind_of(Array, CodeRay::Encoders.list)
+ assert CodeRay::Encoders.list.include?(:count)
+ end
+
+ def test_list_of_scanners
+ assert_kind_of(Array, CodeRay::Scanners.list)
+ assert CodeRay::Scanners.list.include?(:text)
+ end
+
+ def test_token_kinds
+ assert_kind_of Hash, CodeRay::TokenKinds
+ for kind, css_class in CodeRay::TokenKinds
+ assert_kind_of Symbol, kind
+ if css_class != false
+ assert_kind_of String, css_class, "TokenKinds[%p] == %p" % [kind, css_class]
+ end
+ end
+ assert_equal 'reserved', CodeRay::TokenKinds[:reserved]
+ assert_equal false, CodeRay::TokenKinds[:shibboleet]
+ end
+
+ class Milk < CodeRay::Encoders::Encoder
+ FILE_EXTENSION = 'cocoa'
+ end
+
+ class HoneyBee < CodeRay::Encoders::Encoder
+ end
+
+ def test_encoder_file_extension
+ assert_nothing_raised do
+ assert_equal 'html', CodeRay::Encoders::Page::FILE_EXTENSION
+ assert_equal 'cocoa', Milk::FILE_EXTENSION
+ assert_equal 'cocoa', Milk.new.file_extension
+ assert_equal 'honeybee', HoneyBee::FILE_EXTENSION
+ assert_equal 'honeybee', HoneyBee.new.file_extension
+ end
+ assert_raise NameError do
+ HoneyBee::MISSING_CONSTANT
+ end
+ end
+
+ def test_encoder_tokens
+ encoder = CodeRay::Encoders::Encoder.new
+ encoder.send :setup, {}
+ assert_raise(ArgumentError) { encoder.token :strange, '' }
+ encoder.token 'test', :debug
+ end
+
+ def test_encoder_deprecated_interface
+ encoder = CodeRay::Encoders::Encoder.new
+ encoder.send :setup, {}
+ assert_warning 'Using old Tokens#<< interface.' do
+ encoder << ['test', :content]
+ end
+ assert_raise ArgumentError do
+ encoder << [:strange, :input]
+ end
+ assert_raise ArgumentError do
+ encoder.encode_tokens [['test', :token]]
+ end
+ end
+
+ def encoder_token_interface_deprecation_warning_given
+ CodeRay::Encoders::Encoder.send :class_variable_get, :@@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN
+ end
+
+ def test_scanner_file_extension
+ assert_equal 'rb', CodeRay::Scanners::Ruby.file_extension
+ assert_equal 'rb', CodeRay::Scanners::Ruby.new.file_extension
+ assert_equal 'java', CodeRay::Scanners::Java.file_extension
+ assert_equal 'java', CodeRay::Scanners::Java.new.file_extension
+ end
+
+ def test_scanner_lang
+ assert_equal :ruby, CodeRay::Scanners::Ruby.lang
+ assert_equal :ruby, CodeRay::Scanners::Ruby.new.lang
+ assert_equal :java, CodeRay::Scanners::Java.lang
+ assert_equal :java, CodeRay::Scanners::Java.new.lang
+ end
+
+ def test_scanner_tokenize
+ assert_equal ['foo', :plain], CodeRay::Scanners::Plain.new.tokenize('foo')
+ assert_equal [['foo', :plain], ['bar', :plain]], CodeRay::Scanners::Plain.new.tokenize(['foo', 'bar'])
+ CodeRay::Scanners::Plain.new.tokenize 42
+ end
+
+ def test_scanner_tokens
+ scanner = CodeRay::Scanners::Plain.new
+ scanner.tokenize('foo')
+ assert_equal ['foo', :plain], scanner.tokens
+ scanner.string = ''
+ assert_equal ['', :plain], scanner.tokens
+ end
+
+ def test_scanner_line_and_column
+ scanner = CodeRay::Scanners::Plain.new "foo\nbär+quux"
+ assert_equal 0, scanner.pos
+ assert_equal 1, scanner.line
+ assert_equal 1, scanner.column
+ scanner.scan(/foo/)
+ assert_equal 3, scanner.pos
+ assert_equal 1, scanner.line
+ assert_equal 4, scanner.column
+ scanner.scan(/\n/)
+ assert_equal 4, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 1, scanner.column
+ scanner.scan(/b/)
+ assert_equal 5, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 2, scanner.column
+ scanner.scan(/a/)
+ assert_equal 5, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 2, scanner.column
+ scanner.scan(/ä/)
+ assert_equal 7, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 4, scanner.column
+ scanner.scan(/r/)
+ assert_equal 8, scanner.pos
+ assert_equal 2, scanner.line
+ assert_equal 5, scanner.column
+ end
+
+ def test_scanner_use_subclasses
+ assert_raise NotImplementedError do
+ CodeRay::Scanners::Scanner.new
+ end
+ end
+
+ class InvalidScanner < CodeRay::Scanners::Scanner
+ end
+
+ def test_scanner_scan_tokens
+ assert_raise NotImplementedError do
+ InvalidScanner.new.tokenize ''
+ end
+ end
+
+ class RaisingScanner < CodeRay::Scanners::Scanner
+ def scan_tokens encoder, options
+ raise_inspect 'message', [], :initial
+ end
+ end
+
+ def test_scanner_raise_inspect
+ assert_raise CodeRay::Scanners::Scanner::ScanError do
+ RaisingScanner.new.tokenize ''
+ end
+ end
+
+ def test_scan_a_frozen_string
+ assert_nothing_raised do
+ CodeRay.scan RUBY_VERSION, :ruby
+ CodeRay.scan RUBY_VERSION, :plain
+ end
+ end
+
+ def test_scan_a_non_string
+ assert_nothing_raised do
+ CodeRay.scan 42, :ruby
+ CodeRay.scan nil, :ruby
+ CodeRay.scan self, :ruby
+ CodeRay.encode ENV.to_hash, :ruby, :page
+ CodeRay.highlight CodeRay, :plain
+ end
+ end
+
+end
diff --git a/test/functional/examples.rb b/test/functional/examples.rb
new file mode 100644
index 0000000..985ef87
--- /dev/null
+++ b/test/functional/examples.rb
@@ -0,0 +1,129 @@
+require 'test/unit'
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
+require 'coderay'
+
+class ExamplesTest < Test::Unit::TestCase
+
+ def test_examples
+ # output as HTML div (using inline CSS styles)
+ div = CodeRay.scan('puts "Hello, world!"', :ruby).div
+ assert_equal <<-DIV, div
+<div class="CodeRay">
+ <div class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, world!</span><span style="color:#710">"</span></span></pre></div>
+</div>
+ DIV
+
+ # ...with line numbers
+ div = CodeRay.scan(<<-CODE.chomp, :ruby).div(:line_numbers => :table)
+5.times do
+ puts 'Hello, world!'
+end
+ CODE
+ assert_equal <<-DIV, div
+<table class="CodeRay"><tr>
+ <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
+<a href="#n2" name="n2">2</a>
+<a href="#n3" name="n3">3</a>
+</pre></td>
+ <td class="code"><pre><span style="color:#00D">5</span>.times <span style="color:#080;font-weight:bold">do</span>
+ puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello, world!</span><span style="color:#710">'</span></span>
+<span style="color:#080;font-weight:bold">end</span></pre></td>
+</tr></table>
+ DIV
+
+ # output as standalone HTML page (using CSS classes)
+ page = CodeRay.scan('puts "Hello, world!"', :ruby).page
+ assert_match <<-PAGE, page
+<body>
+
+<table class="CodeRay"><tr>
+ <td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
+</pre></td>
+ <td class="code"><pre>puts <span class="string"><span class="delimiter">"</span><span class="content">Hello, world!</span><span class="delimiter">"</span></span></pre></td>
+</tr></table>
+
+</body>
+ PAGE
+
+ # keep scanned tokens for later use
+ tokens = CodeRay.scan('{ "just": "an", "example": 42 }', :json)
+ assert_kind_of CodeRay::TokensProxy, tokens
+
+ assert_equal ["{", :operator, " ", :space, :begin_group, :key,
+ "\"", :delimiter, "just", :content, "\"", :delimiter,
+ :end_group, :key, ":", :operator, " ", :space,
+ :begin_group, :string, "\"", :delimiter, "an", :content,
+ "\"", :delimiter, :end_group, :string, ",", :operator,
+ " ", :space, :begin_group, :key, "\"", :delimiter,
+ "example", :content, "\"", :delimiter, :end_group, :key,
+ ":", :operator, " ", :space, "42", :integer,
+ " ", :space, "}", :operator], tokens.tokens
+
+ # produce a token statistic
+ assert_equal <<-STATISTIC, tokens.statistic
+
+Code Statistics
+
+Tokens 26
+ Non-Whitespace 15
+Bytes Total 31
+
+Token Types (7):
+ type count ratio size (average)
+-------------------------------------------------------------
+ TOTAL 26 100.00 % 1.2
+ delimiter 6 23.08 % 1.0
+ operator 5 19.23 % 1.0
+ space 5 19.23 % 1.0
+ key 4 15.38 % 0.0
+ :begin_group 3 11.54 % 0.0
+ :end_group 3 11.54 % 0.0
+ content 3 11.54 % 4.3
+ string 2 7.69 % 0.0
+ integer 1 3.85 % 2.0
+
+ STATISTIC
+
+ # count the tokens
+ assert_equal 26, tokens.count
+
+ # produce a HTML div, but with CSS classes
+ div = tokens.div(:css => :class)
+ assert_equal <<-DIV, div
+<div class="CodeRay">
+ <div class="code"><pre>{ <span class="key"><span class="delimiter">"</span><span class="content">just</span><span class="delimiter">"</span></span>: <span class="string"><span class="delimiter">"</span><span class="content">an</span><span class="delimiter">"</span></span>, <span class="key"><span class="delimiter">"</span><span class="content">example</span><span class="delimiter">"</span></span>: <span class="integer">42</span> }</pre></div>
+</div>
+ DIV
+
+ # highlight a file (HTML div); guess the file type base on the extension
+ assert_equal :ruby, CodeRay::FileType[__FILE__]
+
+ # get a new scanner for Python
+ python_scanner = CodeRay.scanner :python
+ assert_kind_of CodeRay::Scanners::Python, python_scanner
+
+ # get a new encoder for terminal
+ terminal_encoder = CodeRay.encoder :term
+ assert_kind_of CodeRay::Encoders::Terminal, terminal_encoder
+
+ # scanning into tokens
+ tokens = python_scanner.tokenize 'import this; # The Zen of Python'
+ assert_equal ["import", :keyword, " ", :space, "this", :include,
+ ";", :operator, " ", :space, "# The Zen of Python", :comment], tokens
+
+ # format the tokens
+ term = terminal_encoder.encode_tokens(tokens)
+ assert_equal "\e[32mimport\e[0m \e[31mthis\e[0m; \e[1;30m# The Zen of Python\e[0m", term
+
+ # re-using scanner and encoder
+ ruby_highlighter = CodeRay::Duo[:ruby, :div]
+ div = ruby_highlighter.encode('puts "Hello, world!"')
+ assert_equal <<-DIV, div
+<div class="CodeRay">
+ <div class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, world!</span><span style="color:#710">"</span></span></pre></div>
+</div>
+ DIV
+ end
+
+end
diff --git a/test/functional/for_redcloth.rb b/test/functional/for_redcloth.rb
new file mode 100644
index 0000000..9fd244e
--- /dev/null
+++ b/test/functional/for_redcloth.rb
@@ -0,0 +1,78 @@
+require 'test/unit'
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
+require 'coderay'
+
+begin
+ require 'rubygems' unless defined? Gem
+ gem 'RedCloth', '>= 4.0.3' rescue nil
+ require 'redcloth'
+rescue LoadError
+ warn 'RedCloth not found - skipping for_redcloth tests.'
+ undef RedCloth if defined? RedCloth
+end
+
+class BasicTest < Test::Unit::TestCase
+
+ def test_for_redcloth
+ require 'coderay/for_redcloth'
+ assert_equal "<p><span lang=\"ruby\" class=\"CodeRay\">puts <span style=\"background-color:hsla(0,100%,50%,0.05)\"><span style=\"color:#710\">"</span><span style=\"color:#D20\">Hello, World!</span><span style=\"color:#710\">"</span></span></span></p>",
+ RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
+ assert_equal <<-BLOCKCODE.chomp,
+<div lang="ruby" class="CodeRay">
+ <div class="code"><pre>puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">Hello, World!</span><span style="color:#710">"</span></span></pre></div>
+</div>
+ BLOCKCODE
+ RedCloth.new('bc[ruby]. puts "Hello, World!"').to_html
+ end
+
+ def test_for_redcloth_no_lang
+ require 'coderay/for_redcloth'
+ assert_equal "<p><code>puts \"Hello, World!\"</code></p>",
+ RedCloth.new('@puts "Hello, World!"@').to_html
+ assert_equal <<-BLOCKCODE.chomp,
+<pre><code>puts \"Hello, World!\"</code></pre>
+ BLOCKCODE
+ RedCloth.new('bc. puts "Hello, World!"').to_html
+ end
+
+ def test_for_redcloth_style
+ require 'coderay/for_redcloth'
+ assert_equal <<-BLOCKCODE.chomp,
+<pre style=\"color: red;\"><code style=\"color: red;\">puts \"Hello, World!\"</code></pre>
+ BLOCKCODE
+ RedCloth.new('bc{color: red}. puts "Hello, World!"').to_html
+ end
+
+ def test_for_redcloth_escapes
+ require 'coderay/for_redcloth'
+ assert_equal '<p><span lang="ruby" class="CodeRay">></span></p>',
+ RedCloth.new('@[ruby]>@').to_html
+ assert_equal <<-BLOCKCODE.chomp,
+<div lang="ruby" class="CodeRay">
+ <div class="code"><pre>&</pre></div>
+</div>
+ BLOCKCODE
+ RedCloth.new('bc[ruby]. &').to_html
+ end
+
+ def test_for_redcloth_escapes2
+ require 'coderay/for_redcloth'
+ assert_equal "<p><span lang=\"c\" class=\"CodeRay\"><span style=\"color:#579\">#include</span> <span style=\"color:#B44;font-weight:bold\"><test.h></span></span></p>",
+ RedCloth.new('@[c]#include <test.h>@').to_html
+ end
+
+ # See http://jgarber.lighthouseapp.com/projects/13054/tickets/124-code-markup-does-not-allow-brackets.
+ def test_for_redcloth_false_positive
+ require 'coderay/for_redcloth'
+ assert_equal '<p><code>[project]_dff.skjd</code></p>',
+ RedCloth.new('@[project]_dff.skjd@').to_html
+ # false positive, but expected behavior / known issue
+ assert_equal "<p><span lang=\"ruby\" class=\"CodeRay\">_dff.skjd</span></p>",
+ RedCloth.new('@[ruby]_dff.skjd@').to_html
+ assert_equal <<-BLOCKCODE.chomp, RedCloth.new('bc. [project]_dff.skjd').to_html
+<pre><code>[project]_dff.skjd</code></pre>
+ BLOCKCODE
+ end
+
+end if defined? RedCloth
\ No newline at end of file
diff --git a/test/functional/suite.rb b/test/functional/suite.rb
new file mode 100644
index 0000000..ec23eec
--- /dev/null
+++ b/test/functional/suite.rb
@@ -0,0 +1,15 @@
+require 'test/unit'
+
+$VERBOSE = $CODERAY_DEBUG = true
+$:.unshift File.expand_path('../../../lib', __FILE__)
+require 'coderay'
+
+mydir = File.dirname(__FILE__)
+suite = Dir[File.join(mydir, '*.rb')].
+ map { |tc| File.basename(tc).sub(/\.rb$/, '') } - %w'suite for_redcloth'
+
+puts "Running basic CodeRay #{CodeRay::VERSION} tests: #{suite.join(', ')}"
+
+for test_case in suite
+ load File.join(mydir, test_case + '.rb')
+end
diff --git a/test/lib/README b/test/lib/README
new file mode 100644
index 0000000..7c41648
--- /dev/null
+++ b/test/lib/README
@@ -0,0 +1,2 @@
+Contents:
+- test/unit: We need the old Test::Unit for the scanner test suite to work with Ruby 1.9.
diff --git a/test/lib/assert_warning.rb b/test/lib/assert_warning.rb
new file mode 100644
index 0000000..828b464
--- /dev/null
+++ b/test/lib/assert_warning.rb
@@ -0,0 +1,15 @@
+class Test::Unit::TestCase
+
+ def assert_warning expected_warning
+ require 'stringio'
+ oldstderr = $stderr
+ $stderr = StringIO.new
+ yield
+ $stderr.rewind
+ given_warning = $stderr.read.chomp
+ assert_equal expected_warning, given_warning
+ ensure
+ $stderr = oldstderr
+ end
+
+end
diff --git a/test/lib/test/unit.rb b/test/lib/test/unit.rb
new file mode 100644
index 0000000..b71f644
--- /dev/null
+++ b/test/lib/test/unit.rb
@@ -0,0 +1,280 @@
+require 'test/unit/testcase'
+require 'test/unit/autorunner'
+
+module Test # :nodoc:
+ #
+ # = Test::Unit - Ruby Unit Testing Framework
+ #
+ # == Introduction
+ #
+ # Unit testing is making waves all over the place, largely due to the
+ # fact that it is a core practice of XP. While XP is great, unit testing
+ # has been around for a long time and has always been a good idea. One
+ # of the keys to good unit testing, though, is not just writing tests,
+ # but having tests. What's the difference? Well, if you just _write_ a
+ # test and throw it away, you have no guarantee that something won't
+ # change later which breaks your code. If, on the other hand, you _have_
+ # tests (obviously you have to write them first), and run them as often
+ # as possible, you slowly build up a wall of things that cannot break
+ # without you immediately knowing about it. This is when unit testing
+ # hits its peak usefulness.
+ #
+ # Enter Test::Unit, a framework for unit testing in Ruby, helping you to
+ # design, debug and evaluate your code by making it easy to write and
+ # have tests for it.
+ #
+ #
+ # == Notes
+ #
+ # Test::Unit has grown out of and superceded Lapidary.
+ #
+ #
+ # == Feedback
+ #
+ # I like (and do my best to practice) XP, so I value early releases,
+ # user feedback, and clean, simple, expressive code. There is always
+ # room for improvement in everything I do, and Test::Unit is no
+ # exception. Please, let me know what you think of Test::Unit as it
+ # stands, and what you'd like to see expanded/changed/improved/etc. If
+ # you find a bug, let me know ASAP; one good way to let me know what the
+ # bug is is to submit a new test that catches it :-) Also, I'd love to
+ # hear about any successes you have with Test::Unit, and any
+ # documentation you might add will be greatly appreciated. My contact
+ # info is below.
+ #
+ #
+ # == Contact Information
+ #
+ # A lot of discussion happens about Ruby in general on the ruby-talk
+ # mailing list (http://www.ruby-lang.org/en/ml.html), and you can ask
+ # any questions you might have there. I monitor the list, as do many
+ # other helpful Rubyists, and you're sure to get a quick answer. Of
+ # course, you're also welcome to email me (Nathaniel Talbott) directly
+ # at mailto:testunit@talbott.ws, and I'll do my best to help you out.
+ #
+ #
+ # == Credits
+ #
+ # I'd like to thank...
+ #
+ # Matz, for a great language!
+ #
+ # Masaki Suketa, for his work on RubyUnit, which filled a vital need in
+ # the Ruby world for a very long time. I'm also grateful for his help in
+ # polishing Test::Unit and getting the RubyUnit compatibility layer
+ # right. His graciousness in allowing Test::Unit to supercede RubyUnit
+ # continues to be a challenge to me to be more willing to defer my own
+ # rights.
+ #
+ # Ken McKinlay, for his interest and work on unit testing, and for his
+ # willingness to dialog about it. He was also a great help in pointing
+ # out some of the holes in the RubyUnit compatibility layer.
+ #
+ # Dave Thomas, for the original idea that led to the extremely simple
+ # "require 'test/unit'", plus his code to improve it even more by
+ # allowing the selection of tests from the command-line. Also, without
+ # RDoc, the documentation for Test::Unit would stink a lot more than it
+ # does now.
+ #
+ # Everyone who's helped out with bug reports, feature ideas,
+ # encouragement to continue, etc. It's a real privilege to be a part of
+ # the Ruby community.
+ #
+ # The guys at RoleModel Software, for putting up with me repeating, "But
+ # this would be so much easier in Ruby!" whenever we're coding in Java.
+ #
+ # My Creator, for giving me life, and giving it more abundantly.
+ #
+ #
+ # == License
+ #
+ # Test::Unit is copyright (c) 2000-2003 Nathaniel Talbott. It is free
+ # software, and is distributed under the Ruby license. See the COPYING
+ # file in the standard Ruby distribution for details.
+ #
+ #
+ # == Warranty
+ #
+ # This software is provided "as is" and without any express or
+ # implied warranties, including, without limitation, the implied
+ # warranties of merchantibility and fitness for a particular
+ # purpose.
+ #
+ #
+ # == Author
+ #
+ # Nathaniel Talbott.
+ # Copyright (c) 2000-2003, Nathaniel Talbott
+ #
+ # ----
+ #
+ # = Usage
+ #
+ # The general idea behind unit testing is that you write a _test_
+ # _method_ that makes certain _assertions_ about your code, working
+ # against a _test_ _fixture_. A bunch of these _test_ _methods_ are
+ # bundled up into a _test_ _suite_ and can be run any time the
+ # developer wants. The results of a run are gathered in a _test_
+ # _result_ and displayed to the user through some UI. So, lets break
+ # this down and see how Test::Unit provides each of these necessary
+ # pieces.
+ #
+ #
+ # == Assertions
+ #
+ # These are the heart of the framework. Think of an assertion as a
+ # statement of expected outcome, i.e. "I assert that x should be equal
+ # to y". If, when the assertion is executed, it turns out to be
+ # correct, nothing happens, and life is good. If, on the other hand,
+ # your assertion turns out to be false, an error is propagated with
+ # pertinent information so that you can go back and make your
+ # assertion succeed, and, once again, life is good. For an explanation
+ # of the current assertions, see Test::Unit::Assertions.
+ #
+ #
+ # == Test Method & Test Fixture
+ #
+ # Obviously, these assertions have to be called within a context that
+ # knows about them and can do something meaningful with their
+ # pass/fail value. Also, it's handy to collect a bunch of related
+ # tests, each test represented by a method, into a common test class
+ # that knows how to run them. The tests will be in a separate class
+ # from the code they're testing for a couple of reasons. First of all,
+ # it allows your code to stay uncluttered with test code, making it
+ # easier to maintain. Second, it allows the tests to be stripped out
+ # for deployment, since they're really there for you, the developer,
+ # and your users don't need them. Third, and most importantly, it
+ # allows you to set up a common test fixture for your tests to run
+ # against.
+ #
+ # What's a test fixture? Well, tests do not live in a vacuum; rather,
+ # they're run against the code they are testing. Often, a collection
+ # of tests will run against a common set of data, also called a
+ # fixture. If they're all bundled into the same test class, they can
+ # all share the setting up and tearing down of that data, eliminating
+ # unnecessary duplication and making it much easier to add related
+ # tests.
+ #
+ # Test::Unit::TestCase wraps up a collection of test methods together
+ # and allows you to easily set up and tear down the same test fixture
+ # for each test. This is done by overriding #setup and/or #teardown,
+ # which will be called before and after each test method that is
+ # run. The TestCase also knows how to collect the results of your
+ # assertions into a Test::Unit::TestResult, which can then be reported
+ # back to you... but I'm getting ahead of myself. To write a test,
+ # follow these steps:
+ #
+ # * Make sure Test::Unit is in your library path.
+ # * require 'test/unit' in your test script.
+ # * Create a class that subclasses Test::Unit::TestCase.
+ # * Add a method that begins with "test" to your class.
+ # * Make assertions in your test method.
+ # * Optionally define #setup and/or #teardown to set up and/or tear
+ # down your common test fixture.
+ # * You can now run your test as you would any other Ruby
+ # script... try it and see!
+ #
+ # A really simple test might look like this (#setup and #teardown are
+ # commented out to indicate that they are completely optional):
+ #
+ # require 'test/unit'
+ #
+ # class TC_MyTest < Test::Unit::TestCase
+ # # def setup
+ # # end
+ #
+ # # def teardown
+ # # end
+ #
+ # def test_fail
+ # assert(false, 'Assertion was false.')
+ # end
+ # end
+ #
+ #
+ # == Test Runners
+ #
+ # So, now you have this great test class, but you still need a way to
+ # run it and view any failures that occur during the run. This is
+ # where Test::Unit::UI::Console::TestRunner (and others, such as
+ # Test::Unit::UI::GTK::TestRunner) comes into play. The console test
+ # runner is automatically invoked for you if you require 'test/unit'
+ # and simply run the file. To use another runner, or to manually
+ # invoke a runner, simply call its run class method and pass in an
+ # object that responds to the suite message with a
+ # Test::Unit::TestSuite. This can be as simple as passing in your
+ # TestCase class (which has a class suite method). It might look
+ # something like this:
+ #
+ # require 'test/unit/ui/console/testrunner'
+ # Test::Unit::UI::Console::TestRunner.run(TC_MyTest)
+ #
+ #
+ # == Test Suite
+ #
+ # As more and more unit tests accumulate for a given project, it
+ # becomes a real drag running them one at a time, and it also
+ # introduces the potential to overlook a failing test because you
+ # forget to run it. Suddenly it becomes very handy that the
+ # TestRunners can take any object that returns a Test::Unit::TestSuite
+ # in response to a suite method. The TestSuite can, in turn, contain
+ # other TestSuites or individual tests (typically created by a
+ # TestCase). In other words, you can easily wrap up a group of
+ # TestCases and TestSuites like this:
+ #
+ # require 'test/unit/testsuite'
+ # require 'tc_myfirsttests'
+ # require 'tc_moretestsbyme'
+ # require 'ts_anothersetoftests'
+ #
+ # class TS_MyTests
+ # def self.suite
+ # suite = Test::Unit::TestSuite.new
+ # suite << TC_MyFirstTests.suite
+ # suite << TC_MoreTestsByMe.suite
+ # suite << TS_AnotherSetOfTests.suite
+ # return suite
+ # end
+ # end
+ # Test::Unit::UI::Console::TestRunner.run(TS_MyTests)
+ #
+ # Now, this is a bit cumbersome, so Test::Unit does a little bit more
+ # for you, by wrapping these up automatically when you require
+ # 'test/unit'. What does this mean? It means you could write the above
+ # test case like this instead:
+ #
+ # require 'test/unit'
+ # require 'tc_myfirsttests'
+ # require 'tc_moretestsbyme'
+ # require 'ts_anothersetoftests'
+ #
+ # Test::Unit is smart enough to find all the test cases existing in
+ # the ObjectSpace and wrap them up into a suite for you. It then runs
+ # the dynamic suite using the console TestRunner.
+ #
+ #
+ # == Questions?
+ #
+ # I'd really like to get feedback from all levels of Ruby
+ # practitioners about typos, grammatical errors, unclear statements,
+ # missing points, etc., in this document (or any other).
+ #
+
+ module Unit
+ # If set to false Test::Unit will not automatically run at exit.
+ def self.run=(flag)
+ @run = flag
+ end
+
+ # Automatically run tests at exit?
+ def self.run?
+ @run ||= false
+ end
+ end
+end
+
+at_exit do
+ unless $! || Test::Unit.run?
+ exit Test::Unit::AutoRunner.run
+ end
+end
diff --git a/test/lib/test/unit/assertionfailederror.rb b/test/lib/test/unit/assertionfailederror.rb
new file mode 100644
index 0000000..a21e4b5
--- /dev/null
+++ b/test/lib/test/unit/assertionfailederror.rb
@@ -0,0 +1,14 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+module Test
+ module Unit
+
+ # Thrown by Test::Unit::Assertions when an assertion fails.
+ class AssertionFailedError < StandardError
+ end
+ end
+end
diff --git a/test/lib/test/unit/assertions.rb b/test/lib/test/unit/assertions.rb
new file mode 100644
index 0000000..aa97799
--- /dev/null
+++ b/test/lib/test/unit/assertions.rb
@@ -0,0 +1,622 @@
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+require 'test/unit/assertionfailederror'
+require 'test/unit/util/backtracefilter'
+
+module Test
+ module Unit
+
+ ##
+ # Test::Unit::Assertions contains the standard Test::Unit assertions.
+ # Assertions is included in Test::Unit::TestCase.
+ #
+ # To include it in your own code and use its functionality, you simply
+ # need to rescue Test::Unit::AssertionFailedError. Additionally you may
+ # override add_assertion to get notified whenever an assertion is made.
+ #
+ # Notes:
+ # * The message to each assertion, if given, will be propagated with the
+ # failure.
+ # * It is easy to add your own assertions based on assert_block().
+ #
+ # = Example Custom Assertion
+ #
+ # def deny(boolean, message = nil)
+ # message = build_message message, '<?> is not false or nil.', boolean
+ # assert_block message do
+ # not boolean
+ # end
+ # end
+
+ module Assertions
+
+ ##
+ # The assertion upon which all other assertions are based. Passes if the
+ # block yields true.
+ #
+ # Example:
+ # assert_block "Couldn't do the thing" do
+ # do_the_thing
+ # end
+
+ public
+ def assert_block(message="assert_block failed.") # :yields:
+ _wrap_assertion do
+ if (! yield)
+ raise AssertionFailedError.new(message.to_s)
+ end
+ end
+ end
+
+ ##
+ # Asserts that +boolean+ is not false or nil.
+ #
+ # Example:
+ # assert [1, 2].include?(5)
+
+ public
+ def assert(boolean, message=nil)
+ _wrap_assertion do
+ assert_block("assert should not be called with a block.") { !block_given? }
+ assert_block(build_message(message, "<?> is not true.", boolean)) { boolean }
+ end
+ end
+
+ ##
+ # Passes if +expected+ == +actual.
+ #
+ # Note that the ordering of arguments is important, since a helpful
+ # error message is generated when this one fails that tells you the
+ # values of expected and actual.
+ #
+ # Example:
+ # assert_equal 'MY STRING', 'my string'.upcase
+
+ public
+ def assert_equal(expected, actual, message=nil)
+ full_message = build_message(message, <<EOT, expected, actual)
+<?> expected but was
+<?>.
+EOT
+ assert_block(full_message) { expected == actual }
+ end
+
+ private
+ def _check_exception_class(args) # :nodoc:
+ args.partition do |klass|
+ next if klass.instance_of?(Module)
+ assert(Exception >= klass, "Should expect a class of exception, #{klass}")
+ true
+ end
+ end
+
+ private
+ def _expected_exception?(actual_exception, exceptions, modules) # :nodoc:
+ exceptions.include?(actual_exception.class) or
+ modules.any? {|mod| actual_exception.is_a?(mod)}
+ end
+
+ ##
+ # Passes if the block raises one of the given exceptions.
+ #
+ # Example:
+ # assert_raise RuntimeError, LoadError do
+ # raise 'Boom!!!'
+ # end
+
+ public
+ def assert_raise(*args)
+ _wrap_assertion do
+ if Module === args.last
+ message = ""
+ else
+ message = args.pop
+ end
+ exceptions, modules = _check_exception_class(args)
+ expected = args.size == 1 ? args.first : args
+ actual_exception = nil
+ full_message = build_message(message, "<?> exception expected but none was thrown.", expected)
+ assert_block(full_message) do
+ begin
+ yield
+ rescue Exception => actual_exception
+ break
+ end
+ false
+ end
+ full_message = build_message(message, "<?> exception expected but was\n?", expected, actual_exception)
+ assert_block(full_message) {_expected_exception?(actual_exception, exceptions, modules)}
+ actual_exception
+ end
+ end
+
+ ##
+ # Alias of assert_raise.
+ #
+ # Will be deprecated in 1.9, and removed in 2.0.
+
+ public
+ def assert_raises(*args, &block)
+ assert_raise(*args, &block)
+ end
+
+ ##
+ # Passes if +object+ .instance_of? +klass+
+ #
+ # Example:
+ # assert_instance_of String, 'foo'
+
+ public
+ def assert_instance_of(klass, object, message="")
+ _wrap_assertion do
+ assert_equal(Class, klass.class, "assert_instance_of takes a Class as its first argument")
+ full_message = build_message(message, <<EOT, object, klass, object.class)
+<?> expected to be an instance of
+<?> but was
+<?>.
+EOT
+ assert_block(full_message){object.instance_of?(klass)}
+ end
+ end
+
+ ##
+ # Passes if +object+ is nil.
+ #
+ # Example:
+ # assert_nil [1, 2].uniq!
+
+ public
+ def assert_nil(object, message="")
+ assert_equal(nil, object, message)
+ end
+
+ ##
+ # Passes if +object+ .kind_of? +klass+
+ #
+ # Example:
+ # assert_kind_of Object, 'foo'
+
+ public
+ def assert_kind_of(klass, object, message="")
+ _wrap_assertion do
+ assert(klass.kind_of?(Module), "The first parameter to assert_kind_of should be a kind_of Module.")
+ full_message = build_message(message, "<?>\nexpected to be kind_of\\?\n<?> but was\n<?>.", object, klass, object.class)
+ assert_block(full_message){object.kind_of?(klass)}
+ end
+ end
+
+ ##
+ # Passes if +object+ .respond_to? +method+
+ #
+ # Example:
+ # assert_respond_to 'bugbear', :slice
+
+ public
+ def assert_respond_to(object, method, message="")
+ _wrap_assertion do
+ full_message = build_message(nil, "<?>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to\\?(:to_str).", method)
+
+ assert_block(full_message) do
+ method.kind_of?(Symbol) || method.respond_to?(:to_str)
+ end
+ full_message = build_message(message, <<EOT, object, object.class, method)
+<?>
+of type <?>
+expected to respond_to\\?<?>.
+EOT
+ assert_block(full_message) { object.respond_to?(method) }
+ end
+ end
+
+ ##
+ # Passes if +string+ =~ +pattern+.
+ #
+ # Example:
+ # assert_match(/\d+/, 'five, 6, seven')
+
+ public
+ def assert_match(pattern, string, message="")
+ _wrap_assertion do
+ pattern = case(pattern)
+ when String
+ Regexp.new(Regexp.escape(pattern))
+ else
+ pattern
+ end
+ full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
+ assert_block(full_message) { string =~ pattern }
+ end
+ end
+
+ ##
+ # Passes if +actual+ .equal? +expected+ (i.e. they are the same
+ # instance).
+ #
+ # Example:
+ # o = Object.new
+ # assert_same o, o
+
+ public
+ def assert_same(expected, actual, message="")
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
+<?>
+with id <?> expected to be equal\\? to
+<?>
+with id <?>.
+EOT
+ assert_block(full_message) { actual.equal?(expected) }
+ end
+
+ ##
+ # Compares the +object1+ with +object2+ using +operator+.
+ #
+ # Passes if object1.__send__(operator, object2) is true.
+ #
+ # Example:
+ # assert_operator 5, :>=, 4
+
+ public
+ def assert_operator(object1, operator, object2, message="")
+ _wrap_assertion do
+ full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
+ assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
+ full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
+<?> expected to be
+?
+<?>.
+EOT
+ assert_block(full_message) { object1.__send__(operator, object2) }
+ end
+ end
+
+ ##
+ # Passes if block does not raise an exception.
+ #
+ # Example:
+ # assert_nothing_raised do
+ # [1, 2].uniq
+ # end
+
+ public
+ def assert_nothing_raised(*args)
+ _wrap_assertion do
+ if Module === args.last
+ message = ""
+ else
+ message = args.pop
+ end
+ exceptions, modules = _check_exception_class(args)
+ begin
+ yield
+ rescue Exception => e
+ if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
+ _expected_exception?(e, exceptions, modules))
+ assert_block(build_message(message, "Exception raised:\n?", e)){false}
+ else
+ raise
+ end
+ end
+ nil
+ end
+ end
+
+ ##
+ # Flunk always fails.
+ #
+ # Example:
+ # flunk 'Not done testing yet.'
+
+ public
+ def flunk(message="Flunked")
+ assert_block(build_message(message)){false}
+ end
+
+ ##
+ # Passes if ! +actual+ .equal? +expected+
+ #
+ # Example:
+ # assert_not_same Object.new, Object.new
+
+ public
+ def assert_not_same(expected, actual, message="")
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
+<?>
+with id <?> expected to not be equal\\? to
+<?>
+with id <?>.
+EOT
+ assert_block(full_message) { !actual.equal?(expected) }
+ end
+
+ ##
+ # Passes if +expected+ != +actual+
+ #
+ # Example:
+ # assert_not_equal 'some string', 5
+
+ public
+ def assert_not_equal(expected, actual, message="")
+ full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
+ assert_block(full_message) { expected != actual }
+ end
+
+ ##
+ # Passes if ! +object+ .nil?
+ #
+ # Example:
+ # assert_not_nil '1 two 3'.sub!(/two/, '2')
+
+ public
+ def assert_not_nil(object, message="")
+ full_message = build_message(message, "<?> expected to not be nil.", object)
+ assert_block(full_message){!object.nil?}
+ end
+
+ ##
+ # Passes if +regexp+ !~ +string+
+ #
+ # Example:
+ # assert_no_match(/two/, 'one 2 three')
+
+ public
+ def assert_no_match(regexp, string, message="")
+ _wrap_assertion do
+ assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
+ full_message = build_message(message, "<?> expected to not match\n<?>.", regexp, string)
+ assert_block(full_message) { regexp !~ string }
+ end
+ end
+
+ UncaughtThrow = {NameError => /^uncaught throw \`(.+)\'$/,
+ ThreadError => /^uncaught throw \`(.+)\' in thread /} #`
+
+ ##
+ # Passes if the block throws +expected_symbol+
+ #
+ # Example:
+ # assert_throws :done do
+ # throw :done
+ # end
+
+ public
+ def assert_throws(expected_symbol, message="", &proc)
+ _wrap_assertion do
+ assert_instance_of(Symbol, expected_symbol, "assert_throws expects the symbol that should be thrown for its first argument")
+ assert_block("Should have passed a block to assert_throws."){block_given?}
+ caught = true
+ begin
+ catch(expected_symbol) do
+ proc.call
+ caught = false
+ end
+ full_message = build_message(message, "<?> should have been thrown.", expected_symbol)
+ assert_block(full_message){caught}
+ rescue NameError, ThreadError => error
+ if UncaughtThrow[error.class] !~ error.message
+ raise error
+ end
+ full_message = build_message(message, "<?> expected to be thrown but\n<?> was thrown.", expected_symbol, $1.intern)
+ flunk(full_message)
+ end
+ end
+ end
+
+ ##
+ # Passes if block does not throw anything.
+ #
+ # Example:
+ # assert_nothing_thrown do
+ # [1, 2].uniq
+ # end
+
+ public
+ def assert_nothing_thrown(message="", &proc)
+ _wrap_assertion do
+ assert(block_given?, "Should have passed a block to assert_nothing_thrown")
+ begin
+ proc.call
+ rescue NameError, ThreadError => error
+ if UncaughtThrow[error.class] !~ error.message
+ raise error
+ end
+ full_message = build_message(message, "<?> was thrown when nothing was expected", $1.intern)
+ flunk(full_message)
+ end
+ assert(true, "Expected nothing to be thrown")
+ end
+ end
+
+ ##
+ # Passes if +expected_float+ and +actual_float+ are equal
+ # within +delta+ tolerance.
+ #
+ # Example:
+ # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
+
+ public
+ def assert_in_delta(expected_float, actual_float, delta, message="")
+ _wrap_assertion do
+ {expected_float => "first float", actual_float => "second float", delta => "delta"}.each do |float, name|
+ assert_respond_to(float, :to_f, "The arguments must respond to to_f; the #{name} did not")
+ end
+ assert_operator(delta, :>=, 0.0, "The delta should not be negative")
+ full_message = build_message(message, <<EOT, expected_float, actual_float, delta)
+<?> and
+<?> expected to be within
+<?> of each other.
+EOT
+ assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
+ end
+ end
+
+ ##
+ # Passes if the method send returns a true value.
+ #
+ # +send_array+ is composed of:
+ # * A receiver
+ # * A method
+ # * Arguments to the method
+ #
+ # Example:
+ # assert_send [[1, 2], :include?, 4]
+
+ public
+ def assert_send(send_array, message="")
+ _wrap_assertion do
+ assert_instance_of(Array, send_array, "assert_send requires an array of send information")
+ assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name")
+ full_message = build_message(message, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1])
+<?> expected to respond to
+<?(?)> with a true value.
+EOT
+ assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
+ end
+ end
+
+ ##
+ # Builds a failure message. +head+ is added before the +template+ and
+ # +arguments+ replaces the '?'s positionally in the template.
+
+ public
+ def build_message(head, template=nil, *arguments)
+ template &&= template.chomp
+ return AssertionMessage.new(head, template, arguments)
+ end
+
+ private
+ def _wrap_assertion
+ @_assertion_wrapped ||= false
+ unless (@_assertion_wrapped)
+ @_assertion_wrapped = true
+ begin
+ add_assertion
+ return yield
+ ensure
+ @_assertion_wrapped = false
+ end
+ else
+ return yield
+ end
+ end
+
+ ##
+ # Called whenever an assertion is made. Define this in classes that
+ # include Test::Unit::Assertions to record assertion counts.
+
+ private
+ def add_assertion
+ end
+
+ ##
+ # Select whether or not to use the pretty-printer. If this option is set
+ # to false before any assertions are made, pp.rb will not be required.
+
+ public
+ def self.use_pp=(value)
+ AssertionMessage.use_pp = value
+ end
+
+ # :stopdoc:
+
+ class AssertionMessage
+ @use_pp = true
+ class << self
+ attr_accessor :use_pp
+ end
+
+ class Literal
+ def initialize(value)
+ @value = value
+ end
+
+ def inspect
+ @value.to_s
+ end
+ end
+
+ class Template
+ def self.create(string)
+ parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
+ self.new(parts)
+ end
+
+ attr_reader :count
+
+ def initialize(parts)
+ @parts = parts
+ @count = parts.find_all{|e| e == '?'}.size
+ end
+
+ def result(parameters)
+ raise "The number of parameters does not match the number of substitutions." if(parameters.size != count)
+ params = parameters.dup
+ @parts.collect{|e| e == '?' ? params.shift : e.gsub(/\\\?/m, '?')}.join('')
+ end
+ end
+
+ def self.literal(value)
+ Literal.new(value)
+ end
+
+ include Util::BacktraceFilter
+
+ def initialize(head, template_string, parameters)
+ @head = head
+ @template_string = template_string
+ @parameters = parameters
+ end
+
+ def convert(object)
+ case object
+ when Exception
+ <<EOM.chop
+Class: <#{convert(object.class)}>
+Message: <#{convert(object.message)}>
+---Backtrace---
+#{filter_backtrace(object.backtrace).join("\n")}
+---------------
+EOM
+ else
+ if(self.class.use_pp)
+ begin
+ require 'pp'
+ rescue LoadError
+ self.class.use_pp = false
+ return object.inspect
+ end unless(defined?(PP))
+ PP.pp(object, '').chomp
+ else
+ object.inspect
+ end
+ end
+ end
+
+ def template
+ @template ||= Template.create(@template_string)
+ end
+
+ def add_period(string)
+ (string =~ /\.\Z/ ? string : string + '.')
+ end
+
+ def to_s
+ message_parts = []
+ if (@head)
+ head = @head.to_s
+ unless(head.empty?)
+ message_parts << add_period(head)
+ end
+ end
+ tail = template.result(@parameters.collect{|e| convert(e)})
+ message_parts << tail unless(tail.empty?)
+ message_parts.join("\n")
+ end
+ end
+
+ # :startdoc:
+
+ end
+ end
+end
diff --git a/test/lib/test/unit/autorunner.rb b/test/lib/test/unit/autorunner.rb
new file mode 100644
index 0000000..0dfc01c
--- /dev/null
+++ b/test/lib/test/unit/autorunner.rb
@@ -0,0 +1,219 @@
+require 'test/unit/ui/testrunnerutilities'
+require 'optparse'
+
+module Test
+ module Unit
+ class AutoRunner
+ def self.run(force_standalone=false, default_dir=nil, argv=ARGV, &block)
+ r = new(force_standalone || standalone?, &block)
+ r.base = default_dir
+ r.process_args(argv)
+ r.run
+ end
+
+ def self.standalone?
+ return false unless("-e" == $0)
+ ObjectSpace.each_object(Class) do |klass|
+ return false if(klass < TestCase)
+ end
+ true
+ end
+
+ RUNNERS = {
+ :console => proc do |r|
+ require 'test/unit/ui/console/testrunner'
+ Test::Unit::UI::Console::TestRunner
+ end,
+ :gtk => proc do |r|
+ require 'test/unit/ui/gtk/testrunner'
+ Test::Unit::UI::GTK::TestRunner
+ end,
+ :gtk2 => proc do |r|
+ require 'test/unit/ui/gtk2/testrunner'
+ Test::Unit::UI::GTK2::TestRunner
+ end,
+ :fox => proc do |r|
+ require 'test/unit/ui/fox/testrunner'
+ Test::Unit::UI::Fox::TestRunner
+ end,
+ :tk => proc do |r|
+ require 'test/unit/ui/tk/testrunner'
+ Test::Unit::UI::Tk::TestRunner
+ end,
+ }
+
+ OUTPUT_LEVELS = [
+ [:silent, UI::SILENT],
+ [:progress, UI::PROGRESS_ONLY],
+ [:normal, UI::NORMAL],
+ [:verbose, UI::VERBOSE],
+ ]
+
+ COLLECTORS = {
+ :objectspace => proc do |r|
+ require 'test/unit/collector/objectspace'
+ c = Collector::ObjectSpace.new
+ c.filter = r.filters
+ c.collect($0.sub(/\.rb\Z/, ''))
+ end,
+ :dir => proc do |r|
+ require 'test/unit/collector/dir'
+ c = Collector::Dir.new
+ c.filter = r.filters
+ c.pattern.concat(r.pattern) if(r.pattern)
+ c.exclude.concat(r.exclude) if(r.exclude)
+ c.base = r.base
+ $:.push(r.base) if r.base
+ c.collect(*(r.to_run.empty? ? ['.'] : r.to_run))
+ end,
+ }
+
+ attr_reader :suite
+ attr_accessor :output_level, :filters, :to_run, :pattern, :exclude, :base, :workdir
+ attr_writer :runner, :collector
+
+ def initialize(standalone)
+ Unit.run = true
+ @standalone = standalone
+ @runner = RUNNERS[:console]
+ @collector = COLLECTORS[(standalone ? :dir : :objectspace)]
+ @filters = []
+ @to_run = []
+ @output_level = UI::NORMAL
+ @workdir = nil
+ yield(self) if(block_given?)
+ end
+
+ def process_args(args = ARGV)
+ begin
+ options.order!(args) {|arg| @to_run << arg}
+ rescue OptionParser::ParseError => e
+ puts e
+ puts options
+ $! = nil
+ abort
+ else
+ @filters << proc{false} unless(@filters.empty?)
+ end
+ not @to_run.empty?
+ end
+
+ def options
+ @options ||= OptionParser.new do |o|
+ o.banner = "Test::Unit automatic runner."
+ o.banner << "\nUsage: #{$0} [options] [-- untouched arguments]"
+
+ o.on
+ o.on('-r', '--runner=RUNNER', RUNNERS,
+ "Use the given RUNNER.",
+ "(" + keyword_display(RUNNERS) + ")") do |r|
+ @runner = r
+ end
+
+ if(@standalone)
+ o.on('-b', '--basedir=DIR', "Base directory of test suites.") do |b|
+ @base = b
+ end
+
+ o.on('-w', '--workdir=DIR', "Working directory to run tests.") do |w|
+ @workdir = w
+ end
+
+ o.on('-a', '--add=TORUN', Array,
+ "Add TORUN to the list of things to run;",
+ "can be a file or a directory.") do |a|
+ @to_run.concat(a)
+ end
+
+ @pattern = []
+ o.on('-p', '--pattern=PATTERN', Regexp,
+ "Match files to collect against PATTERN.") do |e|
+ @pattern << e
+ end
+
+ @exclude = []
+ o.on('-x', '--exclude=PATTERN', Regexp,
+ "Ignore files to collect against PATTERN.") do |e|
+ @exclude << e
+ end
+ end
+
+ o.on('-n', '--name=NAME', String,
+ "Runs tests matching NAME.",
+ "(patterns may be used).") do |n|
+ n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
+ case n
+ when Regexp
+ @filters << proc{|t| n =~ t.method_name ? true : nil}
+ else
+ @filters << proc{|t| n == t.method_name ? true : nil}
+ end
+ end
+
+ o.on('-t', '--testcase=TESTCASE', String,
+ "Runs tests in TestCases matching TESTCASE.",
+ "(patterns may be used).") do |n|
+ n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
+ case n
+ when Regexp
+ @filters << proc{|t| n =~ t.class.name ? true : nil}
+ else
+ @filters << proc{|t| n == t.class.name ? true : nil}
+ end
+ end
+
+ o.on('-I', "--load-path=DIR[#{File::PATH_SEPARATOR}DIR...]",
+ "Appends directory list to $LOAD_PATH.") do |dirs|
+ $LOAD_PATH.concat(dirs.split(File::PATH_SEPARATOR))
+ end
+
+ o.on('-v', '--verbose=[LEVEL]', OUTPUT_LEVELS,
+ "Set the output level (default is verbose).",
+ "(" + keyword_display(OUTPUT_LEVELS) + ")") do |l|
+ @output_level = l || UI::VERBOSE
+ end
+
+ o.on('--',
+ "Stop processing options so that the",
+ "remaining options will be passed to the",
+ "test."){o.terminate}
+
+ o.on('-h', '--help', 'Display this help.'){puts o; exit}
+
+ o.on_tail
+ o.on_tail('Deprecated options:')
+
+ o.on_tail('--console', 'Console runner (use --runner).') do
+ warn("Deprecated option (--console).")
+ @runner = RUNNERS[:console]
+ end
+
+ o.on_tail('--gtk', 'GTK runner (use --runner).') do
+ warn("Deprecated option (--gtk).")
+ @runner = RUNNERS[:gtk]
+ end
+
+ o.on_tail('--fox', 'Fox runner (use --runner).') do
+ warn("Deprecated option (--fox).")
+ @runner = RUNNERS[:fox]
+ end
+
+ o.on_tail
+ end
+ end
+
+ def keyword_display(array)
+ list = array.collect {|e, *| e.to_s}
+ Array === array or list.sort!
+ list.collect {|e| e.sub(/^(.)([A-Za-z]+)(?=\w*$)/, '\\1[\\2]')}.join(", ")
+ end
+
+ def run
+ @suite = @collector[self]
+ result = @runner[self] or return false
+ Dir.chdir(@workdir) if @workdir
+ result.run(@suite, @output_level).passed?
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/collector.rb b/test/lib/test/unit/collector.rb
new file mode 100644
index 0000000..9e9e654
--- /dev/null
+++ b/test/lib/test/unit/collector.rb
@@ -0,0 +1,43 @@
+module Test
+ module Unit
+ module Collector
+ def initialize
+ @filters = []
+ end
+
+ def filter=(filters)
+ @filters = case(filters)
+ when Proc
+ [filters]
+ when Array
+ filters
+ end
+ end
+
+ def add_suite(destination, suite)
+ to_delete = suite.tests.find_all{|t| !include?(t)}
+ to_delete.each{|t| suite.delete(t)}
+ destination << suite unless(suite.size == 0)
+ end
+
+ def include?(test)
+ return true if(@filters.empty?)
+ @filters.each do |filter|
+ result = filter[test]
+ if(result.nil?)
+ next
+ elsif(!result)
+ return false
+ else
+ return true
+ end
+ end
+ true
+ end
+
+ def sort(suites)
+ suites.sort_by{|s| s.name}
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/collector/dir.rb b/test/lib/test/unit/collector/dir.rb
new file mode 100644
index 0000000..97c8d28
--- /dev/null
+++ b/test/lib/test/unit/collector/dir.rb
@@ -0,0 +1,107 @@
+require 'test/unit/testsuite'
+require 'test/unit/collector'
+
+module Test
+ module Unit
+ module Collector
+ class Dir
+ include Collector
+
+ attr_reader :pattern, :exclude
+ attr_accessor :base
+
+ def initialize(dir=::Dir, file=::File, object_space=::ObjectSpace, req=nil)
+ super()
+ @dir = dir
+ @file = file
+ @object_space = object_space
+ @req = req
+ @pattern = [/\btest_.*\.rb\Z/m]
+ @exclude = []
+ end
+
+ def collect(*from)
+ basedir = @base
+ $:.push(basedir) if basedir
+ if(from.empty?)
+ recursive_collect('.', find_test_cases)
+ elsif(from.size == 1)
+ recursive_collect(from.first, find_test_cases)
+ else
+ suites = []
+ from.each do |f|
+ suite = recursive_collect(f, find_test_cases)
+ suites << suite unless(suite.tests.empty?)
+ end
+ suite = TestSuite.new("[#{from.join(', ')}]")
+ sort(suites).each{|s| suite << s}
+ suite
+ end
+ ensure
+ $:.delete_at($:.rindex(basedir)) if basedir
+ end
+
+ def find_test_cases(ignore=[])
+ cases = []
+ @object_space.each_object(Class) do |c|
+ cases << c if(c < TestCase && !ignore.include?(c))
+ end
+ ignore.concat(cases)
+ cases
+ end
+
+ def recursive_collect(name, already_gathered)
+ sub_suites = []
+ path = realdir(name)
+ if @file.directory?(path)
+ dir_name = name unless name == '.'
+ @dir.entries(path).each do |e|
+ next if(e == '.' || e == '..')
+ e_name = dir_name ? @file.join(dir_name, e) : e
+ if @file.directory?(realdir(e_name))
+ next if /\ACVS\z/ =~ e
+ sub_suite = recursive_collect(e_name, already_gathered)
+ sub_suites << sub_suite unless(sub_suite.empty?)
+ else
+ next if /~\z/ =~ e_name or /\A\.\#/ =~ e
+ if @pattern and !@pattern.empty?
+ next unless @pattern.any? {|pat| pat =~ e_name}
+ end
+ if @exclude and !@exclude.empty?
+ next if @exclude.any? {|pat| pat =~ e_name}
+ end
+ collect_file(e_name, sub_suites, already_gathered)
+ end
+ end
+ else
+ collect_file(name, sub_suites, already_gathered)
+ end
+ suite = TestSuite.new(@file.basename(name))
+ sort(sub_suites).each{|s| suite << s}
+ suite
+ end
+
+ def collect_file(name, suites, already_gathered)
+ dir = @file.dirname(@file.expand_path(name, @base))
+ $:.unshift(dir)
+ if(@req)
+ @req.require(name)
+ else
+ require(name)
+ end
+ find_test_cases(already_gathered).each{|t| add_suite(suites, t.suite)}
+ ensure
+ $:.delete_at($:.rindex(dir)) if(dir)
+ end
+
+ def realdir(path)
+ if @base
+ @file.join(@base, path)
+ else
+ path
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/error.rb b/test/lib/test/unit/error.rb
new file mode 100644
index 0000000..43a813f
--- /dev/null
+++ b/test/lib/test/unit/error.rb
@@ -0,0 +1,56 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+require 'test/unit/util/backtracefilter'
+
+module Test
+ module Unit
+
+ # Encapsulates an error in a test. Created by
+ # Test::Unit::TestCase when it rescues an exception thrown
+ # during the processing of a test.
+ class Error
+ include Util::BacktraceFilter
+
+ attr_reader(:test_name, :exception)
+
+ SINGLE_CHARACTER = 'E'
+
+ # Creates a new Error with the given test_name and
+ # exception.
+ def initialize(test_name, exception)
+ @test_name = test_name
+ @exception = exception
+ end
+
+ # Returns a single character representation of an error.
+ def single_character_display
+ SINGLE_CHARACTER
+ end
+
+ # Returns the message associated with the error.
+ def message
+ "#{@exception.class.name}: #{@exception.message}"
+ end
+
+ # Returns a brief version of the error description.
+ def short_display
+ "#@test_name: #{message.split("\n")[0]}"
+ end
+
+ # Returns a verbose version of the error description.
+ def long_display
+ backtrace = filter_backtrace(@exception.backtrace).join("\n ")
+ "Error:\n#@test_name:\n#{message}\n #{backtrace}"
+ end
+
+ # Overridden to return long_display.
+ def to_s
+ long_display
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/failure.rb b/test/lib/test/unit/failure.rb
new file mode 100644
index 0000000..832c998
--- /dev/null
+++ b/test/lib/test/unit/failure.rb
@@ -0,0 +1,51 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+module Test
+ module Unit
+
+ # Encapsulates a test failure. Created by Test::Unit::TestCase
+ # when an assertion fails.
+ class Failure
+ attr_reader :test_name, :location, :message
+
+ SINGLE_CHARACTER = 'F'
+
+ # Creates a new Failure with the given location and
+ # message.
+ def initialize(test_name, location, message)
+ @test_name = test_name
+ @location = location
+ @message = message
+ end
+
+ # Returns a single character representation of a failure.
+ def single_character_display
+ SINGLE_CHARACTER
+ end
+
+ # Returns a brief version of the error description.
+ def short_display
+ "#@test_name: #{@message.split("\n")[0]}"
+ end
+
+ # Returns a verbose version of the error description.
+ def long_display
+ location_display = if(location.size == 1)
+ location[0].sub(/\A(.+:\d+).*/, ' [\\1]')
+ else
+ "\n [#{location.join("\n ")}]"
+ end
+ "Failure:\n#@test_name#{location_display}:\n#@message"
+ end
+
+ # Overridden to return long_display.
+ def to_s
+ long_display
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/testcase.rb b/test/lib/test/unit/testcase.rb
new file mode 100644
index 0000000..f53b460
--- /dev/null
+++ b/test/lib/test/unit/testcase.rb
@@ -0,0 +1,160 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+require 'test/unit/assertions'
+require 'test/unit/failure'
+require 'test/unit/error'
+require 'test/unit/testsuite'
+require 'test/unit/assertionfailederror'
+require 'test/unit/util/backtracefilter'
+
+module Test
+ module Unit
+
+ # Ties everything together. If you subclass and add your own
+ # test methods, it takes care of making them into tests and
+ # wrapping those tests into a suite. It also does the
+ # nitty-gritty of actually running an individual test and
+ # collecting its results into a Test::Unit::TestResult object.
+ class TestCase
+ include Assertions
+ include Util::BacktraceFilter
+
+ attr_reader :method_name
+
+ STARTED = name + "::STARTED"
+ FINISHED = name + "::FINISHED"
+
+ ##
+ # These exceptions are not caught by #run.
+
+ PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, Interrupt,
+ SystemExit]
+
+ # Creates a new instance of the fixture for running the
+ # test represented by test_method_name.
+ def initialize(test_method_name)
+ unless(respond_to?(test_method_name) and
+ (method(test_method_name).arity == 0 ||
+ method(test_method_name).arity == -1))
+ throw :invalid_test
+ end
+ @method_name = test_method_name
+ @test_passed = true
+ end
+
+ # Rolls up all of the test* methods in the fixture into
+ # one suite, creating a new instance of the fixture for
+ # each method.
+ def self.suite
+ method_names = public_instance_methods(true)
+ tests = method_names.delete_if {|method_name| method_name !~ /^test./}
+ suite = TestSuite.new(name)
+ tests.sort.each do
+ |test|
+ catch(:invalid_test) do
+ suite << new(test)
+ end
+ end
+ if (suite.empty?)
+ catch(:invalid_test) do
+ suite << new("default_test")
+ end
+ end
+ return suite
+ end
+
+ # Runs the individual test method represented by this
+ # instance of the fixture, collecting statistics, failures
+ # and errors in result.
+ def run(result)
+ yield(STARTED, name)
+ @_result = result
+ begin
+ setup
+ __send__(@method_name)
+ rescue AssertionFailedError => e
+ add_failure(e.message, e.backtrace)
+ rescue Exception
+ raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
+ add_error($!)
+ ensure
+ begin
+ teardown
+ rescue AssertionFailedError => e
+ add_failure(e.message, e.backtrace)
+ rescue Exception
+ raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
+ add_error($!)
+ end
+ end
+ result.add_run
+ yield(FINISHED, name)
+ end
+
+ # Called before every test method runs. Can be used
+ # to set up fixture information.
+ def setup
+ end
+
+ # Called after every test method runs. Can be used to tear
+ # down fixture information.
+ def teardown
+ end
+
+ def default_test
+ flunk("No tests were specified")
+ end
+
+ # Returns whether this individual test passed or
+ # not. Primarily for use in teardown so that artifacts
+ # can be left behind if the test fails.
+ def passed?
+ return @test_passed
+ end
+ private :passed?
+
+ def size
+ 1
+ end
+
+ def add_assertion
+ @_result.add_assertion
+ end
+ private :add_assertion
+
+ def add_failure(message, all_locations=caller())
+ @test_passed = false
+ @_result.add_failure(Failure.new(name, filter_backtrace(all_locations), message))
+ end
+ private :add_failure
+
+ def add_error(exception)
+ @test_passed = false
+ @_result.add_error(Error.new(name, exception))
+ end
+ private :add_error
+
+ # Returns a human-readable name for the specific test that
+ # this instance of TestCase represents.
+ def name
+ "#{@method_name}(#{self.class.name})"
+ end
+
+ # Overridden to return #name.
+ def to_s
+ name
+ end
+
+ # It's handy to be able to compare TestCase instances.
+ def ==(other)
+ return false unless(other.kind_of?(self.class))
+ return false unless(@method_name == other.method_name)
+ self.class == other.class
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/testresult.rb b/test/lib/test/unit/testresult.rb
new file mode 100644
index 0000000..e3a472e
--- /dev/null
+++ b/test/lib/test/unit/testresult.rb
@@ -0,0 +1,80 @@
+#--
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+require 'test/unit/util/observable'
+
+module Test
+ module Unit
+
+ # Collects Test::Unit::Failure and Test::Unit::Error so that
+ # they can be displayed to the user. To this end, observers
+ # can be added to it, allowing the dynamic updating of, say, a
+ # UI.
+ class TestResult
+ include Util::Observable
+
+ CHANGED = "CHANGED"
+ FAULT = "FAULT"
+
+ attr_reader(:run_count, :assertion_count)
+
+ # Constructs a new, empty TestResult.
+ def initialize
+ @run_count, @assertion_count = 0, 0
+ @failures, @errors = Array.new, Array.new
+ end
+
+ # Records a test run.
+ def add_run
+ @run_count += 1
+ notify_listeners(CHANGED, self)
+ end
+
+ # Records a Test::Unit::Failure.
+ def add_failure(failure)
+ @failures << failure
+ notify_listeners(FAULT, failure)
+ notify_listeners(CHANGED, self)
+ end
+
+ # Records a Test::Unit::Error.
+ def add_error(error)
+ @errors << error
+ notify_listeners(FAULT, error)
+ notify_listeners(CHANGED, self)
+ end
+
+ # Records an individual assertion.
+ def add_assertion
+ @assertion_count += 1
+ notify_listeners(CHANGED, self)
+ end
+
+ # Returns a string contain the recorded runs, assertions,
+ # failures and errors in this TestResult.
+ def to_s
+ "#{run_count} tests, #{assertion_count} assertions, #{failure_count} failures, #{error_count} errors"
+ end
+
+ # Returns whether or not this TestResult represents
+ # successful completion.
+ def passed?
+ return @failures.empty? && @errors.empty?
+ end
+
+ # Returns the number of failures this TestResult has
+ # recorded.
+ def failure_count
+ return @failures.size
+ end
+
+ # Returns the number of errors this TestResult has
+ # recorded.
+ def error_count
+ return @errors.size
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/testsuite.rb b/test/lib/test/unit/testsuite.rb
new file mode 100644
index 0000000..6fea976
--- /dev/null
+++ b/test/lib/test/unit/testsuite.rb
@@ -0,0 +1,76 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+module Test
+ module Unit
+
+ # A collection of tests which can be #run.
+ #
+ # Note: It is easy to confuse a TestSuite instance with
+ # something that has a static suite method; I know because _I_
+ # have trouble keeping them straight. Think of something that
+ # has a suite method as simply providing a way to get a
+ # meaningful TestSuite instance.
+ class TestSuite
+ attr_reader :name, :tests
+
+ STARTED = name + "::STARTED"
+ FINISHED = name + "::FINISHED"
+
+ # Creates a new TestSuite with the given name.
+ def initialize(name="Unnamed TestSuite")
+ @name = name
+ @tests = []
+ end
+
+ # Runs the tests and/or suites contained in this
+ # TestSuite.
+ def run(result, &progress_block)
+ yield(STARTED, name)
+ @tests.each do |test|
+ test.run(result, &progress_block)
+ end
+ yield(FINISHED, name)
+ end
+
+ # Adds the test to the suite.
+ def <<(test)
+ @tests << test
+ self
+ end
+
+ def delete(test)
+ @tests.delete(test)
+ end
+
+ # Retuns the rolled up number of tests in this suite;
+ # i.e. if the suite contains other suites, it counts the
+ # tests within those suites, not the suites themselves.
+ def size
+ total_size = 0
+ @tests.each { |test| total_size += test.size }
+ total_size
+ end
+
+ def empty?
+ tests.empty?
+ end
+
+ # Overridden to return the name given the suite at
+ # creation.
+ def to_s
+ @name
+ end
+
+ # It's handy to be able to compare TestSuite instances.
+ def ==(other)
+ return false unless(other.kind_of?(self.class))
+ return false unless(@name == other.name)
+ @tests == other.tests
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/ui/console/testrunner.rb b/test/lib/test/unit/ui/console/testrunner.rb
new file mode 100644
index 0000000..6b600e3
--- /dev/null
+++ b/test/lib/test/unit/ui/console/testrunner.rb
@@ -0,0 +1,127 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+require 'test/unit/ui/testrunnermediator'
+require 'test/unit/ui/testrunnerutilities'
+
+module Test
+ module Unit
+ module UI
+ module Console
+
+ # Runs a Test::Unit::TestSuite on the console.
+ class TestRunner
+ extend TestRunnerUtilities
+
+ # Creates a new TestRunner for running the passed
+ # suite. If quiet_mode is true, the output while
+ # running is limited to progress dots, errors and
+ # failures, and the final result. io specifies
+ # where runner output should go to; defaults to
+ # STDOUT.
+ def initialize(suite, output_level=NORMAL, io=STDOUT)
+ if (suite.respond_to?(:suite))
+ @suite = suite.suite
+ else
+ @suite = suite
+ end
+ @output_level = output_level
+ @io = io
+ @already_outputted = false
+ @faults = []
+ end
+
+ # Begins the test run.
+ def start
+ setup_mediator
+ attach_to_mediator
+ return start_mediator
+ end
+
+ private
+ def setup_mediator
+ @mediator = create_mediator(@suite)
+ suite_name = @suite.to_s
+ if ( @suite.kind_of?(Module) )
+ suite_name = @suite.name
+ end
+ output("Loaded suite #{suite_name}")
+ end
+
+ def create_mediator(suite)
+ return TestRunnerMediator.new(suite)
+ end
+
+ def attach_to_mediator
+ @mediator.add_listener(TestResult::FAULT, &method(:add_fault))
+ @mediator.add_listener(TestRunnerMediator::STARTED, &method(:started))
+ @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished))
+ @mediator.add_listener(TestCase::STARTED, &method(:test_started))
+ @mediator.add_listener(TestCase::FINISHED, &method(:test_finished))
+ end
+
+ def start_mediator
+ return @mediator.run_suite
+ end
+
+ def add_fault(fault)
+ @faults << fault
+ output_single(fault.single_character_display, PROGRESS_ONLY)
+ @already_outputted = true
+ end
+
+ def started(result)
+ @result = result
+ output("Started")
+ end
+
+ def finished(elapsed_time)
+ nl
+ output("Finished in #{elapsed_time} seconds.")
+ @faults.each_with_index do |fault, index|
+ nl
+ output("%3d) %s" % [index + 1, fault.long_display])
+ end
+ nl
+ output(@result)
+ end
+
+ def test_started(name)
+ output_single(name + ": ", VERBOSE)
+ end
+
+ def test_finished(name)
+ output_single(".", PROGRESS_ONLY) unless (@already_outputted)
+ nl(VERBOSE)
+ @already_outputted = false
+ end
+
+ def nl(level=NORMAL)
+ output("", level)
+ end
+
+ def output(something, level=NORMAL)
+ @io.puts(something) if (output?(level))
+ @io.flush
+ end
+
+ def output_single(something, level=NORMAL)
+ @io.write(something) if (output?(level))
+ @io.flush
+ end
+
+ def output?(level)
+ level <= @output_level
+ end
+ end
+ end
+ end
+ end
+end
+
+if __FILE__ == $0
+ Test::Unit::UI::Console::TestRunner.start_command_line_test
+end
diff --git a/test/lib/test/unit/ui/testrunnermediator.rb b/test/lib/test/unit/ui/testrunnermediator.rb
new file mode 100644
index 0000000..d34510d
--- /dev/null
+++ b/test/lib/test/unit/ui/testrunnermediator.rb
@@ -0,0 +1,68 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+require 'test/unit'
+require 'test/unit/util/observable'
+require 'test/unit/testresult'
+
+module Test
+ module Unit
+ module UI
+
+ # Provides an interface to write any given UI against,
+ # hopefully making it easy to write new UIs.
+ class TestRunnerMediator
+ RESET = name + "::RESET"
+ STARTED = name + "::STARTED"
+ FINISHED = name + "::FINISHED"
+
+ include Util::Observable
+
+ # Creates a new TestRunnerMediator initialized to run
+ # the passed suite.
+ def initialize(suite)
+ @suite = suite
+ end
+
+ # Runs the suite the TestRunnerMediator was created
+ # with.
+ def run_suite
+ Unit.run = true
+ begin_time = Time.now
+ notify_listeners(RESET, @suite.size)
+ result = create_result
+ notify_listeners(STARTED, result)
+ result_listener = result.add_listener(TestResult::CHANGED) do |updated_result|
+ notify_listeners(TestResult::CHANGED, updated_result)
+ end
+
+ fault_listener = result.add_listener(TestResult::FAULT) do |fault|
+ notify_listeners(TestResult::FAULT, fault)
+ end
+
+ @suite.run(result) do |channel, value|
+ notify_listeners(channel, value)
+ end
+
+ result.remove_listener(TestResult::FAULT, fault_listener)
+ result.remove_listener(TestResult::CHANGED, result_listener)
+ end_time = Time.now
+ elapsed_time = end_time - begin_time
+ notify_listeners(FINISHED, elapsed_time) #"Finished in #{elapsed_time} seconds.")
+ return result
+ end
+
+ private
+ # A factory method to create the result the mediator
+ # should run with. Can be overridden by subclasses if
+ # one wants to use a different result.
+ def create_result
+ return TestResult.new
+ end
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/ui/testrunnerutilities.rb b/test/lib/test/unit/ui/testrunnerutilities.rb
new file mode 100644
index 0000000..70b885b
--- /dev/null
+++ b/test/lib/test/unit/ui/testrunnerutilities.rb
@@ -0,0 +1,46 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+module Test
+ module Unit
+ module UI
+
+ SILENT = 0
+ PROGRESS_ONLY = 1
+ NORMAL = 2
+ VERBOSE = 3
+
+ # Provides some utilities common to most, if not all,
+ # TestRunners.
+ #
+ #--
+ #
+ # Perhaps there ought to be a TestRunner superclass? There
+ # seems to be a decent amount of shared code between test
+ # runners.
+
+ module TestRunnerUtilities
+
+ # Creates a new TestRunner and runs the suite.
+ def run(suite, output_level=NORMAL)
+ return new(suite, output_level).start
+ end
+
+ # Takes care of the ARGV parsing and suite
+ # determination necessary for running one of the
+ # TestRunners from the command line.
+ def start_command_line_test
+ if ARGV.empty?
+ puts "You should supply the name of a test suite file to the runner"
+ exit
+ end
+ require ARGV[0].gsub(/.+::/, '')
+ new(eval(ARGV[0])).start
+ end
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/util/backtracefilter.rb b/test/lib/test/unit/util/backtracefilter.rb
new file mode 100644
index 0000000..7ebec2d
--- /dev/null
+++ b/test/lib/test/unit/util/backtracefilter.rb
@@ -0,0 +1,40 @@
+module Test
+ module Unit
+ module Util
+ module BacktraceFilter
+ TESTUNIT_FILE_SEPARATORS = %r{[\\/:]}
+ TESTUNIT_PREFIX = __FILE__.split(TESTUNIT_FILE_SEPARATORS)[0..-3]
+ TESTUNIT_RB_FILE = /\.rb\Z/
+
+ def filter_backtrace(backtrace, prefix=nil)
+ return ["No backtrace"] unless(backtrace)
+ split_p = if(prefix)
+ prefix.split(TESTUNIT_FILE_SEPARATORS)
+ else
+ TESTUNIT_PREFIX
+ end
+ match = proc do |e|
+ split_e = e.split(TESTUNIT_FILE_SEPARATORS)[0, split_p.size]
+ next false unless(split_e[0..-2] == split_p[0..-2])
+ split_e[-1].sub(TESTUNIT_RB_FILE, '') == split_p[-1]
+ end
+ return backtrace unless(backtrace.detect(&match))
+ found_prefix = false
+ new_backtrace = backtrace.reverse.reject do |e|
+ if(match[e])
+ found_prefix = true
+ true
+ elsif(found_prefix)
+ false
+ else
+ true
+ end
+ end.reverse
+ new_backtrace = (new_backtrace.empty? ? backtrace : new_backtrace)
+ new_backtrace = new_backtrace.reject(&match)
+ new_backtrace.empty? ? backtrace : new_backtrace
+ end
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/util/observable.rb b/test/lib/test/unit/util/observable.rb
new file mode 100644
index 0000000..3567d34
--- /dev/null
+++ b/test/lib/test/unit/util/observable.rb
@@ -0,0 +1,90 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+require 'test/unit/util/procwrapper'
+
+module Test
+ module Unit
+ module Util
+
+ # This is a utility class that allows anything mixing
+ # it in to notify a set of listeners about interesting
+ # events.
+ module Observable
+ # We use this for defaults since nil might mean something
+ NOTHING = "NOTHING/#{__id__}"
+
+ # Adds the passed proc as a listener on the
+ # channel indicated by channel_name. listener_key
+ # is used to remove the listener later; if none is
+ # specified, the proc itself is used.
+ #
+ # Whatever is used as the listener_key is
+ # returned, making it very easy to use the proc
+ # itself as the listener_key:
+ #
+ # listener = add_listener("Channel") { ... }
+ # remove_listener("Channel", listener)
+ def add_listener(channel_name, listener_key=NOTHING, &listener) # :yields: value
+ unless(block_given?)
+ raise ArgumentError.new("No callback was passed as a listener")
+ end
+
+ key = listener_key
+ if (listener_key == NOTHING)
+ listener_key = listener
+ key = ProcWrapper.new(listener)
+ end
+
+ channels[channel_name] ||= {}
+ channels[channel_name][key] = listener
+ return listener_key
+ end
+
+ # Removes the listener indicated by listener_key
+ # from the channel indicated by
+ # channel_name. Returns the registered proc, or
+ # nil if none was found.
+ def remove_listener(channel_name, listener_key)
+ channel = channels[channel_name]
+ return nil unless (channel)
+ key = listener_key
+ if (listener_key.instance_of?(Proc))
+ key = ProcWrapper.new(listener_key)
+ end
+ if (channel.has_key?(key))
+ return channel.delete(key)
+ end
+ return nil
+ end
+
+ # Calls all the procs registered on the channel
+ # indicated by channel_name. If value is
+ # specified, it is passed in to the procs,
+ # otherwise they are called with no arguments.
+ #
+ #--
+ #
+ # Perhaps this should be private? Would it ever
+ # make sense for an external class to call this
+ # method directly?
+ def notify_listeners(channel_name, *arguments)
+ channel = channels[channel_name]
+ return 0 unless (channel)
+ listeners = channel.values
+ listeners.each { |listener| listener.call(*arguments) }
+ return listeners.size
+ end
+
+ private
+ def channels
+ @channels ||= {}
+ return @channels
+ end
+ end
+ end
+ end
+end
diff --git a/test/lib/test/unit/util/procwrapper.rb b/test/lib/test/unit/util/procwrapper.rb
new file mode 100644
index 0000000..ad72521
--- /dev/null
+++ b/test/lib/test/unit/util/procwrapper.rb
@@ -0,0 +1,48 @@
+#--
+#
+# Author:: Nathaniel Talbott.
+# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
+# License:: Ruby license.
+
+module Test
+ module Unit
+ module Util
+
+ # Allows the storage of a Proc passed through '&' in a
+ # hash.
+ #
+ # Note: this may be inefficient, since the hash being
+ # used is not necessarily very good. In Observable,
+ # efficiency is not too important, since the hash is
+ # only accessed when adding and removing listeners,
+ # not when notifying.
+
+ class ProcWrapper
+
+ # Creates a new wrapper for a_proc.
+ def initialize(a_proc)
+ @a_proc = a_proc
+ @hash = a_proc.inspect.sub(/^(#<#{a_proc.class}:)/){''}.sub(/(>)$/){''}.hex
+ end
+
+ def hash
+ return @hash
+ end
+
+ def ==(other)
+ case(other)
+ when ProcWrapper
+ return @a_proc == other.to_proc
+ else
+ return super
+ end
+ end
+ alias :eql? :==
+
+ def to_proc
+ return @a_proc
+ end
+ end
+ end
+ end
+end
diff --git a/test/unit/comment_filter.rb b/test/unit/comment_filter.rb
new file mode 100644
index 0000000..e255d07
--- /dev/null
+++ b/test/unit/comment_filter.rb
@@ -0,0 +1,53 @@
+require 'test/unit'
+require 'coderay'
+
+class CommentFilterTest < Test::Unit::TestCase
+
+ def test_filtering_comments
+ tokens = CodeRay.scan <<-RUBY, :ruby
+#!/usr/bin/env ruby
+# a minimal Ruby program
+puts "Hello world!"
+ RUBY
+ assert_equal <<-RUBY_FILTERED, tokens.comment_filter.text
+#!/usr/bin/env ruby
+
+puts "Hello world!"
+ RUBY_FILTERED
+ end
+
+ def test_filtering_docstrings
+ tokens = CodeRay.scan <<-PYTHON, :python
+'''
+Assuming this is file mymodule.py then this string, being the
+first statement in the file will become the mymodule modules
+docstring when the file is imported
+'''
+
+class Myclass():
+ """
+ The class's docstring
+ """
+
+ def mymethod(self):
+ '''The method's docstring'''
+
+def myfunction():
+ """The function's docstring"""
+ PYTHON
+ assert_equal <<-PYTHON_FILTERED.chomp, tokens.comment_filter.text
+
+
+class Myclass():
+
+
+ def mymethod(self):
+
+
+def myfunction():
+
+
+PYTHON_FILTERED
+ end
+
+end
\ No newline at end of file
diff --git a/test/unit/count.rb b/test/unit/count.rb
new file mode 100644
index 0000000..448e8f1
--- /dev/null
+++ b/test/unit/count.rb
@@ -0,0 +1,15 @@
+require 'test/unit'
+require 'coderay'
+
+class CountTest < Test::Unit::TestCase
+
+ def test_count
+ tokens = CodeRay.scan <<-RUBY.strip, :ruby
+#!/usr/bin/env ruby
+# a minimal Ruby program
+puts "Hello world!"
+ RUBY
+ assert_equal 11, tokens.encode(:count)
+ end
+
+end
\ No newline at end of file
diff --git a/test/unit/debug.rb b/test/unit/debug.rb
new file mode 100644
index 0000000..88baf56
--- /dev/null
+++ b/test/unit/debug.rb
@@ -0,0 +1,77 @@
+require 'test/unit'
+require 'coderay'
+
+class DebugEncoderTest < Test::Unit::TestCase
+
+ def test_creation
+ debug = nil
+ assert_nothing_raised do
+ debug = CodeRay.encoder :debug
+ end
+ assert CodeRay::Encoders::Debug < CodeRay::Encoders::Encoder
+ assert_kind_of CodeRay::Encoders::Encoder, debug
+ end
+
+ TEST_INPUT = CodeRay::Tokens[
+ ['10', :integer],
+ ['(\\)', :operator],
+ [:begin_group, :string],
+ ['test', :content],
+ [:end_group, :string],
+ [:begin_line, :head],
+ ["\n", :space],
+ ["\n \t", :space],
+ [" \n", :space],
+ ["[]", :method],
+ [:end_line, :head],
+ ].flatten
+ TEST_OUTPUT = <<-'DEBUG'.chomp
+integer(10)operator((\\\))string<content(test)>head[
+
+
+method([])]
+ DEBUG
+
+ def test_filtering_text_tokens
+ assert_equal TEST_OUTPUT, CodeRay::Encoders::Debug.new.encode_tokens(TEST_INPUT)
+ assert_equal TEST_OUTPUT, TEST_INPUT.debug
+ end
+
+end
+
+class DebugScannerTest < Test::Unit::TestCase
+
+ def test_creation
+ assert CodeRay::Scanners::Debug < CodeRay::Scanners::Scanner
+ debug = nil
+ assert_nothing_raised do
+ debug = CodeRay.scanner :debug
+ end
+ assert_kind_of CodeRay::Scanners::Scanner, debug
+ end
+
+ TEST_INPUT = <<-'DEBUG'.chomp
+integer(10)operator((\\\))string<content(test)>test[
+
+
+method([])]
+ DEBUG
+ TEST_OUTPUT = CodeRay::Tokens[
+ ['10', :integer],
+ ['(\\)', :operator],
+ [:begin_group, :string],
+ ['test', :content],
+ [:end_group, :string],
+ [:begin_line, :unknown],
+ ["\n\n \t \n", :space],
+ ["[]", :method],
+ [:end_line, :unknown],
+ ].flatten
+
+ def test_filtering_text_tokens
+ assert_equal TEST_OUTPUT, CodeRay::Scanners::Debug.new.tokenize(TEST_INPUT)
+ assert_kind_of CodeRay::TokensProxy, CodeRay.scan(TEST_INPUT, :debug)
+ assert_equal TEST_OUTPUT, CodeRay.scan(TEST_INPUT, :debug).tokens
+ end
+
+end
diff --git a/test/unit/duo.rb b/test/unit/duo.rb
new file mode 100644
index 0000000..05c26a5
--- /dev/null
+++ b/test/unit/duo.rb
@@ -0,0 +1,35 @@
+require 'test/unit'
+require 'yaml'
+require 'coderay'
+
+class DuoTest < Test::Unit::TestCase
+
+ def test_two_arguments
+ duo = CodeRay::Duo[:ruby, :html]
+ assert_kind_of CodeRay::Scanners[:ruby], duo.scanner
+ assert_kind_of CodeRay::Encoders[:html], duo.encoder
+ end
+
+ def test_two_hash
+ duo = CodeRay::Duo[:ruby => :html]
+ assert_kind_of CodeRay::Scanners[:ruby], duo.scanner
+ assert_kind_of CodeRay::Encoders[:html], duo.encoder
+ end
+
+ def test_call
+ duo = CodeRay::Duo[:python => :yml]
+ yaml = [["def", :keyword],
+ [" ", :space],
+ ["test", :method],
+ [":", :operator],
+ [" ", :space],
+ [:begin_group, :string],
+ ["\"", :delimiter],
+ ["pass", :content],
+ ["\"", :delimiter],
+ [:end_group, :string]]
+
+ assert_equal yaml, YAML.load(duo.call('def test: "pass"'))
+ end
+
+end
diff --git a/test/unit/file_type.rb b/test/unit/file_type.rb
new file mode 100644
index 0000000..263517b
--- /dev/null
+++ b/test/unit/file_type.rb
@@ -0,0 +1,116 @@
+require 'test/unit'
+require File.expand_path('../../lib/assert_warning', __FILE__)
+
+require 'coderay/helpers/file_type'
+
+class FileTypeTests < Test::Unit::TestCase
+
+ include CodeRay
+
+ def test_fetch
+ assert_raise FileType::UnknownFileType do
+ FileType.fetch ''
+ end
+
+ assert_throws :not_found do
+ FileType.fetch '.' do
+ throw :not_found
+ end
+ end
+
+ assert_equal :default, FileType.fetch('c', :default)
+ end
+
+ def test_block_supersedes_default_warning
+ assert_warning 'Block supersedes default value argument; use either.' do
+ FileType.fetch('c', :default) { }
+ end
+ end
+
+ def test_ruby
+ assert_equal :ruby, FileType[__FILE__]
+ assert_equal :ruby, FileType['test.rb']
+ assert_equal :ruby, FileType['test.java.rb']
+ assert_equal :java, FileType['test.rb.java']
+ assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
+ assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
+ assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
+ assert_equal :ruby, FileType['./lib/tasks\repository.rake']
+ assert_not_equal :ruby, FileType['test_rb']
+ assert_not_equal :ruby, FileType['Makefile']
+ assert_not_equal :ruby, FileType['set.rb/set']
+ assert_not_equal :ruby, FileType['~/projects/blabla/rb']
+ end
+
+ def test_c
+ assert_equal :c, FileType['test.c']
+ assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
+ assert_not_equal :c, FileType['test_c']
+ assert_not_equal :c, FileType['Makefile']
+ assert_not_equal :c, FileType['set.h/set']
+ assert_not_equal :c, FileType['~/projects/blabla/c']
+ end
+
+ def test_cpp
+ assert_equal :cpp, FileType['test.c++']
+ assert_equal :cpp, FileType['test.cxx']
+ assert_equal :cpp, FileType['test.hh']
+ assert_equal :cpp, FileType['test.hpp']
+ assert_equal :cpp, FileType['test.cu']
+ assert_equal :cpp, FileType['test.C']
+ assert_not_equal :cpp, FileType['test.c']
+ assert_not_equal :cpp, FileType['test.h']
+ end
+
+ def test_html
+ assert_equal :html, FileType['test.htm']
+ assert_equal :html, FileType['test.xhtml']
+ assert_equal :html, FileType['test.html.xhtml']
+ assert_equal :erb, FileType['_form.rhtml']
+ assert_equal :erb, FileType['_form.html.erb']
+ end
+
+ def test_yaml
+ assert_equal :yaml, FileType['test.yml']
+ assert_equal :yaml, FileType['test.yaml']
+ assert_equal :yaml, FileType['my.html.yaml']
+ assert_not_equal :yaml, FileType['YAML']
+ end
+
+ def test_pathname
+ require 'pathname'
+ pn = Pathname.new 'test.rb'
+ assert_equal :ruby, FileType[pn]
+ dir = Pathname.new '/etc/var/blubb'
+ assert_equal :ruby, FileType[dir + pn]
+ assert_equal :cpp, FileType[dir + 'test.cpp']
+ end
+
+ def test_no_shebang
+ dir = './test'
+ if File.directory? dir
+ Dir.chdir dir do
+ assert_equal :c, FileType['test.c']
+ end
+ end
+ end
+
+ def test_shebang_empty_file
+ require 'tmpdir'
+ tmpfile = File.join(Dir.tmpdir, 'bla')
+ File.open(tmpfile, 'w') { } # touch
+ assert_equal nil, FileType[tmpfile, true]
+ end
+
+ def test_shebang_no_file
+ assert_equal nil, FileType['i do not exist', true]
+ end
+
+ def test_shebang
+ require 'tmpdir'
+ tmpfile = File.join(Dir.tmpdir, 'bla')
+ File.open(tmpfile, 'w') { |f| f.puts '#!/usr/bin/env ruby' }
+ assert_equal :ruby, FileType[tmpfile, true]
+ end
+
+end
diff --git a/test/unit/filter.rb b/test/unit/filter.rb
new file mode 100644
index 0000000..25dff77
--- /dev/null
+++ b/test/unit/filter.rb
@@ -0,0 +1,38 @@
+require 'test/unit'
+require 'coderay'
+
+class FilterTest < Test::Unit::TestCase
+
+ def test_creation
+ filter = nil
+ assert_nothing_raised do
+ filter = CodeRay.encoder :filter
+ end
+ assert CodeRay::Encoders::Filter < CodeRay::Encoders::Encoder
+ assert_kind_of CodeRay::Encoders::Encoder, filter
+ end
+
+ def test_filtering_text_tokens
+ tokens = CodeRay::Tokens.new
+ 10.times do |i|
+ tokens.text_token i.to_s, :index
+ end
+ assert_equal tokens, CodeRay::Encoders::Filter.new.encode_tokens(tokens)
+ assert_equal tokens, tokens.filter
+ end
+
+ def test_filtering_block_tokens
+ tokens = CodeRay::Tokens.new
+ 10.times do |i|
+ tokens.begin_group :index
+ tokens.text_token i.to_s, :content
+ tokens.end_group :index
+ tokens.begin_line :index
+ tokens.text_token i.to_s, :content
+ tokens.end_line :index
+ end
+ assert_equal tokens, CodeRay::Encoders::Filter.new.encode_tokens(tokens)
+ assert_equal tokens, tokens.filter
+ end
+
+end
diff --git a/test/unit/html.rb b/test/unit/html.rb
new file mode 100644
index 0000000..0072635
--- /dev/null
+++ b/test/unit/html.rb
@@ -0,0 +1,103 @@
+require 'test/unit'
+require 'coderay'
+
+class HtmlTest < Test::Unit::TestCase
+
+ def test_break_lines_option
+ snippets = {}
+
+ snippets[:ruby] = {}
+
+ snippets[:ruby][:in] = <<-RUBY
+ruby_inside = <<-RUBY_INSIDE
+This is tricky,
+isn't it?
+RUBY_INSIDE
+ RUBY
+
+ snippets[:ruby][:expected_with_option_off] = <<-HTML_OPT_INDEPENDENT_LINES_OFF
+ruby_inside = <span class=\"string\"><span class=\"delimiter\"><<-RUBY_INSIDE</span></span><span class=\"string\"><span class=\"content\">
+This is tricky,
+isn't it?</span><span class=\"delimiter\">
+RUBY_INSIDE</span></span>
+ HTML_OPT_INDEPENDENT_LINES_OFF
+
+ snippets[:ruby][:expected_with_option_on] = <<-HTML_OPT_INDEPENDENT_LINES_ON
+ruby_inside = <span class=\"string\"><span class=\"delimiter\"><<-RUBY_INSIDE</span></span><span class=\"string\"><span class=\"content\"></span></span>
+<span class=\"string\"><span class=\"content\">This is tricky,</span></span>
+<span class=\"string\"><span class=\"content\">isn't it?</span><span class=\"delimiter\"></span></span>
+<span class=\"string\"><span class=\"delimiter\">RUBY_INSIDE</span></span>
+ HTML_OPT_INDEPENDENT_LINES_ON
+
+ snippets[:java] = {}
+
+ snippets[:java][:in] = <<-JAVA
+import java.lang.*;
+
+/**
+ * This is some multiline javadoc
+ * used to test the
+ */
+public class Test {
+ public static final String MESSAGE = "My message\
+ To the world";
+
+ static void main() {
+ /*
+ * Another multiline
+ * comment
+ */
+ System.out.println(MESSAGE);
+ }
+}
+ JAVA
+
+ snippets[:java][:expected_with_option_off] = <<-HTML_OPT_INDEPENDENT_LINES_OFF
+<span class=\"keyword\">import</span> <span class=\"include\">java.lang</span>.*;
+
+<span class=\"comment\">/**
+ * This is some multiline javadoc
+ * used to test the
+ */</span>
+<span class=\"directive\">public</span> <span class=\"type\">class</span> <span class=\"class\">Test</span> {
+ <span class=\"directive\">public</span> <span class=\"directive\">static</span> <span class=\"directive\">final</span> <span class=\"predefined-type\">String</span> MESSAGE = <span class=\"string\"><span class=\"delimiter\">"</span><span class=\"content\">My message To the world</span><span class=\"delimiter\">"</span></span>;
+
+ <span class=\"directive\">static</span> <span class=\"type\">void</span> main() {
+ <span class=\"comment\">/*
+ * Another multiline
+ * comment
+ */</span>
+ <span class=\"predefined-type\">System</span>.out.println(MESSAGE);
+ }
+}
+ HTML_OPT_INDEPENDENT_LINES_OFF
+
+ snippets[:java][:expected_with_option_on] = <<-HTML_OPT_INDEPENDENT_LINES_ON
+<span class=\"keyword\">import</span> <span class=\"include\">java.lang</span>.*;
+
+<span class=\"comment\">/**</span>
+<span class=\"comment\"> * This is some multiline javadoc</span>
+<span class=\"comment\"> * used to test the</span>
+<span class=\"comment\"> */</span>
+<span class=\"directive\">public</span> <span class=\"type\">class</span> <span class=\"class\">Test</span> {
+ <span class=\"directive\">public</span> <span class=\"directive\">static</span> <span class=\"directive\">final</span> <span class=\"predefined-type\">String</span> MESSAGE = <span class=\"string\"><span class=\"delimiter\">"</span><span class=\"content\">My message To the world</span><span class=\"delimiter\">"</span></span>;
+
+ <span class=\"directive\">static</span> <span class=\"type\">void</span> main() {
+ <span class=\"comment\">/*</span>
+<span class=\"comment\"> * Another multiline</span>
+<span class=\"comment\"> * comment</span>
+<span class=\"comment\"> */</span>
+ <span class=\"predefined-type\">System</span>.out.println(MESSAGE);
+ }
+}
+ HTML_OPT_INDEPENDENT_LINES_ON
+
+ for lang, code in snippets
+ tokens = CodeRay.scan code[:in], lang
+
+ assert_equal code[:expected_with_option_off], tokens.html
+ assert_equal code[:expected_with_option_off], tokens.html(:break_lines => false)
+ assert_equal code[:expected_with_option_on], tokens.html(:break_lines => true)
+ end
+ end
+end
diff --git a/test/unit/json_encoder.rb b/test/unit/json_encoder.rb
new file mode 100644
index 0000000..4e44a64
--- /dev/null
+++ b/test/unit/json_encoder.rb
@@ -0,0 +1,28 @@
+require 'test/unit'
+require 'coderay'
+
+class JSONEncoderTest < Test::Unit::TestCase
+
+ def test_json_output
+ old_load_paths = $:.dup
+ begin
+ $:.delete '.'
+ $:.delete File.dirname(__FILE__)
+ json = CodeRay.scan('puts "Hello world!"', :ruby).json
+ assert_equal [
+ {"type"=>"text", "text"=>"puts", "kind"=>"ident"},
+ {"type"=>"text", "text"=>" ", "kind"=>"space"},
+ {"type"=>"block", "action"=>"open", "kind"=>"string"},
+ {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
+ {"type"=>"text", "text"=>"Hello world!", "kind"=>"content"},
+ {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
+ {"type"=>"block", "action"=>"close", "kind"=>"string"},
+ ], JSON.load(json)
+ ensure
+ for path in old_load_paths - $:
+ $: << path
+ end
+ end
+ end
+
+end
\ No newline at end of file
diff --git a/test/unit/lines_of_code.rb b/test/unit/lines_of_code.rb
new file mode 100644
index 0000000..e2c0caf
--- /dev/null
+++ b/test/unit/lines_of_code.rb
@@ -0,0 +1,52 @@
+require 'test/unit'
+require 'coderay'
+$VERBOSE = true
+
+require File.expand_path('../../lib/assert_warning', __FILE__)
+
+class LinesOfCodeTest < Test::Unit::TestCase
+
+ def test_creation
+ assert CodeRay::Encoders::LinesOfCode < CodeRay::Encoders::Encoder
+ filter = nil
+ assert_nothing_raised do
+ filter = CodeRay.encoder :loc
+ end
+ assert_kind_of CodeRay::Encoders::LinesOfCode, filter
+ assert_nothing_raised do
+ filter = CodeRay.encoder :lines_of_code
+ end
+ assert_kind_of CodeRay::Encoders::LinesOfCode, filter
+ end
+
+ def test_lines_of_code
+ tokens = CodeRay.scan <<-RUBY, :ruby
+#!/usr/bin/env ruby
+
+# a minimal Ruby program
+puts "Hello world!"
+ RUBY
+ assert_equal 1, CodeRay::Encoders::LinesOfCode.new.encode_tokens(tokens)
+ assert_equal 1, tokens.lines_of_code
+ assert_equal 1, tokens.loc
+ end
+
+ class ScannerMockup
+ KINDS_NOT_LOC = [:space]
+ end
+
+ def test_filtering_block_tokens
+ tokens = CodeRay::Tokens.new
+ tokens.concat ["Hello\n", :world]
+ tokens.concat ["\n", :space]
+ tokens.concat ["Hello\n", :comment]
+
+ assert_warning 'Tokens have no associated scanner, counting all nonempty lines.' do
+ assert_equal 1, tokens.lines_of_code
+ end
+
+ tokens.scanner = ScannerMockup.new
+ assert_equal 2, tokens.lines_of_code
+ end
+
+end
\ No newline at end of file
diff --git a/test/unit/null.rb b/test/unit/null.rb
new file mode 100644
index 0000000..d3a9b0d
--- /dev/null
+++ b/test/unit/null.rb
@@ -0,0 +1,14 @@
+require 'test/unit'
+require 'coderay'
+
+class NullTest < Test::Unit::TestCase
+
+ def test_null
+ ruby = <<-RUBY
+puts "Hello world!"
+ RUBY
+ tokens = CodeRay.scan ruby, :ruby
+ assert_equal '', tokens.encode(:null)
+ end
+
+end
\ No newline at end of file
diff --git a/test/unit/plugin.rb b/test/unit/plugin.rb
new file mode 100755
index 0000000..41eec51
--- /dev/null
+++ b/test/unit/plugin.rb
@@ -0,0 +1,69 @@
+require 'test/unit'
+require 'pathname'
+
+$:.unshift File.expand_path('../../../lib', __FILE__)
+require 'coderay'
+
+class PluginScannerTest < Test::Unit::TestCase
+
+ module Plugins
+ extend CodeRay::PluginHost
+ plugin_path File.dirname(__FILE__), 'plugins'
+ class Plugin
+ extend CodeRay::Plugin
+ plugin_host Plugins
+ end
+ end
+
+ module PluginsWithDefault
+ extend CodeRay::PluginHost
+ plugin_path File.dirname(__FILE__), 'plugins_with_default'
+ class Plugin
+ extend CodeRay::Plugin
+ plugin_host PluginsWithDefault
+ end
+ default :default_plugin
+ end
+
+ def test_load
+ require Pathname.new(__FILE__).realpath.dirname + 'plugins' + 'user_defined' + 'user_plugin'
+ assert_equal 'UserPlugin', Plugins.load(:user_plugin).name
+ end
+
+ def test_load_all
+ assert_instance_of Symbol, Plugins.load_all.first
+ assert_operator Plugins.all_plugins.first, :<, Plugins::Plugin
+ assert_equal 'The Example', Plugins.all_plugins.map { |plugin| plugin.title }.sort.first
+ end
+
+ def test_default
+ assert_nothing_raised do
+ assert_operator PluginsWithDefault[:gargamel], :<, PluginsWithDefault::Plugin
+ end
+ assert_equal PluginsWithDefault::Default, PluginsWithDefault.default
+ end
+
+ def test_plugin_not_found
+ assert_raise CodeRay::PluginHost::PluginNotFound do
+ Plugins[:thestral]
+ end
+ assert_raise ArgumentError do
+ Plugins[14]
+ end
+ assert_raise ArgumentError do
+ Plugins['test/test']
+ end
+ assert_raise CodeRay::PluginHost::PluginNotFound do
+ PluginsWithDefault[:example_without_register_for]
+ end
+ end
+
+ def test_autoload_constants
+ assert_operator Plugins::Example, :<, Plugins::Plugin
+ end
+
+ def test_title
+ assert_equal 'The Example', Plugins::Example.title
+ end
+
+end
diff --git a/test/unit/plugins/example.rb b/test/unit/plugins/example.rb
new file mode 100644
index 0000000..af1aeba
--- /dev/null
+++ b/test/unit/plugins/example.rb
@@ -0,0 +1,6 @@
+class Example < PluginScannerTest::Plugins::Plugin
+
+ register_for :example
+ title 'The Example'
+
+end
diff --git a/test/unit/plugins/user_defined/user_plugin.rb b/test/unit/plugins/user_defined/user_plugin.rb
new file mode 100644
index 0000000..f47c934
--- /dev/null
+++ b/test/unit/plugins/user_defined/user_plugin.rb
@@ -0,0 +1,5 @@
+class UserPlugin < PluginScannerTest::Plugins::Plugin
+
+ register_for :user_plugin
+
+end
diff --git a/test/unit/plugins_with_default/default_plugin.rb b/test/unit/plugins_with_default/default_plugin.rb
new file mode 100644
index 0000000..ae9e4c5
--- /dev/null
+++ b/test/unit/plugins_with_default/default_plugin.rb
@@ -0,0 +1,5 @@
+class DefaultPlugin < PluginScannerTest::PluginsWithDefault::Plugin
+
+ register_for :default_plugin
+
+end
diff --git a/test/unit/plugins_with_default/example_without_register_for.rb b/test/unit/plugins_with_default/example_without_register_for.rb
new file mode 100644
index 0000000..083baf6
--- /dev/null
+++ b/test/unit/plugins_with_default/example_without_register_for.rb
@@ -0,0 +1,5 @@
+class ExampleWithoutRegisterFor < PluginScannerTest::PluginsWithDefault::Plugin
+
+ register_for :wrong_id
+
+end
diff --git a/test/unit/statistic.rb b/test/unit/statistic.rb
new file mode 100644
index 0000000..1326dca
--- /dev/null
+++ b/test/unit/statistic.rb
@@ -0,0 +1,59 @@
+require 'test/unit'
+require 'coderay'
+
+class StatisticEncoderTest < Test::Unit::TestCase
+
+ def test_creation
+ assert CodeRay::Encoders::Statistic < CodeRay::Encoders::Encoder
+ stats = nil
+ assert_nothing_raised do
+ stats = CodeRay.encoder :statistic
+ end
+ assert_kind_of CodeRay::Encoders::Encoder, stats
+ end
+
+ TEST_INPUT = CodeRay::Tokens[
+ ['10', :integer],
+ ['(\\)', :operator],
+ [:begin_group, :string],
+ ['test', :content],
+ [:end_group, :string],
+ [:begin_line, :test],
+ ["\n", :space],
+ ["\n \t", :space],
+ [" \n", :space],
+ ["[]", :method],
+ [:end_line, :test],
+ ].flatten
+ TEST_OUTPUT = <<-'DEBUG'
+
+Code Statistics
+
+Tokens 11
+ Non-Whitespace 4
+Bytes Total 20
+
+Token Types (7):
+ type count ratio size (average)
+-------------------------------------------------------------
+ TOTAL 11 100.00 % 1.8
+ space 3 27.27 % 3.0
+ string 2 18.18 % 0.0
+ test 2 18.18 % 0.0
+ :begin_group 1 9.09 % 0.0
+ :begin_line 1 9.09 % 0.0
+ :end_group 1 9.09 % 0.0
+ :end_line 1 9.09 % 0.0
+ content 1 9.09 % 4.0
+ integer 1 9.09 % 2.0
+ method 1 9.09 % 2.0
+ operator 1 9.09 % 3.0
+
+ DEBUG
+
+ def test_filtering_text_tokens
+ assert_equal TEST_OUTPUT, CodeRay::Encoders::Statistic.new.encode_tokens(TEST_INPUT)
+ assert_equal TEST_OUTPUT, TEST_INPUT.statistic
+ end
+
+end
\ No newline at end of file
diff --git a/test/unit/suite.rb b/test/unit/suite.rb
new file mode 100755
index 0000000..417dfed
--- /dev/null
+++ b/test/unit/suite.rb
@@ -0,0 +1,16 @@
+require 'test/unit'
+require 'rubygems'
+
+$VERBOSE = $CODERAY_DEBUG = true
+$:.unshift 'lib'
+
+mydir = File.dirname(__FILE__)
+suite = Dir[File.join(mydir, '*.rb')].
+ map { |tc| File.basename(tc).sub(/\.rb$/, '') } - %w'suite vhdl'
+
+puts "Running CodeRay unit tests: #{suite.join(', ')}"
+
+helpers = %w(file_type word_list tokens)
+for test_case in helpers + (suite - helpers)
+ load File.join(mydir, test_case + '.rb')
+end
diff --git a/test/unit/text.rb b/test/unit/text.rb
new file mode 100644
index 0000000..db086f5
--- /dev/null
+++ b/test/unit/text.rb
@@ -0,0 +1,14 @@
+require 'test/unit'
+require 'coderay'
+
+class TextTest < Test::Unit::TestCase
+
+ def test_count
+ ruby = <<-RUBY
+puts "Hello world!"
+ RUBY
+ tokens = CodeRay.scan ruby, :ruby
+ assert_equal ruby, tokens.encode(:text)
+ end
+
+end
\ No newline at end of file
diff --git a/test/unit/token_kind_filter.rb b/test/unit/token_kind_filter.rb
new file mode 100644
index 0000000..13bae52
--- /dev/null
+++ b/test/unit/token_kind_filter.rb
@@ -0,0 +1,50 @@
+require 'test/unit'
+require 'coderay'
+
+class TokenKindFilterTest < Test::Unit::TestCase
+
+ def test_creation
+ assert CodeRay::Encoders::TokenKindFilter < CodeRay::Encoders::Encoder
+ assert CodeRay::Encoders::TokenKindFilter < CodeRay::Encoders::Filter
+ filter = nil
+ assert_nothing_raised do
+ filter = CodeRay.encoder :token_kind_filter
+ end
+ assert_instance_of CodeRay::Encoders::TokenKindFilter, filter
+ end
+
+ def test_filtering_text_tokens
+ tokens = CodeRay::Tokens.new
+ for i in 1..10
+ tokens.text_token i.to_s, :index
+ tokens.text_token ' ', :space if i < 10
+ end
+ assert_equal 10, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :space).count
+ assert_equal 10, tokens.token_kind_filter(:exclude => :space).count
+ assert_equal 9, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => :space).count
+ assert_equal 9, tokens.token_kind_filter(:include => :space).count
+ assert_equal 0, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :all).count
+ assert_equal 0, tokens.token_kind_filter(:exclude => :all).count
+ end
+
+ def test_filtering_block_tokens
+ tokens = CodeRay::Tokens.new
+ 10.times do |i|
+ tokens.begin_group :index
+ tokens.text_token i.to_s, :content
+ tokens.end_group :index
+ tokens.begin_group :naught if i == 5
+ tokens.end_group :naught if i == 7
+ tokens.begin_line :blubb
+ tokens.text_token i.to_s, :content
+ tokens.end_line :blubb
+ end
+ assert_equal 16, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => :blubb).count
+ assert_equal 16, tokens.token_kind_filter(:include => :blubb).count
+ assert_equal 24, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :include => [:blubb, :content]).count
+ assert_equal 24, tokens.token_kind_filter(:include => [:blubb, :content]).count
+ assert_equal 32, CodeRay::Encoders::TokenKindFilter.new.encode_tokens(tokens, :exclude => :index).count
+ assert_equal 32, tokens.token_kind_filter(:exclude => :index).count
+ end
+
+end
diff --git a/test/unit/tokens.rb b/test/unit/tokens.rb
new file mode 100644
index 0000000..73b0fd5
--- /dev/null
+++ b/test/unit/tokens.rb
@@ -0,0 +1,77 @@
+require 'test/unit'
+require 'coderay'
+
+class TokensTest < Test::Unit::TestCase
+
+ def test_creation
+ assert CodeRay::Tokens < Array
+ tokens = nil
+ assert_nothing_raised do
+ tokens = CodeRay::Tokens.new
+ end
+ assert_kind_of Array, tokens
+ end
+
+ def test_adding_tokens
+ tokens = make_tokens
+ assert_equal tokens.size, 8
+ assert_equal tokens.count, 4
+ end
+
+ def test_to_s
+ assert_equal 'string()', make_tokens.to_s
+ end
+
+ def test_encode_with_nonsense
+ assert_raise NoMethodError do
+ make_tokens.nonsense
+ end
+ end
+
+ def test_split_into_parts
+ parts_4_3 = [
+ ["stri", :type],
+ ["ng", :type, :begin_group, :operator, "(", :content, :end_group, :operator],
+ [:begin_group, :operator, ")", :content, :end_group, :operator]
+ ]
+ assert_equal parts_4_3, make_tokens.split_into_parts(4, 3)
+ assert_equal [make_tokens.to_a], make_tokens.split_into_parts
+
+ parts_7_0_1 = [
+ ["string", :type, :begin_group, :operator, "(", :content, :end_group, :operator],
+ [],
+ [:begin_group, :operator, ")", :content, :end_group, :operator]
+ ]
+ assert_equal parts_7_0_1, make_tokens.split_into_parts(7, 0, 1)
+
+ line = CodeRay::Tokens[:begin_line, :head, '...', :plain]
+ line_parts = [
+ [:begin_line, :head, ".", :plain, :end_line, :head],
+ [:begin_line, :head, "..", :plain]
+ ]
+ assert_equal line_parts, line.split_into_parts(1)
+
+ assert_raise ArgumentError do
+ CodeRay::Tokens[:bullshit, :input].split_into_parts
+ end
+ assert_raise ArgumentError do
+ CodeRay::Tokens[42, 43].split_into_parts
+ end
+ end
+
+ def test_encode
+ assert_match(/\A\[\{(?:"type":"text"|"text":"string"|"kind":"type"|,){5}\},/, make_tokens.encode(:json))
+ end
+
+ def make_tokens
+ tokens = CodeRay::Tokens.new
+ assert_nothing_raised do
+ tokens.text_token 'string', :type
+ tokens.begin_group :operator
+ tokens.text_token '()', :content
+ tokens.end_group :operator
+ end
+ tokens
+ end
+
+end
\ No newline at end of file
diff --git a/test/unit/word_list.rb b/test/unit/word_list.rb
new file mode 100644
index 0000000..40d5a26
--- /dev/null
+++ b/test/unit/word_list.rb
@@ -0,0 +1,64 @@
+require 'test/unit'
+require 'coderay/helpers/word_list'
+
+class WordListTest < Test::Unit::TestCase
+
+ include CodeRay
+
+ # define word arrays
+ RESERVED_WORDS = %w[
+ asm break case continue default do else
+ ...
+ ]
+
+ PREDEFINED_TYPES = %w[
+ int long short char void
+ ...
+ ]
+
+ PREDEFINED_CONSTANTS = %w[
+ EOF NULL ...
+ ]
+
+ # make a WordList
+ IDENT_KIND = WordList.new(:ident).
+ add(RESERVED_WORDS, :reserved).
+ add(PREDEFINED_TYPES, :predefined_type).
+ add(PREDEFINED_CONSTANTS, :predefined_constant)
+
+ def test_word_list_example
+ assert_equal :predefined_type, IDENT_KIND['void']
+ # assert_equal :predefined_constant, IDENT_KIND['...'] # not specified
+ end
+
+ def test_word_list
+ list = WordList.new(:ident).add(['foobar'], :reserved)
+ assert_equal :reserved, list['foobar']
+ assert_equal :ident, list['FooBar']
+ assert_equal 1, list.size
+ end
+
+ def test_case_ignoring_word_list
+ list = WordList::CaseIgnoring.new(:ident).add(['foobar'], :reserved)
+ assert_equal :ident, list['foo']
+ assert_equal :reserved, list['foobar']
+ assert_equal :reserved, list['FooBar']
+ assert_equal 1, list.size
+
+ list = WordList::CaseIgnoring.new(:ident).add(['FooBar'], :reserved)
+ assert_equal :ident, list['foo']
+ assert_equal :reserved, list['foobar']
+ assert_equal :reserved, list['FooBar']
+ assert_equal 1, list.size
+ end
+
+ def test_dup
+ list = WordList.new(:ident).add(['foobar'], :reserved)
+ assert_equal :reserved, list['foobar']
+ list2 = list.dup
+ list2.add(%w[foobar], :keyword)
+ assert_equal :keyword, list2['foobar']
+ assert_equal :reserved, list['foobar']
+ end
+
+end
|