/// <summary>
        /// Generates static or dynamic tests from the contract.
        /// </summary>
        /// <param name="fieldScope">The field scope.</param>
        /// <param name="field">The field.</param>
        /// <param name="containingScope">The containing scope.</param>
        protected virtual void GenerateTestsFromContract(IPatternScope fieldScope, IFieldInfo field, IPatternScope containingScope)
        {
            if (field.IsStatic)
            {
                FieldInfo resolvedField = field.Resolve(false);
                if (resolvedField == null)
                {
                    fieldScope.TestModelBuilder.AddAnnotation(new Annotation(AnnotationType.Info, field,
                                                                             "This test runner does not fully support static contract verifier methods "
                                                                             + "because the code that defines the contract cannot be executed "
                                                                             + "at test exploration time.  Consider making the contract field non-static instead."));
                    return;
                }

                var contract = resolvedField.GetValue(null) as IContract;
                if (contract == null)
                {
                    fieldScope.TestModelBuilder.AddAnnotation(new Annotation(AnnotationType.Error, field,
                                                                             "Expected the contract field to contain a value that is assignable "
                                                                             + "to type IContract."));
                    return;
                }

                IEnumerable <Test> contractTests = GetContractVerificationTests(contract, field);
                Test.BuildStaticTests(contractTests, fieldScope, field);
            }
            else
            {
                fieldScope.TestBuilder.TestInstanceActions.ExecuteTestInstanceChain.After(state =>
                {
                    var invoker = new FixtureMemberInvoker <IContract>(null, containingScope, field.Name);
                    IContract contract;

                    try
                    {
                        contract = invoker.Invoke(FixtureMemberInvokerTargets.Field);
                    }
                    catch (PatternUsageErrorException exception)
                    {
                        throw new TestFailedException(
                            String.Format(
                                "The field '{0}' must contain an instance of type IContract that describes a contract to be verified.",
                                field.Name), exception);
                    }

                    IEnumerable <Test> contractTests = GetContractVerificationTests(contract, field);

                    TestOutcome outcome = Test.RunDynamicTests(contractTests, field, null, null);
                    if (outcome != TestOutcome.Passed)
                    {
                        throw new SilentTestException(outcome);
                    }
                });
            }
        }
Example #2
0
        private void ResolveFields()
        {
            resolvedFieldValues = new Dictionary <FieldInfo, object>();

            foreach (KeyValuePair <ISlotInfo, object> slotValue in SlotValues)
            {
                IFieldInfo field = slotValue.Key as IFieldInfo;
                if (field != null)
                {
                    FieldInfo resolvedField = ResolveMember(resolvedType, field.Resolve(true));
                    resolvedFieldValues.Add(resolvedField, Converter.Convert(slotValue.Value, resolvedField.FieldType));
                }
            }
        }
        private GallioFunc <object[], TOutput> TryGetMemberAsField(FixtureMemberInvokerTargets targets, ITypeInfo ownerInfo)
        {
            if ((targets & FixtureMemberInvokerTargets.Field) != 0)
            {
                IFieldInfo info = ownerInfo.GetField(memberName, bindingFlags);

                if (info != null)
                {
                    return(args =>
                    {
                        object fixtureInstance = GetFixtureInstance(info.IsStatic);
                        FieldInfo field = (type == null) ? GetMemberInfo <FieldInfo>(t => t.GetField(memberName, bindingFlags)) : info.Resolve(true);

                        if (field == null)
                        {
                            throw new TestFailedException(String.Format("Could not find field '{0}''.", memberName));
                        }

                        return (TOutput)field.GetValue(fixtureInstance);
                    });
                }
            }

            return(null);
        }
        /// <summary>
        /// Generates static or dynamic tests from the contract.
        /// </summary>
        /// <param name="fieldScope">The field scope.</param>
        /// <param name="field">The field.</param>
        /// <param name="containingScope">The containing scope.</param>
        protected virtual void GenerateTestsFromContract(IPatternScope fieldScope, IFieldInfo field, IPatternScope containingScope)
        {
            if (field.IsStatic)
            {
                FieldInfo resolvedField = field.Resolve(false);
                if (resolvedField == null)
                {
                    fieldScope.TestModelBuilder.AddAnnotation(new Annotation(AnnotationType.Info, field,
                        "This test runner does not fully support static contract verifier methods "
                        + "because the code that defines the contract cannot be executed "
                        + "at test exploration time.  Consider making the contract field non-static instead."));
                    return;
                }

                var contract = resolvedField.GetValue(null) as IContract;
                if (contract == null)
                {
                    fieldScope.TestModelBuilder.AddAnnotation(new Annotation(AnnotationType.Error, field,
                        "Expected the contract field to contain a value that is assignable "
                        + "to type IContract."));
                    return;
                }

                IEnumerable<Test> contractTests = GetContractVerificationTests(contract, field);
                Test.BuildStaticTests(contractTests, fieldScope, field);
            }
            else
            {
                fieldScope.TestBuilder.TestInstanceActions.ExecuteTestInstanceChain.After(state =>
                {
                    var invoker = new FixtureMemberInvoker<IContract>(null, containingScope, field.Name);
                    IContract contract;

                    try
                    {
                        contract = invoker.Invoke(FixtureMemberInvokerTargets.Field);
                    }
                    catch (PatternUsageErrorException exception)
                    {
                        throw new TestFailedException(
                            String.Format(
                                "The field '{0}' must contain an instance of type IContract that describes a contract to be verified.",
                                field.Name), exception);
                    }

                    IEnumerable<Test> contractTests = GetContractVerificationTests(contract, field);

                    TestOutcome outcome = Test.RunDynamicTests(contractTests, field, null, null);
                    if (outcome != TestOutcome.Passed)
                        throw new SilentTestException(outcome);
                });
            }
        }
Example #5
0
        public override FieldInfo GetField(string name, BindingFlags bindingAttr)
        {
            IFieldInfo field = adapter.GetField(name, bindingAttr);

            return(field != null?field.Resolve(false) : null);
        }