sanitytest.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // simple testing interface
  3. // written by Einar Lielmanis, einar@jsbeautifier.org
  4. //
  5. // usage:
  6. //
  7. // var t = new SanityTest(function (x) { return x; }, 'my function');
  8. // t.expect('input', 'output');
  9. // t.expect('a', 'a');
  10. // output_somewhere(t.results()); // good for <pre>, html safe-ish
  11. // alert(t.results_raw()); // html unescaped
  12. function SanityTest (func, name_of_test) {
  13. var test_func = func || function (x) {
  14. return x;
  15. };
  16. var test_name = name_of_test || '';
  17. var n_failed = 0;
  18. var n_succeeded = 0;
  19. this.failures = [];
  20. this.successes = [];
  21. this.test_function = function(func, name) {
  22. test_func = func;
  23. test_name = name || '';
  24. };
  25. this.get_exitcode = function() {
  26. return n_succeeded === 0 || n_failed !== 0 ? 1 : 0;
  27. };
  28. this.expect = function(parameters, expected_value) {
  29. // multi-parameter calls not supported (I don't need them now).
  30. var result = test_func(parameters);
  31. // proper array checking is a pain. i'll maybe do it later, compare strings representations instead
  32. if ((result === expected_value) || (expected_value instanceof Array && result.join(', ') == expected_value.join(', '))) {
  33. n_succeeded += 1;
  34. this.successes.push([test_name, parameters, expected_value, result]);
  35. } else {
  36. n_failed += 1;
  37. this.failures.push([test_name, parameters, expected_value, result]);
  38. }
  39. };
  40. this.results_raw = function() {
  41. var results = '';
  42. if (n_failed === 0) {
  43. if (n_succeeded === 0) {
  44. results = 'No tests run.';
  45. } else {
  46. results = 'All ' + n_succeeded + ' tests passed.';
  47. }
  48. } else {
  49. for (var i = 0 ; i < this.failures.length; i++) {
  50. var f = this.failures[i];
  51. if (f[0]) {
  52. f[0] = f[0] + ' ';
  53. }
  54. results += '---- ' + f[0] + 'input -------\n' + this.prettyprint(f[1]) + '\n';
  55. results += '---- ' + f[0] + 'expected ----\n' + this.prettyprint(f[2]) + '\n';
  56. results += '---- ' + f[0] + 'output ------\n' + this.prettyprint(f[3]) + '\n\n';
  57. }
  58. results += n_failed + ' tests failed.\n';
  59. }
  60. return results;
  61. };
  62. this.results = function() {
  63. return this.lazy_escape(this.results_raw());
  64. };
  65. this.prettyprint = function(something, quote_strings) {
  66. var type = typeof something;
  67. switch(type.toLowerCase()) {
  68. case 'string':
  69. if (quote_strings) {
  70. return "'" + something.replace("'", "\\'") + "'";
  71. } else {
  72. return something;
  73. }
  74. case 'number':
  75. return '' + something;
  76. case 'boolean':
  77. return something ? 'true' : 'false';
  78. case 'undefined':
  79. return 'undefined';
  80. case 'object':
  81. if (something instanceof Array) {
  82. var x = [];
  83. var expected_index = 0;
  84. for (var k in something) {
  85. if (k == expected_index) {
  86. x.push(this.prettyprint(something[k], true));
  87. expected_index += 1;
  88. } else {
  89. x.push('\n' + k + ': ' + this.prettyprint(something[k], true));
  90. }
  91. }
  92. return '[' + x.join(', ') + ']';
  93. } else {
  94. return 'object: ' + something;
  95. }
  96. default:
  97. return type + ': ' + something;
  98. }
  99. };
  100. this.lazy_escape = function (str) {
  101. return str.replace(/</g, '&lt;').replace(/\>/g, '&gt;').replace(/\n/g, '<br />');
  102. };
  103. this.log = function () {
  104. if (window.console) {
  105. if (console.firebug) {
  106. console.log.apply(console, Array.prototype.slice.call(arguments));
  107. } else {
  108. console.log.call(console, Array.prototype.slice.call(arguments));
  109. }
  110. }
  111. };
  112. }
  113. if (typeof module !== 'undefined' && module.exports) {
  114. module.exports = SanityTest;
  115. }