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(start, _config.asyncTimeout * 1000);
42 clearTimeout(_config.timeout);
43 _config.blocking = false;
48 _config.blocking = false;
49 var time = new Date();
50 _config.fixture = document.getElementById('main').innerHTML;
51 synchronize(function() {
52 time = new Date() - time;
53 $("<div>").html(['<p class="result">Tests completed in ',
54 time, ' milliseconds.<br/>',
55 _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
58 $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
62 function test(name, callback) {
63 if(_config.currentModule)
64 name = _config.currentModule + " module: " + name;
65 synchronize(function() {
70 if( typeof console != "undefined" && console.error && console.warn ) {
71 console.error("Test " + name + " died, exception and test follows");
73 console.warn(callback.toString());
75 _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
78 synchronize(function() {
81 if(_config.expected && _config.expected != _config.Test.length) {
82 _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
84 _config.expected = null;
86 var good = 0, bad = 0;
87 var ol = document.createElement("ol");
88 ol.style.display = "none";
89 var li = "", state = "pass";
90 for ( var i = 0; i < _config.Test.length; i++ ) {
91 var li = document.createElement("li");
92 li.className = _config.Test[i][0] ? "pass" : "fail";
93 li.innerHTML = _config.Test[i][1];
97 if ( !_config.Test[i][0] ) {
104 var li = document.createElement("li");
105 li.className = state;
107 var b = document.createElement("b");
108 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
109 b.onclick = function(){
110 var n = this.nextSibling;
111 if ( jQuery.css( n, "display" ) == "none" )
112 n.style.display = "block";
114 n.style.display = "none";
117 li.appendChild( ol );
119 document.getElementById("tests").appendChild( li );
123 // call on start of module test to prepend name to all tests
124 function module(moduleName) {
125 _config.currentModule = moduleName;
129 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
131 function expect(asserts) {
132 _config.expected = asserts;
136 * Resets the test setup. Useful for tests that modify the DOM.
139 document.getElementById('main').innerHTML = _config.fixture;
144 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
146 function ok(a, msg) {
147 _config.Test.push( [ !!a, msg ] );
151 * Asserts that two arrays are the same
153 function isSet(a, b, msg) {
155 if ( a && b && a.length == b.length ) {
162 _config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
164 _config.Test.push( [ ret, msg ] );
168 * Returns an array of elements with the given IDs, eg.
169 * @example q("main", "foo", "bar")
170 * @result [<div id="main">, <span id="foo">, <input id="bar">]
174 for ( var i = 0; i < arguments.length; i++ )
175 r.push( document.getElementById( arguments[i] ) );
180 * Asserts that a select matches the given IDs
181 * @example t("Check for something", "//[a]", ["foo", "baar"]);
182 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
185 var f = jQuery.find(b);
187 for ( var i = 0; i < f.length; i++ )
188 s += (s && ",") + '"' + f[i].id + '"';
189 isSet(f, q.apply(q,c), a + " (" + b + ")");