You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.1 KiB

5 months ago
  1. var tests = [], filters = [], nameCounts = {};
  2. function Failure(why) {this.message = why;}
  3. Failure.prototype.toString = function() { return this.message; };
  4. function indexOf(collection, elt) {
  5. if (collection.indexOf) return collection.indexOf(elt);
  6. for (var i = 0, e = collection.length; i < e; ++i)
  7. if (collection[i] == elt) return i;
  8. return -1;
  9. }
  10. function test(name, run, expectedFail) {
  11. // Force unique names
  12. if (nameCounts[name] == undefined){
  13. nameCounts[name] = 2;
  14. } else {
  15. // Append number if not first test with this name.
  16. name = name + '_' + (nameCounts[name]++);
  17. }
  18. // Add test
  19. tests.push({name: name, func: run, expectedFail: expectedFail});
  20. return name;
  21. }
  22. var namespace = "";
  23. function testCM(name, run, opts, expectedFail) {
  24. return test(namespace + name, function() {
  25. var place = document.getElementById("testground"), cm = window.cm = CodeMirror(place, opts);
  26. var successful = false;
  27. try {
  28. run(cm);
  29. successful = true;
  30. } finally {
  31. if (!successful || verbose) {
  32. place.style.visibility = "visible";
  33. } else {
  34. place.removeChild(cm.getWrapperElement());
  35. }
  36. }
  37. }, expectedFail);
  38. }
  39. function runTests(callback) {
  40. var totalTime = 0;
  41. function step(i) {
  42. for (;;) {
  43. if (i === tests.length) {
  44. running = false;
  45. return callback("done");
  46. }
  47. var test = tests[i], skip = false;
  48. if (filters.length) {
  49. skip = true;
  50. for (var j = 0; j < filters.length; j++)
  51. if (test.name.match(filters[j])) skip = false;
  52. }
  53. if (skip) {
  54. callback("skipped", test.name, message);
  55. i++;
  56. } else {
  57. break;
  58. }
  59. }
  60. var expFail = test.expectedFail, startTime = +new Date, threw = false;
  61. try {
  62. var message = test.func();
  63. } catch(e) {
  64. threw = true;
  65. if (expFail) callback("expected", test.name);
  66. else if (e instanceof Failure) callback("fail", test.name, e.message);
  67. else {
  68. var pos = /(?:\bat |@).*?([^\/:]+):(\d+)/.exec(e.stack);
  69. if (pos) console["log"](e.stack);
  70. callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : ""));
  71. }
  72. }
  73. if (!threw) {
  74. if (expFail) callback("fail", test.name, message || "expected failure, but passed");
  75. else callback("ok", test.name, message);
  76. }
  77. if (!quit) { // Run next test
  78. var delay = 0;
  79. totalTime += (+new Date) - startTime;
  80. if (totalTime > 500){
  81. totalTime = 0;
  82. delay = 50;
  83. }
  84. setTimeout(function(){step(i + 1);}, delay);
  85. } else { // Quit tests
  86. running = false;
  87. return null;
  88. }
  89. }
  90. step(0);
  91. }
  92. function label(str, msg) {
  93. if (msg) return str + " (" + msg + ")";
  94. return str;
  95. }
  96. function eq(a, b, msg) {
  97. if (a != b) throw new Failure(label(a + " != " + b, msg));
  98. }
  99. function notEq(a, b, msg) {
  100. if (a == b) throw new Failure(label(a + " == " + b, msg));
  101. }
  102. function near(a, b, margin, msg) {
  103. if (Math.abs(a - b) > margin)
  104. throw new Failure(label(a + " is not close to " + b + " (" + margin + ")", msg));
  105. }
  106. function eqCharPos(a, b, msg) {
  107. function str(p) { return "{line:" + p.line + ",ch:" + p.ch + ",sticky:" + p.sticky + "}"; }
  108. if (a == b) return;
  109. if (a == null) throw new Failure(label("comparing null to " + str(b), msg));
  110. if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg));
  111. if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg));
  112. }
  113. function eqCursorPos(a, b, msg) {
  114. eqCharPos(a, b, msg);
  115. if (a) eq(a.sticky, b.sticky, msg ? msg + ' (sticky)' : 'sticky');
  116. }
  117. function is(a, msg) {
  118. if (!a) throw new Failure(label("assertion failed", msg));
  119. }
  120. function countTests() {
  121. if (!filters.length) return tests.length;
  122. var sum = 0;
  123. for (var i = 0; i < tests.length; ++i) {
  124. var name = tests[i].name;
  125. for (var j = 0; j < filters.length; j++) {
  126. if (name.match(filters[j])) {
  127. ++sum;
  128. break;
  129. }
  130. }
  131. }
  132. return sum;
  133. }
  134. function parseTestFilter(s) {
  135. if (/_\*$/.test(s)) return new RegExp("^" + s.slice(0, s.length - 2), "i");
  136. else return new RegExp(s, "i");
  137. }