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 = 'Tests completed in ' + runTime + ' milliseconds.<br/>' +
47 stats.bad + ' tests of ' + stats.all + ' failed.';
48 document.getElementsByTagName("body")[0].appendChild(result);
52 function test(name, callback) {
53 synchronize(function() {
58 if( typeof console != "undefined" && console.error && console.warn ) {
59 console.error("Test " + name + " died, exception and test follows");
61 console.warn(callback.toString());
63 Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
66 synchronize(function() {
69 var good = 0, bad = 0;
70 var ol = document.createElement("ol");
72 var li = "", state = "pass";
73 for ( var i = 0; i < Test.length; i++ ) {
74 var li = document.createElement("li");
75 li.className = Test[i][0] ? "pass" : "fail";
76 li.innerHTML = Test[i][1];
87 var li = document.createElement("li");
90 var b = document.createElement("b");
91 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
92 b.onclick = function(){
93 var n = this.nextSibling;
94 if ( jQuery.css( n, "display" ) == "none" )
95 n.style.display = "block";
97 n.style.display = "none";
100 li.appendChild( ol );
102 document.getElementById("tests").appendChild( li );
107 * Resets the test setup. Useful for tests that modify the DOM.
110 document.getElementById('main').innerHTML = fixture;
115 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
117 function ok(a, msg) {
118 Test.push( [ !!a, msg ] );
122 * Asserts that two arrays are the same
124 function isSet(a, b, msg) {
126 if ( a && b && a.length == b.length ) {
132 if ( !ret && console )
133 console.log( msg, a, b );
134 Test.push( [ ret, msg ] );
138 * Returns an array of elements with the given IDs, eg.
139 * @example q("main", "foo", "bar")
140 * @result [<div id="main">, <span id="foo">, <input id="bar">]
144 for ( var i = 0; i < arguments.length; i++ )
145 r.push( document.getElementById( arguments[i] ) );
150 * Asserts that a select matches the given IDs
151 * @example t("Check for something", "//[a]", ["foo", "baar"]);
152 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
155 var f = jQuery.find(b);
157 for ( var i = 0; i < f.length; i++ )
158 s += (s && ",") + '"' + f[i].id + '"';
159 isSet(f, q.apply(q,c), a + " (" + b + ")");