public void TestLoadPropertiesAndFieldsSameName() { // It's ok to have newline with padding at start, should trim field names (not values) // Include spaces in string field value to prove all content preserved string csvData = @"StringMember, Hello World ,This is an ignored description,also ignored EnumMember,Blue,Something Something IntMember,1234,Comment here FloatMember,1.5,More commenting" ; TestPropertiesAndFieldsObject t = new TestPropertiesAndFieldsObject(); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(csvData))) { using (var sr = new StreamReader(ms)) { CsvUtil.LoadObject(sr, ref t); } } Assert.AreEqual(" Hello World ", t.StringMember); Assert.AreEqual(" Hello World ", t.GetStringField()); Assert.AreEqual(1234, t.IntMember); Assert.AreEqual(1234, t.GetIntField()); Assert.That(t.FloatMember, Is.InRange(1.4999f, 1.5001f)); // float imprecision Assert.That(t.GetFloatField(), Is.InRange(1.4999f, 1.5001f)); // float imprecision Assert.AreEqual(TestPropertiesAndFieldsObject.Colour.Blue, t.EnumMember); Assert.AreEqual(TestPropertiesAndFieldsObject.Colour.Blue, t.GetEnumField()); }
public void TestLoadStruct() { string csvData = @"s, Hello World ,This is an ignored description,also ignored f,1234.5,Comment here" ; TestStruct t = new TestStruct(); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(csvData))) { using (var sr = new StreamReader(ms)) { CsvUtil.LoadObject(sr, ref t); } } Assert.AreEqual(" Hello World ", t.s); Assert.That(t.f, Is.InRange(1234.4999f, 1234.5001f)); // float imprecision }
public void TestLoadSingleWithHeader() { // Test that we can include a header line if we want string csvData = @"#Field,#Value,#Description StringField, Hello World ,This is an ignored description,also ignored EnumField,Blue,Something Something IntField,1234,Comment here FloatField,1.5,More commenting" ; TestObject t = new TestObject(); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(csvData))) { using (var sr = new StreamReader(ms)) { CsvUtil.LoadObject(sr, t); } } Assert.AreEqual(" Hello World ", t.StringField); Assert.AreEqual(1234, t.IntField); Assert.That(t.FloatField, Is.InRange(1.4999f, 1.5001f)); // float imprecision Assert.AreEqual(TestObject.Colour.Blue, t.EnumField); }
public void TestLoadSingleEmbeddedCommas() { // It's ok to have newline with padding at start, should trim field names (not values) // Include spaces in string field value to prove all content preserved // Also put fields out of order string csvData = @"StringField,""Commas, commas everywhere,abcd"",Ignored,ignored EnumField,Purple,Something Something IntField,-5002,Comment here FloatField,-3.142,Pi Pi Baby" ; TestObject t = new TestObject(); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(csvData))) { using (var sr = new StreamReader(ms)) { CsvUtil.LoadObject(sr, t); } } Assert.AreEqual("Commas, commas everywhere,abcd", t.StringField); Assert.AreEqual(-5002, t.IntField); Assert.That(t.FloatField, Is.InRange(-3.142001f, -3.141999f)); // float imprecision Assert.AreEqual(TestObject.Colour.Purple, t.EnumField); }