Modified example for css(String) as proposed in the list (concerning camlCase for...
[jquery.git] / build / test / js / test.js
1 var queue = [];
2 var blocking = false;
3 var fixture;
4
5 function reset() {
6         if(fixture) {
7                 $("#main").html(fixture);
8         } else {
9                 fixture = $("#main").html();
10         }
11 }
12
13 function synchronize(callback) {
14         queue[queue.length] = callback;
15         if(!blocking) {
16                 process();
17         }
18 }
19
20 function process() {
21         while(queue.length && !blocking) {
22                 var call = queue[0];
23                 queue = queue.slice(1);
24                 call();
25         }
26 }
27
28 function runTests(files) {
29         var fixture = null;
30         reset();
31         var startTime = new Date();
32         for( var i=0; i < files.length; i++) {
33                 runTest( files, i );
34         }
35         synchronize(function() {
36                 var runTime = new Date() - startTime;
37                 $('body').append('<br/>Tests completed in ' + runTime + ' milliseconds.');
38         });
39 }
40
41 function runTest( files, num ) {
42         synchronize(function() {
43                 blocking = true;
44                 $.get(files[num],function(js){
45                         js = js.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
46         
47                         try {
48                                 eval(js);
49                         } catch(e) {
50                                 if(typeof console != "undefined") 
51                                         console.error(e);
52                                 Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
53                         }
54         
55                         var good = 0, bad = 0;
56                         var ol = document.createElement("ol");
57         
58                         var li = "", state = "pass";
59                         for ( var i = 0; i < Test.length; i++ ) {
60                                 var li = document.createElement("li");
61                                 li.className = Test[i][0] ? "pass" : "fail";
62                                 li.innerHTML = Test[i][1];
63                                 ol.appendChild( li );
64         
65                                 if ( !Test[i][0] ) {
66                                         state = "fail";
67                                         bad++;
68                                 } else good++;
69                         }
70         
71                         var li = document.createElement("li");
72                         li.className = state;
73         
74                         var b = document.createElement("b");
75                         b.innerHTML = files[num] + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
76                         b.onclick = function(){
77                                 var n = this.nextSibling;
78                                 if ( jQuery.css( n, "display" ) == "none" )
79                                         n.style.display = "block";
80                                 else
81                                         n.style.display = "none";
82                         };
83                         li.appendChild( b );
84         
85                         li.appendChild( ol );
86         
87                         document.getElementById("tests").appendChild( li );
88         
89                         Test = [];
90                         reset();
91                         blocking = false;
92                         process();
93                 });
94         });
95 }
96
97 var Test = [];
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
144 function o(a) {
145         var li = document.createElement("li");
146         li.innerHTML = a;
147         if ( a.indexOf("#") == 0 )
148                 li.className = "comment";
149         else if ( a.indexOf("TODO") >= 0 )
150                 li.className = "todo";
151         else if ( a.indexOf("not ok") == 0 )
152                 li.classname = "fail";
153         else
154                 li.className = "pass";
155         document.getElementById("test").appendChild(li);
156 }
157
158 //plan({noPlan: true});