Beispiel #1
0
        public void FormatThrowsIfFormatterIsNull()
        {
            ObjectCreationSpec spec = new ObjectCreationSpec(EmptyClassInfo,
                                                             EmptyArray <KeyValuePair <ISlotInfo, object> > .Instance, Mocks.Stub <IConverter>());

            Assert.Throws <ArgumentNullException>(delegate { spec.Format("Foo", null); });
        }
Beispiel #2
0
        public void CreateInstanceWithNonInstantiableClassThrows()
        {
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            ObjectCreationSpec spec = new ObjectCreationSpec(AbstractClassInfo, slotValues, NullConverter.Instance);

            Assert.Throws <InvalidOperationException>(delegate { spec.CreateInstance(); });
        }
        /// <summary>
        /// Applies semantic actions to a test to estalish its runtime behavior.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method is called after <see cref="InitializeTest" />.
        /// </para>
        /// <para>
        /// The default behavior for a <see cref="TestTypePatternAttribute" />
        /// is to configure the test actions as follows:
        /// <list type="bullet">
        /// <item><see cref="PatternTestInstanceActions.BeforeTestInstanceChain" />: Set the
        /// fixture instance name and <see cref="PatternTestInstanceState.FixtureType" />.</item>
        /// <item><see cref="PatternTestInstanceActions.InitializeTestInstanceChain" />: Create
        /// the fixture instance and set the <see cref="PatternTestInstanceState.FixtureInstance" />
        /// property accordingly.</item>
        /// <item><see cref="PatternTestInstanceActions.DisposeTestInstanceChain" />: If the fixture type
        /// implements <see cref="IDisposable" />, disposes the fixture instance.</item>
        /// <item><see cref="PatternTestInstanceActions.DecorateChildTestChain" />: Decorates the child's
        /// <see cref="PatternTestInstanceActions.BeforeTestInstanceChain" /> to set its <see cref="PatternTestInstanceState.FixtureInstance" />
        /// and <see cref="PatternTestInstanceState.FixtureType" /> properties to those
        /// of the fixture.  The child test may override these values later on but this
        /// is a reasonable default setting for test methods within a fixture.</item>
        /// </list>
        /// </para>
        /// <para>
        /// You can override this method to change the semantics as required.
        /// </para>
        /// </remarks>
        /// <param name="testBuilder">The test builder.</param>
        /// <param name="type">The test type.</param>
        protected virtual void SetTestSemantics(ITestBuilder testBuilder, ITypeInfo type)
        {
            testBuilder.TestInstanceActions.BeforeTestInstanceChain.After(
                delegate(PatternTestInstanceState testInstanceState)
            {
                ObjectCreationSpec spec = testInstanceState.GetFixtureObjectCreationSpec(type);
                testInstanceState.Data.SetValue(FixtureObjectCreationSpecKey, spec);

                testInstanceState.FixtureType = spec.ResolvedType;

                if (!testInstanceState.IsReusingPrimaryTestStep)
                {
                    testInstanceState.NameBase = spec.Format(testInstanceState.NameBase, testInstanceState.Formatter);
                }
            });

            testBuilder.TestInstanceActions.InitializeTestInstanceChain.After(
                delegate(PatternTestInstanceState testInstanceState)
            {
                if (!type.IsAbstract && !type.IsInterface)
                {
                    ObjectCreationSpec spec = testInstanceState.Data.GetValue(FixtureObjectCreationSpecKey);

                    testInstanceState.FixtureInstance = spec.CreateInstance();
                }
            });

            testBuilder.TestInstanceActions.DisposeTestInstanceChain.After(
                delegate(PatternTestInstanceState testInstanceState)
            {
                IDisposable dispose = testInstanceState.FixtureInstance as IDisposable;
                if (dispose != null)
                {
                    dispose.Dispose();
                }
            });

            testBuilder.TestInstanceActions.DecorateChildTestChain.After(
                delegate(PatternTestInstanceState testInstanceState, PatternTestActions decoratedTestActions)
            {
                decoratedTestActions.TestInstanceActions.BeforeTestInstanceChain.Before(delegate(PatternTestInstanceState childTestInstanceState)
                {
                    IMemberInfo member = childTestInstanceState.Test.CodeElement as IMemberInfo;
                    if (member != null)
                    {
                        ITypeInfo memberDeclaringType = member.DeclaringType;
                        if (memberDeclaringType != null)
                        {
                            if (type.Equals(memberDeclaringType) || type.IsSubclassOf(memberDeclaringType))
                            {
                                childTestInstanceState.FixtureType     = testInstanceState.FixtureType;
                                childTestInstanceState.FixtureInstance = testInstanceState.FixtureInstance;
                            }
                        }
                    }
                });
            });
        }
