PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.3.2/jsdoc-toolkit/app/t/TestDoc.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 144 lines | 121 code | 20 blank | 3 comment | 37 complexity | b2b6c69537dcd89333b243b24f93b9f7 MD5 | raw file
  1var TestDoc = {
  2	fails: 0,
  3	plans: 0,
  4	passes: 0,
  5	results: []
  6};
  7
  8TestDoc.record = function(result) {
  9	TestDoc.results.push(result);
 10	if (typeof result.verdict == "boolean") {
 11		if (result.verdict === false) TestDoc.fails++;
 12		if (result.verdict === true) TestDoc.passes++;
 13	}
 14}
 15
 16TestDoc.prove = function(filePath) {
 17	if (typeof document != "undefined" && typeof document.write != "undefined") {
 18		if (TestDoc.console) print = function(s) { TestDoc.console.appendChild(document.createTextNode(s+"\n")); }
 19		else print = function(s) { document.write(s+"<br />"); }
 20	}
 21	TestDoc.run(TestDoc.readFile(filePath));
 22}
 23
 24TestDoc.run = function(src) {
 25	try { eval(src); } catch(e) { print("# ERROR! "+e); }
 26	
 27	var chunks = src.split(/\/\*t:/);
 28	
 29	var run = function(chunk) {
 30		// local shortcuts
 31		var is = TestDoc.assertEquals;
 32		var isnt = TestDoc.assertNotEquals;
 33		var plan = TestDoc.plan;
 34		var requires = TestDoc.requires;
 35		
 36		try { eval(chunk); } catch(e) { print("# ERROR! "+e); }
 37	}
 38	for (var start = -1, end = 0; (start = src.indexOf("/*t:", end)) > end; start = end) {
 39		run(
 40			src.substring(
 41				start+4,
 42				(end = src.indexOf("*/", start))
 43			)
 44		);
 45	}
 46}
 47
 48TestDoc.Result = function(verdict, message) {
 49	this.verdict = verdict;
 50	this.message = message;
 51}
 52
 53TestDoc.Result.prototype.toString = function() {
 54	if (typeof this.verdict == "boolean") {
 55		return (this.verdict? "ok" : "not ok") + " " + (++TestDoc.report.counter) + " - " + this.message;
 56	}
 57	
 58	return "# " + this.message;
 59}
 60
 61TestDoc.requires = function(file) {
 62	if (!TestDoc.requires.loaded[file]) {
 63		load(file);
 64		TestDoc.requires.loaded[file] = true;
 65	}
 66}
 67TestDoc.requires.loaded = {};
 68
 69TestDoc.report = function() {
 70	TestDoc.report.counter = 0;
 71	print("1.."+TestDoc.plans);
 72	for (var i = 0; i < TestDoc.results.length; i++) {
 73		print(TestDoc.results[i]);
 74	}
 75	print("----------------------------------------");
 76	if (TestDoc.fails == 0 && TestDoc.passes == TestDoc.plans) {
 77		print("All tests successful.");
 78	}
 79	else {
 80		print("Failed " + TestDoc.fails + "/" + TestDoc.plans + " tests, "+((TestDoc.plans == 0)? 0 : Math.round(TestDoc.passes/(TestDoc.passes+TestDoc.fails)*10000)/100)+"% okay. Planned to run "+TestDoc.plans+", did run "+(TestDoc.passes+TestDoc.fails)+".")
 81	}
 82}
 83
 84TestDoc.plan = function(n, message) {
 85	TestDoc.plans += n;
 86	TestDoc.record(new TestDoc.Result(null, message+" ("+n+" tests)"));
 87}
 88
 89TestDoc.assertEquals = function(a, b, message) {
 90	var result = (a == b);
 91	if (!result) message += "\n#\n# " + a + " does not equal " + b + "\n#";
 92	TestDoc.record(new TestDoc.Result(result, message));
 93}
 94
 95TestDoc.assertNotEquals = function(a, b, message) {
 96	var result = (a != b);
 97	if (!result) message += "\n#\n# " + a + " equals " + b + "\n#";
 98	TestDoc.record(new TestDoc.Result(result, message));
 99}
100
101TestDoc.readFile = (function(){
102	// rhino
103	if (typeof readFile == "function") {
104		return function(url) {
105			var text = readFile(url);
106			return text || "";
107		}
108	}
109
110	// a web browser
111	else {
112		return function(url) {
113			var httpRequest;
114		
115			if (window.XMLHttpRequest) { // Mozilla, Safari, etc
116				httpRequest = new XMLHttpRequest();
117			} 
118			else if (window.ActiveXObject) { // IE
119				try {
120					httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
121				} 
122				catch (e) {
123				   try {
124						httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
125					} 
126					catch (e) {
127					}
128				}
129			}
130		
131			if (!httpRequest) { throw "Cannot create HTTP Request."; }
132			
133			httpRequest.open('GET', url, false);
134			httpRequest.send('');
135			if (httpRequest.readyState == 4) {
136				if (httpRequest.status >= 400) {
137					throw "The HTTP Request returned an error code: "+httpRequest.status;
138				}
139			}
140			
141			return httpRequest.responseText || "";
142		}
143	}
144})();