Assertion.as

Go to the documentation of this file.
00001 import gugga.collections.ArrayList;
00002 import gugga.collections.CheckList;
00003 import gugga.collections.HashTable;
00004 import gugga.collections.LinkedList;
00005 import gugga.collections.ObjectHashTable;
00006 import gugga.logging.Level;
00007 import gugga.logging.Logger;
00008 import gugga.utils.DebugUtils;
00009 
00013 dynamic class gugga.debug.Assertion 
00014 {
00015         private static function logAssertion(aLogLevel : Level, aMessage:String, aContextObject:Object, aContextMethodArguments:Array)
00016         {
00017                 var logger : Logger = Logger.getLoggerFor(aContextObject);              
00018                 var contextString : String = DebugUtils.getCallContextString(aContextObject, aContextMethodArguments);
00019         
00020                 var logString : String = aMessage + " [" + contextString + "]";
00021                 
00022                 logger.log(aLogLevel, logString);
00023         }
00024         
00025         private static function evaluateMethod(aScope:Object, aMethod:Function, aArguments:Array) : Object
00026         {
00027                 if(aArguments == null || aArguments == undefined)
00028                 {
00029                         aArguments = new Array();
00030                 }
00031                 
00032                 return aMethod.apply(aScope, aArguments);
00033         }
00034         
00035         private static function containsValue(aCollection:Object, aValue:Object) : Boolean
00036         {
00037                 if(aCollection instanceof LinkedList)
00038                 {
00039                         return LinkedList(aCollection).contains(aValue);
00040                 }
00041                 else if(aCollection instanceof HashTable)
00042                 {
00043                         return HashTable(aCollection).containsValue(aValue);
00044                 }
00045                 else if(aCollection instanceof ObjectHashTable)
00046                 {
00047                         return ObjectHashTable(aCollection).containsValue(aValue);
00048                 }
00049                 else if(aCollection instanceof ArrayList)
00050                 {
00051                         return ArrayList(aCollection).containsItem(aValue);
00052                 }
00053                 else if(aCollection instanceof CheckList)
00054                 {
00055                         return CheckList(aCollection).isObjectRegistered(aValue);
00056                 }
00057                 else
00058                 {
00059                         for (var key:String in aCollection)
00060                         {
00061                                 if(aCollection[key] === aValue)
00062                                 {
00063                                         return true;
00064                                 }
00065                         }
00066                         
00067                         return false;
00068                 }
00069         }
00070         
00071         private static function containsKey(aCollection:Object, aKey:Object) : Boolean
00072         {
00073                 if(aCollection instanceof HashTable)
00074                 {
00075                         return HashTable(aCollection).containsKey(aKey);
00076                 }
00077                 else if(aCollection instanceof ObjectHashTable)
00078                 {
00079                         return ObjectHashTable(aCollection).containsKey(aKey);
00080                 }
00081                 else
00082                 {
00083                         for (var key:String in aCollection)
00084                         {
00085                                 if(key === aKey)
00086                                 {
00087                                         return true;
00088                                 }
00089                         }
00090                         
00091                         return false;
00092                 }
00093         }
00094         
00095         //------------ FAIL
00096         
00097         public static function fail(aMessage:String, aContextObject:Object, aContextMethodArguments:Array) : Void
00098         {
00099                 logAssertion(Level.SEVERE, aMessage, aContextObject, aContextMethodArguments);
00100                 throw new Error(aMessage);
00101         }
00102         
00103         public static function failIfTrue(aVar:Boolean, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00104         {
00105                 if(aVar)
00106                 {
00107                         fail(aMessage, aContextObject, aContextMethodArguments);
00108                 }
00109         }
00110         
00111         public static function failIfFalse(aVar:Boolean, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00112         {
00113                 if(!aVar)
00114                 {
00115                         fail(aMessage, aContextObject, aContextMethodArguments);
00116                 }
00117         }
00118         
00119         public static function failIfNotNull(aVar:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00120         {
00121                 if(aVar != null && aVar != undefined)
00122                 {
00123                         fail(aMessage, aContextObject, aContextMethodArguments);
00124                 }
00125         }
00126         
00127         public static function failIfNull(aVar:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00128         {
00129                 if(aVar == null || aVar == undefined)
00130                 {
00131                         fail(aMessage, aContextObject, aContextMethodArguments);
00132                 }
00133         }
00134         
00135         public static function failIfEmpty(aVar:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00136         {
00137                 if(aVar == null || aVar == undefined || aVar == "")
00138                 {
00139                         fail(aMessage, aContextObject, aContextMethodArguments);
00140                 }
00141         }
00142         
00143         public static function failIfNotEqual(aVar1:Object, aVar2:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00144         {
00145                 if(aVar1 != aVar2)
00146                 {
00147                         fail(aMessage + "(" + aVar1 + " != " + aVar2 + ")", aContextObject, aContextMethodArguments);
00148                 }
00149         }
00150         
00151         public static function failIfNotSame(aVar1:Object, aVar2:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00152         {
00153                 if(aVar1 !== aVar2)
00154                 {
00155                         fail(aMessage + "(" + aVar1 + " !== " + aVar2 + ")", aContextObject, aContextMethodArguments);
00156                 }
00157         }
00158 
00159         public static function failIfNotInstanceOf(aVar:Object, aType:Function, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00160         {
00161                 if(!(aVar instanceof aType))
00162                 {
00163                         fail(aMessage, aContextObject, aContextMethodArguments);
00164                 }
00165         }
00166         
00167         public static function failIfReturns(aScope:Object, aMethod:Function, aArguments:Array, aReturnValue:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00168         {
00169                 if(evaluateMethod(aScope, aMethod, aArguments) === aReturnValue)
00170                 {
00171                         fail(aMessage, aContextObject, aContextMethodArguments);
00172                 }
00173         }
00174 
00175         public static function failIfReturnsTrue(aScope:Object, aMethod:Function, aArguments:Array, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00176         {
00177                 failIfReturns(aScope, aMethod, aArguments, true, aMessage, aContextObject, aContextMethodArguments);
00178         }
00179 
00180         public static function failIfReturnsFalse(aScope:Object, aMethod:Function, aArguments:Array, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00181         {
00182                 failIfReturns(aScope, aMethod, aArguments, false, aMessage, aContextObject, aContextMethodArguments);
00183         }
00184         
00185         public static function failIfReturnsNull(aScope:Object, aMethod:Function, aArguments:Array, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00186         {
00187                 var methodResult = evaluateMethod(aScope, aMethod, aArguments);
00188                 if(methodResult === null || methodResult === undefined)
00189                 {
00190                         fail(aMessage, aContextObject, aContextMethodArguments);
00191                 }
00192         }
00193         
00194         public static function failIfReturnsNotNull(aScope:Object, aMethod:Function, aArguments:Array, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00195         {
00196                 var methodResult = evaluateMethod(aScope, aMethod, aArguments);
00197                 if(methodResult !== null && methodResult !== undefined)
00198                 {
00199                         fail(aMessage, aContextObject, aContextMethodArguments);
00200                 }
00201         }
00202         
00203         public static function failIfContainsValue(aCollection:Object, aValue:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00204         {
00205                 if(containsValue(aCollection, aValue))
00206                 {
00207                         fail(aMessage, aContextObject, aContextMethodArguments);
00208                 }
00209         }
00210 
00211         public static function failIfContainsKey(aCollection:Object, aKey:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00212         {
00213                 if(containsValue(aCollection, aKey))
00214                 {
00215                         fail(aMessage, aContextObject, aContextMethodArguments);
00216                 }
00217         }
00218         
00219         public static function failIfNotContainsValue(aCollection:Object, aValue:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00220         {
00221                 if(!containsValue(aCollection, aValue))
00222                 {
00223                         fail(aMessage, aContextObject, aContextMethodArguments);
00224                 }
00225         }
00226 
00227         public static function failIfNotContainsKey(aCollection:Object, aKey:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00228         {
00229                 if(!containsValue(aCollection, aKey))
00230                 {
00231                         fail(aMessage, aContextObject, aContextMethodArguments);
00232                 }
00233         }
00234         
00235         //------------ WARNING
00236         
00237         public static function warning(aMessage:String, aContextObject:Object, aContextMethodArguments:Array) : Void
00238         {
00239                 logAssertion(Level.WARNING, aMessage, aContextObject, aContextMethodArguments);
00240         }
00241         
00242         public static function warningIfTrue(aVar:Boolean, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00243         {
00244                 if(aVar)
00245                 {
00246                         warning(aMessage, aContextObject, aContextMethodArguments);
00247                 }
00248         }
00249         
00250         public static function warningIfFalse(aVar:Boolean, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00251         {
00252                 if(!aVar)
00253                 {
00254                         warning(aMessage, aContextObject, aContextMethodArguments);
00255                 }
00256         }
00257         
00258         public static function warningIfNotNull(aVar:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00259         {
00260                 if(aVar != null && aVar != undefined)
00261                 {
00262                         warning(aMessage, aContextObject, aContextMethodArguments);
00263                 }
00264         }
00265         
00266         public static function warningIfNull(aVar:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00267         {
00268                 if(aVar == null || aVar == undefined)
00269                 {
00270                         warning(aMessage, aContextObject, aContextMethodArguments);
00271                 }
00272         }
00273         
00274         public static function warningIfEmpty(aVar:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00275         {
00276                 if(aVar == null || aVar == undefined || aVar == "")
00277                 {
00278                         warning(aMessage, aContextObject, aContextMethodArguments);
00279                 }
00280         }
00281         
00282         public static function warningIfNotEqual(aVar1:Object, aVar2:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00283         {
00284                 if(aVar1 != aVar2)
00285                 {
00286                         warning(aMessage + "(" + aVar1 + " != " + aVar2 + ")", aContextObject, aContextMethodArguments);
00287                 }
00288         }
00289         
00290         public static function warningIfNotSame(aVar1:Object, aVar2:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00291         {
00292                 if(aVar1 !== aVar2)
00293                 {
00294                         warning(aMessage + "(" + aVar1 + " !== " + aVar2 + ")", aContextObject, aContextMethodArguments);
00295                 }
00296         }
00297         
00298         public static function warningIfNotInstanceOf(aVar:Object, aType:Function, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00299         {
00300                 if(!(aVar instanceof aType))
00301                 {
00302                         warning(aMessage, aContextObject, aContextMethodArguments);
00303                 }
00304         }
00305         
00306         public static function warningIfReturns(aScope:Object, aMethod:Function, aArguments:Array, aReturnValue:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00307         {
00308                 if(evaluateMethod(aScope, aMethod, aArguments) === aReturnValue)
00309                 {
00310                         warning(aMessage, aContextObject, aContextMethodArguments);
00311                 }
00312         }
00313 
00314         public static function warningIfReturnsTrue(aScope:Object, aMethod:Function, aArguments:Array, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00315         {
00316                 warningIfReturns(aScope, aMethod, aArguments, true, aMessage, aContextObject, aContextMethodArguments);
00317         }
00318 
00319         public static function warningIfReturnsFalse(aScope:Object, aMethod:Function, aArguments:Array, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00320         {
00321                 warningIfReturns(aScope, aMethod, aArguments, false, aMessage, aContextObject, aContextMethodArguments);
00322         }
00323         
00324         public static function warningIfReturnsNull(aScope:Object, aMethod:Function, aArguments:Array, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00325         {
00326                 var methodResult = evaluateMethod(aScope, aMethod, aArguments);
00327                 if(methodResult === null || methodResult === undefined)
00328                 {
00329                         warning(aMessage, aContextObject, aContextMethodArguments);
00330                 }
00331         }
00332         
00333         public static function warningIfReturnsNotNull(aScope:Object, aMethod:Function, aArguments:Array, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00334         {
00335                 var methodResult = evaluateMethod(aScope, aMethod, aArguments);
00336                 if(methodResult !== null && methodResult !== undefined)
00337                 {
00338                         warning(aMessage, aContextObject, aContextMethodArguments);
00339                 }
00340         }
00341         
00342         public static function warningIfContainsValue(aCollection:Object, aValue:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00343         {
00344                 if(containsValue(aCollection, aValue))
00345                 {
00346                         warning(aMessage, aContextObject, aContextMethodArguments);
00347                 }
00348         }
00349 
00350         public static function warningIfContainsKey(aCollection:Object, aKey:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00351         {
00352                 if(containsValue(aCollection, aKey))
00353                 {
00354                         warning(aMessage, aContextObject, aContextMethodArguments);
00355                 }
00356         }
00357         
00358         public static function warningIfNotContainsValue(aCollection:Object, aValue:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00359         {
00360                 if(!containsValue(aCollection, aValue))
00361                 {
00362                         warning(aMessage, aContextObject, aContextMethodArguments);
00363                 }
00364         }
00365 
00366         public static function warningIfNotContainsKey(aCollection:Object, aKey:Object, aMessage:String, aContextObject:Object, aContextMethodArguments:Array):Void
00367         {
00368                 if(!containsValue(aCollection, aKey))
00369                 {
00370                         warning(aMessage, aContextObject, aContextMethodArguments);
00371                 }
00372         }
00373         
00374         //------------------------------------------------------------------
00375         
00376         public static function disableLogging()
00377         {
00378                 Assertion.fail = null;
00379                 Assertion.failIfEmpty = null;
00380                 Assertion.failIfFalse = null;
00381                 Assertion.failIfNotEqual = null;
00382                 Assertion.failIfNotNull = null;
00383                 Assertion.failIfNotSame = null;
00384                 Assertion.failIfNull = null;
00385                 Assertion.failIfTrue = null;
00386                 Assertion.failIfNotInstanceOf = null;
00387                 Assertion.failIfReturns = null;
00388                 Assertion.warningIfReturnsFalse = null;
00389                 Assertion.failIfReturnsTrue = null;
00390                 Assertion.failIfReturnsNull = null;
00391                 Assertion.failIfReturnsNotNull = null;
00392                 Assertion.failIfContainsKey = null;
00393                 Assertion.failIfContainsValue = null;
00394                 Assertion.failIfNotContainsKey = null;
00395                 Assertion.failIfNotContainsValue = null;
00396 
00397                 Assertion.warning = null;
00398                 Assertion.warningIfEmpty = null;
00399                 Assertion.warningIfFalse = null;
00400                 Assertion.warningIfNotEqual = null;
00401                 Assertion.warningIfNotNull = null;
00402                 Assertion.warningIfNotSame = null;
00403                 Assertion.warningIfNull = null;
00404                 Assertion.warningIfTrue = null;
00405                 Assertion.failIfNotInstanceOf = null;
00406                 Assertion.failIfReturns = null;
00407                 Assertion.warningIfReturnsFalse = null;
00408                 Assertion.warningIfReturnsTrue = null;
00409                 Assertion.warningIfReturnsNull = null;
00410                 Assertion.warningIfReturnsNotNull = null;
00411                 Assertion.warningIfContainsKey = null;
00412                 Assertion.warningIfContainsValue = null;
00413                 Assertion.warningIfNotContainsKey = null;
00414                 Assertion.warningIfNotContainsValue = null;
00415         }
00416 }

Generated on Fri May 11 17:12:42 2007 for GuggaFramework by  doxygen 1.5.2