コード例 #1
0
        public void RequestForEnumTypeReturnsResultAsString(
            Type enumType, int requestCount, string expectedResult)
        {
            // Arrange
            var enumDataTypeAttribute = new EnumDataTypeAttribute(enumType);
            var providedAttribute     = new ProvidedAttribute(enumDataTypeAttribute, true);
            var request      = new FakeMemberInfo(providedAttribute);
            var dummyContext = new DelegatingSpecimenContext();

            var sut = new EnumDataTypeAttributeRelay
            {
                RequestMemberTypeResolver = new DelegatingRequestMemberTypeResolver
                {
                    OnTryGetMemberType = _ => typeof(string)
                }
            };

            // Act
            var result = Enumerable
                         .Repeat <Func <object> >(() => sut.Create(request, dummyContext), requestCount)
                         .Select(f => f()).ToArray().Last();

            // Assert
            Assert.Equal(expectedResult, result);
        }
コード例 #2
0
        public void CanCorrectlyInterleaveDifferentEnumTypes(Type enumType, string expectedResult)
        {
            // Arrange
            var enumDataTypeAttribute = new EnumDataTypeAttribute(enumType);
            var providedAttribute     = new ProvidedAttribute(enumDataTypeAttribute, true);
            var request      = new FakeMemberInfo(providedAttribute);
            var dummyContext = new DelegatingSpecimenContext();

            var sut = new EnumDataTypeAttributeRelay
            {
                RequestMemberTypeResolver = new DelegatingRequestMemberTypeResolver
                {
                    OnTryGetMemberType = _ => typeof(string)
                }
            };

            var triStateEnumDataTypeAttribute  = new EnumDataTypeAttribute(typeof(TriState));
            var triStateProvidedAttribute      = new ProvidedAttribute(triStateEnumDataTypeAttribute, true);
            var dayOfWeekEnumDataTypeAttribute = new EnumDataTypeAttribute(typeof(DayOfWeek));
            var dayOfWeekProvidedAttribute     = new ProvidedAttribute(dayOfWeekEnumDataTypeAttribute, true);

            sut.Create(new FakeMemberInfo(triStateProvidedAttribute), dummyContext);
            sut.Create(new FakeMemberInfo(triStateProvidedAttribute), dummyContext);
            sut.Create(new FakeMemberInfo(dayOfWeekProvidedAttribute), dummyContext);

            // Act
            var result = sut.Create(request, dummyContext);

            // Assert
            Assert.Equal(expectedResult, result);
        }
コード例 #3
0
        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
            {
                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
        }
コード例 #4
0
        public void ReturnsNoSpecimenWhenEnumGeneratorReturnsNoSpecimen(Type enumType)
        {
            // Arrange
            var enumDataTypeAttribute = new EnumDataTypeAttribute(enumType);
            var providedAttribute     = new ProvidedAttribute(enumDataTypeAttribute, true);
            var request       = new FakeMemberInfo(providedAttribute);
            var enumGenerator = new DelegatingSpecimenBuilder
            {
                OnCreate = (_, _) => new NoSpecimen()
            };
            var sut = new EnumDataTypeAttributeRelay(enumGenerator);

            // Act
            var actual = sut.Create(request, null);

            // Assert
            Assert.IsType <NoSpecimen>(actual);
        }
コード例 #5
0
        public void CreateWithRegularExpressionAttributeRequestReturnsCorrectResult(string pattern)
        {
            // Arrange
            var regularExpressionAttribute = new RegularExpressionAttribute(pattern);
            var providedAttribute          = new ProvidedAttribute(regularExpressionAttribute, true);
            var request         = new FakeMemberInfo(providedAttribute);
            var expectedRequest = new RegularExpressionRequest(regularExpressionAttribute.Pattern);
            var expectedResult  = new object();
            var context         = new DelegatingSpecimenContext
            {
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen()
            };
            var sut = new RegularExpressionAttributeRelay();
            // Act
            var result = sut.Create(request, context);

            // Assert
            Assert.Equal(expectedResult, result);
        }
