[483] | 1 | dojo.provide("dojox.widget.DocTester"); |
---|
| 2 | |
---|
| 3 | dojo.require("dojo.string"); |
---|
| 4 | dojo.require("dijit._Widget"); |
---|
| 5 | dojo.require("dijit._Templated"); |
---|
| 6 | dojo.require("dojox.form.BusyButton"); |
---|
| 7 | dojo.require("dojox.testing.DocTest"); |
---|
| 8 | |
---|
| 9 | dojo.declare('dojox.widget.DocTester', |
---|
| 10 | [dijit._Widget, dijit._Templated], |
---|
| 11 | { |
---|
| 12 | // summary: |
---|
| 13 | // A widget to run DocTests inside an HTML page. |
---|
| 14 | |
---|
| 15 | templateString: dojo.cache('dojox.widget','DocTester/DocTester.html'), |
---|
| 16 | widgetsInTemplate: true, |
---|
| 17 | |
---|
| 18 | _fillContent:function(/*DomNode*/source){ |
---|
| 19 | // summary: |
---|
| 20 | // Overridden from _Templates.js, which actually just takes care of filling the containerNode. |
---|
| 21 | var src = source.innerHTML; |
---|
| 22 | this.doctests = new dojox.testing.DocTest(); |
---|
| 23 | this.tests = this.doctests.getTestsFromString(this._unescapeHtml(src)); |
---|
| 24 | var lineNumbers = dojo.map(this.tests, 'return item.line-1'); |
---|
| 25 | var lines = src.split("\n"); |
---|
| 26 | var actualResultHtml = '<div class="actualResult">FAILED, actual result was: <span class="result"></span></div>'; |
---|
| 27 | var content = '<pre class="testCase testNum0 odd">'; |
---|
| 28 | for (var i=0; i<lines.length; i++){ |
---|
| 29 | var index = dojo.indexOf(lineNumbers, i); |
---|
| 30 | if (index>0 && index!=-1){ |
---|
| 31 | var evenOdd = index%2 ? "even" : "odd"; |
---|
| 32 | content += actualResultHtml; |
---|
| 33 | content += '</pre><pre class="testCase testNum'+ index +' '+evenOdd+'">'; |
---|
| 34 | } |
---|
| 35 | content += lines[i].replace(/^\s+/, "")+"\n"; |
---|
| 36 | } |
---|
| 37 | content += actualResultHtml + '</pre>'; |
---|
| 38 | this.containerNode.innerHTML = content; |
---|
| 39 | }, |
---|
| 40 | |
---|
| 41 | postCreate:function(){ |
---|
| 42 | this.inherited("postCreate", arguments); |
---|
| 43 | dojo.connect(this.runButtonNode, "onClick", dojo.hitch(this, "runTests")); |
---|
| 44 | dojo.connect(this.resetButtonNode, "onClick", dojo.hitch(this, "reset")); |
---|
| 45 | this.numTestsNode.innerHTML = this.tests.length; |
---|
| 46 | }, |
---|
| 47 | |
---|
| 48 | runTests:function(){ |
---|
| 49 | var results = {ok:0, nok:0}; |
---|
| 50 | for (var i=0; i<this.tests.length; i++){ |
---|
| 51 | var ret = this.doctests.runTest(this.tests[i].commands, this.tests[i].expectedResult); |
---|
| 52 | dojo.query(".testNum"+i, this.domNode).addClass(ret.success ? "resultOk" : "resultNok"); |
---|
| 53 | if (!ret.success){ |
---|
| 54 | results.nok++; |
---|
| 55 | this.numTestsNokNode.innerHTML = results.nok; |
---|
| 56 | var act = dojo.query(".testNum"+i+" .actualResult", this.domNode)[0]; |
---|
| 57 | dojo.style(act, "display", "inline"); |
---|
| 58 | dojo.query(".result", act)[0].innerHTML = dojo.toJson(ret.actualResult); |
---|
| 59 | } else { |
---|
| 60 | results.ok++; |
---|
| 61 | this.numTestsOkNode.innerHTML = results.ok; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | this.runButtonNode.cancel(); |
---|
| 65 | dojo.style(this.runButtonNode.domNode, "display", "none"); |
---|
| 66 | dojo.style(this.resetButtonNode.domNode, "display", ""); |
---|
| 67 | }, |
---|
| 68 | |
---|
| 69 | reset:function(){ |
---|
| 70 | // summary: |
---|
| 71 | // Reset the DocTester visuals and enable the "Run tests" button again. |
---|
| 72 | dojo.style(this.runButtonNode.domNode, "display", ""); |
---|
| 73 | dojo.style(this.resetButtonNode.domNode, "display", "none"); |
---|
| 74 | this.numTestsOkNode.innerHTML = "0"; |
---|
| 75 | this.numTestsNokNode.innerHTML = "0"; |
---|
| 76 | dojo.query(".actualResult", this.domNode).style("display", "none"); |
---|
| 77 | dojo.query(".testCase", this.domNode).removeClass("resultOk").removeClass("resultNok"); |
---|
| 78 | }, |
---|
| 79 | |
---|
| 80 | _unescapeHtml:function(/* String */ str){ |
---|
| 81 | // summary: |
---|
| 82 | // Adds escape sequences for special characters in XML: &<>"' |
---|
| 83 | str = String(str).replace(/&/gm, "&").replace(/</gm, "<") |
---|
| 84 | .replace(/>/gm, ">").replace(/"/gm, '"'); |
---|
| 85 | // TODO Should become dojo.html.unentities() or so, when exists use instead |
---|
| 86 | return str; // String |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | ); |
---|