Changeset 21
- Timestamp:
- 11/16/05 15:43:54 (3 years ago)
- Files:
-
- jsolait/trunk/jsolait/lib/dom.js (modified) (6 diffs)
- jsolait/trunk/jsolait/lib/operators.js (added)
- jsolait/trunk/jsolait/lib/sets.js (modified) (25 diffs)
- jsolait/trunk/jsolait/lib/testing.js (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
jsolait/trunk/jsolait/lib/dom.js
r5 r21 1 1 /* 2 2 Copyright (c) 2005 Jan-Klaas Kollhof 3 3 4 4 This file is part of the JavaScript o lait library(jsolait). 5 5 6 6 jsolait is free software; you can redistribute it and/or modify 7 7 it under the terms of the GNU Lesser General Public License as published by 8 8 the Free Software Foundation; either version 2.1 of the License, or 9 9 (at your option) any later version. 10 10 11 11 This software is distributed in the hope that it will be useful, 12 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 GNU Lesser General Public License for more details. 15 15 16 16 You should have received a copy of the GNU Lesser General Public License 17 17 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 19 19 */ 20 20 /** 21 21 Module providing DOM implementations. 22 22 23 23 @author Jan-Klaas Kollhof 24 24 @lastchangedby $LastChangedBy$ … … 27 27 Module("dom", "$Revision$", function(mod){ 28 28 var sets=imprt("sets"); 29 29 30 30 /** 31 31 Event class. … … 35 35 publ.type=null; 36 36 }); 37 38 37 38 39 39 /** 40 40 An EventTarget implementation. … … 52 52 var l = this.eventListeners[evt.type].items; 53 53 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 } 55 59 } 56 60 } … … 80 84 }; 81 85 }); 82 86 83 87 /** 84 88 An EventListener wrapper. … … 93 97 publ.handleEvent=function(evt){ 94 98 if(this[evt.type]){ 95 this[ evt.type](evt);99 this['handleEvent_' + evt.type](evt); 96 100 } 97 101 }; 98 102 }); 99 103 100 104 /** 101 105 A combination of an EventTarget and a EventListener. jsolait/trunk/jsolait/lib/sets.js
r17 r21 1 1 /* 2 2 Copyright (c) 2005 Jan-Klaas Kollhof 3 3 4 4 This file is part of the JavaScript O Lait library(jsolait). 5 5 6 6 jsolait is free software; you can redistribute it and/or modify 7 7 it under the terms of the GNU Lesser General Public License as published by 8 8 the Free Software Foundation; either version 2.1 of the License, or 9 9 (at your option) any later version. 10 10 11 11 This software is distributed in the hope that it will be useful, 12 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 GNU Lesser General Public License for more details. 15 15 16 16 You should have received a copy of the GNU Lesser General Public License 17 17 along with this software; if not, write to the Free Software 18 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 19 */ 20 20 21 21 /** 22 22 A module providing Sets. 23 23 24 24 @creator Jan-Klaas Kollhof 25 25 @created 2005-03-11 … … 28 28 **/ 29 29 Module("sets", "$Revision$", function(mod){ 30 30 31 31 /** 32 32 Thrown if an item was not found in the set. … … 37 37 ///The item that was not found. 38 38 publ.item; 39 39 40 40 publ.__init__=function(set, item){ 41 41 this.set =set; … … 43 43 }; 44 44 }); 45 45 46 46 /** 47 47 The Set class. … … 55 55 **/ 56 56 publ.__init__=function(elem){ 57 57 58 58 this.items = {}; 59 59 var elems =[]; 60 60 61 61 if(arguments.length > 1){ 62 62 elems=arguments; … … 77 77 } 78 78 } 79 79 80 80 for(var i=0;i<elems.length;i++){ 81 81 this.add(elems[i]); 82 82 } 83 83 }; 84 84 85 85 /** 86 86 Adds an item to the set if it does not exist yet. … … 98 98 return item; 99 99 }; 100 101 102 103 /** 104 Removes an item from the set raising 100 101 102 103 /** 104 Removes an item from the set raising 105 105 an ItemNotFoundInSet error if the item is not present 106 106 @param item The item to remove. … … 118 118 }else{ 119 119 item = this.items[h]; 120 delete this.items[h]; 120 delete this.items[h]; 121 121 return item; 122 122 } 123 123 }; 124 124 125 125 /** 126 126 Removes an item from the set wether or not the item realy exists. … … 136 136 } 137 137 item = this.items[h]; 138 delete this.items[h]; 138 delete this.items[h]; 139 139 return item; 140 140 }; 141 141 142 142 /** 143 143 Returns wether or not an item is part of the set. … … 167 167 return true; 168 168 }; 169 169 170 170 /** 171 171 Returns wether or not the set is a super set of another set. … … 176 176 return setObj.isSubSet(this); 177 177 }; 178 178 179 179 /** 180 180 Returns wether or not the set is equal to the other set. … … 185 185 return (this.isSubSet(setObj) && setObj.isSubSet(this)); 186 186 }; 187 188 publ.__eq uals__=function(setObj){189 if(setObj instanceof publ.constructor){187 188 publ.__eq__=function(setObj){ 189 if(setObj.isSubSet!==undefined){ 190 190 return this.equals(setObj); 191 191 }else{ … … 193 193 } 194 194 }; 195 195 196 196 /** 197 197 Creates a new set containing all elements of set and setObj (newSet = set | setObj). … … 204 204 return ns; 205 205 }; 206 206 207 207 /** 208 208 Creates a new set containing elements common to the set and setObj (newSet = set & setObj). … … 235 235 return ns; 236 236 }; 237 238 237 238 239 239 /** 240 240 Creates a new set containing elements from the set and setObj but no elements present in both(newSet = (set - setObj) | (setObj - set)). … … 246 246 return ns.unionUpdate(setObj.difference(this)); 247 247 }; 248 249 248 249 250 250 /** 251 251 Updates the set adding all elements from setObj (set = set | setObj). … … 259 259 return this; 260 260 }; 261 261 262 262 /** 263 263 Updates the set keeping only elements also found in setObj (set = set & setObj). … … 274 274 return this; 275 275 }; 276 276 277 277 /** 278 278 Updates the set removing all elements found in setObj (set = set - setObj). … … 289 289 return this; 290 290 }; 291 291 292 292 /** 293 293 Updates the set to only contain elements from the set and setObj but no elements present in both(set = (set - setObj) | (setObj - set)). … … 300 300 return this.unionUpdate(union); 301 301 }; 302 302 303 303 /** 304 304 Creates a copy of the set. … … 309 309 return ns.unionUpdate(this); 310 310 }; 311 311 312 312 /** 313 313 Removes all elements from teh set. … … 316 316 this.items={}; 317 317 }; 318 318 319 319 /** 320 320 Returns an array containing all elements of the set. … … 328 328 return a; 329 329 }; 330 330 331 331 publ.toString=function(){ 332 332 var items =[]; … … 337 337 }; 338 338 }); 339 340 339 340 341 341 mod.__main__=function(){ 342 343 342 343 344 344 var s1=new mod.Set("0123456"); 345 345 var s2=new mod.Set("3456789"); 346 346 var testing=imprt('testing'); 347 347 348 348 print(testing.test(function(){ 349 testing.assertEquals("checking %s | %s".format(s1, s2), 349 testing.assertEquals("checking %s | %s".format(s1, s2), 350 350 new mod.Set("0123456789"), s1.union(s2)); 351 351 352 352 testing.assertEquals("checking %s | %s".format(s2, s1), 353 353 new mod.Set("0123456789"), s2.union(s1)); … … 355 355 testing.assertEquals("checking %s & %s".format(s1, s2), 356 356 new mod.Set("3456"), s1.intersection(s2)); 357 357 358 358 testing.assertEquals("checking %s & %s".format(s2, s1), 359 359 new mod.Set("3456"), s2.intersection(s1)); 360 360 361 361 testing.assertEquals("checking %s - %s".format(s1, s2), 362 362 new mod.Set("012"), s1.difference(s2)); 363 363 364 364 testing.assertEquals("checking %s - %s".format(s2, s1), 365 365 new mod.Set("789"), s2.difference(s1)); 366 366 367 367 testing.assertEquals("checking %s ^ %s".format(s1, s2), 368 368 new mod.Set("012789"),s1.symmDifference(s2)); 369 369 370 370 testing.assertEquals("checking %s ^ %s".format(s2, s1), 371 371 new mod.Set("012789"),s2.symmDifference(s1)); jsolait/trunk/jsolait/lib/testing.js
r18 r21 1 1 /* 2 2 Copyright (c) 2005 Jan-Klaas Kollhof 3 3 4 4 This file is part of jsolait 5 5 6 6 jsolait is free software; you can redistribute it and/or modify 7 7 it under the terms of the GNU Lesser General Public License as published by 8 8 the Free Software Foundation; either version 2.1 of the License, or 9 9 (at your option) any later version. 10 10 11 11 This software is distributed in the hope that it will be useful, 12 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 GNU Lesser General Public License for more details. 15 15 16 16 You should have received a copy of the GNU Lesser General Public License 17 17 along with this software; if not, write to the Free Software 18 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 19 20 20 */ 21 21 /** 22 22 23 23 @author Jan-Klaas Kollhof 24 @created 2003-12-14 24 @created 2003-12-14 25 25 @lastchangedby $LastChangedBy$ 26 26 @lastchangeddate $Date$ … … 28 28 29 29 Module("testing", "$Revision$", function(mod){ 30 30 31 var ops=imprt('operators'); 32 31 33 mod.minProfileTime=500; 32 34 /** … … 43 45 var t=(new Date()).getTime(); 44 46 for(var i=0;i<=repeat;i++){ 45 fn.apply(null, args); 47 fn.apply(null, args); 46 48 } 47 49 return ((new Date()).getTime()-t) / (repeat+1); 48 50 }; 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 **/ 50 59 mod.profile=function(min,fn){ 51 60 if(arguments.length==1){ … … 62 71 while(t2-t1<min){ 63 72 cnt++; 64 fn.apply(null, args); 73 fn.apply(null, args); 65 74 t2=(new Date()).getTime(); 66 75 } 67 76 return (t2-t1) / cnt; 68 77 }; 69 70 78 79 /** 80 Provides a test task. 81 **/ 71 82 mod.Test=Class(function(publ,supr){ 72 83 publ.__init__=function(testScope){ 73 84 this.testScope=testScope; 74 85 }; 75 86 87 /** 88 Runs the test and generates a report. 89 **/ 76 90 publ.run=function(){ 77 91 this.failed=false; … … 91 105 this.duration=this.endTime-this.startTime; 92 106 }; 93 107 108 /** 109 Returns a report about the test run. 110 **/ 94 111 publ.report=function(){ 95 112 if(this.error){ … … 105 122 publ.duration; 106 123 }); 107 124 125 /** 126 Runs a test on the given function. 127 @param testScope A function to test. 128 **/ 108 129 mod.test=function(testScope){ 109 130 var t= new mod.Test(testScope); … … 111 132 return t.report(); 112 133 }; 113 134 135 /** 136 Raised when an assertion fails. 137 **/ 114 138 mod.AssertFailed=Class(mod.Exception, function(publ,supr){ 115 139 publ.__init__=function(comment, failMsg){ … … 119 143 }; 120 144 }); 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 **/ 122 152 mod.assert=function(comment, value, failMsg){ 123 153 if(typeof comment == 'boolean'){ … … 126 156 comment =''; 127 157 } 128 158 129 159 if(value!==true){ 130 160 throw new mod.AssertFailed(comment, failMsg===undefined ? "Expected true but found: %s".format(value) : failMsg); 131 161 } 132 162 }; 133 163 164 /** 165 Assert for true; 166 @param comment='' A comment for the assertion. 167 @param value A boolean to test. 168 **/ 134 169 mod.assertTrue=function(comment, value){ 135 170 if(arguments.length==1){ … … 137 172 comment =''; 138 173 } 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 **/ 142 182 mod.assertFalse=function(comment, value){ 143 183 if(arguments.length==1){ … … 145 185 comment =''; 146 186 } 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 **/ 150 195 mod.assertEquals=function(comment, value1, value2){ 151 196 if(arguments.length==2){ … … 154 199 comment =''; 155 200 } 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 163 204 mod.assertNotEquals=function(comment, value1, value2){ 164 205 if(arguments.length==2){ … … 167 208 comment =''; 168 209 } 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 176 213 mod.assertNull=function(comment, value){ 177 214 if(arguments.length==1){ … … 181 218 mod.assert(comment, value===null, "Expected %s === null.".format(value)); 182 219 }; 183 220 184 221 mod.assertNotNull=function(comment, value){ 185 222 if(arguments.length==1){ … … 189 226 mod.assert(comment, value !==null, "Expected %s !== null.".format(value)); 190 227 }; 191 228 192 229 mod.assertUndefined=function(comment, value){ 193 230 if(arguments.length==1){ … … 197 234 mod.assert(comment, value===undefined, "Expected %s === undefined.".format(value)); 198 235 }; 199 236 200 237 mod.assertNotUndefined=function(comment, value){ 201 238 if(arguments.length==1){ … … 205 242 mod.assert(comment, value!==undefined, "Expected %s !== undefined".format(value)); 206 243 }; 207 244 208 245 mod.assertNaN=function(comment, value){ 209 246 if(arguments.length==1){ … … 213 250 mod.assert(comment, isNaN(value)===true, "Expected %s === NaN.".format(value)); 214 251 }; 215 252 216 253 mod.assertNotNaN=function(comment, value){ 217 254 if(arguments.length==1){ … … 221 258 mod.assert(comment, isNaN(value)!==true, "Expected %s !== NaN".format(value)); 222 259 }; 223 260 224 261 mod.fail=function(comment){ 225 262 throw new mod.AssertFailed(comment, "Fail was called"); 226 263 }; 227 264 228 265 mod.objectKeys=function(obj){ 229 266 var keys=[]; … … 233 270 return keys; 234 271 }; 235 272 236 273 mod.__main__=function(){ 237 274 print(mod.test(function(){
