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>';
49 document.getElementsByTagName("body")[0].appendChild(result);
53 function test(name, callback) {
54 synchronize(function() {
59 if( typeof console != "undefined" && console.error && console.warn ) {
60 console.error("Test " + name + " died, exception and test follows");
62 console.warn(callback.toString());
64 Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
67 synchronize(function() {
70 var good = 0, bad = 0;
71 var ol = document.createElement("ol");
72 ol.style.display = "none";
73 var li = "", state = "pass";
74 for ( var i = 0; i < Test.length; i++ ) {
75 var li = document.createElement("li");
76 li.className = Test[i][0] ? "pass" : "fail";
77 li.innerHTML = Test[i][1];
88 var li = document.createElement("li");
91 var b = document.createElement("b");
92 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
93 b.onclick = function(){
94 var n = this.nextSibling;
95 if ( jQuery.css( n, "display" ) == "none" )
96 n.style.display = "block";
98 n.style.display = "none";
101 li.appendChild( ol );
103 document.getElementById("tests").appendChild( li );
108 * Resets the test setup. Useful for tests that modify the DOM.
111 document.getElementById('main').innerHTML = fixture;
116 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
118 function ok(a, msg) {
119 Test.push( [ !!a, msg ] );
123 * Asserts that two arrays are the same
125 function isSet(a, b, msg) {
127 if ( a && b && a.length == b.length ) {
134 Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
136 Test.push( [ ret, msg ] );
140 * Returns an array of elements with the given IDs, eg.
141 * @example q("main", "foo", "bar")
142 * @result [<div id="main">, <span id="foo">, <input id="bar">]
146 for ( var i = 0; i < arguments.length; i++ )
147 r.push( document.getElementById( arguments[i] ) );
152 * Asserts that a select matches the given IDs
153 * @example t("Check for something", "//[a]", ["foo", "baar"]);
154 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
157 var f = jQuery.find(b);
159 for ( var i = 0; i < f.length; i++ )
160 s += (s && ",") + '"' + f[i].id + '"';
161 isSet(f, q.apply(q,c), a + " (" + b + ")");