22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
5+ using System . IO ;
56using System . Reflection ;
7+ using System . Text ;
68using Xunit ;
79using Xunit . Abstractions ;
810
@@ -17,14 +19,14 @@ public SubcommandTests(ITestOutputHelper output)
1719 _output = output ;
1820 }
1921
20- [ Command ( StopParsingAfterHelpOption = false ) ]
22+ [ Command ( Name = "master" ) ]
2123 [ Subcommand ( "level1" , typeof ( Level1Cmd ) ) ]
2224 private class MasterApp : CommandBase
2325 {
2426 [ Option ( Inherited = true ) ]
2527 public bool Verbose { get ; set ; }
2628
27- protected override int OnExecute ( CommandLineApplication app ) => IsHelp ? 100 : 0 ;
29+ protected override int OnExecute ( CommandLineApplication app ) => 100 ;
2830 }
2931
3032 [ Subcommand ( "level2" , typeof ( Level2Cmd ) ) ]
@@ -33,7 +35,7 @@ private class Level1Cmd : CommandBase
3335 [ Option ( "--mid" ) ]
3436 public bool Mid { get ; }
3537
36- protected override int OnExecute ( CommandLineApplication app ) => IsHelp ? 101 : 1 ;
38+ protected override int OnExecute ( CommandLineApplication app ) => 101 ;
3739
3840 public MasterApp Parent { get ; }
3941 }
@@ -43,10 +45,8 @@ private class Level2Cmd : CommandBase
4345 [ Option ( "--value" ) ]
4446 public int ? Value { get ; set ; }
4547
46- protected override int OnExecute ( CommandLineApplication app ) =>
47- IsHelp
48- ? 102
49- : Value . HasValue ? Value . Value : 2 ;
48+ protected override int OnExecute ( CommandLineApplication app )
49+ => Value . HasValue ? Value . Value : 102 ;
5050
5151 public Level1Cmd Parent { get ; }
5252 }
@@ -66,41 +66,65 @@ public void ItInvokesExecuteOnSubcommand_Bottom()
6666 {
6767 var rc = CommandLineApplication . Execute < MasterApp > ( new TestConsole ( _output ) , "level1" , "level2" , "--value" , "6" ) ;
6868 Assert . Equal ( 6 , rc ) ;
69+
70+ rc = CommandLineApplication . Execute < MasterApp > ( new TestConsole ( _output ) , "level1" , "level2" ) ;
71+ Assert . Equal ( 102 , rc ) ;
6972 }
7073
7174 [ Fact ]
7275 public void ItInvokesExecuteOnSubcommand_Middle ( )
7376 {
7477 var rc = CommandLineApplication . Execute < MasterApp > ( new TestConsole ( _output ) , "level1" ) ;
75- Assert . Equal ( 1 , rc ) ;
78+ Assert . Equal ( 101 , rc ) ;
7679 }
7780
7881 [ Fact ]
7982 public void ItInvokesExecuteOnSubcommand_Top ( )
8083 {
8184 var rc = CommandLineApplication . Execute < MasterApp > ( new TestConsole ( _output ) ) ;
82- Assert . Equal ( 0 , rc ) ;
85+ Assert . Equal ( 100 , rc ) ;
8386 }
8487
8588 [ Fact ]
8689 public void HandlesHelp_Bottom ( )
8790 {
88- var rc = CommandLineApplication . Execute < MasterApp > ( new TestConsole ( _output ) , "level1" , "level2" , "--help" ) ;
89- Assert . Equal ( 102 , rc ) ;
91+ var sb = new StringBuilder ( ) ;
92+ var output = new StringWriter ( sb ) ;
93+ var console = new TestConsole ( _output )
94+ {
95+ Out = output ,
96+ } ;
97+ var rc = CommandLineApplication . Execute < MasterApp > ( console , "level1" , "level2" , "--help" ) ;
98+ Assert . Equal ( 0 , rc ) ;
99+ Assert . Contains ( "Usage: master level1 level2 [options]" , sb . ToString ( ) ) ;
90100 }
91101
92102 [ Fact ]
93103 public void HandlesHelp_Middle ( )
94104 {
95- var rc = CommandLineApplication . Execute < MasterApp > ( new TestConsole ( _output ) , "level1" , "--help" ) ;
96- Assert . Equal ( 101 , rc ) ;
105+ var sb = new StringBuilder ( ) ;
106+ var output = new StringWriter ( sb ) ;
107+ var console = new TestConsole ( _output )
108+ {
109+ Out = output ,
110+ } ;
111+ var rc = CommandLineApplication . Execute < MasterApp > ( console , "level1" , "--help" ) ;
112+ Assert . Equal ( 0 , rc ) ;
113+ Assert . Contains ( "Usage: master level1 [options]" , sb . ToString ( ) ) ;
97114 }
98115
99116 [ Fact ]
100117 public void HandlesHelp_Top ( )
101118 {
102- var rc = CommandLineApplication . Execute < MasterApp > ( new TestConsole ( _output ) , "--help" ) ;
103- Assert . Equal ( 100 , rc ) ;
119+ var sb = new StringBuilder ( ) ;
120+ var output = new StringWriter ( sb ) ;
121+ var console = new TestConsole ( _output )
122+ {
123+ Out = output ,
124+ } ;
125+ var rc = CommandLineApplication . Execute < MasterApp > ( console , "--help" ) ;
126+ Assert . Equal ( 0 , rc ) ;
127+ Assert . Contains ( "Usage: master [options]" , sb . ToString ( ) ) ;
104128 }
105129
106130 [ Fact ]
0 commit comments