Exemplo n.º 1
0
        /// <summary>Composes the supplied builders.</summary>
        /// <param name="builders">The builders to compose.</param>
        /// <returns>
        /// A new <see cref="ISpecimenBuilderNode" /> instance containing
        /// <paramref name="builders" /> as child nodes.
        /// </returns>
        public override ISpecimenBuilderNode Compose(IEnumerable <ISpecimenBuilder> builders)
        {
            var composedBuilder = CompositeSpecimenBuilder.ComposeIfMultiple(builders);
            var pp = new Postprocessor(composedBuilder, this.Command, this.Specification);

#pragma warning disable 618
            ObsoletedMemberShims.Postprocessor_SetAction(pp, ObsoletedMemberShims.Postprocessor_GetAction(this));
#pragma warning restore 618
            return(pp);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new specimen based on a request.
        /// </summary>
        /// <param name="request">The request that describes what to create.</param>
        /// <param name="context">A container that can be used to create other specimens.</param>
        /// <returns>
        /// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance.
        /// </returns>
        /// <remarks>
        ///     <para>
        /// The <paramref name="request"/> can be any object, but will often be a
        /// <see cref="Type"/> or other <see cref="System.Reflection.MemberInfo"/> instances.
        /// </para>
        /// </remarks>
        public object Create(object request, ISpecimenContext context)
        {
            var requestsForCurrentThread = this.GetMonitoredRequestsForCurrentThread();

            if (requestsForCurrentThread.Count > 0)
            {
                // This is performance-sensitive code when used repeatedly over many requests.
                // See discussion at https://github.com/AutoFixture/AutoFixture/pull/218
                var requestsArray            = requestsForCurrentThread.ToArray();
                int numRequestsSameAsThisOne = 0;
                for (int i = 0; i < requestsArray.Length; i++)
                {
                    var existingRequest = requestsArray[i];
                    if (this.Comparer.Equals(existingRequest, request))
                    {
                        numRequestsSameAsThisOne++;
                    }

                    if (numRequestsSameAsThisOne >= this.RecursionDepth)
                    {
#pragma warning disable 618
                        return(ObsoletedMemberShims.RecursionGuard_HandleRecursiveRequest(this, request));

#pragma warning restore 618
                    }
                }
            }

            requestsForCurrentThread.Push(request);
            try
            {
                return(this.Builder.Create(request, context));
            }
            finally
            {
                requestsForCurrentThread.Pop();
            }
        }