11namespace System . IO . Abstractions . TestingHelpers . Tests
22{
33 using Collections . Generic ;
4+ using Collections . Specialized ;
5+ using Threading ;
6+ using Threading . Tasks ;
47
58 using NUnit . Framework ;
69
710 using Text ;
811
912 using XFS = MockUnixSupport ;
1013
11- using System . Threading . Tasks ;
12- using System . Threading ;
13-
1414 public class MockFileReadAllLinesTests
1515 {
1616 [ Test ]
@@ -63,7 +63,7 @@ public void MockFile_ReadAllLines_NotExistingFile_ThrowsCorrectFileNotFoundExcep
6363 var mockFileSystem = new MockFileSystem ( ) ;
6464
6565 var act = new TestDelegate ( ( ) =>
66- mockFileSystem . File . ReadAllText ( absentFileNameFullPath )
66+ mockFileSystem . File . ReadAllLines ( absentFileNameFullPath )
6767 ) ;
6868
6969 var exception = Assert . Catch < FileNotFoundException > ( act ) ;
@@ -149,13 +149,95 @@ public void MockFile_ReadAllLinesAsync_NotExistingFile_ThrowsCorrectFileNotFound
149149 var mockFileSystem = new MockFileSystem ( ) ;
150150
151151 var act = new AsyncTestDelegate ( async ( ) =>
152- await mockFileSystem . File . ReadAllTextAsync ( absentFileNameFullPath )
152+ await mockFileSystem . File . ReadAllLinesAsync ( absentFileNameFullPath )
153153 ) ;
154154
155155 var exception = Assert . CatchAsync < FileNotFoundException > ( act ) ;
156156 Assert . That ( exception . FileName , Is . EqualTo ( absentFileNameFullPath ) ) ;
157157 Assert . That ( exception . Message , Is . EqualTo ( "Could not find file '" + absentFileNameFullPath + "'." ) ) ;
158158 }
159+
160+ #if FEATURE_READ_LINES_ASYNC
161+ [ Test ]
162+ public async Task MockFile_ReadLinesAsync_ShouldReturnOriginalTextData ( )
163+ {
164+ // Arrange
165+ var fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData >
166+ {
167+ { XFS . Path ( @"c:\something\demo.txt" ) , new MockFileData ( "Demo\r \n text\n content\r value" ) } ,
168+ { XFS . Path ( @"c:\something\other.gif" ) , new MockFileData ( new byte [ ] { 0x21 , 0x58 , 0x3f , 0xa9 } ) }
169+ } ) ;
170+
171+ var file = new MockFile ( fileSystem ) ;
172+
173+ // Act
174+ var enumerable = file . ReadLinesAsync ( XFS . Path ( @"c:\something\demo.txt" ) ) ;
175+ StringCollection result = new ( ) ;
176+ await foreach ( var line in enumerable )
177+ result . Add ( line ) ;
178+
179+ // Assert
180+ CollectionAssert . AreEqual (
181+ new [ ] { "Demo" , "text" , "content" , "value" } ,
182+ result ) ;
183+ }
184+
185+ [ Test ]
186+ public async Task MockFile_ReadLinesAsync_ShouldReturnOriginalDataWithCustomEncoding ( )
187+ {
188+ // Arrange
189+ string text = "Hello\r \n there\r Bob\n Bob!" ;
190+ var encodedText = Encoding . BigEndianUnicode . GetBytes ( text ) ;
191+ var fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData >
192+ {
193+ { XFS . Path ( @"c:\something\demo.txt" ) , new MockFileData ( encodedText ) }
194+ } ) ;
195+
196+ var file = new MockFile ( fileSystem ) ;
197+
198+ // Act
199+ var enumerable = file . ReadLinesAsync ( XFS . Path ( @"c:\something\demo.txt" ) , Encoding . BigEndianUnicode ) ;
200+ StringCollection result = new ( ) ;
201+ await foreach ( var line in enumerable )
202+ result . Add ( line ) ;
203+
204+ // Assert
205+ CollectionAssert . AreEqual (
206+ new [ ] { "Hello" , "there" , "Bob" , "Bob!" } ,
207+ result ) ;
208+ }
209+
210+ [ Test ]
211+ public void MockFile_ReadLinesAsync_ShouldThrowOperationCanceledExceptionIfCanceled ( )
212+ {
213+ var fileSystem = new MockFileSystem ( ) ;
214+
215+ AsyncTestDelegate action = async ( ) =>
216+ {
217+ var enumerable = fileSystem . File . ReadLinesAsync ( @"C:\a.txt" , new CancellationToken ( canceled : true ) ) ;
218+ await foreach ( var line in enumerable ) ;
219+ } ;
220+
221+ Assert . ThrowsAsync < OperationCanceledException > ( action ) ;
222+ }
223+
224+ [ Test ]
225+ public void MockFile_ReadLinesAsync_NotExistingFile_ThrowsCorrectFileNotFoundException ( )
226+ {
227+ var absentFileNameFullPath = XFS . Path ( @"c:\you surely don't have such file.hope-so" ) ;
228+ var mockFileSystem = new MockFileSystem ( ) ;
229+
230+ AsyncTestDelegate action = async ( ) =>
231+ {
232+ var enumerable = mockFileSystem . File . ReadLinesAsync ( absentFileNameFullPath ) ;
233+ await foreach ( var line in enumerable ) ;
234+ } ;
235+
236+ var exception = Assert . CatchAsync < FileNotFoundException > ( action ) ;
237+ Assert . That ( exception . FileName , Is . EqualTo ( absentFileNameFullPath ) ) ;
238+ Assert . That ( exception . Message , Is . EqualTo ( "Could not find file '" + absentFileNameFullPath + "'." ) ) ;
239+ }
240+ #endif
159241#endif
160242 }
161- }
243+ }
0 commit comments