-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathCountingLinesTests.cs
More file actions
265 lines (233 loc) · 7.15 KB
/
CountingLinesTests.cs
File metadata and controls
265 lines (233 loc) · 7.15 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
namespace Easy.Common.Tests.Unit.StreamExtensions;
using Easy.Common.Extensions;
using NUnit.Framework;
using Shouldly;
using System;
using System.IO;
using System.Text;
[TestFixture]
internal sealed class CountingLinesTests
{
[Test]
public void When_processing_null_stream()
{
Should.Throw<NullReferenceException>(() => ((MemoryStream) null).CountLines())
.Message.ShouldBe("Object reference not set to an instance of an object.");
}
[Test]
public void When_processing_empty_stream()
{
using var mem = new MemoryStream();
mem.CountLines().ShouldBe(0);
}
[TestCase("\r")]
[TestCase("\n")]
[TestCase("\r\n")]
public void When_processing_stream_with_empty_line(string content)
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes(content);
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(1, $"Content was: '{content}'");
}
[TestCase("\r\r")]
[TestCase("\n\n")]
[TestCase("\r\n\r\n")]
public void When_processing_stream_with_empty_lines(string content)
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes(content);
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(2, $"Content was: '{content}'");
}
[TestCase("A")]
[TestCase("A\r")]
[TestCase("A\n")]
[TestCase("A\r\n")]
public void When_processing_stream_with_single_line(string content)
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes(content);
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(1, $"Content was: '{content}'");
}
[Test]
public void When_processing_stream_with_utf16_characters()
{
byte[] bytes = Encoding.BigEndianUnicode.GetBytes(" ਊ ");
using var stream = new MemoryStream(bytes);
stream.CountLines(Encoding.BigEndianUnicode).ShouldBe(1);
}
[Test]
public void When_processing_stream_with_lines_terminated_by_carriage_return()
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes("A\rB\r\r");
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(3);
}
[Test]
public void When_processing_stream_with_lines_terminated_by_new_line()
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes("A\nB\n\n");
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(3);
}
[Test]
public void When_processing_stream_with_lines_terminated_by_carriage_return_followed_by_new_line()
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes("A\r\nB\r\n\r\n");
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(3);
}
[Test]
public void When_processing_long_line_terminated_by_carriage_return()
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes($"A\r{new string('_', 1_500_000)}\rB");
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(3);
}
[Test]
public void When_processing_long_line_terminated_by_carriage_by_new_line()
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes($"A\n{new string('_', 1_500_000)}\nB");
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(3);
}
[Test]
public void When_processing_long_line_terminated_by_carriage_return_followed_by_new_line()
{
using var mem = new MemoryStream();
var input = Encoding.UTF8.GetBytes($"A\r\n{new string('_', 1_500_000)}\r\nB");
mem.Write(input, 0, input.Length);
mem.Position = 0;
mem.CountLines().ShouldBe(3);
}
[Test]
public void When_processing_a_ascii_file()
{
FileInfo file = null;
try
{
file = new FileInfo(Path.GetTempFileName());
using(var writer = new StreamWriter(File.OpenWrite(file.FullName), Encoding.ASCII))
{
writer.WriteLine("A");
writer.WriteLine("B");
writer.WriteLine("❤");
writer.WriteLine("C");
}
using (var stream = file.OpenRead())
{
stream.CountLines().ShouldBe(4);
}
} finally
{
file?.Delete();
}
}
[Test]
public void When_processing_a_utf8_file()
{
FileInfo file = null;
try
{
file = new FileInfo(Path.GetTempFileName());
using(var writer = new StreamWriter(File.OpenWrite(file.FullName), Encoding.UTF8))
{
writer.WriteLine("A");
writer.WriteLine("B");
writer.WriteLine("C");
}
using (var stream = file.OpenRead())
{
stream.CountLines().ShouldBe(3);
}
} finally
{
file?.Delete();
}
}
[Test]
public void When_processing_a_utf7_file()
{
FileInfo file = null;
try
{
file = new FileInfo(Path.GetTempFileName());
#pragma warning disable SYSLIB0001
#pragma warning disable CS0618
using(var writer = new StreamWriter(File.OpenWrite(file.FullName), Encoding.UTF7))
#pragma warning restore CS0618
#pragma warning restore SYSLIB0001
{
writer.WriteLine("A");
writer.WriteLine("B");
writer.WriteLine("C");
}
using (var stream = file.OpenRead())
{
stream.CountLines().ShouldBe(3);
}
} finally
{
file?.Delete();
}
}
[Test]
public void When_processing_a_utf16_file()
{
FileInfo file = null;
try
{
file = new FileInfo(Path.GetTempFileName());
using(var writer = new StreamWriter(File.OpenWrite(file.FullName), Encoding.Unicode))
{
writer.WriteLine("A");
writer.WriteLine("B");
writer.WriteLine("C");
}
using (var stream = file.OpenRead())
{
stream.CountLines().ShouldBe(3);
}
} finally
{
file?.Delete();
}
}
[Test]
public void When_processing_a_utf32_file()
{
FileInfo file = null;
try
{
file = new FileInfo(Path.GetTempFileName());
using(var writer = new StreamWriter(File.OpenWrite(file.FullName), Encoding.UTF32))
{
writer.WriteLine("A");
writer.WriteLine("B");
writer.WriteLine("C");
}
using (var stream = file.OpenRead())
{
stream.CountLines().ShouldBe(3);
}
} finally
{
file?.Delete();
}
}
}