13 asyncTimeout: 2 // seconds for async timeout
17 $('#userAgent').html(navigator.userAgent);
21 function synchronize(callback) {
22 _config.queue[_config.queue.length] = callback;
23 if(!_config.blocking) {
29 while(_config.queue.length && !_config.blocking) {
30 var call = _config.queue[0];
31 _config.queue = _config.queue.slice(1);
37 _config.blocking = true;
38 _config.timeout = setTimeout(function() {
39 ok( false, "Test timed out" );
41 }, _config.asyncTimeout * 1000);
45 clearTimeout(_config.timeout);
46 _config.blocking = false;
51 _config.blocking = false;
52 var time = new Date();
53 _config.fixture = document.getElementById('main').innerHTML;
54 synchronize(function() {
55 time = new Date() - time;
56 $("<div>").html(['<p class="result">Tests completed in ',
57 time, ' milliseconds.<br/>',
58 _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
61 $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
65 function test(name, callback) {
66 if(_config.currentModule)
67 name = _config.currentModule + " module: " + name;
68 synchronize(function() {
73 if( typeof console != "undefined" && console.error && console.warn ) {
74 console.error("Test " + name + " died, exception and test follows");
76 console.warn(callback.toString());
78 _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
81 synchronize(function() {
84 if(_config.expected && _config.expected != _config.Test.length) {
85 _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
87 _config.expected = null;
89 var good = 0, bad = 0;
90 var ol = document.createElement("ol");
91 ol.style.display = "none";
92 var li = "", state = "pass";
93 for ( var i = 0; i < _config.Test.length; i++ ) {
94 var li = document.createElement("li");
95 li.className = _config.Test[i][0] ? "pass" : "fail";
96 li.innerHTML = _config.Test[i][1];
100 if ( !_config.Test[i][0] ) {
107 var li = document.createElement("li");
108 li.className = state;
110 var b = document.createElement("b");
111 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
112 b.onclick = function(){
113 var n = this.nextSibling;
114 if ( jQuery.css( n, "display" ) == "none" )
115 n.style.display = "block";
117 n.style.display = "none";
120 li.appendChild( ol );
122 document.getElementById("tests").appendChild( li );
126 // call on start of module test to prepend name to all tests
127 function module(moduleName) {
128 _config.currentModule = moduleName;
132 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
134 function expect(asserts) {
135 _config.expected = asserts;
139 * Resets the test setup. Useful for tests that modify the DOM.
142 document.getElementById('main').innerHTML = _config.fixture;
147 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
149 function ok(a, msg) {
150 _config.Test.push( [ !!a, msg ] );
154 * Asserts that two arrays are the same
156 function isSet(a, b, msg) {
158 if ( a && b && a.length == b.length ) {
165 _config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
167 _config.Test.push( [ ret, msg ] );
171 * Returns an array of elements with the given IDs, eg.
172 * @example q("main", "foo", "bar")
173 * @result [<div id="main">, <span id="foo">, <input id="bar">]
177 for ( var i = 0; i < arguments.length; i++ )
178 r.push( document.getElementById( arguments[i] ) );
183 * Asserts that a select matches the given IDs
184 * @example t("Check for something", "//[a]", ["foo", "baar"]);
185 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
188 var f = jQuery.find(b);
190 for ( var i = 0; i < f.length; i++ )
191 s += (s && ",") + '"' + f[i].id + '"';
192 isSet(f, q.apply(q,c), a + " (" + b + ")");