public void ConvertWithIndexEndTest()
        {
            var config = new CsvHelper.Configuration.Configuration {
                HasHeaderRecord = false
            };
            var rowMock       = new Mock <IReaderRow>();
            var headers       = new[] { "Id", "Name", "Prop1", "Prop2", "Prop3" };
            var currentRecord = new[] { "1", "One", "1", "2", "3" };
            var context       = new ReadingContext(new StringReader(string.Empty), config, false)
            {
                HeaderRecord = headers,
                Record       = currentRecord
            };

            rowMock.Setup(m => m.Configuration).Returns(config);
            rowMock.Setup(m => m.Context).Returns(context);
            rowMock.Setup(m => m.GetField(It.IsAny <Type>(), It.IsAny <int>())).Returns <Type, int>((type, index) => Convert.ToInt32(currentRecord[index]));
            var data = new MemberMapData(typeof(Test).GetProperty("Dictionary"))
            {
                Index    = 2,
                IndexEnd = 3
            };

            data.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var converter  = new IDictionaryGenericConverter();
            var dictionary = (IDictionary)converter.ConvertFromString("1", rowMock.Object, data);

            Assert.AreEqual(2, dictionary.Count);
            Assert.AreEqual(1, dictionary["Prop1"]);
            Assert.AreEqual(2, dictionary["Prop2"]);
        }
예제 #2
0
        public void ConvertNoIndexEndTest()
        {
            var config = new CsvConfiguration {
                HasHeaderRecord = false
            };
            var rowMock       = new Mock <ICsvReaderRow>();
            var headers       = new[] { "Id", "Name", "Prop1", "Prop2", "Prop3" };
            var currentRecord = new[] { "1", "One", "1", "2", "3" };

            rowMock.Setup(m => m.Configuration).Returns(config);
            rowMock.Setup(m => m.FieldHeaders).Returns(headers);
            rowMock.Setup(m => m.CurrentRecord).Returns(currentRecord);
            rowMock.Setup(m => m.GetField(It.IsAny <Type>(), It.IsAny <int>())).Returns <Type, int>((type, index) => Convert.ToInt32(currentRecord[index]));
            var data = new CsvPropertyMapData(typeof(Test).GetTypeInfo().GetProperty("Dictionary"))
            {
                Index = 2
            };

            data.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var converter  = new IDictionaryGenericConverter();
            var dictionary = (IDictionary <string, int?>)converter.ConvertFromString("1", rowMock.Object, data);

            Assert.AreEqual(3, dictionary.Count);
            Assert.AreEqual(1, dictionary["Prop1"]);
            Assert.AreEqual(2, dictionary["Prop2"]);
            Assert.AreEqual(3, dictionary["Prop3"]);
        }