1- using MatthiWare . CommandLine ;
1+ using System . Linq ;
22using MatthiWare . CommandLine . Abstractions ;
33using MatthiWare . CommandLine . Abstractions . Command ;
44using MatthiWare . CommandLine . Abstractions . Usage ;
55using MatthiWare . CommandLine . Core . Attributes ;
6+ using MatthiWare . CommandLine . Core . Exceptions ;
7+ using MatthiWare . CommandLine . Core . Usage ;
68using Moq ;
79using Xunit ;
810
@@ -52,7 +54,7 @@ public void UsagePrinterPrintsOptionCorrectly()
5254
5355 parser . Parse ( new [ ] { "-o" , "--help" } ) ;
5456
55- printerMock . Verify ( mock => mock . PrintUsage ( It . IsAny < ICommandLineOption > ( ) ) , Times . Once ( ) ) ;
57+ printerMock . Verify ( mock => mock . PrintUsage ( It . IsAny < IArgument > ( ) ) , Times . Once ( ) ) ;
5658 }
5759
5860 [ Fact ]
@@ -71,7 +73,63 @@ public void UsagePrinterPrintsCommandCorrectly()
7173
7274 parser . Parse ( new [ ] { "-o" , "bla" , "cmd" , "--help" } ) ;
7375
74- printerMock . Verify ( mock => mock . PrintUsage ( It . IsAny < ICommandLineCommand > ( ) ) , Times . Once ( ) ) ;
76+ printerMock . Verify ( mock => mock . PrintUsage ( It . IsAny < IArgument > ( ) ) , Times . Once ( ) ) ;
7577 }
78+
79+ [ Theory ]
80+ [ InlineData ( new string [ ] { "-o" , "bla" , "cmd" } , true , false ) ]
81+ [ InlineData ( new string [ ] { "-o" , "bla" , "cmd" , "-x" , "bla" } , false , false ) ]
82+ [ InlineData ( new string [ ] { "cmd" , "-x" , "bla" } , false , true ) ]
83+ public void CustomInvokedPrinterWorksCorrectly ( string [ ] args , bool cmdPassed , bool optPassed )
84+ {
85+ var builderMock = new Mock < IUsageBuilder > ( ) ;
86+
87+ var parserOptions = new CommandLineParserOptions
88+ {
89+ AutoPrintUsageAndErrors = false
90+ } ;
91+
92+ var parser = new CommandLineParser < UsagePrinterGetsCalledOptions > ( parserOptions ) ;
93+
94+ parser . Printer = new UsagePrinter ( parserOptions , parser , builderMock . Object ) ;
95+
96+ parser . AddCommand < UsagePrinterCommandOptions > ( )
97+ . Name ( "cmd" )
98+ . Required ( ) ;
99+
100+ var result = parser . Parse ( args ) ;
101+
102+ builderMock . Verify ( mock => mock . Print ( ) , Times . Never ( ) ) ;
103+ builderMock . Verify ( mock => mock . PrintCommand ( It . IsAny < string > ( ) , It . IsAny < ICommandLineCommandContainer > ( ) ) , Times . Never ( ) ) ;
104+ builderMock . Verify ( mock => mock . PrintOption ( It . IsAny < ICommandLineOption > ( ) , It . IsAny < int > ( ) , It . IsAny < bool > ( ) ) , Times . Never ( ) ) ;
105+
106+ if ( result . HelpRequested )
107+ parser . Printer . PrintUsage ( result . HelpRequestedFor ) ;
108+
109+ if ( result . HasErrors )
110+ {
111+ foreach ( var err in result . Errors )
112+ {
113+ if ( ! ( err is BaseParserException baseParserException ) ) continue ;
114+
115+ parser . Printer . PrintUsage ( baseParserException . Argument ) ;
116+ }
117+ }
118+
119+ builderMock . Verify (
120+ mock => mock . Print ( ) ,
121+ ToTimes ( result . HelpRequested || result . HasErrors ) ) ;
122+
123+ builderMock . Verify (
124+ mock => mock . PrintCommand ( It . IsAny < string > ( ) , It . IsAny < ICommandLineCommandContainer > ( ) ) ,
125+ ToTimes ( cmdPassed ) ) ;
126+
127+ builderMock . Verify (
128+ mock => mock . PrintOption ( It . IsAny < ICommandLineOption > ( ) , It . IsAny < int > ( ) , It . IsAny < bool > ( ) ) ,
129+ ToTimes ( optPassed ) ) ;
130+ }
131+
132+ private Times ToTimes ( bool input )
133+ => input ? Times . Once ( ) : Times . Never ( ) ;
76134 }
77135}
0 commit comments