From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/util/doh/LICENSE | 195 +++++++ includes/js/util/doh/README | 12 + includes/js/util/doh/_browserRunner.js | 465 ++++++++++++++++ includes/js/util/doh/_rhinoRunner.js | 17 + includes/js/util/doh/_sounds/LICENSE | 10 + includes/js/util/doh/_sounds/doh.wav | Bin 0 -> 2878 bytes includes/js/util/doh/_sounds/dohaaa.wav | Bin 0 -> 14318 bytes includes/js/util/doh/_sounds/woohoo.wav | Bin 0 -> 3814 bytes includes/js/util/doh/runner.html | 283 ++++++++++ includes/js/util/doh/runner.js | 948 ++++++++++++++++++++++++++++++++ includes/js/util/doh/runner.sh | 3 + includes/js/util/doh/small_logo.png | Bin 0 -> 1893 bytes 12 files changed, 1933 insertions(+) create mode 100644 includes/js/util/doh/LICENSE create mode 100644 includes/js/util/doh/README create mode 100644 includes/js/util/doh/_browserRunner.js create mode 100644 includes/js/util/doh/_rhinoRunner.js create mode 100644 includes/js/util/doh/_sounds/LICENSE create mode 100644 includes/js/util/doh/_sounds/doh.wav create mode 100644 includes/js/util/doh/_sounds/dohaaa.wav create mode 100644 includes/js/util/doh/_sounds/woohoo.wav create mode 100644 includes/js/util/doh/runner.html create mode 100644 includes/js/util/doh/runner.js create mode 100644 includes/js/util/doh/runner.sh create mode 100644 includes/js/util/doh/small_logo.png (limited to 'includes/js/util') diff --git a/includes/js/util/doh/LICENSE b/includes/js/util/doh/LICENSE new file mode 100644 index 0000000..3fa2720 --- /dev/null +++ b/includes/js/util/doh/LICENSE @@ -0,0 +1,195 @@ +Dojo is available under *either* the terms of the modified BSD license *or* the +Academic Free License version 2.1. As a recipient of Dojo, you may choose which +license to receive this code under (except as noted in per-module LICENSE +files). Some modules may not be the copyright of the Dojo Foundation. These +modules contain explicit declarations of copyright in both the LICENSE files in +the directories in which they reside and in the code itself. No external +contributions are allowed under licenses which are fundamentally incompatible +with the AFL or BSD licenses that Dojo is distributed under. + +The text of the AFL and BSD licenses is reproduced below. + +------------------------------------------------------------------------------- +The "New" BSD License: +********************** + +Copyright (c) 2005-2008, The Dojo Foundation +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the Dojo Foundation nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +------------------------------------------------------------------------------- +The Academic Free License, v. 2.1: +********************************** + +This Academic Free License (the "License") applies to any original work of +authorship (the "Original Work") whose owner (the "Licensor") has placed the +following notice immediately following the copyright notice for the Original +Work: + +Licensed under the Academic Free License version 2.1 + +1) Grant of Copyright License. Licensor hereby grants You a world-wide, +royalty-free, non-exclusive, perpetual, sublicenseable license to do the +following: + +a) to reproduce the Original Work in copies; + +b) to prepare derivative works ("Derivative Works") based upon the Original +Work; + +c) to distribute copies of the Original Work and Derivative Works to the +public; + +d) to perform the Original Work publicly; and + +e) to display the Original Work publicly. + +2) Grant of Patent License. Licensor hereby grants You a world-wide, +royalty-free, non-exclusive, perpetual, sublicenseable license, under patent +claims owned or controlled by the Licensor that are embodied in the Original +Work as furnished by the Licensor, to make, use, sell and offer for sale the +Original Work and Derivative Works. + +3) Grant of Source Code License. The term "Source Code" means the preferred +form of the Original Work for making modifications to it and all available +documentation describing how to modify the Original Work. Licensor hereby +agrees to provide a machine-readable copy of the Source Code of the Original +Work along with each copy of the Original Work that Licensor distributes. +Licensor reserves the right to satisfy this obligation by placing a +machine-readable copy of the Source Code in an information repository +reasonably calculated to permit inexpensive and convenient access by You for as +long as Licensor continues to distribute the Original Work, and by publishing +the address of that information repository in a notice immediately following +the copyright notice that applies to the Original Work. + +4) Exclusions From License Grant. Neither the names of Licensor, nor the names +of any contributors to the Original Work, nor any of their trademarks or +service marks, may be used to endorse or promote products derived from this +Original Work without express prior written permission of the Licensor. Nothing +in this License shall be deemed to grant any rights to trademarks, copyrights, +patents, trade secrets or any other intellectual property of Licensor except as +expressly stated herein. No patent license is granted to make, use, sell or +offer to sell embodiments of any patent claims other than the licensed claims +defined in Section 2. No right is granted to the trademarks of Licensor even if +such marks are included in the Original Work. Nothing in this License shall be +interpreted to prohibit Licensor from licensing under different terms from this +License any Original Work that Licensor otherwise would have a right to +license. + +5) This section intentionally omitted. + +6) Attribution Rights. You must retain, in the Source Code of any Derivative +Works that You create, all copyright, patent or trademark notices from the +Source Code of the Original Work, as well as any notices of licensing and any +descriptive text identified therein as an "Attribution Notice." You must cause +the Source Code for any Derivative Works that You create to carry a prominent +Attribution Notice reasonably calculated to inform recipients that You have +modified the Original Work. + +7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that +the copyright in and to the Original Work and the patent rights granted herein +by Licensor are owned by the Licensor or are sublicensed to You under the terms +of this License with the permission of the contributor(s) of those copyrights +and patent rights. Except as expressly stated in the immediately proceeding +sentence, the Original Work is provided under this License on an "AS IS" BASIS +and WITHOUT WARRANTY, either express or implied, including, without limitation, +the warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. +This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No +license to Original Work is granted hereunder except under this disclaimer. + +8) Limitation of Liability. Under no circumstances and under no legal theory, +whether in tort (including negligence), contract, or otherwise, shall the +Licensor be liable to any person for any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License +or the use of the Original Work including, without limitation, damages for loss +of goodwill, work stoppage, computer failure or malfunction, or any and all +other commercial damages or losses. This limitation of liability shall not +apply to liability for death or personal injury resulting from Licensor's +negligence to the extent applicable law prohibits such limitation. Some +jurisdictions do not allow the exclusion or limitation of incidental or +consequential damages, so this exclusion and limitation may not apply to You. + +9) Acceptance and Termination. If You distribute copies of the Original Work or +a Derivative Work, You must make a reasonable effort under the circumstances to +obtain the express assent of recipients to the terms of this License. Nothing +else but this License (or another written agreement between Licensor and You) +grants You permission to create Derivative Works based upon the Original Work +or to exercise any of the rights granted in Section 1 herein, and any attempt +to do so except under the terms of this License (or another written agreement +between Licensor and You) is expressly prohibited by U.S. copyright law, the +equivalent laws of other countries, and by international treaty. Therefore, by +exercising any of the rights granted to You in Section 1 herein, You indicate +Your acceptance of this License and all of its terms and conditions. + +10) Termination for Patent Action. This License shall terminate automatically +and You may no longer exercise any of the rights granted to You by this License +as of the date You commence an action, including a cross-claim or counterclaim, +against Licensor or any licensee alleging that the Original Work infringes a +patent. This termination provision shall not apply for an action alleging +patent infringement by combinations of the Original Work with other software or +hardware. + +11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this +License may be brought only in the courts of a jurisdiction wherein the +Licensor resides or in which Licensor conducts its primary business, and under +the laws of that jurisdiction excluding its conflict-of-law provisions. The +application of the United Nations Convention on Contracts for the International +Sale of Goods is expressly excluded. Any use of the Original Work outside the +scope of this License or after its termination shall be subject to the +requirements and penalties of the U.S. Copyright Act, 17 U.S.C. § 101 et +seq., the equivalent laws of other countries, and international treaty. This +section shall survive the termination of this License. + +12) Attorneys Fees. In any action to enforce the terms of this License or +seeking damages relating thereto, the prevailing party shall be entitled to +recover its costs and expenses, including, without limitation, reasonable +attorneys' fees and costs incurred in connection with such action, including +any appeal of such action. This section shall survive the termination of this +License. + +13) Miscellaneous. This License represents the complete agreement concerning +the subject matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent necessary to +make it enforceable. + +14) Definition of "You" in This License. "You" throughout this License, whether +in upper or lower case, means an individual or a legal entity exercising rights +under, and complying with all of the terms of, this License. For legal +entities, "You" includes any entity that controls, is controlled by, or is +under common control with you. For purposes of this definition, "control" means +(i) the power, direct or indirect, to cause the direction or management of such +entity, whether by contract or otherwise, or (ii) ownership of fifty percent +(50%) or more of the outstanding shares, or (iii) beneficial ownership of such +entity. + +15) Right to Use. You may use the Original Work in all ways not otherwise +restricted or conditioned by this License or by law, and Licensor promises not +to interfere with or be responsible for such uses by You. + +This license is Copyright (C) 2003-2004 Lawrence E. Rosen. All rights reserved. +Permission is hereby granted to copy and distribute this license without +modification. This license may not be modified without the express written +permission of its copyright owner. diff --git a/includes/js/util/doh/README b/includes/js/util/doh/README new file mode 100644 index 0000000..c4439dd --- /dev/null +++ b/includes/js/util/doh/README @@ -0,0 +1,12 @@ +DOH may be run standalone by issuing a command like the following: + +java -jar ../shrinksafe/custom_rhino.jar runner.js testModule=tests.colors + +where the testModule argument is optional and custom_rhino.jar is just a +convenient copy of the Rhino JavaScript engine -- the custom patch is not +required. + +Optional arguments include: + * dojoUrl - specifies the location of dojo.js + * testUrl - specifies a Javascript file to load with initialization code + * testModule - specifies a test module in the dojo package namespace diff --git a/includes/js/util/doh/_browserRunner.js b/includes/js/util/doh/_browserRunner.js new file mode 100644 index 0000000..9e9e3f3 --- /dev/null +++ b/includes/js/util/doh/_browserRunner.js @@ -0,0 +1,465 @@ +if(window["dojo"]){ + dojo.provide("doh._browserRunner"); +} + +// FIXME: need to add prompting for monkey-do testing +// FIXME: need to implement progress bar +// FIXME: need to implement errors in progress bar + +(function(){ + if(window.parent == window){ + // we're the top-dog window. + + // borrowed from Dojo, etc. + var byId = function(id){ + return document.getElementById(id); + } + + var _addOnEvt = function( type, // string + refOrName, // function or string + scope){ // object, defaults is window + + if(!scope){ scope = window; } + + var funcRef = refOrName; + if(typeof refOrName == "string"){ + funcRef = scope[refOrName]; + } + var enclosedFunc = function(){ return funcRef.apply(scope, arguments); }; + + if((window["dojo"])&&(type == "load")){ + dojo.addOnLoad(enclosedFunc); + }else{ + if(window["attachEvent"]){ + window.attachEvent("on"+type, enclosedFunc); + }else if(window["addEventListener"]){ + window.addEventListener(type, enclosedFunc, false); + }else if(document["addEventListener"]){ + document.addEventListener(type, enclosedFunc, false); + } + } + }; + + // + // Over-ride or implement base runner.js-provided methods + // + var _logBacklog = []; + var sendToLogPane = function(args, skip){ + var msg = ""; + for(var x=0; x "); + if(!byId("logBody")){ + _logBacklog.push(msg); + return; + }else if((_logBacklog.length)&&(!skip)){ + var tm; + while(tm=_logBacklog.shift()){ + sendToLogPane(tm, true); + } + } + var tn = document.createElement("div"); + tn.innerHTML = msg; + byId("logBody").appendChild(tn); + } + + doh._init = (function(oi){ + return function(){ + var lb = byId("logBody"); + if(lb){ + // clear the console before each run + while(lb.firstChild){ + lb.removeChild(lb.firstChild); + } + } + oi.apply(doh, arguments); + } + })(doh._init); + + if(this["opera"] && opera.postError){ + doh.debug = function(){ + var msg = ""; + for(var x=0; x 700)){ + _nameTimes[name] = new Date(); + var tc = document.createElement("span"); + byId("hiddenAudio").appendChild(tc); + tc.innerHTML = ''; + } + } + } + + doh._testFinished = function(group, fixture, success){ + var fn = getFixtureNode(group, fixture); + if(fn){ + fn.getElementsByTagName("td")[3].innerHTML = (fixture.endTime-fixture.startTime)+"ms"; + fn.className = (success) ? "success" : "failure"; + + if(!success){ + _playSound("doh"); + var gn = getGroupNode(group); + if(gn){ + gn.className = "failure"; + _getGroupToggler(group)(null, true); + } + } + } + this.debug(((success) ? "PASSED" : "FAILED"), "test:", fixture.name); + } + + // FIXME: move implementation to _browserRunner? + doh.registerUrl = function( /*String*/ group, + /*String*/ url, + /*Integer*/ timeout){ + var tg = new String(group); + this.register(group, { + name: url, + setUp: function(){ + doh.currentGroupName = tg; + doh.currentGroup = this; + doh.currentUrl = url; + this.d = new doh.Deferred(); + doh.currentTestDeferred = this.d; + showTestPage(); + byId("testBody").src = url; + }, + timeout: timeout||10000, // 10s + // timeout: timeout||1000, // 10s + runTest: function(){ + // FIXME: implement calling into the url's groups here!! + return this.d; + }, + tearDown: function(){ + doh.currentGroupName = null; + doh.currentGroup = null; + doh.currentTestDeferred = null; + doh.currentUrl = null; + // this.d.errback(false); + // byId("testBody").src = "about:blank"; + showLogPage(); + } + }); + } + + // + // Utility code for runner.html + // + // var isSafari = navigator.appVersion.indexOf("Safari") >= 0; + var tabzidx = 1; + var _showTab = function(toShow, toHide){ + // FIXME: I don't like hiding things this way. + byId(toHide).style.display = "none"; + with(byId(toShow).style){ + display = ""; + zIndex = ++tabzidx; + } + } + + showTestPage = function(){ + _showTab("testBody", "logBody"); + } + + showLogPage = function(){ + _showTab("logBody", "testBody"); + } + + var runAll = true; + toggleRunAll = function(){ + // would be easier w/ query...sigh + runAll = (!runAll); + if(!byId("testList")){ return; } + var tb = byId("testList").tBodies[0]; + var inputs = tb.getElementsByTagName("input"); + var x=0; var tn; + while(tn=inputs[x++]){ + tn.checked = runAll; + doh._groups[tn.group].skip = (!runAll); + } + } + + var listHeightTimer = null; + var setListHeight = function(){ + if(listHeightTimer){ + clearTimeout(listHeightTimer); + } + var tl = byId("testList"); + if(!tl){ return; } + listHeightTimer = setTimeout(function(){ + tl.style.display = "none"; + tl.style.display = ""; + + }, 10); + } + + _addOnEvt("resize", setListHeight); + _addOnEvt("load", setListHeight); + _addOnEvt("load", function(){ + if(loaded){ return; } + loaded = true; + groupTemplate = byId("groupTemplate"); + if(!groupTemplate){ + // make sure we've got an ammenable DOM structure + return; + } + groupTemplate.parentNode.removeChild(groupTemplate); + groupTemplate.style.display = ""; + testTemplate = byId("testTemplate"); + testTemplate.parentNode.removeChild(testTemplate); + testTemplate.style.display = ""; + doh._updateTestList(); + }); + + _addOnEvt("load", + function(){ + doh._onEnd = function(){ + if(doh._failureCount == 0){ + doh.debug("WOOHOO!!"); + _playSound("woohoo"); + }else{ + console.debug("doh._failureCount:", doh._failureCount); + } + if(byId("play")){ + toggleRunning(); + } + } + if(!byId("play")){ + // make sure we've got an ammenable DOM structure + return; + } + var isRunning = false; + var toggleRunning = function(){ + // ugg, this would be so much better w/ dojo.query() + if(isRunning){ + byId("play").style.display = byId("pausedMsg").style.display = ""; + byId("playingMsg").style.display = byId("pause").style.display = "none"; + isRunning = false; + }else{ + byId("play").style.display = byId("pausedMsg").style.display = "none"; + byId("playingMsg").style.display = byId("pause").style.display = ""; + isRunning = true; + } + } + doh.run = (function(oldRun){ + return function(){ + if(!doh._currentGroup){ + toggleRunning(); + } + return oldRun.apply(doh, arguments); + } + })(doh.run); + var btns = byId("toggleButtons").getElementsByTagName("span"); + var node; var idx=0; + while(node=btns[idx++]){ + node.onclick = toggleRunning; + } + } + ); + }else{ + // we're in an iframe environment. Time to mix it up a bit. + + _doh = window.parent.doh; + var _thisGroup = _doh.currentGroupName; + var _thisUrl = _doh.currentUrl; + if(_thisGroup){ + doh._testRegistered = function(group, tObj){ + _doh._updateTestList(_thisGroup, tObj); + } + doh._onEnd = function(){ + _doh._errorCount += doh._errorCount; + _doh._failureCount += doh._failureCount; + _doh._testCount += doh._testCount; + // should we be really adding raw group counts? + _doh._groupCount += doh._groupCount; + _doh.currentTestDeferred.callback(true); + } + var otr = doh._getTestObj; + doh._getTestObj = function(){ + var tObj = otr.apply(doh, arguments); + tObj.name = _thisUrl+"::"+arguments[0]+"::"+tObj.name; + return tObj; + } + doh.debug = doh.hitch(_doh, "debug"); + doh.registerUrl = doh.hitch(_doh, "registerUrl"); + doh._testStarted = function(group, fixture){ + _doh._testStarted(_thisGroup, fixture); + } + doh._testFinished = function(g, f, s){ + _doh._testFinished(_thisGroup, f, s); + } + doh._report = function(){}; + } + } + +})(); diff --git a/includes/js/util/doh/_rhinoRunner.js b/includes/js/util/doh/_rhinoRunner.js new file mode 100644 index 0000000..ae47597 --- /dev/null +++ b/includes/js/util/doh/_rhinoRunner.js @@ -0,0 +1,17 @@ +if(this["dojo"]){ + dojo.provide("doh._rhinoRunner"); +} + +doh.debug = print; + +// Override the doh._report method to make it quit with an +// appropriate exit code in case of test failures. +(function(){ + var oldReport = doh._report; + doh._report = function(){ + oldReport.apply(doh, arguments); + if(this._failureCount > 0 || this._errorCount > 0){ + quit(1); + } + } +})(); diff --git a/includes/js/util/doh/_sounds/LICENSE b/includes/js/util/doh/_sounds/LICENSE new file mode 100644 index 0000000..e8e11d4 --- /dev/null +++ b/includes/js/util/doh/_sounds/LICENSE @@ -0,0 +1,10 @@ +License Disclaimer: + +All contents of this directory are Copyright (c) the Dojo Foundation, with the +following exceptions: +------------------------------------------------------------------------------- + +woohoo.wav, doh.wav, dohaaa.wav: + * Copyright original authors. + Copied from: + http://simpson-homer.com/homer-simpson-soundboard.html diff --git a/includes/js/util/doh/_sounds/doh.wav b/includes/js/util/doh/_sounds/doh.wav new file mode 100644 index 0000000..5e8a583 Binary files /dev/null and b/includes/js/util/doh/_sounds/doh.wav differ diff --git a/includes/js/util/doh/_sounds/dohaaa.wav b/includes/js/util/doh/_sounds/dohaaa.wav new file mode 100644 index 0000000..2220921 Binary files /dev/null and b/includes/js/util/doh/_sounds/dohaaa.wav differ diff --git a/includes/js/util/doh/_sounds/woohoo.wav b/includes/js/util/doh/_sounds/woohoo.wav new file mode 100644 index 0000000..eb69217 Binary files /dev/null and b/includes/js/util/doh/_sounds/woohoo.wav differ diff --git a/includes/js/util/doh/runner.html b/includes/js/util/doh/runner.html new file mode 100644 index 0000000..dbcd68c --- /dev/null +++ b/includes/js/util/doh/runner.html @@ -0,0 +1,283 @@ + + + + + The Dojo Unit Test Harness, $Rev$ + + + + + + + + + + + + + + + + + + +
+

D.O.H.: The Dojo Objective Harness

+ + + + + +
+ + + + + + Stopped + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + +
  + + testtime
+
+
+
+
+

+						
+ + +
+
+ + + + diff --git a/includes/js/util/doh/runner.js b/includes/js/util/doh/runner.js new file mode 100644 index 0000000..f5e47e1 --- /dev/null +++ b/includes/js/util/doh/runner.js @@ -0,0 +1,948 @@ +// 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= 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 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(); + } +} + +try{ + setTimeout(function(){}, 0); +}catch(e){ + setTimeout = function(func){ + return func(); + } +} + +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? + 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 = 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; + } + } + if(fixture["tearDown"]){ fixture.tearDown(this); } + }catch(e){ + threw = true; + err = e; + if(!fixture.endTime){ + fixture.endTime = new Date(); + } + } + var d = new doh.Deferred(); + 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){ + 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