Ejemplo n.º 1
0
        public ObjectReader(Stream stream,
                            Serializer.ReadMethods readMethods,
                            SwapList objectsForSurrogates,
                            IDictionary <Type, Recipe> recipes,
                            Action <object> postDeserializationCallback,
                            bool treatCollectionAsUserObject,
                            VersionToleranceLevel versionToleranceLevel,
                            bool useBuffering,
                            bool disableStamping,
                            ReferencePreservation referencePreservation,
                            bool forceStampVerification)
        {
            this.readMethods = readMethods;
            this.postDeserializationCallback = postDeserializationCallback;
            this.treatCollectionAsUserObject = treatCollectionAsUserObject;
            this.referencePreservation       = referencePreservation;
            this.objectsForSurrogates        = objectsForSurrogates;
            this.recipes = recipes;

            VersionToleranceLevel = versionToleranceLevel;
            types      = new List <TypeDescriptor>();
            Methods    = new IdentifiedElementsList <MethodDescriptor>(this);
            Assemblies = new IdentifiedElementsList <AssemblyDescriptor>(this);
            Modules    = new IdentifiedElementsList <ModuleDescriptor>(this);
            HashCodeBasedWaitingValues   = new Dictionary <int, (object Dictionary, object Value)>();
            latePostDeserializationHooks = new List <Action>();

            reader = new PrimitiveReader(stream, useBuffering);
            surrogatesWhileReading = new OneToOneMap <int, object>();

            readTypeMethod         = disableStamping ? (Func <TypeDescriptor>)ReadSimpleTypeDescriptor : ReadFullTypeDescriptor;
            ForceStampVerification = forceStampVerification;
        }
Ejemplo n.º 2
0
        public void WithDateFormats_NullableEmptyFormats_ThrowsArgumentException()
        {
            OneToOneMap <DateTime?> propertyMap = Map(t => t.NullableDateValue);

            Assert.Throws <ArgumentException>("formats", () => propertyMap.WithDateFormats(new string[0]));
            Assert.Throws <ArgumentException>("formats", () => propertyMap.WithDateFormats(new List <string>()));
        }
Ejemplo n.º 3
0
        public void Ctor_MemberNotFieldOrProperty_ThrowsArgumentException()
        {
            MemberInfo eventInfo = typeof(ClassWithEvent).GetEvent(nameof(ClassWithEvent.Event));
            var        map       = new OneToOneMap <int>(new ColumnNameValueReader("Property"));

            Assert.Throws <ArgumentException>("member", () => new ExcelPropertyMap(eventInfo, map));
        }
        public ObjectReader(Stream stream, Serializer.ReadMethods readMethods, SwapList objectsForSurrogates = null, Action <object> postDeserializationCallback = null,
                            bool treatCollectionAsUserObject            = false,
                            VersionToleranceLevel versionToleranceLevel = 0, bool useBuffering = true, bool disableStamping = false,
                            ReferencePreservation referencePreservation = ReferencePreservation.Preserve,
                            bool forceStampVerification = false)
        {
            this.readMethods = readMethods;
            this.postDeserializationCallback = postDeserializationCallback;
            this.treatCollectionAsUserObject = treatCollectionAsUserObject;
            this.referencePreservation       = referencePreservation;
            this.objectsForSurrogates        = objectsForSurrogates ?? new SwapList();

            VersionToleranceLevel = versionToleranceLevel;
            types      = new List <TypeDescriptor>();
            Methods    = new IdentifiedElementsList <MethodDescriptor>(this);
            Assemblies = new IdentifiedElementsList <AssemblyDescriptor>(this);
            Modules    = new IdentifiedElementsList <ModuleDescriptor>(this);
            latePostDeserializationHooks = new List <Action>();

            reader = new PrimitiveReader(stream, useBuffering);
            surrogatesWhileReading = new OneToOneMap <int, object>();

            readTypeMethod         = disableStamping ? (Func <TypeDescriptor>)ReadSimpleTypeDescriptor : ReadFullTypeDescriptor;
            ForceStampVerification = forceStampVerification;
        }
