Skip to content

Commit f058dd4

Browse files
committed
Update FilePolyfillTests.cs
1 parent e66f9e6 commit f058dd4

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

src/Tests/FilePolyfillTests.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,88 @@ public async Task Move_ShouldThrowIOException_WhenDestinationExistsAndOverwriteI
218218
await Assert.That(() =>
219219
File.Move(sourceFilePath, destinationFilePath, overwrite: false)).Throws<IOException>();
220220
}
221+
222+
#if FeatureAsyncInterfaces
223+
224+
[Test]
225+
public async Task ReadLinesAsync_ReadsAllLines()
226+
{
227+
var lines = new[] { "Line1", "Line2", "Line3" };
228+
File.WriteAllLines(restFilePath, lines);
229+
230+
var result = new List<string>();
231+
await foreach (var line in File.ReadLinesAsync(restFilePath))
232+
{
233+
result.Add(line);
234+
}
235+
236+
await Assert.That(result.SequenceEqual(lines)).IsTrue();
237+
}
238+
239+
[Test]
240+
public async Task ReadLinesAsync_WithEncoding_ReadsAllLines()
241+
{
242+
var lines = new[] { "Línea1", "Línea2", "Línea3" };
243+
File.WriteAllLines(restFilePath, lines, Encoding.UTF8);
244+
245+
var result = new List<string>();
246+
await foreach (var line in File.ReadLinesAsync(restFilePath, Encoding.UTF8))
247+
{
248+
result.Add(line);
249+
}
250+
251+
await Assert.That(result.SequenceEqual(lines)).IsTrue();
252+
}
253+
254+
[Test]
255+
public async Task ReadLinesAsync_EmptyFile_ReturnsNoLines()
256+
{
257+
File.WriteAllText(restFilePath, "");
258+
259+
var result = new List<string>();
260+
await foreach (var line in File.ReadLinesAsync(restFilePath))
261+
{
262+
result.Add(line);
263+
}
264+
265+
await Assert.That(result.Count).IsEqualTo(0);
266+
}
267+
268+
[Test]
269+
public async Task ReadLinesAsync_ThrowsIfFileNotFound()
270+
{
271+
var nonExistent = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
272+
await Assert.That(async () =>
273+
{
274+
await foreach (var _ in File.ReadLinesAsync(nonExistent))
275+
{
276+
}
277+
}).Throws<FileNotFoundException>();
278+
}
279+
280+
[Test]
281+
public async Task ReadLinesAsync_ThrowsIfCancelled()
282+
{
283+
var lines = new[] { "Line1", "Line2", "Line3" };
284+
File.WriteAllLines(restFilePath, lines);
285+
var cancelSource = new CancelSource();
286+
cancelSource.Cancel();
287+
288+
Exception? exception = null;
289+
try
290+
{
291+
await foreach (var _ in File.ReadLinesAsync(restFilePath, cancelSource.Token))
292+
{
293+
}
294+
}
295+
catch (Exception e)
296+
{
297+
exception = e;
298+
}
299+
300+
await Assert.That(exception).IsNotNull();
301+
await Assert.That(exception is OperationCanceledException or TaskCanceledException).IsTrue();
302+
}
303+
304+
#endif
221305
}

0 commit comments

Comments
 (0)