Example #1
0
        public IEnumerable <KeyValuePair <string, object> > GetMetadata(FieldGenerationContext fieldGenerationContext)
        {
            if (fieldGenerationContext == null)
            {
                throw new ArgumentNullException(nameof(fieldGenerationContext));
            }

            var propertyType = fieldGenerationContext.FieldDescriptor.PropertyType;
            var typeCode     = this._typeCodeExtractor.GetTypeCode(propertyType);

            if (typeCode != TypeCode.String)
            {
                yield break;
            }

            var type = "text";
            var fieldCustomAttributes = fieldGenerationContext.FieldDescriptor.CustomAttributes;
            var dataType = _dataTypeAttributeExtractor.GetDataTypeAttribute(fieldCustomAttributes);

            if (dataType != null)
            {
                type = this._stringPropertyTypeMap.MapDataType(dataType.DataType);
            }

            yield return(KeyValuePair.Create("type", (object)type));
        }
Example #2
0
        private void ShouldGetTypeCodeFromTypeCodeExtractor(
            [Frozen] Mock <ITypeCodeExtractor> typeMock,
            FieldGenerationContext fieldGenerationContext,
            StringMetaProvider sut)
        {
            var _ = sut.GetMetadata(fieldGenerationContext)
                    .ToArray();

            typeMock.Verify(t => t.GetTypeCode(fieldGenerationContext.FieldDescriptor.PropertyType), Times.Once);
        }
Example #3
0
        private void ShouldIterateOverTypeMetaProviders(
            Mock <IEnumerable <ITypeMetaProvider> > typeMetaProviders,
            FieldGenerationContext context
            )
        {
            var sut  = new TypeMetadataProvider(typeMetaProviders.Object);
            var meta = sut.GetMetadata(context).ToArray();

            typeMetaProviders.Verify(e => e.GetEnumerator(), Times.Once);
        }
Example #4
0
        private void ShouldGetDataTypeAttributeFromDataTypeAttributeExtractor(
            [Frozen] Mock <ITypeCodeExtractor> typeMock,
            [Frozen] Mock <IDataTypeAttributeExtractor> dataTypeMock,
            FieldGenerationContext fieldGenerationContext,
            StringMetaProvider sut)
        {
            typeMock.Setup(t => t.GetTypeCode(It.IsAny <System.Type>()))
            .Returns(TypeCode.String);

            var _ = sut.GetMetadata(fieldGenerationContext)
                    .ToArray();

            dataTypeMock.Verify(t => t.GetDataTypeAttribute(fieldGenerationContext.FieldDescriptor.CustomAttributes), Times.Once);
        }
Example #5
0
        private void ShouldReturnEmptyMetadata(
            TypeCode typeCode,
            FieldGenerationContext fieldGenerationContext,
            [Frozen] Mock <ITypeCodeExtractor> typeMock,
            StringMetaProvider sut)
        {
            typeMock.Setup(t => t.GetTypeCode(It.IsAny <System.Type>()))
            .Returns(typeCode);

            var meta = sut.GetMetadata(fieldGenerationContext)
                       .ToArray();

            Assert.Empty(meta);
        }
Example #6
0
        private void ShouldReturnMetadata(
            TypeCode typeCode,
            FieldGenerationContext fieldGenerationContext,
            [Frozen] Mock <ITypeCodeExtractor> typeMock,
            NumberMetaProvider sut)
        {
            typeMock.Setup(t => t.GetTypeCode(It.IsAny <Type>()))
            .Returns(typeCode);

            var meta = sut.GetMetadata(fieldGenerationContext)
                       .ToArray();

            Assert.Equal(meta, new[] { new KeyValuePair <string, object>("type", "number"), });
        }
Example #7
0
        public IEnumerable <KeyValuePair <string, object> > GetMetadata(FieldGenerationContext fieldGenerationContext)
        {
            if (fieldGenerationContext == null)
            {
                throw new ArgumentNullException(nameof(fieldGenerationContext));
            }

            var fieldDescriptorPropertyType = fieldGenerationContext.FieldDescriptor.PropertyType;
            var typeCode = this._typeCodeExtractor.GetTypeCode(fieldDescriptorPropertyType);

            if (this._numberTypeCodes.Contains(typeCode))
            {
                yield return(KeyValuePair.Create("type", "number" as object));
            }
        }
