public void FromCsv_WrongLineCount() { dynamic element; List <string> csvContent = new List <string>() { "FirstName,LastName,Age,Street", "\"Hans\",\"Mu\"\"eller\",30,\"\"", "\"Hans\",\"Mu\"\"eller\",30,\"\"" }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age", -1), new DynamicTableColumn <string>("Street"), }; //import try { element = ExpandoObjectSerializer.FromCsv(csvContent, elementDefinitions); Assert.Fail(); } catch (FormatException) { } }
public void FromXml_Empty_Defintions() { dynamic element; List <string> xmlContent = new List <string>() { "<DynamicObject>" + Environment.NewLine, " <Properties>" + Environment.NewLine, " <Street value=\"\" />" + Environment.NewLine, " <LastName value=\"Mueller\" />" + Environment.NewLine, " <Age value=\"30\" />" + Environment.NewLine, " <FirstName value=\"Hans\" />" + Environment.NewLine, " </Properties>" + Environment.NewLine, "</DynamicObject>" }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>(); //import //import try { element = ExpandoObjectSerializer.FromXml(xmlContent, elementDefinitions); Assert.Fail(); } catch (FormatException) { } }
public void FromCsv_Semicolon_WithoutHeaders_WithoutQuotes() { dynamic element; List <string> csvContent = new List <string>() { "Hans;Mueller;30;" }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age", -1), new DynamicTableColumn <string>("Street"), }; //import element = ExpandoObjectSerializer.FromCsv(csvContent, elementDefinitions, false, ';', false); //compare IDictionary <string, object> elementDictionary = element; Assert.AreEqual(4, elementDictionary.Count); Assert.AreEqual("Hans", element.FirstName); Assert.AreEqual("Mueller", element.LastName); Assert.AreEqual(30, element.Age); Assert.AreEqual("", element.Street); }
public void FromXml_WrongPropertyNames() { dynamic element; List <string> csvContent = new List <string>() { "<DynamicObject>" + Environment.NewLine, " <Properties>" + Environment.NewLine, " <FirstName value=\"Hans\" />" + Environment.NewLine, " <WrongName value=\"Mueller\" />" + Environment.NewLine, " <Age value=\"30\" />" + Environment.NewLine, " <Street value=\"\" />" + Environment.NewLine, " </Properties>" + Environment.NewLine, "</DynamicObject>" }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age", -1), new DynamicTableColumn <string>("Street"), }; //import try { element = ExpandoObjectSerializer.FromXml(csvContent, elementDefinitions); Assert.Fail(); } catch (FormatException) { } }
public void FromCsv_SeparatorInField() { dynamic element; List <string> csvContent = new List <string>() { "FirstName,LastName,Age,Street", "\"Ha,ns\",\"Mu,\"\",\"\",\"\"\"\",eller\",30,\"\"\"\"\"\"" }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age", -1), new DynamicTableColumn <string>("Street"), }; //import element = ExpandoObjectSerializer.FromCsv(csvContent, elementDefinitions, true, ',', true); //compare IDictionary <string, object> elementDictionary = element; Assert.AreEqual(4, elementDictionary.Count); Assert.AreEqual("Ha,ns", element.FirstName); Assert.AreEqual("Mu,\",\",\"\",eller", element.LastName); Assert.AreEqual(30, element.Age); Assert.AreEqual("\"\"", element.Street); }
public void FromCsv_EmptyLineOrWhitespace() { dynamic element; List <string> csvContent = new List <string>() { null, "", "\"Hans\",\"Mueller\",30,\"\"", " " }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age", -1), new DynamicTableColumn <string>("Street"), }; //import element = ExpandoObjectSerializer.FromCsv(csvContent, elementDefinitions); //compare IDictionary <string, object> elementDictionary = element; Assert.AreEqual(4, elementDictionary.Count); Assert.AreEqual("Hans", element.FirstName); Assert.AreEqual("Mueller", element.LastName); Assert.AreEqual(30, element.Age); Assert.AreEqual("", element.Street); }
public void FromCsv_WithHeaders_OtherSortOrderInFile() { dynamic element; List <string> csvContent = new List <string>() { "Street,FirstName,Age,LastName", ",Hans,30,Mueller" }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age", -1), new DynamicTableColumn <string>("Street"), }; //import element = ExpandoObjectSerializer.FromCsv(csvContent, elementDefinitions, true, ',', false); //compare IDictionary <string, object> elementDictionary = element; Assert.AreEqual(4, elementDictionary.Count); Assert.AreEqual("Hans", element.FirstName); Assert.AreEqual("Mueller", element.LastName); Assert.AreEqual(30, element.Age); Assert.AreEqual("", element.Street); }
public void FromXml_Null_Definitions() { dynamic element; List <string> csvContent = new List <string>() { "<DynamicObject>" + Environment.NewLine, " <Properties>" + Environment.NewLine, " <FirstName value=\"Hans\" />" + Environment.NewLine, " <LastName value=\"Mueller\" />" + Environment.NewLine, " <Age value=\"30\" />" + Environment.NewLine, " <Street value=\"\" />" + Environment.NewLine, " </Properties>" + Environment.NewLine, "</DynamicObject>" }; //import try { element = ExpandoObjectSerializer.FromXml(csvContent, null); Assert.Fail(); } catch (ArgumentNullException) { } }
public void AsXml_Standard() { dynamic element; string xmlContent; string expectedContent; //set values element = new ExpandoObject(); element.FirstName = "Hans"; element.LastName = "Mueller"; element.Age = 30; element.Street = ""; //get xml xmlContent = ExpandoObjectSerializer.AsXml(element); //compare expectedContent = "<DynamicObject>" + Environment.NewLine + " <Properties>" + Environment.NewLine + " <FirstName value=\"Hans\" />" + Environment.NewLine + " <LastName value=\"Mueller\" />" + Environment.NewLine + " <Age value=\"30\" />" + Environment.NewLine + " <Street value=\"\" />" + Environment.NewLine + " </Properties>" + Environment.NewLine + "</DynamicObject>"; Assert.AreEqual(expectedContent, xmlContent); }
static void Main(string[] args) { dynamic element = new ExpandoObject(); element.FirstName = "John"; element.LastName = "Doe"; element.Age = 30; using (StreamWriter writer = new StreamWriter("test.csv")) { writer.Write(ExpandoObjectSerializer.AsCsv(element)); } ImportElementCsvFile(); IDynamicTable table = new DynamicTable(DynamicTableType.Expandable); dynamic row; row = new ExpandoObject(); row.FirstName = "John"; row.LastName = "Doe"; row.Age = 30; table.AddRow(row); row = new ExpandoObject(); row.FirstName = "Jane"; row.LastName = "Doe"; row.Street = "Main street"; table.AddRow(row); foreach (dynamic actualRow in table.Rows) { Console.WriteLine( string.Format("{0} {1} is {2} years old.", actualRow.FirstName, actualRow.LastName, actualRow.Age)); } using (StreamWriter writer = new StreamWriter("test.csv")) { writer.Write(table.AsCsv()); } using (StreamWriter writer = new StreamWriter("test.xml")) { writer.Write(table.AsXml()); } ImportTableCsvFile(); Console.ReadKey(); }
public void ExportAndImport_CSV_Semicolon_WithoutHeader_WithoutQuotes() { dynamic element; string csvExport; string fileName = _assemblyDirectory + @"\CsvTest.txt"; //define elements List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age"), new DynamicTableColumn <DateTime>("TimeStamp") }; //set values element = new ExpandoObject(); element.FirstName = "Hans"; element.LastName = "Mueller"; element.Age = 30; element.TimeStamp = new DateTime(2012, 12, 24, 1, 2, 3); //export csvExport = ExpandoObjectSerializer.AsCsv(element, false, ';', false); using (StreamWriter writer = new StreamWriter(fileName)) { writer.Write(csvExport); } //import using (StreamReader reader = new StreamReader(fileName)) { element = ExpandoObjectSerializer.FromCsv(ReadFile(reader), elementDefinitions, false, ';', false); } //compare IDictionary <string, object> elementDictionary = element; Assert.AreEqual(4, elementDictionary.Count); Assert.AreEqual("Hans", element.FirstName); Assert.AreEqual("Mueller", element.LastName); Assert.AreEqual(30, element.Age); Assert.AreEqual(2012, element.TimeStamp.Year); Assert.AreEqual(12, element.TimeStamp.Month); Assert.AreEqual(24, element.TimeStamp.Day); Assert.AreEqual(1, element.TimeStamp.Hour); Assert.AreEqual(2, element.TimeStamp.Minute); Assert.AreEqual(3, element.TimeStamp.Second); }
public void AsXml_Null() { dynamic element = null; string xmlContent; string expectedContent; //get xml xmlContent = ExpandoObjectSerializer.AsXml(element); //compare expectedContent = ""; Assert.AreEqual(expectedContent, xmlContent); }
public void AsXml_Empty() { dynamic element; string xmlContent; string expectedContent; //set values element = new ExpandoObject(); //get xml xmlContent = ExpandoObjectSerializer.AsXml(element); //compare expectedContent = ""; Assert.AreEqual(expectedContent, xmlContent); }
public void AsCsv_Empty_WithHeaders() { dynamic element; string csvContent; string expectedContent; //add values element = new ExpandoObject(); //get csv csvContent = ExpandoObjectSerializer.AsCsv(element); //compare expectedContent = ""; Assert.AreEqual(expectedContent, csvContent); }
public void FromXml_EmptyLineOrWhitespace() { dynamic element; List <string> csvContent = new List <string>() { "<DynamicObject>" + Environment.NewLine, "", " ", null, " <Properties>" + Environment.NewLine, " <FirstName value=\"Hans\" />" + Environment.NewLine, " <LastName value=\"Mueller\" />" + Environment.NewLine, " <Age value=\"30\" />" + Environment.NewLine, " <Street value=\"\" />" + Environment.NewLine, " </Properties>" + Environment.NewLine, "</DynamicObject>" }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age", -1), new DynamicTableColumn <string>("Street"), }; //import element = ExpandoObjectSerializer.FromXml(csvContent, elementDefinitions); //compare IDictionary <string, object> elementDictionary = element; Assert.AreEqual(4, elementDictionary.Count); Assert.AreEqual("Hans", element.FirstName); Assert.AreEqual("Mueller", element.LastName); Assert.AreEqual(30, element.Age); Assert.AreEqual("", element.Street); }
public void AsCsv_Semicolon_WithoutHeaders_WithoutQuotes() { dynamic element; string csvContent; string expectedContent; //add values element = new ExpandoObject(); element.FirstName = "Hans"; element.LastName = "Mueller"; element.Age = 30; element.Street = ""; //get csv csvContent = ExpandoObjectSerializer.AsCsv(element, false, ';', false); //compare expectedContent = "Hans;Mueller;30;" + Environment.NewLine; Assert.AreEqual(expectedContent, csvContent); }
public void FromXml_Null_Content() { dynamic element; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age", -1), new DynamicTableColumn <string>("Street"), }; //import element = ExpandoObjectSerializer.FromXml(null, elementDefinitions); //compare IDictionary <string, object> elementDictionary = element; Assert.AreEqual(0, elementDictionary.Count); }
public void AsCsv_Standard() { dynamic element; string csvContent; string expectedContent; //set values element = new ExpandoObject(); element.FirstName = "Hans"; element.LastName = "Mueller"; element.Age = 30; element.Street = ""; //get csv csvContent = ExpandoObjectSerializer.AsCsv(element); //compare expectedContent = "\"Hans\",\"Mueller\",30,\"\"" + Environment.NewLine; Assert.AreEqual(expectedContent, csvContent); }
public void AsCsv_Comma_WithHeaders_WithQuotes() { dynamic element; string csvContent; string expectedContent; //add values element = new ExpandoObject(); element.FirstName = "Hans"; element.LastName = "Mu\"eller"; element.Age = 30; element.Street = ""; //get csv csvContent = ExpandoObjectSerializer.AsCsv(element, true, ',', true); //compare expectedContent = "FirstName,LastName,Age,Street" + Environment.NewLine + "\"Hans\",\"Mu\"\"eller\",30,\"\"" + Environment.NewLine; Assert.AreEqual(expectedContent, csvContent); }
public void FromCsv_Empty_Defintions() { dynamic element; List <string> csvContent = new List <string>() { "FirstName,LastName,Age,Street", "\"Ha,ns\",\"Mu,\"\",\"\",\"\"\"\",eller\",30,\"\"\"\"\"\"" }; //columns List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>(); //import try { element = ExpandoObjectSerializer.FromCsv(csvContent, elementDefinitions); Assert.Fail(); } catch (FormatException) { } }
private static void ImportElementCsvFile() { dynamic element; //define elements List <IDynamicTableColumn> elementDefinitions = new List <IDynamicTableColumn>() { new DynamicTableColumn <string>("FirstName"), new DynamicTableColumn <string>("LastName"), new DynamicTableColumn <int>("Age") }; //import using (StreamReader reader = new StreamReader("test.csv")) { element = ExpandoObjectSerializer.FromCsv(ReadFile(reader), elementDefinitions); } Console.WriteLine( string.Format("{0} {1} is {2} years old.", element.FirstName, element.LastName, element.Age)); }