Testcase now logs exceptions if a console is avaiable, makes it easier to find bugs...
[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                                 if(typeof console != "undefined") 
55                                         console.error(e);
56                                 Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
57                         }
58         
59                         var good = 0, bad = 0;
60                         var ol = document.createElement("ol");
61         
62                         var li = "", state = "pass";
63                         for ( var i = 0; i < Test.length; i++ ) {
64                                 var li = document.createElement("li");
65                                 li.className = Test[i][0] ? "pass" : "fail";
66                                 li.innerHTML = Test[i][1];
67                                 ol.appendChild( li );
68         
69                                 if ( !Test[i][0] ) {
70                                         state = "fail";
71                                         bad++;
72                                 } else good++;
73                         }
74         
75                         var li = document.createElement("li");
76                         li.className = state;
77         
78                         var b = document.createElement("b");
79                         b.innerHTML = files[num] + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
80                         b.onclick = function(){
81                                 var n = this.nextSibling;
82                                 if ( jQuery.css( n, "display" ) == "none" )
83                                         n.style.display = "block";
84                                 else
85                                         n.style.display = "none";
86                         };
87                         li.appendChild( b );
88         
89                         li.appendChild( ol );
90         
91                         document.getElementById("tests").appendChild( li );
92         
93                         Test = [];
94                         blocking = false;
95                         process();
96                 });
97         });
98 }
99
100 var Test = [];
101
102 function ok(a, msg) {
103         Test.push( [ !!a, msg ] );
104 }
105
106 function cmpOK( a, c, b, msg ) {
107         var res;
108         eval( "res = (a " + c + " b)" );
109         Test.push( [ res, msg ] );
110 }
111
112 function isSet(a, b, msg) {
113         var ret = true;
114
115         if ( a && b && a.length == b.length ) {
116                 for ( var i in a )
117                         if ( a[i] != b[i] )
118                                 ret = false;
119         } else
120                 ret = false;
121
122         if ( !ret && console )
123                 console.log( msg, a, b );
124
125         Test.push( [ ret, msg ] );
126 }
127
128 function q() {
129         var r = [];
130
131         for ( var i = 0; i < arguments.length; i++ )
132                 r.push( document.getElementById( arguments[i] ) );
133
134         return r;
135 }
136
137 function t(a,b,c) {
138         var f = jQuery.find(b);
139
140         var s = "";
141         for ( var i = 0; i < f.length; i++ )
142                 s += (s && ",") + '"' + f[i].id + '"';
143
144         isSet(f, q.apply(q,c), a + " (" + b + ")");
145 }
146
147 function o(a) {
148         var li = document.createElement("li");
149         li.innerHTML = a;
150         if ( a.indexOf("#") == 0 )
151                 li.className = "comment";
152         else if ( a.indexOf("TODO") >= 0 )
153                 li.className = "todo";
154         else if ( a.indexOf("not ok") == 0 )
155                 li.classname = "fail";
156         else
157                 li.className = "pass";
158         document.getElementById("test").appendChild(li);
159 }
160
161 //plan({noPlan: true});