Commit b0c3711d authored by Jörn Zaefferer's avatar Jörn Zaefferer

Refactored test suite to allow async tests (use stop() before starting an...

Refactored test suite to allow async tests (use stop() before starting an async request, use start() when finished), added test for bug #164
parent 4dcbfc92
...@@ -61,8 +61,10 @@ ...@@ -61,8 +61,10 @@
<target name="test" depends="jquery"> <target name="test" depends="jquery">
<echo message="Building Test Suite" /> <echo message="Building Test Suite" />
<delete dir="${TEST_DIR}" /> <delete dir="${TEST_DIR}" />
<mkdir dir="${TEST_DIR}" /> <mkdir dir="${TEST_DIR}/data" />
<copy todir="${TEST_DIR}" file="${BUILD_DIR}/test/testrunner.js" /> <copy todir="${TEST_DIR}/data">
<fileset dir="${BUILD_DIR}/test/data/" />
</copy>
<java jar="${JAR}" fork="true"> <java jar="${JAR}" fork="true">
<arg value="${BUILD_DIR}/test/test.js" /> <arg value="${BUILD_DIR}/test/test.js" />
<arg value="${JQ}" /> <arg value="${JQ}" />
......
<?xml version="1.0" encoding="UTF-8"?>
<dashboard>
<locations>
<location>
<infowindowtab>
<tab title="Location">
<![CDATA[blabla]]>
</tab>
<tab title="Users">
<![CDATA[blublu]]>
</tab>
</infowindowtab>
</location>
</locations>
</dashboard>
\ No newline at end of file
var asyncTimeout = 2 // seconds for async timeout
var fixture; var fixture;
var Test; var Test;
var stats = { var stats = {
all: 0, all: 0,
bad: 0 bad: 0
}; };
var queue = [];
var blocking = false;
function synchronize(callback) {
queue[queue.length] = callback;
if(!blocking) {
process();
}
}
function process() {
while(queue.length && !blocking) {
var call = queue[0];
queue = queue.slice(1);
call();
}
}
function stop() {
blocking = true;
setTimeout(start, asyncTimeout * 1000);
}
function start() {
blocking = false;
process();
}
function runTest(tests) { function runTest(tests) {
var startTime = new Date(); var startTime = new Date();
fixture = document.getElementById('main').innerHTML; fixture = document.getElementById('main').innerHTML;
tests(); tests();
var runTime = new Date() - startTime; synchronize(function() {
var result = document.createElement("div"); var runTime = new Date() - startTime;
result.innerHTML = 'Tests completed in ' + runTime + ' milliseconds.<br/>' + var result = document.createElement("div");
stats.bad + ' tests of ' + stats.all + ' failed.'; result.innerHTML = 'Tests completed in ' + runTime + ' milliseconds.<br/>' +
document.getElementsByTagName("body")[0].appendChild(result); stats.bad + ' tests of ' + stats.all + ' failed.';
document.getElementsByTagName("body")[0].appendChild(result);
});
} }
function test(name, callback) { function test(name, callback) {
Test = []; Test = [];
try { synchronize(function() {
callback(); try {
} catch(e) { callback();
if(typeof console != "undefined") { } catch(e) {
console.error("Test " + name + " died, exception and test follows"); if(typeof console != "undefined") {
console.error(e); console.error("Test " + name + " died, exception and test follows");
console.warn(callback.toString()); console.error(e);
console.warn(callback.toString());
}
Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
}
});
synchronize(function() {
reset();
var good = 0, bad = 0;
var ol = document.createElement("ol");
var li = "", state = "pass";
for ( var i = 0; i < Test.length; i++ ) {
var li = document.createElement("li");
li.className = Test[i][0] ? "pass" : "fail";
li.innerHTML = Test[i][1];
ol.appendChild( li );
stats.all++;
if ( !Test[i][0] ) {
state = "fail";
bad++;
stats.bad++;
} else good++;
} }
Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
}
reset();
var good = 0, bad = 0;
var ol = document.createElement("ol");
var li = "", state = "pass";
for ( var i = 0; i < Test.length; i++ ) {
var li = document.createElement("li"); var li = document.createElement("li");
li.className = Test[i][0] ? "pass" : "fail"; li.className = state;
li.innerHTML = Test[i][1];
ol.appendChild( li ); var b = document.createElement("b");
b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
stats.all++; b.onclick = function(){
if ( !Test[i][0] ) { var n = this.nextSibling;
state = "fail"; if ( jQuery.css( n, "display" ) == "none" )
bad++; n.style.display = "block";
stats.bad++; else
} else good++; n.style.display = "none";
} };
li.appendChild( b );
var li = document.createElement("li"); li.appendChild( ol );
li.className = state;
document.getElementById("tests").appendChild( li );
var b = document.createElement("b"); });
b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
b.onclick = function(){
var n = this.nextSibling;
if ( jQuery.css( n, "display" ) == "none" )
n.style.display = "block";
else
n.style.display = "none";
};
li.appendChild( b );
li.appendChild( ol );
document.getElementById("tests").appendChild( li );
} }
/** /**
......
<html id="html"> <html id="html">
<head> <head>
<script type="text/javascript" src="../dist/jquery.js"></script> <script type="text/javascript" src="../dist/jquery.js"></script>
<script type="text/javascript" src="testrunner.js"></script> <script type="text/javascript" src="data/testrunner.js"></script>
<script> <script>
$(document).ready(function(){ $(document).ready(function(){
runTest(function() { runTest(function() {
......
...@@ -215,6 +215,19 @@ jQuery.extend({ ...@@ -215,6 +215,19 @@ jQuery.extend({
* } * }
* ) * )
* *
* @test stop();
* $.get("data/dashboard.xml", function(xml) {
* var content = [];
* $('tab', xml).each(function(k) {
* // workaround for IE needed here, $(this).text() throws an error
* // content[k] = $.trim(this.firstChild.data) || $(this).text();
* content[k] = $(this).text();
* });
* ok( content[0].match(/blabla/), 'Check first tab' );
* ok( content[1].match(/blublu/), 'Check second tab' );
* start();
* });
*
* @name $.get * @name $.get
* @type jQuery * @type jQuery
* @param String url The URL of the page to load. * @param String url The URL of the page to load.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment