The Assert class assists with validating arguments and is a relief mechanism
for having to write the same logic over and over when checking arguments.
The default error that's thrown when a condition is not met is an
ArgumentError. You can change what error is thrown by providing the exceptionType
parameter.
Example
Using the Assert class:
public function setItems(array:Array, maxCount:int)
{
Assert.NotNullOrEmpty(array, "Parameter array cannot be null or empty");
Assert.NotNull(maxCount, "Parameter maxCount cannot be null");
}
public static function Compatible(object:*, type:Class, message:String, exceptionType:Class = null):void
Check that an object is a compatible instance, or interface of a certain class.
Parameters
| object:* — The object to check.
|
| |
| type:Class — The Class that object needs to be an instance of in order to pass the test.
|
| |
| message:String — A message to use for an ArgumentError.
|
| |
| exceptionType:Class (default = null) — The class of the exception type you want to throw. Default is ArgumentError if not specified.
|
Throws
| — If object is null.
|
| |
| — ArgumentError If message is null.
|
public static function DictionaryKeysOfType(dictionary:Dictionary, type:Class, message:String, exceptionType:Class = null):void
Assert that a dictionary has keys that are only of the specified type.
Parameters
| dictionary:Dictionary — The dictionary to validate.
|
| |
| type:Class — The class type that each key should be.
|
| |
| message:String — A message to use for an Error.
|
| |
| exceptionType:Class (default = null) — The class of the exception type you want to throw. Default is ArgumentError if not specified.
|
Throws
| — ArgumentError If message is null.
|
| |
| — If a key in the dictionary is not of the specified type.
|
public static function False(val:Boolean, message:String, exceptionType:Class = null):Boolean
Assert that the passed value is false.
Parameters
| val:Boolean — The boolean value to assert.
|
| |
| message:String — The message to throw if the assertion is false.
|
| |
| exceptionType:Class (default = null) — The exception type to throw if there is an error.
|
Returns
public static function GreaterThan(target:Number, minimum:Number, message:String, exceptionType:Class = null):void
Check that a target number is greater than a minimum amount.
Parameters
| target:Number — The target number.
|
| |
| minimum:Number — The minimum the target can be.
|
| |
| message:String — A message to use for an ArgumentError.
|
| |
| exceptionType:Class (default = null) — The class of the exception type you want to throw. Default is ArgumentError if not specified.
|
Throws
| — If object is null.
|
| |
| — ArgumentError If message is null.
|
public static function NotNull(object:*, message:String, exceptionType:Class = null):void
Check that the object is not null.
Parameters
| object:* — The object to check.
|
| |
| message:String — A message to use for an ArgumentError.
|
| |
| exceptionType:Class (default = null) — The class of the exception type you want to throw. Default is ArgumentError if not specified.
|
Throws
| — If object is null.
|
| |
| — ArgumentError If message is null.
|
public static function NotNullOrEmpty(array:Array, message:String, exceptionType:Class = null):void
Check to see if an array is null or empty.
Parameters
| array:Array — An array to validate that it's not null, and not empty.
|
| |
| message:String — A message to use for an ArgumentError.
|
| |
| exceptionType:Class (default = null) — The class of the exception type you want to throw. Default is ArgumentError if not specified.
|
Throws
| — If array is null.
|
| |
| — ArgumentError If message is null.
|
public static function State(expression:Boolean, message:String, exceptionType:Class = null):void
Assert a boolean expression. If the expression is false an error is raised.
Parameters
| expression:Boolean — An expression as a boolean.
|
| |
| message:String — A message to use for an Error.
|
| |
| exceptionType:Class (default = null) — The class of the exception type you want to throw. Default is ArgumentError if not specified.
|
Throws
| — If object is null.
|
| |
| — ArgumentError If message is null.
|
public static function True(val:Boolean, message:String, exceptionType:Class = null):Boolean
Assert that the passed value is true.
Parameters
| val:Boolean — The boolean value to assert.
|
| |
| message:String — The message to throw if the assertion is false.
|
| |
| exceptionType:Class (default = null) — The exception type to throw if there is an error.
|
Returns