Example #8
0
        public IEnumerable <KeyValuePair <string, object> > GetMetadata(FieldGenerationContext fieldGenerationContext)
        {
            if (fieldGenerationContext == null)
            {
                throw new ArgumentNullException(nameof(fieldGenerationContext));
            }

            foreach (var typeMetaProvider in this._typeMetaProviders)
            {
                foreach (var keyValuePair in typeMetaProvider.GetMetadata(fieldGenerationContext))
                {
                    yield return(keyValuePair);
                }
            }
        }
Example #9
0
        private void ShouldGetOptionsFromEnumOptionsExtractor(
            [Frozen] Mock <IEnumOptionsExtractor> enumOptionsExtractor,
            FieldGenerationContext fieldGenerationContext,
            OptionMetaProvider sut)
        {
            var _ = sut.GetMetadata(fieldGenerationContext).ToArray();

            FieldOption[] options;
            enumOptionsExtractor
            .Verify(e =>
                    e.TryGetEnumOptions(
                        fieldGenerationContext.FieldDescriptor.PropertyType,
                        out options
                        ), Times.Once);
        }
Example #10
0
        private void ShouldCallGetMetadataForEachProvider(
            List <Mock <ITypeMetaProvider> > typeMetaProviderMocks,
            FieldGenerationContext context
            )
        {
            var typeMetaProviders = typeMetaProviderMocks.Select(m => m.Object).ToArray();

            var sut  = new TypeMetadataProvider(typeMetaProviders);
            var meta = sut.GetMetadata(context).ToArray();

            foreach (var typeMetaProviderMock in typeMetaProviderMocks)
            {
                typeMetaProviderMock.Verify(m => m.GetMetadata(context), Times.Once);
            }
        }
Example #11
0
        public IEnumerable <KeyValuePair <string, object> > GetMetadata(FieldGenerationContext fieldGenerationContext)
        {
            var propertyType = fieldGenerationContext.FieldDescriptor.PropertyType;

            if (!propertyType.IsEnum)
            {
                yield break;
            }

            var options = GetOptions(propertyType);

            yield return(KeyValuePair.Create("type", "options" as object));

            yield return(KeyValuePair.Create("options", options));

            yield return(KeyValuePair.Create("value", fieldGenerationContext.FieldDescriptor.Value));
        }
Example #12
0
        private void ShouldPassDataTypeToStringPropertyTypeMap(
            [Frozen] Mock <ITypeCodeExtractor> typeMock,
            [Frozen] Mock <IDataTypeAttributeExtractor> dataTypeMock,
            [Frozen] Mock <IStringPropertyTypeMap> stringPropertyTypeMap,
            FieldGenerationContext fieldGenerationContext,
            StringMetaProvider sut,
            DataTypeAttribute expectedDataTypeAttribute)
        {
            typeMock.Setup(t => t.GetTypeCode(It.IsAny <System.Type>()))
            .Returns(TypeCode.String);
            dataTypeMock.Setup(t => t.GetDataTypeAttribute(fieldGenerationContext.FieldDescriptor.CustomAttributes))
            .Returns(expectedDataTypeAttribute);

            var _ = sut.GetMetadata(fieldGenerationContext)
                    .ToArray();

            stringPropertyTypeMap.Verify(m => m.MapDataType(expectedDataTypeAttribute.DataType), Times.Once);
        }
Example #13
0
        public IEnumerable <KeyValuePair <string, object> > GetMetadata(FieldGenerationContext fieldGenerationContext)
        {
            if (fieldGenerationContext == null)
            {
                throw new ArgumentNullException(nameof(fieldGenerationContext));
            }

            var propertyType = fieldGenerationContext.FieldDescriptor.PropertyType;

            if (!this._enumOptionsExtractor.TryGetEnumOptions(propertyType, out var options))
            {
                yield break;
            }

            yield return(KeyValuePair.Create("type", "option" as object));

            yield return(KeyValuePair.Create("options", options as object));
        }
