Refactored the testsuite code with the hope in mind to make the test suite run faster
[jquery.git] / build / test / js / test.js
1 var queue = [];
2 var blocking = false;
3
4 function reset(fixture) {
5         synchronize(function() {
6                 document.getElementById('main').innerHTML = fixture;
7         });
8 }
9
10 function synchronize(callback) {
11         queue[queue.length] = callback;
12         if(!blocking) {
13                 process();
14         }
15 }
16
17 function process() {
18         while(queue.length && !blocking) {
19                 var call = queue[0];
20                 queue = queue.slice(1);
21                 call();
22         }
23 }
24
25 function runTests(files) {
26         var fixture = document.getElementById('main').innerHTML;
27         var startTime = new Date();
28         for( var i=0; i < files.length; i++) {
29                 runTest( files, i );
30                 reset(fixture);
31         }
32         synchronize(function() {
33                 var runTime = new Date() - startTime;
34                 // lets use jQuery for this, its not important anyway
35                 $('body').append('<br/>Tests completed in ' + runTime + ' milliseconds.');
36         });
37 }
38
39 function runTest( files, num ) {
40         synchronize(function() {
41                 blocking = true;
42                 $.get(files[num],function(js) {
43                         evaluateTest(files, num, js);
44                 });
45         });
46 }
47
48 function evaluateTest(files, num, js) {
49         var Test = [];
50         js = js.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
51         
52         try {
53                 eval(js);
54         } catch(e) {
55                 if(typeof console != "undefined") 
56                         console.error(e);
57                         console.debug(js);
58                 Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
59         }
60
61         var good = 0, bad = 0;
62         var ol = document.createElement("ol");
63
64         var li = "", state = "pass";
65         for ( var i = 0; i < Test.length; i++ ) {
66                 var li = document.createElement("li");
67                 li.className = Test[i][0] ? "pass" : "fail";
68                 li.innerHTML = Test[i][1];
69                 ol.appendChild( li );
70
71                 if ( !Test[i][0] ) {
72                         state = "fail";
73                         bad++;
74                 } else good++;
75         }
76
77         var li = document.createElement("li");
78         li.className = state;
79
80         var b = document.createElement("b");
81         b.innerHTML = files[num] + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
82         b.onclick = function(){
83                 var n = this.nextSibling;
84                 if ( jQuery.css( n, "display" ) == "none" )
85                         n.style.display = "block";
86                 else
87                         n.style.display = "none";
88         };
89         li.appendChild( b );
90
91         li.appendChild( ol );
92
93         document.getElementById("tests").appendChild( li );
94
95         blocking = false;
96         process();
97         
98         function ok(a, msg) {
99                 Test.push( [ !!a, msg ] );
100         }
101         
102         function cmpOK( a, c, b, msg ) {
103                 var res;
104                 eval( "res = (a " + c + " b)" );
105                 Test.push( [ res, msg ] );
106         }
107         
108         function isSet(a, b, msg) {
109                 var ret = true;
110         
111                 if ( a && b && a.length == b.length ) {
112                         for ( var i in a )
113                                 if ( a[i] != b[i] )
114                                         ret = false;
115                 } else
116                         ret = false;
117         
118                 if ( !ret && console )
119                         console.log( msg, a, b );
120         
121                 Test.push( [ ret, msg ] );
122         }
123         
124         function q() {
125                 var r = [];
126         
127                 for ( var i = 0; i < arguments.length; i++ )
128                         r.push( document.getElementById( arguments[i] ) );
129         
130                 return r;
131         }
132         
133         function t(a,b,c) {
134                 var f = jQuery.find(b);
135         
136                 var s = "";
137                 for ( var i = 0; i < f.length; i++ )
138                         s += (s && ",") + '"' + f[i].id + '"';
139         
140                 isSet(f, q.apply(q,c), a + " (" + b + ")");
141         }
142 }