Changeset 8

Show
Ignore:
Timestamp:
10/20/05 18:05:13 (3 years ago)
Author:
Jan-Klaas Kollhof
Message:

added some unit testing to testing module, improved supr handling(reverted to old version, some changes to exception class

Files:

Legend:

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

    r7 r8  
    171171        } 
    172172    } 
    173         
    174     //create a supr  function to be used to call methods of the super class 
    175     var supr = function(self){ 
    176         //make sure its a legal call to supr 
    177         if(! (self.constructor.isSubclassOf(superClass))){ 
    178             throw "Illegal call of supr:\n %s is not an instance of %s".format(self, superClass); 
    179         } 
    180         var wrapper; 
    181         //if a wrapper has been created already then use it 
    182         if(self[" super_this_" + superClass.__name__]){ 
    183             wrapper = self[" super_this_" + superClass.__name__]; 
    184         }else{ 
    185             //set up super class functionality so a call to super(this) will return an object with all super class methods  
    186             //the methods can be called like super(this).foo and the this object will be bound to that method. 
    187             wrapper = {}; 
    188             var superProto = superClass.prototype; 
    189             for(var n in superProto){ 
    190                 if(superProto[n] instanceof Function){ 
    191                     wrapper[n] = function(){ 
    192                                             var f = arguments.callee; 
    193                                             return superProto[f._name].apply(self, arguments); 
    194                                         }; 
    195                     wrapper[n]._name = n; 
    196                 } 
    197             } 
    198             //save the wrapper so calling of supr a second time is much faster and skips the wrapper setup 
    199             self[" super_this_" + superClass.__name__]=wrapper; 
    200         } 
    201         return wrapper; 
    202     }; 
    203          
     173     
    204174    //execute the scope of the class 
    205     classScope(NewClass.prototype, supr); 
     175    switch(classScope.length){ 
     176        case 3: //publ, statc, supr   
     177            classScope(NewClass.prototype, NewClass, superClass.prototype); 
     178            break; 
     179        default://publ, supr 
     180            classScope(NewClass.prototype, superClass.prototype); 
     181            break; 
     182    } 
     183     
    206184    return NewClass; 
    207185};     
     186 
    208187Class.toString = function(){ 
    209188    return "[object Class]"; 
    210189}; 
     190 
    211191Class.__createProto__=function(){  
    212192    throw "Can't use Class as a super class."; 
     
    262242    throw "Can't use Module as a super class."; 
    263243}; 
     244 
     245     
    264246/** 
    265247    Base class for all module-Exceptions. 
     
    275257    publ.__init__=function(msg, trace){ 
    276258        this.name = this.constructor.__name__; 
    277         this.message = msg; 
     259        this.message = ''+msg; 
    278260        this.trace = trace; 
    279261    }; 
    280262     
     263     
    281264    publ.toString=function(){ 
    282         var s = "%s %s\n\n".format(this.name, this.module); 
    283         s += this.message; 
     265        var s = "%s %s".format(this.name, this.module); 
    284266        return s; 
    285267    }; 
     
    288270        @return The error trace. 
    289271    **/ 
    290     publ.toTraceString=function(){ 
    291     //todo:use  constructor.__name__ 
    292         var s = "%s in %s:\n    ".format(this.name, this.module ); 
    293         s+="%s\n\n".format(this.message); 
     272    publ.toTraceString=function(indent){ 
     273        indent = indent==null ? 0 : indent; 
     274         
     275        //todo:use  constructor.__name__ 
     276        var s="%s in %s:\n%s".format(this.name, this.module, this.message.indent(4)).indent(indent); 
    294277        if(this.trace){ 
    295278            if(this.trace.toTraceString){ 
    296                 s+= this.trace.toTraceString(); 
     279                s+='\n\n'+ this.trace.toTraceString(indent + 8); 
    297280            }else{ 
    298                 s+= this.trace + 'sdfsdf'
     281                s+=(this.trace +'\n').indent(indent)
    299282            } 
    300283        } 
    301284        return s; 
    302285    }; 
     286     
     287    
    303288     
    304289    ///The name of the Exception. 
     
    322307    **/ 
    323308    publ.__init__=function(module, trace){ 
    324         supr(this).__init__("Failed to run the module scope for %s".format(module), trace); 
     309        supr.__init__.call(this, "Failed to run the module scope for %s".format(module), trace); 
    325310        this.failedModule = module; 
    326311    }; 
     
    426411        **/ 
    427412        publ.__init__=function(sourceURI, trace){ 
    428             supr(this).__init__("Failed to load file: '%s'".format(sourceURI), trace); 
     413            supr.__init__.call(this, "Failed to load file: '%s'".format(sourceURI), trace); 
    429414            this.sourceURI = sourceURI; 
    430415        }; 
     
    504489        **/ 
    505490        publ.__init__=function(moduleName, moduleURIs, trace){ 
    506             supr(this).__init__("Failed to import module: '%s' from:\n%s".format(moduleName, moduleURIs.join(',\n')), trace); 
     491            supr.__init__.call(this, "Failed to import module: '%s' from:\n%s".format(moduleName, moduleURIs.join(',\n')), trace); 
    507492            this.moduleName = moduleName; 
    508493            this.moduleURIs = moduleURIs; 
     
    761746        return s; 
    762747    }; 
    763  
     748     
     749    String.prototype.indent=function(indent){ 
     750        var out=[]; 
     751        var s=this.split('\n'); 
     752        for(var i=0;i<s.length;i++){ 
     753            var pr=''; 
     754            for(var k=0;k<indent;k++){ 
     755                pr +=' '; 
     756            }   
     757            out.push(pr + s[i]); 
     758        } 
     759        return out.join('\n'); 
     760    } 
     761     
    764762    ///Tests the module. 
    765763    mod.test=function(){ 
  • jsolait/trunk/jsolait/jsolait.wsf

    r5 r8  
    159159                        if(modl.__main__){ 
    160160                            //todo find arguments 
    161                             modl.__main__.call(modl,[]); 
     161                            try{ 
     162                                modl.__main__.call(modl,[]); 
     163                            }catch(e){ 
     164                                throw new mod.Exception("runing %s  __main__()  failed\n".format(modl),e) 
     165                            } 
    162166                        } 
    163167                        return; 
  • jsolait/trunk/jsolait/lib/jsonrpc.js

    r5 r8  
    3737        */ 
    3838        publ.__init__= function(status){ 
    39             supr(this).__init__("The server did not respond with a status 200 (OK) but with: " + status); 
     39            supr.__init__.call(this, "The server did not respond with a status 200 (OK) but with: " + status); 
    4040            this.status = status; 
    4141        }; 
     
    5555        */ 
    5656        publ.__init__= function(msg, s, trace){ 
    57             supr(this).__init__(msg,trace); 
     57            supr.__init__.call(this, msg,trace); 
    5858            this.source = s; 
    5959        }; 
     
    7171        */ 
    7272        publ.__init__= function(err, trace){ 
    73             supr(this).__init__(err,trace); 
     73            supr.__init__.call(this, err,trace); 
    7474        }; 
    7575    }); 
  • jsolait/trunk/jsolait/lib/lang.js

    r7 r8  
    384384         
    385385        publ.__init__=function(s, globalNode){ 
    386             supr(this).__init__(s); 
     386            supr.__init__.call(this, s); 
    387387            globalNode = globalNode === undefined ? new mod.GlobalNode() : globalNode; 
    388388            this.currentNode=globalNode; 
     
    10111011    mod.Compressor=Class(mod.Tokenizer, function(publ,supr){ 
    10121012        publ.__init__=function(source){ 
    1013             supr(this).__init__(source); 
     1013            supr.__init__.call(this, source); 
    10141014            this.wsNeeded=false; 
    10151015        }; 
     
    11661166    mod.__main__=function(){ 
    11671167        //var s='switch(a){\n case "as":\n\na=2;\nbreak;case "df":\nbreak;   } a.'; 
    1168         var i =imprt('iter'); 
     1168        var it =imprt('iter'); 
    11691169        var filenames= ['jsolait.js',  
    11701170                'lib/codecs.js',   
     
    12021202}); 
    12031203 
     1204 
     1205 
  • jsolait/trunk/jsolait/lib/sets.js

    r5 r8  
    5656                iterable = iterable.split(""); 
    5757            } 
    58             for(var i=0;i<iterable.length;i++){ 
    59                 this.add(iterable[i]); 
    60             } 
     58            //todo do iterable impl 
     59            /* 
     60            if(iterable.__iter__){ 
     61                var i=iterable.__iter__(); 
     62                var item; 
     63                while(item=i.next()!==undefined){ 
     64                    this.add(item); 
     65                } 
     66            }else{*/ 
     67                for(var i=0;i<iterable.length;i++){ 
     68                    this.add(iterable[i]); 
     69                } 
     70            //} 
    6171        }; 
    6272         
     
    164174        }; 
    165175         
     176        publ.__equals__=function(setObj){ 
     177            if(setObj instanceof publ.constructor){ 
     178                return this.equals(setObj); 
     179            }else{ 
     180                return false; 
     181            } 
     182        }; 
     183         
    166184        /** 
    167185            Creates a new set containing all elements of set and setObj (newSet = set | setObj). 
     
    309327     
    310328    mod.__main__=function(){ 
    311          
    312         var prntRslt=function(expected, rslt){ 
    313             print("expected\t:" + expected); 
    314             print("calculated\t:" + rslt); 
    315             if(rslt.equals(expected) == false){ 
    316                 throw "Sets are not Equal:\n" + expected + " != " + rslt; 
    317             } 
    318             print(""); 
    319         }; 
     329        
    320330         
    321331        var s1=new mod.Set("0123456"); 
    322332        var s2=new mod.Set("3456789"); 
    323          
    324         print("%s | %s".format(s1, s2)); 
    325         prntRslt(new mod.Set("0123456789"), s1.union(s2)); 
    326          
    327         print("%s | %s".format(s2, s1)); 
    328         prntRslt(new mod.Set("0123456789"), s2.union(s1)); 
    329          
    330         print("%s & %s".format(s1, s2)); 
    331         prntRslt(new mod.Set("3456"), s1.intersection(s2)); 
    332          
    333         print("%s & %s".format(s2, s1)); 
    334         prntRslt(new mod.Set("3456"), s2.intersection(s1)); 
    335          
    336         print("%s - %s".format(s1, s2)); 
    337         prntRslt(new mod.Set("012"), s1.difference(s2)); 
    338          
    339         print("%s - %s".format(s2, s1)); 
    340         prntRslt(new mod.Set("789"), s2.difference(s1)); 
    341          
    342         print("%s ^ %s".format(s1, s2)); 
    343         prntRslt(new mod.Set("012789"),s1.symmDifference(s2)); 
    344          
    345         print("%s ^ %s".format(s2, s1)); 
    346         prntRslt(new mod.Set("012789"),s2.symmDifference(s1)); 
     333        var testing=imprt('testing'); 
     334         
     335        print(testing.test(function(){ 
     336            testing.assertEquals("checking %s | %s".format(s1, s2),  
     337                                        new mod.Set("0123456789"), s1.union(s2)) 
     338             
     339            testing.assertEquals("checking %s | %s".format(s2, s1), 
     340                                        new mod.Set("0123456789"), s2.union(s1)); 
     341 
     342            testing.assertEquals("checking %s & %s".format(s1, s2), 
     343                                         new mod.Set("3456"), s1.intersection(s2)); 
     344             
     345            testing.assertEquals("checking %s & %s".format(s2, s1), 
     346                                        new mod.Set("3456"), s2.intersection(s1)); 
     347             
     348            testing.assertEquals("checking %s - %s".format(s1, s2), 
     349                                        new mod.Set("012"), s1.difference(s2)); 
     350             
     351            testing.assertEquals("checking %s - %s".format(s2, s1), 
     352                                        new mod.Set("789"), s2.difference(s1)); 
     353             
     354            testing.assertEquals("checking %s ^ %s".format(s1, s2), 
     355                                        new mod.Set("012789"),s1.symmDifference(s2)); 
     356             
     357            testing.assertEquals("checking %s ^ %s".format(s2, s1), 
     358                                        new mod.Set("012789"),s2.symmDifference(s1)); 
     359        })) 
    347360    }; 
    348361}); 
  • jsolait/trunk/jsolait/lib/testing.js

    r5 r8  
    4747    }; 
    4848     
    49      
    50     mod.testModule=function(modName){ 
    51         var t=(new Date()).getTime(); 
    52         log("Starting module test"); 
    53         log("importing '%s' for testing ...".format(modName)); 
    54         try{ 
    55             var m = jsolait.imprt(modName); 
    56         }catch(e){ 
    57             log(e); 
    58             log("Premeture exiting of module test because importing of '%s' failed.".format(modName), LogError); 
    59             return; 
    60         } 
    61          
    62         if(m.test==null){ 
    63             log("Premeture exiting of module test because %s does not expose a 'test()' method.".format(m), LogError);  
    64             return; 
    65         } 
    66         log("%s imported, running '%s.test()' ...".format(m,modName));  
    67         try{ 
    68             m.test(mod); 
    69         }catch(e){ 
    70             log(e+""); 
    71             log("Premeture exiting of module test due to an error running '%s.test()'".format(modName), LogError); 
    72             return; 
    73         } 
    74         var t= (new Date()).getTime()-t ;  
    75         log("Testing of module '%s' completed in %s ms.".format(modName, t < 1? "< 1":t)) ; 
    76      
     49    mod.Test=Class(function(publ,supr){ 
     50        publ.__init__=function(testScope){ 
     51            this.testScope=testScope; 
     52        } 
     53         
     54        publ.run=function(){ 
     55            this.startTime=(new Date()).getTime(); 
     56            try{ 
     57                this.testScope(); 
     58            }catch(e){ 
     59                if(e instanceof mod.AssertFailed){ 
     60                    this.error = e; 
     61                }else{ 
     62                    throw new mod.Exception("Failed to run test.", e); 
     63                } 
     64            } 
     65            this.endTime=(new Date()).getTime(); 
     66            this.duration=this.endTime-this.startTime; 
     67        } 
     68         
     69        publ.report=function(){ 
     70            if(this.error){ 
     71                return "Test has failed after %s ms due to:\n\n%s".format(this.duration, this.error.toTraceString().indent(4)); 
     72            }else{ 
     73                return "Test completed in %s ms".format(this.duration); 
     74            } 
     75        } 
     76         
     77        publ.startTime; 
     78        publ.endTime; 
     79        publ.duration; 
     80    }); 
     81     
     82    mod.test=function(testScope){ 
     83        var t= new mod.Test(testScope); 
     84        t.run(); 
     85        return t.report(); 
    7786    }; 
    7887     
     88    mod.AssertFailed=Class(mod.Exception, function(publ,supr){ 
     89        publ.__init__=function(comment, failMsg){ 
     90            this.failMessage = failMsg; 
     91            this.comment = comment; 
     92            supr.__init__.call(this, "%s failed: %s".format(comment, failMsg)); 
     93        } 
     94    }); 
     95     
     96    mod.assert=function(comment, value, failMsg){ 
     97        if(typeof comment == 'boolean'){ 
     98            failMsg=value; 
     99            value = comment; 
     100            comment =''; 
     101        } 
     102         
     103        if(value!==true){ 
     104            throw new mod.AssertFailed(comment, failMsg===undefined ? "Expected true but found: %s".format(value) : failMsg); 
     105        } 
     106    } 
     107     
     108    mod.assertTrue=function(comment, value){ 
     109        if(arguments.length==1){ 
     110            value = comment; 
     111            comment =''; 
     112        } 
     113        mod.assert(comment, value===true, "Expected true but found: %s".format(value));             
     114    } 
     115     
     116    mod.assertFalse=function(comment, value){ 
     117        if(arguments.length==1){ 
     118            value = comment; 
     119            comment =''; 
     120        } 
     121        mod.assert(comment, value===false, "Expected false but found: %s".format(value));             
     122    } 
     123     
     124    mod.assertEquals=function(comment, value1, value2){ 
     125        if(arguments.length==2){ 
     126            value2=value1; 
     127            value1 = comment; 
     128            comment =''; 
     129        } 
     130        if((value1 != null) && (value1.__equals__) || ((value2 != null) && (value2.__equals__))){ 
     131            mod.assert(comment, value1.__equals__(value2), "Expected (using __equals__) %s === %s.".format(value1, value2));             
     132        }else{ 
     133            mod.assert(comment, value1  === value2, "Expected %s === %s.".format(value1, value2));             
     134        } 
     135    } 
     136     
     137    mod.assertNotEquals=function(comment, value1, value2){ 
     138        if(arguments.length==2){ 
     139            value2=value1; 
     140            value1 = comment; 
     141            comment =''; 
     142        } 
     143        if((value1 != null) && (value1.__equals__) || ((value2 != null) && (value2.__equals__))){ 
     144            mod.assert(comment, ! value1.__equals__(value2), "Expected (using __equals__) %s !== %s.".format(value1, value2));             
     145        }else{ 
     146            mod.assert(comment, value1  !== value2, "Expected %s !== %s.".format(value1, value2));             
     147        } 
     148    } 
     149     
     150    mod.assertNull=function(comment, value){ 
     151        if(arguments.length==1){ 
     152            value = comment; 
     153            comment =''; 
     154        } 
     155        mod.assert(comment, value===null, "Expected %s === null.".format(value)); 
     156    } 
     157     
     158    mod.assertNotNull=function(comment, value){ 
     159        if(arguments.length==1){ 
     160            value = comment; 
     161            comment =''; 
     162        } 
     163        mod.assert(comment, value !==null, "Expected %s !== null.".format(value)); 
     164    } 
     165     
     166    mod.assertUndefined=function(comment, value){ 
     167        if(arguments.length==1){ 
     168            value = comment; 
     169            comment =''; 
     170        } 
     171        mod.assert(comment, value===undefined, "Expected %s === undefined.".format(value)); 
     172    } 
     173     
     174    mod.assertNotUndefined=function(comment, value){ 
     175        if(arguments.length==1){ 
     176            value = comment; 
     177            comment =''; 
     178        } 
     179        mod.assert(comment, value!==undefined, "Expected %s !== undefined".format(value)); 
     180    } 
     181     
     182    mod.assertNaN=function(comment, value){ 
     183        if(arguments.length==1){ 
     184            value = comment; 
     185            comment =''; 
     186        } 
     187        mod.assert(comment, isNaN(value)===true, "Expected %s === NaN.".format(value)); 
     188    } 
     189     
     190    mod.assertNotNaN=function(comment, value){ 
     191        if(arguments.length==1){ 
     192            value = comment; 
     193            comment =''; 
     194        } 
     195        mod.assert(comment, isNaN(value)!==true, "Expected %s !== NaN".format(value)); 
     196    } 
     197     
     198    mod.fail=function(){ 
     199         
     200    } 
     201         
    79202    mod.objectKeys=function(obj){ 
    80203        var keys=[]; 
     
    84207        return keys; 
    85208    }; 
     209     
     210    mod.__main__=function(){ 
     211        print(mod.test(function(){ 
     212            mod.assert(true); 
     213            mod.assertTrue(true); 
     214            mod.assertFalse(false); 
     215            mod.assertNull(null); 
     216            mod.assertNotNull(undefined); 
     217            mod.assertNotNull(''); 
     218            mod.assertNotNull({}); 
     219            mod.assertNotNull(0); 
     220            mod.assertUndefined(undefined); 
     221            mod.assertNotUndefined(null); 
     222            mod.assertNaN(NaN); 
     223            mod.assertNotNaN(435); 
     224            mod.assertEquals(1,1); 
     225            mod.assertEquals("a","a"); 
     226            mod.assertEquals(null,null); 
     227            mod.assertEquals(undefined,undefined); 
     228            mod.assertEquals(mod,mod); 
     229            mod.assertNotEquals(1,2); 
     230            mod.assertNotEquals(null,undefined); 
     231            mod.assertNotEquals(mod,{}); 
     232        })); 
     233    } 
    86234}); 
     235 
     236 
  • jsolait/trunk/jsolait/lib/urllib.js

    r5 r8  
    3636        */ 
    3737        publ.__init__=function(trace){ 
    38             supr(this).__init__( "Could not create an HTTP request object", trace); 
     38            supr.__init__.call(this, "Could not create an HTTP request object", trace); 
    3939        }; 
    4040    }); 
     
    4949        */ 
    5050        publ.__init__=function(trace){ 
    51             supr(this).__init__( "Opening of HTTP request failed.", trace); 
     51            supr.__init__.call(this, "Opening of HTTP request failed.", trace); 
    5252        }; 
    5353    }); 
     
    6262        */ 
    6363        publ.__init__ = function(trace){ 
    64             supr(this).__init__( "Sending of HTTP request failed.", trace); 
     64            supr.__init__.call(this, "Sending of HTTP request failed.", trace); 
    6565        }; 
    6666    }); 
  • jsolait/trunk/jsolait/lib/xml.js

    r5 r8  
    4141        */ 
    4242        publ.__init__=function(trace){ 
    43             supr(this).__init__("Could not create an XML parser.", trace); 
     43            supr.__init__.call(this, "Could not create an XML parser.", trace); 
    4444        }; 
    4545    }); 
     
    5454        */ 
    5555        publ.__init__=function(xml,trace){ 
    56              supr(this).__init__("Failed parsing XML document.",trace); 
     56             supr.__init__.call(this, "Failed parsing XML document.",trace); 
    5757            this.xml = xml; 
    5858        }; 
  • jsolait/trunk/jsolait/lib/xmlrpc.js

    r2 r8  
    3939        */ 
    4040        publ.__init__= function(status){ 
    41             supr(this).__init__("The server did not respond with a status 200 (OK) but with: " + status); 
     41            supr.__init__.call(this, "The server did not respond with a status 200 (OK) but with: " + status); 
    4242            this.status = status; 
    4343        }; 
     
    5757        */ 
    5858        publ.__init__= function(msg, xml, trace){ 
    59             supr(this).__init__(msg,trace); 
     59            supr.__init__.call(this, msg,trace); 
    6060            this.xml = xml; 
    6161        }; 
     
    7373        */ 
    7474        publ.__init__= function(faultCode, faultString){ 
    75             supr(this).__init__("XML-RPC Fault: " +  faultCode + "\n\n" + faultString); 
     75            supr.__init__.call(this, "XML-RPC Fault: " +  faultCode + "\n\n" + faultString); 
    7676            this.faultCode = faultCode; 
    7777            this.faultString = faultString; 
     
    812812 
    813813 
    814     mod.test = function(){ 
     814    mod.__main__ = function(){ 
    815815        var s = new mod.ServiceProxy("http://jsolait.net/test.py",['echo']); 
    816816        print("creating ServiceProxy object using introspection for method construction...\n");