test.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. }
  32. console.log(tab + log);
  33. }
  34. function run(tests){
  35. var stat = {count: 0, failCount: 0};
  36. var start = Date.now();
  37. if (typeof process != "undefined" && process.argv.length > 2)
  38. runTest(process.argv[2], tests, stat, "");
  39. else
  40. runImpl(tests, stat, "");
  41. var stop = Date.now();
  42. console.log("elapsed: " + (stop - start) / 1000 + " s" );
  43. console.log(stat.count + " test(s) run");
  44. if (!stat.failCount)
  45. console.log("All OK!");
  46. else
  47. console.log(stat.failCount + " test(s) failed");
  48. }
  49. exports.run = run;
  50. exports.TestError = TestError;