コード例 #6
0
        public void CreateWithConstrainedStringRequestReturnsCorrectResult(int maximum)
        {
            // Arrange
            var stringLengthAttribute = new StringLengthAttribute(maximum);
            var providedAttribute     = new ProvidedAttribute(stringLengthAttribute, true);
            var request         = new FakeMemberInfo(providedAttribute);
            var expectedRequest = new ConstrainedStringRequest(stringLengthAttribute.MaximumLength);
            var expectedResult  = new object();
            var context         = new DelegatingSpecimenContext
            {
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen()
            };
            var sut = new StringLengthAttributeRelay();
            // Act
            var result = sut.Create(request, context);

            // Assert
            Assert.Equal(expectedResult, result);
        }
コード例 #7
0
        public void RequestForEnumWithNoValuesThrows()
        {
            // Arrange
            var enumDataTypeAttribute = new EnumDataTypeAttribute(typeof(EmptyEnum));
            var providedAttribute     = new ProvidedAttribute(enumDataTypeAttribute, true);
            var request      = new FakeMemberInfo(providedAttribute);
            var dummyContext = new DelegatingSpecimenContext();

            var sut = new EnumDataTypeAttributeRelay
            {
                RequestMemberTypeResolver = new DelegatingRequestMemberTypeResolver
                {
                    OnTryGetMemberType = _ => typeof(string)
                }
            };

            // Act & assert
            Assert.Throws <ObjectCreationException>(() => sut.Create(request, dummyContext));
        }
