thirdparty/jsdoctoolkit/app/frame/Testrun.js
author Mario Ferraro <fadinlight@gmail.com>
Sun, 15 Nov 2009 22:12:20 +0100
changeset 3093 d1be59b6b627
parent 3041 c8f47f0b6697
permissions -rw-r--r--
GMaps related JS changed to use new google namespace. Google is going to change permanently in the future the way to load its services, so better stay safe. Also this commit shows uses of the new melange.js module. Fixes Issue 634.

/**
 * @fileOverview
 * @name JsTestrun
 * @author Michael Mathews micmath@gmail.com
 * @url $HeadURL: https://jsdoc-toolkit.googlecode.com/svn/trunk/jsdoc-toolkit/app/frame/Testrun.js $
 * @revision $Id: Testrun.js 418 2008-01-15 21:40:33Z micmath $
 * @license <a href="http://en.wikipedia.org/wiki/MIT_License">X11/MIT License</a>
 *          (See the accompanying README file for full details.)
 */
 
/**
	Yet another unit testing tool for JavaScript.
	@author Michael Mathews <a href="mailto:micmath@gmail.com">micmath@gmail.com</a>
	@param {object} testCases Properties are testcase names, values are functions to execute as tests.
*/
function testrun(testCases) {
	var ran = 0;
	for (t in testCases) {
		var result = testCases[t]();
		ran++;
	}
	
	return testrun.reportOut+"-------------------------------\n"+((testrun.fails>0)? ":( Failed "+testrun.fails+"/" : ":) Passed all ")+testrun.count+" test"+((testrun.count == 1)? "":"s")+".\n";
}


testrun.count = 0;
testrun.current = null;
testrun.passes = 0;
testrun.fails = 0;
testrun.reportOut = "";

/** @private */
testrun.report = function(text) {
	testrun.reportOut += text+"\n";
}

/**
	Check if test evaluates to true.
	@param {string} test To be evaluated.
	@param {string} message Optional. To be displayed in the report.
	@return {boolean} True if the string test evaluates to true.
*/
ok = function(test, message) {
	testrun.count++;
	
	var result;
	try {
		result = eval(test);
		
		if (result) {
			testrun.passes++;
			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
		}
		else {
			testrun.fails++;
			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
		}
	}
	catch(e) {
		testrun.fails++
		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));

	}
}

/**
	Check if test is same as expected.
	@param {string} test To be evaluated.
	@param {string} expected
	@param {string} message Optional. To be displayed in the report. 
	@return {boolean} True if (test == expected). Note that the comparison is not a strict equality check.
*/
is = function(test, expected, message) {
	testrun.count++;
	
	var result;
	try {
		result = eval(test);
		
		if (result == expected) {
			testrun.passes++
			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
		}
		else {
			testrun.fails++
			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
			testrun.report("expected: "+expected);
			testrun.report("     got: "+result);
		}
	}
	catch(e) {
		testrun.fails++
		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
		testrun.report("expected: "+expected);
		testrun.report("     got: "+result);}
}

/**
	Check if test matches pattern.
	@param {string} test To be evaluated.
	@param {string} pattern Used to create a RegExp.
	@param {string} message Optional. To be displayed in the report.
	@return {boolean} True if test matches pattern.
*/
like = function(test, pattern, message) {
	testrun.count++;

	var result;
	try {
		result = eval(test);
		var rgx = new RegExp(pattern);
		
		if (rgx.test(result)) {
			testrun.passes++
			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
		}
		else {
			testrun.fails++
			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
			testrun.report("       this: "+result);
			testrun.report("is not like: "+pattern);
		}
	}
	catch(e) {
		testrun.fails++
		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
	}
}