test.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Clone Test-Suite (Browser)</title>
  6. <script>
  7. var module = {};
  8. var tests = exports = module.exports = {};
  9. function require(moduleName) {
  10. if (moduleName == './') {
  11. return clone;
  12. }
  13. }
  14. function log(str) {
  15. logList.innerHTML += '<li>' + str + '</li>';
  16. }
  17. </script>
  18. <script src="clone.js"></script>
  19. <script src="test.js"></script>
  20. </head>
  21. <body>
  22. <h1 id="nodeunit-header">Clone Test-Suite (Browser)</h1>
  23. Tests started: <span id="testsStarted"></span>;
  24. Tests finished: <span id="testsFinished"></span>.
  25. <ul id="logList"></ul>
  26. <script>
  27. /* Methods copied from
  28. * https://github.com/caolan/nodeunit/blob/master/lib/assert.js
  29. */
  30. function isUndefinedOrNull(value) {
  31. return value === null || value === undefined;
  32. }
  33. function isArguments(object) {
  34. return Object.prototype.toString.call(object) == '[object Arguments]';
  35. }
  36. var _keys = function (obj){
  37. if (Object.keys) return Object.keys(obj);
  38. if (typeof obj != 'object' && typeof obj != 'function') {
  39. throw new TypeError('-');
  40. }
  41. var keys = [];
  42. for(var k in obj) if(obj.hasOwnProperty(k)) keys.push(k);
  43. return keys;
  44. };
  45. function objEquiv(a, b) {
  46. if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
  47. return false;
  48. if (a.prototype !== b.prototype)
  49. return false;
  50. if (isArguments(a)) {
  51. if (!isArguments(b)) return false;
  52. a = pSlice.call(a);
  53. b = pSlice.call(b);
  54. return _deepEqual(a, b);
  55. }
  56. try {
  57. var ka = _keys(a), kb = _keys(b), key, i;
  58. } catch (e) {
  59. return false
  60. }
  61. if (ka.length != kb.length)
  62. return false;
  63. ka.sort();
  64. kb.sort();
  65. for (i = ka.length - 1; i >= 0; i--) {
  66. if (ka[i] != kb[i]) return false;
  67. }
  68. for (i = ka.length - 1; i >= 0; i--) {
  69. key = ka[i];
  70. if (!_deepEqual(a[key], b[key] ))
  71. return false;
  72. }
  73. return true;
  74. }
  75. function _deepEqual(actual, expected) {
  76. if (actual === expected) {
  77. return true;
  78. } else if (actual instanceof Date && expected instanceof Date) {
  79. return actual.getTime() === expected.getTime();
  80. } else if (actual instanceof RegExp && expected instanceof RegExp) {
  81. return actual.source === expected.source &&
  82. actual.global === expected.global &&
  83. actual.ignoreCase === expected.ignoreCase &&
  84. actual.multiline === expected.multiline;
  85. } else if (typeof actual != 'object' && typeof expected != 'object') {
  86. return actual == expected;
  87. } else {
  88. return objEquiv(actual, expected);
  89. }
  90. }
  91. for (var testName in tests) {
  92. setTimeout((function (testName) {
  93. try {
  94. testsStarted.innerHTML = (parseInt(testsStarted.innerHTML) || 0) + 1;
  95. function incFinished() {
  96. testsFinished.innerHTML = (parseInt(testsFinished.innerHTML) || 0) + 1;
  97. }
  98. tests[testName]({
  99. expect: function (num) {
  100. this._expect = num
  101. },
  102. ok: function (val) {
  103. if(!val) throw new Error(val + ' is not ok.')
  104. },
  105. equal: function (a,b) {
  106. if (a != b) throw new Error(a + ' is not equal to ' + b)
  107. },
  108. notEqual: function (a,b) {
  109. if (a == b) throw new Error(a + ' is equal to ' + b)
  110. },
  111. strictEqual: function (a,b) {
  112. if (a !== b) throw new Error(a + ' is not strict equal to ' + b)
  113. },
  114. deepEqual: function (a,b) {
  115. if (!_deepEqual(a,b))
  116. throw new Error(JSON.stringify(a) + ' is not deep equal to ' +
  117. JSON.stringify(b))
  118. },
  119. done: function () {
  120. log(testName + ' <span style="color:blue">is ok</span>.');
  121. incFinished();
  122. }
  123. });
  124. } catch(e) {
  125. log(testName + ' <span style="color:red">FAIL.</span> <small>'+ e +'</small>');
  126. incFinished();
  127. console.log(e);
  128. }
  129. })(testName), 1);
  130. }
  131. </script>
  132. </body>
  133. </html>