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.TestCase;
    18  import org.as2lib.test.unit.TestCaseRunner;
    19  
    20  /**
    21   * {@code TestCaseHelper} is a class that helps writing {@link TestCase}s.
    22   * <p>A {@code TestCase} allows proper execution for any kind of test. But if
    23   * you want to refactor/break apart your code and simplify your test it disallows
    24   * you to handle assertions.
    25   * 
    26   * <p>A common example is if you want to test for a interface. Any implementation
    27   * has to match the rules of the interface. A standardised test for the interface
    28   * would therefore be a helper. Code could look like this:
    29   * 
    30   * <p>Interface
    31   * <code>
    32   *   interface MyInterface {
    33   *   	public function addInstance(obj):Void;
    34   *   	public function containsInstance(obj):Void;
    35   *   	public function removeInstance(obj):Void;
    36   *   }
    37   * </code>
    38   * 
    39   * <p>Implementation #1
    40   * <code>
    41   *   import org.as2lib.util.ArrayUtil;
    42   * 
    43   *   class MyImplementationA implements MyInterface {
    44   *   	private var array:Array;
    45   *   	
    46   *   	public fucntion MyImplementationA() {
    47   *   		array = new Array();
    48   *   	}
    49   *   	
    50   *   	public function addInstance(obj):Void {
    51   *   		removeInstance(obj);
    52   *   		array.push(obj);
    53   *   	}
    54   *   	
    55   *   	public function removeInstance(obj):Void {
    56   *   		ArrayUtil.removeElement(array, obj);
    57   *   	}
    58   *   	
    59   *   	public function containsInstance(obj):Boolean {
    60   *   		for (var i:Number = 0; i < array.length; i++) {
    61   *   			if (array[i] === obj) {
    62   *   				return true;
    63   *   			}
    64   *   		}
    65   *   		return false;
    66   *   	}
    67   *   }
    68   * </code>
    69   * 
    70   * <p>Implementation #2
    71   * <code>
    72   *   class MyImplementationB extends MyImplementationA {
    73   *   	public function containsInstance(obj):Boolean {
    74   *   		for (var i:Number = array.length-1; i > 0; i++) {
    75   *   			if (array[i] === obj) {
    76   *   				return true;
    77   *   			}
    78   *   		}
    79   *   		return false;
    80   *   	}
    81   *   }
    82   * </code>
    83   * 
    84   * <p>Test for the Interface
    85   * <code>
    86   *   import org.as2lib.test.unit.TestCaseHelper;
    87   *   import org.as2lib.test.mock.MockControl;
    88   *   
    89   *   class TMyInterface extends TestCaseHelper {
    90   *   	public function testAccess(instance:MyInterface) {
    91   *   		var mC:MockControl = new MockControl(Object);
    92   *   		mC.replay();
    93   *   		assertfalse("Instance should not be contained",	
    94   *   			instance.containsListener(mC.getMock()));
    95   *   		instance.addInstance(mC.getMock());
    96   *   		assertTrue("Instance should be contained",
    97   *   			instance.containsListener(mC.getMock()));
    98   *   		instance.removeInstance(mC.getMock());
    99   *   		assertfalse("Instance should not be contained",
   100   *   			instance.containsListener(mC.getMock()));
   101   *   		instance.addInstance(mC.getMock());
   102   *   		instance.addInstance(mC.getMock());
   103   *   		instance.removeInstance(mC.getMock());
   104   *   		assertfalse("Instance should not be contained after double adding",
   105   *   			instance.containsListener(mC.getMock()));
   106   *   		mC.verify();
   107   *   	}
   108   *   }
   109   * </code>
   110   * 
   111   * <p>Test for Implementation A
   112   * <code>
   113   * 	 import org.as2lib.test.unit.TestCase;
   114   * 	 
   115   * 	 class TMyImplementationA extends TestCase {
   116   * 	 
   117   * 	 	public function testMyInterface() {
   118   * 	 		var i:TMyInterface = new TMyInterface(this);
   119   * 	 		i.testAccess(new MyImplementationA());
   120   * 	 	}
   121   * 	 }
   122   * </code>
   123   * 
   124   * <p>Test for Implementation B
   125   * <code>
   126   * 	 import org.as2lib.test.unit.TestCase;
   127   * 	 
   128   * 	 class TMyImplementationB extends TestCase {
   129   * 	 
   130   * 	 	public function testMyInterface() {
   131   * 	 		var i:TMyInterface = new TMyInterface(this);
   132   * 	 		i.testAccess(new MyImplementationB());
   133   * 	 	}
   134   * 	 }
   135   * </code>
   136   * 
   137   * @author Martin Heidegger
   138   * @version 1.1
   139   */
   140  class org.as2lib.test.unit.TestCaseHelper extends TestCase {
   141  	
   142  	/** Reference testCase to work with */
   143  	private var testCase:TestCase;
   144  	
   145  	/**
   146  	 * Flag to hide from getting collected by
   147  	 * {@link org.as2lib.test.unit.TestSuiteFactory#collectAllTestCases}
   148  	 * 
   149  	 * @return true to block collecting
   150  	 */
   151  	public static function blockCollecting(Void):Boolean {
   152  		return true;
   153  	}
   154  	
   155  	/**
   156  	 * Constructs a new {@code TestCaseHelper} instance.
   157  	 * 
   158  	 * @param testCase {@code TestCase} instance that should be used with all
   159  	 *        assertions.
   160  	 */
   161  	public function TestCaseHelper(testCase:TestCase) {
   162  		this.testCase = testCase;
   163  		testRunner = TestCaseRunner(testCase.getTestRunner());
   164  	}
   165  }