driver.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. var tests = [], debug = null, debugUsed = new Array(), 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 = CodeMirror(place, opts);
  27. var successful = false;
  28. try {
  29. run(cm);
  30. successful = true;
  31. } finally {
  32. if ((debug && !successful) || verbose) {
  33. place.style.visibility = "visible";
  34. } else {
  35. place.removeChild(cm.getWrapperElement());
  36. }
  37. }
  38. }, expectedFail);
  39. }
  40. function runTests(callback) {
  41. if (debug) {
  42. if (indexOf(debug, "verbose") === 0) {
  43. verbose = true;
  44. debug.splice(0, 1);
  45. }
  46. if (debug.length < 1) {
  47. debug = null;
  48. }
  49. }
  50. var totalTime = 0;
  51. function step(i) {
  52. if (i === tests.length){
  53. running = false;
  54. return callback("done");
  55. }
  56. var test = tests[i], expFail = test.expectedFail, startTime = +new Date;
  57. if (debug !== null) {
  58. var debugIndex = indexOf(debug, test.name);
  59. if (debugIndex !== -1) {
  60. // Remove from array for reporting incorrect tests later
  61. debug.splice(debugIndex, 1);
  62. } else {
  63. var wildcardName = test.name.split("_")[0] + "_*";
  64. debugIndex = indexOf(debug, wildcardName);
  65. if (debugIndex !== -1) {
  66. // Remove from array for reporting incorrect tests later
  67. debug.splice(debugIndex, 1);
  68. debugUsed.push(wildcardName);
  69. } else {
  70. debugIndex = indexOf(debugUsed, wildcardName);
  71. if (debugIndex == -1) return step(i + 1);
  72. }
  73. }
  74. }
  75. var threw = false;
  76. try {
  77. var message = test.func();
  78. } catch(e) {
  79. threw = true;
  80. if (expFail) callback("expected", test.name);
  81. else if (e instanceof Failure) callback("fail", test.name, e.message);
  82. else {
  83. var pos = /\bat .*?([^\/:]+):(\d+):/.exec(e.stack);
  84. callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : ""));
  85. }
  86. }
  87. if (!threw) {
  88. if (expFail) callback("fail", test.name, message || "expected failure, but succeeded");
  89. else callback("ok", test.name, message);
  90. }
  91. if (!quit) { // Run next test
  92. var delay = 0;
  93. totalTime += (+new Date) - startTime;
  94. if (totalTime > 500){
  95. totalTime = 0;
  96. delay = 50;
  97. }
  98. setTimeout(function(){step(i + 1);}, delay);
  99. } else { // Quit tests
  100. running = false;
  101. return null;
  102. }
  103. }
  104. step(0);
  105. }
  106. function label(str, msg) {
  107. if (msg) return str + " (" + msg + ")";
  108. return str;
  109. }
  110. function eq(a, b, msg) {
  111. if (a != b) throw new Failure(label(a + " != " + b, msg));
  112. }
  113. function eqPos(a, b, msg) {
  114. function str(p) { return "{line:" + p.line + ",ch:" + p.ch + "}"; }
  115. if (a == b) return;
  116. if (a == null) throw new Failure(label("comparing null to " + str(b), msg));
  117. if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg));
  118. if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg));
  119. }
  120. function is(a, msg) {
  121. if (!a) throw new Failure(label("assertion failed", msg));
  122. }
  123. function countTests() {
  124. if (!debug) return tests.length;
  125. var sum = 0;
  126. for (var i = 0; i < tests.length; ++i) {
  127. var name = tests[i].name;
  128. if (indexOf(debug, name) != -1 ||
  129. indexOf(debug, name.split("_")[0] + "_*") != -1)
  130. ++sum;
  131. }
  132. return sum;
  133. }