Changeset 27

Show
Ignore:
Timestamp:
11/25/05 15:05:31 (3 years ago)
Author:
Jan-Klaas Kollhof
Message:

added a optional name param for Test in testing module, added isinstance() and issubclass() to the core script as global functions.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/jsolait/jsolait.js

    r26 r27  
    153153                    rslt[n] = proto[n]; 
    154154                } 
    155                 rslt.constructor = arguments.callee
     155                rslt.constructor = proto.__class__
    156156                rslt.toString = proto.__str__; 
    157157                if(rslt.__init__){ 
     
    175175                    rslt[n] = proto[n]; 
    176176                } 
    177                 rslt.constructor = proto
     177                rslt.constructor = proto.__class__
    178178                rslt.toString = proto.__str__; 
    179179                if(rslt.__init__){ 
     
    224224Class.__createProto__=function(){ throw "Can't use Class as a base class.";}; 
    225225 
     226Function.__createProto__ = function(){ throw "Cannot inherit from Function. implement the callable interface instead using YourClass::__call__.";}; 
     227Array.__createProto__=function(){ var r =[]; r.__str__ = Array.prototype.toString;  return r; }; 
    226228Array.__isArray__=true; 
    227229Array.__str__=Array.toString=function(){return "[class Array]";}; 
    228 Array.__createProto__=function(){ var r =[]; r.__str__ = Array.prototype.toString;  return r; }; 
    229230Object.__str__=Object.toString=function(){return "[class Object]";}; 
    230 Function.__createProto__ = function(){ throw "Cannot inherit from Function. implement the callable interface instead using YourClass::__call__.";}; 
     231Number.__str__ =Number.toString=function(){return "[class Number]";}; 
     232String.__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**/ 
     240str = 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**/ 
     260isinstance=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**/ 
     281issubclass=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 
    231302 
    232303/** 
     
    297368    }; 
    298369 
    299  
    300370    publ.__str__=function(){ 
    301         var s = "%s %s".format(this.name, this.module); 
    302         return s
    303     }; 
     371        return this.toTraceString(); 
     372    }
     373     
    304374    /** 
    305375        Returns the complete trace of the exception. 
     376        @param indent=0  The indention to use for each line. 
    306377        @return The error trace. 
    307378    **/ 
     
    309380        indent = indent==null ? 0 : indent; 
    310381 
    311         //todo:use  constructor.__name__ 
     382        //todo:use  constructor.__name__? 
    312383        var s="%s in %s:\n%s".format(this.name, this.module, this.message.indent(4)).indent(indent); 
    313384        if(this.trace){ 
     
    320391        return s; 
    321392    }; 
    322  
    323  
    324393 
    325394    ///The name of the Exception. 
     
    782851    **/ 
    783852    String.prototype.pad = function(flag, len){ 
    784         var s = ""; 
     853         
    785854        if(flag == "-"){ 
    786855            var c = " "; 
    787856        }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         
    793861        if(flag == "-"){ 
    794862            s = this + s; 
     
    798866        return s; 
    799867    }; 
    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    **/ 
    801874    String.prototype.indent=function(indent){ 
    802875        var out=[]; 
     
    807880        return out.join('\n'); 
    808881    }; 
    809  
     882     
     883    /** 
     884        Multiplies a string. 
     885        @param l The multiplier. 
     886        @return A string. 
     887    **/ 
    810888    String.prototype.mul=function(l){ 
     889        l = (l < 0 )? 0: l; 
    811890        var a=new Array(l+1); 
    812891        return a.join(this); 
  • trunk/jsolait/lib/testing.js

    r22 r27  
    8181    **/ 
    8282    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; 
    8489            this.testScope=testScope; 
    8590        }; 
     
    111116        publ.report=function(){ 
    112117            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)); 
    114119            }else{ 
    115                 return "Test completed in %s ms".format(this.duration); 
     120                return "Test %s completed in %s ms".format( this.name, this.duration); 
    116121            } 
    117122        };