1
// package system gunk.
3
// dojo.provide("doh.runner");
11
// Utility Functions and Classes
18
doh.hitch = function(/*Object*/thisObject, /*Function|String*/method /*, ...*/){
20
for(var x=2; x<arguments.length; x++){
21
args.push(arguments[x]);
23
var fcn = ((typeof method == "string") ? thisObject[method] : method) || function(){};
25
var ta = args.concat([]); // make a copy
26
for(var x=0; x<arguments.length; x++){
27
ta.push(arguments[x]);
29
return fcn.apply(thisObject, ta); // Function
33
doh._mixin = function(/*Object*/ obj, /*Object*/ props){
35
// Adds all properties and methods of props to obj. This addition is
36
// "prototype extension safe", so that instances of objects will not
37
// pass along prototype defaults.
40
// the "tobj" condition avoid copying properties in "props"
41
// inherited from Object.prototype. For example, if obj has a custom
42
// toString() method, don't overwrite it with the toString() method
43
// that props inherited from Object.protoype
44
if(tobj[x] === undefined || tobj[x] != props[x]){
48
// IE doesn't recognize custom toStrings in for..in
51
&& (typeof props["toString"] == "function")
52
&& (props["toString"] != obj["toString"])
53
&& (props["toString"] != tobj["toString"])
55
obj.toString = props.toString;
60
doh.mixin = function(/*Object*/obj, /*Object...*/props){
61
// summary: Adds all properties and methods of props to obj.
62
for(var i=1, l=arguments.length; i<l; i++){
63
doh._mixin(obj, arguments[i]);
68
doh.extend = function(/*Object*/ constructor, /*Object...*/ props){
70
// Adds all properties and methods of props to constructor's
71
// prototype, making them available to all instances created with
73
for(var i=1, l=arguments.length; i<l; i++){
74
doh._mixin(constructor.prototype, arguments[i]);
76
return constructor; // Object
80
doh._line = "------------------------------------------------------------";
83
doh._delegate = function(obj, props){
84
// boodman-crockford delegation
89
dojo.lang.mixin(tmp, props);
95
doh.debug = function(){
97
// takes any number of arguments and sends them to whatever debugging
98
// or logging facility is available in this environment
100
// YOUR TEST RUNNER NEEDS TO IMPLEMENT THIS
103
doh._AssertFailure = function(msg, hint){
104
// idea for this as way of dis-ambiguating error types is from JUM.
105
// The JUM is dead! Long live the JUM!
107
if(!(this instanceof doh._AssertFailure)){
108
return new doh._AssertFailure(msg, hint);
111
msg = (new String(msg||""))+" with hint: \n\t\t"+(new String(hint)+"\n");
113
this.message = new String(msg||"");
116
doh._AssertFailure.prototype = new Error();
117
doh._AssertFailure.prototype.constructor = doh._AssertFailure;
118
doh._AssertFailure.prototype.name = "doh._AssertFailure";
120
doh.Deferred = function(canceller){
122
this.id = this._nextId();
125
this.results = [null, null];
126
this.canceller = canceller;
127
this.silentlyCancelled = false;
130
doh.extend(doh.Deferred, {
131
getTestErrback: function(cb, scope){
132
// summary: Replaces outer getTextCallback's in nested situations to avoid multiple callback(true)'s
136
cb.apply(scope||doh.global||_this, arguments);
143
getTestCallback: function(cb, scope){
147
cb.apply(scope||doh.global||_this, arguments);
152
_this.callback(true);
156
getFunctionFromArgs: function(){
159
if(typeof a[0] == "function"){
161
}else if(typeof a[0] == "string"){
162
return doh.global[a[0]];
164
}else if((a[0])&&(a[1])){
165
return doh.hitch(a[0], a[1]);
170
makeCalled: function() {
171
var deferred = new doh.Deferred();
176
_nextId: (function(){
178
return function(){ return n++; };
182
if(this.fired == -1){
184
this.canceller(this);
186
this.silentlyCancelled = true;
188
if(this.fired == -1){
189
this.errback(new Error("Deferred(unfired)"));
191
}else if(this.fired == 0 &&
192
(this.results[0] instanceof doh.Deferred)){
193
this.results[0].cancel();
202
_unpause: function(){
204
if ((this.paused == 0) && (this.fired >= 0)) {
209
_continue: function(res){
214
_resback: function(res){
215
this.fired = ((res instanceof Error) ? 1 : 0);
216
this.results[this.fired] = res;
221
if(this.fired != -1){
222
if(!this.silentlyCancelled){
223
throw new Error("already called!");
225
this.silentlyCancelled = false;
230
callback: function(res){
235
errback: function(res){
237
if(!(res instanceof Error)){
238
res = new Error(res);
243
addBoth: function(cb, cbfn){
244
var enclosed = this.getFunctionFromArgs(cb, cbfn);
245
if(arguments.length > 2){
246
enclosed = doh.hitch(null, enclosed, arguments, 2);
248
return this.addCallbacks(enclosed, enclosed);
251
addCallback: function(cb, cbfn){
252
var enclosed = this.getFunctionFromArgs(cb, cbfn);
253
if(arguments.length > 2){
254
enclosed = doh.hitch(null, enclosed, arguments, 2);
256
return this.addCallbacks(enclosed, null);
259
addErrback: function(cb, cbfn){
260
var enclosed = this.getFunctionFromArgs(cb, cbfn);
261
if(arguments.length > 2){
262
enclosed = doh.hitch(null, enclosed, arguments, 2);
264
return this.addCallbacks(null, enclosed);
267
addCallbacks: function(cb, eb){
268
this.chain.push([cb, eb]);
276
var chain = this.chain;
277
var fired = this.fired;
278
var res = this.results[fired];
281
while(chain.length > 0 && this.paused == 0){
283
var pair = chain.shift();
290
fired = ((res instanceof Error) ? 1 : 0);
291
if(res instanceof doh.Deferred){
303
this.results[fired] = res;
304
if((cb)&&(this.paused)){
311
// State Keeping and Reporting
317
doh._failureCount = 0;
318
doh._currentGroup = null;
319
doh._currentTest = null;
322
doh._init = function(){
323
this._currentGroup = null;
324
this._currentTest = null;
325
this._errorCount = 0;
326
this._failureCount = 0;
327
this.debug(this._testCount, "tests to run in", this._groupCount, "groups");
337
doh.registerTestNs = function(/*String*/ group, /*Object*/ ns){
339
// adds the passed namespace object to the list of objects to be
340
// searched for test groups. Only "public" functions (not prefixed
341
// with "_") will be added as tests to be run. If you'd like to use
342
// fixtures (setUp(), tearDown(), and runTest()), please use
343
// registerTest() or registerTests().
345
if( (x.charAt(0) != "_") &&
346
(typeof ns[x] == "function") ){
347
this.registerTest(group, ns[x]);
352
doh._testRegistered = function(group, fixture){
353
// slot to be filled in
356
doh._groupStarted = function(group){
357
// slot to be filled in
360
doh._groupFinished = function(group, success){
361
// slot to be filled in
364
doh._testStarted = function(group, fixture){
365
// slot to be filled in
368
doh._testFinished = function(group, fixture, success){
369
// slot to be filled in
372
doh.registerGroup = function( /*String*/ group,
373
/*Array||Function||Object*/ tests,
375
/*Function*/ tearDown,
378
// registers an entire group of tests at once and provides a setUp and
379
// tearDown facility for groups. If you call this method with only
380
// setUp and tearDown parameters, they will replace previously
381
// installed setUp or tearDown functions for the group with the new
384
// string name of the group
386
// either a function or an object or an array of functions/objects. If
387
// an object, it must contain at *least* a "runTest" method, and may
388
// also contain "setUp" and "tearDown" methods. These will be invoked
389
// on either side of the "runTest" method (respectively) when the test
390
// is run. If an array, it must contain objects matching the above
391
// description or test functions.
392
// setUp: a function for initializing the test group
393
// tearDown: a function for initializing the test group
394
// type: The type of tests these are, such as a group of performance tests
395
// null/undefied are standard DOH tests, the valye 'perf' enables
396
// registering them as performance tests.
398
this.register(group, tests, type);
401
this._groups[group].setUp = setUp;
404
this._groups[group].tearDown = tearDown;
408
doh._getTestObj = function(group, test, type){
410
if(typeof test == "string"){
411
if(test.substr(0, 4)=="url:"){
412
return this.registerUrl(group, test);
415
name: test.replace("/\s/g", "_") // FIXME: bad escapement
417
tObj.runTest = new Function("t", test);
419
}else if(typeof test == "function"){
420
// if we didn't get a fixture, wrap the function
421
tObj = { "runTest": test };
423
tObj.name = test.name;
426
var fStr = "function ";
427
var ts = tObj.runTest+"";
428
if(0 <= ts.indexOf(fStr)){
429
tObj.name = ts.split(fStr)[1].split("(", 1)[0];
431
// doh.debug(tObj.runTest.toSource());
435
// FIXME: try harder to get the test name here
438
//Augment the test with some specific options to make it identifiable as a
439
//particular type of test so it can be executed properly.
440
if(type === "perf" || tObj.testType === "perf"){
441
tObj.testType = "perf";
443
//Build an object on the root DOH class to contain all the test results.
444
//Cache it on the test object for quick lookup later for results storage.
445
if(!doh.perfTestResults){
446
doh.perfTestResults = {};
447
doh.perfTestResults[group] = {};
449
if(!doh.perfTestResults[group]){
450
doh.perfTestResults[group] = {};
452
if(!doh.perfTestResults[group][tObj.name]){
453
doh.perfTestResults[group][tObj.name] = {};
455
tObj.results = doh.perfTestResults[group][tObj.name];
457
//If it's not set, then set the trial duration
459
if(!("trialDuration" in tObj)){
460
tObj.trialDuration = 100;
463
//If it's not set, then set the delay between trial runs to 100ms
464
//default to 100ms to allow for GC and to make IE happy.
465
if(!("trialDelay" in tObj)){
466
tObj.trialDelay = 100;
469
//If it's not set, then set number of times a trial is run to 10.
470
if(!("trialIterations" in tObj)){
471
tObj.trialIterations = 10;
477
doh.registerTest = function(/*String*/ group, /*Function||Object*/ test, /*String*/ type){
479
// add the provided test function or fixture object to the specified
482
// string name of the group to add the test to
484
// either a function or an object. If an object, it must contain at
485
// *least* a "runTest" method, and may also contain "setUp" and
486
// "tearDown" methods. These will be invoked on either side of the
487
// "runTest" method (respectively) when the test is run.
489
// An identifier denoting the type of testing that the test performs, such
490
// as a performance test. If null, defaults to regular DOH test.
491
if(!this._groups[group]){
493
this._groups[group] = [];
494
this._groups[group].inFlight = 0;
496
var tObj = this._getTestObj(group, test, type);
497
if(!tObj){ return null; }
498
this._groups[group].push(tObj);
500
this._testRegistered(group, tObj);
504
doh.registerTests = function(/*String*/ group, /*Array*/ testArr, /*String*/ type){
506
// registers a group of tests, treating each element of testArr as
507
// though it were being (along with group) passed to the registerTest
508
// method. It also uses the type to decide how the tests should
509
// behave, by defining the type of tests these are, such as performance tests
510
for(var x=0; x<testArr.length; x++){
511
this.registerTest(group, testArr[x], type);
515
// FIXME: move implementation to _browserRunner?
516
doh.registerUrl = function( /*String*/ group,
520
this.debug("ERROR:");
521
this.debug("\tNO registerUrl() METHOD AVAILABLE.");
522
// this._urls.push(url);
525
doh.registerString = function(group, str, type){
528
// FIXME: remove the doh.add alias SRTL.
529
doh.register = doh.add = function(groupOrNs, testOrNull, type){
531
// "magical" variant of registerTests, registerTest, and
532
// registerTestNs. Will accept the calling arguments of any of these
533
// methods and will correctly guess the right one to register with.
534
if( (arguments.length == 1)&&
535
(typeof groupOrNs == "string") ){
536
if(groupOrNs.substr(0, 4)=="url:"){
537
this.registerUrl(groupOrNs, null, null, type);
539
this.registerTest("ungrouped", groupOrNs, type);
542
if(arguments.length == 1){
543
this.debug("invalid args passed to doh.register():", groupOrNs, ",", testOrNull);
546
if(typeof testOrNull == "string"){
547
if(testOrNull.substr(0, 4)=="url:"){
548
this.registerUrl(testOrNull, null, null, type);
550
this.registerTest(groupOrNs, testOrNull, type);
552
// this.registerTestNs(groupOrNs, testOrNull);
555
if(doh._isArray(testOrNull)){
556
this.registerTests(groupOrNs, testOrNull, type);
559
this.registerTest(groupOrNs, testOrNull, type);
562
doh.registerDocTests = function(module){
563
// no-op for when Dojo isn't loaded into the page
564
this.debug("registerDocTests() requires dojo to be loaded into the environment. Skipping doctest set for module:", module);
567
if(typeof dojo != "undefined"){
569
dojo.require("dojox.testing.DocTest");
571
// if the DocTest module isn't available (e.g., the build we're
572
// running from doesn't include it), stub it out and log the error
575
doh.registerDocTests = function(){}
578
doh.registerDocTests = function(module){
580
// Get all the doctests from the given module and register each of them
581
// as a single test case here.
584
var docTest = new dojox.testing.DocTest();
585
var docTests = docTest.getTests(module);
586
var len = docTests.length;
588
for (var i=0; i<len; i++){
589
var test = docTests[i];
590
// Extract comment on first line and add to test name.
592
if (test.commands.length && test.commands[0].indexOf("//")!=-1) {
593
var parts = test.commands[0].split("//");
594
comment = ", "+parts[parts.length-1]; // Get all after the last //, so we dont get trapped by http:// or alikes :-).
597
runTest: (function(test){
599
var r = docTest.runTest(test.commands, test.expectedResult);
600
t.assertTrue(r.success);
603
name:"Line "+test.line+comment
607
this.register("DocTests: "+module, tests);
613
// Assertions and In-Test Utilities
616
doh.t = doh.assertTrue = function(/*Object*/ condition, /*String?*/ hint){
618
// is the passed item "truthy"?
619
if(arguments.length < 1){
620
throw new doh._AssertFailure("assertTrue failed because it was not passed at least 1 argument");
622
if(!eval(condition)){
623
throw new doh._AssertFailure("assertTrue('" + condition + "') failed", hint);
627
doh.f = doh.assertFalse = function(/*Object*/ condition, /*String?*/ hint){
629
// is the passed item "falsey"?
630
if(arguments.length < 1){
631
throw new doh._AssertFailure("assertFalse failed because it was not passed at least 1 argument");
634
throw new doh._AssertFailure("assertFalse('" + condition + "') failed", hint);
638
doh.e = doh.assertError = function(/*Error object*/expectedError, /*Object*/scope, /*String*/functionName, /*Array*/args, /*String?*/ hint){
640
// Test for a certain error to be thrown by the given function.
642
// t.assertError(dojox.data.QueryReadStore.InvalidAttributeError, store, "getValue", [item, "NOT THERE"]);
643
// t.assertError(dojox.data.QueryReadStore.InvalidItemError, store, "getValue", ["not an item", "NOT THERE"]);
645
scope[functionName].apply(scope, args);
647
if(e instanceof expectedError){
650
throw new doh._AssertFailure("assertError() failed:\n\texpected error\n\t\t"+expectedError+"\n\tbut got\n\t\t"+e+"\n\n", hint);
653
throw new doh._AssertFailure("assertError() failed:\n\texpected error\n\t\t"+expectedError+"\n\tbut no error caught\n\n", hint);
657
doh.is = doh.assertEqual = function(/*Object*/ expected, /*Object*/ actual, /*String?*/ hint){
659
// are the passed expected and actual objects/values deeply
662
// Compare undefined always with three equal signs, because undefined==null
663
// is true, but undefined===null is false.
664
if((expected === undefined)&&(actual === undefined)){
667
if(arguments.length < 2){
668
throw doh._AssertFailure("assertEqual failed because it was not passed 2 arguments");
670
if((expected === actual)||(expected == actual)||
671
( typeof expected == "number" && typeof actual == "number" && isNaN(expected) && isNaN(actual) )){
674
if( (this._isArray(expected) && this._isArray(actual))&&
675
(this._arrayEq(expected, actual)) ){
678
if( ((typeof expected == "object")&&((typeof actual == "object")))&&
679
(this._objPropEq(expected, actual)) ){
682
throw new doh._AssertFailure("assertEqual() failed:\n\texpected\n\t\t"+expected+"\n\tbut got\n\t\t"+actual+"\n\n", hint);
685
doh.isNot = doh.assertNotEqual = function(/*Object*/ notExpected, /*Object*/ actual, /*String?*/ hint){
687
// are the passed notexpected and actual objects/values deeply
690
// Compare undefined always with three equal signs, because undefined==null
691
// is true, but undefined===null is false.
692
if((notExpected === undefined)&&(actual === undefined)){
693
throw new doh._AssertFailure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint);
695
if(arguments.length < 2){
696
throw doh._AssertFailure("assertEqual failed because it was not passed 2 arguments");
698
if((notExpected === actual)||(notExpected == actual)){
699
throw new doh._AssertFailure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint);
701
if( (this._isArray(notExpected) && this._isArray(actual))&&
702
(this._arrayEq(notExpected, actual)) ){
703
throw new doh._AssertFailure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint);
705
if( ((typeof notExpected == "object")&&((typeof actual == "object")))&&
706
(this._objPropEq(notExpected, actual)) ){
707
throw new doh._AssertFailure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint);
712
doh._arrayEq = function(expected, actual){
713
if(expected.length != actual.length){ return false; }
714
// FIXME: we're not handling circular refs. Do we care?
715
for(var x=0; x<expected.length; x++){
716
if(!doh.assertEqual(expected[x], actual[x])){ return false; }
721
doh._objPropEq = function(expected, actual){
722
// Degenerate case: if they are both null, then their "properties" are equal.
723
if(expected === null && actual === null){
726
// If only one is null, they aren't equal.
727
if(expected === null || actual === null){
730
if(expected instanceof Date){
731
return actual instanceof Date && expected.getTime()==actual.getTime();
734
// Make sure ALL THE SAME properties are in both objects!
735
for(x in actual){ // Lets check "actual" here, expected is checked below.
736
if(expected[x] === undefined){
742
if(!doh.assertEqual(expected[x], actual[x])){
749
doh._isArray = function(it){
750
return (it && it instanceof Array || typeof it == "array" ||
752
!!doh.global["dojo"] &&
753
doh.global["dojo"]["NodeList"] !== undefined &&
754
it instanceof doh.global["dojo"]["NodeList"]
763
doh._setupGroupForRun = function(/*String*/ groupName, /*Integer*/ idx){
764
var tg = this._groups[groupName];
765
this.debug(this._line);
766
this.debug("GROUP", "\""+groupName+"\"", "has", tg.length, "test"+((tg.length > 1) ? "s" : "")+" to run");
769
doh._handleFailure = function(groupName, fixture, e){
770
// this.debug("FAILED test:", fixture.name);
771
// mostly borrowed from JUM
772
this._groups[groupName].failures++;
774
if(e instanceof this._AssertFailure){
775
this._failureCount++;
776
if(e["fileName"]){ out += e.fileName + ':'; }
777
if(e["lineNumber"]){ out += e.lineNumber + ' '; }
778
out += e+": "+e.message;
779
this.debug("\t_AssertFailure:", out);
784
if(fixture.runTest["toSource"]){
785
var ss = fixture.runTest.toSource();
786
this.debug("\tERROR IN:\n\t\t", ss);
788
this.debug("\tERROR IN:\n\t\t", fixture.runTest);
791
if(e.rhinoException){
792
e.rhinoException.printStackTrace();
793
}else if(e.javaException){
794
e.javaException.printStackTrace();
798
//Assume a setTimeout implementation that is synchronous, so that
799
//the Node and Rhino envs work similar to each other. Node defines
800
//a setTimeout, so testing for setTimeout is not enough, each environment
801
//adapter should set this value accordingly.
802
doh.setTimeout = function(func){
806
doh._runPerfFixture = function(/*String*/groupName, /*Object*/fixture){
808
// This function handles how to execute a 'performance' test
809
// which is different from a straight UT style test. These
810
// will often do numerous iterations of the same operation and
811
// gather execution statistics about it, like max, min, average,
812
// etc. It makes use of the already in place DOH deferred test
813
// handling since it is a good idea to put a pause inbetween each
814
// iteration to allow for GC cleanup and the like.
817
// The test group that contains this performance test.
819
// The performance test fixture.
820
var tg = this._groups[groupName];
821
fixture.startTime = new Date();
823
//Perf tests always need to act in an async manner as there is a
824
//number of iterations to flow through.
825
var def = new doh.Deferred();
827
def.groupName = groupName;
828
def.fixture = fixture;
830
def.addErrback(function(err){
831
doh._handleFailure(groupName, fixture, err);
834
//Set up the finalizer.
835
var retEnd = function(){
836
if(fixture["tearDown"]){ fixture.tearDown(doh); }
838
if((!tg.inFlight)&&(tg.iterated)){
839
doh._groupFinished(groupName, !tg.failures);
841
doh._testFinished(groupName, fixture, def.results[0]);
847
//Since these can take who knows how long, we don't want to timeout
848
//unless explicitly set
850
var to = fixture.timeout;
852
timer = doh.setTimeout(function(){
855
def.errback(new Error("test timeout in "+fixture.name.toString()));
859
//Set up the end calls to the test into the deferred we'll return.
860
def.addBoth(function(arg){
867
//Okay, now set up the timing loop for the actual test.
868
//This is down as an async type test where there is a delay
869
//between each execution to allow for GC time, etc, so the GC
870
//has less impact on the tests.
871
var res = fixture.results;
874
//Try to figure out how many calls are needed to hit a particular threshold.
875
var itrDef = doh._calcTrialIterations(groupName, fixture);
876
itrDef.addErrback(function(err){
877
fixture.endTime = new Date();
881
//Blah, since tests can be deferred, the actual run has to be deferred until after
882
//we know how many iterations to run. This is just plain ugly.
883
itrDef.addCallback(function(iterations){
885
var countdown = fixture.trialIterations;
886
doh.debug("TIMING TEST: [" + fixture.name +
887
"]\n\t\tITERATIONS PER TRIAL: " +
888
iterations + "\n\tTRIALS: " +
891
//Figure out how many times we want to run our 'trial'.
892
//Where each trial consists of 'iterations' of the test.
894
var trialRunner = function() {
895
//Set up our function to execute a block of tests
896
var start = new Date();
897
var tTimer = new doh.Deferred();
898
var tCountdown = iterations;
901
countdown: iterations
903
var testRunner = function(state){
908
var ret = fixture.runTest(doh);
909
if(ret instanceof doh.Deferred){
910
//Deferreds have to be handled async,
911
//otherwise we just keep looping.
913
countdown: state.countdown
915
ret.addCallback(function(){
918
ret.addErrback(function(err) {
919
doh._handleFailure(groupName, fixture, err);
920
fixture.endTime = new Date();
926
tTimer.callback(new Date());
930
fixture.endTime = new Date();
935
tTimer.addCallback(function(end){
936
//Figure out the results and try to factor out function call costs.
938
trial: (fixture.trialIterations - countdown),
939
testIterations: iterations,
940
executionTime: (end.getTime() - start.getTime()),
941
average: (end.getTime() - start.getTime())/iterations
943
res.trials.push(tResults);
944
doh.debug("\n\t\tTRIAL #: " +
945
tResults.trial + "\n\tTIME: " +
946
tResults.executionTime + "ms.\n\tAVG TEST TIME: " +
947
(tResults.executionTime/tResults.testIterations) + "ms.");
949
//Okay, have we run all the trials yet?
952
doh.setTimeout(trialRunner, fixture.trialDelay);
954
//Okay, we're done, lets compute some final performance results.
960
fixture.endTime = new Date();
964
tTimer.addErrback(function(err){
965
fixture.endTime = new Date();
974
//Set for a pause, returned the deferred.
981
doh._calcTrialIterations = function(/*String*/ groupName, /*Object*/ fixture){
983
// This function determines the rough number of iterations to
984
// use to reach a particular MS threshold. This returns a deferred
985
// since tests can theoretically by async. Async tests aren't going to
986
// give great perf #s, though.
987
// The callback is passed the # of iterations to hit the requested
991
// The test fixture we want to calculate iterations for.
992
var def = new doh.Deferred();
993
var calibrate = function () {
994
var testFunc = fixture.runTest;
996
//Set the initial state. We have to do this as a loop instead
997
//of a recursive function. Otherwise, it blows the call stack
1004
var handleIteration = function(state){
1006
if(state.curIter < state.iterations){
1008
var ret = testFunc(doh);
1009
if(ret instanceof doh.Deferred){
1012
curIter: state.curIter + 1,
1013
iterations: state.iterations
1015
ret.addCallback(function(){
1016
handleIteration(aState);
1018
ret.addErrback(function(err) {
1019
fixture.endTime = new Date();
1027
fixture.endTime = new Date();
1032
var end = new Date();
1033
var totalTime = (end.getTime() - state.start.getTime());
1034
if(totalTime < fixture.trialDuration){
1036
iterations: state.iterations * 2,
1040
doh.setTimeout(function(){
1041
nState.start = new Date();
1042
handleIteration(nState);
1045
var itrs = state.iterations;
1046
doh.setTimeout(function(){def.callback(itrs)}, 50);
1052
handleIteration(iState);
1054
doh.setTimeout(calibrate, 10);
1058
doh._runRegFixture = function(/*String*/groupName, /*Object*/fixture){
1060
// Function to run a generic doh test. These are not
1061
// specialized tests, like performance groups and such.
1064
// The groupName of the test.
1066
// The test fixture to execute.
1067
var tg = this._groups[groupName];
1068
fixture.startTime = new Date();
1069
var ret = fixture.runTest(this);
1070
fixture.endTime = new Date();
1071
// if we get a deferred back from the test runner, we know we're
1072
// gonna wait for an async result. It's up to the test code to trap
1073
// errors and give us an errback or callback.
1074
if(ret instanceof doh.Deferred){
1076
ret.groupName = groupName;
1077
ret.fixture = fixture;
1079
ret.addErrback(function(err){
1080
doh._handleFailure(groupName, fixture, err);
1083
var retEnd = function(){
1084
if(fixture["tearDown"]){ fixture.tearDown(doh); }
1086
if((!tg.inFlight)&&(tg.iterated)){
1087
doh._groupFinished(groupName, !tg.failures);
1089
doh._testFinished(groupName, fixture, ret.results[0]);
1095
var timer = doh.setTimeout(function(){
1098
ret.errback(new Error("test timeout in "+fixture.name.toString()));
1099
}, fixture["timeout"]||1000);
1101
ret.addBoth(function(arg){
1102
clearTimeout(timer);
1112
doh._runFixture = function(groupName, fixture){
1113
var tg = this._groups[groupName];
1114
this._testStarted(groupName, fixture);
1117
// run it, catching exceptions and reporting them
1119
// let doh reference "this.group.thinger..." which can be set by
1120
// another test or group-level setUp function
1122
// only execute the parts of the fixture we've got
1124
if(fixture["setUp"]){ fixture.setUp(this); }
1125
if(fixture["runTest"]){ // should we error out of a fixture doesn't have a runTest?
1126
if(fixture.testType === "perf"){
1127
//Always async deferred, so return it.
1128
return doh._runPerfFixture(groupName, fixture);
1130
//May or may not by async.
1131
var ret = doh._runRegFixture(groupName, fixture);
1137
if(fixture["tearDown"]){ fixture.tearDown(this); }
1141
if(!fixture.endTime){
1142
fixture.endTime = new Date();
1145
var d = new doh.Deferred();
1146
doh.setTimeout(this.hitch(this, function(){
1148
this._handleFailure(groupName, fixture, err);
1150
this._testFinished(groupName, fixture, !threw);
1152
if((!tg.inFlight)&&(tg.iterated)){
1153
doh._groupFinished(groupName, !tg.failures);
1154
}else if(tg.inFlight > 0){
1155
doh.setTimeout(this.hitch(this, function(){
1156
doh.runGroup(groupName); // , idx);
1158
this._paused = true;
1169
doh.runGroup = function(/*String*/ groupName, /*Integer*/ idx){
1171
// runs the specified test group
1173
// the general structure of the algorithm is to run through the group's
1174
// list of doh, checking before and after each of them to see if we're in
1175
// a paused state. This can be caused by the test returning a deferred or
1176
// the user hitting the pause button. In either case, we want to halt
1177
// execution of the test until something external to us restarts it. This
1178
// means we need to pickle off enough state to pick up where we left off.
1180
// FIXME: need to make fixture execution async!!
1182
var tg = this._groups[groupName];
1183
if(tg.skip === true){ return; }
1184
if(this._isArray(tg)){
1186
if((!tg.inFlight)&&(tg.iterated == true)){
1187
if(tg["tearDown"]){ tg.tearDown(this); }
1188
doh._groupFinished(groupName, !tg.failures);
1194
tg.iterated = false;
1197
doh._groupStarted(groupName);
1199
this._setupGroupForRun(groupName, idx);
1200
if(tg["setUp"]){ tg.setUp(this); }
1202
for(var y=(idx||0); y<tg.length; y++){
1204
this._currentTest = y;
1205
// this.debug("PAUSED at:", tg[y].name, this._currentGroup, this._currentTest);
1208
doh._runFixture(groupName, tg[y]);
1210
this._currentTest = y+1;
1211
if(this._currentTest == tg.length){
1214
// this.debug("PAUSED at:", tg[y].name, this._currentGroup, this._currentTest);
1220
if(tg["tearDown"]){ tg.tearDown(this); }
1221
doh._groupFinished(groupName, !tg.failures);
1226
doh._onEnd = function(){}
1228
doh._report = function(){
1230
// a private method to be implemented/replaced by the "locally
1231
// appropriate" test runner
1233
// this.debug("ERROR:");
1234
// this.debug("\tNO REPORTING OUTPUT AVAILABLE.");
1235
// this.debug("\tIMPLEMENT doh._report() IN YOUR TEST RUNNER");
1237
this.debug(this._line);
1238
this.debug("| TEST SUMMARY:");
1239
this.debug(this._line);
1240
this.debug("\t", this._testCount, "tests in", this._groupCount, "groups");
1241
this.debug("\t", this._errorCount, "errors");
1242
this.debug("\t", this._failureCount, "failures");
1245
doh.togglePaused = function(){
1246
this[(this._paused) ? "run" : "pause"]();
1249
doh.pause = function(){
1251
// halt test run. Can be resumed.
1252
this._paused = true;
1255
doh.run = function(){
1257
// begins or resumes the test process.
1258
// this.debug("STARTING");
1259
this._paused = false;
1260
var cg = this._currentGroup;
1261
var ct = this._currentTest;
1264
this._init(); // we weren't paused
1267
this._currentGroup = null;
1268
this._currentTest = null;
1270
for(var x in this._groups){
1272
( (!found)&&(x == cg) )||( found )
1274
if(this._paused){ return; }
1275
this._currentGroup = x;
1278
this.runGroup(x, ct);
1282
if(this._paused){ return; }
1285
this._currentGroup = null;
1286
this._currentTest = null;
1287
this._paused = false;
1292
//Statistics functions to handle computing performance metrics.
1293
//Taken from dojox.math
1295
doh.standardDeviation = function(/* Number[] */a){
1297
// Returns the standard deviation of the passed arguments.
1298
return Math.sqrt(this.variance(a)); // Number
1301
doh.variance = function(/* Number[] */a){
1303
// Find the variance in the passed array of numbers.
1304
var mean=0, squares=0;
1305
dojo.forEach(a, function(item){
1307
squares+=Math.pow(item,2);
1309
return (squares/a.length)-Math.pow(mean/a.length, 2); // Number
1312
doh.mean = function(/* Number[] */a){
1314
// Returns the mean value in the passed array.
1316
dojo.forEach(a, function(v){
1319
return t / Math.max(a.length, 1); // Number
1322
doh.min = function(/* Number[] */a){
1324
// Returns the min value in the passed array.
1325
return Math.min.apply(null, a); // Number
1328
doh.max = function(/* Number[] */a){
1330
// Returns the max value in the passed array.
1331
return Math.max.apply(null, a); // Number
1334
doh.median= function(/* Number[] */a){
1336
// Returns the value closest to the middle from a sorted version of the passed array.
1337
return a.slice(0).sort()[Math.ceil(a.length/2)-1]; // Number
1340
doh.mode = function(/* Number[] */a){
1342
// Returns the mode from the passed array (number that appears the most often).
1343
// This is not the most efficient method, since it requires a double scan, but
1344
// is ensures accuracy.
1345
var o = {}, r = 0, m = Number.MIN_VALUE;
1346
dojo.forEach(a, function(v){
1347
(o[v]!==undefined)?o[v]++:o[v]=1;
1350
// we did the lookup map because we need the number that appears the most.
1359
doh.average = function(/* Number [] */ a){
1362
for(i = 0; i < a.length; i++){
1370
if (typeof skipDohSetup === "undefined") {
1376
if(typeof dojo != "undefined"){
1377
dojo.platformRequire({
1378
browser: ["doh._browserRunner"],
1379
rhino: ["doh._rhinoRunner"],
1380
spidermonkey: ["doh._rhinoRunner"]
1383
var _shouldRequire = dojo.isBrowser ? (dojo.global == dojo.global["parent"] || !Boolean(dojo.global.parent.doh) ) : true;
1385
//can't access dojo.global.parent.doh, then we need to do require
1386
_shouldRequire = true;
1390
dojo.addOnLoad(function(){
1391
if (dojo.global.registerModulePath){
1392
dojo.forEach(dojo.global.registerModulePath, function(m){
1393
dojo.registerModulePath(m[0], m[1]);
1396
if(dojo.byId("testList")){
1397
var _tm = ( (dojo.global.testModule && dojo.global.testModule.length) ? dojo.global.testModule : "dojo.tests.module");
1398
dojo.forEach(_tm.split(","), dojo.require, dojo);
1399
doh.setTimeout(function(){
1405
// dojo.require("doh._base");
1409
if(typeof load == "function" &&
1410
(typeof Packages == "function" || typeof Packages == "object")){
1412
}else if(typeof load == "function"){
1416
if(this["document"]){
1418
// if we survived all of that, we're probably in a browser but
1419
// don't have Dojo handy. Load _browserRunner.js using a
1420
// document.write() call.
1422
// find runner.js, load _browserRunner relative to it
1423
var scripts = document.getElementsByTagName("script"), runnerFile;
1424
for(x=0; x<scripts.length; x++){
1425
var s = scripts[x].src;
1427
if(!runnerFile && s.substr(s.length - 9) == "runner.js"){
1429
}else if(s.substr(s.length - 17) == "_browserRunner.js"){
1436
document.write("<scri"+"pt src='" + runnerFile.substr(0, runnerFile.length - 9)
1437
+ "_browserRunner.js' type='text/javascript'></scr"+"ipt>");
1443
print("\n"+doh._line);
1444
print("The Dojo Unit Test Harness, $Rev: 20389 $");
1445
print("Copyright (c) 2009, The Dojo Foundation, All Rights Reserved");
1446
print(doh._line, "\n");
1449
var dojoUrl = "../../dojo/dojo.js";
1451
var testModule = "dojo.tests.module";
1453
for(x=0; x<arguments.length; x++){
1454
if(arguments[x].indexOf("=") > 0){
1455
var tp = arguments[x].split("=");
1456
if(tp[0] == "dohBase"){
1458
//Convert slashes to unix style and make sure properly
1460
dohBase = dohBase.replace(/\\/g, "/");
1461
if(dohBase.charAt(dohBase.length - 1) != "/"){
1465
if(tp[0] == "dojoUrl"){
1468
if(tp[0] == "testUrl"){
1471
if(tp[0] == "testModule"){
1477
load(dohBase + "_rhinoRunner.js");
1480
if(!this["djConfig"]){
1483
djConfig.baseUrl = dojoUrl.split("dojo.js")[0];
1489
if(testModule.length){
1490
dojo.forEach(testModule.split(","), dojo.require, dojo);
1493
print("An exception occurred: " + e);
1498
}).apply(this, typeof arguments != "undefined" ? arguments : [null]);