Пример #1
0
        /// <summary>
        /// Creates a new specimen based on a requested range.
        /// </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>
        /// A specimen created from a <see cref="RangedNumberRequest"/> encapsulating the operand
        /// type, the minimum and the maximum of the requested number, if possible; otherwise,
        /// a <see cref="NoSpecimen"/> instance.
        /// </returns>
        public object Create(object request, ISpecimenContext context)
        {
            if (request == null)
            {
                return(new NoSpecimen());
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var customAttributeProvider = request as ICustomAttributeProvider;

            if (customAttributeProvider == null)
            {
#pragma warning disable 618
                return(new NoSpecimen(request));

#pragma warning restore 618
            }

            var rangeAttribute = customAttributeProvider.GetCustomAttributes(typeof(RangeAttribute), inherit: true).Cast <RangeAttribute>().SingleOrDefault();
            if (rangeAttribute == null)
            {
#pragma warning disable 618
                return(new NoSpecimen(request));

#pragma warning restore 618
            }

            return(context.Resolve(RangeAttributeRelay.Create(rangeAttribute, request)));
        }
 public void CreateWithNullContextThrows()
 {
     // Fixture setup
     var sut = new RangeAttributeRelay();
     var dummyRequest = new object();
     // Exercise system and verify outcome
     Assert.Throws<ArgumentNullException>(() =>
         sut.Create(dummyRequest, null));
     // Teardown
 }
 public void CreateWithNullRequestReturnsCorrectResult()
 {
     // Fixture setup
     var sut = new RangeAttributeRelay();
     // Exercise system
     var dummyContext = new DelegatingSpecimenContext();
     var result = sut.Create(null, dummyContext);
     // Verify outcome
     Assert.Equal(new NoSpecimen(), result);
     // Teardown
 }
 public void CreateWithNonRangeAttributeRequestReturnsCorrectResult(object request)
 {
     // Fixture setup
     var sut = new RangeAttributeRelay();
     // Exercise system
     var dummyContext = new DelegatingSpecimenContext();
     var result = sut.Create(request, dummyContext);
     // Verify outcome
     var expectedResult = new NoSpecimen(request);
     Assert.Equal(expectedResult, result);
     // Teardown
 }
        public void CreateWithAnonymousRequestReturnsCorrectResult()
        {
            // Fixture setup
            var sut = new RangeAttributeRelay();
            var dummyRequest = new object();
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            var result = sut.Create(dummyRequest, dummyContainer);
            // Verify outcome
#pragma warning disable 618
            var expectedResult = new NoSpecimen(dummyRequest);
#pragma warning restore 618
            Assert.Equal(expectedResult, result);
            // Teardown
        }
        public void CreateWithRangeAttributeRequestReturnsCorrectResult(Type type, object minimum, object maximum)
        {
            // Fixture setup
            var rangeAttribute = new RangeAttribute(type, minimum.ToString(), maximum.ToString());
            var providedAttribute = new ProvidedAttribute(rangeAttribute, true);
            ICustomAttributeProvider request = new FakeCustomAttributeProvider(providedAttribute);
            Type conversionType = rangeAttribute.OperandType;
            var expectedRequest = new RangedNumberRequest(
                conversionType,
                Convert.ChangeType(rangeAttribute.Minimum, conversionType, CultureInfo.CurrentCulture),
                Convert.ChangeType(rangeAttribute.Maximum, conversionType, CultureInfo.CurrentCulture)
                );
            var expectedResult = new object();
            var context = new DelegatingSpecimenContext
            {
#pragma warning disable 618
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r)
#pragma warning restore 618
            };
            var sut = new RangeAttributeRelay();
            // Exercise system
            var result = sut.Create(request, context);
            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
        public void CreateWithFieldDecoratedWithRangeAttributeReturnsCorrectResult(
            string name,
            object attributeMinimum,
            object attributeMaximum)
        {
            // Fixture setup
            var request = typeof(RangeValidatedType).GetField(name);
            Type target = Nullable.GetUnderlyingType(request.FieldType)
                ?? request.FieldType;

            var expectedRequest = new RangedNumberRequest(
                target,
                Convert.ChangeType(RangeValidatedType.Minimum, target, CultureInfo.CurrentCulture),
                Convert.ChangeType(RangeValidatedType.Maximum, target, CultureInfo.CurrentCulture)
                );

            var expectedResult = new object();
            var context = new DelegatingSpecimenContext
            {
#pragma warning disable 618
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r)
#pragma warning restore 618
            };
            var sut = new RangeAttributeRelay();
            // Exercise system
            var result = sut.Create(request, context);
            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
        public void CreateWithPropertyDecoratedWithRangeAttributeReturnsCorrectResult(
            Type attributeType, 
            object attributeMinimum, 
            object attributeMaximum)
        {
            // Fixture setup
            var request = typeof(RangeValidatedType).GetProperty("Property");
            Type target = request.PropertyType;

            var expectedRequest = new RangedNumberRequest(
                target,
                Convert.ChangeType(RangeValidatedType.Minimum, target, CultureInfo.CurrentCulture),
                Convert.ChangeType(RangeValidatedType.Maximum, target, CultureInfo.CurrentCulture)
                );
           
            var expectedResult = new object();
            var context = new DelegatingSpecimenContext
            {
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r)
            };
            var sut = new RangeAttributeRelay();
            // Exercise system
            var result = sut.Create(request, context);
            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }