44using Xunit ;
55using FluentAssertions ;
66using CommandLine . Tests . Fakes ;
7+ using System . Threading . Tasks ;
78
89namespace CommandLine . Tests . Unit
910{
@@ -19,6 +20,16 @@ public static void Invoke_parsed_lambda_when_parsed()
1920 "value" . Should ( ) . BeEquivalentTo ( expected ) ;
2021 }
2122
23+ [ Fact ]
24+ public static async Task Invoke_parsed_lambda_when_parsedAsync ( )
25+ {
26+ var expected = string . Empty ;
27+ await Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "--stringvalue" , "value" } )
28+ . WithParsedAsync ( opts => Task . Run ( ( ) => expected = opts . StringValue ) ) ;
29+
30+ "value" . Should ( ) . BeEquivalentTo ( expected ) ;
31+ }
32+
2233 [ Fact ]
2334 public static void Invoke_parsed_lambda_when_parsed_for_verbs ( )
2435 {
@@ -32,6 +43,20 @@ public static void Invoke_parsed_lambda_when_parsed_for_verbs()
3243 "https://value.org/user/file.git" . Should ( ) . BeEquivalentTo ( expected ) ;
3344 }
3445
46+ [ Fact ]
47+ public static async Task Invoke_parsed_lambda_when_parsed_for_verbsAsync ( )
48+ {
49+ var expected = string . Empty ;
50+ var parsedArguments = Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb > (
51+ new [ ] { "clone" , "https://value.org/user/file.git" } ) ;
52+
53+ await parsedArguments . WithParsedAsync < Add_Verb > ( opts => Task . Run ( ( ) => expected = "wrong1" ) ) ;
54+ await parsedArguments . WithParsedAsync < Commit_Verb > ( opts => Task . Run ( ( ) => expected = "wrong2" ) ) ;
55+ await parsedArguments . WithParsedAsync < Clone_Verb > ( opts => Task . Run ( ( ) => expected = opts . Urls . First ( ) ) ) ;
56+
57+ "https://value.org/user/file.git" . Should ( ) . BeEquivalentTo ( expected ) ;
58+ }
59+
3560 [ Fact ]
3661 public static void Invoke_not_parsed_lambda_when_not_parsed ( )
3762 {
@@ -42,6 +67,16 @@ public static void Invoke_not_parsed_lambda_when_not_parsed()
4267 "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
4368 }
4469
70+ [ Fact ]
71+ public static async Task Invoke_not_parsed_lambda_when_not_parsedAsync ( )
72+ {
73+ var expected = "a default" ;
74+ await Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "-i" , "aaa" } )
75+ . WithNotParsedAsync ( _ => Task . Run ( ( ) => expected = "changed" ) ) ;
76+
77+ "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
78+ }
79+
4580 [ Fact ]
4681 public static void Invoke_not_parsed_lambda_when_parsed_for_verbs ( )
4782 {
@@ -55,6 +90,20 @@ public static void Invoke_not_parsed_lambda_when_parsed_for_verbs()
5590 "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
5691 }
5792
93+ [ Fact ]
94+ public static async Task Invoke_not_parsed_lambda_when_parsed_for_verbsAsync ( )
95+ {
96+ var expected = "a default" ;
97+ var parsedArguments = Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb > ( new [ ] { "undefined" , "-xyz" } ) ;
98+
99+ await parsedArguments . WithParsedAsync < Add_Verb > ( opts => Task . Run ( ( ) => expected = "wrong1" ) ) ;
100+ await parsedArguments . WithParsedAsync < Commit_Verb > ( opts => Task . Run ( ( ) => expected = "wrong2" ) ) ;
101+ await parsedArguments . WithParsedAsync < Clone_Verb > ( opts => Task . Run ( ( ) => expected = "wrong3" ) ) ;
102+ await parsedArguments . WithNotParsedAsync ( _ => Task . Run ( ( ) => expected = "changed" ) ) ;
103+
104+ "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
105+ }
106+
58107 [ Fact ]
59108 public static void Invoke_proper_lambda_when_parsed ( )
60109 {
@@ -66,6 +115,18 @@ public static void Invoke_proper_lambda_when_parsed()
66115 "value" . Should ( ) . BeEquivalentTo ( expected ) ;
67116 }
68117
118+ [ Fact ]
119+ public static async Task Invoke_proper_lambda_when_parsedAsync ( )
120+ {
121+ var expected = string . Empty ;
122+ var parsedArguments = Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "--stringvalue" , "value" } ) ;
123+
124+ await parsedArguments . WithParsedAsync ( opts => Task . Run ( ( ) => expected = opts . StringValue ) ) ;
125+ await parsedArguments . WithNotParsedAsync ( _ => Task . Run ( ( ) => expected = "changed" ) ) ;
126+
127+ "value" . Should ( ) . BeEquivalentTo ( expected ) ;
128+ }
129+
69130 [ Fact ]
70131 public static void Invoke_proper_lambda_when_not_parsed ( )
71132 {
@@ -77,6 +138,18 @@ public static void Invoke_proper_lambda_when_not_parsed()
77138 "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
78139 }
79140
141+ [ Fact ]
142+ public static async Task Invoke_proper_lambda_when_not_parsedAsync ( )
143+ {
144+ var expected = "a default" ;
145+ var parsedArguments = Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "-i" , "aaa" } ) ;
146+
147+ await parsedArguments . WithParsedAsync ( opts => Task . Run ( ( ) => expected = opts . StringValue ) ) ;
148+ await parsedArguments . WithNotParsedAsync ( _ => Task . Run ( ( ) => expected = "changed" ) ) ;
149+
150+ "changed" . Should ( ) . BeEquivalentTo ( expected ) ;
151+ }
152+
80153 [ Fact ]
81154 public static void Turn_sucessful_parsing_into_exit_code ( )
82155 {
@@ -86,6 +159,15 @@ public static void Turn_sucessful_parsing_into_exit_code()
86159 0 . Should ( ) . Be ( expected ) ;
87160 }
88161
162+ [ Fact ]
163+ public static async Task Turn_sucessful_parsing_into_exit_codeAsync ( )
164+ {
165+ var expected = await Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "--stringvalue" , "value" } )
166+ . MapResultAsync ( _ => Task . FromResult ( 0 ) , _ => Task . FromResult ( - 1 ) ) ;
167+
168+ 0 . Should ( ) . Be ( expected ) ;
169+ }
170+
89171 [ Fact ]
90172 public static void Turn_sucessful_parsing_into_exit_code_for_verbs ( )
91173 {
@@ -100,6 +182,20 @@ public static void Turn_sucessful_parsing_into_exit_code_for_verbs()
100182 2 . Should ( ) . Be ( expected ) ;
101183 }
102184
185+ [ Fact ]
186+ public static async Task Turn_sucessful_parsing_into_exit_code_for_verbsAsync ( )
187+ {
188+ var expected = await Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb > (
189+ new [ ] { "clone" , "https://value.org/user/file.git" } )
190+ . MapResultAsync (
191+ ( Add_Verb opts ) => Task . FromResult ( 0 ) ,
192+ ( Commit_Verb opts ) => Task . FromResult ( 1 ) ,
193+ ( Clone_Verb opts ) => Task . FromResult ( 2 ) ,
194+ errs => Task . FromResult ( 3 ) ) ;
195+
196+ 2 . Should ( ) . Be ( expected ) ;
197+ }
198+
103199 [ Fact ]
104200 public static void Turn_failed_parsing_into_exit_code ( )
105201 {
@@ -109,6 +205,15 @@ public static void Turn_failed_parsing_into_exit_code()
109205 ( - 1 ) . Should ( ) . Be ( expected ) ;
110206 }
111207
208+ [ Fact ]
209+ public static async Task Turn_failed_parsing_into_exit_codeAsync ( )
210+ {
211+ var expected = await Parser . Default . ParseArguments < Simple_Options > ( new [ ] { "-i" , "aaa" } )
212+ . MapResultAsync ( _ => Task . FromResult ( 0 ) , _ => Task . FromResult ( - 1 ) ) ;
213+
214+ ( - 1 ) . Should ( ) . Be ( expected ) ;
215+ }
216+
112217 [ Fact ]
113218 public static void Turn_failed_parsing_into_exit_code_for_verbs ( )
114219 {
@@ -123,6 +228,20 @@ public static void Turn_failed_parsing_into_exit_code_for_verbs()
123228 3 . Should ( ) . Be ( expected ) ;
124229 }
125230
231+ [ Fact ]
232+ public static async Task Turn_failed_parsing_into_exit_code_for_verbsAsync ( )
233+ {
234+ var expected = await Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb > (
235+ new [ ] { "undefined" , "-xyz" } )
236+ . MapResultAsync (
237+ ( Add_Verb opts ) => Task . FromResult ( 0 ) ,
238+ ( Commit_Verb opts ) => Task . FromResult ( 1 ) ,
239+ ( Clone_Verb opts ) => Task . FromResult ( 2 ) ,
240+ errs => Task . FromResult ( 3 ) ) ;
241+
242+ 3 . Should ( ) . Be ( expected ) ;
243+ }
244+
126245 [ Fact ]
127246 public static void Invoke_parsed_lambda_when_parsed_for_base_verbs ( )
128247 {
@@ -137,6 +256,21 @@ public static void Invoke_parsed_lambda_when_parsed_for_base_verbs()
137256 "dummy.bin" . Should ( ) . BeEquivalentTo ( expected ) ;
138257 }
139258
259+ [ Fact ]
260+ public static async Task Invoke_parsed_lambda_when_parsed_for_base_verbsAsync ( )
261+ {
262+ var expected = string . Empty ;
263+ var parsedArguments = Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb , Derived_Verb > (
264+ new [ ] { "derivedadd" , "dummy.bin" } ) ;
265+
266+ await parsedArguments . WithParsedAsync < Add_Verb > ( opts => Task . Run ( ( ) => expected = "wrong1" ) ) ;
267+ await parsedArguments . WithParsedAsync < Commit_Verb > ( opts => Task . Run ( ( ) => expected = "wrong2" ) ) ;
268+ await parsedArguments . WithParsedAsync < Clone_Verb > ( opts => Task . Run ( ( ) => expected = "wrong3" ) ) ;
269+ await parsedArguments . WithParsedAsync < Base_Class_For_Verb > ( opts => Task . Run ( ( ) => expected = opts . FileName ) ) ;
270+
271+ "dummy.bin" . Should ( ) . BeEquivalentTo ( expected ) ;
272+ }
273+
140274 [ Fact ]
141275 public static void Turn_sucessful_parsing_into_exit_code_for_single_base_verbs ( )
142276 {
@@ -149,6 +283,18 @@ public static void Turn_sucessful_parsing_into_exit_code_for_single_base_verbs()
149283 1 . Should ( ) . Be ( expected ) ;
150284 }
151285
286+ [ Fact ]
287+ public static async Task Turn_sucessful_parsing_into_exit_code_for_single_base_verbsAsync ( )
288+ {
289+ var expected = await Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb , Derived_Verb > (
290+ new [ ] { "derivedadd" , "dummy.bin" } )
291+ . MapResultAsync (
292+ ( Base_Class_For_Verb opts ) => Task . FromResult ( 1 ) ,
293+ errs => Task . FromResult ( 2 ) ) ;
294+
295+ 1 . Should ( ) . Be ( expected ) ;
296+ }
297+
152298 [ Fact ]
153299 public static void Turn_sucessful_parsing_into_exit_code_for_multiple_base_verbs ( )
154300 {
@@ -164,5 +310,21 @@ public static void Turn_sucessful_parsing_into_exit_code_for_multiple_base_verbs
164310
165311 4 . Should ( ) . Be ( expected ) ;
166312 }
313+
314+ [ Fact ]
315+ public static async Task Turn_sucessful_parsing_into_exit_code_for_multiple_base_verbsAsync ( )
316+ {
317+ var expected = await Parser . Default . ParseArguments < Add_Verb , Commit_Verb , Clone_Verb , Derived_Verb > (
318+ new [ ] { "derivedadd" , "dummy.bin" } )
319+ . MapResultAsync (
320+ ( Add_Verb opts ) => Task . FromResult ( 0 ) ,
321+ ( Commit_Verb opts ) => Task . FromResult ( 1 ) ,
322+ ( Clone_Verb opts ) => Task . FromResult ( 2 ) ,
323+ ( Base_Class_For_Verb opts ) => Task . FromResult ( 4 ) ,
324+ ( Derived_Verb opts ) => Task . FromResult ( 3 ) ,
325+ errs => Task . FromResult ( 5 ) ) ;
326+
327+ 4 . Should ( ) . Be ( expected ) ;
328+ }
167329 }
168330}
0 commit comments