From fb0f6baf2470900f087eba0f2653b747534500b3 Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 11 Aug 2011 22:32:59 -0700 Subject: [PATCH] refactor of test suite runner, added passed/failed counts to suite results --- tests/unit/runner.js | 94 +++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/tests/unit/runner.js b/tests/unit/runner.js index 2f36e1ed..bc741e96 100644 --- a/tests/unit/runner.js +++ b/tests/unit/runner.js @@ -1,58 +1,64 @@ -(function(){ - var test = function(data){ - var $frameElem = $("#testFrame"), - template = $frameElem.attr("data-src"), - updateFrame = function(dir){ - return $frameElem.attr("src", template.replace("{{testdir}}", dir)); - }; +$(function() { + var Runner = function( ) { + var self = this; - $.each(data.directories, function(i, dir){ - asyncTest( dir, function(){ - var testTimeout = 3 * 60 * 1000, checkInterval = 2000; + $.extend( self, { + frame: window.frames[ "testFrame" ], - // establish a timeout for a given suite in case of async tests hanging - var testTimer = setTimeout( function(){ - // prevent any schedule checks for completion - clearTimeouts(); - start(); - }, testTimeout ), + testTimeout: 3 * 60 * 1000, - checkTimer = setInterval( check, checkInterval ), + $frameElem: $( "#testFrame" ), - clearTimeouts = function(){ - // prevent the next interval of the check function and the test timeout - clearTimeout( checkTimer ); - clearTimeout( testTimer ); - }; + onTimeout: QUnit.start, - // check the iframe for success or failure and respond accordingly - function check(){ - // check for the frames jquery object each time - var framejQuery = window.frames["testFrame"].jQuery; + onFrameDone: function( failed, passed, total, runtime ){ + // record success + self.recordResult( false , failed ); + self.recordResult( true , passed ); - // if the iframe hasn't loaded (ie loaded jQuery) check back again shortly - if( !framejQuery ) return; + // make sure we don't time out the tests + clearTimeout( self.testTimer ); - // grab the result of the iframe test suite - // TODO strip extra white space - var result = framejQuery( "#qunit-banner" ).attr( "class" ); + // TODO decipher actual cause of multiple test results firing twice + // clear the done call to prevent early completion of other test cases + self.frame.QUnit.done = $.noop; - // if we have a result check it, otherwise check back shortly - if( result ){ - ok( result === "qunit-pass" ); - clearTimeouts(); - start(); - } - }; + // continue on to the next suite + QUnit.start(); + }, - expect( 1 ); + recordResult: function( result, count ) { + for( var i = 0; i < count; i++ ) { + ok( result ); + } + }, - // set the test suite page on the iframe - updateFrame( dir ); - }); + createTest: function( dir ) { + // check for the frames jquery object each time + $( "iframe#testFrame" ).one( "load", function() { + + // establish a timeout for a given suite in case of async tests hanging + self.testTimer = setTimeout( self.onTimeout, self.testTimeout ); + + // when the QUnit object reports done in the iframe + // run the onFrameDone method + self.frame.QUnit.done = self.onFrameDone; + }); + }, + + exec: function( data ) { + var template = self.$frameElem.attr( "data-src" ); + + $.each( data.directories, function(i, dir) { + asyncTest( dir, function() { + self.createTest( dir ); + self.$frameElem.attr( "src", template.replace("{{testdir}}", dir) ); + }); + }); + } }); }; // get the test directories - $.get("ls.php", test); -})(); + $.get( "ls.php", (new Runner()).exec ); +});