Changeset 21

Show
Ignore:
Timestamp:
11/16/05 15:43:54 (3 years ago)
Author:
Jan-Klaas Kollhof
Message:

added operators and updated testing and dom a little

Files:

Legend:

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

    r5 r21  
    11/* 
    22    Copyright (c) 2005 Jan-Klaas Kollhof 
    3      
     3 
    44    This file is part of the JavaScript o lait library(jsolait). 
    5      
     5 
    66    jsolait is free software; you can redistribute it and/or modify 
    77    it under the terms of the GNU Lesser General Public License as published by 
    88    the Free Software Foundation; either version 2.1 of the License, or 
    99    (at your option) any later version. 
    10      
     10 
    1111    This software is distributed in the hope that it will be useful, 
    1212    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414    GNU Lesser General Public License for more details. 
    15      
     15 
    1616    You should have received a copy of the GNU Lesser General Public License 
    1717    along with this software; if not, write to the Free Software 
    18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
     18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    1919*/ 
    2020/** 
    2121    Module providing DOM implementations. 
    22      
     22 
    2323    @author                  Jan-Klaas Kollhof 
    2424    @lastchangedby       $LastChangedBy$ 
     
    2727Module("dom", "$Revision$", function(mod){ 
    2828    var sets=imprt("sets"); 
    29      
     29 
    3030    /** 
    3131        Event class. 
     
    3535        publ.type=null; 
    3636    }); 
    37      
    38      
     37 
     38 
    3939    /** 
    4040        An EventTarget implementation. 
     
    5252                var l = this.eventListeners[evt.type].items; 
    5353                for(var h in l){ 
    54                     l[h].handleEvent(evt); 
     54                    if(typeof l == 'function'){ 
     55                        l(evt); 
     56                    }else{ 
     57                        l[h].handleEvent(evt); 
     58                    } 
    5559                } 
    5660            } 
     
    8084        }; 
    8185    }); 
    82      
     86 
    8387    /** 
    8488        An EventListener wrapper. 
     
    9397        publ.handleEvent=function(evt){ 
    9498            if(this[evt.type]){ 
    95                 this[evt.type](evt); 
     99                this['handleEvent_' + evt.type](evt); 
    96100            } 
    97101        }; 
    98102    }); 
    99      
     103 
    100104    /** 
    101105        A combination of an EventTarget and a EventListener. 
  • jsolait/trunk/jsolait/lib/sets.js

    r17 r21  
    11/* 
    22  Copyright (c) 2005 Jan-Klaas Kollhof 
    3    
     3 
    44  This file is part of the JavaScript O Lait library(jsolait). 
    5    
     5 
    66  jsolait is free software; you can redistribute it and/or modify 
    77  it under the terms of the GNU Lesser General Public License as published by 
    88  the Free Software Foundation; either version 2.1 of the License, or 
    99  (at your option) any later version. 
    10   
     10 
    1111  This software is distributed in the hope that it will be useful, 
    1212  but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414  GNU Lesser General Public License for more details. 
    15   
     15 
    1616  You should have received a copy of the GNU Lesser General Public License 
    1717  along with this software; if not, write to the Free Software 
    1818  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    19 */  
     19*/ 
    2020 
    2121/** 
    2222    A module providing Sets. 
    23      
     23 
    2424    @creator                 Jan-Klaas Kollhof 
    2525    @created                2005-03-11 
     
    2828**/ 
    2929Module("sets", "$Revision$", function(mod){ 
    30      
     30 
    3131    /** 
    3232        Thrown if an item was not found in the set. 
     
    3737        ///The item that was not found. 
    3838        publ.item; 
    39          
     39 
    4040        publ.__init__=function(set, item){ 
    4141            this.set =set; 
     
    4343        }; 
    4444    }); 
    45      
     45 
    4646    /** 
    4747        The Set class. 
     
    5555        **/ 
    5656        publ.__init__=function(elem){ 
    57              
     57 
    5858            this.items = {}; 
    5959            var elems =[]; 
    60              
     60 
    6161            if(arguments.length > 1){ 
    6262               elems=arguments; 
     
    7777                } 
    7878            } 
    79              
     79 
    8080            for(var i=0;i<elems.length;i++){ 
    8181                this.add(elems[i]); 
    8282            } 
    8383        }; 
    84          
     84 
    8585        /** 
    8686            Adds an item to the set if it does not exist yet. 
     
    9898            return item; 
    9999        }; 
    100          
    101          
    102          
    103         /** 
    104             Removes an item from the set raising  
     100 
     101 
     102 
     103        /** 
     104            Removes an item from the set raising 
    105105            an ItemNotFoundInSet error if the item is not present 
    106106            @param item The item to remove. 
     
    118118            }else{ 
    119119                item = this.items[h]; 
    120                 delete this.items[h];     
     120                delete this.items[h]; 
    121121                return item; 
    122122            } 
    123123        }; 
    124          
     124 
    125125        /** 
    126126            Removes an item from the set wether or not the item realy exists. 
     
    136136            } 
    137137            item = this.items[h]; 
    138             delete this.items[h];     
     138            delete this.items[h]; 
    139139            return item; 
    140140        }; 
    141          
     141 
    142142        /** 
    143143            Returns wether or not an item is part of the set. 
     
    167167            return true; 
    168168        }; 
    169                  
     169 
    170170        /** 
    171171            Returns wether or not the set is a super set of another set. 
     
    176176            return setObj.isSubSet(this); 
    177177        }; 
    178          
     178 
    179179        /** 
    180180            Returns wether or not the set is equal to the other set. 
     
    185185            return (this.isSubSet(setObj) && setObj.isSubSet(this)); 
    186186        }; 
    187          
    188         publ.__equals__=function(setObj){ 
    189             if(setObj instanceof publ.constructor){ 
     187 
     188        publ.__eq__=function(setObj){ 
     189            if(setObj.isSubSet!==undefined){ 
    190190                return this.equals(setObj); 
    191191            }else{ 
     
    193193            } 
    194194        }; 
    195          
     195 
    196196        /** 
    197197            Creates a new set containing all elements of set and setObj (newSet = set | setObj). 
     
    204204            return ns; 
    205205        }; 
    206          
     206 
    207207        /** 
    208208            Creates a new set containing elements common to the set and setObj (newSet = set & setObj). 
     
    235235            return ns; 
    236236        }; 
    237                 
    238          
     237 
     238 
    239239        /** 
    240240            Creates a new set containing elements from the set and setObj but no elements present in both(newSet = (set - setObj) | (setObj - set)). 
     
    246246            return ns.unionUpdate(setObj.difference(this)); 
    247247        }; 
    248          
    249          
     248 
     249 
    250250        /** 
    251251            Updates the set adding all elements from setObj (set = set | setObj). 
     
    259259            return this; 
    260260        }; 
    261          
     261 
    262262        /** 
    263263            Updates the set keeping only elements also found in setObj (set = set & setObj). 
     
    274274            return this; 
    275275        }; 
    276          
     276 
    277277        /** 
    278278            Updates the set removing all elements found in setObj  (set = set - setObj). 
     
    289289            return this; 
    290290        }; 
    291          
     291 
    292292        /** 
    293293            Updates the set to only contain elements from the set and setObj but no elements present in both(set = (set - setObj) | (setObj - set)). 
     
    300300            return this.unionUpdate(union); 
    301301        }; 
    302          
     302 
    303303        /** 
    304304            Creates a copy of the set. 
     
    309309            return ns.unionUpdate(this); 
    310310        }; 
    311          
     311 
    312312        /** 
    313313            Removes all elements from teh set. 
     
    316316            this.items={}; 
    317317        }; 
    318          
     318 
    319319        /** 
    320320            Returns an array containing all elements of the set. 
     
    328328            return a; 
    329329        }; 
    330          
     330 
    331331        publ.toString=function(){ 
    332332            var items =[]; 
     
    337337        }; 
    338338    }); 
    339      
    340      
     339 
     340 
    341341    mod.__main__=function(){ 
    342         
    343          
     342 
     343 
    344344        var s1=new mod.Set("0123456"); 
    345345        var s2=new mod.Set("3456789"); 
    346346        var testing=imprt('testing'); 
    347          
     347 
    348348        print(testing.test(function(){ 
    349             testing.assertEquals("checking %s | %s".format(s1, s2),  
     349            testing.assertEquals("checking %s | %s".format(s1, s2), 
    350350                                        new mod.Set("0123456789"), s1.union(s2)); 
    351              
     351 
    352352            testing.assertEquals("checking %s | %s".format(s2, s1), 
    353353                                        new mod.Set("0123456789"), s2.union(s1)); 
     
    355355            testing.assertEquals("checking %s & %s".format(s1, s2), 
    356356                                         new mod.Set("3456"), s1.intersection(s2)); 
    357              
     357 
    358358            testing.assertEquals("checking %s & %s".format(s2, s1), 
    359359                                        new mod.Set("3456"), s2.intersection(s1)); 
    360              
     360 
    361361            testing.assertEquals("checking %s - %s".format(s1, s2), 
    362362                                        new mod.Set("012"), s1.difference(s2)); 
    363              
     363 
    364364            testing.assertEquals("checking %s - %s".format(s2, s1), 
    365365                                        new mod.Set("789"), s2.difference(s1)); 
    366              
     366 
    367367            testing.assertEquals("checking %s ^ %s".format(s1, s2), 
    368368                                        new mod.Set("012789"),s1.symmDifference(s2)); 
    369              
     369 
    370370            testing.assertEquals("checking %s ^ %s".format(s2, s1), 
    371371                                        new mod.Set("012789"),s2.symmDifference(s1)); 
  • jsolait/trunk/jsolait/lib/testing.js

    r18 r21  
    11/* 
    22    Copyright (c) 2005 Jan-Klaas Kollhof 
    3        
     3 
    44    This file is part of jsolait 
    5      
     5 
    66    jsolait is free software; you can redistribute it and/or modify 
    77    it under the terms of the GNU Lesser General Public License as published by 
    88    the Free Software Foundation; either version 2.1 of the License, or 
    99    (at your option) any later version. 
    10      
     10 
    1111    This software is distributed in the hope that it will be useful, 
    1212    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1313    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1414    GNU Lesser General Public License for more details. 
    15      
     15 
    1616    You should have received a copy of the GNU Lesser General Public License 
    1717    along with this software; if not, write to the Free Software 
    1818    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    19     
     19 
    2020*/ 
    2121/** 
    22      
     22 
    2323    @author                 Jan-Klaas Kollhof 
    24     @created                2003-12-14     
     24    @created                2003-12-14 
    2525    @lastchangedby       $LastChangedBy$ 
    2626    @lastchangeddate    $Date$ 
     
    2828 
    2929Module("testing", "$Revision$", function(mod){ 
    30       
     30 
     31    var ops=imprt('operators'); 
     32 
    3133    mod.minProfileTime=500; 
    3234    /** 
     
    4345        var t=(new Date()).getTime(); 
    4446        for(var i=0;i<=repeat;i++){ 
    45             fn.apply(null, args);     
     47            fn.apply(null, args); 
    4648        } 
    4749        return ((new Date()).getTime()-t) / (repeat+1); 
    4850    }; 
    49      
     51 
     52 
     53    /** 
     54        Messures the time it takes to run a function given as a parameter. 
     55        @param min=mod.minProfileTime  The minimum time to use for profiling. 
     56        @param fn                              The function to profile. The function wil be called until the min-time is reached. 
     57        @return                                  The time it took to run the function a single time. The time is averaged by the total time/repetitions 
     58    **/ 
    5059    mod.profile=function(min,fn){ 
    5160        if(arguments.length==1){ 
     
    6271        while(t2-t1<min){ 
    6372            cnt++; 
    64             fn.apply(null, args);     
     73            fn.apply(null, args); 
    6574            t2=(new Date()).getTime(); 
    6675        } 
    6776        return (t2-t1) / cnt; 
    6877    }; 
    69      
    70      
     78 
     79    /** 
     80        Provides a test task. 
     81    **/ 
    7182    mod.Test=Class(function(publ,supr){ 
    7283        publ.__init__=function(testScope){ 
    7384            this.testScope=testScope; 
    7485        }; 
    75          
     86 
     87        /** 
     88            Runs the test and generates a report. 
     89        **/ 
    7690        publ.run=function(){ 
    7791            this.failed=false; 
     
    91105            this.duration=this.endTime-this.startTime; 
    92106        }; 
    93          
     107 
     108        /** 
     109            Returns a report about the test run. 
     110        **/ 
    94111        publ.report=function(){ 
    95112            if(this.error){ 
     
    105122        publ.duration; 
    106123    }); 
    107      
     124 
     125    /** 
     126        Runs a test on the given function. 
     127        @param testScope  A function to test. 
     128    **/ 
    108129    mod.test=function(testScope){ 
    109130        var t= new mod.Test(testScope); 
     
    111132        return t.report(); 
    112133    }; 
    113      
     134 
     135    /** 
     136        Raised when an assertion fails. 
     137    **/ 
    114138    mod.AssertFailed=Class(mod.Exception, function(publ,supr){ 
    115139        publ.__init__=function(comment, failMsg){ 
     
    119143        }; 
    120144    }); 
    121      
     145 
     146    /** 
     147        Basic assert method. 
     148        @param comment=''  A comment for the assertion. 
     149        @param value          A boolean to testfor true. 
     150        @param failMsg=''     A message to pass to the AssertFailed constructor in case the assertion fails. 
     151    **/ 
    122152    mod.assert=function(comment, value, failMsg){ 
    123153        if(typeof comment == 'boolean'){ 
     
    126156            comment =''; 
    127157        } 
    128          
     158 
    129159        if(value!==true){ 
    130160            throw new mod.AssertFailed(comment, failMsg===undefined ? "Expected true but found: %s".format(value) : failMsg); 
    131161        } 
    132162    }; 
    133      
     163 
     164    /** 
     165        Assert for true; 
     166        @param comment=''  A comment for the assertion. 
     167        @param value          A boolean to test. 
     168    **/ 
    134169    mod.assertTrue=function(comment, value){ 
    135170        if(arguments.length==1){ 
     
    137172            comment =''; 
    138173        } 
    139         mod.assert(comment, value===true, "Expected true but found: %s".format(value));             
    140     }; 
    141      
     174        mod.assert(comment, value===true, "Expected true but found: %s".format(value)); 
     175    }; 
     176 
     177    /** 
     178        Assert for false; 
     179        @param comment=''  A comment for the assertion. 
     180        @param value          A boolean to test. 
     181    **/ 
    142182    mod.assertFalse=function(comment, value){ 
    143183        if(arguments.length==1){ 
     
    145185            comment =''; 
    146186        } 
    147         mod.assert(comment, value===false, "Expected false but found: %s".format(value));             
    148     }; 
    149      
     187        mod.assert(comment, value===false, "Expected false but found: %s".format(value)); 
     188    }; 
     189 
     190    /** 
     191        Assert for 2 values being equal; 
     192        @param comment=''  A comment for the assertion. 
     193        @param value          A boolean to test. 
     194    **/ 
    150195    mod.assertEquals=function(comment, value1, value2){ 
    151196        if(arguments.length==2){ 
     
    154199            comment =''; 
    155200        } 
    156         if((value1 != null) && (value1.__equals__) || ((value2 != null) && (value2.__equals__))){ 
    157             mod.assert(comment, value1.__equals__(value2), "Expected (using __equals__) %s === %s.".format(value1, value2));             
    158         }else{ 
    159             mod.assert(comment, value1  === value2, "Expected %s === %s.".format(value1, value2));             
    160         } 
    161     }; 
    162      
     201        mod.assert(comment, eq(value1, value2), "Expected %s === %s.".format(value1, value2)); 
     202    }; 
     203 
    163204    mod.assertNotEquals=function(comment, value1, value2){ 
    164205        if(arguments.length==2){ 
     
    167208            comment =''; 
    168209        } 
    169         if((value1 != null) && (value1.__equals__) || ((value2 != null) && (value2.__equals__))){ 
    170             mod.assert(comment, ! value1.__equals__(value2), "Expected (using __equals__) %s !== %s.".format(value1, value2));             
    171         }else{ 
    172             mod.assert(comment, value1  !== value2, "Expected %s !== %s.".format(value1, value2));             
    173         } 
    174     }; 
    175      
     210        mod.assert(comment, ne(value1, value2), "Expected %s !== %s.".format(value1, value2)); 
     211    }; 
     212 
    176213    mod.assertNull=function(comment, value){ 
    177214        if(arguments.length==1){ 
     
    181218        mod.assert(comment, value===null, "Expected %s === null.".format(value)); 
    182219    }; 
    183      
     220 
    184221    mod.assertNotNull=function(comment, value){ 
    185222        if(arguments.length==1){ 
     
    189226        mod.assert(comment, value !==null, "Expected %s !== null.".format(value)); 
    190227    }; 
    191      
     228 
    192229    mod.assertUndefined=function(comment, value){ 
    193230        if(arguments.length==1){ 
     
    197234        mod.assert(comment, value===undefined, "Expected %s === undefined.".format(value)); 
    198235    }; 
    199      
     236 
    200237    mod.assertNotUndefined=function(comment, value){ 
    201238        if(arguments.length==1){ 
     
    205242        mod.assert(comment, value!==undefined, "Expected %s !== undefined".format(value)); 
    206243    }; 
    207      
     244 
    208245    mod.assertNaN=function(comment, value){ 
    209246        if(arguments.length==1){ 
     
    213250        mod.assert(comment, isNaN(value)===true, "Expected %s === NaN.".format(value)); 
    214251    }; 
    215      
     252 
    216253    mod.assertNotNaN=function(comment, value){ 
    217254        if(arguments.length==1){ 
     
    221258        mod.assert(comment, isNaN(value)!==true, "Expected %s !== NaN".format(value)); 
    222259    }; 
    223      
     260 
    224261    mod.fail=function(comment){ 
    225262        throw new mod.AssertFailed(comment, "Fail was called"); 
    226263    }; 
    227          
     264 
    228265    mod.objectKeys=function(obj){ 
    229266        var keys=[]; 
     
    233270        return keys; 
    234271    }; 
    235      
     272 
    236273    mod.__main__=function(){ 
    237274        print(mod.test(function(){