77
88public class TestFileSystem : IFileSystem
99{
10- Dictionary < string , string > fileSystem = new Dictionary < string , string > ( ) ;
10+ Dictionary < string , byte [ ] > fileSystem = new Dictionary < string , byte [ ] > ( ) ;
1111
1212 public void Copy ( string @from , string to , bool overwrite )
1313 {
@@ -19,7 +19,7 @@ public void Copy(string @from, string to, bool overwrite)
1919 throw new IOException ( "File already exists" ) ;
2020 }
2121
22- string source ;
22+ byte [ ] source ;
2323 if ( ! fileSystem . TryGetValue ( from , out source ) )
2424 throw new FileNotFoundException ( string . Format ( "The source file '{0}' was not found" , from ) , from ) ;
2525
@@ -44,19 +44,25 @@ public void Delete(string path)
4444
4545 public string ReadAllText ( string path )
4646 {
47- return fileSystem [ path ] ;
47+ byte [ ] content ;
48+ if ( ! fileSystem . TryGetValue ( path , out content ) )
49+ throw new FileNotFoundException ( string . Format ( "The file '{0}' was not found" , path ) , path ) ;
50+
51+ var encoding = EncodingHelper . DetectEncoding ( content ) ?? Encoding . UTF8 ;
52+ return encoding . GetString ( content ) ;
4853 }
4954
5055 public void WriteAllText ( string file , string fileContents )
5156 {
52- if ( fileSystem . ContainsKey ( file ) )
53- {
54- fileSystem [ file ] = fileContents ;
55- }
56- else
57- {
58- fileSystem . Add ( file , fileContents ) ;
59- }
57+ var encoding = fileSystem . ContainsKey ( file )
58+ ? EncodingHelper . DetectEncoding ( fileSystem [ file ] ) ?? Encoding . UTF8
59+ : Encoding . UTF8 ;
60+ WriteAllText ( file , fileContents , encoding ) ;
61+ }
62+
63+ public void WriteAllText ( string file , string fileContents , Encoding encoding )
64+ {
65+ fileSystem [ file ] = encoding . GetBytes ( fileContents ) ;
6066 }
6167
6268 public IEnumerable < string > DirectoryGetFiles ( string directory , string searchPattern , SearchOption searchOption )
@@ -74,7 +80,7 @@ public Stream OpenRead(string path)
7480 if ( fileSystem . ContainsKey ( path ) )
7581 {
7682 var content = fileSystem [ path ] ;
77- return new MemoryStream ( Encoding . UTF8 . GetBytes ( content ) ) ;
83+ return new MemoryStream ( content ) ;
7884 }
7985
8086 throw new FileNotFoundException ( "File not found." , path ) ;
@@ -84,11 +90,11 @@ public void CreateDirectory(string path)
8490 {
8591 if ( fileSystem . ContainsKey ( path ) )
8692 {
87- fileSystem [ path ] = "" ;
93+ fileSystem [ path ] = new byte [ 0 ] ;
8894 }
8995 else
9096 {
91- fileSystem . Add ( path , "" ) ;
97+ fileSystem . Add ( path , new byte [ 0 ] ) ;
9298 }
9399 }
94100
0 commit comments