PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/jsdoc_toolkit-1.3.3/app/JsTestrun.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 141 lines | 79 code | 14 blank | 48 comment | 18 complexity | e7957deacdb17288fe33e48437b6af34 MD5 | raw file
  1. /**
  2. * @fileOverview
  3. * @name JsTestrun
  4. * @author Michael Mathews micmath@gmail.com
  5. * @url $HeadURL: http://jsdoc-toolkit.googlecode.com/svn/tags/jsdoc_toolkit-1.3.3/app/JsTestrun.js $
  6. * @revision $Id: JsTestrun.js 213 2007-08-22 10:21:50Z micmath $
  7. * @license <a href="http://en.wikipedia.org/wiki/MIT_License">X11/MIT License</a>
  8. * (See the accompanying README file for full details.)
  9. */
  10. /**
  11. Runs tests and prints out report.
  12. @class Yet another unit testing tool for JavaScript.
  13. @author Michael Mathews <a href="mailto:micmath@gmail.com">micmath@gmail.com</a>
  14. @param {object} testCases Properties are testcase names, values are functions to execute as tests.
  15. */
  16. function testrun(testCases) {
  17. var ran = 0;
  18. for (t in testCases) {
  19. var result = testCases[t]();
  20. ran++;
  21. }
  22. return testrun.reportOut+"-------------------------------\n"+((testrun.fails>0)? ":( Failed "+testrun.fails+"/" : ":) Passed all ")+testrun.count+" test"+((testrun.count == 1)? "":"s")+".\n";
  23. }
  24. /** @memberOf testrun
  25. @static */
  26. testrun.count = 0;
  27. /** @memberOf testrun
  28. @static */
  29. testrun.current = null;
  30. /** @memberOf testrun
  31. @static */
  32. testrun.passes = 0;
  33. /** @memberOf testrun
  34. @static */
  35. testrun.fails = 0;
  36. /** @memberOf testrun
  37. @static */
  38. testrun.reportOut = "";
  39. /** Add text to the report.
  40. @memberOf testrun
  41. @static */
  42. testrun.report = function(text) {
  43. testrun.reportOut += text+"\n";
  44. }
  45. /**
  46. Check if test evaluates to true.
  47. @param {string} test To be evaluated.
  48. @param {string} message Optional. To be displayed in the report.
  49. @return {boolean} True if the string test evaluates to true.
  50. */
  51. ok = function(test, message) {
  52. testrun.count++;
  53. var result;
  54. try {
  55. result = eval(test);
  56. if (result) {
  57. testrun.passes++;
  58. testrun.report(" OK "+testrun.count+" - "+((message != null)? message : ""));
  59. }
  60. else {
  61. testrun.fails++;
  62. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  63. }
  64. }
  65. catch(e) {
  66. testrun.fails++
  67. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  68. }
  69. }
  70. /**
  71. Check if test is same as expected.
  72. @param {string} test To be evaluated.
  73. @param {string} expected
  74. @param {string} message Optional. To be displayed in the report.
  75. @return {boolean} True if (test == expected). Note that the comparison is not a strict equality check.
  76. */
  77. is = function(test, expected, message) {
  78. testrun.count++;
  79. var result;
  80. try {
  81. result = eval(test);
  82. if (result == expected) {
  83. testrun.passes++
  84. testrun.report(" OK "+testrun.count+" - "+((message != null)? message : ""));
  85. }
  86. else {
  87. testrun.fails++
  88. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  89. testrun.report("expected: "+expected);
  90. testrun.report(" got: "+result);
  91. }
  92. }
  93. catch(e) {
  94. testrun.fails++
  95. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  96. testrun.report("expected: "+expected);
  97. testrun.report(" got: "+result);}
  98. }
  99. /**
  100. Check if test matches pattern.
  101. @param {string} test To be evaluated.
  102. @param {string} pattern Used to create a RegExp.
  103. @param {string} message Optional. To be displayed in the report.
  104. @return {boolean} True if test matches pattern.
  105. */
  106. like = function(test, pattern, message) {
  107. testrun.count++;
  108. var result;
  109. try {
  110. result = eval(test);
  111. var rgx = new RegExp(pattern);
  112. if (rgx.test(result)) {
  113. testrun.passes++
  114. testrun.report(" OK "+testrun.count+" - "+((message != null)? message : ""));
  115. }
  116. else {
  117. testrun.fails++
  118. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  119. testrun.report(" this: "+result);
  120. testrun.report("is not like: "+pattern);
  121. }
  122. }
  123. catch(e) {
  124. testrun.fails++
  125. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  126. }
  127. }