driver.js 4.0 KB

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