root / trunk / web / dojo / dojox / testing / DocTest.js @ 10
History | View | Annotate | Download (2.69 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.testing.DocTest"]){ |
||
9 | dojo._hasResource["dojox.testing.DocTest"]=true; |
||
10 | dojo.provide("dojox.testing.DocTest");
|
||
11 | dojo.require("dojo.string");
|
||
12 | dojo.declare("dojox.testing.DocTest",null,{errors:[],getTests:function(_1){ |
||
13 | var _2=dojo.moduleUrl(_1).path;
|
||
14 | var _3=_2.substring(0,_2.length-1)+".js"; |
||
15 | var _4=dojo.xhrGet({url:_3,handleAs:"text"}); |
||
16 | var _5=dojo._getText(_3);
|
||
17 | return this._getTestsFromString(_5,true); |
||
18 | },getTestsFromString:function(_6){ |
||
19 | return this._getTestsFromString(_6,false); |
||
20 | },_getTestsFromString:function(_7,_8){ |
||
21 | var _9=dojo.hitch(dojo.string,"trim"); |
||
22 | var _a=_7.split("\n"); |
||
23 | var _b=_a.length;
|
||
24 | var _c=[];
|
||
25 | var _d={commands:[],expectedResult:[],line:null}; |
||
26 | for(var i=0;i<_b+1;i++){ |
||
27 | var l=_9(_a[i]||""); |
||
28 | if((_8&&l.match(/^\/\/\s+>>>\s.*/))||l.match(/^\s*>>>\s.*/)){ |
||
29 | if(!_d.line){
|
||
30 | _d.line=i+1;
|
||
31 | } |
||
32 | if(_d.expectedResult.length>0){ |
||
33 | _c.push({commands:_d.commands,expectedResult:_d.expectedResult.join("\n"),line:_d.line}); |
||
34 | _d={commands:[],expectedResult:[],line:i+1}; |
||
35 | } |
||
36 | l=_8?_9(l).substring(2,l.length):l;
|
||
37 | l=_9(l).substring(3,l.length);
|
||
38 | _d.commands.push(_9(l)); |
||
39 | }else{
|
||
40 | if((!_8||l.match(/^\/\/\s+.*/))&&_d.commands.length&&_d.expectedResult.length==0){ |
||
41 | l=_8?_9(l).substring(3,l.length):l;
|
||
42 | _d.expectedResult.push(_9(l)); |
||
43 | }else{
|
||
44 | if(_d.commands.length>0&&_d.expectedResult.length){ |
||
45 | if(!_8||l.match(/^\/\/\s*$/)){ |
||
46 | _c.push({commands:_d.commands,expectedResult:_d.expectedResult.join("\n"),line:_d.line}); |
||
47 | } |
||
48 | if(_8&&!l.match(/^\/\//)){ |
||
49 | _c.push({commands:_d.commands,expectedResult:_d.expectedResult.join("\n"),line:_d.line}); |
||
50 | } |
||
51 | _d={commands:[],expectedResult:[],line:0}; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | return _c;
|
||
57 | },run:function(_e){ |
||
58 | this.errors=[];
|
||
59 | var _f=this.getTests(_e); |
||
60 | if(_f){
|
||
61 | this._run(_f);
|
||
62 | } |
||
63 | },_run:function(_10){ |
||
64 | var len=_10.length;
|
||
65 | this.tests=len;
|
||
66 | var oks=0; |
||
67 | for(var i=0;i<len;i++){ |
||
68 | var t=_10[i];
|
||
69 | var res=this.runTest(t.commands,t.expectedResult); |
||
70 | var msg="Test "+(i+1)+": "; |
||
71 | var _11=t.commands.join(" "); |
||
72 | _11=(_11.length>50?_11.substr(0,50)+"...":_11); |
||
73 | if(res.success){
|
||
74 | oks+=1;
|
||
75 | }else{
|
||
76 | this.errors.push({commands:t.commands,actual:res.actualResult,expected:t.expectedResult}); |
||
77 | console.error(msg+"Failed: "+_11,{commands:t.commands,actualResult:res.actualResult,expectedResult:t.expectedResult}); |
||
78 | } |
||
79 | } |
||
80 | },runTest:function(_12,_13){ |
||
81 | var ret={success:false,actualResult:null}; |
||
82 | var _14=_12.join("\n"); |
||
83 | ret.actualResult=eval(_14); |
||
84 | if((String(ret.actualResult)==_13)||(dojo.toJson(ret.actualResult)==_13)||((_13.charAt(0)=="\"")&&(_13.charAt(_13.length-1)=="\"")&&(String(ret.actualResult)==_13.substring(1,_13.length-1)))){ |
||
85 | ret.success=true;
|
||
86 | } |
||
87 | return ret;
|
||
88 | }}); |
||
89 | } |