Testsuite 2.0
[jquery.git] / build / test / data / testrunner.js
1 var _config = {
2         fixture: null,
3         Test: [],
4         stats: {
5                 all: 0,
6                 bad: 0
7         },
8         queue: [],
9         blocking: true,
10         timeout: null,
11         expected: null,
12         currentModule: null,
13         asyncTimeout: 2 // seconds for async timeout
14 };
15
16 $(function() {
17         $('#userAgent').html(navigator.userAgent);
18         runTest();      
19 });
20
21 function synchronize(callback) {
22         _config.queue[_config.queue.length] = callback;
23         if(!_config.blocking) {
24                 process();
25         }
26 }
27
28 function process() {
29         while(_config.queue.length && !_config.blocking) {
30                 var call = _config.queue[0];
31                 _config.queue = _config.queue.slice(1);
32                 call();
33         }
34 }
35
36 function stop() {
37         _config.blocking = true;
38         _config.timeout = setTimeout(start, _config.asyncTimeout * 1000);
39 }
40 function start() {
41         if(_config.timeout)
42                 clearTimeout(_config.timeout);
43         _config.blocking = false;
44         process();
45 }
46
47 function runTest() {
48         _config.blocking = false;
49         var time = new Date();
50         _config.fixture = document.getElementById('main').innerHTML;
51         synchronize(function() {
52                 time = new Date() - time;
53                 $("<div>").html(['<p class="result">Tests completed in ',
54                         time, ' milliseconds.<br/>',
55                         _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
56                         .join(''))
57                         .appendTo("body");
58                 $("<div id='banner'>").addClass(_config.stats.bad ? "fail" : "pass").insertAfter("h1");
59         });
60 }
61
62 function test(name, callback) {
63         if(_config.currentModule)
64                 name = _config.currentModule + " module: " + name;
65         synchronize(function() {
66                 _config.Test = [];
67                 try {
68                         callback();
69                 } catch(e) {
70                         if( typeof console != "undefined" && console.error && console.warn ) {
71                                 console.error("Test " + name + " died, exception and test follows");
72                                 console.error(e);
73                                 console.warn(callback.toString());
74                         }
75                         _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
76                 }
77         });
78         synchronize(function() {
79                 reset();
80                 
81                 if(_config.expected && _config.expected != _config.Test.length) {
82                         _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
83                 }
84                 _config.expected = null;
85                 
86                 var good = 0, bad = 0;
87                 var ol = document.createElement("ol");
88                 ol.style.display = "none";
89                 var li = "", state = "pass";
90                 for ( var i = 0; i < _config.Test.length; i++ ) {
91                         var li = document.createElement("li");
92                         li.className = _config.Test[i][0] ? "pass" : "fail";
93                         li.innerHTML = _config.Test[i][1];
94                         ol.appendChild( li );
95                         
96                         _config.stats.all++;
97                         if ( !_config.Test[i][0] ) {
98                                 state = "fail";
99                                 bad++;
100                                 _config.stats.bad++;
101                         } else good++;
102                 }
103         
104                 var li = document.createElement("li");
105                 li.className = state;
106         
107                 var b = document.createElement("b");
108                 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
109                 b.onclick = function(){
110                         var n = this.nextSibling;
111                         if ( jQuery.css( n, "display" ) == "none" )
112                                 n.style.display = "block";
113                         else
114                                 n.style.display = "none";
115                 };
116                 li.appendChild( b );
117                 li.appendChild( ol );
118         
119                 document.getElementById("tests").appendChild( li );             
120         });
121 }
122
123 // call on start of module test to prepend name to all tests
124 function module(moduleName) {
125         _config.currentModule = moduleName;
126 }
127
128 /**
129  * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
130  */
131 function expect(asserts) {
132         _config.expected = asserts;
133 }
134
135 /**
136  * Resets the test setup. Useful for tests that modify the DOM.
137  */
138 function reset() {
139         document.getElementById('main').innerHTML = _config.fixture;
140 }
141
142 /**
143  * Asserts true.
144  * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
145  */
146 function ok(a, msg) {
147         _config.Test.push( [ !!a, msg ] );
148 }
149
150 /**
151  * Asserts that two arrays are the same
152  */
153 function isSet(a, b, msg) {
154         var ret = true;
155         if ( a && b && a.length == b.length ) {
156                 for ( var i in a )
157                         if ( a[i] != b[i] )
158                                 ret = false;
159         } else
160                 ret = false;
161         if ( !ret )
162                 _config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
163         else 
164                 _config.Test.push( [ ret, msg ] );
165 }
166
167 /**
168  * Returns an array of elements with the given IDs, eg.
169  * @example q("main", "foo", "bar")
170  * @result [<div id="main">, <span id="foo">, <input id="bar">]
171  */
172 function q() {
173         var r = [];
174         for ( var i = 0; i < arguments.length; i++ )
175                 r.push( document.getElementById( arguments[i] ) );
176         return r;
177 }
178
179 /**
180  * Asserts that a select matches the given IDs
181  * @example t("Check for something", "//[a]", ["foo", "baar"]);
182  * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
183  */
184 function t(a,b,c) {
185         var f = jQuery.find(b);
186         var s = "";
187         for ( var i = 0; i < f.length; i++ )
188                 s += (s && ",") + '"' + f[i].id + '"';
189         isSet(f, q.apply(q,c), a + " (" + b + ")");
190 }