@@ -12,7 +12,9 @@ public class FileSystemTests
1212 public void ReadTrimsBlankLines ( )
1313 {
1414 //arrange
15- var fs = new FileSystem ( ) ;
15+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
16+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
17+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
1618
1719 //act
1820 var data = fs . Read ( "readme.txt" ) ;
@@ -25,7 +27,9 @@ public void ReadTrimsBlankLines()
2527 public void WriteTrimsBlankLines ( )
2628 {
2729 //arrange
28- var fs = new FileSystem ( ) ;
30+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
31+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
32+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
2933
3034 //act
3135 fs . Write ( "writeme.txt" , "456\n " ) ;
@@ -38,7 +42,9 @@ public void WriteTrimsBlankLines()
3842 public void ExistsWhenDirectoryExists ( )
3943 {
4044 //arrange
41- var fs = new FileSystem ( ) ;
45+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
46+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
47+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
4248
4349 //act
4450 var exists = fs . Exists ( Directory . GetCurrentDirectory ( ) ) ;
@@ -51,7 +57,9 @@ public void ExistsWhenDirectoryExists()
5157 public void ExistsWhenFileExists ( )
5258 {
5359 //arrange
54- var fs = new FileSystem ( ) ;
60+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
61+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
62+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
5563
5664 //act
5765 var exists = fs . Exists ( "readme.txt" ) ;
@@ -64,7 +72,9 @@ public void ExistsWhenFileExists()
6472 public void DoesNotExistWhenNeitherDirectoryNorFileExists ( )
6573 {
6674 //arrange
67- var fs = new FileSystem ( ) ;
75+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
76+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
77+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
6878
6979 //act
7080 var exists = fs . Exists ( "other" ) ;
@@ -77,10 +87,13 @@ public void DoesNotExistWhenNeitherDirectoryNorFileExists()
7787 public void WaitForPassesWhenFileExists ( )
7888 {
7989 //arrange
80- var fs = new FileSystem ( ) ;
90+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
91+ fileInfo . Exists . Returns ( true ) ;
92+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
93+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
8194
8295 //act
83- fs . WaitFor ( "readme.txt" , TimeSpan . FromMilliseconds ( 1 ) ) ;
96+ fs . WaitFor ( "readme.txt" , TimeSpan . FromMilliseconds ( 10 ) ) ;
8497
8598 //assert
8699 Assert . True ( true ) ;
@@ -90,10 +103,12 @@ public void WaitForPassesWhenFileExists()
90103 public void WaitForFailsWhenFileDoesNotExist ( )
91104 {
92105 //arrange
93- var fs = new FileSystem ( ) ;
106+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
107+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
108+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
94109
95110 //act
96- var wait = new Action ( ( ) => fs . WaitFor ( "other" , TimeSpan . FromMilliseconds ( 1 ) ) ) ;
111+ var wait = new Action ( ( ) => fs . WaitFor ( "other" , TimeSpan . FromMilliseconds ( 10 ) ) ) ;
97112
98113 //assert
99114 Assert . Throws < TimeoutException > ( wait ) ;
@@ -103,10 +118,14 @@ public void WaitForFailsWhenFileDoesNotExist()
103118 public void WaitForWriteablePassesWhenFileIsWriteable ( )
104119 {
105120 //arrange
106- var fs = new FileSystem ( ) ;
121+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
122+ fileInfo . Exists . Returns ( true ) ;
123+ fileInfo . IsReadOnly . Returns ( false ) ;
124+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
125+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
107126
108127 //act
109- fs . WaitForWriteable ( "readme.txt" , TimeSpan . FromMilliseconds ( 1 ) ) ;
128+ fs . WaitForWriteable ( "readme.txt" , TimeSpan . FromMilliseconds ( 10 ) ) ;
110129
111130 //assert
112131 Assert . True ( true ) ;
@@ -118,13 +137,60 @@ public void WaitForWriteableFailsWhenFileIsReadOnly()
118137 //arrange
119138 var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
120139 fileInfo . IsReadOnly . Returns ( true ) ;
121- var fs = new FileSystem ( path => fileInfo ) ;
140+ var nfw = Substitute . For < IFileWatcher > ( ) ;
141+ var fs = new FileSystem ( path => fileInfo , ( fileSystem , path , predicate , action ) => nfw ) ;
122142
123143 //act
124- var wait = new Action ( ( ) => fs . WaitForWriteable ( "readonly.txt" , TimeSpan . FromMilliseconds ( 1 ) ) ) ;
144+ var wait = new Action ( ( ) => fs . WaitForWriteable ( "readonly.txt" , TimeSpan . FromMilliseconds ( 10 ) ) ) ;
125145
126146 //assert
127147 Assert . Throws < TimeoutException > ( wait ) ;
128148 }
149+
150+ [ Fact ]
151+ public void WatchStartsWatcherWhenActionIsNotNull ( )
152+ {
153+ //arrange
154+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
155+ var nfw = Substitute . For < IFileWatcher > ( ) ;
156+ var fs = new FileSystem ( path => fileInfo , ( fileSystem , path , predicate , action ) => nfw ) ;
157+
158+ //act
159+ fs . Watch ( "" , ( ) => true , ( ) => { } ) ;
160+
161+ //assert
162+ nfw . Received ( ) . Watch ( ) ;
163+ }
164+
165+ [ Fact ]
166+ public void WatchStopsWatcherWhenActionIsNull ( )
167+ {
168+ //arrange
169+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
170+ var nfw = Substitute . For < IFileWatcher > ( ) ;
171+ var fs = new FileSystem ( path => fileInfo , ( fileSystem , path , predicate , action ) => nfw ) ;
172+
173+ //act
174+ fs . Watch ( "" , ( ) => true , null ) ;
175+
176+ //assert
177+ nfw . Received ( ) . Stop ( ) ;
178+ }
179+
180+ [ Fact ]
181+ public void WatchUsesCachedWatcherWhenExists ( )
182+ {
183+ //arrange
184+ var fileInfo = Substitute . For < IFileInfoWrapper > ( ) ;
185+ var nfw = Substitute . For < Func < IFileSystem , string , Func < bool > , Action , IFileWatcher > > ( ) ;
186+ var fs = new FileSystem ( path => fileInfo , nfw ) ;
187+ fs . Watch ( "" , ( ) => true , ( ) => { } ) ;
188+
189+ //act
190+ fs . Watch ( "" , ( ) => true , ( ) => { } ) ;
191+
192+ //assert
193+ nfw . Received ( 1 ) . Invoke ( Arg . Any < IFileSystem > ( ) , Arg . Any < string > ( ) , Arg . Any < Func < bool > > ( ) , Arg . Any < Action > ( ) ) ;
194+ }
129195 }
130196}
0 commit comments