diff options
Diffstat (limited to 'ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing')
18 files changed, 2107 insertions, 0 deletions
diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/AnonymousTypesTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/AnonymousTypesTests.cs new file mode 100644 index 0000000..8ad8682 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/AnonymousTypesTests.cs @@ -0,0 +1,34 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using System.Collections; +using System.Globalization; +using System.IO; +using Xunit; + +namespace CsvHelper.Tests.Writing +{ + + public class AnonymousTypesTests + { + [Fact] + public void AnonymousIEnumerableTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + IEnumerable records = new ArrayList + { + new + { + Id = 1, + Name = "one", + } + }; + + csv.WriteRecords(records); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/ConstantTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/ConstantTests.cs new file mode 100644 index 0000000..6bf29e4 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/ConstantTests.cs @@ -0,0 +1,126 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Text; +using CsvHelper.Configuration; +using Xunit; + +namespace CsvHelper.Tests.Writing +{ + + public class ConstantTests + { + [Fact] + public void StringConstantTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + var records = new List<Test> + { + new Test { Id = 1, Name = "one" }, + new Test { Id = 2, Name = "two" } + }; + + csv.Context.RegisterClassMap<TestStringMap>(); + csv.WriteRecords(records); + writer.Flush(); + stream.Position = 0; + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("Id,Name"); + expected.AppendLine("1,constant"); + expected.AppendLine("2,constant"); + + var result = reader.ReadToEnd(); + + Assert.Equal(expected.ToString(), result); + } + } + + [Fact] + public void NullConstantTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + var records = new List<Test> + { + new Test { Id = 1, Name = "one" }, + }; + + csv.Context.RegisterClassMap<TestNullMap>(); + csv.WriteRecords(records); + writer.Flush(); + + Assert.Equal("1,\r\n", writer.ToString()); + } + } + + [Fact] + public void IntConstantTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + var records = new List<Test> + { + new Test { Id = 1, Name = "one" }, + }; + + csv.Context.RegisterClassMap<TestIntMap>(); + csv.WriteRecords(records); + writer.Flush(); + + Assert.Equal("-1,one\r\n", writer.ToString()); + } + } + + private class Test + { + public int Id { get; set; } + public string Name { get; set; } + } + + private sealed class TestIntMap : ClassMap<Test> + { + public TestIntMap() + { + Map(m => m.Id).Constant(-1); + Map(m => m.Name); + } + } + + private sealed class TestNullMap : ClassMap<Test> + { + public TestNullMap() + { + Map(m => m.Id); + Map(m => m.Name).Constant(null); + } + } + + private sealed class TestStringMap : ClassMap<Test> + { + public TestStringMap() + { + Map(m => m.Id); + Map(m => m.Name).Constant("constant"); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/CsvModeTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/CsvModeTests.cs new file mode 100644 index 0000000..65e5e49 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/CsvModeTests.cs @@ -0,0 +1,145 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CsvHelper.Tests.Writing +{ + + public class CsvModeTests + { + [Fact] + public void WriteField_EscapeMode_ContainsQuote_EscapesWithoutQuotingField() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + Mode = CsvMode.Escape, + Escape = '\\', + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("a\"b", true); + csv.Flush(); + + Assert.Equal("a\\\"b", writer.ToString()); + } + } + + [Fact] + public void WriteField_NoEscapeMode_ContainsQuote_EscapesWithoutQuotingField() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + Mode = CsvMode.NoEscape, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("a\"b", true); + csv.Flush(); + + Assert.Equal("a\"b", writer.ToString()); + } + } + + [Fact] + public void WriteField_EscapeMode_ContainsDelimiter_EscapesWithoutQuotingField() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + Mode = CsvMode.Escape, + Escape = '\\', + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("a,b", true); + csv.Flush(); + + Assert.Equal("a\\,b", writer.ToString()); + } + } + + [Fact] + public void WriteField_NoEscapeMode_ContainsDelimiter_EscapesWithoutQuotingField() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + Mode = CsvMode.NoEscape, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("a,b", true); + csv.Flush(); + + Assert.Equal("a,b", writer.ToString()); + } + } + + [Fact] + public void WriteField_EscapeMode_ContainsNewline_EscapesWithoutQuotingField() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + Mode = CsvMode.Escape, + Escape = '\\', + NewLine = "\n", + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("a\nb", true); + csv.Flush(); + + Assert.Equal("a\\\nb", writer.ToString()); + } + } + + [Fact] + public void WriteField_EscapeMode_Contains2CharNewline_EscapesWithoutQuotingField() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + Mode = CsvMode.Escape, + Escape = '\\', + NewLine = "\r\n", + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("a\r\nb", true); + csv.Flush(); + + Assert.Equal("a\\\r\\\nb", writer.ToString()); + } + } + + [Fact] + public void WriteField_NoEscapeMode_ContainsNewline_EscapesWithoutQuotingField() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + Mode = CsvMode.NoEscape, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("a\r\nb", true); + csv.Flush(); + + Assert.Equal("a\r\nb", writer.ToString()); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/DynamicTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/DynamicTests.cs new file mode 100644 index 0000000..1cf33dc --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/DynamicTests.cs @@ -0,0 +1,218 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using System.Collections.Generic; +using System.Dynamic; +using System.Globalization; +using System.IO; +using CsvHelper.Configuration; +using CsvHelper.Tests.Mocks; +using Xunit; + +namespace CsvHelper.Tests.Writing +{ + + public class DynamicTests + { + [Fact] + public void WriteDynamicExpandoObjectsTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + var list = new List<dynamic>(); + + dynamic obj = new ExpandoObject(); + obj.Id = 1; + obj.Name = "one"; + list.Add(obj); + + obj = new ExpandoObject(); + obj.Id = 2; + obj.Name = "two"; + list.Add(obj); + + csv.WriteRecords(list); + writer.Flush(); + stream.Position = 0; + + var expected = "Id,Name\r\n"; + expected += "1,one\r\n"; + expected += "2,two\r\n"; + + Assert.Equal(expected, reader.ReadToEnd()); + } + } + + [Fact] + public void WriteDynamicExpandoObjectTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + dynamic obj = new ExpandoObject(); + obj.Id = 1; + obj.Name = "one"; + + csv.WriteDynamicHeader(obj); + csv.NextRecord(); + + csv.WriteRecord(obj); + csv.NextRecord(); + + obj = new ExpandoObject(); + obj.Id = 2; + obj.Name = "two"; + + csv.WriteRecord(obj); + csv.NextRecord(); + + writer.Flush(); + stream.Position = 0; + + var expected = "Id,Name\r\n"; + expected += "1,one\r\n"; + expected += "2,two\r\n"; + + Assert.Equal(expected, reader.ReadToEnd()); + } + } + + [Fact] + public void WriteDynamicExpandoObjectHasDifferentPropertyOrderingTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + dynamic obj = new ExpandoObject(); + obj.Name = "one"; + obj.Id = 1; + + csv.WriteDynamicHeader(obj); + csv.NextRecord(); + + csv.WriteRecord(obj); + csv.NextRecord(); + + obj = new ExpandoObject(); + obj.Name = "two"; + obj.Id = 2; + + csv.WriteRecord(obj); + csv.NextRecord(); + + var expected = "Name,Id\r\n"; + expected += "one,1\r\n"; + expected += "two,2\r\n"; + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteDynamicIDynamicMetaObjectProviderHasDifferentPropertyOrderingTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + dynamic obj = new DynamicObjectMock(); + obj.Name = "one"; + obj.Id = 1; + + csv.WriteDynamicHeader(obj); + csv.NextRecord(); + + csv.WriteRecord(obj); + csv.NextRecord(); + + obj = new ExpandoObject(); + obj.Name = "two"; + obj.Id = 2; + + csv.WriteRecord(obj); + csv.NextRecord(); + + var expected = "Name,Id\r\n"; + expected += "one,1\r\n"; + expected += "two,2\r\n"; + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteDynamicExpandoObjectHasDifferentPropertyOrderingWithDynamicSortTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + DynamicPropertySort = Comparer<string>.Create((x, y) => x.CompareTo(y)), + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + dynamic obj = new ExpandoObject(); + obj.Name = "one"; + obj.Id = 1; + + csv.WriteDynamicHeader(obj); + csv.NextRecord(); + + csv.WriteRecord(obj); + csv.NextRecord(); + + obj = new ExpandoObject(); + obj.Id = 2; + obj.Name = "two"; + + csv.WriteRecord(obj); + csv.NextRecord(); + + var expected = "Id,Name\r\n"; + expected += "1,one\r\n"; + expected += "2,two\r\n"; + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteDynamicIDynamicMetaObjectProviderHasDifferentPropertyOrderingWithDynamicSortTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + DynamicPropertySort = Comparer<string>.Create((x, y) => x.CompareTo(y)), + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + dynamic obj = new DynamicObjectMock(); + obj.Name = "one"; + obj.Id = 1; + + csv.WriteDynamicHeader(obj); + csv.NextRecord(); + + csv.WriteRecord(obj); + csv.NextRecord(); + + obj = new ExpandoObject(); + obj.Id = 2; + obj.Name = "two"; + + csv.WriteRecord(obj); + csv.NextRecord(); + + var expected = "Id,Name\r\n"; + expected += "1,one\r\n"; + expected += "2,two\r\n"; + + Assert.Equal(expected, writer.ToString()); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/FieldTypeTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/FieldTypeTests.cs new file mode 100644 index 0000000..507bd44 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/FieldTypeTests.cs @@ -0,0 +1,87 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CsvHelper.Tests.Writing +{ + + public class FieldTypeTests + { + [Fact] + public void WriteField_ShouldQuote_HasCorrectFieldType() + { + Type type = null; + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + ShouldQuote = args => + { + type = args.FieldType; + return ConfigurationFunctions.ShouldQuote(args); + }, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField(string.Empty); + Assert.Equal(typeof(string), type); + + csv.WriteField(1); + Assert.Equal(typeof(int), type); + + csv.WriteField(string.Empty); + Assert.Equal(typeof(string), type); + } + } + + [Fact] + public void WriteRecords_ShouldQuote_HasCorrectFieldType() + { + var records = new List<Foo> + { + new Foo { Id = 1, Name = "one" }, + }; + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + ShouldQuote = args => + { + if (args.Row.Row > 1) + { + switch (args.Row.Index) + { + case 0: + Assert.Equal(typeof(int), args.FieldType); + break; + case 1: + Assert.Equal(typeof(string), args.FieldType); + break; + } + } + + return ConfigurationFunctions.ShouldQuote(args); + }, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteRecords(records); + } + } + + private class Foo + { + public int Id { get; set; } + + public string Name { get; set; } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/IAsyncEnumerableTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/IAsyncEnumerableTests.cs new file mode 100644 index 0000000..dc5fd61 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/IAsyncEnumerableTests.cs @@ -0,0 +1,50 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +#if !NET45 +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace CsvHelper.Tests.Writing +{ + public class IAsyncEnumerableTests + { + [Fact] + public async Task Test() + { + var records = new List<Foo> + { + new Foo { Id = 1, Name = "one" }, + new Foo { Id = 2, Name = "two" }, + }.ToAsyncEnumerable(); + + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + await csv.WriteRecordsAsync(records); + + var expected = new StringBuilder(); + expected.Append("Id,Name\r\n"); + expected.Append("1,one\r\n"); + expected.Append("2,two\r\n"); + + Assert.Equal(expected.ToString(), writer.ToString()); + } + } + + private class Foo + { + public int Id { get; set; } + + public string Name { get; set; } + } + } +} +#endif diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/IgnoreTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/IgnoreTests.cs new file mode 100644 index 0000000..3fbe5e7 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/IgnoreTests.cs @@ -0,0 +1,51 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CsvHelper.Tests.Writing +{ + + public class IgnoreTests + { + [Fact] + public void WritingWithAllPropertiesIgnoredTest() + { + var records = new List<Foo> + { + new Foo { Id = 1 }, + }; + + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.Context.RegisterClassMap<FooMap>(); + csv.WriteRecords(records); + + Assert.Equal("\r\n\r\n", writer.ToString()); + } + } + + private class Foo + { + public int Id { get; set; } + } + + private class FooMap : ClassMap<Foo> + { + public FooMap() + { + Map(m => m.Id).Ignore(); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/InterfaceTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/InterfaceTests.cs new file mode 100644 index 0000000..b3f2cad --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/InterfaceTests.cs @@ -0,0 +1,90 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System.Collections.Generic; +using System.Globalization; +using System.IO; + +namespace CsvHelper.Tests.Writing +{ + + public class InterfaceTests + { + [Fact] + public void WriteRecordsGenericTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + var records = new List<IRecord>(); + IRecord record = new Record { A = 1, B = 2 }; + records.Add(record); + record = new Record { A = 3, B = 4 }; + records.Add(record); + + csv.Context.RegisterClassMap<RecordMap>(); + csv.WriteRecords(records); + writer.Flush(); + stream.Position = 0; + + var expected = "RenameA\r\n1\r\n3\r\n"; + Assert.Equal(expected, reader.ReadToEnd()); + } + } + + [Fact] + public void WriteRecordTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.Context.RegisterClassMap<RecordMap>(); + + csv.WriteHeader<IRecord>(); + csv.NextRecord(); + + IRecord record = new Record { A = 1, B = 2 }; + csv.WriteRecord(record); + csv.NextRecord(); + + record = new Record { A = 3, B = 4 }; + csv.WriteRecord(record); + csv.NextRecord(); + + writer.Flush(); + stream.Position = 0; + + var expected = "RenameA\r\n1\r\n3\r\n"; + Assert.Equal(expected, reader.ReadToEnd()); + } + } + + private interface IRecord + { + int A { get; set; } + int B { get; set; } + } + + private class Record : IRecord + { + public int A { get; set; } + public int B { get; set; } + } + + private sealed class RecordMap : ClassMap<IRecord> + { + public RecordMap() + { + Map(m => m.A).Name("RenameA"); + } + } + + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/MultipleFieldsFromOnePropertyTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/MultipleFieldsFromOnePropertyTests.cs new file mode 100644 index 0000000..f33508b --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/MultipleFieldsFromOnePropertyTests.cs @@ -0,0 +1,79 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; + +namespace CsvHelper.Tests.Writing +{ + + public class MultipleFieldsFromOnePropertyTests + { + [Fact] + public void WriteMultipleFieldsFromSinglePropertyTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, new CultureInfo("en-US"))) + { + var records = new List<Test> + { + new Test { Dob = DateTime.Parse("9/6/2017", new CultureInfo("en-US")) } + }; + csv.Context.RegisterClassMap<TestMap>(); + csv.WriteRecords(records); + writer.Flush(); + stream.Position = 0; + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("A,B,C"); + expected.AppendLine("9/6/2017 12:00:00 AM,9/6/2017 12:00:00 AM,9/6/2017 12:00:00 AM"); + + Assert.Equal(expected.ToString(), reader.ReadToEnd()); + } + } + + [Fact] + public void ReadingWhenMultipleMapsForAPropertyAreSpecifiedUsesTheLastMapTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) + { + writer.WriteLine("A,B,C"); + writer.WriteLine("9/6/2017 12:00:00 AM,9/7/2017 12:00:00 AM,9/8/2017 12:00:00 AM"); + writer.Flush(); + stream.Position = 0; + + csv.Context.RegisterClassMap<TestMap>(); + var records = csv.GetRecords<Test>().ToList(); + + Assert.Equal(DateTime.Parse("9/8/2017", CultureInfo.InvariantCulture), records[0].Dob); + } + } + + private class Test + { + public DateTime Dob { get; set; } + } + + private sealed class TestMap : ClassMap<Test> + { + public TestMap() + { + Map(m => m.Dob, false).Name("A"); + Map(m => m.Dob, false).Name("B"); + Map(m => m.Dob, false).Name("C"); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/MultipleHeadersTest.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/MultipleHeadersTest.cs new file mode 100644 index 0000000..086bb86 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/MultipleHeadersTest.cs @@ -0,0 +1,84 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using Xunit; +using System.Dynamic; +using System.Globalization; +using System.IO; +using System.Text; + +namespace CsvHelper.Tests.Writing +{ + + public class MultipleHeadersTest + { + [Fact] + public void GenericTypeTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteHeader<A>(); + csv.NextRecord(); + csv.WriteRecord(new A { Id = 1 }); + csv.NextRecord(); + + csv.WriteHeader<B>(); + csv.NextRecord(); + csv.WriteRecord(new B { Name = "one" }); + csv.NextRecord(); + writer.Flush(); + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("Id"); + expected.AppendLine("1"); + expected.AppendLine("Name"); + expected.AppendLine("one"); + + Assert.Equal(expected.ToString(), writer.ToString()); + } + } + + [Fact] + public void DynamicTypeTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + dynamic a = new ExpandoObject(); + a.Id = 1; + csv.WriteDynamicHeader(a); + csv.NextRecord(); + csv.WriteRecord(a); + csv.NextRecord(); + + dynamic b = new ExpandoObject(); + b.Name = "one"; + csv.WriteDynamicHeader(b); + csv.NextRecord(); + csv.WriteRecord(b); + csv.NextRecord(); + writer.Flush(); + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("Id"); + expected.AppendLine("1"); + expected.AppendLine("Name"); + expected.AppendLine("one"); + + Assert.Equal(expected.ToString(), writer.ToString()); + } + } + + private class A + { + public int Id { get; set; } + } + + private class B + { + public string Name { get; set; } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/NewLineTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/NewLineTests.cs new file mode 100644 index 0000000..34e1e0b --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/NewLineTests.cs @@ -0,0 +1,92 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using CsvHelper.Tests.Mocks; +using Xunit; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CsvHelper.Tests.Writing +{ + + public class NewLineTests + { + [Fact] + public void CRLFTest() + { + var records = new List<Foo> + { + new Foo { Id = 1, Name = "one" }, + }; + + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteRecords(records); + + Assert.Equal("1,one\r\n", writer.ToString()); + } + } + + [Fact] + public void CRTest() + { + var records = new List<Foo> + { + new Foo { Id = 1, Name = "one" }, + }; + + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + NewLine = "\r", + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteRecords(records); + + Assert.Equal("1,one\r", writer.ToString()); + } + } + + [Fact] + public void LFTest() + { + var records = new List<Foo> + { + new Foo { Id = 1, Name = "one" }, + }; + + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + NewLine = "\n", + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteRecords(records); + + Assert.Equal("1,one\n", writer.ToString()); + } + } + + private class Foo + { + public int Id { get; set; } + public string Name { get; set; } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/NoPropertyMappingTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/NoPropertyMappingTests.cs new file mode 100644 index 0000000..e284836 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/NoPropertyMappingTests.cs @@ -0,0 +1,218 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Text; +using CsvHelper.Configuration; +using Xunit; + +namespace CsvHelper.Tests.Writing +{ + + public class NoPropertyMappingTests + { + [Fact] + public void NoPropertyWithHeaderAndNameTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + var list = new List<Test> + { + new Test { Id = 1 }, + new Test { Id = 2 } + }; + + csv.Context.RegisterClassMap<TestWithNameMap>(); + csv.WriteRecords(list); + + writer.Flush(); + stream.Position = 0; + + var result = reader.ReadToEnd(); + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("Id,Constant,Name"); + expected.AppendLine("1,const,"); + expected.AppendLine("2,const,"); + + Assert.Equal(expected.ToString(), result); + } + } + + [Fact] + public void NoPropertyWithHeaderAndNoNameTest() + { + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + var list = new List<Test> + { + new Test { Id = 1 }, + new Test { Id = 2 } + }; + + csv.Context.RegisterClassMap<TestWithNoNameMap>(); + csv.WriteRecords(list); + + writer.Flush(); + stream.Position = 0; + + var result = reader.ReadToEnd(); + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("Id,"); + expected.AppendLine("1,const"); + expected.AppendLine("2,const"); + + Assert.Equal(expected.ToString(), result); + } + } + + [Fact] + public void NoPropertyWithNoHeaderAndNameTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + }; + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, config)) + { + var list = new List<Test> + { + new Test { Id = 1 }, + new Test { Id = 2 } + }; + + csv.Context.RegisterClassMap<TestWithNameMap>(); + csv.WriteRecords(list); + + writer.Flush(); + stream.Position = 0; + + var result = reader.ReadToEnd(); + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("1,const,"); + expected.AppendLine("2,const,"); + + Assert.Equal(expected.ToString(), result); + } + } + + [Fact] + public void NoPropertyWithNoHeaderAndNoNameTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + }; + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, config)) + { + var list = new List<Test> + { + new Test { Id = 1 }, + new Test { Id = 2 } + }; + + csv.Context.RegisterClassMap<TestWithNoNameMap>(); + csv.WriteRecords(list); + + writer.Flush(); + stream.Position = 0; + + var result = reader.ReadToEnd(); + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("1,const"); + expected.AppendLine("2,const"); + + Assert.Equal(expected.ToString(), result); + } + } + + [Fact] + public void OutOfOrderTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + }; + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, config)) + { + var list = new List<Test> + { + new Test { Id = 1, Name = "one" }, + new Test { Id = 2, Name = "two" } + }; + + csv.Context.RegisterClassMap<TestMapOutOfOrderWithEmptyFieldsMap>(); + csv.WriteRecords(list); + + writer.Flush(); + stream.Position = 0; + + var result = reader.ReadToEnd(); + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("one,,,1"); + expected.AppendLine("two,,,2"); + + Assert.Equal(expected.ToString(), result); + } + } + + private class Test + { + public int Id { get; set; } + + public string Name { get; set; } + } + + private sealed class TestWithNameMap : ClassMap<Test> + { + public TestWithNameMap() + { + Map(m => m.Id); + Map().Name("Constant").Constant("const"); + Map(m => m.Name); + } + } + + private sealed class TestWithNoNameMap : ClassMap<Test> + { + public TestWithNoNameMap() + { + Map(m => m.Id); + Map().Constant("const"); + } + } + + private sealed class TestMapOutOfOrderWithEmptyFieldsMap : ClassMap<Test> + { + public TestMapOutOfOrderWithEmptyFieldsMap() + { + Map(m => m.Name).Index(0); + Map().Index(1).Constant(null); + Map().Index(2).Constant(string.Empty); + Map(m => m.Id).Index(3); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/SanitizationTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/SanitizationTests.cs new file mode 100644 index 0000000..1442cd5 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/SanitizationTests.cs @@ -0,0 +1,422 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System.Globalization; +using System.IO; +using System.Linq; + +namespace CsvHelper.Tests.Serializing +{ + + public class SanitizationTests + { + [Fact] + public void WriteField_NoQuotes_OptionsNone_DoesNotSanitize() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.None, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{ch}foo", false); + } + + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{ch}foo")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_Quotes_OptionsNone_DoesNotSanitize() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.None, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{config.Quote}{ch}foo{config.Quote}", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}{ch}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_NoQuotes_OptionsException_ThrowsException() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Exception, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + Assert.Throws<WriterException>(() => csv.WriteField($"{ch}foo", false)); + } + } + } + + [Fact] + public void WriteField_Quotes_OptionsException_ThrowsException() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Exception, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + Assert.Throws<WriterException>(() => csv.WriteField($"{config.Quote}{ch}foo{config.Quote}", false)); + } + } + } + + [Fact] + public void WriteField_NoQuotes_OptionsException_CharIsNotFirst_DoesNotThrowException() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Exception, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"a{ch}foo", false); + } + + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"a{ch}foo")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_Quotes_OptionsException_CharIsNotFirst_DoesNotThrowException() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Exception, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{config.Quote}a{ch}foo{config.Quote}", false); + } + + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}a{ch}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_NoQuotes_OptionsStrip_StripsCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Strip, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{ch}foo", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"foo")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_NoQuotes_OptionsStrip_CharIsNotFirst_DoesNotStripCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Strip, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"a{ch}foo", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"a{ch}foo")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_NoQuotes_MultipleChars_OptionsStrip_StripsCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Strip, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{ch}{ch}{ch}foo", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"foo")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_NoQuotes_MultipleChars_OptionsStrip_CharIsNotFirst_DoesNotStripCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Strip, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"a{ch}{ch}{ch}foo", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"a{ch}{ch}{ch}foo")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_Quotes_OptionsStrip_StripsCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Strip, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{config.Quote}{ch}foo{config.Quote}", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_Quotes_OptionsStrip_CharIsNotFirst_DoesNotStripCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Strip, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{config.Quote}a{ch}foo{config.Quote}", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}a{ch}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_Quotes_MultipleChars_OptionsStrip_StripsCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Strip, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{config.Quote}{ch}{ch}{ch}foo{config.Quote}", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_Quotes_MultipleChars_OptionsStripCharIsNotFirst_DoesNotStripCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Strip, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{config.Quote}a{ch}{ch}{ch}foo{config.Quote}", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}a{ch}{ch}{ch}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_NoQuotes_OptionsEscape_QuotesFieldAndEscapes() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Escape, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{ch}foo", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}{config.InjectionEscapeCharacter}{ch}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_NoQuotes_OptionsEscape_CharIsNotFirst_DoesNotQuoteFieldAndEscape() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Escape, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"a{ch}foo", false); + } + + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"a{ch}foo")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_Quotes_OptionsEscape_EscapesInsideQuotes() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Escape, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{config.Quote}{ch}foo{config.Quote}", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}{config.InjectionEscapeCharacter}{ch}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_Quotes_OptionsEscape_CharIsNotFirst_DoesNotEscapeInsideQuotes() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + InjectionOptions = InjectionOptions.Escape, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + foreach (var ch in config.InjectionCharacters) + { + csv.WriteField($"{config.Quote}a{ch}foo{config.Quote}", false); + } + csv.Flush(); + writer.Flush(); + + var expected = string.Join(config.Delimiter, config.InjectionCharacters.Select(ch => $"{config.Quote}a{ch}foo{config.Quote}")); + + Assert.Equal(expected, writer.ToString()); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/ShouldQuoteTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/ShouldQuoteTests.cs new file mode 100644 index 0000000..a97d2c6 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/ShouldQuoteTests.cs @@ -0,0 +1,187 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System.Collections.Generic; +using System.Globalization; +using System.IO; + +namespace CsvHelper.Tests.Writing +{ + + public class ShouldQuoteTests + { + [Fact] + public void QuoteAllFieldsTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + ShouldQuote = _ => true, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("one"); + csv.Flush(); + + Assert.Equal("\"one\"", writer.ToString()); + } + } + + [Fact] + public void QuoteNoFieldsTest() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + ShouldQuote = _ => false, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("o\"e"); + csv.Flush(); + + Assert.Equal("o\"e", writer.ToString()); + } + } + + [Fact] + public void ContainsQuoteTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteField($"o{csv.Configuration.Quote}e"); + csv.Flush(); + + Assert.Equal($"\"o{csv.Configuration.Quote}{csv.Configuration.Quote}e\"", writer.ToString()); + } + } + + [Fact] + public void StartsWithSpaceTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteField(" one"); + csv.Flush(); + + Assert.Equal("\" one\"", writer.ToString()); + } + } + + [Fact] + public void EndsWithSpaceTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteField("one "); + csv.Flush(); + + Assert.Equal("\"one \"", writer.ToString()); + } + } + + [Fact] + public void ContainsCrTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteField("o\re"); + csv.Flush(); + + Assert.Equal("\"o\re\"", writer.ToString()); + } + } + + [Fact] + public void ContainsLfTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteField("o\ne"); + csv.Flush(); + + Assert.Equal("\"o\ne\"", writer.ToString()); + } + } + + [Fact] + public void ContainsCrLfTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteField("o\r\ne"); + csv.Flush(); + + Assert.Equal("\"o\r\ne\"", writer.ToString()); + } + } + + [Fact] + public void ContainsDelimiterTest() + { + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csv.WriteField($"o{csv.Configuration.Delimiter}e"); + csv.Flush(); + + Assert.Equal($"\"o{csv.Configuration.Delimiter}e\"", writer.ToString()); + } + } + + [Fact] + public void Test1() + { + var data = new List<(int row, int column, string field)>(); + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + ShouldQuote = args => + { + data.Add((args.Row.Row, args.Row.Index, args.Field)); + + return ConfigurationFunctions.ShouldQuote(args); + }, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField("Id"); + csv.WriteField("Name"); + csv.NextRecord(); + + csv.WriteField("1"); + csv.WriteField("one"); + csv.NextRecord(); + + csv.Flush(); + } + + Assert.Equal(4, data.Count); + + Assert.Equal(1, data[0].row); + Assert.Equal(0, data[0].column); + Assert.Equal("Id", data[0].field); + + Assert.Equal(1, data[1].row); + Assert.Equal(1, data[1].column); + Assert.Equal("Name", data[1].field); + + Assert.Equal(2, data[2].row); + Assert.Equal(0, data[2].column); + Assert.Equal("1", data[2].field); + + Assert.Equal(2, data[3].row); + Assert.Equal(1, data[3].column); + Assert.Equal("one", data[3].field); + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/TrimTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/TrimTests.cs new file mode 100644 index 0000000..836f7df --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/TrimTests.cs @@ -0,0 +1,41 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System.Globalization; +using System.IO; +using System.Text; + +namespace CsvHelper.Tests.Writing +{ + + public class TrimTests + { + [Fact] + public void Test() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + TrimOptions = TrimOptions.Trim, + }; + using (var stream = new MemoryStream()) + using (var reader = new StreamReader(stream)) + using (var writer = new StreamWriter(stream)) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteField(" a b c "); + csv.WriteField(" d e f "); + csv.NextRecord(); + writer.Flush(); + stream.Position = 0; + + var expected = new TestStringBuilder(csv.Configuration.NewLine); + expected.AppendLine("a b c,d e f"); + + Assert.Equal(expected.ToString(), reader.ReadToEnd()); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteBufferTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteBufferTests.cs new file mode 100644 index 0000000..4e73fab --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteBufferTests.cs @@ -0,0 +1,36 @@ +// Copyright 2009-2022 Josh Close +// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. +// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. +// https://github.com/JoshClose/CsvHelper +using CsvHelper.Configuration; +using Xunit; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CsvHelper.Tests.Writing +{ + + public class WriteBufferTests + { + [Fact] + public void Write_FieldThatIsLargerThenTwiceTheBuffer_Writes() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + BufferSize = 16 + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + var random = new Random(); + csv.WriteField("one"); + csv.WriteField(new string(Enumerable.Range(0, 1000).Select(i => (char)random.Next((int)'a', (int)'z' + 1)).ToArray())); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteCustomEscapeTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteCustomEscapeTests.cs new file mode 100644 index 0000000..f45da08 --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteCustomEscapeTests.cs @@ -0,0 +1,63 @@ +using CsvHelper.Configuration; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Threading.Tasks; +using System.Xml.Linq; +using Xunit; + +namespace CsvHelper.Tests.Writing +{ + public class WriteCustomEscapeTests + { + [Fact] + public void WriteField_CustomEscapeChar_ModeRFC4180_EscapesQuotesAndEscapeCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + Escape = '\\', + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + // {"json":"{\"name\":\"foo\"}"} + // json string -> csv field + // "{\"json\":\"{\\\"name\\\":\\\"foo\\\"}\"}" + csv.WriteField(@"{""json"":""{\""name\"":\""foo\""}""}"); + csv.Flush(); + + var expected = @"""{\""json\"":\""{\\\""name\\\"":\\\""foo\\\""}\""}"""; + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteField_CustomEscapeChar_ModeEscape_EscapesQuotesAndEscapeCharacter() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture) + { + HasHeaderRecord = false, + Escape = '\\', + Mode = CsvMode.Escape, + }; + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + // {"json":"{\"name\":\"foo\"}"} + // json string -> csv field + // {\"json\":\"{\\\"name\\\":\\\"foo\\\"}\"} + csv.WriteField(@"{""json"":""{\""name\"":\""foo\""}""}"); + csv.Flush(); + + var expected = @"{\""json\"":\""{\\\""name\\\"":\\\""foo\\\""}\""}"; + Assert.Equal(expected, writer.ToString()); + } + } + } +} diff --git a/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteNullTests.cs b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteNullTests.cs new file mode 100644 index 0000000..7d7f23f --- /dev/null +++ b/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteNullTests.cs @@ -0,0 +1,84 @@ +using CsvHelper.Configuration; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using Xunit; + +namespace CsvHelper.Tests.Writing +{ + public class WriteNullTests + { + [Fact] + public void WriteRecordsEnumerableGeneric_RecordIsNull_WritesEmptyRecord() + { + var records = new List<Foo> + { + new Foo { Id = 1, Name = "one"}, + null, + new Foo { Id = 2, Name = "two" }, + }; + var config = new CsvConfiguration(CultureInfo.InvariantCulture); + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteRecords(records); + csv.Flush(); + + var expected = new TestStringBuilder(config.NewLine); + expected.AppendLine("Id,Name"); + expected.AppendLine("1,one"); + expected.AppendLine(","); + expected.AppendLine("2,two"); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteRecordsEnumerable_RecordIsNull_WritesEmptyRecord() + { + IEnumerable records = new List<Foo> + { + new Foo { Id = 1, Name = "one"}, + null, + new Foo { Id = 2, Name = "two" }, + }; + var config = new CsvConfiguration(CultureInfo.InvariantCulture); + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteRecords(records); + csv.Flush(); + + var expected = new TestStringBuilder(config.NewLine); + expected.AppendLine("Id,Name"); + expected.AppendLine("1,one"); + expected.AppendLine(""); + expected.AppendLine("2,two"); + + Assert.Equal(expected, writer.ToString()); + } + } + + [Fact] + public void WriteRecord_RecordIsNull_WritesEmptyRecord() + { + var config = new CsvConfiguration(CultureInfo.InvariantCulture); + using (var writer = new StringWriter()) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteRecord((Foo)null); + csv.Flush(); + + Assert.Equal(",", writer.ToString()); + } + } + + private class Foo + { + public int Id { get; set; } + public string Name { get; set; } + } + } +} |