Skip to content
Permalink
Browse files

Testsuite 2.0

  • Loading branch information
jzaefferer committed Nov 18, 2006
1 parent 797ccba commit 7cc550727c4e0bcd93c5d435f0799f568fc74dfa
Showing with 930 additions and 746 deletions.
  1. +7 −11 build.xml
  2. +1 −0 build/test/data/test.js
  3. +71 −44 build/test/data/testrunner.js
  4. +8 −10 build/test/index.html
  5. +0 −220 src/ajax/ajax.js
  6. +271 −0 src/ajax/ajaxTest.js
  7. +0 −51 src/event/event.js
  8. +11 −0 src/event/eventTest.js
  9. +0 −8 src/fx/fx.js
  10. +12 −0 src/fx/fxTest.js
  11. +549 −0 src/jquery/coreTest.js
  12. +0 −402 src/jquery/jquery.js
@@ -45,10 +45,10 @@
<mkdir dir="${DIST_DIR}" />
<concat destfile="${JQ}">
<fileset dir="${SRC_DIR}" includes="intro.js" />
<fileset dir="${SRC_DIR}" includes="jquery/*.js" />
<fileset dir="${SRC_DIR}" includes="event/*.js" />
<fileset dir="${SRC_DIR}" includes="fx/*.js" />
<fileset dir="${SRC_DIR}" includes="ajax/*.js" />
<fileset dir="${SRC_DIR}" includes="jquery/jquery.js" />
<fileset dir="${SRC_DIR}" includes="event/event.js" />
<fileset dir="${SRC_DIR}" includes="fx/fx.js" />
<fileset dir="${SRC_DIR}" includes="ajax/ajax.js" />
<fileset dir="${PLUGIN_DIR}" includes="${PLUGINS}" />
<fileset dir="${SRC_DIR}" includes="outro.js" />
</concat>
@@ -101,18 +101,14 @@
<echo message="${JQ_PACK} built." />
</target>

<target name="test" depends="jquery" description="Reads tests from source and compiles into html file, testsuite must be run on a webserver">
<target name="test" depends="jquery" description="Copy files for the test suite into their own directory.">
<echo message="Building Test Suite" />
<delete dir="${TEST_DIR}" />
<mkdir dir="${TEST_DIR}/data" />
<copy todir="${TEST_DIR}/data">
<fileset dir="${BUILD_DIR}/test/data/" />
</copy>
<java jar="${JAR}" fork="true">
<arg value="${BUILD_DIR}/test/test.js" />
<arg value="${JQ}" />
<arg value="${TEST_DIR}" />
</java>
</copy>
<copy todir="${TEST_DIR}" file="${BUILD_DIR}/test/index.html" />
<echo message="Test Suite built." />
</target>

@@ -1,2 +1,3 @@
foobar = "bar";
$('#ap').html('bar');
ok( true, "test.js executed");
@@ -1,59 +1,69 @@
var asyncTimeout = 2 // seconds for async timeout

