Ejemplo n.º 1
0
        public void AliasTest1()
        {
            var sb = new StringBuilder("Given Name, Family Name, Age, Height").AppendLine();

            sb.Append("Homer,   \"Simpson\"  , 41, 5.6").AppendLine();
            sb.Append("\"Lisa Anne\", Simpson, 7, 3.9").AppendLine();

            var aliases = new Dictionary <string, string>
            {
                ["Given Name"]  = "FirstName",
                ["Family Name"] = "LastName"
            };

            var csv = new CsvImporter
            {
                ColumnNameSpaceReplacement = " "
            };

            csv.ImportString(sb.ToString());

            Assert.AreEqual(2, csv.GetData().Count, "Should be 2 lines of data");

            var list = csv.GetData <CTestPerson>(aliases).ToList();

            Assert.AreEqual(2, list.Count, "List count should also be 2");

            list[0].TestPerson("Homer", "Simpson", 41, 5.6);
            list[1].TestPerson("Lisa Anne", "Simpson", 7, 3.9);
        }
Ejemplo n.º 2
0
        public void BlankLinesTest()
        {
            var sb = new StringBuilder("FirstName, Last   Name, Age, Height").AppendLine();

            sb.AppendLine();
            sb.Append("Homer,   \"Simpson\"  , 41, 5.6").AppendLine();
            sb.Append("          ").AppendLine();
            sb.AppendLine();
            sb.Append("\"Lisa Anne\", Simpson, 7, 3.9").AppendLine();
            sb.Append("          ").AppendLine();
            sb.Append("          ").AppendLine();
            sb.AppendLine();
            sb.Append("          ").AppendLine();
            sb.AppendLine();
            sb.Append("     ");

            var csv = new CsvImporter
            {
                ColumnNameSpaceReplacement = null
            };

            csv.ImportString(sb.ToString());

            Assert.AreEqual(2, csv.GetData().Count, "Should be 2 lines of data");

            var list = csv.GetData <CTestPerson>().ToList();

            Assert.AreEqual(2, list.Count, "List count should also be 2");

            list[0].TestPerson("Homer", "Simpson", 41, 5.6);
            list[1].TestPerson("Lisa Anne", "Simpson", 7, 3.9);
        }
Ejemplo n.º 3
0
        public void EmptyObjectTest()
        {
            var x = new CsvImporter();

            Assert.IsNull(x.GetColumnNames(), "Column Names");
            Assert.IsNull(x.GetData(), "Get Data");
        }
Ejemplo n.º 4
0
        public void UnknownColumnNameTest()
        {
            var invalidColumnFound = false;
            var invalidColumnCount = 0;

            var sb = new StringBuilder("FirstName, Last_Name, Age, Height").AppendLine();

            sb.Append("Homer,   \"Simpson\"  , 41, 5.6").AppendLine();
            sb.Append("\"Lisa Anne\", Simpson, 7, 3.9").AppendLine();
            sb.Append("     ");

            var csv = new CsvImporter
            {
                ColumnNameSpaceReplacement = null
            };

            csv.OnUnusableColumn += _colname =>
            {
                invalidColumnCount++;
                if (_colname == "Last_Name")
                {
                    invalidColumnFound = true;
                }
            };

            csv.ImportString(sb.ToString());
            csv.GetData <CTestPerson>().ToList();

            Assert.AreEqual(1, invalidColumnCount, "Invalid column count wrong");
            Assert.IsTrue(invalidColumnFound, "Didn't find the invalid column");
        }
Ejemplo n.º 5
0
        public void ColumnHeaderTest()
        {
            var s   = "First Name, Last  Name, Age";
            var csv = new CsvImporter();
            var onHeaderReadCalled = false;

            csv.OnHeaderRead += _sb =>
            {
                Assert.AreEqual(s, _sb.ToString(), "Header string should match");
                onHeaderReadCalled = true;
            };
            csv.ColumnNameSpaceReplacement = "_";

            csv.ImportString(s);

            Assert.IsTrue(onHeaderReadCalled, "Should have called OnHeaderRead lambda");

            var data = csv.GetData();

            Assert.AreEqual(0, data.Count, "No Data- should be zero length");

            var cols = csv.GetColumnNames();

            Assert.IsNotNull(cols);
            Assert.AreEqual(3, cols.Count, "Column Count");
            Assert.AreEqual("First_Name", cols[0], "0");
            Assert.AreEqual("Last__Name", cols[1], "1");
            Assert.AreEqual("Age", cols[2], "2");
        }
Ejemplo n.º 6
0
        public void FileIoTest()
        {
            var csv = new CsvImporter();

            csv.ImportFile("UnitTestCsvFileIo.csv");
            var list = csv.GetData <CTestPerson>().ToList();

            Assert.AreEqual(2, list.Count, "Number of data elements is incorrect");
            list[0].TestPerson("Homer", "Simpson", 41, 0);
            list[1].TestPerson("Bart", "Simpson", 6, 0);
        }
Ejemplo n.º 7
0
        public void PreProcessLineTest()
        {
            var linesRead = 0;

            var csv = new CsvImporter();

            csv.OnLineRead += _sb =>
            {
                linesRead++;
                for (var i = 0; i < _sb.Length; i++)
                {
                    _sb[i] = char.ToUpper(_sb[i]);
                }
            };
            csv.ImportFile("UnitTestCsvFileIo.csv");
            var list = csv.GetData <CTestPerson>().ToList();

            Assert.AreEqual(2, list.Count, "Number of data elements is incorrect");
            list[0].TestPerson("HOMER", "SIMPSON", 41, 0);
            list[1].TestPerson("BART", "SIMPSON", 6, 0);
            Assert.AreEqual(7, linesRead, "Number of lines preprocessed, including blank lines");
        }