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