1 var asyncTimeout = 2 // seconds for async timeout
13 function synchronize(callback) {
14 queue[queue.length] = callback;
21 while(queue.length && !blocking) {
23 queue = queue.slice(1);
30 timeout = setTimeout(start, asyncTimeout * 1000);
34 clearTimeout(timeout);
39 function runTest(tests) {
40 var startTime = new Date();
41 fixture = document.getElementById('main').innerHTML;
43 synchronize(function() {
44 var runTime = new Date() - startTime;
45 var result = document.createElement("div");
46 result.innerHTML = ['<p class="result">Tests completed in ',
47 runTime, ' milliseconds.<br/>',
48 stats.bad, ' tests of ', stats.all, ' failed.</p>'].join('');
49 document.getElementsByTagName("body")[0].appendChild(result);
50 $("<div id='banner'>").addClass(stats.bad ? "fail" : "pass").insertAfter("h1");
54 function test(name, callback) {
55 synchronize(function() {
60 if( typeof console != "undefined" && console.error && console.warn ) {
61 console.error("Test " + name + " died, exception and test follows");
63 console.warn(callback.toString());
65 Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
68 synchronize(function() {
71 var good = 0, bad = 0;
72 var ol = document.createElement("ol");
73 ol.style.display = "none";
74 var li = "", state = "pass";
75 for ( var i = 0; i < Test.length; i++ ) {
76 var li = document.createElement("li");
77 li.className = Test[i][0] ? "pass" : "fail";
78 li.innerHTML = Test[i][1];
89 var li = document.createElement("li");
92 var b = document.createElement("b");
93 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
94 b.onclick = function(){
95 var n = this.nextSibling;
96 if ( jQuery.css( n, "display" ) == "none" )
97 n.style.display = "block";
99 n.style.display = "none";
102 li.appendChild( ol );
104 document.getElementById("tests").appendChild( li );
109 * Resets the test setup. Useful for tests that modify the DOM.
112 document.getElementById('main').innerHTML = fixture;
117 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
119 function ok(a, msg) {
120 Test.push( [ !!a, msg ] );
124 * Asserts that two arrays are the same
126 function isSet(a, b, msg) {
128 if ( a && b && a.length == b.length ) {
135 Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
137 Test.push( [ ret, msg ] );
141 * Returns an array of elements with the given IDs, eg.
142 * @example q("main", "foo", "bar")
143 * @result [<div id="main">, <span id="foo">, <input id="bar">]
147 for ( var i = 0; i < arguments.length; i++ )
148 r.push( document.getElementById( arguments[i] ) );
153 * Asserts that a select matches the given IDs
154 * @example t("Check for something", "//[a]", ["foo", "baar"]);
155 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
158 var f = jQuery.find(b);
160 for ( var i = 0; i < f.length; i++ )
161 s += (s && ",") + '"' + f[i].id + '"';
162 isSet(f, q.apply(q,c), a + " (" + b + ")");