Ejemplo n.º 5
0
        public void Ctor_PropertyReadOnly_ThrowsArgumentException()
        {
            MemberInfo propertyInfo = typeof(ClassWithEvent).GetProperty(nameof(ClassWithEvent.ReadOnlyProperty));
            var        map          = new OneToOneMap <int>(new ColumnNameValueReader("Property"));

            Assert.Throws <ArgumentException>("member", () => new ExcelPropertyMap(propertyInfo, map));
        }
Ejemplo n.º 6
0
        public void WithDateFormats_NullFormats_ThrowsArgumentNullException()
        {
            OneToOneMap <DateTime> propertyMap = Map(t => t.DateValue);

            Assert.Throws <ArgumentNullException>("formats", () => propertyMap.WithDateFormats(null));
            Assert.Throws <ArgumentNullException>("formats", () => propertyMap.WithDateFormats((IEnumerable <string>)null));
        }
Ejemplo n.º 7
0
        public void WithDateFormats_NullableNullFormats_ThrowsArgumentNullException()
        {
            OneToOneMap <DateTime?> mapping = Map(t => t.NullableDateValue);

            Assert.Throws <ArgumentNullException>("formats", () => mapping.WithDateFormats(null));
            Assert.Throws <ArgumentNullException>("formats", () => mapping.WithDateFormats((IEnumerable <string>)null));
        }
Ejemplo n.º 8
0
        public void WithColumnNameMatching_ValidColumnName_Success()
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value).WithColumnNameMatching(e => e == "ColumnName");

            Assert.Same(propertyMap, propertyMap.WithColumnNameMatching(e => e == "ColumnName"));

            Assert.IsType <ColumnNameMatchingValueReader>(propertyMap.CellReader);
        }
Ejemplo n.º 9
0
        public void WithConverter_NullConverter_ThrowsArgumentNullException()
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value);

            ConvertUsingSimpleMapperDelegate <string> converter = null;

            Assert.Throws <ArgumentNullException>("converter", () => propertyMap.WithConverter(converter));
        }
Ejemplo n.º 10
0
        public void WithThrowingInvalidFallback_Invoke_Success()
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithThrowingInvalidFallback());

            Assert.IsType <ThrowFallback>(propertyMap.Pipeline.InvalidFallback);
        }
Ejemplo n.º 11
0
        public void WithInvalidFallbackItem_ValidFallbackItem_Success()
        {
            IFallbackItem        fallback    = new FixedValueFallback(10);
            OneToOneMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithInvalidFallbackItem(fallback));

            Assert.Same(fallback, propertyMap.Pipeline.InvalidFallback);
        }
Ejemplo n.º 12
0
        public void WithCellValueMappers_ValidMappers_ThrowsArgumentNullException()
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value);
            ICellValueMapper     mapper1     = Assert.Single(propertyMap.Pipeline.CellValueMappers);
            ICellValueMapper     mapper2     = new BoolMapper();;

            Assert.Same(propertyMap, propertyMap.WithCellValueMappers(mapper2));
            Assert.Equal(new ICellValueMapper[] { mapper1, mapper2 }, propertyMap.Pipeline.CellValueMappers);
        }
Ejemplo n.º 13
0
        public void WithMapper_ValidMapper_Success()
        {
            var reader = new ColumnNameValueReader("ColumnName");
            OneToOneMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithReader(reader));

            Assert.Same(reader, propertyMap.CellReader);
        }
Ejemplo n.º 14
0
        public void WithInvalidFallback_Invoke_Success()
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithInvalidFallback("abc"));

            FixedValueFallback fallback = Assert.IsType <FixedValueFallback>(propertyMap.Pipeline.InvalidFallback);

            Assert.Equal("abc", fallback.Value);
        }
Ejemplo n.º 15
0
        public void WithColumnIndex_ValidColumnIndex_Success(int columnIndex)
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithColumnIndex(columnIndex));

            ColumnIndexValueReader reader = Assert.IsType <ColumnIndexValueReader>(propertyMap.CellReader);

            Assert.Equal(columnIndex, reader.ColumnIndex);
        }
Ejemplo n.º 16
0
        public void MakeOptional_HasMapper_ReturnsExpected()
        {
            var innerReader = new ColumnIndexValueReader(1);
            OneToOneMap <string> propertyMap = Map(t => t.Value).WithReader(innerReader);

            Assert.False(propertyMap.Optional);
            Assert.Same(propertyMap, propertyMap.MakeOptional());
            Assert.True(propertyMap.Optional);
            Assert.Same(innerReader, propertyMap.CellReader);
        }
