Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 261
feat: implement MockFile.ReadLinesAsync#935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mergify
merged 2 commits into
TestableIO:main
from
patricksadowski:implement-mockfile-asyncJan 24, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
26 changes: 13 additions & 13 deletions
26 src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFile.Async.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 88 additions & 6 deletions
94 tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileReadAllLinesTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| namespace System.IO.Abstractions.TestingHelpers.Tests | ||
| { | ||
| using Collections.Generic; | ||
| using Collections.Specialized; | ||
| using Threading; | ||
| using Threading.Tasks; | ||
| using NUnit.Framework; | ||
| using Text; | ||
| using XFS = MockUnixSupport; | ||
| using System.Threading.Tasks; | ||
| using System.Threading; | ||
| public class MockFileReadAllLinesTests | ||
| { | ||
| [Test] | ||
| @@ -63,7 +63,7 @@ public void MockFile_ReadAllLines_NotExistingFile_ThrowsCorrectFileNotFoundExcep | ||
| var mockFileSystem = new MockFileSystem(); | ||
| var act = new TestDelegate(() => | ||
| mockFileSystem.File.ReadAllText(absentFileNameFullPath) | ||
| mockFileSystem.File.ReadAllLines(absentFileNameFullPath) | ||
| ); | ||
| var exception = Assert.Catch<FileNotFoundException>(act); | ||
| @@ -149,13 +149,95 @@ public void MockFile_ReadAllLinesAsync_NotExistingFile_ThrowsCorrectFileNotFound | ||
| var mockFileSystem = new MockFileSystem(); | ||
| var act = new AsyncTestDelegate(async () => | ||
| await mockFileSystem.File.ReadAllTextAsync(absentFileNameFullPath) | ||
| await mockFileSystem.File.ReadAllLinesAsync(absentFileNameFullPath) | ||
| ); | ||
| var exception = Assert.CatchAsync<FileNotFoundException>(act); | ||
| Assert.That(exception.FileName, Is.EqualTo(absentFileNameFullPath)); | ||
| Assert.That(exception.Message, Is.EqualTo("Could not find file '" + absentFileNameFullPath + "'.")); | ||
| } | ||
| #if FEATURE_READ_LINES_ASYNC | ||
| [Test] | ||
| public async Task MockFile_ReadLinesAsync_ShouldReturnOriginalTextData() | ||
fgreinacher marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| // Arrange | ||
| var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> | ||
| { | ||
| { XFS.Path(@"c:\something\demo.txt"), new MockFileData("Demo\r\ntext\ncontent\rvalue") }, | ||
| { XFS.Path(@"c:\something\other.gif"), new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } | ||
| }); | ||
| var file = new MockFile(fileSystem); | ||
| // Act | ||
| var enumerable = file.ReadLinesAsync(XFS.Path(@"c:\something\demo.txt")); | ||
| StringCollection result = new(); | ||
| await foreach (var line in enumerable) | ||
| result.Add(line); | ||
| // Assert | ||
| CollectionAssert.AreEqual( | ||
| new[] { "Demo", "text", "content", "value" }, | ||
| result); | ||
| } | ||
| [Test] | ||
| public async Task MockFile_ReadLinesAsync_ShouldReturnOriginalDataWithCustomEncoding() | ||
| { | ||
| // Arrange | ||
| string text = "Hello\r\nthere\rBob\nBob!"; | ||
| var encodedText = Encoding.BigEndianUnicode.GetBytes(text); | ||
| var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> | ||
| { | ||
| { XFS.Path(@"c:\something\demo.txt"), new MockFileData(encodedText) } | ||
| }); | ||
| var file = new MockFile(fileSystem); | ||
| // Act | ||
| var enumerable = file.ReadLinesAsync(XFS.Path(@"c:\something\demo.txt"), Encoding.BigEndianUnicode); | ||
| StringCollection result = new(); | ||
| await foreach (var line in enumerable) | ||
| result.Add(line); | ||
| // Assert | ||
| CollectionAssert.AreEqual( | ||
| new[] { "Hello", "there", "Bob", "Bob!" }, | ||
| result); | ||
| } | ||
| [Test] | ||
| public void MockFile_ReadLinesAsync_ShouldThrowOperationCanceledExceptionIfCanceled() | ||
| { | ||
| var fileSystem = new MockFileSystem(); | ||
| AsyncTestDelegate action = async () => | ||
| { | ||
| var enumerable = fileSystem.File.ReadLinesAsync(@"C:\a.txt", new CancellationToken(canceled: true)); | ||
| await foreach (var line in enumerable); | ||
| }; | ||
| Assert.ThrowsAsync<OperationCanceledException>(action); | ||
| } | ||
| [Test] | ||
| public void MockFile_ReadLinesAsync_NotExistingFile_ThrowsCorrectFileNotFoundException() | ||
| { | ||
| var absentFileNameFullPath = XFS.Path(@"c:\you surely don't have such file.hope-so"); | ||
| var mockFileSystem = new MockFileSystem(); | ||
| AsyncTestDelegate action = async () => | ||
| { | ||
| var enumerable = mockFileSystem.File.ReadLinesAsync(absentFileNameFullPath); | ||
| await foreach (var line in enumerable) ; | ||
| }; | ||
| var exception = Assert.CatchAsync<FileNotFoundException>(action); | ||
| Assert.That(exception.FileName, Is.EqualTo(absentFileNameFullPath)); | ||
| Assert.That(exception.Message, Is.EqualTo("Could not find file '" + absentFileNameFullPath + "'.")); | ||
| } | ||
| #endif | ||
| #endif | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.