8 TestDoc
.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
++;
16 TestDoc
.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 />"); }
21 TestDoc
.run(TestDoc
.readFile(filePath
));
24 TestDoc
.run
= function(src
) {
25 try { eval(src
); } catch(e
) { print("# ERROR! "+e
); }
27 var chunks
= src
.split(/\/\*t
:/);
29 var run
= function(chunk
) {
31 var is
= TestDoc
.assertEquals
;
32 var isnt
= TestDoc
.assertNotEquals
;
33 var plan
= TestDoc
.plan
;
34 var requires
= TestDoc
.requires
;
36 try { eval(chunk
); } catch(e
) { print("# ERROR! "+e
); }
38 for (var start
= -1, end
= 0; (start
= src
.indexOf("/*t:", end
)) > end
; start
= end
) {
42 (end
= src
.indexOf("*/", start
))
48 TestDoc
.Result
= function(verdict
, message
) {
49 this.verdict
= verdict
;
50 this.message
= message
;
53 TestDoc
.Result
.prototype.toString
= function() {
54 if (typeof this.verdict
== "boolean") {
55 return (this.verdict
? "ok" : "not ok") + " " + (++TestDoc
.report
.counter
) + " - " + this.message
;
58 return "# " + this.message
;
61 TestDoc
.requires
= function(file
) {
62 if (!TestDoc
.requires
.loaded
[file
]) {
64 TestDoc
.requires
.loaded
[file
] = true;
67 TestDoc
.requires
.loaded
= {};
69 TestDoc
.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
]);
75 print("----------------------------------------");
76 if (TestDoc
.fails
== 0 && TestDoc
.passes
== TestDoc
.plans
) {
77 print("All tests successful.");
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)+".")
84 TestDoc.plan = function(n, message) {
86 TestDoc.record(new TestDoc.Result(null, message+" ("+n+" tests
)"));
89 TestDoc.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));
95 TestDoc.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));
101 TestDoc.readFile = (function(){
103 if (typeof readFile == "function") {
104 return function(url) {
105 var text = readFile(url);
112 return function(url) {
115 if (window.XMLHttpRequest) { // Mozilla, Safari, etc
116 httpRequest = new XMLHttpRequest();
118 else if (window.ActiveXObject) { // IE
120 httpRequest = new ActiveXObject("Msxml2
.XMLHTTP
");
124 httpRequest = new ActiveXObject("Microsoft
.XMLHTTP
");
131 if (!httpRequest) { throw "Cannot create HTTP Request
."; }
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;
141 return httpRequest.responseText || "";