1 | /*
|
---|
2 | * QUnit - jQuery unit testrunner
|
---|
3 | *
|
---|
4 | * http://docs.jquery.com/QUnit
|
---|
5 | *
|
---|
6 | * Copyright (c) 2009 John Resig, Jörn Zaefferer
|
---|
7 | * Dual licensed under the MIT (MIT-LICENSE.txt)
|
---|
8 | * and GPL (GPL-LICENSE.txt) licenses.
|
---|
9 | *
|
---|
10 | * $Id: testrunner.js 6588 2009-09-29 00:07:51Z jeresig $
|
---|
11 | */
|
---|
12 |
|
---|
13 | (function() {
|
---|
14 |
|
---|
15 | // Test for equality any JavaScript type.
|
---|
16 | // Discussions and reference: http://philrathe.com/articles/equiv
|
---|
17 | // Test suites: http://philrathe.com/tests/equiv
|
---|
18 | // Author: Philippe Rathé <[email protected]>
|
---|
19 | var equiv = function () {
|
---|
20 |
|
---|
21 | var innerEquiv; // the real equiv function
|
---|
22 | var callers = []; // stack to decide between skip/abort functions
|
---|
23 |
|
---|
24 |
|
---|
25 | // Determine what is o.
|
---|
26 | function hoozit(o) {
|
---|
27 | if (o.constructor === String) {
|
---|
28 | return "string";
|
---|
29 |
|
---|
30 | } else if (o.constructor === Boolean) {
|
---|
31 | return "boolean";
|
---|
32 |
|
---|
33 | } else if (o.constructor === Number) {
|
---|
34 |
|
---|
35 | if (isNaN(o)) {
|
---|
36 | return "nan";
|
---|
37 | } else {
|
---|
38 | return "number";
|
---|
39 | }
|
---|
40 |
|
---|
41 | } else if (typeof o === "undefined") {
|
---|
42 | return "undefined";
|
---|
43 |
|
---|
44 | // consider: typeof null === object
|
---|
45 | } else if (o === null) {
|
---|
46 | return "null";
|
---|
47 |
|
---|
48 | // consider: typeof [] === object
|
---|
49 | } else if (o instanceof Array) {
|
---|
50 | return "array";
|
---|
51 |
|
---|
52 | // consider: typeof new Date() === object
|
---|
53 | } else if (o instanceof Date) {
|
---|
54 | return "date";
|
---|
55 |
|
---|
56 | // consider: /./ instanceof Object;
|
---|
57 | // /./ instanceof RegExp;
|
---|
58 | // typeof /./ === "function"; // => false in IE and Opera,
|
---|
59 | // true in FF and Safari
|
---|
60 | } else if (o instanceof RegExp) {
|
---|
61 | return "regexp";
|
---|
62 |
|
---|
63 | } else if (typeof o === "object") {
|
---|
64 | return "object";
|
---|
65 |
|
---|
66 | } else if (o instanceof Function) {
|
---|
67 | return "function";
|
---|
68 | } else {
|
---|
69 | return undefined;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | // Call the o related callback with the given arguments.
|
---|
74 | function bindCallbacks(o, callbacks, args) {
|
---|
75 | var prop = hoozit(o);
|
---|
76 | if (prop) {
|
---|
77 | if (hoozit(callbacks[prop]) === "function") {
|
---|
78 | return callbacks[prop].apply(callbacks, args);
|
---|
79 | } else {
|
---|
80 | return callbacks[prop]; // or undefined
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | var callbacks = function () {
|
---|
86 |
|
---|
87 | // for string, boolean, number and null
|
---|
88 | function useStrictEquality(b, a) {
|
---|
89 | if (b instanceof a.constructor || a instanceof b.constructor) {
|
---|
90 | // to catch short annotaion VS 'new' annotation of a declaration
|
---|
91 | // e.g. var i = 1;
|
---|
92 | // var j = new Number(1);
|
---|
93 | return a == b;
|
---|
94 | } else {
|
---|
95 | return a === b;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | return {
|
---|
100 | "string": useStrictEquality,
|
---|
101 | "boolean": useStrictEquality,
|
---|
102 | "number": useStrictEquality,
|
---|
103 | "null": useStrictEquality,
|
---|
104 | "undefined": useStrictEquality,
|
---|
105 |
|
---|
106 | "nan": function (b) {
|
---|
107 | return isNaN(b);
|
---|
108 | },
|
---|
109 |
|
---|
110 | "date": function (b, a) {
|
---|
111 | return hoozit(b) === "date" && a.valueOf() === b.valueOf();
|
---|
112 | },
|
---|
113 |
|
---|
114 | "regexp": function (b, a) {
|
---|
115 | return hoozit(b) === "regexp" &&
|
---|
116 | a.source === b.source && // the regex itself
|
---|
117 | a.global === b.global && // and its modifers (gmi) ...
|
---|
118 | a.ignoreCase === b.ignoreCase &&
|
---|
119 | a.multiline === b.multiline;
|
---|
120 | },
|
---|
121 |
|
---|
122 | // - skip when the property is a method of an instance (OOP)
|
---|
123 | // - abort otherwise,
|
---|
124 | // initial === would have catch identical references anyway
|
---|
125 | "function": function () {
|
---|
126 | var caller = callers[callers.length - 1];
|
---|
127 | return caller !== Object &&
|
---|
128 | typeof caller !== "undefined";
|
---|
129 | },
|
---|
130 |
|
---|
131 | "array": function (b, a) {
|
---|
132 | var i;
|
---|
133 | var len;
|
---|
134 |
|
---|
135 | // b could be an object literal here
|
---|
136 | if ( ! (hoozit(b) === "array")) {
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 |
|
---|
140 | len = a.length;
|
---|
141 | if (len !== b.length) { // safe and faster
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 | for (i = 0; i < len; i++) {
|
---|
145 | if( ! innerEquiv(a[i], b[i])) {
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | return true;
|
---|
150 | },
|
---|
151 |
|
---|
152 | "object": function (b, a) {
|
---|
153 | var i;
|
---|
154 | var eq = true; // unless we can proove it
|
---|
155 | var aProperties = [], bProperties = []; // collection of strings
|
---|
156 |
|
---|
157 | // comparing constructors is more strict than using instanceof
|
---|
158 | if ( a.constructor !== b.constructor) {
|
---|
159 | return false;
|
---|
160 | }
|
---|
161 |
|
---|
162 | // stack constructor before traversing properties
|
---|
163 | callers.push(a.constructor);
|
---|
164 |
|
---|
165 | for (i in a) { // be strict: don't ensures hasOwnProperty and go deep
|
---|
166 |
|
---|
167 | aProperties.push(i); // collect a's properties
|
---|
168 |
|
---|
169 | if ( ! innerEquiv(a[i], b[i])) {
|
---|
170 | eq = false;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | callers.pop(); // unstack, we are done
|
---|
175 |
|
---|
176 | for (i in b) {
|
---|
177 | bProperties.push(i); // collect b's properties
|
---|
178 | }
|
---|
179 |
|
---|
180 | // Ensures identical properties name
|
---|
181 | return eq && innerEquiv(aProperties.sort(), bProperties.sort());
|
---|
182 | }
|
---|
183 | };
|
---|
184 | }();
|
---|
185 |
|
---|
186 | innerEquiv = function () { // can take multiple arguments
|
---|
187 | var args = Array.prototype.slice.apply(arguments);
|
---|
188 | if (args.length < 2) {
|
---|
189 | return true; // end transition
|
---|
190 | }
|
---|
191 |
|
---|
192 | return (function (a, b) {
|
---|
193 | if (a === b) {
|
---|
194 | return true; // catch the most you can
|
---|
195 | } else if (a === null || b === null || typeof a === "undefined" || typeof b === "undefined" || hoozit(a) !== hoozit(b)) {
|
---|
196 | return false; // don't lose time with error prone cases
|
---|
197 | } else {
|
---|
198 | return bindCallbacks(a, callbacks, [b, a]);
|
---|
199 | }
|
---|
200 |
|
---|
201 | // apply transition with (1..n) arguments
|
---|
202 | })(args[0], args[1]) && arguments.callee.apply(this, args.splice(1, args.length -1));
|
---|
203 | };
|
---|
204 |
|
---|
205 | return innerEquiv;
|
---|
206 |
|
---|
207 | }();
|
---|
208 |
|
---|
209 | var GETParams = location.search.slice(1).split('&'), ngindex = -1;
|
---|
210 |
|
---|
211 | for ( var i = 0; i < GETParams.length; i++ ) {
|
---|
212 | GETParams[i] = decodeURIComponent( GETParams[i] );
|
---|
213 | if ( GETParams[i] === "noglobals" ) {
|
---|
214 | ngindex = i;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | var noglobals = ngindex !== -1;
|
---|
219 |
|
---|
220 | if ( noglobals ) {
|
---|
221 | GETParams.splice( ngindex, 1 );
|
---|
222 | }
|
---|
223 |
|
---|
224 | var config = {
|
---|
225 | stats: {
|
---|
226 | all: 0,
|
---|
227 | bad: 0
|
---|
228 | },
|
---|
229 | queue: [],
|
---|
230 | // block until document ready
|
---|
231 | blocking: true,
|
---|
232 | //restrict modules/tests by get parameters
|
---|
233 | filters: GETParams,
|
---|
234 | isLocal: !!(window.location.protocol == 'file:')
|
---|
235 | };
|
---|
236 |
|
---|
237 | // public API as global methods
|
---|
238 | extend(window, {
|
---|
239 | test: test,
|
---|
240 | module: module,
|
---|
241 | expect: expect,
|
---|
242 | ok: ok,
|
---|
243 | equals: equals,
|
---|
244 | start: start,
|
---|
245 | stop: stop,
|
---|
246 | reset: reset,
|
---|
247 | isLocal: config.isLocal,
|
---|
248 | same: function(a, b, message) {
|
---|
249 | push(equiv(a, b), a, b, message);
|
---|
250 | },
|
---|
251 | QUnit: {
|
---|
252 | equiv: equiv,
|
---|
253 | ok: ok,
|
---|
254 | done: function(failures, total){},
|
---|
255 | log: function(result, message){}
|
---|
256 | },
|
---|
257 | // legacy methods below
|
---|
258 | isSet: isSet,
|
---|
259 | isObj: isObj,
|
---|
260 | compare: function() {
|
---|
261 | throw "compare is deprecated - use same() instead";
|
---|
262 | },
|
---|
263 | compare2: function() {
|
---|
264 | throw "compare2 is deprecated - use same() instead";
|
---|
265 | },
|
---|
266 | serialArray: function() {
|
---|
267 | throw "serialArray is deprecated - use jsDump.parse() instead";
|
---|
268 | },
|
---|
269 | q: q,
|
---|
270 | t: t,
|
---|
271 | url: url,
|
---|
272 | triggerEvent: triggerEvent
|
---|
273 | });
|
---|
274 |
|
---|
275 | addEvent(window, "load", function(){
|
---|
276 |
|
---|
277 | if ( !document.getElementById("header") ) {
|
---|
278 | var header = document.createElement("h1");
|
---|
279 | header.id = "header";
|
---|
280 | header.innerHTML = document.title;
|
---|
281 |
|
---|
282 | var banner = document.createElement("h2");
|
---|
283 | banner.id = "banner";
|
---|
284 |
|
---|
285 | var userAgent = document.createElement("h2");
|
---|
286 | userAgent.id = "userAgent";
|
---|
287 |
|
---|
288 | var ol = document.createElement("ol");
|
---|
289 | ol.id = "tests";
|
---|
290 |
|
---|
291 | document.body.insertBefore( ol, document.body.firstChild );
|
---|
292 | document.body.insertBefore( userAgent, document.body.firstChild );
|
---|
293 | document.body.insertBefore( banner, document.body.firstChild );
|
---|
294 | document.body.insertBefore( header, document.body.firstChild );
|
---|
295 | }
|
---|
296 |
|
---|
297 | var userAgent = document.getElementById("userAgent");
|
---|
298 | if ( userAgent ) {
|
---|
299 | userAgent.innerHTML = navigator.userAgent;
|
---|
300 |
|
---|
301 | var toolbar = document.createElement("div");
|
---|
302 | toolbar.className = "testrunner-toolbar";
|
---|
303 | userAgent.parentNode.insertBefore( toolbar, userAgent );
|
---|
304 |
|
---|
305 | var filter = document.createElement("input");
|
---|
306 | filter.type = "checkbox";
|
---|
307 | filter.id = "filter-pass";
|
---|
308 | filter.disabled = true;
|
---|
309 | addEvent( filter, "click", function(){
|
---|
310 | var li = document.getElementsByTagName("li");
|
---|
311 | for ( var i = 0; i < li.length; i++ ) {
|
---|
312 | if ( li[i].className.indexOf("pass") > -1 ) {
|
---|
313 | li[i].style.display = filter.checked ? "none" : "block";
|
---|
314 | }
|
---|
315 | }
|
---|
316 | });
|
---|
317 | toolbar.appendChild( filter );
|
---|
318 |
|
---|
319 | var label = document.createElement("label");
|
---|
320 | label.setAttribute("for", "filter-pass");
|
---|
321 | label.innerHTML = "Hide passed tests";
|
---|
322 | toolbar.appendChild( label );
|
---|
323 |
|
---|
324 | var missing = document.createElement("input");
|
---|
325 | missing.type = "checkbox";
|
---|
326 | missing.id = "filter-missing";
|
---|
327 | missing.disabled = true;
|
---|
328 | addEvent( missing, "click", function(){
|
---|
329 | var li = document.getElementsByTagName("li");
|
---|
330 | for ( var i = 0; i < li.length; i++ ) {
|
---|
331 | if ( li[i].className.indexOf("fail") > -1 && li[i].innerHTML.indexOf('missing test - untested code is broken code') > - 1 ) {
|
---|
332 | li[i].parentNode.parentNode.style.display = missing.checked ? "none" : "block";
|
---|
333 | }
|
---|
334 | }
|
---|
335 | });
|
---|
336 | toolbar.appendChild( missing );
|
---|
337 |
|
---|
338 | label = document.createElement("label");
|
---|
339 | label.setAttribute("for", "filter-missing");
|
---|
340 | label.innerHTML = "Hide missing tests (untested code is broken code)";
|
---|
341 | toolbar.appendChild( label );
|
---|
342 | }
|
---|
343 |
|
---|
344 | runTest();
|
---|
345 | });
|
---|
346 |
|
---|
347 | function synchronize(callback) {
|
---|
348 | config.queue.push(callback);
|
---|
349 | if(!config.blocking) {
|
---|
350 | process();
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | function process() {
|
---|
355 | while(config.queue.length && !config.blocking) {
|
---|
356 | config.queue.shift()();
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | function stop(timeout) {
|
---|
361 | config.blocking = true;
|
---|
362 | if (timeout)
|
---|
363 | config.timeout = setTimeout(function() {
|
---|
364 | QUnit.ok( false, "Test timed out" );
|
---|
365 | start();
|
---|
366 | }, timeout);
|
---|
367 | }
|
---|
368 | function start() {
|
---|
369 | // A slight delay, to avoid any current callbacks
|
---|
370 | setTimeout(function() {
|
---|
371 | if(config.timeout)
|
---|
372 | clearTimeout(config.timeout);
|
---|
373 | config.blocking = false;
|
---|
374 | process();
|
---|
375 | }, 13);
|
---|
376 | }
|
---|
377 |
|
---|
378 | function validTest( name ) {
|
---|
379 | var i = config.filters.length,
|
---|
380 | run = false;
|
---|
381 |
|
---|
382 | if( !i )
|
---|
383 | return true;
|
---|
384 |
|
---|
385 | while( i-- ){
|
---|
386 | var filter = config.filters[i],
|
---|
387 | not = filter.charAt(0) == '!';
|
---|
388 | if( not )
|
---|
389 | filter = filter.slice(1);
|
---|
390 | if( name.indexOf(filter) != -1 )
|
---|
391 | return !not;
|
---|
392 | if( not )
|
---|
393 | run = true;
|
---|
394 | }
|
---|
395 | return run;
|
---|
396 | }
|
---|
397 |
|
---|
398 | function runTest() {
|
---|
399 | config.blocking = false;
|
---|
400 | var started = +new Date;
|
---|
401 | config.fixture = document.getElementById('main').innerHTML;
|
---|
402 |
|
---|
403 | if ( window.jQuery ) {
|
---|
404 | config.ajaxSettings = window.jQuery.ajaxSettings;
|
---|
405 | }
|
---|
406 |
|
---|
407 | synchronize(function() {
|
---|
408 | var html = ['Tests completed in ',
|
---|
409 | +new Date - started, ' milliseconds.<br/>',
|
---|
410 | '<span class="bad">', config.stats.all - config.stats.bad, '</span> tests of <span class="all">', config.stats.all, '</span> passed, ', config.stats.bad,' failed.'].join('');
|
---|
411 |
|
---|
412 | var result = document.createElement("p");
|
---|
413 | result.id = "testresult";
|
---|
414 | result.className = "result";
|
---|
415 | result.innerHTML = html;
|
---|
416 | document.body.appendChild( result );
|
---|
417 |
|
---|
418 | document.getElementById("banner").className +=
|
---|
419 | " " + (config.stats.bad ? "fail" : "pass");
|
---|
420 |
|
---|
421 | QUnit.done( config.stats.bad, config.stats.all );
|
---|
422 | });
|
---|
423 | }
|
---|
424 |
|
---|
425 | var pollution;
|
---|
426 |
|
---|
427 | function saveGlobal(){
|
---|
428 | pollution = [ ];
|
---|
429 |
|
---|
430 | if ( noglobals ) {
|
---|
431 | for ( var key in window ) {
|
---|
432 | pollution.push(key);
|
---|
433 | }
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | function checkPollution( name ){
|
---|
438 | var old = pollution;
|
---|
439 | saveGlobal();
|
---|
440 |
|
---|
441 | if ( pollution.length > old.length ){
|
---|
442 | ok( false, "Introduced global variable(s): " + diff(old, pollution).join(", ") );
|
---|
443 | config.expected++;
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | function diff( clean, dirty ){
|
---|
448 | var results = [];
|
---|
449 |
|
---|
450 | for ( var i = 0; i < dirty.length; i++ ) {
|
---|
451 | for ( var c = 0; c < clean.length; c++ ) {
|
---|
452 | if ( clean[c] === dirty[i] ) {
|
---|
453 | results.push( clean[c] );
|
---|
454 | }
|
---|
455 | }
|
---|
456 | }
|
---|
457 |
|
---|
458 | return results;
|
---|
459 | }
|
---|
460 |
|
---|
461 | function test(name, callback) {
|
---|
462 | if ( config.currentModule ) {
|
---|
463 | name = config.currentModule + " module: " + name;
|
---|
464 | }
|
---|
465 |
|
---|
466 | var lifecycle = extend({
|
---|
467 | setup: function() {},
|
---|
468 | teardown: function() {}
|
---|
469 | }, config.moduleLifecycle);
|
---|
470 |
|
---|
471 | if ( !validTest(name) ) {
|
---|
472 | return;
|
---|
473 | }
|
---|
474 |
|
---|
475 | var testEnvironment = {};
|
---|
476 |
|
---|
477 | synchronize(function() {
|
---|
478 | config.assertions = [];
|
---|
479 | config.expected = null;
|
---|
480 | try {
|
---|
481 | if ( !pollution ) {
|
---|
482 | saveGlobal();
|
---|
483 | }
|
---|
484 |
|
---|
485 | lifecycle.setup.call(testEnvironment);
|
---|
486 | } catch(e) {
|
---|
487 | QUnit.ok( false, "Setup failed on " + name + ": " + e.message );
|
---|
488 | }
|
---|
489 | });
|
---|
490 | synchronize(function() {
|
---|
491 | try {
|
---|
492 | callback.call(testEnvironment);
|
---|
493 | } catch(e) {
|
---|
494 | fail("Test " + name + " died, exception and test follows", e, callback);
|
---|
495 | QUnit.ok( false, "Died on test #" + (config.assertions.length + 1) + ": " + e.message );
|
---|
496 | // else next test will carry the responsibility
|
---|
497 | saveGlobal();
|
---|
498 | }
|
---|
499 | });
|
---|
500 | synchronize(function() {
|
---|
501 | try {
|
---|
502 | checkPollution();
|
---|
503 | lifecycle.teardown.call(testEnvironment);
|
---|
504 | } catch(e) {
|
---|
505 | QUnit.ok( false, "Teardown failed on " + name + ": " + e.message );
|
---|
506 | }
|
---|
507 | });
|
---|
508 | synchronize(function() {
|
---|
509 | try {
|
---|
510 | reset();
|
---|
511 | } catch(e) {
|
---|
512 | fail("reset() failed, following Test " + name + ", exception and reset fn follows", e, reset);
|
---|
513 | }
|
---|
514 |
|
---|
515 | if ( config.expected && config.expected != config.assertions.length ) {
|
---|
516 | QUnit.ok( false, "Expected " + config.expected + " assertions, but " + config.assertions.length + " were run" );
|
---|
517 | }
|
---|
518 |
|
---|
519 | var good = 0, bad = 0;
|
---|
520 | var ol = document.createElement("ol");
|
---|
521 | ol.style.display = "none";
|
---|
522 | config.stats.all += config.assertions.length;
|
---|
523 | for ( var i = 0; i < config.assertions.length; i++ ) {
|
---|
524 | var assertion = config.assertions[i];
|
---|
525 | var li = document.createElement("li");
|
---|
526 | li.className = assertion.result ? "pass" : "fail";
|
---|
527 | li.innerHTML = assertion.message || "(no message)";
|
---|
528 | ol.appendChild( li );
|
---|
529 | assertion.result ? good++ : bad++;
|
---|
530 | }
|
---|
531 | config.stats.bad += bad;
|
---|
532 |
|
---|
533 | var b = document.createElement("strong");
|
---|
534 | b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + config.assertions.length + ")</b>";
|
---|
535 | addEvent(b, "click", function(){
|
---|
536 | var next = b.nextSibling, display = next.style.display;
|
---|
537 | next.style.display = display === "none" ? "block" : "none";
|
---|
538 | });
|
---|
539 | addEvent(b, "dblclick", function(e){
|
---|
540 | var target = (e || window.event).target;
|
---|
541 | if ( target.nodeName.toLowerCase() === "strong" ) {
|
---|
542 | var text = "", node = target.firstChild;
|
---|
543 |
|
---|
544 | while ( node.nodeType === 3 ) {
|
---|
545 | text += node.nodeValue;
|
---|
546 | node = node.nextSibling;
|
---|
547 | }
|
---|
548 |
|
---|
549 | text = text.replace(/(^\s*|\s*$)/g, "");
|
---|
550 |
|
---|
551 | location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent(text);
|
---|
552 | }
|
---|
553 | });
|
---|
554 |
|
---|
555 | var li = document.createElement("li");
|
---|
556 | li.className = bad ? "fail" : "pass";
|
---|
557 | li.appendChild( b );
|
---|
558 | li.appendChild( ol );
|
---|
559 | document.getElementById("tests").appendChild( li );
|
---|
560 |
|
---|
561 | if(bad) {
|
---|
562 | document.getElementById("filter-pass").disabled = null;
|
---|
563 | document.getElementById("filter-missing").disabled = null;
|
---|
564 | }
|
---|
565 | });
|
---|
566 | }
|
---|
567 |
|
---|
568 | function fail(message, exception, callback) {
|
---|
569 | if( typeof console != "undefined" && console.error && console.warn ) {
|
---|
570 | console.error(message);
|
---|
571 | console.error(exception);
|
---|
572 | console.warn(callback.toString());
|
---|
573 | } else if (window.opera && opera.postError) {
|
---|
574 | opera.postError(message, exception, callback.toString);
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | // call on start of module test to prepend name to all tests
|
---|
579 | function module(name, lifecycle) {
|
---|
580 | config.currentModule = name;
|
---|
581 | config.moduleLifecycle = lifecycle;
|
---|
582 | }
|
---|
583 |
|
---|
584 | /**
|
---|
585 | * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
|
---|
586 | */
|
---|
587 | function expect(asserts) {
|
---|
588 | config.expected = asserts;
|
---|
589 | }
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Resets the test setup. Useful for tests that modify the DOM.
|
---|
593 | */
|
---|
594 | function reset() {
|
---|
595 | if ( window.jQuery ) {
|
---|
596 | jQuery("#main").html( config.fixture );
|
---|
597 | jQuery.event.global = {};
|
---|
598 | jQuery.ajaxSettings = extend({}, config.ajaxSettings);
|
---|
599 | } else {
|
---|
600 | document.getElementById("main").innerHTML = config.fixture;
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | /**
|
---|
605 | * Asserts true.
|
---|
606 | * @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" );
|
---|
607 | */
|
---|
608 | function ok(a, msg) {
|
---|
609 | QUnit.log(a, msg);
|
---|
610 |
|
---|
611 | config.assertions.push({
|
---|
612 | result: !!a,
|
---|
613 | message: msg
|
---|
614 | });
|
---|
615 | }
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * Asserts that two arrays are the same
|
---|
619 | */
|
---|
620 | function isSet(a, b, msg) {
|
---|
621 | function serialArray( a ) {
|
---|
622 | var r = [];
|
---|
623 |
|
---|
624 | if ( a && a.length )
|
---|
625 | for ( var i = 0; i < a.length; i++ ) {
|
---|
626 | var str = a[i].nodeName;
|
---|
627 | if ( str ) {
|
---|
628 | str = str.toLowerCase();
|
---|
629 | if ( a[i].id )
|
---|
630 | str += "#" + a[i].id;
|
---|
631 | } else
|
---|
632 | str = a[i];
|
---|
633 | r.push( str );
|
---|
634 | }
|
---|
635 |
|
---|
636 | return "[ " + r.join(", ") + " ]";
|
---|
637 | }
|
---|
638 | var ret = true;
|
---|
639 | if ( a && b && a.length != undefined && a.length == b.length ) {
|
---|
640 | for ( var i = 0; i < a.length; i++ )
|
---|
641 | if ( a[i] != b[i] )
|
---|
642 | ret = false;
|
---|
643 | } else
|
---|
644 | ret = false;
|
---|
645 | QUnit.ok( ret, !ret ? (msg + " expected: " + serialArray(b) + " result: " + serialArray(a)) : msg );
|
---|
646 | }
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Asserts that two objects are equivalent
|
---|
650 | */
|
---|
651 | function isObj(a, b, msg) {
|
---|
652 | var ret = true;
|
---|
653 |
|
---|
654 | if ( a && b ) {
|
---|
655 | for ( var i in a )
|
---|
656 | if ( a[i] != b[i] )
|
---|
657 | ret = false;
|
---|
658 |
|
---|
659 | for ( i in b )
|
---|
660 | if ( a[i] != b[i] )
|
---|
661 | ret = false;
|
---|
662 | } else
|
---|
663 | ret = false;
|
---|
664 |
|
---|
665 | QUnit.ok( ret, msg );
|
---|
666 | }
|
---|
667 |
|
---|
668 | /**
|
---|
669 | * Returns an array of elements with the given IDs, eg.
|
---|
670 | * @example q("main", "foo", "bar")
|
---|
671 | * @result [<div id="main">, <span id="foo">, <input id="bar">]
|
---|
672 | */
|
---|
673 | function q() {
|
---|
674 | var r = [];
|
---|
675 | for ( var i = 0; i < arguments.length; i++ )
|
---|
676 | r.push( document.getElementById( arguments[i] ) );
|
---|
677 | return r;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Asserts that a select matches the given IDs
|
---|
682 | * @example t("Check for something", "//[a]", ["foo", "baar"]);
|
---|
683 | * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
|
---|
684 | */
|
---|
685 | function t(a,b,c) {
|
---|
686 | var f = jQuery(b);
|
---|
687 | var s = "";
|
---|
688 | for ( var i = 0; i < f.length; i++ )
|
---|
689 | s += (s && ",") + '"' + f[i].id + '"';
|
---|
690 | isSet(f, q.apply(q,c), a + " (" + b + ")");
|
---|
691 | }
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * Add random number to url to stop IE from caching
|
---|
695 | *
|
---|
696 | * @example url("data/test.html")
|
---|
697 | * @result "data/test.html?10538358428943"
|
---|
698 | *
|
---|
699 | * @example url("data/test.php?foo=bar")
|
---|
700 | * @result "data/test.php?foo=bar&10538358345554"
|
---|
701 | */
|
---|
702 | function url(value) {
|
---|
703 | return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
|
---|
704 | }
|
---|
705 |
|
---|
706 | /**
|
---|
707 | * Checks that the first two arguments are equal, with an optional message.
|
---|
708 | * Prints out both actual and expected values.
|
---|
709 | *
|
---|
710 | * Prefered to ok( actual == expected, message )
|
---|
711 | *
|
---|
712 | * @example equals( format("Received {0} bytes.", 2), "Received 2 bytes." );
|
---|
713 | *
|
---|
714 | * @param Object actual
|
---|
715 | * @param Object expected
|
---|
716 | * @param String message (optional)
|
---|
717 | */
|
---|
718 | function equals(actual, expected, message) {
|
---|
719 | push(expected == actual, actual, expected, message);
|
---|
720 | }
|
---|
721 |
|
---|
722 | function push(result, actual, expected, message) {
|
---|
723 | message = message || (result ? "okay" : "failed");
|
---|
724 | QUnit.ok( result, result ? message + ": " + expected : message + ", expected: " + jsDump.parse(expected) + " result: " + jsDump.parse(actual) );
|
---|
725 | }
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Trigger an event on an element.
|
---|
729 | *
|
---|
730 | * @example triggerEvent( document.body, "click" );
|
---|
731 | *
|
---|
732 | * @param DOMElement elem
|
---|
733 | * @param String type
|
---|
734 | */
|
---|
735 | function triggerEvent( elem, type, event ) {
|
---|
736 | if ( document.createEvent ) {
|
---|
737 | event = document.createEvent("MouseEvents");
|
---|
738 | event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
|
---|
739 | 0, 0, 0, 0, 0, false, false, false, false, 0, null);
|
---|
740 | elem.dispatchEvent( event );
|
---|
741 | } else if ( elem.fireEvent ) {
|
---|
742 | elem.fireEvent("on"+type);
|
---|
743 | }
|
---|
744 | }
|
---|
745 |
|
---|
746 | function extend(a, b){
|
---|
747 | for ( var prop in b ) {
|
---|
748 | a[prop] = b[prop];
|
---|
749 | }
|
---|
750 |
|
---|
751 | return a;
|
---|
752 | }
|
---|
753 |
|
---|
754 | function addEvent(elem, type, fn){
|
---|
755 | if ( elem.addEventListener ) {
|
---|
756 | elem.addEventListener( type, fn, false );
|
---|
757 | } else if ( elem.attachEvent ) {
|
---|
758 | elem.attachEvent( "on" + type, fn );
|
---|
759 | }
|
---|
760 | }
|
---|
761 |
|
---|
762 | })();
|
---|
763 |
|
---|
764 | /**
|
---|
765 | * jsDump
|
---|
766 | * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
|
---|
767 | * Licensed under BSD (http://www.opensource.org/licenses/bsd-license.php)
|
---|
768 | * Date: 5/15/2008
|
---|
769 | * @projectDescription Advanced and extensible data dumping for Javascript.
|
---|
770 | * @version 1.0.0
|
---|
771 | * @author Ariel Flesler
|
---|
772 | * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html}
|
---|
773 | */
|
---|
774 | (function(){
|
---|
775 | function quote( str ){
|
---|
776 | return '"' + str.toString().replace(/"/g, '\\"') + '"';
|
---|
777 | };
|
---|
778 | function literal( o ){
|
---|
779 | return o + '';
|
---|
780 | };
|
---|
781 | function join( pre, arr, post ){
|
---|
782 | var s = jsDump.separator(),
|
---|
783 | base = jsDump.indent(),
|
---|
784 | inner = jsDump.indent(1);
|
---|
785 | if( arr.join )
|
---|
786 | arr = arr.join( ',' + s + inner );
|
---|
787 | if( !arr )
|
---|
788 | return pre + post;
|
---|
789 | return [ pre, inner + arr, base + post ].join(s);
|
---|
790 | };
|
---|
791 | function array( arr ){
|
---|
792 | var i = arr.length, ret = Array(i);
|
---|
793 | this.up();
|
---|
794 | while( i-- )
|
---|
795 | ret[i] = this.parse( arr[i] );
|
---|
796 | this.down();
|
---|
797 | return join( '[', ret, ']' );
|
---|
798 | };
|
---|
799 |
|
---|
800 | var reName = /^function (\w+)/;
|
---|
801 |
|
---|
802 | var jsDump = window.jsDump = {
|
---|
803 | parse:function( obj, type ){//type is used mostly internally, you can fix a (custom)type in advance
|
---|
804 | var parser = this.parsers[ type || this.typeOf(obj) ];
|
---|
805 | type = typeof parser;
|
---|
806 |
|
---|
807 | return type == 'function' ? parser.call( this, obj ) :
|
---|
808 | type == 'string' ? parser :
|
---|
809 | this.parsers.error;
|
---|
810 | },
|
---|
811 | typeOf:function( obj ){
|
---|
812 | var type = typeof obj,
|
---|
813 | f = 'function';//we'll use it 3 times, save it
|
---|
814 | return type != 'object' && type != f ? type :
|
---|
815 | !obj ? 'null' :
|
---|
816 | obj.exec ? 'regexp' :// some browsers (FF) consider regexps functions
|
---|
817 | obj.getHours ? 'date' :
|
---|
818 | obj.scrollBy ? 'window' :
|
---|
819 | obj.nodeName == '#document' ? 'document' :
|
---|
820 | obj.nodeName ? 'node' :
|
---|
821 | obj.item ? 'nodelist' : // Safari reports nodelists as functions
|
---|
822 | obj.callee ? 'arguments' :
|
---|
823 | obj.call || obj.constructor != Array && //an array would also fall on this hack
|
---|
824 | (obj+'').indexOf(f) != -1 ? f : //IE reports functions like alert, as objects
|
---|
825 | 'length' in obj ? 'array' :
|
---|
826 | type;
|
---|
827 | },
|
---|
828 | separator:function(){
|
---|
829 | return this.multiline ? this.HTML ? '<br />' : '\n' : this.HTML ? ' ' : ' ';
|
---|
830 | },
|
---|
831 | indent:function( extra ){// extra can be a number, shortcut for increasing-calling-decreasing
|
---|
832 | if( !this.multiline )
|
---|
833 | return '';
|
---|
834 | var chr = this.indentChar;
|
---|
835 | if( this.HTML )
|
---|
836 | chr = chr.replace(/\t/g,' ').replace(/ /g,' ');
|
---|
837 | return Array( this._depth_ + (extra||0) ).join(chr);
|
---|
838 | },
|
---|
839 | up:function( a ){
|
---|
840 | this._depth_ += a || 1;
|
---|
841 | },
|
---|
842 | down:function( a ){
|
---|
843 | this._depth_ -= a || 1;
|
---|
844 | },
|
---|
845 | setParser:function( name, parser ){
|
---|
846 | this.parsers[name] = parser;
|
---|
847 | },
|
---|
848 | // The next 3 are exposed so you can use them
|
---|
849 | quote:quote,
|
---|
850 | literal:literal,
|
---|
851 | join:join,
|
---|
852 | //
|
---|
853 | _depth_: 1,
|
---|
854 | // This is the list of parsers, to modify them, use jsDump.setParser
|
---|
855 | parsers:{
|
---|
856 | window: '[Window]',
|
---|
857 | document: '[Document]',
|
---|
858 | error:'[ERROR]', //when no parser is found, shouldn't happen
|
---|
859 | unknown: '[Unknown]',
|
---|
860 | 'null':'null',
|
---|
861 | undefined:'undefined',
|
---|
862 | 'function':function( fn ){
|
---|
863 | var ret = 'function',
|
---|
864 | name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE
|
---|
865 | if( name )
|
---|
866 | ret += ' ' + name;
|
---|
867 | ret += '(';
|
---|
868 |
|
---|
869 | ret = [ ret, this.parse( fn, 'functionArgs' ), '){'].join('');
|
---|
870 | return join( ret, this.parse(fn,'functionCode'), '}' );
|
---|
871 | },
|
---|
872 | array: array,
|
---|
873 | nodelist: array,
|
---|
874 | arguments: array,
|
---|
875 | object:function( map ){
|
---|
876 | var ret = [ ];
|
---|
877 | this.up();
|
---|
878 | for( var key in map )
|
---|
879 | ret.push( this.parse(key,'key') + ': ' + this.parse(map[key]) );
|
---|
880 | this.down();
|
---|
881 | return join( '{', ret, '}' );
|
---|
882 | },
|
---|
883 | node:function( node ){
|
---|
884 | var open = this.HTML ? '<' : '<',
|
---|
885 | close = this.HTML ? '>' : '>';
|
---|
886 |
|
---|
887 | var tag = node.nodeName.toLowerCase(),
|
---|
888 | ret = open + tag;
|
---|
889 |
|
---|
890 | for( var a in this.DOMAttrs ){
|
---|
891 | var val = node[this.DOMAttrs[a]];
|
---|
892 | if( val )
|
---|
893 | ret += ' ' + a + '=' + this.parse( val, 'attribute' );
|
---|
894 | }
|
---|
895 | return ret + close + open + '/' + tag + close;
|
---|
896 | },
|
---|
897 | functionArgs:function( fn ){//function calls it internally, it's the arguments part of the function
|
---|
898 | var l = fn.length;
|
---|
899 | if( !l ) return '';
|
---|
900 |
|
---|
901 | var args = Array(l);
|
---|
902 | while( l-- )
|
---|
903 | args[l] = String.fromCharCode(97+l);//97 is 'a'
|
---|
904 | return ' ' + args.join(', ') + ' ';
|
---|
905 | },
|
---|
906 | key:quote, //object calls it internally, the key part of an item in a map
|
---|
907 | functionCode:'[code]', //function calls it internally, it's the content of the function
|
---|
908 | attribute:quote, //node calls it internally, it's an html attribute value
|
---|
909 | string:quote,
|
---|
910 | date:quote,
|
---|
911 | regexp:literal, //regex
|
---|
912 | number:literal,
|
---|
913 | 'boolean':literal
|
---|
914 | },
|
---|
915 | DOMAttrs:{//attributes to dump from nodes, name=>realName
|
---|
916 | id:'id',
|
---|
917 | name:'name',
|
---|
918 | 'class':'className'
|
---|
919 | },
|
---|
920 | HTML:false,//if true, entities are escaped ( <, >, \t, space and \n )
|
---|
921 | indentChar:' ',//indentation unit
|
---|
922 | multiline:true //if true, items in a collection, are separated by a \n, else just a space.
|
---|
923 | };
|
---|
924 |
|
---|
925 | })();
|
---|