1/***************************************************************************** 2 * 3 * Copyright (c) 2004-2007 Guido Wesdorp. All rights reserved. 4 * 5 * This software is distributed under the terms of the JSBase 6 * License. See LICENSE.txt for license text. 7 * 8 *****************************************************************************/ 9 10var global = this; 11global.testing = new function() { 12 var testing = this; 13 14 this.DEBUG = true; // set to false to disable debug messages 15 16 this.assert = function(expr, message) { 17 /* raise an exception if expr resolves to false */ 18 if (!testing.DEBUG) { 19 return; 20 }; 21 if (!expr) { 22 if (!message) { 23 message = 'false assertion' 24 } else { 25 message = '' + message; 26 }; 27 if (global.exception && global.exception.AssertionError) { 28 throw(new exception.AssertionError(message)); 29 } else { 30 throw(message); 31 }; 32 }; 33 }; 34 35 this.assertFalse = function(s) { 36 if (s) { 37 throw(new exception.AssertionError('!! ' + misclib.repr(s))); 38 }; 39 }; 40 41 this.assertEquals = function(var1, var2, message) { 42 /* assert whether 2 vars have the same value */ 43 if (var1 != var2 && 44 (!(var1 instanceof Array && var2 instanceof Array) || 45 misclib.repr(var1) != misclib.repr(var2)) && 46 (!(var1 instanceof Date && var2 instanceof Date) || 47 var1.getTime() != var2.getTime())) { 48 throw(new exception.AssertionError('' + misclib.repr(s1) + ' != ' + 49 misclib.repr(s2))); 50 }; 51 }; 52 53 this.assertThrows = function(exctype, callable, context) { 54 /* assert whether a certain exception is raised */ 55 // we changed the argument order here, so an explicit check is the 56 // least we can do ;) 57 if (typeof callable != 'function') { 58 var msg = 'wrong argument type for callable'; 59 if (global.exception) { 60 throw(new exception.ValueError(msg)); 61 } else { 62 throw(msg); 63 }; 64 }; 65 if (!context) { 66 context = null; 67 }; 68 var exception_thrown = false; 69 // remove the first three args, they're the function's normal args 70 var args = []; 71 for (var i=3; i < arguments.length; i++) { 72 args.push(arguments[i]); 73 }; 74 try { 75 callable.apply(context, args); 76 } catch(e) { 77 // allow catching undefined exceptions too 78 if (exctype === undefined) { 79 } else if (exctype) { 80 var isinstance = false; 81 try { 82 if (e instanceof exctype) { 83 isinstance = true; 84 }; 85 } catch(f) { 86 }; 87 if (!isinstance) { 88 if (exctype.toSource && e.toSource) { 89 exctype = exctype.toSource(); 90 e = e.toSource(); 91 }; 92 if (exctype.toString && e.toString) { 93 exctype = exctype.toString(); 94 e = e.toString(); 95 }; 96 if (e != exctype) { 97 throw(new exception.AssertionError( 98 'exception ' + e + 99 ', while expecting ' + exctype)); 100 }; 101 }; 102 }; 103 exception_thrown = true; 104 }; 105 if (!exception_thrown) { 106 if (exctype) { 107 throw(new exception.AssertionError( 108 "function didn\'t raise exception \'" + 109 exctype.toString() + "'")); 110 } else { 111 throw(new exception.AssertionError( 112 "function didn\'t raise exception")); 113 }; 114 }; 115 }; 116 117 this.debug = function(str) { 118 /* append a message to the document with a string */ 119 if (!testing.DEBUG) { 120 return; 121 }; 122 try { 123 var div = document.createElement('pre'); 124 div.appendChild(document.createTextNode('' + str)); 125 div.style.color = 'red'; 126 div.style.borderWidth = '1px'; 127 div.style.borderStyle = 'solid'; 128 div.style.borderColor = 'black'; 129 document.getElementsByTagName('body')[0].appendChild(div); 130 } catch(e) { 131 alert(str); 132 }; 133 }; 134}(); 135 136