Exemplo n.º 1
0
        public static void CheckSampleData1(CsvRecordReader csv)
        {
            Assert.AreEqual(CsvReaderSampleData.SampleData1FieldCount, csv.FieldCount);

            if (csv.HasHeaders)
            {
                var headers = csv.GetFieldHeaders();
                Assert.AreEqual(0, headers.GetFieldIndex(SampleData1Header0));
                Assert.AreEqual(1, headers.GetFieldIndex(SampleData1Header1));
                Assert.AreEqual(2, headers.GetFieldIndex(SampleData1Header2));
                Assert.AreEqual(3, headers.GetFieldIndex(SampleData1Header3));
                Assert.AreEqual(4, headers.GetFieldIndex(SampleData1Header4));
                Assert.AreEqual(5, headers.GetFieldIndex(SampleData1Header5));
            }

            Assert.AreEqual(-1, csv.CurrentRecordIndex);

            int recordCount = 0;

            foreach (CsvRecord record in csv)
            {
                CheckSampleData1(csv.HasHeaders, csv.CurrentRecordIndex, record.ToArray());
                recordCount++;
            }

            if (csv.HasHeaders)
                Assert.AreEqual(CsvReaderSampleData.SampleData1RecordCount, recordCount);
            else
                Assert.AreEqual(CsvReaderSampleData.SampleData1RecordCount + 1, recordCount);
        }
Exemplo n.º 2
0
        public static void CheckSampleData1(CsvRecordReader csv)
        {
            Assert.AreEqual(CsvReaderSampleData.SampleData1FieldCount, csv.FieldCount);

            if (csv.HasHeaders)
            {
                var headers = csv.GetFieldHeaders();
                Assert.AreEqual(0, headers.GetFieldIndex(SampleData1Header0));
                Assert.AreEqual(1, headers.GetFieldIndex(SampleData1Header1));
                Assert.AreEqual(2, headers.GetFieldIndex(SampleData1Header2));
                Assert.AreEqual(3, headers.GetFieldIndex(SampleData1Header3));
                Assert.AreEqual(4, headers.GetFieldIndex(SampleData1Header4));
                Assert.AreEqual(5, headers.GetFieldIndex(SampleData1Header5));
            }

            Assert.AreEqual(-1, csv.CurrentRecordIndex);

            int recordCount = 0;

            foreach (CsvRecord record in csv)
            {
                CheckSampleData1(csv.HasHeaders, csv.CurrentRecordIndex, record.ToArray());
                recordCount++;
            }

            if (csv.HasHeaders)
            {
                Assert.AreEqual(CsvReaderSampleData.SampleData1RecordCount, recordCount);
            }
            else
            {
                Assert.AreEqual(CsvReaderSampleData.SampleData1RecordCount + 1, recordCount);
            }
        }
Exemplo n.º 3
0
        public void GetFieldHeadersTest_EmptyCsv()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("#asdf\n\n#asdf,asdf"), true))
            {
                var headers = csv.GetFieldHeaders();

                Assert.IsNotNull(headers);
                Assert.AreEqual(0, headers.Length);
            }
        }
Exemplo n.º 4
0
        public void GetFieldHeadersTest1()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), false))
            {
                var headers = csv.GetFieldHeaders();

                Assert.IsNull(headers);
                Assert.IsFalse(csv.HasHeaders);
            }
        }
Exemplo n.º 5
0
        public void GetFieldHeaders_WithEmptyHeaderNames(string defaultHeaderName)
        {
            if (defaultHeaderName == null)
            {
                defaultHeaderName = "Column";
            }

            using (var csv = new CsvRecordReader(new StringReader(",  ,,aaa,\"   \",,,"), true))
            {
                csv.DefaultHeaderName = defaultHeaderName;

                Assert.IsNull(csv.Read());
                Assert.AreEqual(8, csv.FieldCount);

                var headers = csv.GetFieldHeaders();
                Assert.AreEqual(csv.FieldCount, headers.Length);

                Assert.AreEqual("aaa", headers[3]);
                foreach (var index in new int[] { 0, 1, 2, 4, 5, 6, 7 })
                {
                    Assert.AreEqual(defaultHeaderName + index.ToString(), headers[index]);
                }
            }
        }
Exemplo n.º 6
0
        public void GetFieldHeadersTest2()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), true))
            {
                var headers = csv.GetFieldHeaders();

                Assert.IsNotNull(headers);
                Assert.AreEqual(CsvReaderSampleData.SampleData1RecordCount, headers.Length);

                Assert.AreEqual(CsvReaderSampleData.SampleData1Header0, headers[0]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header1, headers[1]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header2, headers[2]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header3, headers[3]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header4, headers[4]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header5, headers[5]);

                Assert.AreEqual(0, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header0));
                Assert.AreEqual(1, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header1));
                Assert.AreEqual(2, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header2));
                Assert.AreEqual(3, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header3));
                Assert.AreEqual(4, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header4));
                Assert.AreEqual(5, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header5));
            }
        }
Exemplo n.º 7
0
        public void GetFieldHeaders_WithEmptyHeaderNames(string defaultHeaderName)
        {
            if (defaultHeaderName == null)
                defaultHeaderName = "Column";

            using (var csv = new CsvRecordReader(new StringReader(",  ,,aaa,\"   \",,,"), true))
            {
                csv.DefaultHeaderName = defaultHeaderName;

                Assert.IsNull(csv.Read());
                Assert.AreEqual(8, csv.FieldCount);

                var headers = csv.GetFieldHeaders();
                Assert.AreEqual(csv.FieldCount, headers.Length);

                Assert.AreEqual("aaa", headers[3]);
                foreach (var index in new int[] { 0, 1, 2, 4, 5, 6, 7 })
                    Assert.AreEqual(defaultHeaderName + index.ToString(), headers[index]);
            }
        }
Exemplo n.º 8
0
        public void GetFieldHeadersTest_EmptyCsv()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader("#asdf\n\n#asdf,asdf"), true))
            {
                var headers = csv.GetFieldHeaders();

                Assert.IsNotNull(headers);
                Assert.AreEqual(0, headers.Length);
            }
        }
Exemplo n.º 9
0
        public void GetFieldHeadersTest2()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), true))
            {
                var headers = csv.GetFieldHeaders();

                Assert.IsNotNull(headers);
                Assert.AreEqual(CsvReaderSampleData.SampleData1RecordCount, headers.Length);

                Assert.AreEqual(CsvReaderSampleData.SampleData1Header0, headers[0]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header1, headers[1]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header2, headers[2]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header3, headers[3]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header4, headers[4]);
                Assert.AreEqual(CsvReaderSampleData.SampleData1Header5, headers[5]);

                Assert.AreEqual(0, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header0));
                Assert.AreEqual(1, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header1));
                Assert.AreEqual(2, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header2));
                Assert.AreEqual(3, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header3));
                Assert.AreEqual(4, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header4));
                Assert.AreEqual(5, headers.GetFieldIndex(CsvReaderSampleData.SampleData1Header5));
            }
        }
Exemplo n.º 10
0
        public void GetFieldHeadersTest1()
        {
            using (CsvRecordReader csv = new CsvRecordReader(new StringReader(CsvReaderSampleData.SampleData1), false))
            {
                var headers = csv.GetFieldHeaders();

                Assert.IsNull(headers);
                Assert.IsFalse(csv.HasHeaders);
            }
        }