/*
 Copyright (c) 2006 - 2009  Eric J. Feminella  <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.

 @internal
 */

package com.ericfeminella.tests
{
    import com.ericfeminella.collections.Collection;
    import com.ericfeminella.collections.Iterator;
    import flexunit.framework.TestCase;

    /**
     *
     * <code>TestSuiteFactoryClient</code> provides a convenience class which
     * determines if item in an <code>Array</code> is of type <code>TestCase</code>
     *
     * @example The following example demonstrates how a typical implementation
     * of <code>TestSuiteFactoryClient</code>, whereas a Class, "AllTests" extends
     * <code>TestSuiteFactoryClient</code> in order to provide a collection of
     * <code>TestCase</code> objects to <code>TestSuiteFactory.createSuites</code>
     *
     * <listing version="3.0">
     *
     * public class AllTests extends TestSuiteFactoryClient
     * {
     *      private static const ALL_TESTS:Array = [ TestA,
     *                                               TestB,
     *                                               TestC
     *                                             ];
     *      public function AllTests()
     *      {
     *          super( ALL_TESTS );
     *      }
     * }
     *
     * </listing>
     *
     */
    public class TestSuiteFactoryClient
    {
        /**
         *
         * Defines the collection of <code>TestCase</code> objects in which
         * to pass to <code>TestSuiteFactory.createSuites</code>
         *
         */
        protected var source:Collection;

        /**
         *
         * <code>TestSuiteFactoryClient</code> constructor creates a new collection
         * which references the <code>Array</code> of <code>TestCase</code> objects
         *
         * @param <code>Array</code> of <code>TestCase</code> objects
         *
         */
        public function TestSuiteFactoryClient(testsArray:Array)
        {
            source = new Collection( testsArray );
            validateTests();
        }

        /**
         *
         * Returns a <code>Collection</code> of <code>TestCase</code> objects
         *
         * @return <code>Collection</code>
         *
         */
        public function get allTests() : Collection
        {
            return source;
        }

        /**
         *
         * Validates that each item specified in the <code>Array</code> passed to the
         * constructor is of type, <code>TestCase</code>. If an item specified in the
         * <code>Array</code> is not of type <code>TestCase</code>, an exception is
         * thrown.
         *
         * throws <code>Exception</code>
         *
         */
        protected function validateTests() : void
        {
            var it:Iterator = source.iterator;
            var testCase:TestCase;
            var TestClass:Class;

            while ( it.hasNext() )
            {
                try {
                    TestClass = it.next() as Class;
                    testCase = new TestClass();
                }
                catch (error:Error )
                {
                    throw new Error( "item must be of type TestCase" );
                }
            }
            it.reset();
        }
    }
}