コード例 #8
0
        public void CreateWithRegularExpressionAttributeRequestReturnsCorrectResult(string pattern)
        {
            // Fixture setup
            var regularExpressionAttribute = new RegularExpressionAttribute(pattern);
            var providedAttribute          = new ProvidedAttribute(regularExpressionAttribute, true);
            var request         = new FakeMemberInfo(providedAttribute);
            var expectedRequest = new RegularExpressionRequest(regularExpressionAttribute.Pattern);
            var expectedResult  = new object();
            var context         = new DelegatingSpecimenContext
            {
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen()
            };
            var sut = new RegularExpressionAttributeRelay();
            // Exercise system
            var result = sut.Create(request, context);

            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
コード例 #9
0
        public void ReturnsNoSpecimenWhenResolverReturnsNoSpecimen(Type enumType)
        {
            // Arrange
            var enumDataTypeAttribute = new EnumDataTypeAttribute(enumType);
            var providedAttribute     = new ProvidedAttribute(enumDataTypeAttribute, true);
            var request = new FakeMemberInfo(providedAttribute);
            var sut     = new EnumDataTypeAttributeRelay
            {
                RequestMemberTypeResolver = new DelegatingRequestMemberTypeResolver
                {
                    OnTryGetMemberType = _ => null
                }
            };

            // Act
            var actual = sut.Create(request, null);

            // Assert
            Assert.IsType <NoSpecimen>(actual);
        }
コード例 #10
0
        public void CreateWithConstrainedStringRequestReturnsCorrectResult(int maximum)
        {
            // Fixture setup
            var stringLengthAttribute        = new StringLengthAttribute(maximum);
            var providedAttribute            = new ProvidedAttribute(stringLengthAttribute, true);
            ICustomAttributeProvider request = new FakeCustomAttributeProvider(providedAttribute);
            var expectedRequest = new ConstrainedStringRequest(stringLengthAttribute.MaximumLength);
            var expectedResult  = new object();
            var context         = new DelegatingSpecimenContext
            {
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen(r)
            };
            var sut = new StringLengthAttributeRelay();
            // Exercise system
            var result = sut.Create(request, context);

            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
コード例 #11
0
        public void CreateWithRangeAttributeRequestReturnsCorrectResult(Type type, object minimum, object maximum)
        {
            // Arrange
            var  rangeAttribute    = new RangeAttribute(type, minimum.ToString(), maximum.ToString());
            var  providedAttribute = new ProvidedAttribute(rangeAttribute, true);
            var  request           = new FakeMemberInfo(providedAttribute);
            Type conversionType    = rangeAttribute.OperandType;
            var  expectedRequest   = new RangedRequest(conversionType, conversionType, rangeAttribute.Minimum,
                                                       rangeAttribute.Maximum);
            var expectedResult = new object();
            var context        = new DelegatingSpecimenContext
            {
                OnResolve = r => expectedRequest.Equals(r) ? expectedResult : new NoSpecimen()
            };
            var sut = new RangeAttributeRelay();
            // Act
            var result = sut.Create(request, context);

            // Assert
            Assert.Equal(expectedResult, result);
        }
コード例 #12
0
        public void RequestReturnsExpectedResultForEnumMemberType(Type enumType, Type memberType, object expectedResult)
        {
            // Arrange
            var enumDataTypeAttribute = new EnumDataTypeAttribute(enumType);
            var providedAttribute     = new ProvidedAttribute(enumDataTypeAttribute, true);
            var request      = new FakeMemberInfo(providedAttribute);
            var dummyContext = new DelegatingSpecimenContext();

            var sut = new EnumDataTypeAttributeRelay
            {
                RequestMemberTypeResolver = new DelegatingRequestMemberTypeResolver
                {
                    OnTryGetMemberType = _ => memberType
                }
            };

            // Act
            var actual = sut.Create(request, dummyContext);

            // Assert
            Assert.Equal(expectedResult, actual);
        }
コード例 #13
0
        public void ReturnsNoSpecimenResultForCustomClassMemberType(Type enumType, Type memberType)
        {
            // Arrange
            var enumDataTypeAttribute = new EnumDataTypeAttribute(enumType);
            var providedAttribute     = new ProvidedAttribute(enumDataTypeAttribute, true);
            var request      = new FakeMemberInfo(providedAttribute);
            var dummyContext = new DelegatingSpecimenContext();

            var sut = new EnumDataTypeAttributeRelay
            {
                RequestMemberTypeResolver = new DelegatingRequestMemberTypeResolver
                {
                    OnTryGetMemberType = _ => memberType
                }
            };

            // Act
            var actual = sut.Create(request, dummyContext);

            // Assert
            Assert.IsType <NoSpecimen>(actual);
        }
コード例 #14
0
        public void RequestReturnsCorrectTypeForDifferentMemberTypes(
            Type enumType, Type memberType)
        {
            // Arrange
            var enumDataTypeAttribute = new EnumDataTypeAttribute(enumType);
            var providedAttribute     = new ProvidedAttribute(enumDataTypeAttribute, true);
            var request      = new FakeMemberInfo(providedAttribute);
            var dummyContext = new DelegatingSpecimenContext();

            var sut = new EnumDataTypeAttributeRelay
            {
                RequestMemberTypeResolver = new DelegatingRequestMemberTypeResolver
                {
                    OnTryGetMemberType = _ => memberType
                }
            };

            // Act
            var result = sut.Create(request, dummyContext);

            // Assert
            Assert.IsType(memberType, result);
        }
コード例 #15
0
        public void CreateWithRegularExpressionAttributeRequestReturnsCorrectResult(string pattern)
        {
            // Fixture setup
            var regularExpressionAttribute = new RegularExpressionAttribute(pattern);
            var providedAttribute = new ProvidedAttribute(regularExpressionAttribute, true);
            ICustomAttributeProvider request = new FakeCustomAttributeProvider(providedAttribute);
            var expectedRequest = new RegularExpressionRequest(regularExpressionAttribute.Pattern);
            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 RegularExpressionAttributeRelay();
            // Exercise system
            var result = sut.Create(request, context);
            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
コード例 #16
0
        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
        }
コード例 #17
0
        public void CreateWithConstrainedStringRequestReturnsCorrectResult(int maximum)
        {
            // Fixture setup
            var stringLengthAttribute = new StringLengthAttribute(maximum);
            var providedAttribute = new ProvidedAttribute(stringLengthAttribute, true);
            ICustomAttributeProvider request = new FakeCustomAttributeProvider(providedAttribute);
            var expectedRequest = new ConstrainedStringRequest(stringLengthAttribute.MaximumLength);
            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 StringLengthAttributeRelay();
            // Exercise system
            var result = sut.Create(request, context);
            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }