7
7
8
8
public class TestFileSystem : IFileSystem
9
9
{
10
- Dictionary < string , string > fileSystem = new Dictionary < string , string > ( ) ;
10
+ Dictionary < string , byte [ ] > fileSystem = new Dictionary < string , byte [ ] > ( ) ;
11
11
12
12
public void Copy ( string @from , string to , bool overwrite )
13
13
{
@@ -19,7 +19,7 @@ public void Copy(string @from, string to, bool overwrite)
19
19
throw new IOException ( "File already exists" ) ;
20
20
}
21
21
22
- string source ;
22
+ byte [ ] source ;
23
23
if ( ! fileSystem . TryGetValue ( from , out source ) )
24
24
throw new FileNotFoundException ( string . Format ( "The source file '{0}' was not found" , from ) , from ) ;
25
25
@@ -44,19 +44,25 @@ public void Delete(string path)
44
44
45
45
public string ReadAllText ( string path )
46
46
{
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 ) ;
48
53
}
49
54
50
55
public void WriteAllText ( string file , string fileContents )
51
56
{
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 ) ;
60
66
}
61
67
62
68
public IEnumerable < string > DirectoryGetFiles ( string directory , string searchPattern , SearchOption searchOption )
@@ -74,7 +80,7 @@ public Stream OpenRead(string path)
74
80
if ( fileSystem . ContainsKey ( path ) )
75
81
{
76
82
var content = fileSystem [ path ] ;
77
- return new MemoryStream ( Encoding . UTF8 . GetBytes ( content ) ) ;
83
+ return new MemoryStream ( content ) ;
78
84
}
79
85
80
86
throw new FileNotFoundException ( "File not found." , path ) ;
@@ -84,11 +90,11 @@ public void CreateDirectory(string path)
84
90
{
85
91
if ( fileSystem . ContainsKey ( path ) )
86
92
{
87
- fileSystem [ path ] = "" ;
93
+ fileSystem [ path ] = new byte [ 0 ] ;
88
94
}
89
95
else
90
96
{
91
- fileSystem . Add ( path , "" ) ;
97
+ fileSystem . Add ( path , new byte [ 0 ] ) ;
92
98
}
93
99
}
94
100
0 commit comments