Changeset 27
- Timestamp:
- 11/25/05 15:05:31 (3 years ago)
- Files:
-
- trunk/jsolait/jsolait.js (modified) (9 diffs)
- trunk/jsolait/lib/testing.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/jsolait/jsolait.js
r26 r27 153 153 rslt[n] = proto[n]; 154 154 } 155 rslt.constructor = arguments.callee;155 rslt.constructor = proto.__class__; 156 156 rslt.toString = proto.__str__; 157 157 if(rslt.__init__){ … … 175 175 rslt[n] = proto[n]; 176 176 } 177 rslt.constructor = proto ;177 rslt.constructor = proto.__class__; 178 178 rslt.toString = proto.__str__; 179 179 if(rslt.__init__){ … … 224 224 Class.__createProto__=function(){ throw "Can't use Class as a base class.";}; 225 225 226 Function.__createProto__ = function(){ throw "Cannot inherit from Function. implement the callable interface instead using YourClass::__call__.";}; 227 Array.__createProto__=function(){ var r =[]; r.__str__ = Array.prototype.toString; return r; }; 226 228 Array.__isArray__=true; 227 229 Array.__str__=Array.toString=function(){return "[class Array]";}; 228 Array.__createProto__=function(){ var r =[]; r.__str__ = Array.prototype.toString; return r; };229 230 Object.__str__=Object.toString=function(){return "[class Object]";}; 230 Function.__createProto__ = function(){ throw "Cannot inherit from Function. implement the callable interface instead using YourClass::__call__.";}; 231 Number.__str__ =Number.toString=function(){return "[class Number]";}; 232 String.__str__ =String.toString=function(){return "[class String]";}; 233 234 /** 235 Returns a string representation of an object. 236 The difference to 237 @param obj The object to return a string repr. of. 238 @return A string repr. the object. 239 **/ 240 str = function(obj){ 241 return "" + obj; 242 }; 243 244 /** 245 Returns if an object is an instance of a specified class or of a direct or indirect subclass thereof. 246 247 It also works for traditional javascript inheritance(i.e. SomeClass=function(){}; SomeClass.prototype=new SuperClass(); ...). 248 Internaly it first checks using instanceof if that fails it uses isinstance(obj.constructor, cls). 249 There are some differences between using isinstance and instanceof. 250 i.e. 251 (123 instanceof Number) == false; 252 isinstance(123, Number) == true; 253 ('abc' instanceof String) == false; 254 isinstance('abc', String) == true; 255 256 @param obj The object to test. 257 @param cls The class to test against. 258 @return True if the object is an instance of cls. False otherwise. 259 **/ 260 isinstance=function(obj, cls){ 261 if(obj instanceof cls){ 262 return true; 263 }else{ 264 return issubclass(obj.constructor, cls); 265 } 266 }; 267 268 /** 269 Returns is a cls is a direct or indirect subclass of another. 270 271 A class is always a subclass of itself and Object is the base for all classes. 272 A class is a subclass of baseclass if it's prototype is an instance of baseclass. 273 A class is a subclass of baseclass if any of it's __bases__ is a subclass of baseclass. 274 If there are no __bases__ defined there is no way to findout about inheritance besides 275 the prototype chain which was checked by instanceof before, so false is returned. 276 277 @param cls The class to test. 278 @param baseclass The assumed superclass. 279 @return True if cls is a subclass of baseclass otherwise false. 280 **/ 281 issubclass=function(cls, baseclass){ 282 if(baseclass === Object || cls===baseclass || (cls.prototype instanceof baseclass)){ 283 return true; 284 }else{ 285 var bases = cls.__bases__; 286 if(bases != null){ 287 for(var i=0;i<bases.length;i++){ 288 if(bases[i] === baseclass){ 289 return true; 290 } 291 } 292 for(var i=0;i<bases.length;i++){ 293 if(issubclass(bases[i], baseclass)){ 294 return true; 295 } 296 } 297 } 298 return false; 299 } 300 }; 301 231 302 232 303 /** … … 297 368 }; 298 369 299 300 370 publ.__str__=function(){ 301 var s = "%s %s".format(this.name, this.module);302 return s;303 };371 return this.toTraceString(); 372 }; 373 304 374 /** 305 375 Returns the complete trace of the exception. 376 @param indent=0 The indention to use for each line. 306 377 @return The error trace. 307 378 **/ … … 309 380 indent = indent==null ? 0 : indent; 310 381 311 //todo:use constructor.__name__ 382 //todo:use constructor.__name__? 312 383 var s="%s in %s:\n%s".format(this.name, this.module, this.message.indent(4)).indent(indent); 313 384 if(this.trace){ … … 320 391 return s; 321 392 }; 322 323 324 393 325 394 ///The name of the Exception. … … 782 851 **/ 783 852 String.prototype.pad = function(flag, len){ 784 var s = "";853 785 854 if(flag == "-"){ 786 855 var c = " "; 787 856 }else{ 788 var c = flag; 789 } 790 for(var i=0;i<len-this.length;i++){ 791 s += c; 792 } 857 var c ='' + flag; 858 } 859 var s = c.mul(len-this.length); 860 793 861 if(flag == "-"){ 794 862 s = this + s; … … 798 866 return s; 799 867 }; 800 868 869 /** 870 Indents each line of a String. 871 @param indent The number of spaces to use for indention. 872 @return The indented string. 873 **/ 801 874 String.prototype.indent=function(indent){ 802 875 var out=[]; … … 807 880 return out.join('\n'); 808 881 }; 809 882 883 /** 884 Multiplies a string. 885 @param l The multiplier. 886 @return A string. 887 **/ 810 888 String.prototype.mul=function(l){ 889 l = (l < 0 )? 0: l; 811 890 var a=new Array(l+1); 812 891 return a.join(this); trunk/jsolait/lib/testing.js
r22 r27 81 81 **/ 82 82 mod.Test=Class(function(publ,supr){ 83 publ.__init__=function(testScope){ 83 publ.__init__=function(name, testScope){ 84 if(testScope === undefined){ 85 testScope=name; 86 name = 'anonymous'; 87 } 88 this.name = name; 84 89 this.testScope=testScope; 85 90 }; … … 111 116 publ.report=function(){ 112 117 if(this.error){ 113 return "Test has failed after %s ms due to:\n\n%s".format(this.duration, this.error.toTraceString().indent(4));118 return "Test %s has failed after %s ms due to:\n\n%s".format(this.name, this.duration, this.error.toTraceString().indent(4)); 114 119 }else{ 115 return "Test completed in %s ms".format(this.duration);120 return "Test %s completed in %s ms".format( this.name, this.duration); 116 121 } 117 122 };
