44
55using System . Collections . Generic ;
66using System . IO . Enumeration ;
7+ using System . Linq ;
78using Xunit ;
89
910namespace System . IO . Tests . Enumeration
1011{
11- // For tests that cover examples from documentation, blog posts, etc.
12+ // For tests that cover examples from documentation, blog posts, etc. While these overlap with
13+ // existing tests, having explicit coverage here is extra insurance we are covering the
14+ // examples we've given out publicly.
1215 public class ExampleTests : FileSystemTest
1316 {
1417 [ Fact ]
1518 public void GetFileNamesEnumerable ( )
1619 {
20+ // https://blogs.msdn.microsoft.com/jeremykuhne/2018/03/09/custom-directory-enumeration-in-net-core-2-1/
1721 DirectoryInfo testDirectory = Directory . CreateDirectory ( GetTestFilePath ( ) ) ;
1822 File . Create ( Path . Join ( testDirectory . FullName , "one" ) ) . Dispose ( ) ;
1923 File . Create ( Path . Join ( testDirectory . FullName , "two" ) ) . Dispose ( ) ;
@@ -29,5 +33,109 @@ public void GetFileNamesEnumerable()
2933
3034 FSAssert . EqualWhenOrdered ( new string [ ] { "one" , "two" } , fileNames ) ;
3135 }
36+
37+ private static IEnumerable < FileInfo > GetFilesWithExtensions ( string directory ,
38+ bool recursive , params string [ ] extensions )
39+ {
40+ return new FileSystemEnumerable < FileInfo > (
41+ directory ,
42+ ( ref FileSystemEntry entry ) => ( FileInfo ) entry . ToFileSystemInfo ( ) ,
43+ new EnumerationOptions ( ) { RecurseSubdirectories = recursive } )
44+ {
45+ ShouldIncludePredicate = ( ref FileSystemEntry entry ) =>
46+ {
47+ if ( entry . IsDirectory )
48+ return false ;
49+ foreach ( string extension in extensions )
50+ {
51+ if ( Path . GetExtension ( entry . FileName ) . SequenceEqual ( extension ) )
52+ return true ;
53+ }
54+ return false ;
55+ }
56+ } ;
57+ }
58+
59+ [ Fact ]
60+ public void TestGetFilesWithExtensions ( )
61+ {
62+ // https://blogs.msdn.microsoft.com/jeremykuhne/2018/03/09/custom-directory-enumeration-in-net-core-2-1/
63+ DirectoryInfo testDirectory = Directory . CreateDirectory ( GetTestFilePath ( ) ) ;
64+ File . Create ( Path . Join ( testDirectory . FullName , "file.one" ) ) . Dispose ( ) ;
65+ File . Create ( Path . Join ( testDirectory . FullName , "file.two" ) ) . Dispose ( ) ;
66+ File . Create ( Path . Join ( testDirectory . FullName , "file.three" ) ) . Dispose ( ) ;
67+ DirectoryInfo subDirectory = testDirectory . CreateSubdirectory ( "three.one" ) ;
68+ File . Create ( Path . Join ( subDirectory . FullName , "subfile.one" ) ) . Dispose ( ) ;
69+
70+ FSAssert . EqualWhenOrdered (
71+ new string [ ] { "file.one" , "file.three" } ,
72+ GetFilesWithExtensions ( testDirectory . FullName , false , ".one" , ".three" ) . Select ( f => f . Name ) ) ;
73+
74+ FSAssert . EqualWhenOrdered (
75+ new string [ ] { "file.one" , "file.three" , "subfile.one" } ,
76+ GetFilesWithExtensions ( testDirectory . FullName , true , ".one" , ".three" ) . Select ( f => f . Name ) ) ;
77+ }
78+
79+ private static int CountFiles ( string directory , bool recursive )
80+ {
81+ return ( new FileSystemEnumerable < int > (
82+ directory ,
83+ ( ref FileSystemEntry entry ) => 1 ,
84+ new EnumerationOptions ( ) { RecurseSubdirectories = recursive } )
85+ {
86+ ShouldIncludePredicate = ( ref FileSystemEntry entry ) => ! entry . IsDirectory
87+ } ) . Count ( ) ;
88+ }
89+
90+ [ Fact ]
91+ public void TestCountFiles ( )
92+ {
93+ // https://blogs.msdn.microsoft.com/jeremykuhne/2018/03/09/custom-directory-enumeration-in-net-core-2-1/
94+ DirectoryInfo testDirectory = Directory . CreateDirectory ( GetTestFilePath ( ) ) ;
95+ File . Create ( Path . Join ( testDirectory . FullName , "file.one" ) ) . Dispose ( ) ;
96+ File . Create ( Path . Join ( testDirectory . FullName , "file.two" ) ) . Dispose ( ) ;
97+ File . Create ( Path . Join ( testDirectory . FullName , "file.three" ) ) . Dispose ( ) ;
98+ DirectoryInfo subDirectory = testDirectory . CreateSubdirectory ( "three.one" ) ;
99+ File . Create ( Path . Join ( subDirectory . FullName , "subfile.one" ) ) . Dispose ( ) ;
100+
101+ Assert . Equal ( 3 , CountFiles ( testDirectory . FullName , false ) ) ;
102+
103+ Assert . Equal ( 4 , CountFiles ( testDirectory . FullName , true ) ) ;
104+ }
105+
106+ private static long CountFileBytes ( string directory , bool recursive )
107+ {
108+ return ( new FileSystemEnumerable < long > (
109+ directory ,
110+ ( ref FileSystemEntry entry ) => entry . Length ,
111+ new EnumerationOptions ( ) { RecurseSubdirectories = recursive } )
112+ {
113+ ShouldIncludePredicate = ( ref FileSystemEntry entry ) => ! entry . IsDirectory
114+ } ) . Sum ( ) ;
115+ }
116+
117+ [ Fact ]
118+ public void TestCountFileBytes ( )
119+ {
120+ // https://blogs.msdn.microsoft.com/jeremykuhne/2018/03/09/custom-directory-enumeration-in-net-core-2-1/
121+ DirectoryInfo testDirectory = Directory . CreateDirectory ( GetTestFilePath ( ) ) ;
122+ FileInfo firstFile = new FileInfo ( Path . Join ( testDirectory . FullName , "file.one" ) ) ;
123+ using ( var writer = firstFile . CreateText ( ) )
124+ {
125+ for ( int i = 0 ; i < 100 ; i ++ )
126+ writer . WriteLine ( "The quick brown fox jumped over the lazy dog." ) ;
127+ }
128+
129+ firstFile . CopyTo ( Path . Join ( testDirectory . FullName , "file.two" ) ) ;
130+ firstFile . CopyTo ( Path . Join ( testDirectory . FullName , "file.three" ) ) ;
131+ DirectoryInfo subDirectory = testDirectory . CreateSubdirectory ( "three.one" ) ;
132+ firstFile . CopyTo ( Path . Join ( subDirectory . FullName , "subfile.one" ) ) ;
133+
134+ firstFile . Refresh ( ) ;
135+ Assert . True ( firstFile . Length > 0 , "The file we wrote should have a length." ) ;
136+ Assert . Equal ( firstFile . Length * 3 , CountFileBytes ( testDirectory . FullName , false ) ) ;
137+
138+ Assert . Equal ( firstFile . Length * 4 , CountFileBytes ( testDirectory . FullName , true ) ) ;
139+ }
32140 }
33141}
0 commit comments