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.util.ObjectUtil;
    19  import org.as2lib.util.StringUtil;
    20  import org.as2lib.env.reflect.ClassInfo;
    21  
    22  /**
    23   * Information holder and examiner of a assertInstanceOf call.
    24   * 
    25   * @author Martin Heidegger.
    26   */
    27  class org.as2lib.test.unit.info.AssertInstanceOfInfo extends AbstractAssertInfo {
    28  	
    29  	/** Internal holder of the variable value. */
    30  	private var val;
    31  	
    32  	/** Internal holder of the type value. */
    33  	private var type:Function;
    34  	
    35  	/**
    36  	 * Constructs a new AssertInstanceOfInfo.
    37  	 * 
    38  	 * @param message Message if the assertion fails.
    39  	 * @param val Value to be checked.
    40  	 * @param type Type of the value.
    41  	 */
    42  	public function AssertInstanceOfInfo(message:String, val, type:Function) {
    43  		super(message);
    44  		this.val = val;
    45  		this.type = type;
    46  	}
    47  	
    48  	/**
    49  	 * Overriding of @see AbstractAssertInfo#execute
    50  	 * 
    51  	 * @return True if the execution fails.
    52  	 */
    53  	public function execute(Void):Boolean {
    54  		return (!(ObjectUtil.typesMatch(val, type)));
    55  	}
    56  	
    57  	/**
    58  	 * Implementation of @see AbstractAssertInfo#getFailureMessage
    59  	 * 
    60  	 * @return Message on failure
    61  	 */
    62  	private function getFailureMessage(Void):String {
    63  		var result:String = "assertTypeOf failed";
    64  		if(hasMessage()) {
    65  			result += " with message: "+message;
    66  		}
    67  		result += "!\n"+StringUtil.addSpaceIndent("'"+val+"' is not a instance of '"+ClassInfo.forClass(type).getFullName()+"'", 2);
    68  		return result;
    69  	}
    70  	
    71  	
    72  	/**
    73  	 * Implementation of @see AbstractAssertInfo#getSuccessMessage
    74  	 * 
    75  	 * @return Message on success
    76  	 */
    77  	private function getSuccessMessage(Void):String {
    78  		return ("assertInstanceOf executed.\n"+StringUtil.addSpaceIndent("'"+val+"' is a instance of '"+ClassInfo.forClass(type).getFullName()+"'.", 2));
    79  	}
    80  }