2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using FluentAssertions ;
5
+ using Microsoft . VisualBasic . FileIO ;
5
6
using System . CommandLine . Directives ;
6
7
using System . CommandLine . Parsing ;
8
+ using System . CommandLine . ValueConditions ;
7
9
using Xunit ;
8
10
using static System . CommandLine . Subsystems . Tests . TestData ;
9
11
@@ -12,39 +14,67 @@ namespace System.CommandLine.Subsystems.Tests;
12
14
public class ValidationSubsystemTests
13
15
{
14
16
// Running exactly the same code is important here because missing a step will result in a false positive. Ask me how I know
15
- private ( CliCommand rootCommand , CliConfiguration configuration ) GetCliWithRange < T > ( T lowerBound , T upperBound )
16
- where T : IComparable < T >
17
+ private CliOption GetOptionWithSimpleRange < T > ( T lowerBound , T upperBound )
18
+ where T : IComparable < T >
17
19
{
18
20
var option = new CliOption < int > ( "--intOpt" ) ;
19
21
option . SetRange ( lowerBound , upperBound ) ;
20
- var rootCommand = new CliRootCommand { option } ;
21
- return ( rootCommand , new CliConfiguration ( rootCommand ) ) ;
22
+ return option ;
22
23
}
23
24
24
- private PipelineResult ExecutedPipelineResultForRange < T > ( T lowerBound , T upperBound , string input )
25
+ private CliOption GetOptionWithRangeBounds < T > ( RangeBound < T > lowerBound , RangeBound < T > upperBound )
25
26
where T : IComparable < T >
26
27
{
27
- ( var rootCommand , var configuration ) = GetCliWithRange ( lowerBound , upperBound ) ;
28
+ var option = new CliOption < int > ( "--intOpt" ) ;
29
+ option . SetRange ( lowerBound , upperBound ) ;
30
+ return option ;
31
+ }
32
+
33
+ private PipelineResult ExecutedPipelineResultForRangeOption ( CliOption option , string input )
34
+ {
35
+ var command = new CliRootCommand { option } ;
36
+ return ExecutedPipelineResultForCommand ( command , input ) ;
37
+ }
38
+
39
+ private PipelineResult ExecutedPipelineResultForCommand ( CliCommand command , string input )
40
+ {
28
41
var validationSubsystem = ValidationSubsystem . Create ( ) ;
29
- var parseResult = CliParser . Parse ( rootCommand , input , configuration ) ;
30
- var pipelineResult = new PipelineResult ( parseResult , input , null ) ;
42
+ var parseResult = CliParser . Parse ( command , input , new CliConfiguration ( command ) ) ;
43
+ var pipelineResult = new PipelineResult ( parseResult , input , Pipeline . CreateEmpty ( ) ) ;
31
44
validationSubsystem . Execute ( pipelineResult ) ;
32
45
return pipelineResult ;
33
46
}
34
47
35
48
[ Fact ]
36
49
public void Int_values_in_specified_range_do_not_have_errors ( )
37
50
{
38
- var pipelineResult = ExecutedPipelineResultForRange ( 0 , 50 , "--intOpt 42" ) ;
51
+ var option = GetOptionWithSimpleRange ( 0 , 50 ) ;
52
+
53
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 42" ) ;
39
54
40
55
pipelineResult . Should ( ) . NotBeNull ( ) ;
41
56
pipelineResult . GetErrors ( ) . Should ( ) . BeEmpty ( ) ;
42
57
}
43
58
44
59
[ Fact ]
45
- public void Int_values_not_in_specified_range_report_error ( )
60
+ public void Int_values_above_upper_bound_report_error ( )
61
+ {
62
+ var option = GetOptionWithSimpleRange ( 0 , 5 ) ;
63
+
64
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 42" ) ;
65
+
66
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
67
+ pipelineResult . GetErrors ( ) . Should ( ) . HaveCount ( 1 ) ;
68
+ var error = pipelineResult . GetErrors ( ) . First ( ) ;
69
+ // TODO: Create test mechanism for CliDiagnostics
70
+ }
71
+
72
+ [ Fact ]
73
+ public void Int_below_lower_bound_report_error ( )
46
74
{
47
- var pipelineResult = ExecutedPipelineResultForRange ( 0 , 5 , "--intOpt 42" ) ;
75
+ var option = GetOptionWithSimpleRange ( 0 , 5 ) ;
76
+
77
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt -42" ) ;
48
78
49
79
pipelineResult . Should ( ) . NotBeNull ( ) ;
50
80
pipelineResult . GetErrors ( ) . Should ( ) . HaveCount ( 1 ) ;
@@ -55,7 +85,9 @@ public void Int_values_not_in_specified_range_report_error()
55
85
[ Fact ]
56
86
public void Int_values_on_lower_range_bound_do_not_report_error ( )
57
87
{
58
- var pipelineResult = ExecutedPipelineResultForRange ( 42 , 50 , "--intOpt 42" ) ;
88
+ var option = GetOptionWithSimpleRange ( 42 , 50 ) ;
89
+
90
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 42" ) ;
59
91
60
92
pipelineResult . Should ( ) . NotBeNull ( ) ;
61
93
pipelineResult . GetErrors ( ) . Should ( ) . BeEmpty ( ) ;
@@ -64,11 +96,141 @@ public void Int_values_on_lower_range_bound_do_not_report_error()
64
96
[ Fact ]
65
97
public void Int_values_on_upper_range_bound_do_not_report_error ( )
66
98
{
67
- var pipelineResult = ExecutedPipelineResultForRange ( 0 , 42 , "--intOpt 42" ) ;
99
+ var option = GetOptionWithSimpleRange ( 0 , 42 ) ;
100
+
101
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 42" ) ;
68
102
69
103
pipelineResult . Should ( ) . NotBeNull ( ) ;
70
104
pipelineResult . GetErrors ( ) . Should ( ) . BeEmpty ( ) ;
71
105
}
72
106
107
+ [ Fact ]
108
+ public void Values_below_calculated_lower_bound_report_error ( )
109
+ {
110
+ var option = GetOptionWithRangeBounds ( RangeBound < int > . Create ( ( ) => 1 ) , 50 ) ;
111
+
112
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 0" ) ;
113
+
114
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
115
+ pipelineResult . GetErrors ( ) . Should ( ) . HaveCount ( 1 ) ;
116
+ var error = pipelineResult . GetErrors ( ) . First ( ) ;
117
+ // TODO: Create test mechanism for CliDiagnostics
118
+ }
119
+
120
+
121
+ [ Fact ]
122
+ public void Values_within_calculated_range_do_not_report_error ( )
123
+ {
124
+ var option = GetOptionWithRangeBounds ( RangeBound < int > . Create ( ( ) => 1 ) , RangeBound < int > . Create ( ( ) => 50 ) ) ;
125
+
126
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 42" ) ;
127
+
128
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
129
+ pipelineResult . GetErrors ( ) . Should ( ) . BeEmpty ( ) ;
130
+ }
131
+
132
+ [ Fact ]
133
+ public void Values_above_calculated_upper_bound_report_error ( )
134
+ {
135
+ var option = GetOptionWithRangeBounds ( 0 , RangeBound < int > . Create ( ( ) => 40 ) ) ;
136
+
137
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 42" ) ;
138
+
139
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
140
+ pipelineResult . GetErrors ( ) . Should ( ) . HaveCount ( 1 ) ;
141
+ var error = pipelineResult . GetErrors ( ) . First ( ) ;
142
+ // TODO: Create test mechanism for CliDiagnostics
143
+ }
144
+
145
+ [ Fact ]
146
+ public void Values_below_relative_lower_bound_report_error ( )
147
+ {
148
+ var otherOption = new CliOption < int > ( "-a" ) ;
149
+ var option = GetOptionWithRangeBounds ( RangeBound < int > . Create ( otherOption , o => ( int ) o + 1 ) , 50 ) ;
150
+ var command = new CliCommand ( "cmd" ) { option , otherOption } ;
151
+
152
+ var pipelineResult = ExecutedPipelineResultForCommand ( command , "--intOpt 0 -a 0" ) ;
153
+
154
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
155
+ pipelineResult . GetErrors ( ) . Should ( ) . HaveCount ( 1 ) ;
156
+ var error = pipelineResult . GetErrors ( ) . First ( ) ;
157
+ // TODO: Create test mechanism for CliDiagnostics
158
+ }
159
+
160
+
161
+ [ Fact ]
162
+ public void Values_within_relative_range_do_not_report_error ( )
163
+ {
164
+ var otherOption = new CliOption < int > ( "-a" ) ;
165
+ var option = GetOptionWithRangeBounds ( RangeBound < int > . Create ( otherOption , o => ( int ) o + 1 ) , RangeBound < int > . Create ( otherOption , o => ( int ) o + 10 ) ) ;
166
+ var command = new CliCommand ( "cmd" ) { option , otherOption } ;
167
+
168
+ var pipelineResult = ExecutedPipelineResultForCommand ( command , "--intOpt 11 -a 3" ) ;
169
+
170
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
171
+ pipelineResult . GetErrors ( ) . Should ( ) . BeEmpty ( ) ;
172
+ }
173
+
174
+ [ Fact ]
175
+ public void Values_above_relative_upper_bound_report_error ( )
176
+ {
177
+ var otherOption = new CliOption < int > ( "-a" ) ;
178
+ var option = GetOptionWithRangeBounds ( 0 , RangeBound < int > . Create ( otherOption , o => ( int ) o + 10 ) ) ;
179
+ var command = new CliCommand ( "cmd" ) { option , otherOption } ;
180
+
181
+ var pipelineResult = ExecutedPipelineResultForCommand ( command , "--intOpt 9 -a -2" ) ;
182
+
183
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
184
+ pipelineResult . GetErrors ( ) . Should ( ) . HaveCount ( 1 ) ;
185
+ var error = pipelineResult . GetErrors ( ) . First ( ) ;
186
+ // TODO: Create test mechanism for CliDiagnostics
187
+ }
188
+
189
+ [ Fact ]
190
+ public void Values_below_environment_lower_bound_report_error ( )
191
+ {
192
+ var envName = "SYSTEM_COMMANDLINE_LOWERBOUND" ;
193
+ Environment . SetEnvironmentVariable ( envName , "2" ) ;
194
+ var option = GetOptionWithRangeBounds ( RangeBound < int > . Create ( envName , s => int . Parse ( s ) + 1 ) , 50 ) ;
195
+
196
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 2" ) ;
197
+ Environment . SetEnvironmentVariable ( envName , null ) ;
198
+
199
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
200
+ pipelineResult . GetErrors ( ) . Should ( ) . HaveCount ( 1 ) ;
201
+ var error = pipelineResult . GetErrors ( ) . First ( ) ;
202
+ // TODO: Create test mechanism for CliDiagnostics
203
+ }
204
+
205
+
206
+ [ Fact ]
207
+ public void Values_within_environment_range_do_not_report_error ( )
208
+ {
209
+ var envName = "SYSTEM_COMMANDLINE_LOWERBOUND" ;
210
+ Environment . SetEnvironmentVariable ( envName , "2" ) ;
211
+ var option = GetOptionWithRangeBounds ( RangeBound < int > . Create ( envName , s => int . Parse ( s ) + 1 ) , 50 ) ;
212
+
213
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 11" ) ;
214
+ Environment . SetEnvironmentVariable ( envName , null ) ;
215
+
216
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
217
+ pipelineResult . GetErrors ( ) . Should ( ) . BeEmpty ( ) ;
218
+ }
219
+
220
+ [ Fact ]
221
+ public void Values_above_environment_upper_bound_report_error ( )
222
+ {
223
+ var envName = "SYSTEM_COMMANDLINE_LOWERBOUND" ;
224
+ Environment . SetEnvironmentVariable ( envName , "2" ) ;
225
+ var option = GetOptionWithRangeBounds ( 0 , RangeBound < int > . Create ( envName , s => int . Parse ( s ) + 1 ) ) ;
226
+
227
+ var pipelineResult = ExecutedPipelineResultForRangeOption ( option , "--intOpt 4" ) ;
228
+ Environment . SetEnvironmentVariable ( envName , null ) ;
229
+
230
+ pipelineResult . Should ( ) . NotBeNull ( ) ;
231
+ pipelineResult . GetErrors ( ) . Should ( ) . HaveCount ( 1 ) ;
232
+ var error = pipelineResult . GetErrors ( ) . First ( ) ;
233
+ // TODO: Create test mechanism for CliDiagnostics
234
+ }
73
235
74
236
}
0 commit comments