PageRenderTime 36ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 2ms 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/**
 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.
 15*/
 16function testrun(testCases) {
 17	var ran = 0;
 18	for (t in testCases) {
 19		var result = testCases[t]();
 20		ran++;
 21	}
 22	
 23	return testrun.reportOut+"-------------------------------\n"+((testrun.fails>0)? ":( Failed "+testrun.fails+"/" : ":) Passed all ")+testrun.count+" test"+((testrun.count == 1)? "":"s")+".\n";
 24}
 25
 26
 27testrun.count = 0;
 28testrun.current = null;
 29testrun.passes = 0;
 30testrun.fails = 0;
 31testrun.reportOut = "";
 32
 33/** @private */
 34testrun.report = function(text) {
 35	testrun.reportOut += text+"\n";
 36}
 37
 38/**
 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.
 43*/
 44ok = function(test, message) {
 45	testrun.count++;
 46	
 47	var result;
 48	try {
 49		result = eval(test);
 50		
 51		if (result) {
 52			testrun.passes++;
 53			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
 54		}
 55		else {
 56			testrun.fails++;
 57			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
 58		}
 59	}
 60	catch(e) {
 61		testrun.fails++
 62		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
 63
 64	}
 65}
 66
 67/**
 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.
 73*/
 74is = function(test, expected, message) {
 75	testrun.count++;
 76	
 77	var result;
 78	try {
 79		result = eval(test);
 80		
 81		if (result == expected) {
 82			testrun.passes++
 83			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
 84		}
 85		else {
 86			testrun.fails++
 87			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
 88			testrun.report("expected: "+expected);
 89			testrun.report("     got: "+result);
 90		}
 91	}
 92	catch(e) {
 93		testrun.fails++
 94		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
 95		testrun.report("expected: "+expected);
 96		testrun.report("     got: "+result);}
 97}
 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*/
106like = function(test, pattern, message) {
107	testrun.count++;
108
109	var result;
110	try {
111		result = eval(test);
112		var rgx = new RegExp(pattern);
113		
114		if (rgx.test(result)) {
115			testrun.passes++
116			testrun.report("    OK "+testrun.count+" - "+((message != null)? message : ""));
117		}
118		else {
119			testrun.fails++
120			testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
121			testrun.report("       this: "+result);
122			testrun.report("is not like: "+pattern);
123		}
124	}
125	catch(e) {
126		testrun.fails++
127		testrun.report("NOT OK "+testrun.count+" - "+((message != null)? message : ""));
128	}
129}