var fixture;
var Test;
var stats = {
all: 0,
bad: 0
var _config = {
fixture: null,
Test: [],
stats: {
all: 0,
bad: 0
},
queue: [],
blocking: true,
timeout: null,
expected: null,
currentModule: null,
asyncTimeout: 2 // seconds for async timeout
};
var queue = [];
var blocking = false;
var timeout;

$(function() {
$('#userAgent').html(navigator.userAgent);
runTest();
});

function synchronize(callback) {
queue[queue.length] = callback;
if(!blocking) {
_config.queue[_config.queue.length] = callback;
if(!_config.blocking) {
process();
}
}

function process() {
while(queue.length && !blocking) {
var call = queue[0];
queue = queue.slice(1);
while(_config.queue.length && !_config.blocking) {
var call = _config.queue[0];
_config.queue = _config.queue.slice(1);
call();
}
}

function stop() {
blocking = true;
timeout = setTimeout(start, asyncTimeout * 1000);
_config.blocking = true;
_config.timeout = setTimeout(start, _config.asyncTimeout * 1000);
}
function start() {
if(timeout)
clearTimeout(timeout);
blocking = false;
if(_config.timeout)
clearTimeout(_config.timeout);
_config.blocking = false;
process();
}

function runTest(tests) {
var startTime = new Date();
fixture = document.getElementById('main').innerHTML;
tests();
function runTest() {
_config.blocking = false;
var time = new Date();
_config.fixture = document.getElementById('main').innerHTML;
synchronize(function() {
var runTime = new Date() - startTime;
var result = document.createElement("div");
result.innerHTML = ['<p class="result">Tests completed in ',
runTime, ' milliseconds.<br/>',
stats.bad, ' tests of ', stats.all, ' failed.</p>'].join('');
document.getElementsByTagName("body")[0].appendChild(result);
$("<div id='banner'>").addClass(stats.bad ? "fail" : "pass").insertAfter("h1");
time = new Date() - time;
$("<div>").html(['<p class="result">Tests completed in ',
time, ' milliseconds.<br/>',
_config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
.join(''))
.appendTo("body");
$("<div id='banner'>").addClass(_config.stats.bad ? "fail" : "pass").insertAfter("h1");
});
}

function test(name, callback) {
if(_config.currentModule)
name = _config.currentModule + " module: " + name;
synchronize(function() {
Test = [];
_config.Test = [];
try {
callback();
} catch(e) {
@@ -62,35 +72,40 @@ function test(name, callback) {
console.error(e);
console.warn(callback.toString());
}
Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
_config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
}
});
synchronize(function() {
reset();

if(_config.expected && _config.expected != _config.Test.length) {
_config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
}
_config.expected = null;

var good = 0, bad = 0;
var ol = document.createElement("ol");
ol.style.display = "none";
var li = "", state = "pass";
for ( var i = 0; i < Test.length; i++ ) {
for ( var i = 0; i < _config.Test.length; i++ ) {
var li = document.createElement("li");
li.className = Test[i][0] ? "pass" : "fail";
li.innerHTML = Test[i][1];
li.className = _config.Test[i][0] ? "pass" : "fail";
li.innerHTML = _config.Test[i][1];
ol.appendChild( li );

stats.all++;
if ( !Test[i][0] ) {
_config.stats.all++;
if ( !_config.Test[i][0] ) {
state = "fail";
bad++;
stats.bad++;
_config.stats.bad++;
} else good++;
}

var li = document.createElement("li");
li.className = state;

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.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
b.onclick = function(){
var n = this.nextSibling;
if ( jQuery.css( n, "display" ) == "none" )
@@ -105,19 +120,31 @@ function test(name, callback) {
});
}

// call on start of module test to prepend name to all tests
function module(moduleName) {
_config.currentModule = moduleName;
}

/**
* Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
*/
function expect(asserts) {
_config.expected = asserts;
}

/**
* Resets the test setup. Useful for tests that modify the DOM.
*/
function reset() {
document.getElementById('main').innerHTML = fixture;
document.getElementById('main').innerHTML = _config.fixture;
}

/**
* Asserts true.
* @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
*/
function ok(a, msg) {
Test.push( [ !!a, msg ] );
_config.Test.push( [ !!a, msg ] );
}

/**
@@ -132,9 +159,9 @@ function isSet(a, b, msg) {
} else
ret = false;
if ( !ret )
Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
_config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] );
else
Test.push( [ ret, msg ] );
_config.Test.push( [ ret, msg ] );
}

/**
@@ -1,19 +1,17 @@
<html id="html">
<head>
<title>jQuery Test Suite</title>
<link rel="Stylesheet" media="screen" href="data/testsuite.css" />
<script type="text/javascript" src="../dist/jquery.js"></script>
<script type="text/javascript" src="data/testrunner.js"></script>
<script>
$(document).ready(function(){
$('#userAgent').html(navigator.userAgent);
runTest(function() {
{TESTS}
});
});
</script>
<link rel="Stylesheet" media="screen" href="data/testsuite.css" />
<script type="text/javascript" src="../src/jquery/coreTest.js"></script>
<script type="text/javascript" src="../src/event/eventTest.js"></script>
<script type="text/javascript" src="../src/ajax/ajaxTest.js"></script>
<script type="text/javascript" src="../src/fx/fxTest.js"></script>
</head>

<body id="body">
<h1 id="banner">jQuery Core - Test Suite</h1>
<h1 id="banner">jQuery Test Suite - Core</h1>
<h2 id="userAgent"></h2>

<!-- Test HTML -->

0 comments on commit 7cc5507

Please sign in to comment.
You can’t perform that action at this time.