@@ -17,36 +17,45 @@ internal DirectoryAssertions(IDirectoryInfo? instance)
1717 }
1818
1919 /// <summary>
20- /// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern"/>.
20+ /// Asserts that the current directory has at least <paramref name="minimumCount" /> directories which match the
21+ /// <paramref name="searchPattern" />.
2122 /// </summary>
22- public AndWhichConstraint < FileSystemAssertions , FileAssertions > HasSingleFileMatching (
23- string searchPattern = "*" , string because = "" , params object [ ] becauseArgs )
23+ public AndConstraint < DirectoryAssertions > HasDirectoriesMatching (
24+ string searchPattern = "*" ,
25+ int minimumCount = 1 ,
26+ string because = "" ,
27+ params object [ ] becauseArgs )
2428 {
2529 Execute . Assertion
2630 . WithDefaultIdentifier ( Identifier )
2731 . BecauseOf ( because , becauseArgs )
2832 . ForCondition ( Subject != null )
2933 . FailWith (
30- "You can't assert a directory having a given file if it is null" )
34+ "You can't assert a directory having directories if the DirectoryInfo is null. " )
3135 . Then
3236 . ForCondition ( ! string . IsNullOrEmpty ( searchPattern ) )
3337 . FailWith (
34- "You can't assert a directory having a given file if you don't pass a proper name " )
38+ "You can't assert a directory having directories if you don't pass a proper search pattern. " )
3539 . Then
3640 . Given ( ( ) => Subject ! )
3741 . ForCondition ( directoryInfo
38- => directoryInfo . GetFiles ( searchPattern ) . Length == 1 )
42+ => directoryInfo . GetDirectories ( searchPattern ) . Length >= minimumCount )
3943 . FailWith (
40- "Expected {context} {1} to contain exactly one file matching {0}{ reason}, but found {2}." ,
44+ $ "Expected {{ context}} {{1}} to contain at least { ( minimumCount == 1 ? " one directory" : $ " { minimumCount } directories" ) } matching {{0}}{{ reason}} , but { ( minimumCount == 1 ? "none was" : "only {2} were" ) } found .",
4145 _ => searchPattern ,
4246 directoryInfo => directoryInfo . Name ,
43- directoryInfo => directoryInfo . GetFiles ( searchPattern ) . Length ) ;
44-
45- return new AndWhichConstraint < FileSystemAssertions , FileAssertions > (
46- new FileSystemAssertions ( Subject ! . FileSystem ) ,
47- new FileAssertions ( Subject ! . GetFiles ( searchPattern ) . Single ( ) ) ) ;
47+ directoryInfo => directoryInfo . GetDirectories ( searchPattern ) . Length ) ;
48+
49+ return new AndConstraint < DirectoryAssertions > ( this ) ;
4850 }
4951
52+ /// <summary>
53+ /// Asserts that the current directory has at least one directory which matches the <paramref name="searchPattern" />.
54+ /// </summary>
55+ public AndConstraint < DirectoryAssertions > HasDirectoryMatching (
56+ string searchPattern = "*" , string because = "" , params object [ ] becauseArgs )
57+ => HasDirectoriesMatching ( searchPattern , 1 , because , becauseArgs ) ;
58+
5059 /// <summary>
5160 /// Asserts that the current directory has at least one file which matches the <paramref name="searchPattern" />.
5261 /// </summary>
@@ -69,11 +78,11 @@ public AndConstraint<DirectoryAssertions> HasFilesMatching(
6978 . BecauseOf ( because , becauseArgs )
7079 . ForCondition ( Subject != null )
7180 . FailWith (
72- "You can't assert a directory having files if the DirectoryInfo is null" )
81+ "You can't assert a directory having files if the DirectoryInfo is null. " )
7382 . Then
7483 . ForCondition ( ! string . IsNullOrEmpty ( searchPattern ) )
7584 . FailWith (
76- "You can't assert a directory having files if you don't pass a proper name " )
85+ "You can't assert a directory having files if you don't pass a proper search pattern. " )
7786 . Then
7887 . Given ( ( ) => Subject ! )
7988 . ForCondition ( directoryInfo
@@ -86,4 +95,66 @@ public AndConstraint<DirectoryAssertions> HasFilesMatching(
8695
8796 return new AndConstraint < DirectoryAssertions > ( this ) ;
8897 }
98+
99+ /// <summary>
100+ /// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
101+ /// </summary>
102+ public AndWhichConstraint < FileSystemAssertions , DirectoryAssertions > HasSingleDirectoryMatching (
103+ string searchPattern = "*" , string because = "" , params object [ ] becauseArgs )
104+ {
105+ Execute . Assertion
106+ . WithDefaultIdentifier ( Identifier )
107+ . BecauseOf ( because , becauseArgs )
108+ . ForCondition ( Subject != null )
109+ . FailWith (
110+ "You can't assert a directory having a given directory if it is null." )
111+ . Then
112+ . ForCondition ( ! string . IsNullOrEmpty ( searchPattern ) )
113+ . FailWith (
114+ "You can't assert a directory having a given directory if you don't pass a proper search pattern." )
115+ . Then
116+ . Given ( ( ) => Subject ! )
117+ . ForCondition ( directoryInfo
118+ => directoryInfo . GetDirectories ( searchPattern ) . Length == 1 )
119+ . FailWith (
120+ "Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}." ,
121+ _ => searchPattern ,
122+ directoryInfo => directoryInfo . Name ,
123+ directoryInfo => directoryInfo . GetDirectories ( searchPattern ) . Length ) ;
124+
125+ return new AndWhichConstraint < FileSystemAssertions , DirectoryAssertions > (
126+ new FileSystemAssertions ( Subject ! . FileSystem ) ,
127+ new DirectoryAssertions ( Subject ! . GetDirectories ( searchPattern ) . Single ( ) ) ) ;
128+ }
129+
130+ /// <summary>
131+ /// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
132+ /// </summary>
133+ public AndWhichConstraint < FileSystemAssertions , FileAssertions > HasSingleFileMatching (
134+ string searchPattern = "*" , string because = "" , params object [ ] becauseArgs )
135+ {
136+ Execute . Assertion
137+ . WithDefaultIdentifier ( Identifier )
138+ . BecauseOf ( because , becauseArgs )
139+ . ForCondition ( Subject != null )
140+ . FailWith (
141+ "You can't assert a directory having a given file if it is null." )
142+ . Then
143+ . ForCondition ( ! string . IsNullOrEmpty ( searchPattern ) )
144+ . FailWith (
145+ "You can't assert a directory having a given file if you don't pass a proper search pattern." )
146+ . Then
147+ . Given ( ( ) => Subject ! )
148+ . ForCondition ( directoryInfo
149+ => directoryInfo . GetFiles ( searchPattern ) . Length == 1 )
150+ . FailWith (
151+ "Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}." ,
152+ _ => searchPattern ,
153+ directoryInfo => directoryInfo . Name ,
154+ directoryInfo => directoryInfo . GetFiles ( searchPattern ) . Length ) ;
155+
156+ return new AndWhichConstraint < FileSystemAssertions , FileAssertions > (
157+ new FileSystemAssertions ( Subject ! . FileSystem ) ,
158+ new FileAssertions ( Subject ! . GetFiles ( searchPattern ) . Single ( ) ) ) ;
159+ }
89160}
0 commit comments