diff --git a/files-csharp/WriteToCsv/WriteToCsv/Contact.cs b/files-csharp/WriteToCsv/WriteToCsv/Contact.cs index b1c114085e..d139f8d059 100644 --- a/files-csharp/WriteToCsv/WriteToCsv/Contact.cs +++ b/files-csharp/WriteToCsv/WriteToCsv/Contact.cs @@ -1,8 +1,10 @@ -namespace WritingCSV +using CsvHelper; + +namespace WritingCSV; + +[CsvRecord] +public partial class Contact { - public class Contact - { - public int Id { get; set; } - public string Name { get; set; } = string.Empty; - } + public int Id { get; set; } + public string Name { get; set; } = string.Empty; } diff --git a/files-csharp/WriteToCsv/WriteToCsv/Employee.cs b/files-csharp/WriteToCsv/WriteToCsv/Employee.cs index d34be1b031..6110a97626 100644 --- a/files-csharp/WriteToCsv/WriteToCsv/Employee.cs +++ b/files-csharp/WriteToCsv/WriteToCsv/Employee.cs @@ -1,46 +1,40 @@ -using CsvHelper.Configuration.Attributes; +using CsvHelper; -namespace WritingCSV +namespace WritingCSV; + +// The class-level [CsvOptions] takes effect only through options built from +// the class, CsvWriterOptions.For(); a writer created with the +// default options ignores it. +[CsvRecord] +[CsvOptions(Separator = ';', HasHeader = false)] +public partial class Employee { - // The two class-level attributes take effect only through a - // configuration built with the class's attributes, for example - // CsvConfiguration.FromAttributes(CultureInfo.InvariantCulture); - // a CsvWriter built from a CultureInfo alone ignores them. - [Delimiter(";")] - [HasHeaderRecord(false)] - public class Employee - { - [Name("Identifier")] - [Index(0)] - public int Id { get; set; } - - [Name("Full Name")] - [Index(1)] - public string Name { get; set; } = string.Empty; - - [Index(2)] - [Format("yyyy-MM-dd")] - public DateTime Hired { get; set; } - - [Index(3)] - [CultureInfo("en-US")] - [Format("C")] - public decimal Salary { get; set; } - - [Index(4)] - [BooleanTrueValues("yes")] - [BooleanFalseValues("no")] - public bool Active { get; set; } - - [Index(5)] - [Constant("EU")] - public string Region { get; set; } = string.Empty; - - [Index(6)] - [NullValues("N/A")] - public string? Nickname { get; set; } - - [Ignore] - public string Notes { get; set; } = string.Empty; - } + [CsvColumn("Identifier", Index = 0)] + public int Id { get; set; } + + [CsvColumn("Full Name", Index = 1)] + public string Name { get; set; } = string.Empty; + + [CsvColumn(Index = 2)] + [CsvFormat("yyyy-MM-dd")] + public DateTime Hired { get; set; } + + [CsvColumn(Index = 3)] + [CsvCulture("de-DE")] + public decimal Salary { get; set; } + + [CsvColumn(Index = 4)] + [CsvBooleanValues("yes", "no")] + public bool Active { get; set; } + + [CsvColumn(Index = 5)] + [CsvConstant("EU")] + public string Region { get; set; } = string.Empty; + + [CsvColumn(Index = 6)] + [CsvNullValues("N/A")] + public string? Nickname { get; set; } + + [CsvIgnore] + public string Notes { get; set; } = string.Empty; } diff --git a/files-csharp/WriteToCsv/WriteToCsv/Person.cs b/files-csharp/WriteToCsv/WriteToCsv/Person.cs index 1e98c6afc8..ede110d820 100644 --- a/files-csharp/WriteToCsv/WriteToCsv/Person.cs +++ b/files-csharp/WriteToCsv/WriteToCsv/Person.cs @@ -1,22 +1,16 @@ -using CsvHelper.Configuration.Attributes; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using CsvHelper; -namespace WritingCSV +namespace WritingCSV; + +[CsvRecord] +public partial class Person { - public class Person - { - [Name("Identifier")] - [Index(0)] - public int Id { get; set; } - [Index(2)] - public string Name { get; set; } = string.Empty; - [Index(1)] - public bool IsLiving { get; set; } - [Format("yyyy-MM-dd")] - public DateTime DateOfBirth { get; set; } - } + [CsvColumn("Identifier", Index = 0)] + public int Id { get; set; } + [CsvColumn(Index = 2)] + public string Name { get; set; } = string.Empty; + [CsvColumn(Index = 1)] + public bool IsLiving { get; set; } + [CsvFormat("yyyy-MM-dd")] + public DateTime DateOfBirth { get; set; } } diff --git a/files-csharp/WriteToCsv/WriteToCsv/PersonMap.cs b/files-csharp/WriteToCsv/WriteToCsv/PersonMap.cs index f70769a462..83e3255b26 100644 --- a/files-csharp/WriteToCsv/WriteToCsv/PersonMap.cs +++ b/files-csharp/WriteToCsv/WriteToCsv/PersonMap.cs @@ -1,14 +1,22 @@ -using CsvHelper.Configuration; +using CsvHelper; -namespace WritingCSV +namespace WritingCSV.Mapped; + +// A class we cannot annotate, such as one from another library. +public class Person +{ + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public bool IsLiving { get; set; } +} + +[CsvRecordMap(typeof(Person))] +public partial class PersonMap { - public sealed class PersonMap : ClassMap - { - public PersonMap() - { - Map(p => p.Id).Name("Identifier").Index(0); - Map(p => p.IsLiving).Index(1); - Map(p => p.Name).Index(2); - } - } + [CsvColumn("Identifier", Index = 0)] + public int Id { get; set; } + [CsvColumn(Index = 2)] + public string Name { get; set; } = string.Empty; + [CsvColumn(Index = 1)] + public bool IsLiving { get; set; } } diff --git a/files-csharp/WriteToCsv/WriteToCsv/WriteMethods.cs b/files-csharp/WriteToCsv/WriteToCsv/WriteMethods.cs index 5b2e8babba..f4955efbb6 100644 --- a/files-csharp/WriteToCsv/WriteToCsv/WriteMethods.cs +++ b/files-csharp/WriteToCsv/WriteToCsv/WriteMethods.cs @@ -1,138 +1,118 @@ using CsvHelper; -using CsvHelper.Configuration; -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace WritingCSV + +namespace WritingCSV; + +public static class WriteMethods { - public static class WriteMethods + public static void WritePersonsWithDoB() { + using var csv = CsvWriter.ToFile("filePersonsWithDoB.csv"); - public static void WritePersonsWithDoB() - { - var myPersons = new List() - { - new Person { Id = 1, IsLiving = true, Name = "John", DateOfBirth = Convert.ToDateTime("03/05/2006") }, - new Person { Id = 2, IsLiving = true, Name = "Steve", DateOfBirth = Convert.ToDateTime("03/09/1998") }, - new Person { Id = 3, IsLiving = true, Name = "James", DateOfBirth = Convert.ToDateTime("03/08/1994") } - }; + csv.WriteRecords(CreatePersons()); + } - using (var writer = new StreamWriter("filePersonsWithDoB.csv")) - using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) - { - csv.WriteRecords(myPersons); - } + public static List CreatePersons() => + [ + new Person { Id = 1, IsLiving = true, Name = "John", DateOfBirth = new DateTime(2006, 3, 5) }, + new Person { Id = 2, IsLiving = true, Name = "Steve", DateOfBirth = new DateTime(1998, 3, 9) }, + new Person { Id = 3, IsLiving = true, Name = "James", DateOfBirth = new DateTime(1994, 3, 8) } + ]; - } + public static void WriteWithDefaultSettings(string path = "filePersons.csv") + { + using var csv = CsvWriter.ToFile(path); + + csv.WriteRecords(CreatePersons()); + } - public static List CreatePersons() => new() + public static void WriteWithoutHeader(string path = "filePersons.csv") + { + var options = new CsvWriterOptions { - new Person { Id = 1, IsLiving = true, Name = "John", DateOfBirth = Convert.ToDateTime("03/05/2006") }, - new Person { Id = 2, IsLiving = true, Name = "Steve", DateOfBirth = Convert.ToDateTime("03/09/1998") }, - new Person { Id = 3, IsLiving = true, Name = "James", DateOfBirth = Convert.ToDateTime("03/08/1994") } + HasHeader = false }; - public static void WriteWithDefaultSettings(string path = "filePersons.csv") - { - using (var writer = new StreamWriter(path)) - using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) - { - csv.WriteRecords(CreatePersons()); - } - } + using var csv = CsvWriter.ToFile(path, options); - public static void WriteWithoutHeader(string path = "filePersons.csv") - { - var configPersons = new CsvConfiguration(CultureInfo.InvariantCulture) - { - HasHeaderRecord = false - }; + csv.WriteRecords(CreatePersons()); + } - using (var writer = new StreamWriter(path)) - using (var csv = new CsvWriter(writer, configPersons)) - { - csv.WriteRecords(CreatePersons()); - } - } + public static void WriteWithRecordMap(string path = "filePersonsMapped.csv") + { + List persons = + [ + new() { Id = 1, IsLiving = true, Name = "John" }, + new() { Id = 2, IsLiving = true, Name = "Steve" }, + new() { Id = 3, IsLiving = true, Name = "James" } + ]; - public static void WriteWithClassMap(string path = "filePersons.csv") - { - using var writer = new StreamWriter(path); - using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture); + using var csv = CsvWriter.ToFile(path); - csv.Context.RegisterClassMap(); - csv.WriteRecords(CreatePersons()); - } + csv.WriteRecords(persons); + } - public static void AppendPersons(string path = "filePersons.csv") + public static void AppendPersons(string path = "filePersons.csv") + { + var options = new CsvWriterOptions { - var configPersons = new CsvConfiguration(CultureInfo.InvariantCulture) - { - HasHeaderRecord = false - }; + HasHeader = false + }; - using (var stream = File.Open(path, FileMode.Append)) - using (var writer = new StreamWriter(stream)) - using (var csv = new CsvWriter(writer, configPersons)) - { - csv.WriteRecords(CreatePersons()); - } - } + using var stream = File.Open(path, FileMode.Append); + using var csv = CsvWriter.Create(stream, options); - public static string WriteTrickyValues() - { - var trickyContacts = new List() - { - new Contact { Id = 1, Name = "Doe, John" }, - new Contact { Id = 2, Name = "He said \"hi\"" }, - new Contact { Id = 3, Name = "line1\nline2" } - }; - - using var writer = new StringWriter(); - using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) - { - csv.WriteRecords(trickyContacts); - } + csv.WriteRecords(CreatePersons()); + } - return writer.ToString(); + public static string WriteTrickyValues(CsvWriterOptions? options = null) + { + List trickyContacts = + [ + new Contact { Id = 1, Name = "Doe, John" }, + new Contact { Id = 2, Name = "He said \"hi\"" }, + new Contact { Id = 3, Name = "line1\nline2" }, + new Contact { Id = 4, Name = "=1+1" } + ]; + + using var writer = new StringWriter(); + using (var csv = CsvWriter.Create(writer, options, leaveOpen: true)) + { + csv.WriteRecords(trickyContacts); } - public static async Task WritePersonsAsync(string path = "filePersons.csv") - { - await using var writer = new StreamWriter(path); - await using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture); + return writer.ToString(); + } - await csv.WriteRecordsAsync(CreatePersons()); - } + public static async Task WritePersonsAsync(string path = "filePersons.csv") + { + await using var csv = CsvWriter.ToFile(path); - public static string WriteEmployees(IWriterConfiguration configuration) - { - var employees = new List() - { - new Employee - { - Id = 1, - Name = "Doe, John", - Hired = new DateTime(2020, 3, 5), - Salary = 1234.50m, - Active = true, - Region = "US", - Nickname = null, - Notes = "not written" - } - }; - - using var writer = new StringWriter(); - using (var csv = new CsvWriter(writer, configuration)) + await csv.WriteRecordsAsync(CreatePersons()); + } + + public static string WriteEmployees(CsvWriterOptions? options = null) + { + List employees = + [ + new Employee { - csv.WriteRecords(employees); + Id = 1, + Name = "Doe, John", + Hired = new DateTime(2020, 3, 5), + Salary = 1234.50m, + Active = true, + Region = "US", + Nickname = null, + Notes = "not written" } + ]; - return writer.ToString(); + using var writer = new StringWriter(); + using (var csv = CsvWriter.Create(writer, options, leaveOpen: true)) + { + csv.WriteRecords(employees); } + + return writer.ToString(); } } diff --git a/files-csharp/WriteToCsv/WriteToCsv/WriteToCsv.csproj b/files-csharp/WriteToCsv/WriteToCsv/WriteToCsv.csproj index 45c7dd411b..58b4a1703f 100644 --- a/files-csharp/WriteToCsv/WriteToCsv/WriteToCsv.csproj +++ b/files-csharp/WriteToCsv/WriteToCsv/WriteToCsv.csproj @@ -2,13 +2,13 @@ Exe - net10.0 + net11.0 enable enable - + diff --git a/files-csharp/WriteToCsv/WriteToCsvTests/WriteMethodsTests.cs b/files-csharp/WriteToCsv/WriteToCsvTests/WriteMethodsTests.cs index a374de81fa..919e339b39 100644 --- a/files-csharp/WriteToCsv/WriteToCsvTests/WriteMethodsTests.cs +++ b/files-csharp/WriteToCsv/WriteToCsvTests/WriteMethodsTests.cs @@ -1,142 +1,155 @@ -using System; using System.Globalization; using System.IO; using System.Linq; using System.Threading.Tasks; -using CsvHelper.Configuration; +using CsvHelper; using Microsoft.VisualStudio.TestTools.UnitTesting; using WritingCSV; -namespace WriteToCsvTests +namespace WriteToCsvTests; + +[TestClass] +public class WriteMethodsTests { - [TestClass] - public class WriteMethodsTests + private static readonly string[] DataRows = + [ + "1,True,John,2006-03-05", + "2,True,Steve,1998-03-09", + "3,True,James,1994-03-08" + ]; + + private static string NewTempPath() => + Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + + [TestMethod] + public void GivenPersons_WhenWritingWithDefaultSettings_ThenHeaderComesFromTheClass() { - private static readonly string[] DataRows = + var path = NewTempPath(); + try { - "1,True,John,2006-03-05", - "2,True,Steve,1998-03-09", - "3,True,James,1994-03-08" - }; - - private static string NewTempPath() => - Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + WriteMethods.WriteWithDefaultSettings(path); - [TestMethod] - public void GivenPersons_WhenWritingWithDefaultSettings_ThenHeaderComesFromTheClass() + var expected = "Identifier,IsLiving,Name,DateOfBirth\r\n" + string.Join("\r\n", DataRows) + "\r\n"; + Assert.AreEqual(expected, File.ReadAllText(path)); + } + finally { - var path = NewTempPath(); - try - { - WriteMethods.WriteWithDefaultSettings(path); - - var expected = "Identifier,IsLiving,Name,DateOfBirth\r\n" + string.Join("\r\n", DataRows) + "\r\n"; - Assert.AreEqual(expected, File.ReadAllText(path)); - } - finally - { - File.Delete(path); - } + File.Delete(path); } + } - [TestMethod] - public void GivenNoHeaderConfig_WhenWriting_ThenFirstLineIsData() + [TestMethod] + public void GivenNoHeaderOptions_WhenWriting_ThenFirstLineIsData() + { + var path = NewTempPath(); + try { - var path = NewTempPath(); - try - { - WriteMethods.WriteWithoutHeader(path); - - CollectionAssert.AreEqual(DataRows, File.ReadAllLines(path)); - } - finally - { - File.Delete(path); - } - } + WriteMethods.WriteWithoutHeader(path); - [TestMethod] - public void GivenClassMap_WhenWriting_ThenHeaderAndOrderFollowTheMap() + CollectionAssert.AreEqual(DataRows, File.ReadAllLines(path)); + } + finally { - var path = NewTempPath(); - try - { - WriteMethods.WriteWithClassMap(path); - - var expected = "Identifier,IsLiving,Name\r\n1,True,John\r\n2,True,Steve\r\n3,True,James\r\n"; - Assert.AreEqual(expected, File.ReadAllText(path)); - } - finally - { - File.Delete(path); - } + File.Delete(path); } + } - [TestMethod] - public void GivenAFileWithoutHeader_WhenAppending_ThenRowsAreAddedAtTheEnd() + [TestMethod] + public void GivenARecordMap_WhenWriting_ThenHeaderAndOrderFollowTheMap() + { + var path = NewTempPath(); + try { - var path = NewTempPath(); - try - { - WriteMethods.WriteWithoutHeader(path); - WriteMethods.AppendPersons(path); - - CollectionAssert.AreEqual(DataRows.Concat(DataRows).ToArray(), File.ReadAllLines(path)); - } - finally - { - File.Delete(path); - } + WriteMethods.WriteWithRecordMap(path); + + var expected = "Identifier,IsLiving,Name\r\n1,True,John\r\n2,True,Steve\r\n3,True,James\r\n"; + Assert.AreEqual(expected, File.ReadAllText(path)); } + finally + { + File.Delete(path); + } + } - [TestMethod] - public void GivenTrickyValues_WhenWriting_ThenFieldsAreQuotedAndQuotesDoubled() + [TestMethod] + public void GivenAFileWithoutHeader_WhenAppending_ThenRowsAreAddedAtTheEnd() + { + var path = NewTempPath(); + try { - var expected = "Id,Name\r\n" - + "1,\"Doe, John\"\r\n" - + "2,\"He said \"\"hi\"\"\"\r\n" - + "3,\"line1\nline2\"\r\n"; + WriteMethods.WriteWithoutHeader(path); + WriteMethods.AppendPersons(path); - Assert.AreEqual(expected, WriteMethods.WriteTrickyValues()); + CollectionAssert.AreEqual(DataRows.Concat(DataRows).ToArray(), File.ReadAllLines(path)); } - - [TestMethod] - public async Task GivenPersons_WhenWritingAsync_ThenFileMatchesTheSynchronousWrite() + finally { - var syncPath = NewTempPath(); - var asyncPath = NewTempPath(); - try - { - WriteMethods.WriteWithDefaultSettings(syncPath); - await WriteMethods.WritePersonsAsync(asyncPath); - - CollectionAssert.AreEqual(File.ReadAllBytes(syncPath), File.ReadAllBytes(asyncPath)); - } - finally - { - File.Delete(syncPath); - File.Delete(asyncPath); - } + File.Delete(path); } + } + + [TestMethod] + public void GivenTrickyValues_WhenWriting_ThenFieldsAreQuotedAndQuotesDoubled() + { + var expected = "Id,Name\r\n" + + "1,\"Doe, John\"\r\n" + + "2,\"He said \"\"hi\"\"\"\r\n" + + "3,\"line1\nline2\"\r\n" + + "4,=1+1\r\n"; + + Assert.AreEqual(expected, WriteMethods.WriteTrickyValues()); + } + + [TestMethod] + public void GivenAFormula_WhenInjectionIsEscape_ThenAnApostropheIsAdded() + { + var csv = WriteMethods.WriteTrickyValues(new CsvWriterOptions { Injection = CsvInjection.Escape }); + + StringAssert.EndsWith(csv, "4,\"'=1+1\"\r\n"); + } - [TestMethod] - public void GivenAttributedEmployee_WhenWritingWithACulture_ThenClassLevelAttributesAreIgnored() + [TestMethod] + public void GivenAFrenchFormatProvider_WhenWriting_ThenTheSeparatorStaysAComma() + { + var csv = WriteMethods.WriteEmployees(new CsvWriterOptions { FormatProvider = new CultureInfo("fr-FR") }); + + StringAssert.StartsWith(csv, "Identifier,Full Name,"); + } + + [TestMethod] + public async Task GivenPersons_WhenWritingAsync_ThenFileMatchesTheSynchronousWrite() + { + var syncPath = NewTempPath(); + var asyncPath = NewTempPath(); + try { - var csv = WriteMethods.WriteEmployees(new CsvConfiguration(CultureInfo.InvariantCulture)); + WriteMethods.WriteWithDefaultSettings(syncPath); + await WriteMethods.WritePersonsAsync(asyncPath); - var expected = "Identifier,Full Name,Hired,Salary,Active,Region,Nickname\r\n" - + "1,\"Doe, John\",2020-03-05,\"$1,234.50\",yes,EU,N/A\r\n"; - Assert.AreEqual(expected, csv); + CollectionAssert.AreEqual(File.ReadAllBytes(syncPath), File.ReadAllBytes(asyncPath)); } - - [TestMethod] - public void GivenAttributedEmployee_WhenWritingFromAttributes_ThenClassLevelAttributesApply() + finally { - var configuration = CsvConfiguration.FromAttributes(CultureInfo.InvariantCulture); + File.Delete(syncPath); + File.Delete(asyncPath); + } + } - var csv = WriteMethods.WriteEmployees(configuration); + [TestMethod] + public void GivenAttributedEmployee_WhenWritingWithDefaultOptions_ThenTheClassLevelAttributeIsIgnored() + { + var csv = WriteMethods.WriteEmployees(); - Assert.AreEqual("1;Doe, John;2020-03-05;$1,234.50;yes;EU;N/A\r\n", csv); - } + var expected = "Identifier,Full Name,Hired,Salary,Active,Region,Nickname\r\n" + + "1,\"Doe, John\",2020-03-05,\"1234,50\",yes,EU,N/A\r\n"; + Assert.AreEqual(expected, csv); + } + + [TestMethod] + public void GivenAttributedEmployee_WhenWritingWithOptionsForTheClass_ThenTheClassLevelAttributeApplies() + { + var csv = WriteMethods.WriteEmployees(CsvWriterOptions.For()); + + Assert.AreEqual("1;Doe, John;2020-03-05;1234,50;yes;EU;N/A\r\n", csv); } } diff --git a/files-csharp/WriteToCsv/WriteToCsvTests/WriteToCsvTests.cs b/files-csharp/WriteToCsv/WriteToCsvTests/WriteToCsvTests.cs index ac21db28cb..e5eee948dc 100644 --- a/files-csharp/WriteToCsv/WriteToCsvTests/WriteToCsvTests.cs +++ b/files-csharp/WriteToCsv/WriteToCsvTests/WriteToCsvTests.cs @@ -1,49 +1,28 @@ using CsvHelper; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Text; using WritingCSV; -namespace WriteToCsvTests +namespace WriteToCsvTests; + +[TestClass] +public class WriteToCsvTests { - [TestClass] - public class WriteToCsvTests + [TestMethod] + public void GivenListOfPersons_WhenWritingPersonsToCsv_ThenOutputShouldHaveCorrectFormat() { - [TestMethod] - public void givenListOfPersons_whenWritingPersonsToCsv_ThenOutputShouldHaveCorrectFormat() + using var memoryStream = new MemoryStream(); + using (var csv = CsvWriter.Create(memoryStream, leaveOpen: true)) { - var myPersons = new List() - { - new Person { Id = 1, IsLiving = true, Name = "John", DateOfBirth = Convert.ToDateTime("03/05/2006") }, - new Person { Id = 2, IsLiving = true, Name = "Steve", DateOfBirth = Convert.ToDateTime("03/09/1998") }, - new Person { Id = 3, IsLiving = true, Name = "James", DateOfBirth = Convert.ToDateTime("03/08/1994") } - }; - - string outputString; - using( var memoryStream = new MemoryStream()) - { - using (var writer = new StreamWriter(memoryStream)) - using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) - { - csv.WriteRecords(myPersons); - } - - outputString = Encoding.ASCII.GetString(memoryStream.ToArray()); - } - - //remove newlines (either \r\n or \n for win resp linux) as it might issues when comparing - outputString = outputString.Replace("\n", "").Replace("\r",""); - var expectedString = @"Identifier,IsLiving,Name,DateOfBirth -1,True,John,2006-03-05 -2,True,Steve,1998-03-09 -3,True,James,1994-03-08 -".Replace("\n", "").Replace("\r", ""); + csv.WriteRecords(WriteMethods.CreatePersons()); + } - Assert.AreEqual(expectedString, outputString, ignoreCase: false, CultureInfo.InvariantCulture); + var expected = "Identifier,IsLiving,Name,DateOfBirth\r\n" + + "1,True,John,2006-03-05\r\n" + + "2,True,Steve,1998-03-09\r\n" + + "3,True,James,1994-03-08\r\n"; - } + Assert.AreEqual(expected, Encoding.UTF8.GetString(memoryStream.ToArray())); } -} \ No newline at end of file +} diff --git a/files-csharp/WriteToCsv/WriteToCsvTests/WriteToCsvTests.csproj b/files-csharp/WriteToCsv/WriteToCsvTests/WriteToCsvTests.csproj index c21fac0092..2ab99a77b7 100644 --- a/files-csharp/WriteToCsv/WriteToCsvTests/WriteToCsvTests.csproj +++ b/files-csharp/WriteToCsv/WriteToCsvTests/WriteToCsvTests.csproj @@ -1,14 +1,14 @@ - net10.0 + net11.0 enable false - +