PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms 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/**
 12	Runs tests and prints out report.
 13	@class Yet another unit testing tool for JavaScript.
 14	@author Michael Mathews <a href="mailto:micmath@gmail.com">micmath@gmail.com</a>
 15	@param {object} testCases Properties are testcase names, values are functions to execute as tests.
 16*/
 17function testrun(testCases) {
 18	var ran = 0;
 19	for (t in testCases) {
 20		var result = testCases[t]();
 21		ran++;
 22	}
 23	
 24	return testrun.reportOut+"-------------------------------\n"+((testrun.fails>0)? ":( Failed "+testrun.fails+"/" : ":) Passed all ")+testrun.count+" test"+((testrun.count == 1)? "":"s")+".\n";
 25}
 26
 27/** @memberOf testrun
 28	@static */
 29testrun.count = 0;
 30/** @memberOf testrun
 31	@static */
 32testrun.current = null;
 33/** @memberOf testrun
 34	@static */
 35testrun.passes = 0;
 36/** @memberOf testrun
 37	@static */
 38testrun.fails = 0;
 39/** @memberOf testrun
 40	@static */
 41testrun.reportOut = "";
 42
 43/** Add text to the report.
 44	@memberOf testrun
 45	@static */
 46testrun.report = function(text) {
 47	testrun.reportOut += text+"\n";
 48}
 49
 50/**
 51	Check if test evaluates to true.
 52	@param {string} test To be evaluated.
 53	@param {string} message Optional. To be displayed in the report.
 54	@return {boolean} True if the string test evaluates to true.
 55*/
 56ok = function(test, message) {
 57	testrun.count++;
 58	
 59	var result;
 60	try {
 61		result = eval(test);
 62		
 63		if (result) {
 64			testrun.passes++;
 65			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
 66		}
 67		else {
 68			testrun.fails++;
 69			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
 70		}
 71	}
 72	catch(e) {
 73		testrun.fails++
 74		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
 75
 76	}
 77}
 78
 79/**
 80	Check if test is same as expected.
 81	@param {string} test To be evaluated.
 82	@param {string} expected
 83	@param {string} message Optional. To be displayed in the report. 
 84	@return {boolean} True if (test == expected). Note that the comparison is not a strict equality check.
 85*/
 86is = function(test, expected, message) {
 87	testrun.count++;
 88	
 89	var result;
 90	try {
 91		result = eval(test);
 92		
 93		if (result == expected) {
 94			testrun.passes++
 95			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
 96		}
 97		else {
 98			testrun.fails++
 99			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
100			testrun.report("expected: "+expected);
101			testrun.report("     got: "+result);
102		}
103	}
104	catch(e) {
105		testrun.fails++
106		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
107		testrun.report("expected: "+expected);
108		testrun.report("     got: "+result);}
109}
110
111/**
112	Check if test matches pattern.
113	@param {string} test To be evaluated.
114	@param {string} pattern Used to create a RegExp.
115	@param {string} message Optional. To be displayed in the report.
116	@return {boolean} True if test matches pattern.
117*/
118like = function(test, pattern, message) {
119	testrun.count++;
120
121	var result;
122	try {
123		result = eval(test);
124		var rgx = new RegExp(pattern);
125		
126		if (rgx.test(result)) {
127			testrun.passes++
128			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
129		}
130		else {
131			testrun.fails++
132			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
133			testrun.report("       this: "+result);
134			testrun.report("is not like: "+pattern);
135		}
136	}
137	catch(e) {
138		testrun.fails++
139		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
140	}
141}