Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tools/jsdoc-toolkit/app/frame/Testrun.js
blob: dbd80fc16629405440f73f581cee3151791ca090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * @fileOverview
 * @name JsTestrun
 * @author Michael Mathews micmath@gmail.com
 * @url $HeadURL: https://jsdoc-toolkit.googlecode.com/svn/tags/jsdoc_toolkit-2.3.2/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 : ""));
	}
}