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.StringUtil;
    19  
    20  /**
    21   * Information holder and examiner of a assertAlmostEquals call.
    22   * 
    23   * @author Martin Heidegger.
    24   */
    25  class org.as2lib.test.unit.info.AssertAlmostEqualsInfo extends AbstractAssertInfo {
    26  	
    27  	/** Internal holder of the variable value. */
    28  	private var val:Number;
    29  	
    30  	/** Internal holder of the value to compare. */
    31  	private var compareTo:Number;
    32  	
    33  	/** Internal holder of the maximal difference between those two values. */
    34  	private var maxDiff:Number;
    35  	
    36  	/**
    37  	 * Constructs a new AssertAlmostEqualsInfo.
    38  	 * 
    39  	 * @param message Message if the assertion fails.
    40  	 * @param val Value to be compared.
    41  	 * @param maxDiff Maximum difference between those two numbers.
    42  	 * @param compareTo Value to be compared with.
    43  	 */
    44  	public function AssertAlmostEqualsInfo(message:String, val:Number, compareTo:Number, maxDiff:Number) {
    45  		super(message);
    46  		this.val = val;
    47  		this.compareTo = compareTo;
    48  		this.maxDiff = maxDiff;
    49  	}
    50  	
    51  	/**
    52  	 * Overriding of @see AbstractAssertInfo#execute
    53  	 * 
    54  	 * @return True if the execution fails.
    55  	 */
    56  	public function execute(Void):Boolean {
    57  		return ( (val > compareTo && val-maxDiff > compareTo) || (val < compareTo && val+maxDiff < compareTo) );
    58  	}
    59  	
    60  	/**
    61  	 * Implementation of @see AbstractAssertInfo#getFailureMessage
    62  	 * 
    63  	 * @return Message on failure
    64  	 */
    65  	private function getFailureMessage(Void):String {
    66  		var result:String = "assertAlmostEquals failed";
    67  		if(hasMessage()) {
    68  			result += " with message: "+message;
    69  		}
    70  		if(val > compareTo) {
    71  			result += "!\n"+StringUtil.addSpaceIndent(val+" > "+compareTo+" and even "+val+" - "+maxDiff+" > "+compareTo, 2);
    72  		} else {
    73  			result += "!\n"+StringUtil.addSpaceIndent(val+" < "+compareTo+" and even "+val+" + "+maxDiff+" < "+compareTo, 2);
    74  		}
    75  		return result;
    76  	}
    77  	
    78  	
    79  	/**
    80  	 * Implementation of @see AbstractAssertInfo#getSuccessMessage
    81  	 * 
    82  	 * @return Message on success
    83  	 */
    84  	private function getSuccessMessage(Void):String {
    85  		return ("assertAlmostEquals executed. \n"+StringUtil.addSpaceIndent(val+" ~(+/-"+maxDiff+") "+compareTo, 2));
    86  	}
    87  }