public void ConvertWithIndexEndTest()
        {
            var config = new CsvConfiguration {
                HasHeaderRecord = false
            };
            var rowMock       = new Mock <ICsvReaderRow>();
            var currentRecord = new[] { "1", "one", "1", "2", "3" };

            rowMock.Setup(m => m.Configuration).Returns(config);
            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).GetProperty("List"))
            {
                Index    = 2,
                IndexEnd = 3
            };

            data.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var converter  = new IEnumerableGenericConverter();
            var enumerable = (IEnumerable <int?>)converter.ConvertFromString("1", rowMock.Object, data);
            var list       = enumerable.ToList();

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
        }
Beispiel #2
0
        public void ConvertNoIndexEndTest()
        {
            var config = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
            {
                HasHeaderRecord = false
            };
            var currentRecord = new[] { "1", "one", "1", "2", "3" };
            var rowMock       = new Mock <IReaderRow>();
            var context       = new ReadingContext(new StringReader(string.Empty), config, false)
            {
                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).GetTypeInfo().GetProperty("List"))
            {
                Index = 2
            };

            data.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var converter  = new IEnumerableGenericConverter();
            var enumerable = (IEnumerable <int?>)converter.ConvertFromString("1", rowMock.Object, data);
            var list       = enumerable.ToList();

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
            Assert.AreEqual(3, list[2]);
        }