Ejemplo n.º 17
0
        public void WithColumnName_ValidColumnName_Success()
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithColumnName("ColumnName"));

            ColumnNameValueReader reader = Assert.IsType <ColumnNameValueReader>(propertyMap.CellReader);

            Assert.Equal("ColumnName", reader.ColumnName);
        }
Ejemplo n.º 18
0
        public void WithDateFormats_NullableAutoMappedIEnumerableString_Success(IEnumerable <string> formats)
        {
            OneToOneMap <DateTime?> propertyMap = Map(t => t.NullableDateValue);

            Assert.Same(propertyMap, propertyMap.WithDateFormats(formats));

            DateTimeMapper item = propertyMap.Pipeline.CellValueMappers.OfType <DateTimeMapper>().Single();

            Assert.Equal(formats, item.Formats);
        }
Ejemplo n.º 19
0
        public void WithReader_OptionalColumn_Success()
        {
            var innerReader = new ColumnNameValueReader("ColumnName");
            OneToOneMap <string> propertyMap = Map(t => t.Value).MakeOptional();

            Assert.True(propertyMap.Optional);
            Assert.Same(propertyMap, propertyMap.WithReader(innerReader));
            Assert.True(propertyMap.Optional);
            Assert.Same(innerReader, propertyMap.CellReader);
        }
Ejemplo n.º 20
0
        public void WithTrim_Invoke_Success()
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithTrim());

            ICellValueTransformer transformer = Assert.Single(propertyMap.Pipeline.CellValueTransformers);

            Assert.IsType <TrimCellValueTransformer>(transformer);
        }
Ejemplo n.º 21
0
        public void Item_SetNull_ThrowsArgumentNullException()
        {
            MemberInfo propertyInfo             = typeof(TestClass).GetProperty(nameof(TestClass.Property));
            var        map                      = new OneToOneMap <int>(new ColumnNameValueReader("Property"));
            var        propertyMap              = new ExcelPropertyMap(propertyInfo, map);
            ExcelPropertyMapCollection mappings = new TestClassMap().Properties;

            mappings.Add(propertyMap);

            Assert.Throws <ArgumentNullException>("item", () => mappings[0] = null);
        }
Ejemplo n.º 22
0
        public void WithColumnIndex_OptionalColumn_Success()
        {
            OneToOneMap <string> propertyMap = Map(t => t.Value).MakeOptional();

            Assert.True(propertyMap.Optional);
            Assert.Same(propertyMap, propertyMap.WithColumnIndex(1));
            Assert.True(propertyMap.Optional);

            ColumnIndexValueReader innerReader = Assert.IsType <ColumnIndexValueReader>(propertyMap.CellReader);

            Assert.Equal(1, innerReader.ColumnIndex);
        }
Ejemplo n.º 23
0
        public void WithDateFormats_NullableAutoMappedStringArray_Success(IEnumerable <string> formats)
        {
            var formatsArray = formats.ToArray();

            OneToOneMap <DateTime?> propertyMap = Map(t => t.NullableDateValue);

            Assert.Same(propertyMap, propertyMap.WithDateFormats(formatsArray));

            DateTimeMapper item = propertyMap.Pipeline.CellValueMappers.OfType <DateTimeMapper>().Single();

            Assert.Same(formatsArray, item.Formats);
        }
Ejemplo n.º 24
0
 protected ProcedureCommand(Bulka.CommandLine.CommandLine commandLine, string[] argumentNames) : base(commandLine)
 {
     _argumentIndexes = new OneToOneMap <string, int>();
     for (var i = 0; i < argumentNames.Length; i++)
     {
         var name = argumentNames[i];
         if (_argumentIndexes.ContainsKey(name))
         {
             throw new Exception($"Duplicate argument '{name}'");
         }
         _argumentIndexes.Add(name, i);
     }
 }
