public void TestReaderGetRecordCustomFieldsDefaultValues()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader))
                        {
                            writer.Write("Field1,Field2,Field3,Field4,Field5\r\n");
                            writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = false;
                            dtReader.ShouldSkipRecord = (row => row[0] == "Field1");
                            dtReader.AddColumn("F1");
                            dtReader.AddColumn("F2");
                            dtReader.AddColumn("F3");
                            dtReader.AddColumn("F4");
                            dtReader.AddColumn("F5");
                            dtReader.AddColumn("F6");
                            dtReader.AddColumn("F7", "defaultVal");

                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                            Assert.Equal("", trecord.Field6);
                            Assert.Equal("defaultVal", trecord.Field7);
                            Assert.Equal("F1", dtReader.FieldHeaders[0]);
                        }
        }
        public void TestReaderMapPropertiesWitoutHeaderRow()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader, '|'))
                        {
                            writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = false;
                            dtReader.MapProperty <TestRecord>(m => m.Field1).MappedColumnIndex = 0;
                            dtReader.MapProperty <TestRecord>(m => m.Field4).MappedColumnIndex = 3;
                            dtReader.MapProperty <TestRecord>(m => m.Field2).MappedColumnIndex = 1;
                            dtReader.MapProperty <TestRecord>(m => m.Field5).MappedColumnIndex = 4;
                            dtReader.MapProperty <TestRecord>(m => m.Field3).MappedColumnIndex = 2;
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                        }
        }
        public void TestReaderGetRecordShouldSkipRecord()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader))
                        {
                            writer.Write("HDR,AAAA11111XXX000-FFF\r]n");
                            writer.Write("Field1,Field2,Field3,Field4,Field5\r\n");
                            writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = true;
                            dtReader.ShouldSkipRecord = (row => row[0] == "HDR" || row[0] == "TRL");
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                        }
        }
        public void TestReaderMapPropertiesWithFunkyDate()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader))
                        {
                            writer.Write("Field1,Field2,Field3,Field4,Field5\r\n");
                            writer.Write("value1,100,true,\"20161231\", 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = true;
                            dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnIndex(0);
                            dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnIndex(1);
                            dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnIndex(2);
                            dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnIndex(3).TypeConverter(new DateTimeConverter()
                            {
                                Format = "yyyyMMdd"
                            });
                            dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnIndex(4);
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                        }
        }
        public void TestReaderMapPropertiesByName()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader, '|'))
                        {
                            writer.Write("X1|X2|X3|X4|X5\r\n");
                            writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = true;
                            dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnName("X1");
                            dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnName("X4");
                            dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnName("X2");
                            dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnName("X5");
                            dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnName("X3");
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                        }
        }
        public void TestReaderGetRecordPropertiesOutnumberFields()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader))
                        {
                            writer.Write("Field1,Field2\r\n");
                            writer.Write("value1,100\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = true;
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                        }
        }
        public void TestReaderGetRecordWithoutFieldHeadersOrPropertyMappingsNestedObject()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader))
                        {
                            writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = false;
                            dtReader.Read();
                            TestRecordData trecord = dtReader.GetRecord <TestRecordData>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Data1);
                            Assert.Equal(100, trecord.Data2);
                            Assert.True(trecord.Data4);
                        }
        }
        public void TestReaderGetRecordWithoutFieldHeadersOrPropertyMappings()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader))
                        {
                            writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = false;
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                        }
        }
        public void GetRecordCaseInsensitiveTest()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader))
                        {
                            writer.Write("field1,field2,Field3,Field4,Field5\r\n");
                            writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = true;
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                        }
        }
        public void TestReaderGetRecordPropertiesNotInFields()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader))
                        {
                            writer.Write("Field1,Field2,Field3,Field5\r\n");
                            writer.Write("value1,100,true, 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = true;
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.MinValue.ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                        }
        }
        public void TestReaderPipeDelimitedGetRecord()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader, '|'))
                        {
                            writer.Write("Field1|Field2|Field3|Field4|Field5\r\n");
                            writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = true;
                            dtReader.Read();
                            TestRecord trecord = dtReader.GetRecord <TestRecord>();
                            Assert.NotNull(trecord);
                            Assert.Equal("value1", trecord.Field1);
                            Assert.Equal(100, trecord.Field2);
                            Assert.True(trecord.Field3);
                            Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString());
                            Assert.Equal(25.76M, trecord.Field5);
                        }
        }
        public void TestReaderMapPropertiesByUnknownName()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var dtReader = new DelimitedTextReader(reader, '|'))
                        {
                            writer.Write("X1|X2|X3|X4|X5\r\n");
                            writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n");
                            writer.Flush();
                            stream.Position = 0;

                            dtReader.FirstRowIsHeader = true;
                            dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnName("X1");
                            dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnName("FOO");
                            dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnName("X2");
                            dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnName("X5");
                            dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnName("X3");
                            dtReader.Read();
                            TestRecord trecord;
                            Assert.Throws <DelimitedTextReaderMappingException>(() => trecord = dtReader.GetRecord <TestRecord>());
                        }
        }