@@ -7,11 +7,19 @@ public partial class PathHelperTests
77 [ TestCase ( "//" , 1 ) ]
88 [ TestCase ( "/dir1" , 1 ) ]
99 [ TestCase ( "dir1" , 1 ) ]
10+ [ TestCase ( "dir1/" , 1 ) ]
11+ [ TestCase ( "/directory_1" , 1 ) ]
12+ [ TestCase ( "directory_1" , 1 ) ]
13+ [ TestCase ( "directory_1/" , 1 ) ]
1014 [ TestCase ( "/dir1/dir2/" , 2 ) ]
1115 [ TestCase ( "dir1/dir2" , 2 ) ]
1216 [ TestCase ( "dir1/dir2/" , 2 ) ]
1317 [ TestCase ( "///dir1/dir2////" , 2 ) ]
14- public void CountPathSegmentsTests ( string path , int expected )
18+ [ TestCase ( "/directory_1/directory_2/" , 2 ) ]
19+ [ TestCase ( "directory_1/directory_2" , 2 ) ]
20+ [ TestCase ( "directory_1/directory_2/" , 2 ) ]
21+ [ TestCase ( "///directory_1/directory_2////" , 2 ) ]
22+ public void CountPathSegments ( string path , int expected )
1523 {
1624 Assert . That (
1725 PathHelper . CountPathSegments ( path , MatchFlags . Windows ) ,
@@ -82,7 +90,7 @@ public void CountPathSegmentsTests(string path, int expected)
8290 [ TestCase ( "dir1/**/dir2/dir3" , 2 , "dir1/**" ) ]
8391 [ TestCase ( "dir1/**/dir2/dir3" , 3 , "dir1/**" ) ]
8492 [ TestCase ( "dir1/**/dir2/dir3" , 4 , "dir1/**" ) ]
85- public void GetPartialPatternTests ( string path , int depth , string expected )
93+ public void GetPartialPattern ( string path , int depth , string expected )
8694 {
8795 Assert . That (
8896 PathHelper . GetPartialPattern ( path , MatchFlags . Windows , depth ) . ToString ( ) ,
@@ -99,4 +107,116 @@ public void GetPartialPatternTests(string path, int depth, string expected)
99107 PathHelper . GetPartialPattern ( path , MatchFlags . Windows , depth ) . ToString ( ) ,
100108 Is . EqualTo ( expected ) ) ;
101109 }
110+
111+ [ Test ]
112+ public void CountPathSegments_Generated ( )
113+ {
114+ foreach ( var ( path , slash , f ) in GeneratePaths ( ) )
115+ {
116+ var expected = Math . Max ( 1 , path . Split ( slash , StringSplitOptions . RemoveEmptyEntries ) . Length ) ;
117+ var count = PathHelper . CountPathSegments ( path , f ) ;
118+
119+ Assert . That ( count , Is . EqualTo ( expected ) ) ;
120+ }
121+ }
122+
123+ [ Test ]
124+ public void GetPartialPattern_Generated ( )
125+ {
126+ foreach ( var ( path , slash , f ) in GeneratePaths ( ) . OrderBy ( _ => Random . Shared . Next ( ) ) . Take ( 500 ) )
127+ {
128+ var count = PathHelper . CountPathSegments ( path , f ) ;
129+ for ( var depth = 1 ; depth <= count * 2 ; depth ++ )
130+ {
131+ var result = PathHelper
132+ . GetPartialPattern ( path , f , depth )
133+ . ToString ( )
134+ . Split ( slash , StringSplitOptions . RemoveEmptyEntries ) ;
135+
136+ var expected = path
137+ . Split ( slash , StringSplitOptions . RemoveEmptyEntries )
138+ . Take ( depth ) ;
139+
140+ Assert . That ( result , Is . EquivalentTo ( expected ) ) ;
141+ }
142+ }
143+ }
144+
145+ [ Test ]
146+ public void SearchPathSeparator ( )
147+ {
148+ for ( var n = 0 ; n < 5000 ; n ++ )
149+ {
150+ var p0 = new string ( 'a' , n ) ;
151+
152+ var p1 = p0 + "\\ " ;
153+ var index1 = PathHelper . SearchPathSeparator ( p1 , MatchFlags . Windows ) ;
154+ var index2 = PathHelper . SearchPathSeparator ( p1 , MatchFlags . Unix ) ;
155+
156+ Assert . That ( index1 , Is . EqualTo ( p1 . IndexOf ( '\\ ' ) ) , $ "length: { n } ") ;
157+ Assert . That ( index2 , Is . EqualTo ( - 1 ) , $ "length: { n } ") ;
158+
159+ var p2 = p0 + "/" ;
160+ var index3 = PathHelper . SearchPathSeparator ( p2 , MatchFlags . Windows ) ;
161+ var index4 = PathHelper . SearchPathSeparator ( p2 , MatchFlags . Unix ) ;
162+
163+ Assert . That ( index3 , Is . EqualTo ( p2 . IndexOf ( '/' ) ) , $ "length: { n } ") ;
164+ Assert . That ( index4 , Is . EqualTo ( index3 ) , $ "length: { n } ") ;
165+ }
166+ }
167+
168+ [ Test ]
169+ public void SearchPathSeparator_Nothing ( )
170+ {
171+ var source = new string ( 'a' , 5000 ) ;
172+
173+ for ( var n = 0 ; n < 5000 ; n ++ )
174+ {
175+ var p = source . AsSpan ( 0 , n ) ;
176+ var index1 = PathHelper . SearchPathSeparator ( p , MatchFlags . Windows ) ;
177+ var index2 = PathHelper . SearchPathSeparator ( p , MatchFlags . Unix ) ;
178+
179+ Assert . That ( index1 , Is . EqualTo ( - 1 ) ) ;
180+ Assert . That ( index2 , Is . EqualTo ( - 1 ) ) ;
181+ }
182+ }
183+
184+ private static IEnumerable < ( string path , char slash , MatchFlags flags ) > GeneratePaths ( )
185+ {
186+ var flags = new [ ]
187+ {
188+ ( '/' , MatchFlags . Unix ) ,
189+ ( '/' , MatchFlags . Windows ) ,
190+ ( '\\ ' , MatchFlags . Windows )
191+ } ;
192+
193+ foreach ( var ( s , f ) in flags )
194+ {
195+ for ( var r = 1 ; r < 5 ; r ++ )
196+ for ( var n = 1 ; n < 37 ; n ++ )
197+ {
198+ var slash = new string ( s , r ) ;
199+ var segments = new string [ n ] ;
200+
201+ for ( var l = 0 ; l < 43 ; l ++ )
202+ {
203+ segments . AsSpan ( ) . Fill ( new string ( 'a' , l ) ) ;
204+
205+ var path = string . Join ( slash , segments ) ;
206+ for ( var v = 0 ; v < 4 ; v ++ )
207+ {
208+ path = v switch
209+ {
210+ 0 => path ,
211+ 1 => slash + path ,
212+ 2 => slash + path + slash ,
213+ _ => path + slash
214+ } ;
215+
216+ yield return ( path , s , f ) ;
217+ }
218+ }
219+ }
220+ }
221+ }
102222}
0 commit comments