summaryrefslogtreecommitdiff
path: root/ThirdParty/CsvHelper-master/tests/CsvHelper.Tests/Writing/WriteNullTests.cs
blob: 7d7f23fe1c3c3f4f3fa997e4b04a82fa904ee930 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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; }
		}
	}
}