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"); }
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); }
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); }
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"); }