// package system gunk. //try{ // dojo.provide("doh.runner"); //}catch(e){ if(!this["doh"]){ doh = {}; } //} // // Utility Functions and Classes // doh.selfTest = false; doh.global = this; doh.hitch = function(/*Object*/thisObject, /*Function|String*/method /*, ...*/){ var args = []; for(var x=2; x= 0)) { this._fire(); } }, _continue: function(res){ this._resback(res); this._unpause(); }, _resback: function(res){ this.fired = ((res instanceof Error) ? 1 : 0); this.results[this.fired] = res; this._fire(); }, _check: function(){ if(this.fired != -1){ if(!this.silentlyCancelled){ throw new Error("already called!"); } this.silentlyCancelled = false; return; } }, callback: function(res){ this._check(); this._resback(res); }, errback: function(res){ this._check(); if(!(res instanceof Error)){ res = new Error(res); } this._resback(res); }, addBoth: function(cb, cbfn){ var enclosed = this.getFunctionFromArgs(cb, cbfn); if(arguments.length > 2){ enclosed = doh.hitch(null, enclosed, arguments, 2); } return this.addCallbacks(enclosed, enclosed); }, addCallback: function(cb, cbfn){ var enclosed = this.getFunctionFromArgs(cb, cbfn); if(arguments.length > 2){ enclosed = doh.hitch(null, enclosed, arguments, 2); } return this.addCallbacks(enclosed, null); }, addErrback: function(cb, cbfn){ var enclosed = this.getFunctionFromArgs(cb, cbfn); if(arguments.length > 2){ enclosed = doh.hitch(null, enclosed, arguments, 2); } return this.addCallbacks(null, enclosed); }, addCallbacks: function(cb, eb){ this.chain.push([cb, eb]); if(this.fired >= 0){ this._fire(); } return this; }, _fire: function(){ var chain = this.chain; var fired = this.fired; var res = this.results[fired]; var self = this; var cb = null; while(chain.length > 0 && this.paused == 0){ // Array var pair = chain.shift(); var f = pair[fired]; if(f == null){ continue; } try { res = f(res); fired = ((res instanceof Error) ? 1 : 0); if(res instanceof doh.Deferred){ cb = function(res){ self._continue(res); }; this._pause(); } }catch(err){ fired = 1; res = err; } } this.fired = fired; this.results[fired] = res; if((cb)&&(this.paused)){ res.addBoth(cb); } } }); // // State Keeping and Reporting // doh._testCount = 0; doh._groupCount = 0; doh._errorCount = 0; doh._failureCount = 0; doh._currentGroup = null; doh._currentTest = null; doh._paused = true; doh._init = function(){ this._currentGroup = null; this._currentTest = null; this._errorCount = 0; this._failureCount = 0; this.debug(this._testCount, "tests to run in", this._groupCount, "groups"); } // doh._urls = []; doh._groups = {}; // // Test Registration // doh.registerTestNs = function(/*String*/ group, /*Object*/ ns){ // summary: // adds the passed namespace object to the list of objects to be // searched for test groups. Only "public" functions (not prefixed // with "_") will be added as tests to be run. If you'd like to use // fixtures (setUp(), tearDown(), and runTest()), please use // registerTest() or registerTests(). for(var x in ns){ if( (x.charAt(0) != "_") && (typeof ns[x] == "function") ){ this.registerTest(group, ns[x]); } } } doh._testRegistered = function(group, fixture){ // slot to be filled in } doh._groupStarted = function(group){ // slot to be filled in } doh._groupFinished = function(group, success){ // slot to be filled in } doh._testStarted = function(group, fixture){ // slot to be filled in } doh._testFinished = function(group, fixture, success){ // slot to be filled in } doh.registerGroup = function( /*String*/ group, /*Array||Function||Object*/ tests, /*Function*/ setUp, /*Function*/ tearDown, /*String*/ type){ // summary: // registers an entire group of tests at once and provides a setUp and // tearDown facility for groups. If you call this method with only // setUp and tearDown parameters, they will replace previously // installed setUp or tearDown functions for the group with the new // methods. // group: // string name of the group // tests: // either a function or an object or an array of functions/objects. If // an object, it must contain at *least* a "runTest" method, and may // also contain "setUp" and "tearDown" methods. These will be invoked // on either side of the "runTest" method (respectively) when the test // is run. If an array, it must contain objects matching the above // description or test functions. // setUp: a function for initializing the test group // tearDown: a function for initializing the test group // type: The type of tests these are, such as a group of performance tests // null/undefied are standard DOH tests, the valye 'perf' enables // registering them as performance tests. if(tests){ this.register(group, tests, type); } if(setUp){ this._groups[group].setUp = setUp; } if(tearDown){ this._groups[group].tearDown = tearDown; } } doh._getTestObj = function(group, test, type){ var tObj = test; if(typeof test == "string"){ if(test.substr(0, 4)=="url:"){ return this.registerUrl(group, test); }else{ tObj = { name: test.replace("/\s/g", "_") // FIXME: bad escapement }; tObj.runTest = new Function("t", test); } }else if(typeof test == "function"){ // if we didn't get a fixture, wrap the function tObj = { "runTest": test }; if(test["name"]){ tObj.name = test.name; }else{ try{ var fStr = "function "; var ts = tObj.runTest+""; if(0 <= ts.indexOf(fStr)){ tObj.name = ts.split(fStr)[1].split("(", 1)[0]; } // doh.debug(tObj.runTest.toSource()); }catch(e){ } } // FIXME: try harder to get the test name here } //Augment the test with some specific options to make it identifiable as a //particular type of test so it can be executed properly. if(type === "perf" || tObj.testType === "perf"){ tObj.testType = "perf"; //Build an object on the root DOH class to contain all the test results. //Cache it on the test object for quick lookup later for results storage. if(!doh.perfTestResults){ doh.perfTestResults = {}; doh.perfTestResults[group] = {}; } if(!doh.perfTestResults[group]){ doh.perfTestResults[group] = {}; } if(!doh.perfTestResults[group][tObj.name]){ doh.perfTestResults[group][tObj.name] = {}; } tObj.results = doh.perfTestResults[group][tObj.name]; //If it's not set, then set the trial duration //default to 100ms. if(!("trialDuration" in tObj)){ tObj.trialDuration = 100; } //If it's not set, then set the delay between trial runs to 100ms //default to 100ms to allow for GC and to make IE happy. if(!("trialDelay" in tObj)){ tObj.trialDelay = 100; } //If it's not set, then set number of times a trial is run to 10. if(!("trialIterations" in tObj)){ tObj.trialIterations = 10; } } return tObj; } doh.registerTest = function(/*String*/ group, /*Function||Object*/ test, /*String*/ type){ // summary: // add the provided test function or fixture object to the specified // test group. // group: // string name of the group to add the test to // test: // either a function or an object. If an object, it must contain at // *least* a "runTest" method, and may also contain "setUp" and // "tearDown" methods. These will be invoked on either side of the // "runTest" method (respectively) when the test is run. // type: // An identifier denoting the type of testing that the test performs, such // as a performance test. If null, defaults to regular DOH test. if(!this._groups[group]){ this._groupCount++; this._groups[group] = []; this._groups[group].inFlight = 0; } var tObj = this._getTestObj(group, test, type); if(!tObj){ return null; } this._groups[group].push(tObj); this._testCount++; this._testRegistered(group, tObj); return tObj; } doh.registerTests = function(/*String*/ group, /*Array*/ testArr, /*String*/ type){ // summary: // registers a group of tests, treating each element of testArr as // though it were being (along with group) passed to the registerTest // method. It also uses the type to decide how the tests should // behave, by defining the type of tests these are, such as performance tests for(var x=0; x 1) ? "s" : "")+" to run"); } doh._handleFailure = function(groupName, fixture, e){ // this.debug("FAILED test:", fixture.name); // mostly borrowed from JUM this._groups[groupName].failures++; var out = ""; if(e instanceof this._AssertFailure){ this._failureCount++; if(e["fileName"]){ out += e.fileName + ':'; } if(e["lineNumber"]){ out += e.lineNumber + ' '; } out += e+": "+e.message; this.debug("\t_AssertFailure:", out); }else{ this._errorCount++; } this.debug(e); if(fixture.runTest["toSource"]){ var ss = fixture.runTest.toSource(); this.debug("\tERROR IN:\n\t\t", ss); }else{ this.debug("\tERROR IN:\n\t\t", fixture.runTest); } if(e.rhinoException){ e.rhinoException.printStackTrace(); }else if(e.javaException){ e.javaException.printStackTrace(); } } //Assume a setTimeout implementation that is synchronous, so that //the Node and Rhino envs work similar to each other. Node defines //a setTimeout, so testing for setTimeout is not enough, each environment //adapter should set this value accordingly. doh.setTimeout = function(func){ return func(); }; doh._runPerfFixture = function(/*String*/groupName, /*Object*/fixture){ // summary: // This function handles how to execute a 'performance' test // which is different from a straight UT style test. These // will often do numerous iterations of the same operation and // gather execution statistics about it, like max, min, average, // etc. It makes use of the already in place DOH deferred test // handling since it is a good idea to put a pause inbetween each // iteration to allow for GC cleanup and the like. // // groupName: // The test group that contains this performance test. // fixture: // The performance test fixture. var tg = this._groups[groupName]; fixture.startTime = new Date(); //Perf tests always need to act in an async manner as there is a //number of iterations to flow through. var def = new doh.Deferred(); tg.inFlight++; def.groupName = groupName; def.fixture = fixture; def.addErrback(function(err){ doh._handleFailure(groupName, fixture, err); }); //Set up the finalizer. var retEnd = function(){ if(fixture["tearDown"]){ fixture.tearDown(doh); } tg.inFlight--; if((!tg.inFlight)&&(tg.iterated)){ doh._groupFinished(groupName, !tg.failures); } doh._testFinished(groupName, fixture, def.results[0]); if(doh._paused){ doh.run(); } }; //Since these can take who knows how long, we don't want to timeout //unless explicitly set var timer; var to = fixture.timeout; if(to > 0) { timer = doh.setTimeout(function(){ // ret.cancel(); // retEnd(); def.errback(new Error("test timeout in "+fixture.name.toString())); }, to); } //Set up the end calls to the test into the deferred we'll return. def.addBoth(function(arg){ if(timer){ clearTimeout(timer); } retEnd(); }); //Okay, now set up the timing loop for the actual test. //This is down as an async type test where there is a delay //between each execution to allow for GC time, etc, so the GC //has less impact on the tests. var res = fixture.results; res.trials = []; //Try to figure out how many calls are needed to hit a particular threshold. var itrDef = doh._calcTrialIterations(groupName, fixture); itrDef.addErrback(function(err){ fixture.endTime = new Date(); def.errback(err); }); //Blah, since tests can be deferred, the actual run has to be deferred until after //we know how many iterations to run. This is just plain ugly. itrDef.addCallback(function(iterations){ if(iterations){ var countdown = fixture.trialIterations; doh.debug("TIMING TEST: [" + fixture.name + "]\n\t\tITERATIONS PER TRIAL: " + iterations + "\n\tTRIALS: " + countdown); //Figure out how many times we want to run our 'trial'. //Where each trial consists of 'iterations' of the test. var trialRunner = function() { //Set up our function to execute a block of tests var start = new Date(); var tTimer = new doh.Deferred(); var tCountdown = iterations; var tState = { countdown: iterations }; var testRunner = function(state){ while(state){ try{ state.countdown--; if(state.countdown){ var ret = fixture.runTest(doh); if(ret instanceof doh.Deferred){ //Deferreds have to be handled async, //otherwise we just keep looping. var atState = { countdown: state.countdown }; ret.addCallback(function(){ testRunner(atState); }); ret.addErrback(function(err) { doh._handleFailure(groupName, fixture, err); fixture.endTime = new Date(); def.errback(err); }); state = null; } }else{ tTimer.callback(new Date()); state = null; } }catch(err){ fixture.endTime = new Date(); tTimer.errback(err); } } }; tTimer.addCallback(function(end){ //Figure out the results and try to factor out function call costs. var tResults = { trial: (fixture.trialIterations - countdown), testIterations: iterations, executionTime: (end.getTime() - start.getTime()), average: (end.getTime() - start.getTime())/iterations }; res.trials.push(tResults); doh.debug("\n\t\tTRIAL #: " + tResults.trial + "\n\tTIME: " + tResults.executionTime + "ms.\n\tAVG TEST TIME: " + (tResults.executionTime/tResults.testIterations) + "ms."); //Okay, have we run all the trials yet? countdown--; if(countdown){ doh.setTimeout(trialRunner, fixture.trialDelay); }else{ //Okay, we're done, lets compute some final performance results. var t = res.trials; //We're done. fixture.endTime = new Date(); def.callback(true); } }); tTimer.addErrback(function(err){ fixture.endTime = new Date(); def.errback(err); }); testRunner(tState); }; trialRunner(); } }); //Set for a pause, returned the deferred. if(def.fired < 0){ doh.pause(); } return def; }; doh._calcTrialIterations = function(/*String*/ groupName, /*Object*/ fixture){ // summary: // This function determines the rough number of iterations to // use to reach a particular MS threshold. This returns a deferred // since tests can theoretically by async. Async tests aren't going to // give great perf #s, though. // The callback is passed the # of iterations to hit the requested // threshold. // // fixture: // The test fixture we want to calculate iterations for. var def = new doh.Deferred(); var calibrate = function () { var testFunc = fixture.runTest; //Set the initial state. We have to do this as a loop instead //of a recursive function. Otherwise, it blows the call stack //on some browsers. var iState = { start: new Date(), curIter: 0, iterations: 5 }; var handleIteration = function(state){ while(state){ if(state.curIter < state.iterations){ try{ var ret = testFunc(doh); if(ret instanceof doh.Deferred){ var aState = { start: state.start, curIter: state.curIter + 1, iterations: state.iterations }; ret.addCallback(function(){ handleIteration(aState); }); ret.addErrback(function(err) { fixture.endTime = new Date(); def.errback(err); }); state = null; }else{ state.curIter++; } }catch(err){ fixture.endTime = new Date(); def.errback(err); return; } }else{ var end = new Date(); var totalTime = (end.getTime() - state.start.getTime()); if(totalTime < fixture.trialDuration){ var nState = { iterations: state.iterations * 2, curIter: 0 } state = null; doh.setTimeout(function(){ nState.start = new Date(); handleIteration(nState); }, 50); }else{ var itrs = state.iterations; doh.setTimeout(function(){def.callback(itrs)}, 50); state = null; } } } }; handleIteration(iState); }; doh.setTimeout(calibrate, 10); return def; }; doh._runRegFixture = function(/*String*/groupName, /*Object*/fixture){ // summary: // Function to run a generic doh test. These are not // specialized tests, like performance groups and such. // // groupName: // The groupName of the test. // fixture: // The test fixture to execute. var tg = this._groups[groupName]; fixture.startTime = new Date(); var ret = fixture.runTest(this); fixture.endTime = new Date(); // if we get a deferred back from the test runner, we know we're // gonna wait for an async result. It's up to the test code to trap // errors and give us an errback or callback. if(ret instanceof doh.Deferred){ tg.inFlight++; ret.groupName = groupName; ret.fixture = fixture; ret.addErrback(function(err){ doh._handleFailure(groupName, fixture, err); }); var retEnd = function(){ if(fixture["tearDown"]){ fixture.tearDown(doh); } tg.inFlight--; if((!tg.inFlight)&&(tg.iterated)){ doh._groupFinished(groupName, !tg.failures); } doh._testFinished(groupName, fixture, ret.results[0]); if(doh._paused){ doh.run(); } } var timer = doh.setTimeout(function(){ // ret.cancel(); // retEnd(); ret.errback(new Error("test timeout in "+fixture.name.toString())); }, fixture["timeout"]||1000); ret.addBoth(function(arg){ clearTimeout(timer); retEnd(); }); if(ret.fired < 0){ doh.pause(); } return ret; } }; doh._runFixture = function(groupName, fixture){ var tg = this._groups[groupName]; this._testStarted(groupName, fixture); var threw = false; var err = null; // run it, catching exceptions and reporting them try{ // let doh reference "this.group.thinger..." which can be set by // another test or group-level setUp function fixture.group = tg; // only execute the parts of the fixture we've got if(fixture["setUp"]){ fixture.setUp(this); } if(fixture["runTest"]){ // should we error out of a fixture doesn't have a runTest? if(fixture.testType === "perf"){ //Always async deferred, so return it. return doh._runPerfFixture(groupName, fixture); }else{ //May or may not by async. var ret = doh._runRegFixture(groupName, fixture); if(ret){ return ret; } } } if(fixture["tearDown"]){ fixture.tearDown(this); } }catch(e){ threw = true; err = e; if(!fixture.endTime){ fixture.endTime = new Date(); } } var d = new doh.Deferred(); doh.setTimeout(this.hitch(this, function(){ if(threw){ this._handleFailure(groupName, fixture, err); } this._testFinished(groupName, fixture, !threw); if((!tg.inFlight)&&(tg.iterated)){ doh._groupFinished(groupName, !tg.failures); }else if(tg.inFlight > 0){ doh.setTimeout(this.hitch(this, function(){ doh.runGroup(groupName); // , idx); }), 100); this._paused = true; } if(doh._paused){ doh.run(); } }), 30); doh.pause(); return d; } doh._testId = 0; doh.runGroup = function(/*String*/ groupName, /*Integer*/ idx){ // summary: // runs the specified test group // the general structure of the algorithm is to run through the group's // list of doh, checking before and after each of them to see if we're in // a paused state. This can be caused by the test returning a deferred or // the user hitting the pause button. In either case, we want to halt // execution of the test until something external to us restarts it. This // means we need to pickle off enough state to pick up where we left off. // FIXME: need to make fixture execution async!! var tg = this._groups[groupName]; if(tg.skip === true){ return; } if(this._isArray(tg)){ if(idx<=tg.length){ if((!tg.inFlight)&&(tg.iterated == true)){ if(tg["tearDown"]){ tg.tearDown(this); } doh._groupFinished(groupName, !tg.failures); return; } } if(!idx){ tg.inFlight = 0; tg.iterated = false; tg.failures = 0; } doh._groupStarted(groupName); if(!idx){ this._setupGroupForRun(groupName, idx); if(tg["setUp"]){ tg.setUp(this); } } for(var y=(idx||0); y"); } */ } } }catch(e){ print("\n"+doh._line); print("The Dojo Unit Test Harness, $Rev: 20389 $"); print("Copyright (c) 2009, The Dojo Foundation, All Rights Reserved"); print(doh._line, "\n"); try{ var dojoUrl = "../../dojo/dojo.js"; var testUrl = ""; var testModule = "dojo.tests.module"; var dohBase = ""; for(x=0; x