10
10
using System . Reflection ;
11
11
using System . Threading . Tasks ;
12
12
using FluentAssertions ;
13
+ using FluentAssertions . Execution ;
13
14
using Xunit ;
14
15
15
16
namespace System . CommandLine . Tests . Binding
@@ -260,10 +261,22 @@ public async Task When_argument_type_is_not_known_until_binding_then_int_paramet
260
261
[ InlineData ( typeof ( ClassWithCtorParameter < string > ) , true ) ]
261
262
[ InlineData ( typeof ( ClassWithSetter < string > ) , false ) ]
262
263
[ InlineData ( typeof ( ClassWithSetter < string > ) , true ) ]
264
+
263
265
[ InlineData ( typeof ( FileInfo ) , false ) ]
264
266
[ InlineData ( typeof ( FileInfo ) , true ) ]
265
267
[ InlineData ( typeof ( FileInfo [ ] ) , false ) ]
266
268
[ InlineData ( typeof ( FileInfo [ ] ) , true ) ]
269
+
270
+ [ InlineData ( typeof ( DirectoryInfo ) , false ) ]
271
+ [ InlineData ( typeof ( DirectoryInfo ) , true ) ]
272
+ [ InlineData ( typeof ( DirectoryInfo [ ] ) , false ) ]
273
+ [ InlineData ( typeof ( DirectoryInfo [ ] ) , true ) ]
274
+
275
+ [ InlineData ( typeof ( FileSystemInfo ) , true , nameof ( ExistingFile ) ) ]
276
+ [ InlineData ( typeof ( FileSystemInfo ) , true , nameof ( ExistingDirectory ) ) ]
277
+ [ InlineData ( typeof ( FileSystemInfo ) , true , nameof ( NonexistentPathWithTrailingSlash ) ) ]
278
+ [ InlineData ( typeof ( FileSystemInfo ) , true , nameof ( NonexistentPathWithoutTrailingSlash ) ) ]
279
+
267
280
[ InlineData ( typeof ( string [ ] ) , false ) ]
268
281
[ InlineData ( typeof ( string [ ] ) , true ) ]
269
282
[ InlineData ( typeof ( List < string > ) , false ) ]
@@ -274,9 +287,10 @@ public async Task When_argument_type_is_not_known_until_binding_then_int_paramet
274
287
[ InlineData ( typeof ( List < int > ) , true ) ]
275
288
public async Task Handler_method_receives_option_arguments_bound_to_the_specified_type (
276
289
Type type ,
277
- bool useDelegate )
290
+ bool useDelegate ,
291
+ string variation = null )
278
292
{
279
- var testCase = _bindingCases [ type ] ;
293
+ var testCase = _bindingCases [ ( type , variation ) ] ;
280
294
281
295
ICommandHandler handler ;
282
296
if ( ! useDelegate )
@@ -318,7 +332,7 @@ public async Task Handler_method_receives_option_arguments_bound_to_the_specifie
318
332
319
333
var boundValue = ( ( BoundValueCapturer ) invocationContext . InvocationResult ) . BoundValue ;
320
334
321
- boundValue . Should ( ) . BeOfType ( testCase . ParameterType ) ;
335
+ boundValue . Should ( ) . BeAssignableTo ( testCase . ParameterType ) ;
322
336
323
337
testCase . AssertBoundValue ( boundValue ) ;
324
338
}
@@ -443,14 +457,82 @@ public void Apply(InvocationContext context)
443
457
o => o . Value . Should ( ) . Be ( "123" ) ) ,
444
458
445
459
BindingTestCase . Create < FileInfo > (
446
- Path . Combine ( Directory . GetCurrentDirectory ( ) , "file1.txt" ) ,
447
- o => o . FullName . Should ( ) . Be ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "file1.txt" ) ) ) ,
460
+ Path . Combine ( ExistingDirectory ( ) , "file1.txt" ) ,
461
+ o => o . FullName
462
+ . Should ( )
463
+ . Be ( Path . Combine ( ExistingDirectory ( ) , "file1.txt" ) ) ) ,
448
464
449
465
BindingTestCase . Create < FileInfo [ ] > (
450
- $ "{ Path . Combine ( Directory . GetCurrentDirectory ( ) , "file1.txt" ) } { Path . Combine ( Directory . GetCurrentDirectory ( ) , "file2.txt" ) } ",
466
+ $ "{ Path . Combine ( ExistingDirectory ( ) , "file1.txt" ) } { Path . Combine ( ExistingDirectory ( ) , "file2.txt" ) } ",
451
467
o => o . Select ( f => f . FullName )
452
468
. Should ( )
453
- . BeEquivalentTo ( new [ ] { Path . Combine ( Directory . GetCurrentDirectory ( ) , "file1.txt" ) , Path . Combine ( Directory . GetCurrentDirectory ( ) , "file2.txt" ) } ) ) ,
469
+ . BeEquivalentTo ( new [ ]
470
+ {
471
+ Path . Combine ( ExistingDirectory ( ) , "file1.txt" ) ,
472
+ Path . Combine ( ExistingDirectory ( ) , "file2.txt" )
473
+ } ) ) ,
474
+
475
+ BindingTestCase . Create < DirectoryInfo > (
476
+ ExistingDirectory ( ) ,
477
+ fsi => fsi . Should ( )
478
+ . BeOfType < DirectoryInfo > ( )
479
+ . Which
480
+ . FullName
481
+ . Should ( )
482
+ . Be ( ExistingDirectory ( ) ) ) ,
483
+
484
+ BindingTestCase . Create < DirectoryInfo [ ] > (
485
+ $ "{ ExistingDirectory ( ) } { ExistingDirectory ( ) } ",
486
+ fsi => fsi . Should ( )
487
+ . BeAssignableTo < IEnumerable < DirectoryInfo > > ( )
488
+ . Which
489
+ . Select ( d => d . FullName )
490
+ . Should ( )
491
+ . BeEquivalentTo ( new [ ]
492
+ {
493
+ ExistingDirectory ( ) ,
494
+ ExistingDirectory ( )
495
+ } ) ) ,
496
+
497
+ BindingTestCase . Create < FileSystemInfo > (
498
+ ExistingFile ( ) ,
499
+ fsi => fsi . Should ( )
500
+ . BeOfType < FileInfo > ( )
501
+ . Which
502
+ . FullName
503
+ . Should ( )
504
+ . Be ( ExistingFile ( ) ) ,
505
+ variationName : nameof ( ExistingFile ) ) ,
506
+
507
+ BindingTestCase . Create < FileSystemInfo > (
508
+ ExistingDirectory ( ) ,
509
+ fsi => fsi . Should ( )
510
+ . BeOfType < DirectoryInfo > ( )
511
+ . Which
512
+ . FullName
513
+ . Should ( )
514
+ . Be ( ExistingDirectory ( ) ) ,
515
+ variationName : nameof ( ExistingDirectory ) ) ,
516
+
517
+ BindingTestCase . Create < FileSystemInfo > (
518
+ NonexistentPathWithTrailingSlash ( ) ,
519
+ fsi => fsi . Should ( )
520
+ . BeOfType < DirectoryInfo > ( )
521
+ . Which
522
+ . FullName
523
+ . Should ( )
524
+ . Be ( NonexistentPathWithTrailingSlash ( ) ) ,
525
+ variationName : nameof ( NonexistentPathWithTrailingSlash ) ) ,
526
+
527
+ BindingTestCase . Create < FileSystemInfo > (
528
+ NonexistentPathWithoutTrailingSlash ( ) ,
529
+ fsi => fsi . Should ( )
530
+ . BeOfType < FileInfo > ( )
531
+ . Which
532
+ . FullName
533
+ . Should ( )
534
+ . Be ( NonexistentPathWithoutTrailingSlash ( ) ) ,
535
+ variationName : nameof ( NonexistentPathWithoutTrailingSlash ) ) ,
454
536
455
537
BindingTestCase . Create < string [ ] > (
456
538
"one two" ,
@@ -468,5 +550,21 @@ public void Apply(InvocationContext context)
468
550
"1 2" ,
469
551
o => o . Should ( ) . BeEquivalentTo ( new List < int > { 1 , 2 } ) )
470
552
} ;
553
+
554
+ private static string NonexistentPathWithoutTrailingSlash ( )
555
+ {
556
+ return Path . Combine (
557
+ ExistingDirectory ( ) ,
558
+ "does-not-exist" ) ;
559
+ }
560
+
561
+ private static string NonexistentPathWithTrailingSlash ( ) =>
562
+ NonexistentPathWithoutTrailingSlash ( ) + Path . DirectorySeparatorChar ;
563
+
564
+ private static string ExistingFile ( ) =>
565
+ Directory . GetFiles ( ExistingDirectory ( ) ) . FirstOrDefault ( ) ??
566
+ throw new AssertionFailedException ( "No files found in current directory" ) ;
567
+
568
+ private static string ExistingDirectory ( ) => Directory . GetCurrentDirectory ( ) ;
471
569
}
472
570
}
0 commit comments