Пример #1
0
        public void ReadBlackTest1()
        {
            using var csv = CsvUtility.ToStream(" ");

            using var reader = new CsvReader(new StreamReader(csv), hasHeader: false);
            Assert.IsNull(reader.Read());
        }
Пример #2
0
        public void TryResetTest()
        {
            using var csv = CsvUtility.ToStream(
                      "name,age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            using var reader = new CsvReader(new StreamReader(csv));

            // Force to consume all the records
            foreach (var _ in reader.ReadAll())
            {
            }

            Assert.IsTrue(reader.IsDone);
            Assert.IsTrue(reader.TryReset());
            Assert.IsFalse(reader.IsDone);

            var enumerator = reader.ReadAll().GetEnumerator();

            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual("Homer,35", enumerator.Current.ToString());

            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual("Marge,28", enumerator.Current.ToString());

            Assert.IsFalse(enumerator.MoveNext());
            Assert.IsTrue(reader.IsDone);
        }
Пример #3
0
        public void ReadBlackTest2()
        {
            using var csv = CsvUtility.ToStream(" ");

            var format = CsvFormat.Default.WithIgnoreWhitespace(false);

            using var reader = new CsvReader(new StreamReader(csv), hasHeader: false, format: format);
            Assert.AreEqual(" ", reader.Read() !.ToString());
        }
Пример #4
0
        public void ReadWithQuoteNeverTest()
        {
            using var csv = CsvUtility.ToStream(
                      "Name,Age\n" +
                      "Frida \"The Painter\", 35\n" +
                      "Pagannini \"The violinist\",28\n");

            var reader = CsvReader.FromStream(csv, CsvFormat.Default.WithStyle(QuoteStyle.Never));

            Assert.AreEqual("Frida The Painter,35", reader.Read() !.ToString());
            Assert.AreEqual("Pagannini The violinist,28", reader.Read() !.ToString());
        }
Пример #5
0
        public void ReadWithQuoteAlwaysTest()
        {
            using var csv = CsvUtility.ToStream(
                      "Name,Age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            var reader = CsvReader.FromStream(csv, CsvFormat.Default.WithStyle(QuoteStyle.Always));

            Assert.AreEqual("\"Homer\",\"35\"", reader.Read() !.ToString());
            Assert.AreEqual("\"Marge\",\"28\"", reader.Read() !.ToString());
        }
Пример #6
0
        public void ReadRecordWithWhiteSpaceTest()
        {
            using var csv = CsvUtility.ToStream(
                      "Name,Age\n" +
                      "Homer , 35\n" +
                      " Marge,28\n");

            using var reader = CsvReader.FromStream(csv, CsvFormat.Default.WithIgnoreWhitespace(false));

            Assert.AreEqual("Homer , 35", reader.Read() !.ToString());
            Assert.AreEqual(" Marge,28", reader.Read() !.ToString());
        }
Пример #7
0
        public void FromStreamTest()
        {
            using var csv = CsvUtility.ToStream(
                      "name,age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            using var reader = CsvReader.FromStream(csv);

            Assert.AreEqual(CsvFormat.Default, reader.Format);
            Assert.IsTrue(reader.HasHeader);
            Assert.IsFalse(reader.IsDone);
        }
Пример #8
0
        public void CsvReaderTest2()
        {
            using var csv = CsvUtility.ToStream(
                      "name,age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            using var reader = new CsvReader(new StreamReader(csv), hasHeader: false);

            Assert.AreEqual(CsvFormat.Default, reader.Format);
            Assert.IsFalse(reader.HasHeader);
            Assert.IsFalse(reader.IsDone);
        }
Пример #9
0
        public void ReadWithUnclosedQuoteTest()
        {
            using var csv = CsvUtility.ToStream(
                      "Name,Age\n" +
                      "Mario \"The plumber, 20\n" +
                      "Luigi, 19\n");

            using var reader = CsvReader.FromStream(csv);

            Assert.Throws <CsvFormatException>(() =>
            {
                var _ = reader.Read();
            });
        }
Пример #10
0
        public void CsvReaderTest1()
        {
            using var csv = CsvUtility.ToStream(
                      "name,age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            var format = new CsvFormat('\t', '\"');

            using var reader = new CsvReader(new StreamReader(csv), format);

            Assert.AreEqual(new CsvFormat('\t', '\"'), reader.Format);
            Assert.IsTrue(reader.HasHeader);
            Assert.IsFalse(reader.IsDone);
        }
Пример #11
0
        public void ReasAllAsTest()
        {
            using var csv = CsvUtility.ToStream(
                      "Name,Age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            using var reader = CsvReader.FromStream(csv);

            var persons = reader.ReadAllAs <Person>().ToList();

            Assert.AreEqual("Homer", persons[0].Name);
            Assert.AreEqual(35, persons[0].Age);

            Assert.AreEqual("Marge", persons[1].Name);
            Assert.AreEqual(28, persons[1].Age);
        }
Пример #12
0
        public void ReadTest()
        {
            using var csv = CsvUtility.ToStream(
                      "name,age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            using var reader = new CsvReader(new StreamReader(csv));
            Assert.IsFalse(reader.IsDone);
            Assert.AreEqual(0, reader.RecordNumber);

            Assert.AreEqual("name,age", reader.Header !.ToString());
            Assert.AreEqual("Homer,35", reader.Read() !.ToString());
            Assert.AreEqual("Marge,28", reader.Read() !.ToString());
            Assert.IsNull(reader.Read());
            Assert.IsTrue(reader.IsDone);
            Assert.AreEqual(2, reader.RecordNumber);
        }
Пример #13
0
        public async Task ReadAllAsyncTest()
        {
            using var csv = CsvUtility.ToStream(
                      "name,age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            using var reader = new CsvReader(new StreamReader(csv));
            var enumerator = reader.ReadAllAsync().GetAsyncEnumerator();

            Assert.IsTrue(await enumerator.MoveNextAsync());
            Assert.AreEqual("Homer,35", enumerator.Current !.ToString());

            Assert.IsTrue(await enumerator.MoveNextAsync());
            Assert.AreEqual("Marge,28", enumerator.Current.ToString());

            Assert.IsFalse(await enumerator.MoveNextAsync());
            Assert.IsTrue(reader.IsDone);
        }
Пример #14
0
        public async Task ReadAsyncTest()
        {
            using var csv = CsvUtility.ToStream(
                      "name,age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            using var reader = new CsvReader(new StreamReader(csv));
            Assert.IsFalse(reader.IsDone);

            Assert.AreEqual("name,age", reader.Header !.ToString());
            Assert.AreEqual("Homer,35", (await reader.ReadAsync()) !.ToString());
            Assert.AreEqual("Marge,28", (await reader.ReadAsync()) !.ToString());

            CsvRecord?record = await reader.ReadAsync();

            Assert.IsNull(record);
            Assert.IsTrue(reader.IsDone);
        }
Пример #15
0
        public void ReasAsTest()
        {
            using var csv = CsvUtility.ToStream(
                      "Name,Age\n" +
                      "Homer,35\n" +
                      "Marge,28\n");

            using var reader = CsvReader.FromStream(csv);

            Person p1 = reader.ReadAs <Person>();

            Assert.AreEqual("Homer", p1.Name);
            Assert.AreEqual(35, p1.Age);

            Person p2 = reader.ReadAs <Person>();

            Assert.AreEqual("Marge", p2.Name);
            Assert.AreEqual(28, p2.Age);

            Assert.IsFalse(reader.ReadAs <Person>().HasValue);
        }
Пример #16
0
        /// <summary>
        /// Creates a <see cref="CsvDocument"/> from the specified string.
        /// </summary>
        /// <param name="csv">The CSV.</param>
        /// <param name="format">The format.</param>
        /// <param name="flexible">if set to <c>true</c> will allow records of differents lengths.</param>
        /// <returns></returns>
        public static CsvDocument FromCsv(string csv, CsvFormat?format = null, bool flexible = false)
        {
            format ??= CsvFormat.Default;

            if (string.IsNullOrWhiteSpace(csv))
            {
                throw new ArgumentException("CSV is empty");
            }

            using MemoryStream memory = CsvUtility.ToStream(csv);

            using (CsvReader reader = new CsvReader(new StreamReader(memory), format))
            {
                List <CsvRecord>?records;

                if (flexible)
                {
                    records = reader.ReadAll().ToList();
                }
                else
                {
                    records = new List <CsvRecord>();
                    int headerLength = reader.Header !.Length;

                    foreach (CsvRecord r in reader.ReadAll())
                    {
                        int recordLength = r.Length;
                        if (recordLength != headerLength)
                        {
                            throw new InvalidOperationException($"Invalid record length for non-flexible csv, " +
                                                                $"expected {headerLength} but {recordLength} was get");
                        }

                        records.Add(r);
                    }
                }

                return(new CsvDocument(records, reader.Header !, format, flexible));
            }
        }