Пример #1
0
        public void GetProperty_ValidStringValue_ReturnsSuccess(string stringValue, bool expected)
        {
            var item = new BoolMapper();

            object value = null;
            PropertyMapperResultType result = item.MapCellValue(new ReadCellValueResult(-1, stringValue), ref value);
            Assert.Equal(PropertyMapperResultType.Success, result);
            Assert.Equal(expected, value);
        }
Пример #2
0
        public void GetProperty_InvalidStringValue_ReturnsInvalid(string stringValue)
        {
            var item = new BoolMapper();

            object value = 1;
            PropertyMapperResultType result = item.MapCellValue(new ReadCellValueResult(-1, stringValue), ref value);
            Assert.Equal(PropertyMapperResultType.Invalid, result);
            Assert.Equal(1, value);
        }
Пример #3
0
        public void WithCellValueMappers_ValidMappers_ThrowsArgumentNullException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);
            ICellValueMapper mapper1 = Assert.Single(propertyMap.CellValueMappers);
            ICellValueMapper mapper2 = new BoolMapper();;

            Assert.Same(propertyMap, propertyMap.WithCellValueMappers(mapper2));
            Assert.Equal(new ICellValueMapper[] { mapper1, mapper2 }, propertyMap.CellValueMappers);
        }
        public void AddCellValueMapper_ValidItem_Success()
        {
            var pipeline = new ValuePipeline();
            var item1    = new BoolMapper();
            var item2    = new BoolMapper();

            pipeline.AddCellValueMapper(item1);
            pipeline.AddCellValueMapper(item2);
            Assert.Equal(new ICellValueMapper[] { item1, item2 }, pipeline.CellValueMappers);
        }
Пример #5
0
 public RetryPolicy(string condition, int count, TimeSpan interval, bool?firstFastRetry = null) : base("retry")
 {
     Attributes.Add("condition", condition);
     Attributes.Add("count", count.ToString());
     Attributes.Add("interval", interval.GetSeconds());
     if (firstFastRetry != null)
     {
         Attributes.Add("first-fast-retry", BoolMapper.Map(firstFastRetry.Value));
     }
 }
Пример #6
0
        public void AddCellValueMapper_ValidItem_Success()
        {
            MemberInfo propertyInfo = typeof(TestClass).GetProperty(nameof(TestClass.Value));
            var        propertyMap  = new SingleExcelPropertyMap(propertyInfo);
            var        item1        = new BoolMapper();
            var        item2        = new BoolMapper();

            propertyMap.AddCellValueMapper(item1);
            propertyMap.AddCellValueMapper(item2);
            Assert.Equal(new ICellValueMapper[] { item1, item2 }, propertyMap.CellValueMappers);
        }
Пример #7
0
        public void AddCellValueMapper_ValidItem_Success()
        {
            var reader = new ColumnNameValueReader("Column");
            var map    = new SubOneToOneMap <int>(reader);
            var item1  = new BoolMapper();
            var item2  = new BoolMapper();

            map.AddCellValueMapper(item1);
            map.AddCellValueMapper(item2);
            Assert.Equal(new ICellValueMapper[] { item1, item2 }, map.CellValueMappers);
        }
Пример #8
0
        private static bool AutoMap(this Type memberType, FallbackStrategy emptyValueStrategy, out ICellValueMapper mapper, out IFallbackItem emptyFallback, out IFallbackItem invalidFallback)
        {
            Type type = memberType.GetNullableTypeOrThis(out bool isNullable);

            Type[] interfaces = type.GetTypeInfo().ImplementedInterfaces.ToArray();

            IFallbackItem ReconcileFallback(FallbackStrategy strategyToPursue, bool empty)
            {
                // Empty nullable values should be set to null.
                if (empty && isNullable)
                {
                    return(new FixedValueFallback(null));
                }
                else if (strategyToPursue == FallbackStrategy.SetToDefaultValue || emptyValueStrategy == FallbackStrategy.SetToDefaultValue)
                {
                    return(new FixedValueFallback(type.DefaultValue()));
                }
                else
                {
                    Debug.Assert(emptyValueStrategy == FallbackStrategy.ThrowIfPrimitive);

                    // The user specified that we should set to the default value if it was empty.
                    return(new ThrowFallback());
                }
            }

            if (type == typeof(DateTime))
            {
                mapper          = new DateTimeMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type == typeof(bool))
            {
                mapper          = new BoolMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type.GetTypeInfo().IsEnum)
            {
                mapper          = new EnumMapper(type);
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type == typeof(string) || type == typeof(object) || type == typeof(IConvertible))
            {
                mapper          = new StringMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: false);
            }
            else if (type == typeof(Uri))
            {
                mapper          = new UriMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (interfaces.Any(t => t == typeof(IConvertible)))
            {
                mapper          = new ChangeTypeMapper(type);
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else
            {
                mapper          = null;
                emptyFallback   = null;
                invalidFallback = null;
                return(false);
            }

            return(true);
        }
Пример #9
0
 protected TAttributesBuilder AddAttribute(string name, bool value)
 {
     return(AddAttribute(name, BoolMapper.Map(value)));
 }