jspec.js 54.9 KB
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

// JSpec - Core - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)

;(function(){

  JSpec = {
    version   : '4.3.3',
    assert    : true,
    cache     : {},
    suites    : [],
    modules   : [],
    allSuites : [],
		sharedBehaviors: [],
    matchers  : {},
    stubbed   : [],
    options   : {},
    request   : 'XMLHttpRequest' in this ? XMLHttpRequest : null,
    stats     : { specs: 0, assertions: 0, failures: 0, passes: 0, specsFinished: 0, suitesFinished: 0 },

    /**
     * Default context in which bodies are evaluated.
     *
     * Replace context simply by setting JSpec.context
     * to your own like below:
     *
     * JSpec.context = { foo : 'bar' }
     *
     * Contexts can be changed within any body, this can be useful
     * in order to provide specific helper methods to specific suites.
     *
     * To reset (usually in after hook) simply set to null like below:
     *
     * JSpec.context = null
     *
     */

     defaultContext : {
      
      /**
       * Return an object used for proxy assertions. 
       * This object is used to indicate that an object
       * should be an instance of _object_, not the constructor
       * itself.
       *
       * @param  {function} constructor
       * @return {hash}
       * @api public
       */
      
      an_instance_of : function(constructor) {
        return { an_instance_of : constructor }
      },
      
      /**
       * Load fixture at _path_.
       *
       * Fixtures are resolved as:
       *
       *  - <path>
       *  - <path>.html
       *
       * @param  {string} path
       * @return {string}
       * @api public
       */
      
      fixture : function(path) {
        if (JSpec.cache[path]) return JSpec.cache[path]
        return JSpec.cache[path] = 
          JSpec.tryLoading(JSpec.options.fixturePath + '/' + path) ||
          JSpec.tryLoading(JSpec.options.fixturePath + '/' + path + '.html')
      },
      
      /**
       * Load json fixture at _path_.
       *
       * JSON fixtures are resolved as:
       *
       *  - <path>
       *  - <path>.json
       *
       * @param  {string} path
       * @return {object}
       * @api public
       */
      
      json_fixture: function(path) {
        if (!JSpec.cache['json:' + path])
          JSpec.cache['json:' + path] =
            JSpec.tryLoading(JSpec.options.fixturePath + '/' + path) ||
            JSpec.tryLoading(JSpec.options.fixturePath + '/' + path + '.json')
        try {
          return eval('(' + JSpec.cache['json:' + path] + ')')
        } catch (e) {
          throw 'json_fixture("' + path + '"): ' + e
        }
      }
    },

    // --- Objects
    
    reporters : {
      
      /**
       * Report to server.
       * 
       * Options:
       *  - uri           specific uri to report to.
       *  - verbose       weither or not to output messages
       *  - failuresOnly  output failure messages only
       *
       * @api public
       */
      
      Server : function(results, options) {
        var uri = options.uri || 'http://' + window.location.host + '/results'
        JSpec.post(uri, {
          stats: JSpec.stats,
          options: options,
          results: map(results.allSuites, function(suite) {
            if (suite.isExecutable())
              return {
                description: suite.description,
                specs: map(suite.specs, function(spec) {
                  return {
                    description: spec.description,
                    message: !spec.passed() ? spec.failure().message : null,
                    status: spec.requiresImplementation() ? 'pending' :
                              spec.passed() ? 'pass' :
                                'fail',
                    assertions: map(spec.assertions, function(assertion){
                      return {
                        passed: assertion.passed  
                      }
                    })
                  }
                })
              }
          })
        })
  			if ('close' in main) main.close()
      },

      /**
       * Default reporter, outputting to the DOM.
       *
       * Options:
       *   - reportToId    id of element to output reports to, defaults to 'jspec'
       *   - failuresOnly  displays only suites with failing specs
       *
       * @api public
       */

      DOM : function(results, options) {
        var id = option('reportToId') || 'jspec',
            report = document.getElementById(id),
            failuresOnly = option('failuresOnly'),
            classes = results.stats.failures ? 'has-failures' : ''
        if (!report) throw 'JSpec requires the element #' + id + ' to output its reports'
        
        function bodyContents(body) {
          return JSpec.
            escape(JSpec.contentsOf(body)).
            replace(/^ */gm, function(a){ return (new Array(Math.round(a.length / 3))).join(' ') }).
            replace(/\r\n|\r|\n/gm, '<br/>')
        }
        
        report.innerHTML = '<div id="jspec-report" class="' + classes + '"><div class="heading"> \
        <span class="passes">Passes: <em>' + results.stats.passes + '</em></span>                \
        <span class="failures">Failures: <em>' + results.stats.failures + '</em></span>          \
        <span class="passes">Duration: <em>' + results.duration + '</em> ms</span>          \
        </div><table class="suites">' + map(results.allSuites, function(suite) {
          var displaySuite = failuresOnly ? suite.ran && !suite.passed() : suite.ran
          if (displaySuite && suite.isExecutable())
            return '<tr class="description"><td colspan="2">' + escape(suite.description) + '</td></tr>' +
              map(suite.specs, function(i, spec) {
                return '<tr class="' + (i % 2 ? 'odd' : 'even') + '">' +
                  (spec.requiresImplementation() ?
                    '<td class="requires-implementation" colspan="2">' + escape(spec.description) + '</td>' :
                      (spec.passed() && !failuresOnly) ?
                        '<td class="pass">' + escape(spec.description)+ '</td><td>' + spec.assertionsGraph() + '</td>' :
                          !spec.passed() ?
                            '<td class="fail">' + escape(spec.description) + 
  													map(spec.failures(), function(a){ return '<em>' + escape(a.message) + '</em>' }).join('') +
 														'</td><td>' + spec.assertionsGraph() + '</td>' :
                              '') +
                  '<tr class="body"><td colspan="2"><pre>' + bodyContents(spec.body) + '</pre></td></tr>'
              }).join('') + '</tr>'
        }).join('') + '</table></div>'
      },
      
      /**
       * Terminal reporter.
       *
       * @api public
       */
       
       Terminal : function(results, options) {
         var failuresOnly = option('failuresOnly')
         print(color("\n Passes: ", 'bold') + color(results.stats.passes, 'green') + 
               color(" Failures: ", 'bold') + color(results.stats.failures, 'red') +
               color(" Duration: ", 'bold') + color(results.duration, 'green') + " ms \n")
              
         function indent(string) {
           return string.replace(/^(.)/gm, '  $1')
         }
         
         each(results.allSuites, function(suite) {
           var displaySuite = failuresOnly ? suite.ran && !suite.passed() : suite.ran
            if (displaySuite && suite.isExecutable()) {
              print(color(' ' + suite.description, 'bold'))
              each(suite.specs, function(spec){
                var assertionsGraph = inject(spec.assertions, '', function(graph, assertion){
                  return graph + color('.', assertion.passed ? 'green' : 'red')
                })
                if (spec.requiresImplementation())
                  print(color('  ' + spec.description, 'blue') + assertionsGraph)
                else if (spec.passed() && !failuresOnly)
                  print(color('  ' + spec.description, 'green') + assertionsGraph)
                else if (!spec.passed())
                  print(color('  ' + spec.description, 'red') + assertionsGraph + 
                        "\n" + indent(map(spec.failures(), function(a){ return a.message }).join("\n")) + "\n")
              })
              print("")
            }
         })
         
         quit(results.stats.failures)
       }
    },
    
    Assertion : function(matcher, actual, expected, negate) {
      extend(this, {
        message: '',
        passed: false,
        actual: actual,
        negate: negate,
        matcher: matcher,
        expected: expected,
        
        // Report assertion results
        
        report : function() {
          if (JSpec.assert) 
            this.passed ? JSpec.stats.passes++ : JSpec.stats.failures++
          return this
        },
        
        // Run the assertion
        
        run : function() {
          // TODO: remove unshifting 
          expected.unshift(actual)
          this.result = matcher.match.apply(this, expected)
          this.passed = negate ? !this.result : this.result
          if (!this.passed) this.message = matcher.message.call(this, actual, expected, negate, matcher.name)
          return this
        }
      })
    },
    
    ProxyAssertion : function(object, method, times, negate) {
      var self = this,
          old = object[method]
      
      // Proxy
      
      object[method] = function(){
        var args = toArray(arguments),
            result = old.apply(object, args)
        self.calls.push({ args : args, result : result })
        return result
      }
      
      // Times
      
      this.times = {
        once  : 1,
        twice : 2
      }[times] || times || 1
      
      extend(this, {
        calls: [],
        message: '',
        defer: true,
        passed: false,
        negate: negate,
        object: object,
        method: method,
        
        // Proxy return value
        
        and_return : function(result) {
          this.expectedResult = result
          return this
        },
        
        // Proxy arguments passed
        
        with_args : function() {
          this.expectedArgs = toArray(arguments)
          return this
        },
        
        // Check if any calls have failing results
        
        anyResultsFail : function() {
          return any(this.calls, function(call){
            return self.expectedResult.an_instance_of ?
                     call.result.constructor != self.expectedResult.an_instance_of:
                       !equal(self.expectedResult, call.result)
          })
        },
        
        // Check if any calls have passing results
        
        anyResultsPass : function() {
          return any(this.calls, function(call){
            return self.expectedResult.an_instance_of ?
                     call.result.constructor == self.expectedResult.an_instance_of:
                       equal(self.expectedResult, call.result)
          })
        },
        
        // Return the passing result
        
        passingResult : function() {
          return this.anyResultsPass().result
        },

        // Return the failing result
        
        failingResult : function() {
          return this.anyResultsFail().result
        },
        
        // Check if any arguments fail
        
        anyArgsFail : function() {
          return any(this.calls, function(call){
            return any(self.expectedArgs, function(i, arg){
              if (arg == null) return call.args[i] == null
              return arg.an_instance_of ?
                       call.args[i].constructor != arg.an_instance_of:
                         !equal(arg, call.args[i])
                       
            })
          })
        },
        
        // Check if any arguments pass
        
        anyArgsPass : function() {
          return any(this.calls, function(call){
            return any(self.expectedArgs, function(i, arg){
              return arg.an_instance_of ?
                       call.args[i].constructor == arg.an_instance_of:
                         equal(arg, call.args[i])
                       
            })
          })
        },
        
        // Return the passing args
        
        passingArgs : function() {
          return this.anyArgsPass().args
        },
                
        // Return the failing args
        
        failingArgs : function() {
          return this.anyArgsFail().args
        },
        
        // Report assertion results
        
        report : function() {
          if (JSpec.assert) 
            this.passed ? ++JSpec.stats.passes : ++JSpec.stats.failures
          return this
        },
        
        // Run the assertion
                
        run : function() {
          var methodString = 'expected ' + object.toString() + '.' + method + '()' + (negate ? ' not' : '' )
          
          function times(n) {
            return n > 2 ?  n + ' times' : { 1: 'once', 2: 'twice' }[n]
          }
          
          if (this.expectedResult != null && (negate ? this.anyResultsPass() : this.anyResultsFail()))
            this.message = methodString + ' to return ' + puts(this.expectedResult) + 
              ' but ' + (negate ? 'it did' : 'got ' + puts(this.failingResult())) 

          if (this.expectedArgs && (negate ? !this.expectedResult && this.anyArgsPass() : this.anyArgsFail()))
            this.message = methodString + ' to be called with ' + puts.apply(this, this.expectedArgs) +
             ' but was' + (negate ? '' : ' called with ' + puts.apply(this, this.failingArgs()))

          if (negate ? !this.expectedResult && !this.expectedArgs && this.calls.length >= this.times : this.calls.length != this.times)
            this.message = methodString + ' to be called ' + times(this.times) + 
            ', but ' +  (this.calls.length == 0 ? ' was not called' : ' was called ' + times(this.calls.length))
                
          if (!this.message.length) 
            this.passed = true
          
          return this
        }
      })
    },
      
    /**
     * Specification Suite block object.
     *
     * @param {string} description
     * @param {function} body
     * @api private
     */

    Suite : function(description, body, isShared) {
      var self = this
      extend(this, {
        body: body,
        description: description,
        suites: [],
				sharedBehaviors: [],
        specs: [],
        ran: false,
				shared: isShared, 
				hooks: { 	'before' : [], 'after' : [], 
									'before_each' : [], 'after_each' : [],
									'before_nested' : [], 'after_nested' : []},
        
				// Add a spec to the suite

        addSpec : function(description, body) {
          var spec = new JSpec.Spec(description, body)
          this.specs.push(spec)
          JSpec.stats.specs++ // TODO: abstract
          spec.suite = this
        },

        // Add a before hook to the suite

        addBefore : function(options, body) {
					body.options = options || {}
          this.befores.push(body)
        },

        // Add an after hook to the suite

        addAfter : function(options, body) {
					body.options = options || {}
          this.afters.unshift(body)
        },

        // Add a hook to the suite
 
        addHook : function(hook, body) {
          this.hooks[hook].push(body)
        },

        // Add a nested suite

        addSuite : function(description, body, isShared) {
          var suite = new JSpec.Suite(description, body, isShared)
          JSpec.allSuites.push(suite)
          suite.name = suite.description
          suite.description = this.description + ' ' + suite.description
          this.suites.push(suite)
          suite.suite = this
        },

				// Invoke a hook in context to this suite

        hook : function(hook) {
					if (hook != 'before' && hook != 'after')	
          	if (this.suite) this.suite.hook(hook)

          each(this.hooks[hook], function(body) {
            JSpec.evalBody(body, "Error in hook '" + hook + "', suite '" + self.description + "': ")
          })
        },
				
        // Check if nested suites are present

        hasSuites : function() {
          return this.suites.length  
        },

        // Check if this suite has specs

        hasSpecs : function() {
          return this.specs.length
        },

        // Check if the entire suite passed

        passed : function() {
          return !any(this.specs, function(spec){
            return !spec.passed() 
          })
        },

				isShared : function(){
					return this.shared
				},

				isExecutable : function() {
					return !this.isShared() && this.hasSpecs()
				}
      })
    },
    
    /**
     * Specification block object.
     *
     * @param {string} description
     * @param {function} body
     * @api private
     */

    Spec : function(description, body) {
      extend(this, {
        body: body,
        description: description,
        assertions: [],
        
        // Add passing assertion
        
        pass : function(message) {
          this.assertions.push({ passed: true, message: message })
          if (JSpec.assert) ++JSpec.stats.passes
        },
        
        // Add failing assertion
        
        fail : function(message) {
          this.assertions.push({ passed: false, message: message })
          if (JSpec.assert) ++JSpec.stats.failures
        },
                
        // Run deferred assertions
        
        runDeferredAssertions : function() {
          each(this.assertions, function(assertion){
            if (assertion.defer) assertion.run().report(), hook('afterAssertion', assertion)
          })
        },
        
        // Find first failing assertion

        failure : function() {
          return find(this.assertions, function(assertion){
            return !assertion.passed
          })
        },

        // Find all failing assertions

        failures : function() {
          return select(this.assertions, function(assertion){
            return !assertion.passed
          })
        },

        // Weither or not the spec passed

        passed : function() {
          return !this.failure()
        },

        // Weither or not the spec requires implementation (no assertions)

        requiresImplementation : function() {
          return this.assertions.length == 0
        },

        // Sprite based assertions graph

        assertionsGraph : function() {
          return map(this.assertions, function(assertion){
            return '<span class="assertion ' + (assertion.passed ? 'passed' : 'failed') + '"></span>'
          }).join('')
        }
      })
    },
    
    Module : function(methods) {
      extend(this, methods)
    },
    
    JSON : {
      
      /**
       * Generic sequences.
       */
      
      meta : {
        '\b' : '\\b',
        '\t' : '\\t',
        '\n' : '\\n',
        '\f' : '\\f',
        '\r' : '\\r',
        '"'  : '\\"',
        '\\' : '\\\\'
      },
      
      /**
       * Escapable sequences.
       */
      
      escapable : /[\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
      
      /**
       * JSON encode _object_.
       *
       * @param  {mixed} object
       * @return {string}
       * @api private
       */
       
      encode : function(object) {
        var self = this
        if (object == undefined || object == null) return 'null'
        if (object === true) return 'true'
        if (object === false) return 'false'
        switch (typeof object) {
          case 'number': return object
          case 'string': return this.escapable.test(object) ?
            '"' + object.replace(this.escapable, function (a) {
              return typeof self.meta[a] === 'string' ? self.meta[a] :
                '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4)
            }) + '"' :
            '"' + object + '"'
          case 'object':  
            if (object.constructor == Array)
              return '[' + map(object, function(val){
                return self.encode(val)
              }).join(', ') + ']'
            else if (object)
              return '{' + map(object, function(key, val){
                return self.encode(key) + ':' + self.encode(val)
              }).join(', ') + '}'
        }
        return 'null'
      }
    },
    
    // --- DSLs
    
    DSLs : {
      snake : {
        expect : function(actual){
          return JSpec.expect(actual)
        },

        describe : function(description, body) {
          return JSpec.currentSuite.addSuite(description, body, false)
        },

        it : function(description, body) {
          return JSpec.currentSuite.addSpec(description, body)
        },

        before : function(body) {
          return JSpec.currentSuite.addHook('before', body)
        },
 
        after : function(body) {
          return JSpec.currentSuite.addHook('after', body)
        },
 
        before_each : function(body) {
          return JSpec.currentSuite.addHook('before_each', body)
        },
 
        after_each : function(body) {
          return JSpec.currentSuite.addHook('after_each', body)
        },

				before_nested : function(body) {
					return JSpec.currentSuite.addHook('before_nested', body)
				},
				
				after_nested : function(body){
					return JSpec.currentSuite.addhook('after_nested', body)
				},
        
				shared_behaviors_for : function(description, body){
				  return JSpec.currentSuite.addSuite(description, body, true)
				},

        should_behave_like : function(description) {
          return JSpec.shareBehaviorsOf(description)
        }
      }
    },

    // --- Methods
    
    /**
     * Check if _value_ is 'stop'. For use as a
     * utility callback function.
     *
     * @param  {mixed} value
     * @return {bool}
     * @api public
     */
    
    haveStopped : function(value) {
      return value === 'stop'
    },
    
    /**
     * Include _object_ which may be a hash or Module instance.
     *
     * @param  {hash, Module} object
     * @return {JSpec}
     * @api public
     */
    
    include : function(object) {
      var module = object.constructor == JSpec.Module ? object : new JSpec.Module(object)
      this.modules.push(module)
      if ('init' in module) module.init()
      if ('utilities' in module) extend(this.defaultContext, module.utilities)
      if ('matchers' in module) this.addMatchers(module.matchers)
      if ('reporters' in module) extend(this.reporters, module.reporters)
      if ('DSLs' in module)
        each(module.DSLs, function(name, methods){
          JSpec.DSLs[name] = JSpec.DSLs[name] || {}
          extend(JSpec.DSLs[name], methods)
        })
      return this
    },
    
    /**
     * Add a module hook _name_, which is immediately
     * called per module with the _args_ given. An array of
     * hook return values is returned.
     *
     * @param  {name} string
     * @param  {...} args
     * @return {array}
     * @api private
     */
    
    hook : function(name, args) {
      args = toArray(arguments, 1)
      return inject(JSpec.modules, [], function(results, module){
        if (typeof module[name] == 'function')
          results.push(JSpec.evalHook(module, name, args))
      })
    },
    
    /**
     * Eval _module_ hook _name_ with _args_. Evaluates in context
     * to the module itself, JSpec, and JSpec.context.
     *
     * @param  {Module} module
     * @param  {string} name
     * @param  {array} args
     * @return {mixed}
     * @api private
     */
    
    evalHook : function(module, name, args) {
      hook('evaluatingHookBody', module, name)
      return module[name].apply(module, args)
    },
    
    /**
     * Same as hook() however accepts only one _arg_ which is
     * considered immutable. This function passes the arg
     * to the first module, then passes the return value of the last
     * module called, to the following module. 
     *
     * @param  {string} name
     * @param  {mixed} arg
     * @return {mixed}
     * @api private
     */
    
    hookImmutable : function(name, arg) {
      return inject(JSpec.modules, arg, function(result, module){
        if (typeof module[name] == 'function')
          return JSpec.evalHook(module, name, [result])
      })
    },
    
    /**
     * Find a shared example suite by its description or name.
     * First searches parent tree of suites for shared behavior
     * before falling back to global scoped nested behaviors.
     *
     * @param  {string} description
     * @return {Suite}
     * @api private
     */
    
    findSharedBehavior : function(description) {
      var behavior
      return (behavior = JSpec.findLocalSharedBehavior(description))
        ? behavior
        : JSpec.findGlobalSharedBehavior(description)
    },

    /**
     * Find a shared example suite within the current suite's
     * parent tree by its description or name.
     *
     * @param  {string} description
     * @return {Suite}
     * @api private
     */
     
		findLocalSharedBehavior : function(description) {
			var behavior,
			    currentSuite = JSpec.currentSuite.suite
			while (currentSuite)
				if (behavior = find(currentSuite.suites, JSpec.suiteDescriptionPredicate(description)))
				  return behavior
				else
				  currentSuite = currentSuite.suite
		},
		
    /**
     * Find a shared example suite within the global
     * scope by its description or name.
     *
     * @param  {string} description
     * @return {Suite}
     * @api private
     */
     
		findGlobalSharedBehavior : function(description) {
	   return find(JSpec.suites, JSpec.suiteDescriptionPredicate(description))
		},
    
    /**
     * Build a predicate that will match a suite based on name or description
     *
     * @param  {string} description
     * @return {function}
     * @api private
     */
     
		suiteDescriptionPredicate : function(description) {
			return function(suite){
			  return suite.name === description ||
			         suite.description === description
			}
		},

    /**
     * Share behaviors (specs) of the given suite with
     * the current suite.
     *
     * @param  {string} description
     * @api public
     */
    
    shareBehaviorsOf : function(description) {
      var suite = JSpec.findSharedBehavior(description)
      if (suite)
        JSpec.evalBody(suite.body)
      else
        throw new Error("failed to find shared behaviors named `" + description + "'")
    },
    
    
    /**
     * Convert arguments to an array.
     *
     * @param  {object} arguments
     * @param  {int} offset
     * @return {array}
     * @api public
     */
    
    toArray : function(arguments, offset) {
      return Array.prototype.slice.call(arguments, offset || 0)
    },
    
    /**
     * Return ANSI-escaped colored string.
     *
     * @param  {string} string
     * @param  {string} color
     * @return {string}
     * @api public
     */
    
    color : function(string, color) {
      if (option('disableColors')) {
        return string
      } else {
        return "\u001B[" + {
         bold    : 1,
         black   : 30,
         red     : 31,
         green   : 32,
         yellow  : 33,
         blue    : 34,
         magenta : 35,
         cyan    : 36,
         white   : 37
        }[color] + 'm' + string + "\u001B[0m"
      }
    },
    
    /**
     * Default matcher message callback.
     *
     * @api private
     */
    
    defaultMatcherMessage : function(actual, expected, negate, name) {
      return 'expected ' + puts(actual) + ' to ' + 
               (negate ? 'not ' : '') + 
                  name.replace(/_/g, ' ') +
                    ' ' + (expected.length > 1 ?
                      puts.apply(this, expected.slice(1)) :
                        '')
    },
    
    /**
     * Normalize a matcher message.
     *
     * When no messge callback is present the defaultMatcherMessage
     * will be assigned, will suffice for most matchers.
     *
     * @param  {hash} matcher
     * @return {hash}
     * @api public
     */
    
    normalizeMatcherMessage : function(matcher) {
      if (typeof matcher.message != 'function') 
        matcher.message = this.defaultMatcherMessage
      return matcher
    },
    
    /**
     * Normalize a matcher body
     * 
     * This process allows the following conversions until
     * the matcher is in its final normalized hash state.
     *
     * - '==' becomes 'actual == expected'
     * - 'actual == expected' becomes 'return actual == expected'
     * - function(actual, expected) { return actual == expected } becomes 
     *   { match : function(actual, expected) { return actual == expected }}
     *
     * @param  {mixed} body
     * @return {hash}
     * @api public
     */
    
    normalizeMatcherBody : function(body) {
      var captures
      switch (body.constructor) {
        case String:
          if (captures = body.match(/^alias (\w+)/)) return JSpec.matchers[last(captures)]
          if (body.length < 4) body = 'actual ' + body + ' expected'
          return { match: function(actual, expected) { return eval(body) }}  
          
        case Function:
          return { match: body }
          
        default:
          return body
      }
    },
    
    /**
     * Get option value. This method first checks if
     * the option key has been set via the query string,
     * otherwise returning the options hash value.
     *
     * @param  {string} key
     * @return {mixed}
     * @api public
     */
     
     option : function(key) {
       return (value = query(key)) !== null ? value :
                JSpec.options[key] || null
     },
     
     /**
      * Check if object _a_, is equal to object _b_.
      *
      * @param  {object} a
      * @param  {object} b
      * @return {bool}
      * @api private
      */
     
     equal: function(a, b) {
       if (typeof a != typeof b) return
       if (a === b) return true
       if (a instanceof RegExp)
         return a.toString() === b.toString()
       if (a instanceof Date)
         return Number(a) === Number(b)
       if (typeof a != 'object') return
       if (a.length !== undefined)
         if (a.length !== b.length) return
         else
           for (var i = 0, len = a.length; i < len; ++i)
             if (!equal(a[i], b[i]))
               return
       for (var key in a)
         if (!equal(a[key], b[key]))
           return
       return true
     },

    /**
     * Return last element of an array.
     *
     * @param  {array} array
     * @return {object}
     * @api public
     */

    last : function(array) {
      return array[array.length - 1]
    },

    /**
     * Convert object(s) to a print-friend string.
     *
     * @param  {...} object
     * @return {string}
     * @api public
     */

    puts : function(object) {
      if (arguments.length > 1)
        return map(toArray(arguments), function(arg){
          return puts(arg)
        }).join(', ')
      if (object === undefined) return 'undefined'
      if (object === null) return 'null'
      if (object === true) return 'true'
      if (object === false) return 'false'
      if (object.an_instance_of) return 'an instance of ' + object.an_instance_of.name
      if (object.jquery && object.selector.length > 0) return 'selector ' + puts(object.selector)
      if (object.jquery && object.get(0) && object.get(0).outerHTML) return object.get(0).outerHTML
      if (object.jquery && object.get(0) && object.get(0).nodeType == 9) return 'jQuery(document)'
      if (object.jquery && object.get(0)) return document.createElement('div').appendChild(object.get(0)).parentNode.innerHTML
      if (object.jquery && object.length == 0) return 'jQuery()'
      if (object.nodeName && object.outerHTML) return object.outerHTML
      if (object.nodeName) return document.createElement('div').appendChild(object).parentNode.innerHTML
      switch (object.constructor) {
        case Function: return object.name || object 
        case String: 
          return '"' + object
            .replace(/"/g,  '\\"')
            .replace(/\n/g, '\\n')
            .replace(/\t/g, '\\t')
            + '"'
        case Array: 
          return inject(object, '[', function(b, v){
            return b + ', ' + puts(v)
          }).replace('[,', '[') + ' ]'
        case Object:
          object.__hit__ = true
          return inject(object, '{', function(b, k, v) {
            if (k == '__hit__') return b
            return b + ', ' + k + ': ' + (v && v.__hit__ ? '<circular reference>' : puts(v))
          }).replace('{,', '{') + ' }'
        default: 
          return object.toString()
      }
    },

    /**
     * Parse an XML String and return a 'document'.
     *
     * @param {string} text
     * @return {document}
     * @api public
     */

    parseXML : function(text) {
      var xmlDoc
      if (window.DOMParser)
        xmlDoc = (new DOMParser()).parseFromString(text, "text/xml")
      else {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
        xmlDoc.async = "false"
        xmlDoc.loadXML(text)
      }
      return xmlDoc
    },

    /**
     * Escape HTML.
     *
     * @param  {string} html
     * @return {string}
     * @api public
     */

     escape : function(html) {
       return html.toString()
         .replace(/&/gmi, '&amp;')
         .replace(/"/gmi, '&quot;')
         .replace(/>/gmi, '&gt;')
         .replace(/</gmi, '&lt;')
     },
     
     /**
      * Perform an assertion without reporting.
      *
      * This method is primarily used for internal
      * matchers in order retain DRYness. May be invoked 
      * like below:
      *
      *   does('foo', 'eql', 'foo')
      *   does([1,2], 'include', 1, 2)
      *
      * External hooks are not run for internal assertions
      * performed by does().
      *
      * @param  {mixed} actual
      * @param  {string} matcher
      * @param  {...} expected
      * @return {mixed}
      * @api private
      */
     
     does : function(actual, matcher, expected) {
       var assertion = new JSpec.Assertion(JSpec.matchers[matcher], actual, toArray(arguments, 2))
       return assertion.run().result
     },

    /**
     * Perform an assertion.
     *
     *   expect(true).to('be', true)
     *   expect('foo').not_to('include', 'bar')
     *   expect([1, [2]]).to('include', 1, [2])
     *
     * @param  {mixed} actual
     * @return {hash}
     * @api public
     */

    expect : function(actual) {
      function assert(matcher, args, negate) {
        var expected = toArray(args, 1)
        matcher.negate = negate  
        var assertion = new JSpec.Assertion(matcher, actual, expected, negate)
        hook('beforeAssertion', assertion)
        if (matcher.defer) assertion.run()
        else JSpec.currentSpec.assertions.push(assertion.run().report()), hook('afterAssertion', assertion)
        return assertion.result
      }
      
      function to(matcher) {
        return assert(matcher, arguments, false)
      }
      
      function not_to(matcher) {
        return assert(matcher, arguments, true)
      }
      
      return {
        to : to,
        should : to,
        not_to: not_to,
        should_not : not_to
      }
    },

    /**
     * Strim whitespace or chars.
     *
     * @param  {string} string
     * @param  {string} chars
     * @return {string}
     * @api public
     */

     strip : function(string, chars) {
       return string.
         replace(new RegExp('['  + (chars || '\\s') + ']*$'), '').
         replace(new RegExp('^[' + (chars || '\\s') + ']*'),  '')
     },
     
     /**
      * Call an iterator callback with arguments a, or b
      * depending on the arity of the callback.
      *
      * @param  {function} callback
      * @param  {mixed} a
      * @param  {mixed} b
      * @return {mixed}
      * @api private
      */
     
     callIterator : function(callback, a, b) {
       return callback.length == 1 ? callback(b) : callback(a, b)
     },
     
     /**
      * Extend an object with another.
      *
      * @param  {object} object
      * @param  {object} other
      * @api public
      */
     
     extend : function(object, other) {
       each(other, function(property, value){
         object[property] = value
       })
     },
     
     /**
      * Iterate an object, invoking the given callback.
      *
      * @param  {hash, array} object
      * @param  {function} callback
      * @return {JSpec}
      * @api public
      */

     each : function(object, callback) {
       if (object.constructor == Array)
         for (var i = 0, len = object.length; i < len; ++i)
           callIterator(callback, i, object[i])
       else
         for (var key in object) 
           if (object.hasOwnProperty(key))
             callIterator(callback, key, object[key])
     },

     /**
      * Iterate with memo.
      *
      * @param  {hash, array} object
      * @param  {object} memo
      * @param  {function} callback
      * @return {object}
      * @api public
      */

     inject : function(object, memo, callback) {
       each(object, function(key, value){
         memo = (callback.length == 2 ?
                   callback(memo, value):
                     callback(memo, key, value)) ||
                       memo
       })
       return memo
     },
     
     /**
      * Destub _object_'s _method_. When no _method_ is passed
      * all stubbed methods are destubbed. When no arguments
      * are passed every object found in JSpec.stubbed will be
      * destubbed.
      *
      * @param  {mixed} object
      * @param  {string} method
      * @api public
      */
     
     destub : function(object, method) {
       var captures
       if (method) {
         if (object.hasOwnProperty('__prototype__' + method))
           delete object[method]
         else if (object.hasOwnProperty('__original__' + method))
           object[method] = object['__original__' + method]

         delete object['__prototype__' + method]
         delete object['__original__' + method]
       }
       else if (object) {
         for (var key in object)
           if (captures = key.match(/^(?:__prototype__|__original__)(.*)/))
             destub(object, captures[1])
       }
       else
         while (JSpec.stubbed.length)
            destub(JSpec.stubbed.shift())
     },
     
     /**
      * Stub _object_'s _method_. 
      *
      * stub(foo, 'toString').and_return('bar')
      *
      * @param  {mixed} object
      * @param  {string} method
      * @return {hash}
      * @api public
      */
     
     stub : function(object, method) {
       hook('stubbing', object, method)
			 
			 //unbind any stub already present on this method
			 JSpec.destub(object, method);
       JSpec.stubbed.push(object)
       var type = object.hasOwnProperty(method) ? '__original__' : '__prototype__'
       object[type + method] = object[method]

       object[method] = function(){}
       return {
         and_return : function(value) {
           if (typeof value == 'function') object[method] = value
           else object[method] = function(){ return value }
         }
      }
     },
     
    /**
     * Map callback return values.
     *
     * @param  {hash, array} object
     * @param  {function} callback
     * @return {array}
     * @api public
     */

    map : function(object, callback) {
      return inject(object, [], function(memo, key, value){
        memo.push(callIterator(callback, key, value))
      })
    },
    
    /**
     * Returns the first matching expression or null.
     *
     * @param  {hash, array} object
     * @param  {function} callback
     * @return {mixed}
     * @api public
     */
         
    any : function(object, callback) {
      return inject(object, null, function(state, key, value){
        if (state == undefined)
          return callIterator(callback, key, value) ? value : state
      })
    },
    
    /**
     * Returns an array of values collected when the callback
     * given evaluates to true.
     *
     * @param  {hash, array} object
     * @return {function} callback
     * @return {array}
     * @api public
     */
    
    select : function(object, callback) {
      return inject(object, [], function(selected, key, value){
        if (callIterator(callback, key, value))
          selected.push(value)
      })
    },

    /**
     * Define matchers.
     *
     * @param  {hash} matchers
     * @api public
     */

    addMatchers : function(matchers) {
      each(matchers, function(name, body){
        JSpec.addMatcher(name, body)  
      })
    },
    
    /**
     * Define a matcher.
     *
     * @param  {string} name
     * @param  {hash, function, string} body
     * @api public
     */
    
    addMatcher : function(name, body) {
      hook('addingMatcher', name, body)
      if (name.indexOf(' ') != -1) {
        var matchers = name.split(/\s+/)
        var prefix = matchers.shift()
        each(matchers, function(name) {
          JSpec.addMatcher(prefix + '_' + name, body(name))
        })
      }
      this.matchers[name] = this.normalizeMatcherMessage(this.normalizeMatcherBody(body))
      this.matchers[name].name = name
    },
    
    /**
     * Add a root suite to JSpec.
     *
     * @param  {string} description
     * @param  {body} function
     * @api public
     */
    
    describe : function(description, body) {
      var suite = new JSpec.Suite(description, body, false)
      hook('addingSuite', suite)
      this.allSuites.push(suite)
      this.suites.push(suite)
    },
    
    /**
     * Add a shared example suite to JSpec.
     *
     * @param  {string} description
     * @param  {body} function
     * @api public
     */
    
    shared_behaviors_for : function(description, body) {
      var suite = new JSpec.Suite(description, body, true)
      hook('addingSuite', suite)
      this.allSuites.push(suite)
      this.suites.push(suite)
    },

    /**
     * Return the contents of a function body.
     *
     * @param  {function} body
     * @return {string}
     * @api public
     */
    
    contentsOf : function(body) {
      return body.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1]
    },

    /**
     * Evaluate a JSpec capture body.
     *
     * @param  {function} body
     * @param  {string} errorMessage (optional)
     * @return {Type}
     * @api private
     */

    evalBody : function(body, errorMessage) {
      var dsl = this.DSL || this.DSLs.snake
      var matchers = this.matchers
      var context = this.context || this.defaultContext
      var contents = this.contentsOf(body)
			hook('evaluatingBody', dsl, matchers, context, contents)
      with (dsl){ with (context) { with (matchers) { eval(contents) }}}
    },

    /**
     * Pre-process a string of JSpec.
     *
     * @param  {string} input
     * @return {string}
     * @api private
     */

    preprocess : function(input) {
      if (typeof input != 'string') return
      input = hookImmutable('preprocessing', input)
      return input.
        replace(/\t/g, '  ').
        replace(/\r\n|\n|\r/g, '\n').
        split('__END__')[0].
        replace(/([\w\.]+)\.(stub|destub)\((.*?)\)$/gm, '$2($1, $3)').
        replace(/describe\s+(.*?)$/gm, 'describe($1, function(){').
        replace(/shared_behaviors_for\s+(.*?)$/gm, 'shared_behaviors_for($1, function(){').
        replace(/^\s+it\s+(.*?)$/gm, ' it($1, function(){').
				replace(/^ *(before_nested|after_nested|before_each|after_each|before|after)(?= |\n|$)/gm, 'JSpec.currentSuite.addHook("$1", function(){').
        replace(/^\s*end(?=\s|$)/gm, '});').
        replace(/-\{/g, 'function(){').
        replace(/(\d+)\.\.(\d+)/g, function(_, a, b){ return range(a, b) }).
        replace(/\.should([_\.]not)?[_\.](\w+)(?: |;|$)(.*)$/gm, '.should$1_$2($3)').
        replace(/([\/\s]*)(.+?)\.(should(?:[_\.]not)?)[_\.](\w+)\((.*)\)\s*;?$/gm, '$1 expect($2).$3($4, $5)').
        replace(/, \)/g, ')').
        replace(/should\.not/g, 'should_not')
    },

    /**
     * Create a range string which can be evaluated to a native array.
     *
     * @param  {int} start
     * @param  {int} end
     * @return {string}
     * @api public
     */

    range : function(start, end) {
      var current = parseInt(start), end = parseInt(end), values = [current]
      if (end > current) while (++current <= end) values.push(current)
      else               while (--current >= end) values.push(current)
      return '[' + values + ']'
    },

    /**
     * Report on the results. 
     *
     * @api public
     */

    report : function() {
      this.duration = Number(new Date) - this.start
      hook('reporting', JSpec.options)
      new (JSpec.options.reporter || JSpec.reporters.DOM)(JSpec, JSpec.options)
    },

    /**
     * Run the spec suites. Options are merged
     * with JSpec options when present.
     *
     * @param  {hash} options
     * @return {JSpec}
     * @api public
     */

    run : function(options) {
      if (any(hook('running'), haveStopped)) return this
      if (options) extend(this.options, options)
      this.start = Number(new Date)
      each(this.suites, function(suite) { JSpec.runSuite(suite) })
      return this
    },
    
    /**
     * Run a suite.
     *
     * @param  {Suite} suite
     * @api public
     */

    runSuite : function(suite) {
			if (!suite.isShared())
			{
	      this.currentSuite = suite
	      this.evalBody(suite.body)
	      suite.ran = true
	      hook('beforeSuite', suite), suite.hook('before'), suite.hook('before_nested')
	      each(suite.specs, function(spec) {
	        hook('beforeSpec', spec)
	        suite.hook('before_each')
	        JSpec.runSpec(spec)
	        hook('afterSpec', spec)
	        suite.hook('after_each')
	      })
	      if (suite.hasSuites()) {
	        each(suite.suites, function(suite) {
	          JSpec.runSuite(suite)
	        })
	      }
	      hook('afterSuite', suite), suite.hook('after_nested'), suite.hook('after')
	      this.stats.suitesFinished++
			}	
		},
         
    /**
     * Report a failure for the current spec.
     *
     * @param  {string} message
     * @api public
     */
     
     fail : function(message) {
       JSpec.currentSpec.fail(message)
     },
     
     /**
      * Report a passing assertion for the current spec.
      *
      * @param  {string} message
      * @api public
      */
      
     pass : function(message) {
       JSpec.currentSpec.pass(message)
     },

    /**
     * Run a spec.
     *
     * @param  {Spec} spec
     * @api public
     */

    runSpec : function(spec) {
      this.currentSpec = spec
      try { this.evalBody(spec.body) }
      catch (e) { fail(e) }
      spec.runDeferredAssertions()
      destub()
      this.stats.specsFinished++
      this.stats.assertions += spec.assertions.length
    },

    /**
     * Require a dependency, with optional message.
     *
     * @param  {string} dependency
     * @param  {string} message (optional)
     * @return {JSpec}
     * @api public
     */

    requires : function(dependency, message) {
      hook('requiring', dependency, message)
      try { eval(dependency) }
      catch (e) { throw 'JSpec depends on ' + dependency + ' ' + message }
      return this
    },

    /**
     * Query against the current query strings keys
     * or the queryString specified.
     *
     * @param  {string} key
     * @param  {string} queryString
     * @return {string, null}
     * @api private
     */

    query : function(key, queryString) {
      var queryString = (queryString || (main.location ? main.location.search : null) || '').substring(1)
      return inject(queryString.split('&'), null, function(value, pair){
        parts = pair.split('=')
        return parts[0] == key ? parts[1].replace(/%20|\+/gmi, ' ') : value
      })
    },

    /**
     * Ad-hoc POST request for JSpec server usage.
     *
     * @param  {string} uri
     * @param  {string} data
     * @api private
     */
    
    post : function(uri, data) {
      if (any(hook('posting', uri, data), haveStopped)) return
      var request = this.xhr()
      request.open('POST', uri, false)
      request.setRequestHeader('Content-Type', 'application/json')
      request.send(JSpec.JSON.encode(data))
    },

    /**
     * Instantiate an XMLHttpRequest.
     *
     * Here we utilize IE's lame ActiveXObjects first which
     * allow IE access serve files via the file: protocol, otherwise
     * we then default to XMLHttpRequest.
     *
     * @return {XMLHttpRequest, ActiveXObject}
     * @api private
     */
    
    xhr : function() {
      return this.ieXhr() || new JSpec.request
    },
    
    /**
     * Return Microsoft piece of crap ActiveXObject.
     *
     * @return {ActiveXObject}
     * @api public
     */
    
    ieXhr : function() {
      function object(str) {
        try { return new ActiveXObject(str) } catch(e) {}
      }
      return object('Msxml2.XMLHTTP.6.0') ||
        object('Msxml2.XMLHTTP.3.0') ||
        object('Msxml2.XMLHTTP') ||
        object('Microsoft.XMLHTTP')
    },
    
    /**
     * Check for HTTP request support.
     *
     * @return {bool}
     * @api private
     */
    
    hasXhr : function() {
      return JSpec.request || 'ActiveXObject' in main
    },
    
    /**
     * Try loading _file_ returning the contents
     * string or null. Chain to locate / read a file.
     *
     * @param  {string} file
     * @return {string}
     * @api public
     */
    
    tryLoading : function(file) {
      try { return JSpec.load(file) } catch (e) {}
    },

    /**
     * Load a _file_'s contents.
     *
     * @param  {string} file
     * @param  {function} callback
     * @return {string}
     * @api public
     */

    load : function(file, callback) {
      if (any(hook('loading', file), haveStopped)) return
      if ('readFile' in main)
        return readFile(file)
      else if (this.hasXhr()) {
        var request = this.xhr()
        request.open('GET', file, false)
        request.send(null)
        if (request.readyState == 4 && 
           (request.status == 0 || 
            request.status.toString().charAt(0) == 2)) 
          return request.responseText
      }
      else
        throw new Error("failed to load `" + file + "'")
    },

    /**
     * Load, pre-process, and evaluate a file.
     *
     * @param {string} file
     * @param {JSpec}
     * @api public
     */

    exec : function(file) {
      if (any(hook('executing', file), haveStopped)) return this
      eval('with (JSpec){' + this.preprocess(this.load(file)) + '}')
      return this
    }
  }
  
  // --- Node.js support
  
  if (typeof GLOBAL === 'object' && typeof exports === 'object') {
    var fs = require('fs')
    quit = process.exit
    print = require('sys').puts
    readFile = function(file){
      return fs.readFileSync(file).toString('utf8')
    }
  }

  // --- envjsrb / johnson support

  if (typeof Johnson === 'object') {
    quit = function () {}
  }
  
  // --- Utility functions

  var main = this,
      find = JSpec.any,
      utils = 'haveStopped stub hookImmutable hook destub map any last pass fail range each option inject select \
               error escape extend puts query strip color does addMatchers callIterator toArray equal'.split(/\s+/)
  while (utils.length) eval('var ' + utils[0] + ' = JSpec.' + utils.shift())
  if (!main.setTimeout) main.setTimeout = function(callback){ callback() }

  // --- Matchers

  addMatchers({
    equal              : "===",
    eql                : "equal(actual, expected)",
    be                 : "alias equal",
    be_greater_than    : ">",
    be_less_than       : "<",
    be_at_least        : ">=",
    be_at_most         : "<=",
    be_a               : "actual.constructor == expected",
    be_an              : "alias be_a",
    be_an_instance_of  : "actual instanceof expected",
    be_null            : "actual == null",
    be_true            : "actual == true",
    be_false           : "actual == false",
    be_undefined       : "typeof actual == 'undefined'",
    be_type            : "typeof actual == expected",
    match              : "typeof actual == 'string' ? actual.match(expected) : false",
    respond_to         : "typeof actual[expected] == 'function'",
    have_length        : "actual.length == expected",
    be_within          : "actual >= expected[0] && actual <= last(expected)",
    have_length_within : "actual.length >= expected[0] && actual.length <= last(expected)",
    
    receive : { defer : true, match : function(actual, method, times) {
      var proxy = new JSpec.ProxyAssertion(actual, method, times, this.negate)
      JSpec.currentSpec.assertions.push(proxy)
      return proxy
    }},
    
    be_empty : function(actual) {
      if (actual.constructor == Object && actual.length == undefined)
        for (var key in actual)
          return false;
      return !actual.length
    },

    include : function(actual) {
      for (var state = true, i = 1; i < arguments.length; i++) {
        var arg = arguments[i]
        switch (actual.constructor) {
          case String: 
          case Number:
          case RegExp:
          case Function:
            state = actual.toString().indexOf(arg) !== -1
            break
         
          case Object:
            state = arg in actual
            break
          
          case Array: 
            state = any(actual, function(value){ return equal(value, arg) })
            break
        }
        if (!state) return false
      }
      return true
    },

    throw_error : { match : function(actual, expected, message) {
      try { actual() }
      catch (e) {
        this.e = e
        var assert = function(arg) {
          switch (arg.constructor) {
            case RegExp   : return arg.test(e.message || e.toString())
            case String   : return arg == (e.message || e.toString())
            case Function : return e instanceof arg || e.name == arg.name
          }
        }
        return message ? assert(expected) && assert(message) :
                 expected ? assert(expected) :
                   true
      }
    }, message : function(actual, expected, negate) {
      // TODO: refactor when actual is not in expected [0]
      var message_for = function(i) {
        if (expected[i] == undefined) return 'exception'
        switch (expected[i].constructor) {
          case RegExp   : return 'exception matching ' + puts(expected[i])
          case String   : return 'exception of ' + puts(expected[i])
          case Function : return expected[i].name || 'Error'
        }
      }
      var exception = message_for(1) + (expected[2] ? ' and ' + message_for(2) : '')
      return 'expected ' + exception + (negate ? ' not ' : '' ) +
               ' to be thrown, but ' + (this.e ? 'got ' + puts(this.e) : 'nothing was')
    }},
    
    have : function(actual, length, property) {
      return actual[property] == null ? false : actual[property].length == length
    },
    
    have_at_least : function(actual, length, property) {
      return actual[property] == null ? (length === 0) : actual[property].length >= length
    },
    
    have_at_most :function(actual, length, property) {
      return actual[property] == null || actual[property].length <= length
    },
    
    have_within : function(actual, range, property) {
      var length = actual[property] == undefined ? 0 : actual[property].length
      return length >= range.shift() && length <= range.pop()
    },
    
    have_prop : function(actual, property, value) {
      var actualVal = actual[property], actualType = typeof actualVal
      return (actualType == 'function' || actualType == 'undefined') ? false :
        typeof value === 'undefined' ||
        does(actual[property],'eql',value)
    },
    
    have_property : function(actual, property, value) {
      var actualVal = actual[property], actualType = typeof actualVal
      return (actualType == 'function' || actualType == 'undefined') ? false :
        typeof value === 'undefined' ||
        value === actualVal
    }
  })
  
})()