Massive overhauls to the test suite - it is now generated dynamically, along with...
[jquery.git] / build / test / js / test.js
1 function runTests(files) {
2         runTest( files, 0 );
3 }
4
5 function runTest( files, num ) {
6         $.get(files[num],function(js){
7                 js = js.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
8
9                 try {
10                         eval(js);
11                 } catch(e) {
12                         Test.push( [ false, "Died on test #" + Test.length + ": " + e ] );
13                 }
14
15                 var good = 0, bad = 0;
16                 var ol = document.createElement("ol");
17
18                 var li = "", state = "pass";
19                 for ( var i = 0; i < Test.length; i++ ) {
20                         var li = document.createElement("li");
21                         li.className = Test[i][0] ? "pass" : "fail";
22                         li.innerHTML = Test[i][1];
23                         ol.appendChild( li );
24
25                         if ( !Test[i][0] ) {
26                                 state = "fail";
27                                 bad++;
28                         } else good++;
29                 }
30
31                 var li = document.createElement("li");
32                 li.className = state;
33
34                 var b = document.createElement("b");
35                 b.innerHTML = files[num] + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
36                 b.onclick = function(){
37                         var n = this.nextSibling;
38                         if ( jQuery.css( n, "display" ) == "none" )
39                                 n.style.display = "block";
40                         else
41                                 n.style.display = "none";
42                 };
43                 li.appendChild( b );
44
45                 li.appendChild( ol );
46
47                 document.getElementById("tests").appendChild( li );
48
49                 Test = [];
50                 if ( ++num < files.length ) runTest( files, num );
51         });
52 }
53
54 var Test = [];
55
56 function ok(a, msg) {
57         Test.push( [ !!a, msg ] );
58 }
59
60 function cmpOK( a, c, b, msg ) {
61         var res;
62         eval( "res = (a " + c + " b)" );
63         Test.push( [ res, msg ] );
64 }
65
66 function isSet(a, b, msg) {
67         var ret = true;
68
69         if ( a && b && a.length == b.length ) {
70                 for ( var i in a )
71                         if ( a[i] != b[i] )
72                                 ret = false;
73         } else
74                 ret = false;
75
76         if ( !ret && console )
77                 console.log( msg, a, b );
78
79         Test.push( [ ret, msg ] );
80 }
81
82 function q() {
83         var r = [];
84
85         for ( var i = 0; i < arguments.length; i++ )
86                 r.push( document.getElementById( arguments[i] ) );
87
88         return r;
89 }
90
91 function t(a,b,c) {
92         var f = jQuery.find(b);
93
94         var s = "";
95         for ( var i = 0; i < f.length; i++ )
96                 s += (s && ",") + '"' + f[i].id + '"';
97
98         isSet(f, q.apply(q,c), a + " (" + b + ")");
99 }
100
101 function o(a) {
102         var li = document.createElement("li");
103         li.innerHTML = a;
104         if ( a.indexOf("#") == 0 )
105                 li.className = "comment";
106         else if ( a.indexOf("TODO") >= 0 )
107                 li.className = "todo";
108         else if ( a.indexOf("not ok") == 0 )
109                 li.classname = "fail";
110         else
111                 li.className = "pass";
112         document.getElementById("test").appendChild(li);
113 }
114
115 //plan({noPlan: true});