Beispiel #4
0
        public void FormatStringIsJustTheEntityIfThereAreNoSlots()
        {
            ITypeInfo type = Reflector.Wrap(typeof(EmptyClass));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.AreEqual("Foo", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>()));
        }
Beispiel #5
0
        public void SpecContainsNullConstructorIfNonInstantiableTypeUsed()
        {
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            ObjectCreationSpec spec = new ObjectCreationSpec(AbstractClassInfo, slotValues, NullConverter.Instance);

            Assert.IsNull(spec.ResolvedConstructor);
            Assert.Count(0, spec.ResolvedConstructorArguments);
        }
Beispiel #6
0
        public void CreateInstance_WhenConstructorThrows_DoesNotWrapExceptionAsTargetInvocationException()
        {
            ITypeInfo type = Reflector.Wrap(typeof(ThrowBoomWhenConstructed));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.Throws <InvalidOperationException>(() => spec.CreateInstance());
        }
Beispiel #7
0
        public void FormatStringShowCommaDelimitedParameters()
        {
            ITypeInfo type = Reflector.Wrap(typeof(NonGenericClass));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[0], 1);
            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[1], "abc");

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.AreEqual("Foo(1, \"abc\")", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>()));
        }
Beispiel #8
0
        public void FormatDescribesTheObject()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[0], 1);
            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.AreEqual("Foo<int>(1): fieldValue=2, Property=3", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>()));
        }
Beispiel #9
0
        public void CreateInstanceWithGenericStructInstantiationDefaultConstructor()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericStruct <int>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec  spec     = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);
            GenericStruct <int> instance = (GenericStruct <int>)spec.CreateInstance();

            Assert.AreEqual(0, instance.constructorParamValue);
            Assert.AreEqual(2, instance.fieldValue);
            Assert.AreEqual(3, instance.propertyValue);
        }
Beispiel #10
0
        public void CreateInstanceWithGenericClass()
        {
            ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>));
            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(type.GetConstructors(PublicInstance)[1].Parameters[0], 1);
            slotValues.Add(type.GetFields(PublicInstance)[0], 2);
            slotValues.Add(type.GetProperties(PublicInstance)[0], 3);

            ObjectCreationSpec spec     = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);
            GenericClass <int> instance = (GenericClass <int>)spec.CreateInstance();

            Assert.AreEqual(1, instance.constructorParamValue);
            Assert.AreEqual(2, instance.fieldValue);
            Assert.AreEqual(3, instance.propertyValue);
        }
Beispiel #11
0
        public void SpecPropertiesDescribeTheObject()
        {
            ITypeInfo        type                 = Reflector.Wrap(typeof(GenericClass <>));
            IConstructorInfo constructor          = type.GetConstructors(PublicInstance)[1];
            IParameterInfo   constructorParameter = constructor.Parameters[0];
            IFieldInfo       field                = type.GetFields(PublicInstance)[0];
            IPropertyInfo    property             = type.GetProperties(PublicInstance)[0];

            Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>();

            slotValues.Add((IGenericParameterInfo)type.GenericArguments[0], typeof(int));
            slotValues.Add(constructorParameter, 1);
            slotValues.Add(field, 2);
            slotValues.Add(property, 3);

            ObjectCreationSpec spec = new ObjectCreationSpec(type, slotValues, NullConverter.Instance);

            Assert.AreSame(type, spec.Type);
            Assert.AreSame(slotValues, spec.SlotValues);
            Assert.AreSame(NullConverter.Instance, spec.Converter);
            Assert.AreEqual(typeof(GenericClass <int>), spec.ResolvedType);

            Assert.AreEqual(constructor, Reflector.Wrap(spec.ResolvedConstructor));
            Assert.AreElementsEqual(new object[] { 1 }, spec.ResolvedConstructorArguments);

            List <KeyValuePair <FieldInfo, object> > fieldValues = new List <KeyValuePair <FieldInfo, object> >(spec.ResolvedFieldValues);

            Assert.Count(1, fieldValues);
            Assert.AreEqual(field, Reflector.Wrap(fieldValues[0].Key));
            Assert.AreEqual(2, fieldValues[0].Value);

            List <KeyValuePair <PropertyInfo, object> > propertyValues = new List <KeyValuePair <PropertyInfo, object> >(spec.ResolvedPropertyValues);

            Assert.Count(1, propertyValues);
            Assert.AreEqual(property, Reflector.Wrap(propertyValues[0].Key));
            Assert.AreEqual(3, propertyValues[0].Value);
        }