PageRenderTime 33ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.0.1/jsdoc-toolkit/app/frame/Testrun.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 129 lines | 79 code | 15 blank | 35 comment | 18 complexity | 2627d646ff96e75711535fd746eb2e21 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-2.0.1/jsdoc-toolkit/app/frame/Testrun.js $
  6. * @revision $Id: Testrun.js 418 2008-01-15 21:40:33Z 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. Yet another unit testing tool for JavaScript.
  12. @author Michael Mathews <a href="mailto:micmath@gmail.com">micmath@gmail.com</a>
  13. @param {object} testCases Properties are testcase names, values are functions to execute as tests.
  14. */
  15. function testrun(testCases) {
  16. var ran = 0;
  17. for (t in testCases) {
  18. var result = testCases[t]();
  19. ran++;
  20. }
  21. return testrun.reportOut+"-------------------------------\n"+((testrun.fails>0)? ":( Failed "+testrun.fails+"/" : ":) Passed all ")+testrun.count+" test"+((testrun.count == 1)? "":"s")+".\n";
  22. }
  23. testrun.count = 0;
  24. testrun.current = null;
  25. testrun.passes = 0;
  26. testrun.fails = 0;
  27. testrun.reportOut = "";
  28. /** @private */
  29. testrun.report = function(text) {
  30. testrun.reportOut += text+"\n";
  31. }
  32. /**
  33. Check if test evaluates to true.
  34. @param {string} test To be evaluated.
  35. @param {string} message Optional. To be displayed in the report.
  36. @return {boolean} True if the string test evaluates to true.
  37. */
  38. ok = function(test, message) {
  39. testrun.count++;
  40. var result;
  41. try {
  42. result = eval(test);
  43. if (result) {
  44. testrun.passes++;
  45. testrun.report(" OK "+testrun.count+" - "+((message != null)? message : ""));
  46. }
  47. else {
  48. testrun.fails++;
  49. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  50. }
  51. }
  52. catch(e) {
  53. testrun.fails++
  54. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  55. }
  56. }
  57. /**
  58. Check if test is same as expected.
  59. @param {string} test To be evaluated.
  60. @param {string} expected
  61. @param {string} message Optional. To be displayed in the report.
  62. @return {boolean} True if (test == expected). Note that the comparison is not a strict equality check.
  63. */
  64. is = function(test, expected, message) {
  65. testrun.count++;
  66. var result;
  67. try {
  68. result = eval(test);
  69. if (result == expected) {
  70. testrun.passes++
  71. testrun.report(" OK "+testrun.count+" - "+((message != null)? message : ""));
  72. }
  73. else {
  74. testrun.fails++
  75. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  76. testrun.report("expected: "+expected);
  77. testrun.report(" got: "+result);
  78. }
  79. }
  80. catch(e) {
  81. testrun.fails++
  82. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  83. testrun.report("expected: "+expected);
  84. testrun.report(" got: "+result);}
  85. }
  86. /**
  87. Check if test matches pattern.
  88. @param {string} test To be evaluated.
  89. @param {string} pattern Used to create a RegExp.
  90. @param {string} message Optional. To be displayed in the report.
  91. @return {boolean} True if test matches pattern.
  92. */
  93. like = function(test, pattern, message) {
  94. testrun.count++;
  95. var result;
  96. try {
  97. result = eval(test);
  98. var rgx = new RegExp(pattern);
  99. if (rgx.test(result)) {
  100. testrun.passes++
  101. testrun.report(" OK "+testrun.count+" - "+((message != null)? message : ""));
  102. }
  103. else {
  104. testrun.fails++
  105. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  106. testrun.report(" this: "+result);
  107. testrun.report("is not like: "+pattern);
  108. }
  109. }
  110. catch(e) {
  111. testrun.fails++
  112. testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
  113. }
  114. }