sanitytest.js 4.0 KB

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