66using CommandLine . Tests . Fakes ;
77using Xunit ;
88using FluentAssertions ;
9+ using System . Threading . Tasks ;
910
1011namespace CommandLine . Tests . Unit
1112{
@@ -21,6 +22,16 @@ public static void Invoke_parsed_lambda_when_parsed()
2122 "value" . Should ( ) . BeEquivalentTo ( expected ) ;
2223 }
2324
25+ [ Fact ]
26+ public static async Task Invoke_parsed_lambda_when_parsedAsync ( )
27+ {
28+ var expected = string . Empty ;
29+ await Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "--stringvalue" , "value" } )
30+ . WithParsedAsync ( opts => Task . Run ( ( ) => expected = opts . StringValue ) ) ;
31+
32+ "value" . Should ( ) . BeEquivalentTo ( expected ) ;
33+ }
34+
2435 [ Fact ]
2536 public static void Invoke_parsed_lambda_when_parsed_for_verbs ( )
2637 {
@@ -34,6 +45,20 @@ public static void Invoke_parsed_lambda_when_parsed_for_verbs()
3445 "https://value.org/user/file.git" . Should ( ) . BeEquivalentTo ( expected ) ;
3546 }
3647
48+ [ Fact ]
49+ public static async Task Invoke_parsed_lambda_when_parsed_for_verbsAsync ( )
50+ {
51+ var expected = string . Empty ;
52+ var parsedArguments = Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb > (
53+ new [ ] { "clone" , "https://value.org/user/file.git" } ) ;
54+
55+ await parsedArguments . WithParsedAsync < Add_Verb > ( opts => Task . Run ( ( ) => expected = "wrong1" ) ) ;
56+ await parsedArguments . WithParsedAsync < Commit_Verb > ( opts => Task . Run ( ( ) => expected = "wrong2" ) ) ;
57+ await parsedArguments . WithParsedAsync < Clone_Verb > ( opts => Task . Run ( ( ) => expected = opts . Urls . First ( ) ) ) ;
58+
59+ "https://value.org/user/file.git" . Should ( ) . BeEquivalentTo ( expected ) ;
60+ }
61+
3762 [ Fact ]
3863 public static void Invoke_not_parsed_lambda_when_not_parsed ( )
3964 {
@@ -44,6 +69,16 @@ public static void Invoke_not_parsed_lambda_when_not_parsed()
4469 "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
4570 }
4671
72+ [ Fact ]
73+ public static async Task Invoke_not_parsed_lambda_when_not_parsedAsync ( )
74+ {
75+ var expected = "a default" ;
76+ await Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "-i" , "aaa" } )
77+ . WithNotParsedAsync ( _ => Task . Run ( ( ) => expected = "changed" ) ) ;
78+
79+ "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
80+ }
81+
4782 [ Fact ]
4883 public static void Invoke_not_parsed_lambda_when_parsed_for_verbs ( )
4984 {
@@ -57,6 +92,20 @@ public static void Invoke_not_parsed_lambda_when_parsed_for_verbs()
5792 "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
5893 }
5994
95+ [ Fact ]
96+ public static async Task Invoke_not_parsed_lambda_when_parsed_for_verbsAsync ( )
97+ {
98+ var expected = "a default" ;
99+ var parsedArguments = Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb > ( new [ ] { "undefined" , "-xyz" } ) ;
100+
101+ await parsedArguments . WithParsedAsync < Add_Verb > ( opts => Task . Run ( ( ) => expected = "wrong1" ) ) ;
102+ await parsedArguments . WithParsedAsync < Commit_Verb > ( opts => Task . Run ( ( ) => expected = "wrong2" ) ) ;
103+ await parsedArguments . WithParsedAsync < Clone_Verb > ( opts => Task . Run ( ( ) => expected = "wrong3" ) ) ;
104+ await parsedArguments . WithNotParsedAsync ( _ => Task . Run ( ( ) => expected = "changed" ) ) ;
105+
106+ "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
107+ }
108+
60109 [ Fact ]
61110 public static void Invoke_proper_lambda_when_parsed ( )
62111 {
@@ -68,6 +117,18 @@ public static void Invoke_proper_lambda_when_parsed()
68117 "value" . Should ( ) . BeEquivalentTo ( expected ) ;
69118 }
70119
120+ [ Fact ]
121+ public static async Task Invoke_proper_lambda_when_parsedAsync ( )
122+ {
123+ var expected = string . Empty ;
124+ var parsedArguments = Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "--stringvalue" , "value" } ) ;
125+
126+ await parsedArguments . WithParsedAsync ( opts => Task . Run ( ( ) => expected = opts . StringValue ) ) ;
127+ await parsedArguments . WithNotParsedAsync ( _ => Task . Run ( ( ) => expected = "changed" ) ) ;
128+
129+ "value" . Should ( ) . BeEquivalentTo ( expected ) ;
130+ }
131+
71132 [ Fact ]
72133 public static void Invoke_proper_lambda_when_not_parsed ( )
73134 {
@@ -79,6 +140,18 @@ public static void Invoke_proper_lambda_when_not_parsed()
79140 "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
80141 }
81142
143+ [ Fact ]
144+ public static async Task Invoke_proper_lambda_when_not_parsedAsync ( )
145+ {
146+ var expected = "a default" ;
147+ var parsedArguments = Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "-i" , "aaa" } ) ;
148+
149+ await parsedArguments . WithParsedAsync ( opts => Task . Run ( ( ) => expected = opts . StringValue ) ) ;
150+ await parsedArguments . WithNotParsedAsync ( _ => Task . Run ( ( ) => expected = "changed" ) ) ;
151+
152+ "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
153+ }
154+
82155 [ Fact ]
83156 public static void Turn_sucessful_parsing_into_exit_code ( )
84157 {
@@ -88,6 +161,15 @@ public static void Turn_sucessful_parsing_into_exit_code()
88161 0 . Should ( ) . Be ( expected ) ;
89162 }
90163
164+ [ Fact ]
165+ public static async Task Turn_sucessful_parsing_into_exit_codeAsync ( )
166+ {
167+ var expected = await Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "--stringvalue" , "value" } )
168+ . MapResultAsync ( _ => Task . FromResult ( 0 ) , _ => Task . FromResult ( - 1 ) ) ;
169+
170+ 0 . Should ( ) . Be ( expected ) ;
171+ }
172+
91173 [ Fact ]
92174 public static void Turn_sucessful_parsing_into_exit_code_for_verbs ( )
93175 {
@@ -102,6 +184,20 @@ public static void Turn_sucessful_parsing_into_exit_code_for_verbs()
102184 2 . Should ( ) . Be ( expected ) ;
103185 }
104186
187+ [ Fact ]
188+ public static async Task Turn_sucessful_parsing_into_exit_code_for_verbsAsync ( )
189+ {
190+ var expected = await Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb > (
191+ new [ ] { "clone" , "https://value.org/user/file.git" } )
192+ . MapResultAsync (
193+ ( Add_Verb opts ) => Task . FromResult ( 0 ) ,
194+ ( Commit_Verb opts ) => Task . FromResult ( 1 ) ,
195+ ( Clone_Verb opts ) => Task . FromResult ( 2 ) ,
196+ errs => Task . FromResult ( 3 ) ) ;
197+
198+ 2 . Should ( ) . Be ( expected ) ;
199+ }
200+
105201 [ Fact ]
106202 public static void Turn_failed_parsing_into_exit_code ( )
107203 {
@@ -111,6 +207,15 @@ public static void Turn_failed_parsing_into_exit_code()
111207 ( - 1 ) . Should ( ) . Be ( expected ) ;
112208 }
113209
210+ [ Fact ]
211+ public static async Task Turn_failed_parsing_into_exit_codeAsync ( )
212+ {
213+ var expected = await Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "-i" , "aaa" } )
214+ . MapResultAsync ( _ => Task . FromResult ( 0 ) , _ => Task . FromResult ( - 1 ) ) ;
215+
216+ ( - 1 ) . Should ( ) . Be ( expected ) ;
217+ }
218+
114219 [ Fact ]
115220 public static void Turn_failed_parsing_into_exit_code_for_verbs ( )
116221 {
@@ -125,6 +230,20 @@ public static void Turn_failed_parsing_into_exit_code_for_verbs()
125230 3 . Should ( ) . Be ( expected ) ;
126231 }
127232
233+ [ Fact ]
234+ public static async Task Turn_failed_parsing_into_exit_code_for_verbsAsync ( )
235+ {
236+ var expected = await Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb > (
237+ new [ ] { "undefined" , "-xyz" } )
238+ . MapResultAsync (
239+ ( Add_Verb opts ) => Task . FromResult ( 0 ) ,
240+ ( Commit_Verb opts ) => Task . FromResult ( 1 ) ,
241+ ( Clone_Verb opts ) => Task . FromResult ( 2 ) ,
242+ errs => Task . FromResult ( 3 ) ) ;
243+
244+ 3 . Should ( ) . Be ( expected ) ;
245+ }
246+
128247 [ Fact ]
129248 public static void Invoke_parsed_lambda_when_parsed_for_base_verbs ( )
130249 {
@@ -139,6 +258,21 @@ public static void Invoke_parsed_lambda_when_parsed_for_base_verbs()
139258 "dummy.bin" . Should ( ) . BeEquivalentTo ( expected ) ;
140259 }
141260
261+ [ Fact ]
262+ public static async Task Invoke_parsed_lambda_when_parsed_for_base_verbsAsync ( )
263+ {
264+ var expected = string . Empty ;
265+ var parsedArguments = Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb , Derived_Verb > (
266+ new [ ] { "derivedadd" , "dummy.bin" } ) ;
267+
268+ await parsedArguments . WithParsedAsync < Add_Verb > ( opts => Task . Run ( ( ) => expected = "wrong1" ) ) ;
269+ await parsedArguments . WithParsedAsync < Commit_Verb > ( opts => Task . Run ( ( ) => expected = "wrong2" ) ) ;
270+ await parsedArguments . WithParsedAsync < Clone_Verb > ( opts => Task . Run ( ( ) => expected = "wrong3" ) ) ;
271+ await parsedArguments . WithParsedAsync < Base_Class_For_Verb > ( opts => Task . Run ( ( ) => expected = opts . FileName ) ) ;
272+
273+ "dummy.bin" . Should ( ) . BeEquivalentTo ( expected ) ;
274+ }
275+
142276 [ Fact ]
143277 public static void Turn_sucessful_parsing_into_exit_code_for_single_base_verbs ( )
144278 {
@@ -151,6 +285,18 @@ public static void Turn_sucessful_parsing_into_exit_code_for_single_base_verbs()
151285 1 . Should ( ) . Be ( expected ) ;
152286 }
153287
288+ [ Fact ]
289+ public static async Task Turn_sucessful_parsing_into_exit_code_for_single_base_verbsAsync ( )
290+ {
291+ var expected = await Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb , Derived_Verb > (
292+ new [ ] { "derivedadd" , "dummy.bin" } )
293+ . MapResultAsync (
294+ ( Base_Class_For_Verb opts ) => Task . FromResult ( 1 ) ,
295+ errs => Task . FromResult ( 2 ) ) ;
296+
297+ 1 . Should ( ) . Be ( expected ) ;
298+ }
299+
154300 [ Fact ]
155301 public static void Turn_sucessful_parsing_into_exit_code_for_multiple_base_verbs ( )
156302 {
@@ -166,5 +312,21 @@ public static void Turn_sucessful_parsing_into_exit_code_for_multiple_base_verbs
166312
167313 4 . Should ( ) . Be ( expected ) ;
168314 }
315+
316+ [ Fact ]
317+ public static async Task Turn_sucessful_parsing_into_exit_code_for_multiple_base_verbsAsync ( )
318+ {
319+ var expected = await Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb , Derived_Verb > (
320+ new [ ] { "derivedadd" , "dummy.bin" } )
321+ . MapResultAsync (
322+ ( Add_Verb opts ) => Task . FromResult ( 0 ) ,
323+ ( Commit_Verb opts ) => Task . FromResult ( 1 ) ,
324+ ( Clone_Verb opts ) => Task . FromResult ( 2 ) ,
325+ ( Base_Class_For_Verb opts ) => Task . FromResult ( 4 ) ,
326+ ( Derived_Verb opts ) => Task . FromResult ( 3 ) ,
327+ errs => Task . FromResult ( 5 ) ) ;
328+
329+ 4 . Should ( ) . Be ( expected ) ;
330+ }
169331 }
170332}
0 commit comments