0ff0ad50065266b9b5f2c6bfb019a69fb6562c5c
[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(allowFailure) {
37         _config.blocking = true;
38         var handler = allowFailure ? start : function() {
39                 ok( false, "Test timed out" );
40                 start();
41         };
42         _config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
43 }
44 function start() {
45         if(_config.timeout)
46                 clearTimeout(_config.timeout);
47         _config.blocking = false;
48         process();
49 }
50
51 function runTest() {
52         _config.blocking = false;
53         var time = new Date();
54         _config.fixture = document.getElementById('main').innerHTML;
55         synchronize(function() {
56                 time = new Date() - time;
57                 $("<div>").html(['<p class="result">Tests completed in ',
58                         time, ' milliseconds.<br/>',
59                         _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
60                         .join(''))
61                         .appendTo("body");
62                 $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
63         });
64 }
65
66 function test(name, callback) {
67         if(_config.currentModule)
68                 name = _config.currentModule + " module: " + name;
69         synchronize(function() {
70                 _config.Test = [];
71                 try {
72                         callback();
73                 } catch(e) {
74                         if( typeof console != "undefined" && console.error && console.warn ) {
75                                 console.error("Test " + name + " died, exception and test follows");
76                                 console.error(e);
77                                 console.warn(callback.toString());
78                         }
79                         _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
80                 }
81         });
82         synchronize(function() {
83                 reset();
84                 
85                 if(_config.expected && _config.expected != _config.Test.length) {
86                         _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
87                 }
88                 _config.expected = null;
89                 
90                 var good = 0, bad = 0;
91                 var ol = document.createElement("ol");
92                 ol.style.display = "none";
93                 var li = "", state = "pass";
94                 for ( var i = 0; i < _config.Test.length; i++ ) {
95                         var li = document.createElement("li");
96                         li.className = _config.Test[i][0] ? "pass" : "fail";
97                         li.innerHTML = _config.Test[i][1];
98                         ol.appendChild( li );
99                         
100                         _config.stats.all++;
101                         if ( !_config.Test[i][0] ) {
102                                 state = "fail";
103                                 bad++;
104                                 _config.stats.bad++;
105                         } else good++;
106                 }
107         
108                 var li = document.createElement("li");
109                 li.className = state;
110         
111                 var b = document.createElement("b");
112                 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
113                 b.onclick = function(){
114                         var n = this.nextSibling;
115                         if ( jQuery.css( n, "display" ) == "none" )
116                                 n.style.display = "block";
117                         else
118                                 n.style.display = "none";
119                 };
120                 li.appendChild( b );
121                 li.appendChild( ol );
122         
123                 document.getElementById("tests").appendChild( li );             
124         });
125 }
126
127 // call on start of module test to prepend name to all tests
128 function module(moduleName) {
129         _config.currentModule = moduleName;
130 }
131
132 /**
133  * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
134  */
135 function expect(asserts) {
136         _config.expected = asserts;
137 }
138
139 /**
140  * Resets the test setup. Useful for tests that modify the DOM.
141  */
142 function reset() {
143         document.getElementById('main').innerHTML = _config.fixture;
144 }
145
146 /**
147  * Asserts true.
148  * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
149  */
150 function ok(a, msg) {
151         _config.Test.push( [ !!a, msg ] );
152 }
153
154 /**
155  * Asserts that two arrays are the same
156  */
157 function isSet(a, b, msg) {
158         var ret = true;
159         if ( a && b && a.length == b.length ) {
160                 for ( var i in a )
161                         if ( a[i] != b[i] )
162                                 ret = false;
163         } else
164                 ret = false;
165         if ( !ret )
166                 _config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
167         else 
168                 _config.Test.push( [ ret, msg ] );
169 }
170
171 /**
172  * Returns an array of elements with the given IDs, eg.
173  * @example q("main", "foo", "bar")
174  * @result [<div id="main">, <span id="foo">, <input id="bar">]
175  */
176 function q() {
177         var r = [];
178         for ( var i = 0; i < arguments.length; i++ )
179                 r.push( document.getElementById( arguments[i] ) );
180         return r;
181 }
182
183 /**
184  * Asserts that a select matches the given IDs
185  * @example t("Check for something", "//[a]", ["foo", "baar"]);
186  * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
187  */
188 function t(a,b,c) {
189         var f = jQuery.find(b);
190         var s = "";
191         for ( var i = 0; i < f.length; i++ )
192                 s += (s && ",") + '"' + f[i].id + '"';
193         isSet(f, q.apply(q,c), a + " (" + b + ")");
194 }
195
196 /**
197  * Add random number to url to stop IE from caching
198  *
199  * @example url("data/test.html")
200  * @result "data/test.html?10538358428943"
201  *
202  * @example url("data/test.php?foo=bar")
203  * @result "data/test.php?foo=bar&10538358345554"
204  */
205 function url(value) {
206         return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
207 }