1 var asyncTimeout = 2 // seconds for async timeout
12 function synchronize(callback) {
13 queue[queue.length] = callback;
20 while(queue.length && !blocking) {
22 queue = queue.slice(1);
29 setTimeout(start, asyncTimeout * 1000);
36 function runTest(tests) {
37 var startTime = new Date();
38 fixture = document.getElementById('main').innerHTML;
40 synchronize(function() {
41 var runTime = new Date() - startTime;
42 var result = document.createElement("div");
43 result.innerHTML = 'Tests completed in ' + runTime + ' milliseconds.<br/>' +
44 stats.bad + ' tests of ' + stats.all + ' failed.';
45 document.getElementsByTagName("body")[0].appendChild(result);
49 function test(name, callback) {
51 synchronize(function() {
55 if(typeof console != "undefined") {
56 console.error("Test " + name + " died, exception and test follows");
58 console.warn(callback.toString());
60 Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
63 synchronize(function() {
66 var good = 0, bad = 0;
67 var ol = document.createElement("ol");
69 var li = "", state = "pass";
70 for ( var i = 0; i < Test.length; i++ ) {
71 var li = document.createElement("li");
72 li.className = Test[i][0] ? "pass" : "fail";
73 li.innerHTML = Test[i][1];
84 var li = document.createElement("li");
87 var b = document.createElement("b");
88 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
89 b.onclick = function(){
90 var n = this.nextSibling;
91 if ( jQuery.css( n, "display" ) == "none" )
92 n.style.display = "block";
94 n.style.display = "none";
99 document.getElementById("tests").appendChild( li );
104 * Resets the test setup. Useful for tests that modify the DOM.
107 document.getElementById('main').innerHTML = fixture;
112 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
114 function ok(a, msg) {
115 Test.push( [ !!a, msg ] );
119 * Asserts that two arrays are the same
121 function isSet(a, b, msg) {
123 if ( a && b && a.length == b.length ) {
129 if ( !ret && console )
130 console.log( msg, a, b );
131 Test.push( [ ret, msg ] );
135 * Returns an array of elements with the given IDs, eg.
136 * @example q("main", "foo", "bar")
137 * @result [<div id="main">, <span id="foo">, <input id="bar">]
141 for ( var i = 0; i < arguments.length; i++ )
142 r.push( document.getElementById( arguments[i] ) );
147 * Asserts that a select matches the given IDs
148 * @example t("Check for something", "//[a]", ["foo", "baar"]);
149 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
152 var f = jQuery.find(b);
154 for ( var i = 0; i < f.length; i++ )
155 s += (s && ",") + '"' + f[i].id + '"';
156 isSet(f, q.apply(q,c), a + " (" + b + ")");