4 * @author Michael Mathews micmath@gmail.com
5 * @url $HeadURL: https://jsdoc-toolkit.googlecode.com/svn/tags/jsdoc_toolkit-2.4.0/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.)
12 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.
16 function testrun(testCases
) {
18 for (t
in testCases
) {
19 var result
= testCases
[t
]();
23 return testrun
.reportOut
+"-------------------------------\n"+((testrun
.fails
>0)? ":( Failed "+testrun
.fails
+"/" : ":) Passed all ")+testrun
.count
+" test"+((testrun
.count
== 1)? "":"s")+".\n";
28 testrun
.current
= null;
31 testrun
.reportOut
= "";
34 testrun
.report
= function(text
) {
35 testrun
.reportOut
+= text
+"\n";
39 Check if test evaluates to true.
40 @param {string} test To be evaluated.
41 @param {string} message Optional. To be displayed in the report.
42 @return {boolean} True if the string test evaluates to true.
44 ok
= function(test
, message
) {
53 testrun
.report(" OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));
57 testrun
.report("NOT OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));
62 testrun
.report("NOT OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));
68 Check if test is same as expected.
69 @param {string} test To be evaluated.
70 @param {string} expected
71 @param {string} message Optional. To be displayed in the report.
72 @return {boolean} True if (test == expected). Note that the comparison is not a strict equality check.
74 is
= function(test
, expected
, message
) {
81 if (result
== expected
) {
83 testrun
.report(" OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));
87 testrun
.report("NOT OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));
88 testrun
.report("expected: "+expected
);
89 testrun
.report(" got: "+result
);
94 testrun
.report("NOT OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));
95 testrun
.report("expected: "+expected
);
96 testrun
.report(" got: "+result
);}
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.
106 like
= function(test
, pattern
, message
) {
112 var rgx
= new RegExp(pattern
);
114 if (rgx
.test(result
)) {
116 testrun
.report(" OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));
120 testrun
.report("NOT OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));
121 testrun
.report(" this: "+result
);
122 testrun
.report("is not like: "+pattern
);
127 testrun
.report("NOT OK "+testrun
.count
+" - "+((message
!= null)? message
: ""));