Esempio n. 1
0
        public void CsvTableReader_Parsing()
        {
            CsvTableReader reader;
            string         table =
                @"Col1,Col2,Col3
10,true,25.20
no,10";

            reader = new CsvTableReader(new CsvReader(table));
            Assert.AreEqual(3, reader.ColumnMap.Count);
            Assert.AreEqual(0, reader.ColumnMap["Col1"]);
            Assert.AreEqual(1, reader.ColumnMap["Col2"]);
            Assert.AreEqual(2, reader.ColumnMap["Col3"]);

            Assert.AreEqual(3, reader.Columns.Count);
            Assert.AreEqual("Col1", reader.Columns[0]);
            Assert.AreEqual("Col2", reader.Columns[1]);
            Assert.AreEqual("Col3", reader.Columns[2]);

            Assert.IsNotNull(reader.ReadRow());
            Assert.AreEqual(10, reader.Parse("Col1", 0));
            Assert.AreEqual(true, reader.Parse("Col2", false));
            Assert.AreEqual(25.20, reader.Parse("Col3", 0.0));

            Assert.IsNotNull(reader.ReadRow());
            Assert.AreEqual(false, reader.Parse("Col1", true));
            Assert.AreEqual(10, reader.Parse("Col2", 0));

            Assert.IsNull(reader.ReadRow());
            Assert.IsNull(reader.ReadRow());
        }
Esempio n. 2
0
        public void CsvTableReader_MissingColumns()
        {
            CsvTableReader reader;
            string         table =
                @"Col1,Col2,Col2
10,true,25.20
no,10";

            reader = new CsvTableReader(new CsvReader(table));

            Assert.IsNotNull(reader.ReadRow());
            Assert.AreEqual(10, reader.Parse("Col1", 0));
            Assert.AreEqual(true, reader.Parse("Col2", false));
            Assert.AreEqual(100, reader.Parse("ColX", 100));
        }
Esempio n. 3
0
        public void CsvTableWriter_BlankRow()
        {
            string path = Path.GetTempFileName();

            try
            {
                using (var writer = new CsvTableWriter(new string[] { "Col0", "Col1", "Col2" }, new FileStream(path, FileMode.Create), Encoding.UTF8))
                {
                    writer.Set("Col0", "(0,0)");
                    writer.Set("Col1", "(1,0)");
                    writer.Set("Col2", "(2,0)");
                    writer.WriteRow();

                    writer.WriteRow();

                    writer.Set("Col0", "(0,2)");
                    writer.Set("Col1", "(1,2)");
                    writer.Set("Col2", "(2,2)");
                    writer.WriteRow();
                }

                using (var reader = new CsvTableReader(new FileStream(path, FileMode.Open), Encoding.UTF8))
                {
                    Assert.IsTrue(reader.ColumnMap.ContainsKey("Col0"));
                    Assert.IsTrue(reader.ColumnMap.ContainsKey("Col1"));
                    Assert.IsTrue(reader.ColumnMap.ContainsKey("Col2"));

                    Assert.IsNotNull(reader.ReadRow());
                    Assert.AreEqual("(0,0)", reader.Parse("Col0", (string)null));
                    Assert.AreEqual("(1,0)", reader.Parse("Col1", (string)null));
                    Assert.AreEqual("(2,0)", reader.Parse("Col2", (string)null));

                    Assert.IsNotNull(reader.ReadRow());
                    Assert.AreEqual("", reader.Parse("Col0", (string)null));
                    Assert.AreEqual("", reader.Parse("Col1", (string)null));
                    Assert.AreEqual("", reader.Parse("Col2", (string)null));

                    Assert.IsNotNull(reader.ReadRow());
                    Assert.AreEqual("(0,2)", reader.Parse("Col0", (string)null));
                    Assert.AreEqual("(1,2)", reader.Parse("Col1", (string)null));
                    Assert.AreEqual("(2,2)", reader.Parse("Col2", (string)null));

                    Assert.IsNull(reader.ReadRow());
                }
            }
            finally
            {
                Helper.DeleteFile(path);
            }
        }
Esempio n. 4
0
        public void CsvTableReader_CaseInsensitivity()
        {
            CsvTableReader reader;
            string         table =
                @"Col1,Col2,Col3
10,true,25.20
no,10";

            reader = new CsvTableReader(new CsvReader(table));

            Assert.IsNotNull(reader.ReadRow());
            Assert.AreEqual(10, reader.Parse("col1", 0));
            Assert.AreEqual(true, reader.Parse("col2", false));
            Assert.AreEqual(25.20, reader.Parse("col3", 0.0));

            Assert.IsNotNull(reader.ReadRow());
            Assert.AreEqual(false, reader.Parse("col1", true));
            Assert.AreEqual(10, reader.Parse("col2", 0));

            Assert.IsNull(reader.ReadRow());
            Assert.IsNull(reader.ReadRow());
        }
Esempio n. 5
0
        public void CsvTableReader_DuplicateColumns()
        {
            CsvTableReader reader;
            string         table =
                @"Col1,Col2,Col2
10,true,25.20
no,10";

            reader = new CsvTableReader(new CsvReader(table));
            Assert.AreEqual(2, reader.ColumnMap.Count);
            Assert.AreEqual(0, reader.ColumnMap["Col1"]);
            Assert.AreEqual(1, reader.ColumnMap["Col2"]);

            Assert.IsNotNull(reader.ReadRow());
            Assert.AreEqual(10, reader.Parse("Col1", 0));
            Assert.AreEqual(true, reader.Parse("Col2", false));

            Assert.IsNotNull(reader.ReadRow());
            Assert.AreEqual(false, reader.Parse("Col1", true));
            Assert.AreEqual(10, reader.Parse("Col2", 0));

            Assert.IsNull(reader.ReadRow());
            Assert.IsNull(reader.ReadRow());
        }