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