1  /**
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.test.unit.AbstractAssertInfo;
    18  import org.as2lib.app.exec.Executable;
    19  import org.as2lib.util.StringUtil;
    20  import org.as2lib.env.reflect.ClassInfo;
    21  
    22  /**
    23   * Information holder and examiner of a assertThrows call.
    24   * 
    25   * @author Martin Heidegger.
    26   */
    27  class org.as2lib.test.unit.info.AssertThrowsInfo extends AbstractAssertInfo {
    28  	
    29  	/** Holder for the Exception type. */
    30  	private var type;
    31  	
    32  	/** Holder for the call to be executed. */
    33  	private var toCall:Executable;
    34  	
    35  	/** Holder for the arguments for the call. */
    36  	private var args:Array;
    37  	
    38  	/** Holder for a thrown exception. */
    39  	private var exception;
    40  	
    41  	/** Flag if a exception was thrown. */
    42  	private var exceptionThrown:Boolean = false;
    43  	
    44  	/**
    45  	 * Constructs a new AssertThrowsInfo.
    46  	 * 
    47  	 * @param message Message if the assertion fails.
    48  	 * @param type Exception type that should be thrown. (if null is given, it fails if no exception was thrown).
    49  	 * @param toCall Call to be executed
    50  	 * @param args Arguments for the Call.
    51  	 */
    52  	public function AssertThrowsInfo(message:String, type, toCall:Executable, args:Array) {
    53  		super(message);
    54  		this.type = type;
    55  		this.toCall = toCall;
    56  		this.args = args;
    57  	}
    58  	
    59  	/**
    60  	 * Overriding of @see AbstractAssertInfo#execute
    61  	 * 
    62  	 * @return True if the execution fails.
    63  	 */
    64  	public function execute(Void):Boolean {
    65  		try {
    66  			// cat to Object because of Flash compiler bug with interfaces
    67  			toCall.execute.apply(Object(toCall), args);
    68  		} catch(e) {
    69  			exception = e;
    70  			exceptionThrown = true;
    71  			if(type != null) {
    72  				return(!(e instanceof type));
    73  			} else {
    74  				return false;
    75  			}
    76  		}
    77  		return true;
    78  	}
    79  	
    80  	/**
    81  	 * Implementation of @see AbstractAssertInfo#getFailureMessage
    82  	 * 
    83  	 * @return Message on failure
    84  	 */
    85  	private function getFailureMessage(Void):String {
    86  		var result:String = "assertThrows failed";
    87  		if(hasMessage()) {
    88  			result += " with message: "+message;
    89  		}
    90  		if(type == null) {
    91  			result += "!\n  No exception thrown - Any exception expected";
    92  		} else {
    93  			result += "!\n  - Expected exception:\n      ";
    94  			if(typeof type == "function") {
    95  				result += ClassInfo.forClass(type).getFullName();
    96  			} else {
    97  				result += type;
    98  			}
    99  			if(exceptionThrown) {
   100  				result += "\n  - Thrown exception:\n"+StringUtil.addSpaceIndent(exception.toString(), 6);
   101  			} else {
   102  				result += "\n  - No exception thrown.";
   103  			}
   104  		}
   105  		return result;
   106  	}
   107  	
   108  	/**
   109  	 * Implementation of @see AbstractAssertInfo#getSuccessMessage
   110  	 * 
   111  	 * @return Message on success
   112  	 */
   113  	private function getSuccessMessage(Void):String {
   114  		var result:String = "assertThrows executed. ";
   115  		
   116  		if(typeof type == "function") {
   117  			result += ClassInfo.forClass(type).getFullName();
   118  		} else {
   119  			result += type;
   120  		}
   121  		
   122  		result += "was thrown by calling "+toCall.toString()+".";
   123  		
   124  		return result;
   125  	}
   126  }