Ejemplo n.º 25
0
        public CommandLine(IEqualityComparer <string> nameComparer = null)
        {
            _commands = new OneToOneMap <string, Command>(nameComparer);
            _listener = new Interpreter(this);

            _lexer  = new CommandLineLexer(null);
            _parser = new CommandLineParser(null);

            _lexer.RemoveErrorListeners();
            _parser.RemoveErrorListeners();

            _lexer.AddErrorListener(this);
            _parser.AddErrorListener(this);
        }
Ejemplo n.º 26
0
        public void Item_SetValidItem_GetReturnsExpected()
        {
            MemberInfo propertyInfo             = typeof(TestClass).GetProperty(nameof(TestClass.Property));
            var        map1                     = new OneToOneMap <int>(new ColumnNameValueReader("Property"));
            var        propertyMap1             = new ExcelPropertyMap(propertyInfo, map1);
            var        map2                     = new OneToOneMap <int>(new ColumnNameValueReader("Property"));
            var        propertyMap2             = new ExcelPropertyMap(propertyInfo, map2);
            ExcelPropertyMapCollection mappings = new TestClassMap().Properties;

            mappings.Add(propertyMap1);

            mappings[0] = propertyMap2;
            Assert.Same(propertyMap2, mappings[0]);
        }
Ejemplo n.º 27
0
        public void Ctor_FieldInfoMember_Success()
        {
            MemberInfo fieldInfo = typeof(ClassWithEvent).GetField(nameof(ClassWithEvent._field));
            var        map       = new OneToOneMap <int>(new ColumnNameValueReader("Property"));

            var propertyMap = new ExcelPropertyMap(fieldInfo, map);

            Assert.Same(fieldInfo, propertyMap.Member);
            Assert.Same(map, propertyMap.Map);

            var instance = new ClassWithEvent();

            propertyMap.SetValueFactory(instance, 10);
            Assert.Equal(10, instance._field);
        }
Ejemplo n.º 28
0
        internal static bool TryCreatePrimitiveMap <T>(MemberInfo member, FallbackStrategy emptyValueStrategy, out OneToOneMap <T> map)
        {
            if (!TryGetWellKnownMap(typeof(T), emptyValueStrategy, out ICellValueMapper mapper, out IFallbackItem emptyFallback, out IFallbackItem invalidFallback))
            {
                map = null;
                return(false);
            }

            ISingleCellValueReader defaultReader = GetDefaultSingleCellValueReader(member);

            map = new OneToOneMap <T>(defaultReader)
                  .WithCellValueMappers(mapper)
                  .WithEmptyFallbackItem(emptyFallback)
                  .WithInvalidFallbackItem(invalidFallback);
            return(true);
        }
Ejemplo n.º 29
0
        public void WithMapping_ValidReader_Success()
        {
            var dictionaryMapping = new Dictionary <string, DateTime> {
                { "key", DateTime.MinValue }
            };
            StringComparer comparer = StringComparer.CurrentCultureIgnoreCase;

            OneToOneMap <DateTime> propertyMap = Map(t => t.DateValue);

            Assert.Same(propertyMap, propertyMap.WithMapping(dictionaryMapping, comparer));

            DictionaryMapper <DateTime> item = propertyMap.Pipeline.CellValueMappers.OfType <DictionaryMapper <DateTime> >().Single();

            Assert.NotSame(dictionaryMapping, item.MappingDictionary);
            Assert.Equal(dictionaryMapping, item.MappingDictionary);

            Assert.Same(comparer, Assert.IsType <Dictionary <string, DateTime> >(item.MappingDictionary).Comparer);
        }
Ejemplo n.º 30
0
        public void WithConverter_InvalidConverter_ReturnsExpected()
        {
            ConvertUsingSimpleMapperDelegate <string> converter = stringValue =>
            {
                Assert.Equal("stringValue", stringValue);
                throw new NotSupportedException();
            };

            OneToOneMap <string> propertyMap = Map(t => t.Value);

            Assert.Same(propertyMap, propertyMap.WithConverter(converter));
            ConvertUsingMapper item = propertyMap.Pipeline.CellValueMappers.OfType <ConvertUsingMapper>().Single();

            object value = 1;
            PropertyMapperResultType result = item.Converter(new ReadCellValueResult(-1, "stringValue"), ref value);

            Assert.Equal(PropertyMapperResultType.Invalid, result);
            Assert.Equal(1, value);
        }