2
0

test.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. function TestError(s) {this.__s = s;}
  2. TestError.prototype.toString = function(){return this.__s;};
  3. function runImpl(tests, stat, tab){
  4. for(var t in tests)
  5. runTest(t, tests, stat, tab);
  6. }
  7. function runTest(t, tests, stat, tab){
  8. var r = tests[t];
  9. if (typeof r != "function"){
  10. console.log(tab + t);
  11. runImpl(r, stat, tab + "\t");
  12. return;
  13. }
  14. var padding = " ";
  15. var log = t;
  16. if (log.length < padding.length)
  17. log = t + padding.substring(log.length);
  18. else
  19. log += " ";
  20. try {
  21. ++stat.count;
  22. r();
  23. //log += "OK";
  24. }
  25. catch (x){
  26. ++stat.failCount;
  27. if (x instanceof TestError)
  28. log += "Failed\n\t" + tab + x;
  29. else
  30. log += "Failed\n" + (x.stack ? x.stack : '\t' + tab + x);
  31. console.log(tab + log);
  32. }
  33. }
  34. function run(tests){
  35. var stat = {count: 0, failCount: 0};
  36. console.log("Running..." );
  37. var start = (new Date()).getTime();
  38. if (typeof process != "undefined" && process.argv.length > 2)
  39. runTest(process.argv[2], tests, stat, "");
  40. else
  41. runImpl(tests, stat, "");
  42. var stop = (new Date()).getTime();
  43. console.log("elapsed: " + (stop - start) / 1000 + " s" );
  44. console.log(stat.count + " test(s) run");
  45. if (!stat.failCount)
  46. console.log("All OK!");
  47. else
  48. console.log(stat.failCount + " test(s) failed");
  49. }
  50. exports.run = run;
  51. exports.TestError = TestError;