summaryrefslogtreecommitdiff
path: root/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteCustomEscapeTests.cs
blob: f45da086e2dceea8e3342e57b1ae22336797b84e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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());
			}
		}
	}
}