Changeset 18
- Timestamp:
- 11/15/05 17:01:40 (3 years ago)
- Files:
-
- jsolait/trunk/jsolait/jsolait.js (modified) (12 diffs)
- jsolait/trunk/jsolait/lib/testing.js (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
jsolait/trunk/jsolait/jsolait.js
r17 r18 23 23 It provides the core functionalities for creating classes, modules and for importing modules. 24 24 25 @author Jan-Klaas Kollhof 25 26 @version 2.0 26 $LastChangedBy$27 $Date$28 $Revision$27 @lastchangedby $LastChangedBy$ 28 @lastchangeddate $Date$ 29 @revision $Revision$ 29 30 **/ 30 31 … … 46 47 args[i] = arguments[i]; 47 48 } 48 //last arg should be a classScope 49 49 50 classScope = args.pop(); 50 51 51 //first arg is the class' name if it is a string52 52 if((args.length>0) && (typeof args[0] =='string')){ 53 53 name=args.shift(); … … 55 55 name="anonymous"; 56 56 } 57 //the rest of the arguments should be base classses57 58 58 var bases = args; 59 59 60 60 //set up the 'public static' fields of the class 61 var statc={__isArray__ : false,61 var __class__={__isArray__ : false, 62 62 __name__ : name, 63 63 __bases__: bases, 64 __hashCount__:0, 64 __id__: Class.__idcount__++, 65 __hash__: function(){ 66 return this.__id__; 67 }, 65 68 __str__ : function(){ 66 69 return "[class %s]".format(this.__name__); 67 70 } 68 71 }; 69 72 70 73 var baseProtos=[];//stores the prototypes of all the base classes 71 74 var proto; //the prototype to use for the new class … … 78 81 //this makes prototype creatin a bit simpler 79 82 proto.toString=proto.__str__; 80 statc.__bases__=[Object];83 __class__.__bases__=[Object]; 81 84 }else{ //inherit from all base classes 82 85 //inheritance is done by … … 87 90 baseProtos.push(baseClass.prototype); 88 91 if(baseClass.__proto__ !== undefined){ 89 baseProto = baseClass.__proto__( );92 baseProto = baseClass.__proto__(bases); 90 93 }else{ 91 94 baseProto = new baseClass(Class); 92 95 } 93 statc.__isArray__ = statc.__isArray__ || baseClass.__isArray__;96 __class__.__isArray__ = __class__.__isArray__ || baseClass.__isArray__; 94 97 95 98 if(i==0){//for the first base class just use it's proto as the final proto … … 104 107 //extend the new class' static interface 105 108 for(var key in baseClass){ 106 if( statc[key] === undefined){107 statc[key] = baseClass[key];109 if(__class__[key] === undefined){ 110 __class__[key] = baseClass[key]; 108 111 } 109 112 } … … 117 120 proto.__hash__=function(){ 118 121 if(this.__id__ === undefined){ 119 this.__id__ = Class.__ hashCount__++;122 this.__id__ = Class.__idcount__++; 120 123 } 121 124 return this.__id__; 122 125 }; 123 126 } 124 125 //todo: run the class scope as scope(publ, [statc,] baseClassProto1, ... ) 126 //publ represents the new class' prototype, statc the public static fields of the class(class properties) 127 if(classScope.length>baseProtos.length+1){ 128 classScope.apply(this,[proto,statc].concat(baseProtos)); 127 proto.__class__=__class__; 128 129 var privId = '__priv__' + __class__.__id__; 130 131 //run teh class setup function provided as classScope 132 if(classScope.length-1 > baseProtos.length){ 133 classScope.apply(this,[proto, privId].concat(baseProtos)); 129 134 }else{ 130 135 classScope.apply(this,[proto].concat(baseProtos)); … … 142 147 return rslt.__call__.apply(rslt, arguments); 143 148 }; 149 150 var privId='__priv__' + arguments.callee.__id__; 151 rslt[privId]={}; 152 144 153 var proto=arguments.callee.prototype; 145 154 for(var n in proto){ … … 154 163 } 155 164 }; 156 }else if( statc.__isArray__){165 }else if(__class__.__isArray__){ 157 166 //Since we cannot inherit from Array directly we take the same approach as with the callable above 158 167 //and just have a constructor which creates an Array … … 160 169 if(calledBy !== Class){ 161 170 rslt=[]; 171 172 var privId='__priv__' + arguments.callee.__id__; 173 rslt[privId]={}; 174 162 175 var proto=arguments.callee.prototype; 163 176 for(var n in proto){ … … 186 199 if(calledBy !== Class){ 187 200 if(this.__init__){ 201 var privId='__priv__' + arguments.callee.__id__; 202 this[privId] = {}; 188 203 this.__init__.apply(this, arguments); 189 204 } … … 200 215 201 216 //apply all the static fileds 202 for(var key in statc){203 NewClass[key] = statc[key];217 for(var key in __class__){ 218 NewClass[key] = __class__[key]; 204 219 } 205 NewClass.toString= statc.__str__;220 NewClass.toString=__class__.__str__; 206 221 207 222 return NewClass; 208 223 }; 209 Class.__hashCount__=0; 210 224 Class.__idcount__=0; 211 225 Class.toString = function(){ 212 226 return "[object Class]"; jsolait/trunk/jsolait/lib/testing.js
r9 r18 28 28 29 29 Module("testing", "$Revision$", function(mod){ 30 30 31 mod.minProfileTime=500; 31 32 /** 32 33 Returns the average time used for executing a function. … … 47 48 }; 48 49 50 mod.profile=function(min,fn){ 51 if(arguments.length==1){ 52 fn=min; 53 min=mod.minProfileTime; 54 } 55 var args = []; 56 for(var i=2;i<arguments.length;i++){ 57 args.push(arguments[i]); 58 } 59 var cnt=0; 60 var t1=(new Date()).getTime(); 61 t2=t1; 62 while(t2-t1<min){ 63 cnt++; 64 fn.apply(null, args); 65 t2=(new Date()).getTime(); 66 } 67 return (t2-t1) / cnt; 68 }; 69 70 49 71 mod.Test=Class(function(publ,supr){ 50 72 publ.__init__=function(testScope){ … … 53 75 54 76 publ.run=function(){ 77 this.failed=false; 78 this.error=null; 55 79 this.startTime=(new Date()).getTime(); 56 80 try{ … … 59 83 if(e instanceof mod.AssertFailed){ 60 84 this.error = e; 85 this.failed=true; 61 86 }else{ 62 87 throw new mod.Exception("Failed to run test.", e); … … 74 99 } 75 100 }; 76 101 publ.failed=false; 102 publ.error; 77 103 publ.startTime; 78 104 publ.endTime; … … 196 222 }; 197 223 198 mod.fail=function( ){199 224 mod.fail=function(comment){ 225 throw new mod.AssertFailed(comment, "Fail was called"); 200 226 }; 201 227
