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.env.overload.Overload;
    18  import org.as2lib.env.reflect.MethodInfo;
    19  import org.as2lib.env.reflect.PropertyInfo;
    20  import org.as2lib.test.speed.Test;
    21  import org.as2lib.test.speed.AbstractTest;
    22  import org.as2lib.test.speed.SimpleTestSuiteResult;
    23  import org.as2lib.test.speed.MethodTestCase;
    24  import org.as2lib.test.speed.PropertyTestCase;
    25  
    26  /**
    27   * {@code TestSuite} is the core interface for standardized performance test suites.
    28   * 
    29   * <p>Test suites hold multiple {@code Test} instances, including {@code TestCase},
    30   * instances, {@code TestSuite} instances and instances of your custom {@code Test}
    31   * implementations.
    32   *
    33   * @author Simon Wacker
    34   * @author Martin Heidegger
    35   */
    36  class org.as2lib.test.speed.TestSuite extends AbstractTest implements Test {
    37  	
    38  	/** Makes the static variables of the super-class accessible through this class. */
    39  	private static var __proto__:Function = AbstractTest;
    40  	
    41  	/** This test suite's name. */
    42  	private var name:String;
    43  	
    44  	/** All added tests. */
    45  	private var tests:Array;
    46  	
    47  	/**
    48  	 * Constructs a new {@code TestSuite} instance.
    49  	 * 
    50  	 * <p>If the passed-in {@code tests} array contains elements that are not of type
    51  	 * {@code Test}, they will be ignored.
    52  	 * 
    53  	 * @param name the name of this test suite
    54  	 * @param tests (optional) {@code Test} instances to populate this test suite with
    55  	 */
    56  	public function TestSuite(name:String, tests:Array) {
    57  		this.name = name;
    58  		this.tests = new Array();
    59  		setResult(new SimpleTestSuiteResult(name));
    60  		for (var i:Number = 0; i < tests.length; i++) {
    61  			addTest(Test(tests[i]));
    62  		}
    63  	}
    64  	
    65  	/**
    66  	 * Runs all added tests.
    67  	 */
    68  	public function run(Void):Void {
    69  		for (var i:Number = 0; i < this.tests.length; i++) {
    70  			var test:Test = this.tests[i];
    71  			test.run();
    72  		}
    73  	}
    74  	
    75  	/**
    76  	 * Returns this test suite's name.
    77  	 * 
    78  	 * @return this test suite's name
    79  	 */
    80  	public function getName(Void):String {
    81  		return this.name;
    82  	}
    83  	
    84  	/**
    85  	 * @overload #addTestByTest
    86  	 * @overload #addTestByMethod
    87  	 * @overload #addTestByProperty
    88  	 */
    89  	public function addTest() {
    90  		var o:Overload = new Overload(this);
    91  		o.addHandler([Test], addTestByTest);
    92  		o.addHandler([MethodInfo], addTestByMethod);
    93  		o.addHandler([PropertyInfo], addTestByProperty);
    94  		return o.forward(arguments);
    95  	}
    96  	
    97  	/**
    98  	 * Adds the passed-in {@code test} to this test suite.
    99  	 * 
   100  	 * <p>If the argument {@code test} is {@code null} or {@code undefined} this method
   101  	 * invocation will be ignored.
   102  	 * 
   103  	 * @param test the test to add
   104  	 */
   105  	public function addTestByTest(test:Test):Void {
   106  		if (test) {
   107  			this.tests.push(test);
   108  			this.result.addTestResult(test.getResult(NONE));
   109  		}
   110  	}
   111  	
   112  	/**
   113  	 * Adds a new test case by {@code method}.
   114  	 * 
   115  	 * <p>If the argument {@code method} is {@code null} or {@code undefined} this
   116  	 * method invocation will be ignored and {@code null} will be returned.
   117  	 * 
   118  	 * @param method the method to profile
   119  	 * @return the created and added test
   120  	 */
   121  	public function addTestByMethod(method:MethodInfo):Test {
   122  		if (method) {
   123  			var test:MethodTestCase = new MethodTestCase(method);
   124  			addTestByTest(test);
   125  			return test;
   126  		}
   127  		return null;
   128  	}
   129  	
   130  	/**
   131  	 * Adds a new test case by {@code property}.
   132  	 * 
   133  	 * <p>If the argument {@code property} is {@code null} or {@code undefined} this
   134  	 * method invocation will be ignored and {@code null} will be returned.
   135  	 * 
   136  	 * @param property the property to profile
   137  	 * @return the created and added test
   138  	 */
   139  	public function addTestByProperty(property:PropertyInfo):Test {
   140  		if (property) {
   141  			var test:PropertyTestCase = new PropertyTestCase(property);
   142  			addTestByTest(test);
   143  			return test;
   144  		}
   145  		return null;
   146  	}
   147  	
   148  	/**
   149  	 * Returns all tests of this test suite.
   150  	 * 
   151  	 * @return all tests of this test suite
   152  	 */
   153  	public function getTests(Void):Array {
   154  		return this.tests.concat();
   155  	}
   156  	
   157  }