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.util.StringUtil;
    18  import org.as2lib.test.speed.TestSuiteResult;
    19  import org.as2lib.test.speed.ConfigurableTestSuiteResult;
    20  import org.as2lib.test.speed.AbstractTestSuiteResult;
    21  import org.as2lib.test.speed.TestResult;
    22  import org.as2lib.test.speed.MethodInvocationHolder;
    23  
    24  /**
    25   * {@code SimpleTestSuiteResult} holds the results of all tests contained by a test suite.
    26   * 
    27   * @author Simon Wacker
    28   */
    29  class org.as2lib.test.speed.SimpleTestSuiteResult extends AbstractTestSuiteResult implements ConfigurableTestSuiteResult {
    30  	
    31  	/** Makes the static variables of the super-class accessible through this class. */
    32  	private static var __proto__:Function = AbstractTestSuiteResult;
    33  	
    34  	/** Name of this result. */
    35  	private var name:String;
    36  	
    37  	/**
    38  	 * Constructs a new {@code SimpleTestSuiteResult} instance.
    39  	 * 
    40  	 * @param name the name of this result
    41  	 */
    42  	public function SimpleTestSuiteResult(name:String) {
    43  		this.name = name;
    44  	}
    45  	
    46  	/**
    47  	 * Returns the name of this test result.
    48  	 * 
    49  	 * @return the name of this test result
    50  	 */
    51  	public function getName(Void):String {
    52  		return this.name;
    53  	}
    54  	
    55  	/**
    56  	 * Returns the total invocation time in milliseconds.
    57  	 * 
    58  	 * @return the total invocation time in milliseconds
    59  	 */
    60  	public function getTime(Void):Number {
    61  		var result:Number = 0;
    62  		for (var i:Number = 0; i < this.testResults.length; i++) {
    63  			var testResult:TestResult = this.testResults[i];
    64  			result += testResult.getTime();
    65  		}
    66  		return result;
    67  	}
    68  	
    69  	/**
    70  	 * Returns the string representation of this test suite result. This includes the
    71  	 * string representation of all added test results.
    72  	 * 
    73  	 * @param rootTestResult test result that holds the total values needed for
    74  	 * percentage calculations
    75  	 * @return the string representation of this test suite result
    76  	 */
    77  	public function toString():String {
    78  		var rootTestResult:TestSuiteResult = TestSuiteResult(arguments[0]);
    79  		if (!rootTestResult) rootTestResult = getThis();
    80  		var result:String = getTimePercentage(rootTestResult.getTime()) + "%";
    81  		result += ", " + getThis().getTime() + " ms";
    82  		result += " - " + getMethodInvocationPercentage(rootTestResult.getMethodInvocationCount()) + "%";
    83  		result += ", " + getMethodInvocationCount() + " inv.";
    84  		result += " - " + getAverageTime() + " ms/inv.";
    85  		if (getTestResultCount() == 1 && !(this.testResults[0] instanceof TestSuiteResult)) {
    86  			result += " - " + this.testResults[0].getName();
    87  		} else if (getTestResultCount() == 1 && !TestSuiteResult(this.testResults[0]).hasMethodInvocations()) {
    88  			result += " - " + this.testResults[0].getName();
    89  		} else {
    90  			result += " - " + getThis().getName();
    91  			var totalTime:Number = getThis().getTime();
    92  			for (var i:Number = 0; i < this.testResults.length; i++) {
    93  				var testResult:TestResult = this.testResults[i];
    94  				if (TestSuiteResult(testResult).hasMethodInvocations()
    95  						|| !(testResult instanceof TestSuiteResult)
    96  						|| testResult instanceof MethodInvocationHolder) {
    97  					result += "\n";
    98  					result += StringUtil.addSpaceIndent(testResult.toString(rootTestResult), 2);
    99  				}
   100  			}
   101  		}
   102  		return result;
   103  	}
   104  	
   105  }