Example #14
0
        private void ShouldReturnEmptyMetadata(
            [Frozen] Mock <IEnumOptionsExtractor> enumOptionsExtractor,
            FieldGenerationContext fieldGenerationContext,
            OptionMetaProvider sut)
        {
            FieldOption[] options;
            enumOptionsExtractor
            .Setup(e =>
                   e.TryGetEnumOptions(
                       fieldGenerationContext.FieldDescriptor.PropertyType,
                       out options
                       ))
            .Returns(false);

            var metadata = sut.GetMetadata(fieldGenerationContext).ToArray();

            Assert.Empty(metadata);
        }
Example #15
0
        private void ShouldFallbackToTypeText(
            [Frozen] Mock <ITypeCodeExtractor> typeMock,
            [Frozen] Mock <IDataTypeAttributeExtractor> dataTypeMock,
            FieldGenerationContext fieldGenerationContext,
            StringMetaProvider sut)
        {
            typeMock.Setup(t => t.GetTypeCode(It.IsAny <System.Type>()))
            .Returns(TypeCode.String);
            dataTypeMock.Setup(t => t.GetDataTypeAttribute(fieldGenerationContext.FieldDescriptor.CustomAttributes))
            .Returns(null as DataTypeAttribute);

            var meta = sut.GetMetadata(fieldGenerationContext)
                       .ToArray();

            Assert.Equal(new[]
            {
                new KeyValuePair <string, object>("type", "text")
            }, meta);
        }
Example #16
0
        private void ShouldReturnMetadata(
            [Frozen] Mock <IEnumOptionsExtractor> enumOptionsExtractor,
            FieldGenerationContext fieldGenerationContext,
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
            FieldOption[] expectedFieldOptions,
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
            OptionMetaProvider sut)
        {
            enumOptionsExtractor
            .Setup(e =>
                   e.TryGetEnumOptions(
                       fieldGenerationContext.FieldDescriptor.PropertyType,
                       out expectedFieldOptions
                       ))
            .Returns(true);

            var metadata = sut.GetMetadata(fieldGenerationContext).ToArray();

            Assert.Equal(new KeyValuePair <string, object>("type", "option"), metadata[0]);
            Assert.Same("options", metadata[1].Key);
            Assert.Same(expectedFieldOptions, metadata[1].Value);
        }
Example #17
0
        private void ShouldForwardGetMetadataForEachProviderAsReturn(
            List <IEnumerable <KeyValuePair <string, object> > > expectedKeyValuePairs,
            List <Mock <ITypeMetaProvider> > typeMetaProviderMocks,
            FieldGenerationContext context
            )
        {
            var typeMetaProviders = typeMetaProviderMocks.Select(m => m.Object).ToArray();

            for (var index = 0; index < typeMetaProviderMocks.Count; index++)
            {
                var typeMetaProviderMock = typeMetaProviderMocks[index];

                typeMetaProviderMock
                .Setup(m => m.GetMetadata(context))
                .Returns(expectedKeyValuePairs[index]);
            }

            var sut  = new TypeMetadataProvider(typeMetaProviders);
            var meta = sut.GetMetadata(context).ToArray();

            Assert.Equal(meta, expectedKeyValuePairs.SelectMany(kvp => kvp));
        }
Example #18
0
        private void ShouldReturnMetadataFromTypeMap(
            [Frozen] Mock <ITypeCodeExtractor> typeMock,
            [Frozen] Mock <IDataTypeAttributeExtractor> dataTypeMock,
            [Frozen] Mock <IStringPropertyTypeMap> stringPropertyTypeMap,
            FieldGenerationContext fieldGenerationContext,
            StringMetaProvider sut,
            DataTypeAttribute expectedDataTypeAttribute,
            string type)
        {
            typeMock.Setup(t => t.GetTypeCode(It.IsAny <System.Type>()))
            .Returns(TypeCode.String);
            dataTypeMock.Setup(t => t.GetDataTypeAttribute(fieldGenerationContext.FieldDescriptor.CustomAttributes))
            .Returns(expectedDataTypeAttribute);
            stringPropertyTypeMap.Setup(m => m.MapDataType(expectedDataTypeAttribute.DataType))
            .Returns(type);

            var meta = sut.GetMetadata(fieldGenerationContext)
                       .ToArray();

            Assert.Equal(new[]
            {
                new KeyValuePair <string, object>("type", type)
            }, meta);
        }