d478b63678da7a7f96eda6150975f7ce25ff1e25
[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                 }
59                 Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
60         }
61
62         var good = 0, bad = 0;
63         var ol = document.createElement("ol");
64
65         var li = "", state = "pass";
66         for ( var i = 0; i < Test.length; i++ ) {
67                 var li = document.createElement("li");
68                 li.className = Test[i][0] ? "pass" : "fail";
69                 li.innerHTML = Test[i][1];
70                 ol.appendChild( li );
71
72                 if ( !Test[i][0] ) {
73                         state = "fail";
74                         bad++;
75                 } else good++;
76         }
77
78         var li = document.createElement("li");
79         li.className = state;
80
81         var b = document.createElement("b");
82         b.innerHTML = files[num] + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
83         b.onclick = function(){
84                 var n = this.nextSibling;
85                 if ( jQuery.css( n, "display" ) == "none" )
86                         n.style.display = "block";
87                 else
88                         n.style.display = "none";
89         };
90         li.appendChild( b );
91
92         li.appendChild( ol );
93
94         document.getElementById("tests").appendChild( li );
95
96         blocking = false;
97         process();
98         
99         function ok(a, msg) {
100                 Test.push( [ !!a, msg ] );
101         }
102         
103         function cmpOK( a, c, b, msg ) {
104                 var res;
105                 eval( "res = (a " + c + " b)" );
106                 Test.push( [ res, msg ] );
107         }
108         
109         function isSet(a, b, msg) {
110                 var ret = true;
111         
112                 if ( a && b && a.length == b.length ) {
113                         for ( var i in a )
114                                 if ( a[i] != b[i] )
115                                         ret = false;
116                 } else
117                         ret = false;
118         
119                 if ( !ret && console )
120                         console.log( msg, a, b );
121         
122                 Test.push( [ ret, msg ] );
123         }
124         
125         function q() {
126                 var r = [];
127         
128                 for ( var i = 0; i < arguments.length; i++ )
129                         r.push( document.getElementById( arguments[i] ) );
130         
131                 return r;
132         }
133         
134         function t(a,b,c) {
135                 var f = jQuery.find(b);
136         
137                 var s = "";
138                 for ( var i = 0; i < f.length; i++ )
139                         s += (s && ",") + '"' + f[i].id + '"';
140         
141                 isSet(f, q.apply(q,c), a + " (" + b + ")");
142         }
143 }