Refactored test suite: All tests are now compiled into one file, runs much faster...
[jquery.git] / build / test / js / test.js
1 function runTest(file) {
2         var startTime = new Date();
3         var Test;
4         var stats = {
5                 all: 0,
6                 bad: 0
7         };
8         var fixture = document.getElementById('main').innerHTML;
9         $.get(file,function(js) {
10                 js = js.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
11                 eval(js);
12                 var runTime = new Date() - startTime;
13                 var result = document.createElement("div");
14                 result.innerHTML = 'Tests completed in ' + runTime + ' milliseconds.<br/>' +
15                         stats.bad + ' tests of ' + stats.all + ' failed.';
16                 document.getElementsByTagName("body")[0].appendChild(result);
17         });
18         
19         function test(name, callback) {
20                 Test = [];
21                 try {
22                         callback();
23                 } catch(e) {
24                         if(typeof console != "undefined") {
25                                 console.error("Test " + name + " died, exception and test follows");
26                                 console.error(e);
27                                 console.warn(callback.toString());
28                         }
29                         Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
30                 }
31                 reset();
32                 
33                 var good = 0, bad = 0;
34                 var ol = document.createElement("ol");
35         
36                 var li = "", state = "pass";
37                 for ( var i = 0; i < Test.length; i++ ) {
38                         var li = document.createElement("li");
39                         li.className = Test[i][0] ? "pass" : "fail";
40                         li.innerHTML = Test[i][1];
41                         ol.appendChild( li );
42                         
43                         stats.all++;
44                         if ( !Test[i][0] ) {
45                                 state = "fail";
46                                 bad++;
47                                 stats.bad++;
48                         } else good++;
49                 }
50         
51                 var li = document.createElement("li");
52                 li.className = state;
53         
54                 var b = document.createElement("b");
55                 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
56                 b.onclick = function(){
57                         var n = this.nextSibling;
58                         if ( jQuery.css( n, "display" ) == "none" )
59                                 n.style.display = "block";
60                         else
61                                 n.style.display = "none";
62                 };
63                 li.appendChild( b );
64                 li.appendChild( ol );
65         
66                 document.getElementById("tests").appendChild( li );
67         }
68         
69         function reset() {
70                 document.getElementById('main').innerHTML = fixture;
71         }
72         
73         /**
74          * Asserts true.
75          * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
76          */
77         function ok(a, msg) {
78                 Test.push( [ !!a, msg ] );
79         }
80
81         /**
82          * Asserts that two arrays are the same
83          */
84         function isSet(a, b, msg) {
85                 var ret = true;
86                 if ( a && b && a.length == b.length ) {
87                         for ( var i in a )
88                                 if ( a[i] != b[i] )
89                                         ret = false;
90                 } else
91                         ret = false;
92                 if ( !ret && console )
93                         console.log( msg, a, b );
94                 Test.push( [ ret, msg ] );
95         }
96         
97         /**
98          * Returns an array of elements with the given IDs, eg.
99          * @example q("main", "foo", "bar")
100          * @result [<div id="main">, <span id="foo">, <input id="bar">]
101          */
102         function q() {
103                 var r = [];
104                 for ( var i = 0; i < arguments.length; i++ )
105                         r.push( document.getElementById( arguments[i] ) );
106                 return r;
107         }
108         
109         /**
110          * Asserts that a select matches the given IDs
111          * @example t("Check for something", "//[a]", ["foo", "baar"]);
112          * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
113          */
114         function t(a,b,c) {
115                 var f = jQuery.find(b);
116                 var s = "";
117                 for ( var i = 0; i < f.length; i++ )
118                         s += (s && ",") + '"' + f[i].id + '"';
119                 isSet(f, q.apply(q,c), a + " (" + b + ")");
120         }
121 }