/usr/share/javascript/yui/yuitest/yuitest.js is in libjs-yui 2.8.2r1~squeeze-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.com/yui/license.html
version: 2.8.2r1
*/
YAHOO.namespace("tool");
//-----------------------------------------------------------------------------
// TestCase object
//-----------------------------------------------------------------------------
(function(){
//used for autogenerating test case names
var tempId = 0;
/**
* Test case containing various tests to run.
* @param template An object containing any number of test methods, other methods,
* an optional name, and anything else the test case needs.
* @class TestCase
* @namespace YAHOO.tool
* @constructor
*/
YAHOO.tool.TestCase = function (template /*:Object*/) {
/**
* Special rules for the test case. Possible subobjects
* are fail, for tests that should fail, and error, for
* tests that should throw an error.
*/
this._should /*:Object*/ = {};
//copy over all properties from the template to this object
for (var prop in template) {
this[prop] = template[prop];
}
//check for a valid name
if (!YAHOO.lang.isString(this.name)){
/**
* Name for the test case.
*/
this.name /*:String*/ = "testCase" + (tempId++);
}
};
YAHOO.tool.TestCase.prototype = {
/**
* Resumes a paused test and runs the given function.
* @param {Function} segment (Optional) The function to run.
* If omitted, the test automatically passes.
* @return {Void}
* @method resume
*/
resume : function (segment /*:Function*/) /*:Void*/ {
YAHOO.tool.TestRunner.resume(segment);
},
/**
* Causes the test case to wait a specified amount of time and then
* continue executing the given code.
* @param {Function} segment (Optional) The function to run after the delay.
* If omitted, the TestRunner will wait until resume() is called.
* @param {int} delay (Optional) The number of milliseconds to wait before running
* the function. If omitted, defaults to zero.
* @return {Void}
* @method wait
*/
wait : function (segment /*:Function*/, delay /*:int*/) /*:Void*/{
var args = arguments;
if (YAHOO.lang.isFunction(args[0])){
throw new YAHOO.tool.TestCase.Wait(args[0], args[1]);
} else {
throw new YAHOO.tool.TestCase.Wait(function(){
YAHOO.util.Assert.fail("Timeout: wait() called but resume() never called.");
}, (YAHOO.lang.isNumber(args[0]) ? args[0] : 10000));
}
},
//-------------------------------------------------------------------------
// Stub Methods
//-------------------------------------------------------------------------
/**
* Function to run before each test is executed.
* @return {Void}
* @method setUp
*/
setUp : function () /*:Void*/ {
},
/**
* Function to run after each test is executed.
* @return {Void}
* @method tearDown
*/
tearDown: function () /*:Void*/ {
}
};
/**
* Represents a stoppage in test execution to wait for an amount of time before
* continuing.
* @param {Function} segment A function to run when the wait is over.
* @param {int} delay The number of milliseconds to wait before running the code.
* @class Wait
* @namespace YAHOO.tool.TestCase
* @constructor
*
*/
YAHOO.tool.TestCase.Wait = function (segment /*:Function*/, delay /*:int*/) {
/**
* The segment of code to run when the wait is over.
* @type Function
* @property segment
*/
this.segment /*:Function*/ = (YAHOO.lang.isFunction(segment) ? segment : null);
/**
* The delay before running the segment of code.
* @type int
* @property delay
*/
this.delay /*:int*/ = (YAHOO.lang.isNumber(delay) ? delay : 0);
};
})();
YAHOO.namespace("tool");
//-----------------------------------------------------------------------------
// TestSuite object
//-----------------------------------------------------------------------------
/**
* A test suite that can contain a collection of TestCase and TestSuite objects.
* @param {String||Object} data The name of the test suite or an object containing
* a name property as well as setUp and tearDown methods.
* @namespace YAHOO.tool
* @class TestSuite
* @constructor
*/
YAHOO.tool.TestSuite = function (data /*:String||Object*/) {
/**
* The name of the test suite.
* @type String
* @property name
*/
this.name /*:String*/ = "";
/**
* Array of test suites and
* @private
*/
this.items /*:Array*/ = [];
//initialize the properties
if (YAHOO.lang.isString(data)){
this.name = data;
} else if (YAHOO.lang.isObject(data)){
YAHOO.lang.augmentObject(this, data, true);
}
//double-check name
if (this.name === ""){
this.name = YAHOO.util.Dom.generateId(null, "testSuite");
}
};
YAHOO.tool.TestSuite.prototype = {
/**
* Adds a test suite or test case to the test suite.
* @param {YAHOO.tool.TestSuite||YAHOO.tool.TestCase} testObject The test suite or test case to add.
* @return {Void}
* @method add
*/
add : function (testObject /*:YAHOO.tool.TestSuite*/) /*:Void*/ {
if (testObject instanceof YAHOO.tool.TestSuite || testObject instanceof YAHOO.tool.TestCase) {
this.items.push(testObject);
}
},
//-------------------------------------------------------------------------
// Stub Methods
//-------------------------------------------------------------------------
/**
* Function to run before each test is executed.
* @return {Void}
* @method setUp
*/
setUp : function () /*:Void*/ {
},
/**
* Function to run after each test is executed.
* @return {Void}
* @method tearDown
*/
tearDown: function () /*:Void*/ {
}
};
YAHOO.namespace("tool");
/**
* The YUI test tool
* @module yuitest
* @namespace YAHOO.tool
* @requires yahoo,dom,event,logger
* @optional event-simulte
*/
//-----------------------------------------------------------------------------
// TestRunner object
//-----------------------------------------------------------------------------
YAHOO.tool.TestRunner = (function(){
/**
* A node in the test tree structure. May represent a TestSuite, TestCase, or
* test function.
* @param {Variant} testObject A TestSuite, TestCase, or the name of a test function.
* @class TestNode
* @constructor
* @private
*/
function TestNode(testObject /*:Variant*/){
/**
* The TestSuite, TestCase, or test function represented by this node.
* @type Variant
* @property testObject
*/
this.testObject = testObject;
/**
* Pointer to this node's first child.
* @type TestNode
* @property firstChild
*/
this.firstChild /*:TestNode*/ = null;
/**
* Pointer to this node's last child.
* @type TestNode
* @property lastChild
*/
this.lastChild = null;
/**
* Pointer to this node's parent.
* @type TestNode
* @property parent
*/
this.parent = null;
/**
* Pointer to this node's next sibling.
* @type TestNode
* @property next
*/
this.next = null;
/**
* Test results for this test object.
* @type object
* @property results
*/
this.results /*:Object*/ = {
passed : 0,
failed : 0,
total : 0,
ignored : 0
};
//initialize results
if (testObject instanceof YAHOO.tool.TestSuite){
this.results.type = "testsuite";
this.results.name = testObject.name;
} else if (testObject instanceof YAHOO.tool.TestCase){
this.results.type = "testcase";
this.results.name = testObject.name;
}
}
TestNode.prototype = {
/**
* Appends a new test object (TestSuite, TestCase, or test function name) as a child
* of this node.
* @param {Variant} testObject A TestSuite, TestCase, or the name of a test function.
* @return {Void}
*/
appendChild : function (testObject /*:Variant*/) /*:Void*/{
var node = new TestNode(testObject);
if (this.firstChild === null){
this.firstChild = this.lastChild = node;
} else {
this.lastChild.next = node;
this.lastChild = node;
}
node.parent = this;
return node;
}
};
/**
* Runs test suites and test cases, providing events to allowing for the
* interpretation of test results.
* @namespace YAHOO.tool
* @class TestRunner
* @static
*/
function TestRunner(){
//inherit from EventProvider
TestRunner.superclass.constructor.apply(this,arguments);
/**
* Suite on which to attach all TestSuites and TestCases to be run.
* @type YAHOO.tool.TestSuite
* @property masterSuite
* @private
* @static
*/
this.masterSuite /*:YAHOO.tool.TestSuite*/ = new YAHOO.tool.TestSuite("YUI Test Results");
/**
* Pointer to the current node in the test tree.
* @type TestNode
* @private
* @property _cur
* @static
*/
this._cur = null;
/**
* Pointer to the root node in the test tree.
* @type TestNode
* @private
* @property _root
* @static
*/
this._root = null;
//create events
var events /*:Array*/ = [
this.TEST_CASE_BEGIN_EVENT,
this.TEST_CASE_COMPLETE_EVENT,
this.TEST_SUITE_BEGIN_EVENT,
this.TEST_SUITE_COMPLETE_EVENT,
this.TEST_PASS_EVENT,
this.TEST_FAIL_EVENT,
this.TEST_IGNORE_EVENT,
this.COMPLETE_EVENT,
this.BEGIN_EVENT
];
for (var i=0; i < events.length; i++){
this.createEvent(events[i], { scope: this });
}
}
YAHOO.lang.extend(TestRunner, YAHOO.util.EventProvider, {
//-------------------------------------------------------------------------
// Constants
//-------------------------------------------------------------------------
/**
* Fires when a test case is opened but before the first
* test is executed.
* @event testcasebegin
*/
TEST_CASE_BEGIN_EVENT /*:String*/ : "testcasebegin",
/**
* Fires when all tests in a test case have been executed.
* @event testcasecomplete
*/
TEST_CASE_COMPLETE_EVENT /*:String*/ : "testcasecomplete",
/**
* Fires when a test suite is opened but before the first
* test is executed.
* @event testsuitebegin
*/
TEST_SUITE_BEGIN_EVENT /*:String*/ : "testsuitebegin",
/**
* Fires when all test cases in a test suite have been
* completed.
* @event testsuitecomplete
*/
TEST_SUITE_COMPLETE_EVENT /*:String*/ : "testsuitecomplete",
/**
* Fires when a test has passed.
* @event pass
*/
TEST_PASS_EVENT /*:String*/ : "pass",
/**
* Fires when a test has failed.
* @event fail
*/
TEST_FAIL_EVENT /*:String*/ : "fail",
/**
* Fires when a test has been ignored.
* @event ignore
*/
TEST_IGNORE_EVENT /*:String*/ : "ignore",
/**
* Fires when all test suites and test cases have been completed.
* @event complete
*/
COMPLETE_EVENT /*:String*/ : "complete",
/**
* Fires when the run() method is called.
* @event begin
*/
BEGIN_EVENT /*:String*/ : "begin",
//-------------------------------------------------------------------------
// Test Tree-Related Methods
//-------------------------------------------------------------------------
/**
* Adds a test case to the test tree as a child of the specified node.
* @param {TestNode} parentNode The node to add the test case to as a child.
* @param {YAHOO.tool.TestCase} testCase The test case to add.
* @return {Void}
* @static
* @private
* @method _addTestCaseToTestTree
*/
_addTestCaseToTestTree : function (parentNode /*:TestNode*/, testCase /*:YAHOO.tool.TestCase*/) /*:Void*/{
//add the test suite
var node = parentNode.appendChild(testCase);
//iterate over the items in the test case
for (var prop in testCase){
if (prop.indexOf("test") === 0 && YAHOO.lang.isFunction(testCase[prop])){
node.appendChild(prop);
}
}
},
/**
* Adds a test suite to the test tree as a child of the specified node.
* @param {TestNode} parentNode The node to add the test suite to as a child.
* @param {YAHOO.tool.TestSuite} testSuite The test suite to add.
* @return {Void}
* @static
* @private
* @method _addTestSuiteToTestTree
*/
_addTestSuiteToTestTree : function (parentNode /*:TestNode*/, testSuite /*:YAHOO.tool.TestSuite*/) /*:Void*/ {
//add the test suite
var node = parentNode.appendChild(testSuite);
//iterate over the items in the master suite
for (var i=0; i < testSuite.items.length; i++){
if (testSuite.items[i] instanceof YAHOO.tool.TestSuite) {
this._addTestSuiteToTestTree(node, testSuite.items[i]);
} else if (testSuite.items[i] instanceof YAHOO.tool.TestCase) {
this._addTestCaseToTestTree(node, testSuite.items[i]);
}
}
},
/**
* Builds the test tree based on items in the master suite. The tree is a hierarchical
* representation of the test suites, test cases, and test functions. The resulting tree
* is stored in _root and the pointer _cur is set to the root initially.
* @return {Void}
* @static
* @private
* @method _buildTestTree
*/
_buildTestTree : function () /*:Void*/ {
this._root = new TestNode(this.masterSuite);
this._cur = this._root;
//iterate over the items in the master suite
for (var i=0; i < this.masterSuite.items.length; i++){
if (this.masterSuite.items[i] instanceof YAHOO.tool.TestSuite) {
this._addTestSuiteToTestTree(this._root, this.masterSuite.items[i]);
} else if (this.masterSuite.items[i] instanceof YAHOO.tool.TestCase) {
this._addTestCaseToTestTree(this._root, this.masterSuite.items[i]);
}
}
},
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/**
* Handles the completion of a test object's tests. Tallies test results
* from one level up to the next.
* @param {TestNode} node The TestNode representing the test object.
* @return {Void}
* @method _handleTestObjectComplete
* @private
* @static
*/
_handleTestObjectComplete : function (node /*:TestNode*/) /*:Void*/ {
if (YAHOO.lang.isObject(node.testObject)){
node.parent.results.passed += node.results.passed;
node.parent.results.failed += node.results.failed;
node.parent.results.total += node.results.total;
node.parent.results.ignored += node.results.ignored;
node.parent.results[node.testObject.name] = node.results;
if (node.testObject instanceof YAHOO.tool.TestSuite){
node.testObject.tearDown();
this.fireEvent(this.TEST_SUITE_COMPLETE_EVENT, { testSuite: node.testObject, results: node.results});
} else if (node.testObject instanceof YAHOO.tool.TestCase){
this.fireEvent(this.TEST_CASE_COMPLETE_EVENT, { testCase: node.testObject, results: node.results});
}
}
},
//-------------------------------------------------------------------------
// Navigation Methods
//-------------------------------------------------------------------------
/**
* Retrieves the next node in the test tree.
* @return {TestNode} The next node in the test tree or null if the end is reached.
* @private
* @static
* @method _next
*/
_next : function () /*:TestNode*/ {
if (this._cur.firstChild) {
this._cur = this._cur.firstChild;
} else if (this._cur.next) {
this._cur = this._cur.next;
} else {
while (this._cur && !this._cur.next && this._cur !== this._root){
this._handleTestObjectComplete(this._cur);
this._cur = this._cur.parent;
}
if (this._cur == this._root){
this._cur.results.type = "report";
this._cur.results.timestamp = (new Date()).toLocaleString();
this._cur.results.duration = (new Date()) - this._cur.results.duration;
this.fireEvent(this.COMPLETE_EVENT, { results: this._cur.results});
this._cur = null;
} else {
this._handleTestObjectComplete(this._cur);
this._cur = this._cur.next;
}
}
return this._cur;
},
/**
* Runs a test case or test suite, returning the results.
* @param {YAHOO.tool.TestCase|YAHOO.tool.TestSuite} testObject The test case or test suite to run.
* @return {Object} Results of the execution with properties passed, failed, and total.
* @private
* @method _run
* @static
*/
_run : function () /*:Void*/ {
//flag to indicate if the TestRunner should wait before continuing
var shouldWait /*:Boolean*/ = false;
//get the next test node
var node = this._next();
if (node !== null) {
var testObject = node.testObject;
//figure out what to do
if (YAHOO.lang.isObject(testObject)){
if (testObject instanceof YAHOO.tool.TestSuite){
this.fireEvent(this.TEST_SUITE_BEGIN_EVENT, { testSuite: testObject });
testObject.setUp();
} else if (testObject instanceof YAHOO.tool.TestCase){
this.fireEvent(this.TEST_CASE_BEGIN_EVENT, { testCase: testObject });
}
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
setTimeout(function(){
YAHOO.tool.TestRunner._run();
}, 0);
} else {
this._run();
}
} else {
this._runTest(node);
}
}
},
_resumeTest : function (segment /*:Function*/) /*:Void*/ {
//get relevant information
var node /*:TestNode*/ = this._cur;
var testName /*:String*/ = node.testObject;
var testCase /*:YAHOO.tool.TestCase*/ = node.parent.testObject;
//cancel other waits if available
if (testCase.__yui_wait){
clearTimeout(testCase.__yui_wait);
delete testCase.__yui_wait;
}
//get the "should" test cases
var shouldFail /*:Object*/ = (testCase._should.fail || {})[testName];
var shouldError /*:Object*/ = (testCase._should.error || {})[testName];
//variable to hold whether or not the test failed
var failed /*:Boolean*/ = false;
var error /*:Error*/ = null;
//try the test
try {
//run the test
segment.apply(testCase);
//if it should fail, and it got here, then it's a fail because it didn't
if (shouldFail){
error = new YAHOO.util.ShouldFail();
failed = true;
} else if (shouldError){
error = new YAHOO.util.ShouldError();
failed = true;
}
} catch (thrown /*:Error*/){
if (thrown instanceof YAHOO.util.AssertionError) {
if (!shouldFail){
error = thrown;
failed = true;
}
} else if (thrown instanceof YAHOO.tool.TestCase.Wait){
if (YAHOO.lang.isFunction(thrown.segment)){
if (YAHOO.lang.isNumber(thrown.delay)){
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
testCase.__yui_wait = setTimeout(function(){
YAHOO.tool.TestRunner._resumeTest(thrown.segment);
}, thrown.delay);
} else {
throw new Error("Asynchronous tests not supported in this environment.");
}
}
}
return;
} else {
//first check to see if it should error
if (!shouldError) {
error = new YAHOO.util.UnexpectedError(thrown);
failed = true;
} else {
//check to see what type of data we have
if (YAHOO.lang.isString(shouldError)){
//if it's a string, check the error message
if (thrown.message != shouldError){
error = new YAHOO.util.UnexpectedError(thrown);
failed = true;
}
} else if (YAHOO.lang.isFunction(shouldError)){
//if it's a function, see if the error is an instance of it
if (!(thrown instanceof shouldError)){
error = new YAHOO.util.UnexpectedError(thrown);
failed = true;
}
} else if (YAHOO.lang.isObject(shouldError)){
//if it's an object, check the instance and message
if (!(thrown instanceof shouldError.constructor) ||
thrown.message != shouldError.message){
error = new YAHOO.util.UnexpectedError(thrown);
failed = true;
}
}
}
}
}
//fireEvent appropriate event
if (failed) {
this.fireEvent(this.TEST_FAIL_EVENT, { testCase: testCase, testName: testName, error: error });
} else {
this.fireEvent(this.TEST_PASS_EVENT, { testCase: testCase, testName: testName });
}
//run the tear down
testCase.tearDown();
//update results
node.parent.results[testName] = {
result: failed ? "fail" : "pass",
message: error ? error.getMessage() : "Test passed",
type: "test",
name: testName
};
if (failed){
node.parent.results.failed++;
} else {
node.parent.results.passed++;
}
node.parent.results.total++;
//set timeout not supported in all environments
if (typeof setTimeout != "undefined"){
setTimeout(function(){
YAHOO.tool.TestRunner._run();
}, 0);
} else {
this._run();
}
},
/**
* Runs a single test based on the data provided in the node.
* @param {TestNode} node The TestNode representing the test to run.
* @return {Void}
* @static
* @private
* @name _runTest
*/
_runTest : function (node /*:TestNode*/) /*:Void*/ {
//get relevant information
var testName /*:String*/ = node.testObject;
var testCase /*:YAHOO.tool.TestCase*/ = node.parent.testObject;
var test /*:Function*/ = testCase[testName];
//get the "should" test cases
var shouldIgnore /*:Object*/ = (testCase._should.ignore || {})[testName];
//figure out if the test should be ignored or not
if (shouldIgnore){
//update results
node.parent.results[testName] = {
result: "ignore",
message: "Test ignored",
type: "test",
name: testName
};
node.parent.results.ignored++;
node.parent.results.total++;
this.fireEvent(this.TEST_IGNORE_EVENT, { testCase: testCase, testName: testName });
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
setTimeout(function(){
YAHOO.tool.TestRunner._run();
}, 0);
} else {
this._run();
}
} else {
//run the setup
testCase.setUp();
//now call the body of the test
this._resumeTest(test);
}
},
//-------------------------------------------------------------------------
// Protected Methods
//-------------------------------------------------------------------------
/**
* Fires events for the TestRunner. This overrides the default fireEvent()
* method from EventProvider to add the type property to the data that is
* passed through on each event call.
* @param {String} type The type of event to fire.
* @param {Object} data (Optional) Data for the event.
* @method fireEvent
* @static
* @protected
*/
fireEvent : function (type /*:String*/, data /*:Object*/) /*:Void*/ {
data = data || {};
data.type = type;
TestRunner.superclass.fireEvent.call(this, type, data);
},
//-------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------
/**
* Adds a test suite or test case to the list of test objects to run.
* @param testObject Either a TestCase or a TestSuite that should be run.
* @return {Void}
* @method add
* @static
*/
add : function (testObject /*:Object*/) /*:Void*/ {
this.masterSuite.add(testObject);
},
/**
* Removes all test objects from the runner.
* @return {Void}
* @method clear
* @static
*/
clear : function () /*:Void*/ {
this.masterSuite.items = [];
},
/**
* Resumes the TestRunner after wait() was called.
* @param {Function} segment The function to run as the rest
* of the haulted test.
* @return {Void}
* @method resume
* @static
*/
resume : function (segment /*:Function*/) /*:Void*/ {
this._resumeTest(segment || function(){});
},
/**
* Runs the test suite.
* @return {Void}
* @method run
* @static
*/
run : function (testObject /*:Object*/) /*:Void*/ {
//pointer to runner to avoid scope issues
var runner = YAHOO.tool.TestRunner;
//build the test tree
runner._buildTestTree();
//set when the test started
runner._root.results.duration = (new Date()).getTime();
//fire the begin event
runner.fireEvent(runner.BEGIN_EVENT);
//begin the testing
runner._run();
}
});
return new TestRunner();
})();
YAHOO.namespace("util");
//-----------------------------------------------------------------------------
// Assert object
//-----------------------------------------------------------------------------
/**
* The Assert object provides functions to test JavaScript values against
* known and expected results. Whenever a comparison (assertion) fails,
* an error is thrown.
*
* @namespace YAHOO.util
* @class Assert
* @static
*/
YAHOO.util.Assert = {
//-------------------------------------------------------------------------
// Helper Methods
//-------------------------------------------------------------------------
/**
* Formats a message so that it can contain the original assertion message
* in addition to the custom message.
* @param {String} customMessage The message passed in by the developer.
* @param {String} defaultMessage The message created by the error by default.
* @return {String} The final error message, containing either or both.
* @protected
* @static
* @method _formatMessage
*/
_formatMessage : function (customMessage /*:String*/, defaultMessage /*:String*/) /*:String*/ {
var message = customMessage;
if (YAHOO.lang.isString(customMessage) && customMessage.length > 0){
return YAHOO.lang.substitute(customMessage, { message: defaultMessage });
} else {
return defaultMessage;
}
},
//-------------------------------------------------------------------------
// Generic Assertion Methods
//-------------------------------------------------------------------------
/**
* Forces an assertion error to occur.
* @param {String} message (Optional) The message to display with the failure.
* @method fail
* @static
*/
fail : function (message /*:String*/) /*:Void*/ {
throw new YAHOO.util.AssertionError(this._formatMessage(message, "Test force-failed."));
},
//-------------------------------------------------------------------------
// Equality Assertion Methods
//-------------------------------------------------------------------------
/**
* Asserts that a value is equal to another. This uses the double equals sign
* so type cohersion may occur.
* @param {Object} expected The expected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areEqual
* @static
*/
areEqual : function (expected /*:Object*/, actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (expected != actual) {
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Values should be equal."), expected, actual);
}
},
/**
* Asserts that a value is not equal to another. This uses the double equals sign
* so type cohersion may occur.
* @param {Object} unexpected The unexpected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areNotEqual
* @static
*/
areNotEqual : function (unexpected /*:Object*/, actual /*:Object*/,
message /*:String*/) /*:Void*/ {
if (unexpected == actual) {
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Values should not be equal."), unexpected);
}
},
/**
* Asserts that a value is not the same as another. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} unexpected The unexpected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areNotSame
* @static
*/
areNotSame : function (unexpected /*:Object*/, actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (unexpected === actual) {
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Values should not be the same."), unexpected);
}
},
/**
* Asserts that a value is the same as another. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} expected The expected value.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areSame
* @static
*/
areSame : function (expected /*:Object*/, actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (expected !== actual) {
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Values should be the same."), expected, actual);
}
},
//-------------------------------------------------------------------------
// Boolean Assertion Methods
//-------------------------------------------------------------------------
/**
* Asserts that a value is false. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isFalse
* @static
*/
isFalse : function (actual /*:Boolean*/, message /*:String*/) {
if (false !== actual) {
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be false."), false, actual);
}
},
/**
* Asserts that a value is true. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isTrue
* @static
*/
isTrue : function (actual /*:Boolean*/, message /*:String*/) /*:Void*/ {
if (true !== actual) {
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be true."), true, actual);
}
},
//-------------------------------------------------------------------------
// Special Value Assertion Methods
//-------------------------------------------------------------------------
/**
* Asserts that a value is not a number.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNaN
* @static
*/
isNaN : function (actual /*:Object*/, message /*:String*/) /*:Void*/{
if (!isNaN(actual)){
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be NaN."), NaN, actual);
}
},
/**
* Asserts that a value is not the special NaN value.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNotNaN
* @static
*/
isNotNaN : function (actual /*:Object*/, message /*:String*/) /*:Void*/{
if (isNaN(actual)){
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Values should not be NaN."), NaN);
}
},
/**
* Asserts that a value is not null. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNotNull
* @static
*/
isNotNull : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (YAHOO.lang.isNull(actual)) {
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Values should not be null."), null);
}
},
/**
* Asserts that a value is not undefined. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNotUndefined
* @static
*/
isNotUndefined : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (YAHOO.lang.isUndefined(actual)) {
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should not be undefined."), undefined);
}
},
/**
* Asserts that a value is null. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNull
* @static
*/
isNull : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.isNull(actual)) {
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be null."), null, actual);
}
},
/**
* Asserts that a value is undefined. This uses the triple equals sign
* so no type cohersion may occur.
* @param {Object} actual The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isUndefined
* @static
*/
isUndefined : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.isUndefined(actual)) {
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be undefined."), undefined, actual);
}
},
//--------------------------------------------------------------------------
// Instance Assertion Methods
//--------------------------------------------------------------------------
/**
* Asserts that a value is an array.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isArray
* @static
*/
isArray : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.isArray(actual)){
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be an array."), actual);
}
},
/**
* Asserts that a value is a Boolean.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isBoolean
* @static
*/
isBoolean : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.isBoolean(actual)){
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be a Boolean."), actual);
}
},
/**
* Asserts that a value is a function.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isFunction
* @static
*/
isFunction : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.isFunction(actual)){
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be a function."), actual);
}
},
/**
* Asserts that a value is an instance of a particular object. This may return
* incorrect results when comparing objects from one frame to constructors in
* another frame. For best results, don't use in a cross-frame manner.
* @param {Function} expected The function that the object should be an instance of.
* @param {Object} actual The object to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isInstanceOf
* @static
*/
isInstanceOf : function (expected /*:Function*/, actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!(actual instanceof expected)){
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value isn't an instance of expected type."), expected, actual);
}
},
/**
* Asserts that a value is a number.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNumber
* @static
*/
isNumber : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.isNumber(actual)){
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be a number."), actual);
}
},
/**
* Asserts that a value is an object.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isObject
* @static
*/
isObject : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.isObject(actual)){
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be an object."), actual);
}
},
/**
* Asserts that a value is a string.
* @param {Object} actual The value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isString
* @static
*/
isString : function (actual /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.isString(actual)){
throw new YAHOO.util.UnexpectedValue(this._formatMessage(message, "Value should be a string."), actual);
}
},
/**
* Asserts that a value is of a particular type.
* @param {String} expectedType The expected type of the variable.
* @param {Object} actualValue The actual value to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isTypeOf
* @static
*/
isTypeOf : function (expected /*:String*/, actual /*:Object*/, message /*:String*/) /*:Void*/{
if (typeof actual != expected){
throw new YAHOO.util.ComparisonFailure(this._formatMessage(message, "Value should be of type " + expected + "."), expected, typeof actual);
}
}
};
//-----------------------------------------------------------------------------
// Assertion errors
//-----------------------------------------------------------------------------
/**
* AssertionError is thrown whenever an assertion fails. It provides methods
* to more easily get at error information and also provides a base class
* from which more specific assertion errors can be derived.
*
* @param {String} message The message to display when the error occurs.
* @namespace YAHOO.util
* @class AssertionError
* @extends Error
* @constructor
*/
YAHOO.util.AssertionError = function (message /*:String*/){
//call superclass
//arguments.callee.superclass.constructor.call(this, message);
/*
* Error message. Must be duplicated to ensure browser receives it.
* @type String
* @property message
*/
this.message /*:String*/ = message;
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name /*:String*/ = "AssertionError";
};
//inherit methods
YAHOO.lang.extend(YAHOO.util.AssertionError, Object, {
/**
* Returns a fully formatted error for an assertion failure. This should
* be overridden by all subclasses to provide specific information.
* @method getMessage
* @return {String} A string describing the error.
*/
getMessage : function () /*:String*/ {
return this.message;
},
/**
* Returns a string representation of the error.
* @method toString
* @return {String} A string representation of the error.
*/
toString : function () /*:String*/ {
return this.name + ": " + this.getMessage();
}
});
/**
* ComparisonFailure is subclass of AssertionError that is thrown whenever
* a comparison between two values fails. It provides mechanisms to retrieve
* both the expected and actual value.
*
* @param {String} message The message to display when the error occurs.
* @param {Object} expected The expected value.
* @param {Object} actual The actual value that caused the assertion to fail.
* @namespace YAHOO.util
* @extends YAHOO.util.AssertionError
* @class ComparisonFailure
* @constructor
*/
YAHOO.util.ComparisonFailure = function (message /*:String*/, expected /*:Object*/, actual /*:Object*/){
//call superclass
YAHOO.util.AssertionError.call(this, message);
/**
* The expected value.
* @type Object
* @property expected
*/
this.expected /*:Object*/ = expected;
/**
* The actual value.
* @type Object
* @property actual
*/
this.actual /*:Object*/ = actual;
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name /*:String*/ = "ComparisonFailure";
};
//inherit methods
YAHOO.lang.extend(YAHOO.util.ComparisonFailure, YAHOO.util.AssertionError, {
/**
* Returns a fully formatted error for an assertion failure. This message
* provides information about the expected and actual values.
* @method toString
* @return {String} A string describing the error.
*/
getMessage : function () /*:String*/ {
return this.message + "\nExpected: " + this.expected + " (" + (typeof this.expected) + ")" +
"\nActual:" + this.actual + " (" + (typeof this.actual) + ")";
}
});
/**
* UnexpectedValue is subclass of AssertionError that is thrown whenever
* a value was unexpected in its scope. This typically means that a test
* was performed to determine that a value was *not* equal to a certain
* value.
*
* @param {String} message The message to display when the error occurs.
* @param {Object} unexpected The unexpected value.
* @namespace YAHOO.util
* @extends YAHOO.util.AssertionError
* @class UnexpectedValue
* @constructor
*/
YAHOO.util.UnexpectedValue = function (message /*:String*/, unexpected /*:Object*/){
//call superclass
YAHOO.util.AssertionError.call(this, message);
/**
* The unexpected value.
* @type Object
* @property unexpected
*/
this.unexpected /*:Object*/ = unexpected;
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name /*:String*/ = "UnexpectedValue";
};
//inherit methods
YAHOO.lang.extend(YAHOO.util.UnexpectedValue, YAHOO.util.AssertionError, {
/**
* Returns a fully formatted error for an assertion failure. The message
* contains information about the unexpected value that was encountered.
* @method getMessage
* @return {String} A string describing the error.
*/
getMessage : function () /*:String*/ {
return this.message + "\nUnexpected: " + this.unexpected + " (" + (typeof this.unexpected) + ") ";
}
});
/**
* ShouldFail is subclass of AssertionError that is thrown whenever
* a test was expected to fail but did not.
*
* @param {String} message The message to display when the error occurs.
* @namespace YAHOO.util
* @extends YAHOO.util.AssertionError
* @class ShouldFail
* @constructor
*/
YAHOO.util.ShouldFail = function (message /*:String*/){
//call superclass
YAHOO.util.AssertionError.call(this, message || "This test should fail but didn't.");
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name /*:String*/ = "ShouldFail";
};
//inherit methods
YAHOO.lang.extend(YAHOO.util.ShouldFail, YAHOO.util.AssertionError);
/**
* ShouldError is subclass of AssertionError that is thrown whenever
* a test is expected to throw an error but doesn't.
*
* @param {String} message The message to display when the error occurs.
* @namespace YAHOO.util
* @extends YAHOO.util.AssertionError
* @class ShouldError
* @constructor
*/
YAHOO.util.ShouldError = function (message /*:String*/){
//call superclass
YAHOO.util.AssertionError.call(this, message || "This test should have thrown an error but didn't.");
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name /*:String*/ = "ShouldError";
};
//inherit methods
YAHOO.lang.extend(YAHOO.util.ShouldError, YAHOO.util.AssertionError);
/**
* UnexpectedError is subclass of AssertionError that is thrown whenever
* an error occurs within the course of a test and the test was not expected
* to throw an error.
*
* @param {Error} cause The unexpected error that caused this error to be
* thrown.
* @namespace YAHOO.util
* @extends YAHOO.util.AssertionError
* @class UnexpectedError
* @constructor
*/
YAHOO.util.UnexpectedError = function (cause /*:Object*/){
//call superclass
YAHOO.util.AssertionError.call(this, "Unexpected error: " + cause.message);
/**
* The unexpected error that occurred.
* @type Error
* @property cause
*/
this.cause /*:Error*/ = cause;
/**
* The name of the error that occurred.
* @type String
* @property name
*/
this.name /*:String*/ = "UnexpectedError";
/**
* Stack information for the error (if provided).
* @type String
* @property stack
*/
this.stack /*:String*/ = cause.stack;
};
//inherit methods
YAHOO.lang.extend(YAHOO.util.UnexpectedError, YAHOO.util.AssertionError);
//-----------------------------------------------------------------------------
// ArrayAssert object
//-----------------------------------------------------------------------------
/**
* The ArrayAssert object provides functions to test JavaScript array objects
* for a variety of cases.
*
* @namespace YAHOO.util
* @class ArrayAssert
* @static
*/
YAHOO.util.ArrayAssert = {
/**
* Asserts that a value is present in an array. This uses the triple equals
* sign so no type cohersion may occur.
* @param {Object} needle The value that is expected in the array.
* @param {Array} haystack An array of values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method contains
* @static
*/
contains : function (needle /*:Object*/, haystack /*:Array*/,
message /*:String*/) /*:Void*/ {
var found /*:Boolean*/ = false;
var Assert = YAHOO.util.Assert;
//begin checking values
for (var i=0; i < haystack.length && !found; i++){
if (haystack[i] === needle) {
found = true;
}
}
if (!found){
Assert.fail(Assert._formatMessage(message, "Value " + needle + " (" + (typeof needle) + ") not found in array [" + haystack + "]."));
}
},
/**
* Asserts that a set of values are present in an array. This uses the triple equals
* sign so no type cohersion may occur. For this assertion to pass, all values must
* be found.
* @param {Object[]} needles An array of values that are expected in the array.
* @param {Array} haystack An array of values to check.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method containsItems
* @static
*/
containsItems : function (needles /*:Object[]*/, haystack /*:Array*/,
message /*:String*/) /*:Void*/ {
//begin checking values
for (var i=0; i < needles.length; i++){
this.contains(needles[i], haystack, message);
}
},
/**
* Asserts that a value matching some condition is present in an array. This uses
* a function to determine a match.
* @param {Function} matcher A function that returns true if the items matches or false if not.
* @param {Array} haystack An array of values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method containsMatch
* @static
*/
containsMatch : function (matcher /*:Function*/, haystack /*:Array*/,
message /*:String*/) /*:Void*/ {
//check for valid matcher
if (typeof matcher != "function"){
throw new TypeError("ArrayAssert.containsMatch(): First argument must be a function.");
}
var found /*:Boolean*/ = false;
var Assert = YAHOO.util.Assert;
//begin checking values
for (var i=0; i < haystack.length && !found; i++){
if (matcher(haystack[i])) {
found = true;
}
}
if (!found){
Assert.fail(Assert._formatMessage(message, "No match found in array [" + haystack + "]."));
}
},
/**
* Asserts that a value is not present in an array. This uses the triple equals
* sign so no type cohersion may occur.
* @param {Object} needle The value that is expected in the array.
* @param {Array} haystack An array of values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method doesNotContain
* @static
*/
doesNotContain : function (needle /*:Object*/, haystack /*:Array*/,
message /*:String*/) /*:Void*/ {
var found /*:Boolean*/ = false;
var Assert = YAHOO.util.Assert;
//begin checking values
for (var i=0; i < haystack.length && !found; i++){
if (haystack[i] === needle) {
found = true;
}
}
if (found){
Assert.fail(Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
}
},
/**
* Asserts that a set of values are not present in an array. This uses the triple equals
* sign so no type cohersion may occur. For this assertion to pass, all values must
* not be found.
* @param {Object[]} needles An array of values that are not expected in the array.
* @param {Array} haystack An array of values to check.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method doesNotContainItems
* @static
*/
doesNotContainItems : function (needles /*:Object[]*/, haystack /*:Array*/,
message /*:String*/) /*:Void*/ {
for (var i=0; i < needles.length; i++){
this.doesNotContain(needles[i], haystack, message);
}
},
/**
* Asserts that no values matching a condition are present in an array. This uses
* a function to determine a match.
* @param {Function} matcher A function that returns true if the items matches or false if not.
* @param {Array} haystack An array of values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method doesNotContainMatch
* @static
*/
doesNotContainMatch : function (matcher /*:Function*/, haystack /*:Array*/,
message /*:String*/) /*:Void*/ {
//check for valid matcher
if (typeof matcher != "function"){
throw new TypeError("ArrayAssert.doesNotContainMatch(): First argument must be a function.");
}
var found /*:Boolean*/ = false;
var Assert = YAHOO.util.Assert;
//begin checking values
for (var i=0; i < haystack.length && !found; i++){
if (matcher(haystack[i])) {
found = true;
}
}
if (found){
Assert.fail(Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
}
},
/**
* Asserts that the given value is contained in an array at the specified index.
* This uses the triple equals sign so no type cohersion will occur.
* @param {Object} needle The value to look for.
* @param {Array} haystack The array to search in.
* @param {int} index The index at which the value should exist.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method indexOf
* @static
*/
indexOf : function (needle /*:Object*/, haystack /*:Array*/, index /*:int*/, message /*:String*/) /*:Void*/ {
//try to find the value in the array
for (var i=0; i < haystack.length; i++){
if (haystack[i] === needle){
YAHOO.util.Assert.areEqual(index, i, message || "Value exists at index " + i + " but should be at index " + index + ".");
return;
}
}
var Assert = YAHOO.util.Assert;
//if it makes it here, it wasn't found at all
Assert.fail(Assert._formatMessage(message, "Value doesn't exist in array [" + haystack + "]."));
},
/**
* Asserts that the values in an array are equal, and in the same position,
* as values in another array. This uses the double equals sign
* so type cohersion may occur. Note that the array objects themselves
* need not be the same for this test to pass.
* @param {Array} expected An array of the expected values.
* @param {Array} actual Any array of the actual values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method itemsAreEqual
* @static
*/
itemsAreEqual : function (expected /*:Array*/, actual /*:Array*/,
message /*:String*/) /*:Void*/ {
//one may be longer than the other, so get the maximum length
var len /*:int*/ = Math.max(expected.length, actual.length || 0);
var Assert = YAHOO.util.Assert;
//begin checking values
for (var i=0; i < len; i++){
Assert.areEqual(expected[i], actual[i],
Assert._formatMessage(message, "Values in position " + i + " are not equal."));
}
},
/**
* Asserts that the values in an array are equivalent, and in the same position,
* as values in another array. This uses a function to determine if the values
* are equivalent. Note that the array objects themselves
* need not be the same for this test to pass.
* @param {Array} expected An array of the expected values.
* @param {Array} actual Any array of the actual values.
* @param {Function} comparator A function that returns true if the values are equivalent
* or false if not.
* @param {String} message (Optional) The message to display if the assertion fails.
* @return {Void}
* @method itemsAreEquivalent
* @static
*/
itemsAreEquivalent : function (expected /*:Array*/, actual /*:Array*/,
comparator /*:Function*/, message /*:String*/) /*:Void*/ {
//make sure the comparator is valid
if (typeof comparator != "function"){
throw new TypeError("ArrayAssert.itemsAreEquivalent(): Third argument must be a function.");
}
//one may be longer than the other, so get the maximum length
var len /*:int*/ = Math.max(expected.length, actual.length || 0);
//begin checking values
for (var i=0; i < len; i++){
if (!comparator(expected[i], actual[i])){
throw new YAHOO.util.ComparisonFailure(YAHOO.util.Assert._formatMessage(message, "Values in position " + i + " are not equivalent."), expected[i], actual[i]);
}
}
},
/**
* Asserts that an array is empty.
* @param {Array} actual The array to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isEmpty
* @static
*/
isEmpty : function (actual /*:Array*/, message /*:String*/) /*:Void*/ {
if (actual.length > 0){
var Assert = YAHOO.util.Assert;
Assert.fail(Assert._formatMessage(message, "Array should be empty."));
}
},
/**
* Asserts that an array is not empty.
* @param {Array} actual The array to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method isNotEmpty
* @static
*/
isNotEmpty : function (actual /*:Array*/, message /*:String*/) /*:Void*/ {
if (actual.length === 0){
var Assert = YAHOO.util.Assert;
Assert.fail(Assert._formatMessage(message, "Array should not be empty."));
}
},
/**
* Asserts that the values in an array are the same, and in the same position,
* as values in another array. This uses the triple equals sign
* so no type cohersion will occur. Note that the array objects themselves
* need not be the same for this test to pass.
* @param {Array} expected An array of the expected values.
* @param {Array} actual Any array of the actual values.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method itemsAreSame
* @static
*/
itemsAreSame : function (expected /*:Array*/, actual /*:Array*/,
message /*:String*/) /*:Void*/ {
//one may be longer than the other, so get the maximum length
var len /*:int*/ = Math.max(expected.length, actual.length || 0);
var Assert = YAHOO.util.Assert;
//begin checking values
for (var i=0; i < len; i++){
Assert.areSame(expected[i], actual[i],
Assert._formatMessage(message, "Values in position " + i + " are not the same."));
}
},
/**
* Asserts that the given value is contained in an array at the specified index,
* starting from the back of the array.
* This uses the triple equals sign so no type cohersion will occur.
* @param {Object} needle The value to look for.
* @param {Array} haystack The array to search in.
* @param {int} index The index at which the value should exist.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method lastIndexOf
* @static
*/
lastIndexOf : function (needle /*:Object*/, haystack /*:Array*/, index /*:int*/, message /*:String*/) /*:Void*/ {
var Assert = YAHOO.util.Assert;
//try to find the value in the array
for (var i=haystack.length; i >= 0; i--){
if (haystack[i] === needle){
Assert.areEqual(index, i, Assert._formatMessage(message, "Value exists at index " + i + " but should be at index " + index + "."));
return;
}
}
//if it makes it here, it wasn't found at all
Assert.fail(Assert._formatMessage(message, "Value doesn't exist in array."));
}
};
YAHOO.namespace("util");
//-----------------------------------------------------------------------------
// ObjectAssert object
//-----------------------------------------------------------------------------
/**
* The ObjectAssert object provides functions to test JavaScript objects
* for a variety of cases.
*
* @namespace YAHOO.util
* @class ObjectAssert
* @static
*/
YAHOO.util.ObjectAssert = {
/**
* Asserts that all properties in the object exist in another object.
* @param {Object} expected An object with the expected properties.
* @param {Object} actual An object with the actual properties.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method propertiesAreEqual
* @static
*/
propertiesAreEqual : function (expected /*:Object*/, actual /*:Object*/,
message /*:String*/) /*:Void*/ {
var Assert = YAHOO.util.Assert;
//get all properties in the object
var properties /*:Array*/ = [];
for (var property in expected){
properties.push(property);
}
//see if the properties are in the expected object
for (var i=0; i < properties.length; i++){
Assert.isNotUndefined(actual[properties[i]],
Assert._formatMessage(message, "Property '" + properties[i] + "' expected."));
}
},
/**
* Asserts that an object has a property with the given name.
* @param {String} propertyName The name of the property to test.
* @param {Object} object The object to search.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method hasProperty
* @static
*/
hasProperty : function (propertyName /*:String*/, object /*:Object*/, message /*:String*/) /*:Void*/ {
if (!(propertyName in object)){
var Assert = YAHOO.util.Assert;
Assert.fail(Assert._formatMessage(message, "Property '" + propertyName + "' not found on object."));
}
},
/**
* Asserts that a property with the given name exists on an object instance (not on its prototype).
* @param {String} propertyName The name of the property to test.
* @param {Object} object The object to search.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method hasProperty
* @static
*/
hasOwnProperty : function (propertyName /*:String*/, object /*:Object*/, message /*:String*/) /*:Void*/ {
if (!YAHOO.lang.hasOwnProperty(object, propertyName)){
var Assert = YAHOO.util.Assert;
Assert.fail(Assert._formatMessage(message, "Property '" + propertyName + "' not found on object instance."));
}
}
};
//-----------------------------------------------------------------------------
// DateAssert object
//-----------------------------------------------------------------------------
/**
* The DateAssert object provides functions to test JavaScript Date objects
* for a variety of cases.
*
* @namespace YAHOO.util
* @class DateAssert
* @static
*/
YAHOO.util.DateAssert = {
/**
* Asserts that a date's month, day, and year are equal to another date's.
* @param {Date} expected The expected date.
* @param {Date} actual The actual date to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method datesAreEqual
* @static
*/
datesAreEqual : function (expected /*:Date*/, actual /*:Date*/, message /*:String*/){
if (expected instanceof Date && actual instanceof Date){
var Assert = YAHOO.util.Assert;
Assert.areEqual(expected.getFullYear(), actual.getFullYear(), Assert._formatMessage(message, "Years should be equal."));
Assert.areEqual(expected.getMonth(), actual.getMonth(), Assert._formatMessage(message, "Months should be equal."));
Assert.areEqual(expected.getDate(), actual.getDate(), Assert._formatMessage(message, "Day of month should be equal."));
} else {
throw new TypeError("DateAssert.datesAreEqual(): Expected and actual values must be Date objects.");
}
},
/**
* Asserts that a date's hour, minutes, and seconds are equal to another date's.
* @param {Date} expected The expected date.
* @param {Date} actual The actual date to test.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method timesAreEqual
* @static
*/
timesAreEqual : function (expected /*:Date*/, actual /*:Date*/, message /*:String*/){
if (expected instanceof Date && actual instanceof Date){
var Assert = YAHOO.util.Assert;
Assert.areEqual(expected.getHours(), actual.getHours(), Assert._formatMessage(message, "Hours should be equal."));
Assert.areEqual(expected.getMinutes(), actual.getMinutes(), Assert._formatMessage(message, "Minutes should be equal."));
Assert.areEqual(expected.getSeconds(), actual.getSeconds(), Assert._formatMessage(message, "Seconds should be equal."));
} else {
throw new TypeError("DateAssert.timesAreEqual(): Expected and actual values must be Date objects.");
}
}
};
YAHOO.namespace("tool");
//-----------------------------------------------------------------------------
// TestManager object
//-----------------------------------------------------------------------------
/**
* Runs pages containing test suite definitions.
* @namespace YAHOO.tool
* @class TestManager
* @static
*/
YAHOO.tool.TestManager = {
/**
* Constant for the testpagebegin custom event
* @property TEST_PAGE_BEGIN_EVENT
* @static
* @type string
* @final
*/
TEST_PAGE_BEGIN_EVENT /*:String*/ : "testpagebegin",
/**
* Constant for the testpagecomplete custom event
* @property TEST_PAGE_COMPLETE_EVENT
* @static
* @type string
* @final
*/
TEST_PAGE_COMPLETE_EVENT /*:String*/ : "testpagecomplete",
/**
* Constant for the testmanagerbegin custom event
* @property TEST_MANAGER_BEGIN_EVENT
* @static
* @type string
* @final
*/
TEST_MANAGER_BEGIN_EVENT /*:String*/ : "testmanagerbegin",
/**
* Constant for the testmanagercomplete custom event
* @property TEST_MANAGER_COMPLETE_EVENT
* @static
* @type string
* @final
*/
TEST_MANAGER_COMPLETE_EVENT /*:String*/ : "testmanagercomplete",
//-------------------------------------------------------------------------
// Private Properties
//-------------------------------------------------------------------------
/**
* The URL of the page currently being executed.
* @type String
* @private
* @property _curPage
* @static
*/
_curPage /*:String*/ : null,
/**
* The frame used to load and run tests.
* @type Window
* @private
* @property _frame
* @static
*/
_frame /*:Window*/ : null,
/**
* The logger used to output results from the various tests.
* @type YAHOO.tool.TestLogger
* @private
* @property _logger
* @static
*/
_logger : null,
/**
* The timeout ID for the next iteration through the tests.
* @type int
* @private
* @property _timeoutId
* @static
*/
_timeoutId /*:int*/ : 0,
/**
* Array of pages to load.
* @type String[]
* @private
* @property _pages
* @static
*/
_pages /*:String[]*/ : [],
/**
* Aggregated results
* @type Object
* @private
* @property _results
* @static
*/
_results: null,
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/**
* Handles TestRunner.COMPLETE_EVENT, storing the results and beginning
* the loop again.
* @param {Object} data Data about the event.
* @return {Void}
* @private
* @static
*/
_handleTestRunnerComplete : function (data /*:Object*/) /*:Void*/ {
this.fireEvent(this.TEST_PAGE_COMPLETE_EVENT, {
page: this._curPage,
results: data.results
});
//save results
//this._results[this.curPage] = data.results;
//process 'em
this._processResults(this._curPage, data.results);
this._logger.clearTestRunner();
//if there's more to do, set a timeout to begin again
if (this._pages.length){
this._timeoutId = setTimeout(function(){
YAHOO.tool.TestManager._run();
}, 1000);
} else {
this.fireEvent(this.TEST_MANAGER_COMPLETE_EVENT, this._results);
}
},
/**
* Processes the results of a test page run, outputting log messages
* for failed tests.
* @return {Void}
* @private
* @static
*/
_processResults : function (page /*:String*/, results /*:Object*/) /*:Void*/ {
var r = this._results;
r.passed += results.passed;
r.failed += results.failed;
r.ignored += results.ignored;
r.total += results.total;
r.duration += results.duration;
if (results.failed){
r.failedPages.push(page);
} else {
r.passedPages.push(page);
}
results.name = page;
results.type = "page";
r[page] = results;
},
/**
* Loads the next test page into the iframe.
* @return {Void}
* @static
* @private
*/
_run : function () /*:Void*/ {
//set the current page
this._curPage = this._pages.shift();
this.fireEvent(this.TEST_PAGE_BEGIN_EVENT, this._curPage);
//load the frame - destroy history in case there are other iframes that
//need testing
this._frame.location.replace(this._curPage);
},
//-------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------
/**
* Signals that a test page has been loaded. This should be called from
* within the test page itself to notify the TestManager that it is ready.
* @return {Void}
* @static
*/
load : function () /*:Void*/ {
if (parent.YAHOO.tool.TestManager !== this){
parent.YAHOO.tool.TestManager.load();
} else {
if (this._frame) {
//assign event handling
var TestRunner = this._frame.YAHOO.tool.TestRunner;
this._logger.setTestRunner(TestRunner);
TestRunner.subscribe(TestRunner.COMPLETE_EVENT, this._handleTestRunnerComplete, this, true);
//run it
TestRunner.run();
}
}
},
/**
* Sets the pages to be loaded.
* @param {String[]} pages An array of URLs to load.
* @return {Void}
* @static
*/
setPages : function (pages /*:String[]*/) /*:Void*/ {
this._pages = pages;
},
/**
* Begins the process of running the tests.
* @return {Void}
* @static
*/
start : function () /*:Void*/ {
if (!this._initialized) {
/**
* Fires when loading a test page
* @event testpagebegin
* @param curPage {string} the page being loaded
* @static
*/
this.createEvent(this.TEST_PAGE_BEGIN_EVENT);
/**
* Fires when a test page is complete
* @event testpagecomplete
* @param obj {page: string, results: object} the name of the
* page that was loaded, and the test suite results
* @static
*/
this.createEvent(this.TEST_PAGE_COMPLETE_EVENT);
/**
* Fires when the test manager starts running all test pages
* @event testmanagerbegin
* @static
*/
this.createEvent(this.TEST_MANAGER_BEGIN_EVENT);
/**
* Fires when the test manager finishes running all test pages. External
* test runners should subscribe to this event in order to get the
* aggregated test results.
* @event testmanagercomplete
* @param obj { pages_passed: int, pages_failed: int, tests_passed: int
* tests_failed: int, passed: string[], failed: string[],
* page_results: {} }
* @static
*/
this.createEvent(this.TEST_MANAGER_COMPLETE_EVENT);
//create iframe if not already available
if (!this._frame){
var frame /*:HTMLElement*/ = document.createElement("iframe");
frame.style.visibility = "hidden";
frame.style.position = "absolute";
document.body.appendChild(frame);
this._frame = frame.contentWindow || frame.contentDocument.parentWindow;
}
//create test logger if not already available
if (!this._logger){
this._logger = new YAHOO.tool.TestLogger();
}
this._initialized = true;
}
// reset the results cache
this._results = {
passed: 0,
failed: 0,
ignored: 0,
total: 0,
type: "report",
name: "YUI Test Results",
duration: 0,
failedPages:[],
passedPages:[]
/*
// number of pages that pass
pages_passed: 0,
// number of pages that fail
pages_failed: 0,
// total number of tests passed
tests_passed: 0,
// total number of tests failed
tests_failed: 0,
// array of pages that passed
passed: [],
// array of pages that failed
failed: [],
// map of full results for each page
page_results: {}*/
};
this.fireEvent(this.TEST_MANAGER_BEGIN_EVENT, null);
this._run();
},
/**
* Stops the execution of tests.
* @return {Void}
* @static
*/
stop : function () /*:Void*/ {
clearTimeout(this._timeoutId);
}
};
YAHOO.lang.augmentObject(YAHOO.tool.TestManager, YAHOO.util.EventProvider.prototype);
YAHOO.namespace("tool");
//-----------------------------------------------------------------------------
// TestLogger object
//-----------------------------------------------------------------------------
/**
* Displays test execution progress and results, providing filters based on
* different key events.
* @namespace YAHOO.tool
* @class TestLogger
* @constructor
* @param {HTMLElement} element (Optional) The element to create the logger in.
* @param {Object} config (Optional) Configuration options for the logger.
*/
YAHOO.tool.TestLogger = function (element, config) {
YAHOO.tool.TestLogger.superclass.constructor.call(this, element, config);
this.init();
};
YAHOO.lang.extend(YAHOO.tool.TestLogger, YAHOO.widget.LogReader, {
footerEnabled : true,
newestOnTop : false,
/**
* Formats message string to HTML for output to console.
* @private
* @method formatMsg
* @param oLogMsg {Object} Log message object.
* @return {String} HTML-formatted message for output to console.
*/
formatMsg : function(message /*:Object*/) {
var category /*:String*/ = message.category;
var text /*:String*/ = this.html2Text(message.msg);
return "<pre><p><span class=\"" + category + "\">" + category.toUpperCase() + "</span> " + text + "</p></pre>";
},
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/*
* Initializes the logger.
* @private
*/
init : function () {
//attach to any available TestRunner
if (YAHOO.tool.TestRunner){
this.setTestRunner(YAHOO.tool.TestRunner);
}
//hide useless sources
this.hideSource("global");
this.hideSource("LogReader");
//hide useless message categories
this.hideCategory("warn");
this.hideCategory("window");
this.hideCategory("time");
//reset the logger
this.clearConsole();
},
/**
* Clears the reference to the TestRunner from previous operations. This
* unsubscribes all events and removes the object reference.
* @return {Void}
* @static
*/
clearTestRunner : function () /*:Void*/ {
if (this._runner){
this._runner.unsubscribeAll();
this._runner = null;
}
},
/**
* Sets the source test runner that the logger should monitor.
* @param {YAHOO.tool.TestRunner} testRunner The TestRunner to observe.
* @return {Void}
* @static
*/
setTestRunner : function (testRunner /*:YAHOO.tool.TestRunner*/) /*:Void*/ {
if (this._runner){
this.clearTestRunner();
}
this._runner = testRunner;
//setup event _handlers
testRunner.subscribe(testRunner.TEST_PASS_EVENT, this._handleTestRunnerEvent, this, true);
testRunner.subscribe(testRunner.TEST_FAIL_EVENT, this._handleTestRunnerEvent, this, true);
testRunner.subscribe(testRunner.TEST_IGNORE_EVENT, this._handleTestRunnerEvent, this, true);
testRunner.subscribe(testRunner.BEGIN_EVENT, this._handleTestRunnerEvent, this, true);
testRunner.subscribe(testRunner.COMPLETE_EVENT, this._handleTestRunnerEvent, this, true);
testRunner.subscribe(testRunner.TEST_SUITE_BEGIN_EVENT, this._handleTestRunnerEvent, this, true);
testRunner.subscribe(testRunner.TEST_SUITE_COMPLETE_EVENT, this._handleTestRunnerEvent, this, true);
testRunner.subscribe(testRunner.TEST_CASE_BEGIN_EVENT, this._handleTestRunnerEvent, this, true);
testRunner.subscribe(testRunner.TEST_CASE_COMPLETE_EVENT, this._handleTestRunnerEvent, this, true);
},
//-------------------------------------------------------------------------
// Event Handlers
//-------------------------------------------------------------------------
/**
* Handles all TestRunner events, outputting appropriate data into the console.
* @param {Object} data The event data object.
* @return {Void}
* @private
*/
_handleTestRunnerEvent : function (data /*:Object*/) /*:Void*/ {
//shortcut variables
var TestRunner /*:Object*/ = YAHOO.tool.TestRunner;
//data variables
var message /*:String*/ = "";
var messageType /*:String*/ = "";
switch(data.type){
case TestRunner.BEGIN_EVENT:
message = "Testing began at " + (new Date()).toString() + ".";
messageType = "info";
break;
case TestRunner.COMPLETE_EVENT:
message = "Testing completed at " + (new Date()).toString() + ".\nPassed:" +
data.results.passed + " Failed:" + data.results.failed + " Total:" + data.results.total;
messageType = "info";
break;
case TestRunner.TEST_FAIL_EVENT:
message = data.testName + ": " + data.error.getMessage();
messageType = "fail";
break;
case TestRunner.TEST_IGNORE_EVENT:
message = data.testName + ": ignored.";
messageType = "ignore";
break;
case TestRunner.TEST_PASS_EVENT:
message = data.testName + ": passed.";
messageType = "pass";
break;
case TestRunner.TEST_SUITE_BEGIN_EVENT:
message = "Test suite \"" + data.testSuite.name + "\" started.";
messageType = "info";
break;
case TestRunner.TEST_SUITE_COMPLETE_EVENT:
message = "Test suite \"" + data.testSuite.name + "\" completed.\nPassed:" +
data.results.passed + " Failed:" + data.results.failed + " Total:" + data.results.total;
messageType = "info";
break;
case TestRunner.TEST_CASE_BEGIN_EVENT:
message = "Test case \"" + data.testCase.name + "\" started.";
messageType = "info";
break;
case TestRunner.TEST_CASE_COMPLETE_EVENT:
message = "Test case \"" + data.testCase.name + "\" completed.\nPassed:" +
data.results.passed + " Failed:" + data.results.failed + " Total:" + data.results.total;
messageType = "info";
break;
default:
message = "Unexpected event " + data.type;
message = "info";
}
YAHOO.log(message, messageType, "TestRunner");
}
});
YAHOO.namespace("tool.TestFormat");
/**
* Returns test results formatted as a JSON string. Requires JSON utility.
* @param {Object} result The results object created by TestRunner.
* @return {String} An XML-formatted string of results.
* @namespace YAHOO.tool.TestFormat
* @method JSON
* @static
*/
YAHOO.tool.TestFormat.JSON = function(results /*:Object*/) /*:String*/ {
return YAHOO.lang.JSON.stringify(results);
};
/**
* Returns test results formatted as an XML string.
* @param {Object} result The results object created by TestRunner.
* @return {String} An XML-formatted string of results.
* @namespace YAHOO.tool.TestFormat
* @method XML
* @static
*/
YAHOO.tool.TestFormat.XML = function(results /*:Object*/) /*:String*/ {
var l = YAHOO.lang;
var xml /*:String*/ = "<" + results.type + " name=\"" + results.name.replace(/"/g, """).replace(/'/g, "'") + "\"";
if (l.isNumber(results.duration)){
xml += " duration=\"" + results.duration + "\"";
}
if (results.type == "test"){
xml += " result=\"" + results.result + "\" message=\"" + results.message + "\">";
} else {
xml += " passed=\"" + results.passed + "\" failed=\"" + results.failed + "\" ignored=\"" + results.ignored + "\" total=\"" + results.total + "\">";
for (var prop in results) {
if (l.hasOwnProperty(results, prop) && l.isObject(results[prop]) && !l.isArray(results[prop])){
xml += arguments.callee(results[prop]);
}
}
}
xml += "</" + results.type + ">";
return xml;
};
YAHOO.namespace("tool");
/**
* An object capable of sending test results to a server.
* @param {String} url The URL to submit the results to.
* @param {Function} format (Optiona) A function that outputs the results in a specific format.
* Default is YAHOO.tool.TestFormat.XML.
* @constructor
* @namespace YAHOO.tool
* @class TestReporter
*/
YAHOO.tool.TestReporter = function(url /*:String*/, format /*:Function*/) {
/**
* The URL to submit the data to.
* @type String
* @property url
*/
this.url /*:String*/ = url;
/**
* The formatting function to call when submitting the data.
* @type Function
* @property format
*/
this.format /*:Function*/ = format || YAHOO.tool.TestFormat.XML;
/**
* Extra fields to submit with the request.
* @type Object
* @property _fields
* @private
*/
this._fields /*:Object*/ = new Object();
/**
* The form element used to submit the results.
* @type HTMLFormElement
* @property _form
* @private
*/
this._form /*:HTMLElement*/ = null;
/**
* Iframe used as a target for form submission.
* @type HTMLIFrameElement
* @property _iframe
* @private
*/
this._iframe /*:HTMLElement*/ = null;
};
YAHOO.tool.TestReporter.prototype = {
//restore missing constructor
constructor: YAHOO.tool.TestReporter,
/**
* Convert a date into ISO format.
* From Douglas Crockford's json2.js
* @param {Date} date The date to convert.
* @return {String} An ISO-formatted date string
* @method _convertToISOString
* @private
*/
_convertToISOString: function(date){
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
return date.getUTCFullYear() + '-' +
f(date.getUTCMonth() + 1) + '-' +
f(date.getUTCDate()) + 'T' +
f(date.getUTCHours()) + ':' +
f(date.getUTCMinutes()) + ':' +
f(date.getUTCSeconds()) + 'Z';
},
/**
* Adds a field to the form that submits the results.
* @param {String} name The name of the field.
* @param {Variant} value The value of the field.
* @return {Void}
* @method addField
*/
addField : function (name /*:String*/, value /*:Variant*/) /*:Void*/{
this._fields[name] = value;
},
/**
* Removes all previous defined fields.
* @return {Void}
* @method addField
*/
clearFields : function() /*:Void*/{
this._fields = new Object();
},
/**
* Cleans up the memory associated with the TestReporter, removing DOM elements
* that were created.
* @return {Void}
* @method destroy
*/
destroy : function() /*:Void*/ {
if (this._form){
this._form.parentNode.removeChild(this._form);
this._form = null;
}
if (this._iframe){
this._iframe.parentNode.removeChild(this._iframe);
this._iframe = null;
}
this._fields = null;
},
/**
* Sends the report to the server.
* @param {Object} results The results object created by TestRunner.
* @return {Void}
* @method report
*/
report : function(results /*:Object*/) /*:Void*/{
//if the form hasn't been created yet, create it
if (!this._form){
this._form = document.createElement("form");
this._form.method = "post";
this._form.style.visibility = "hidden";
this._form.style.position = "absolute";
this._form.style.top = 0;
document.body.appendChild(this._form);
//IE won't let you assign a name using the DOM, must do it the hacky way
if (YAHOO.env.ua.ie){
this._iframe = document.createElement("<iframe name=\"yuiTestTarget\" />");
} else {
this._iframe = document.createElement("iframe");
this._iframe.name = "yuiTestTarget";
}
this._iframe.src = "javascript:false";
this._iframe.style.visibility = "hidden";
this._iframe.style.position = "absolute";
this._iframe.style.top = 0;
document.body.appendChild(this._iframe);
this._form.target = "yuiTestTarget";
}
//set the form's action
this._form.action = this.url;
//remove any existing fields
while(this._form.hasChildNodes()){
this._form.removeChild(this._form.lastChild);
}
//create default fields
this._fields.results = this.format(results);
this._fields.useragent = navigator.userAgent;
this._fields.timestamp = this._convertToISOString(new Date());
//add fields to the form
for (var prop in this._fields){
if (YAHOO.lang.hasOwnProperty(this._fields, prop) && typeof this._fields[prop] != "function"){
if (YAHOO.env.ua.ie){
input = document.createElement("<input name=\"" + prop + "\" >");
} else {
input = document.createElement("input");
input.name = prop;
}
input.type = "hidden";
input.value = this._fields[prop];
this._form.appendChild(input);
}
}
//remove default fields
delete this._fields.results;
delete this._fields.useragent;
delete this._fields.timestamp;
if (arguments[1] !== false){
this._form.submit();
}
}
};
YAHOO.register("yuitest", YAHOO.tool.TestRunner, {version: "2.8.2r1", build: "7"});
|