Skip to content
Dima Kruk edited this page Jan 22, 2013 · 3 revisions

Assertion is a language extension, that enables assertation syntax in ActionScript3.

Using assertion statement enables you to check various custom assumptions about your program. For example, if you write a method, that calculates the arithmetical square root, you might assert that the input parameter cannot be negative. Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw the assertion exception.

##Syntax

  • assert expression

The purpose of assertation statement is to provide the quickest and most effective ways to detect and correct bugs. By verifying that the boolean expression is indeed true, the assertion confirms your assumptions about the behavior of your program, increasing your confidence that the program is free of errors.

public static function testAssert(a : Number) : Number { 
  assert a >= 0; // throw an exception if the argument was negative
  return Math.sqrt(a); 
}
  • assert expression : message

Please note, that exception message will appear on the screen in only Debugger version of the Flash Player. In the Standalone Flash Player you’ll get only the logging message (in Code Orchestra double click on such a message will lead you to the line, that has invoked it). You can customize such message view using an extended syntax of assert expression.

public static function testAssert(a : Number) : Number { 
  assert a >= 0 : "Wrong parameter in testAssert()!!!"; 
  return Math.sqrt(a); 
}
  • time-stamp<name> … delay<name>

Time-stamp is a useful expression included in the Assertion language extension, that lets you evaluate the execution time of a code.

public static function methodA() : void { 
  time-stamp<myStamp>; 
  methodB(); 
  assert delay<myStamp> < 200; // delay should be less than 200 ms 
}
      
public static function methodB() : void {
  var s : String = ""; 
  while ((s += " ").length < 1000000) { 
    // a very heavy method indeed 
  } 
}
Clone this wiki locally