public void EngineShouldCallTypeConverterWhenConverterAttributeIsPresent() { var converter = A.Fake<ITypeConverter>(); A.CallTo(converter).WithReturnType<object>().Returns("foo"); A.CallTo(converter).WithReturnType<bool>().Returns(true); var attribute = A.Fake<IFixedFieldSettings>(); attribute.CallsTo(x => x.Index).Returns(1); attribute.CallsTo(x => x.Length).Returns(1); attribute.CallsTo(x => x.TypeConverter).Returns(converter); var properties = typeof (ConverterTestObject).GetProperties(BindingFlags.Instance | BindingFlags.Public).ToDictionary(info => info.Name); var container = new FieldsContainer<IFixedFieldSettingsContainer>(); container.AddOrUpdate(properties["Foo"], new FixedFieldSettings(properties["Foo"], attribute)); var descriptor = new LayoutDescriptorBase<IFixedFieldSettingsContainer>(container) {HasHeader = false}; var engine = fileEngineFactory.GetEngine(descriptor); using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) { writer.WriteLine("A"); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); // Capture first result to force enumerable to be iterated var result = engine.Read<ConverterTestObject>(stream).FirstOrDefault(); } A.CallTo(converter).WithReturnType<object>().MustHaveHappened(); }
public ILayoutDescriptor <IFixedFieldSettingsContainer> GetDescriptor(Type t) { var container = new FieldsContainer <IFixedFieldSettingsContainer>(); var fileMappingType = t; var fileAttribute = fileMappingType.GetAttribute <FixedLengthFileAttribute>(); if (fileAttribute == null) { throw new NotSupportedException(string.Format("Mapping type {0} should be marked with {1} attribute", fileMappingType.Name, typeof(FixedLengthFileAttribute).Name)); } var properties = fileMappingType.GetTypeDescription <FixedLengthFieldAttribute>(); foreach (var p in properties) { var attribute = p.Attributes.FirstOrDefault() as IFixedFieldSettings; if (attribute != null) { container.AddOrUpdate(p.Property, new FixedFieldSettings(p.Property, attribute)); } } var descriptor = new LayoutDescriptorBase <IFixedFieldSettingsContainer>(container, t) { HasHeader = false }; return(descriptor); }
public void GivenIHaveSpecificationForType(string type, Table table) { var targetType = Assembly .GetExecutingAssembly() .GetTypes() .FirstOrDefault(x => x.FullName.EndsWith(type)); var properties = targetType .GetProperties(BindingFlags.Instance | BindingFlags.Public) .ToDictionary(info => info.Name); var typeMappings = table.CreateSet <FixedLengthTypeMapping>().ToArray(); var container = new FieldsContainer <IFixedFieldSettingsContainer>(); foreach (var typeMapping in typeMappings) { var propertyInfo = properties[typeMapping.Name]; var settings = new FixedFieldSettingsConstructor(propertyInfo); settings.WithLength(typeMapping.Length); if (!string.IsNullOrEmpty(typeMapping.NullValue)) { settings.AllowNull(typeMapping.NullValue); } switch (typeMapping.Padding) { case Padding.Right: settings.WithRightPadding(typeMapping.PaddingCharElement); break; case Padding.Left: settings.WithLeftPadding(typeMapping.PaddingCharElement); break; } container.AddOrUpdate(propertyInfo, settings); } var descriptor = new LayoutDescriptorBase <IFixedFieldSettingsContainer>(container) { HasHeader = false }; ScenarioContext.Current.Add(() => descriptor, descriptor); }
public void GivenIHaveSpecificationForType(string type, Table table) { var targetType = Assembly .GetExecutingAssembly() .GetTypes() .FirstOrDefault(x => x.FullName.EndsWith(type)); var properties = targetType .GetProperties(BindingFlags.Instance | BindingFlags.Public) .ToDictionary(info => info.Name); var typeMappings = table.CreateSet<FixedLengthTypeMapping>().ToArray(); var container = new FieldsContainer<IFixedFieldSettingsContainer>(); foreach (var typeMapping in typeMappings) { var propertyInfo = properties[typeMapping.Name]; var settings = new FixedFieldSettingsConstructor(propertyInfo); settings.WithLength(typeMapping.Length); if (!string.IsNullOrEmpty(typeMapping.NullValue)) { settings.AllowNull(typeMapping.NullValue); } switch (typeMapping.Padding) { case Padding.Right: settings.WithRightPadding(typeMapping.PaddingCharElement); break; case Padding.Left: settings.WithLeftPadding(typeMapping.PaddingCharElement); break; } container.AddOrUpdate(propertyInfo, settings); } var descriptor = new LayoutDescriptorBase<IFixedFieldSettingsContainer>(container) { HasHeader = false }; ScenarioContext.Current.Add(() => descriptor, descriptor); }
public IDelimitedLayoutDescriptor GetDescriptor(Type t) { var container = new FieldsContainer <IDelimitedFieldSettingsContainer>(); var fileMappingType = t; var fileAttribute = fileMappingType.GetAttribute <DelimitedFileAttribute>(); if (fileAttribute == null) { throw new NotSupportedException(string.Format("Mapping type {0} should be marked with {1} attribute", fileMappingType.Name, typeof(DelimitedFileAttribute).Name)); } var properties = fileMappingType.GetTypeDescription <DelimitedFieldAttribute>(); foreach (var p in properties) { var settings = p.Attributes.FirstOrDefault() as IDelimitedFieldSettings; if (settings != null) { container.AddOrUpdate(p.Property, new DelimitedFieldSettings(p.Property, settings)); } } var methodInfo = typeof(DelimedLayoutGeneric) .GetTypeInfo() .GetDeclaredMethod("GetDelimitedLayout") .MakeGenericMethod(new[] { t }); object[] args = { null, new DelimitedFieldSettingsFactory(), container, fileAttribute }; var descriptor = (IDelimitedLayoutDescriptor)methodInfo.Invoke(null, args); //var descriptor = DelimedLayoutGeneric.GetDelimitedLayout(t, new DelimitedFieldSettingsFactory(), container) // .WithDelimiter(fileAttribute.Delimiter) // .WithQuote(fileAttribute.Quotes); //if (fileAttribute.HasHeader) //{ // descriptor.WithHeader(); //} return(descriptor); }
public void EngineShouldCallTypeConverterWhenConverterAttributeIsPresent() { var converter = A.Fake <ITypeConverter>(); A.CallTo(converter).WithReturnType <object>().Returns("foo"); A.CallTo(converter).WithReturnType <bool>().Returns(true); var attribute = A.Fake <IFixedFieldSettings>(); attribute.CallsTo(x => x.Index).Returns(1); attribute.CallsTo(x => x.Length).Returns(1); attribute.CallsTo(x => x.TypeConverter).Returns(converter); var properties = typeof(ConverterTestObject).GetProperties(BindingFlags.Instance | BindingFlags.Public).ToDictionary(info => info.Name); var container = new FieldsContainer <IFixedFieldSettingsContainer>(); container.AddOrUpdate(properties["Foo"], new FixedFieldSettings(properties["Foo"], attribute)); var descriptor = new LayoutDescriptorBase <IFixedFieldSettingsContainer>(container) { HasHeader = false }; var engine = fileEngineFactory.GetEngine(descriptor); using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) { writer.WriteLine("A"); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); // Capture first result to force enumerable to be iterated var result = engine.Read <ConverterTestObject>(stream).FirstOrDefault(); } A.CallTo(converter).WithReturnType <object>().MustHaveHappened(); }
public IDelimitedLayoutDescriptor GetDescriptor <T>() { var container = new FieldsContainer <IDelimitedFieldSettingsContainer>(); var fileMappingType = typeof(T); var fileAttribute = fileMappingType.GetAttribute <DelimitedFileAttribute>(); if (fileAttribute == null) { throw new NotSupportedException(string.Format("Mapping type {0} should be marked with {1} attribute", fileMappingType.Name, typeof(DelimitedFileAttribute).Name)); } var properties = fileMappingType.GetTypeDescription <DelimitedFieldAttribute>(); foreach (var p in properties) { var settings = p.Attributes.FirstOrDefault() as IDelimitedFieldSettings; if (settings != null) { container.AddOrUpdate(p.Property, new DelimitedFieldSettings(p.Property, settings)); } } var descriptor = new DelimitedLayout <T>(new DelimitedFieldSettingsFactory(), container) .WithDelimiter(fileAttribute.Delimiter) .WithQuote(fileAttribute.Quotes); if (fileAttribute.HasHeader) { descriptor.WithHeader(); } return(descriptor); }
public void EngineShouldCallTypeConverterWhenConverterAttributeIsPresent() { // a converter to convert "A" to "foo" var converter = A.Fake <ITypeConverter>(); A.CallTo(() => converter.ConvertFromString("A")).Returns("foo"); A.CallTo(() => converter.CanConvertFrom(typeof(string))).Returns(true); A.CallTo(() => converter.CanConvertTo(typeof(string))).Returns(true); // an attribute to assign the property var attribute = A.Fake <IDelimitedFieldSettings>(); A.CallTo(() => attribute.Index).Returns(1); A.CallTo(() => attribute.TypeConverter).Returns(converter); // the properties of the class var properties = typeof(ConverterTestObject).GetProperties(BindingFlags.Instance | BindingFlags.Public).ToDictionary(info => info.Name); // assign the attribute to the Foo property var container = new FieldsContainer <IDelimitedFieldSettingsContainer>(); container.AddOrUpdate(properties["Foo"], new DelimitedFieldSettings(properties["Foo"], attribute)); var layout = new DelimitedLayout <ConverterTestObject>(new DelimitedFieldSettingsFactory(), container); var engine = _fileEngineFactory.GetEngine(layout); // write "A" to the stream and verify it is converted to "foo" using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) { writer.WriteLine("A"); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); // Capture first result to force enumerable to be iterated var result = engine.Read <ConverterTestObject>(stream).FirstOrDefault(); Assert.Equal("foo", result.Foo); } }
public void OrderedFieldsShouldContainsOneItemAfterAdd() { _fieldsContainer.AddOrUpdate(_propertyInfo, _fieldSettings); _fieldsContainer.OrderedFields.Should().HaveCount(1); }