📄 runner.js
字号:
// FIXME: need to add async tests// FIXME: need to handle URL wrapping and test registration/running from URLs// package system gunk. try{ dojo.provide("doh.runner");}catch(e){ if(!this["doh"]){ doh = {}; }}//// Utility Functions and Classes//doh.selfTest = false;doh.hitch = function(/*Object*/thisObject, /*Function|String*/method /*, ...*/){ var args = []; for(var x=2; x<arguments.length; x++){ args.push(arguments[x]); } var fcn = ((typeof method == "string") ? thisObject[method] : method) || function(){}; return function(){ var ta = args.concat([]); // make a copy for(var x=0; x<arguments.length; x++){ ta.push(arguments[x]); } return fcn.apply(thisObject, ta); // Function };}doh._mixin = function(/*Object*/ obj, /*Object*/ props){ // summary: // Adds all properties and methods of props to obj. This addition is // "prototype extension safe", so that instances of objects will not // pass along prototype defaults. var tobj = {}; for(var x in props){ // the "tobj" condition avoid copying properties in "props" // inherited from Object.prototype. For example, if obj has a custom // toString() method, don't overwrite it with the toString() method // that props inherited from Object.protoype if((typeof tobj[x] == "undefined") || (tobj[x] != props[x])){ obj[x] = props[x]; } } // IE doesn't recognize custom toStrings in for..in if( this["document"] && document.all && (typeof props["toString"] == "function") && (props["toString"] != obj["toString"]) && (props["toString"] != tobj["toString"]) ){ obj.toString = props.toString; } return obj; // Object}doh.mixin = function(/*Object*/obj, /*Object...*/props){ // summary: Adds all properties and methods of props to obj. for(var i=1, l=arguments.length; i<l; i++){ doh._mixin(obj, arguments[i]); } return obj; // Object}doh.extend = function(/*Object*/ constructor, /*Object...*/ props){ // summary: // Adds all properties and methods of props to constructor's // prototype, making them available to all instances created with // constructor. for(var i=1, l=arguments.length; i<l; i++){ doh._mixin(constructor.prototype, arguments[i]); } return constructor; // Object}doh._line = "------------------------------------------------------------";/*doh._delegate = function(obj, props){ // boodman-crockford delegation function TMP(){}; TMP.prototype = obj; var tmp = new TMP(); if(props){ dojo.lang.mixin(tmp, props); } return tmp;}*/doh.debug = function(){ // summary: // takes any number of arguments and sends them to whatever debugging // or logging facility is available in this environment // YOUR TEST RUNNER NEEDS TO IMPLEMENT THIS}doh._AssertFailure = function(msg){ // idea for this as way of dis-ambiguating error types is from JUM. // The JUM is dead! Long live the JUM! if(!(this instanceof doh._AssertFailure)){ return new doh._AssertFailure(msg); } this.message = new String(msg||""); return this;}doh._AssertFailure.prototype = new Error();doh._AssertFailure.prototype.constructor = doh._AssertFailure;doh._AssertFailure.prototype.name = "doh._AssertFailure";doh.Deferred = function(canceller){ this.chain = []; this.id = this._nextId(); this.fired = -1; this.paused = 0; this.results = [null, null]; this.canceller = canceller; this.silentlyCancelled = false;};doh.extend(doh.Deferred, { getTestCallback: function(cb, scope){ var _this = this; return function(){ try{ cb.apply(scope||dojo.global||_this, arguments); }catch(e){ _this.errback(e); return; } _this.callback(true); } }, getFunctionFromArgs: function(){ var a = arguments; if((a[0])&&(!a[1])){ if(typeof a[0] == "function"){ return a[0]; }else if(typeof a[0] == "string"){ return dojo.global[a[0]]; } }else if((a[0])&&(a[1])){ return doh.hitch(a[0], a[1]); } return null; }, makeCalled: function() { var deferred = new doh.Deferred(); deferred.callback(); return deferred; }, _nextId: (function(){ var n = 1; return function(){ return n++; }; })(), cancel: function(){ if(this.fired == -1){ if (this.canceller){ this.canceller(this); }else{ this.silentlyCancelled = true; } if(this.fired == -1){ this.errback(new Error("Deferred(unfired)")); } }else if( (this.fired == 0)&& (this.results[0] instanceof doh.Deferred)){ this.results[0].cancel(); } }, _pause: function(){ this.paused++; }, _unpause: function(){ this.paused--; if ((this.paused == 0) && (this.fired >= 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){ // 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 if(tests){ this.register(group, tests); } if(setUp){ this._groups[group].setUp = setUp; } if(tearDown){ this._groups[group].tearDown = tearDown; }}doh._getTestObj = function(group, test){ 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", "_") }; 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 } return tObj;}doh.registerTest = function(/*String*/ group, /*Function||Object*/ test){ // 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. if(!this._groups[group]){ this._groupCount++; this._groups[group] = []; this._groups[group].inFlight = 0; } var tObj = this._getTestObj(group, test); if(!tObj){ return; } this._groups[group].push(tObj); this._testCount++; this._testRegistered(group, tObj); return tObj;}doh.registerTests = function(/*String*/ group, /*Array*/ testArr){ // summary: // registers a group of tests, treating each element of testArr as // though it were being (along with group) passed to the registerTest // method. for(var x=0; x<testArr.length; x++){ this.registerTest(group, testArr[x]); }}// FIXME: move implementation to _browserRunner?doh.registerUrl = function( /*String*/ group, /*String*/ url, /*Integer*/ timeout){ this.debug("ERROR:"); this.debug("\tNO registerUrl() METHOD AVAILABLE."); // this._urls.push(url);}doh.registerString = function(group, str){}// FIXME: remove the doh.add alias SRTL.doh.register = doh.add = function(groupOrNs, testOrNull){ // summary: // "magical" variant of registerTests, registerTest, and // registerTestNs. Will accept the calling arguments of any of these // methods and will correctly guess the right one to register with. if( (arguments.length == 1)&& (typeof groupOrNs == "string") ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -