Skip to content

Commit 3d60c6f

Browse files
Added success to ValueSource.GetValue and some reorg
1 parent 28a89e2 commit 3d60c6f

16 files changed

+191
-141
lines changed

src/System.CommandLine.Subsystems.Tests/ValidationSubsystemTests.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using FluentAssertions;
5-
using Microsoft.VisualBasic.FileIO;
6-
using System.CommandLine.Directives;
75
using System.CommandLine.Parsing;
8-
using System.CommandLine.ValueConditions;
6+
using System.CommandLine.ValueSources;
97
using Xunit;
10-
using static System.CommandLine.Subsystems.Tests.TestData;
118

129
namespace System.CommandLine.Subsystems.Tests;
1310

@@ -107,7 +104,7 @@ public void Int_values_on_upper_range_bound_do_not_report_error()
107104
[Fact]
108105
public void Values_below_calculated_lower_bound_report_error()
109106
{
110-
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(() => 1), 50);
107+
var option = GetOptionWithRangeBounds<int>(ValueSource.Create(() => (true, 1)), 50);
111108

112109
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 0");
113110

@@ -121,7 +118,7 @@ public void Values_below_calculated_lower_bound_report_error()
121118
[Fact]
122119
public void Values_within_calculated_range_do_not_report_error()
123120
{
124-
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(() => 1), ValueSource<int>.Create(() => 50));
121+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(() => (true, 1)), ValueSource<int>.Create(() => (true, 50)));
125122

126123
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42");
127124

@@ -132,7 +129,7 @@ public void Values_within_calculated_range_do_not_report_error()
132129
[Fact]
133130
public void Values_above_calculated_upper_bound_report_error()
134131
{
135-
var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(() => 40));
132+
var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(() => (true, 40)));
136133

137134
var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42");
138135

@@ -146,7 +143,7 @@ public void Values_above_calculated_upper_bound_report_error()
146143
public void Values_below_relative_lower_bound_report_error()
147144
{
148145
var otherOption = new CliOption<int>("-a");
149-
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(otherOption, o => (int)o + 1), 50);
146+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(otherOption, o => (true, (int)o + 1)), 50);
150147
var command = new CliCommand("cmd") { option, otherOption };
151148

152149
var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 0 -a 0");
@@ -162,7 +159,7 @@ public void Values_below_relative_lower_bound_report_error()
162159
public void Values_within_relative_range_do_not_report_error()
163160
{
164161
var otherOption = new CliOption<int>("-a");
165-
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(otherOption, o => (int)o + 1), ValueSource<int>.Create(otherOption, o => (int)o + 10));
162+
var option = GetOptionWithRangeBounds(ValueSource<int>.Create(otherOption, o => (true, (int)o + 1)), ValueSource<int>.Create(otherOption, o => (true, (int)o + 10)));
166163
var command = new CliCommand("cmd") { option, otherOption };
167164

168165
var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 11 -a 3");
@@ -175,7 +172,7 @@ public void Values_within_relative_range_do_not_report_error()
175172
public void Values_above_relative_upper_bound_report_error()
176173
{
177174
var otherOption = new CliOption<int>("-a");
178-
var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(otherOption, o => (int)o + 10));
175+
var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(otherOption, o => (true, (int)o + 10)));
179176
var command = new CliCommand("cmd") { option, otherOption };
180177

181178
var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 9 -a -2");
@@ -185,4 +182,6 @@ public void Values_above_relative_upper_bound_report_error()
185182
var error = pipelineResult.GetErrors().First();
186183
// TODO: Create test mechanism for CliDiagnostics
187184
}
185+
186+
188187
}

src/System.CommandLine.Subsystems.Tests/ValueSourceTests.cs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.VisualBasic.FileIO;
66
using System.CommandLine.Parsing;
77
using System.CommandLine.ValueConditions;
8+
using System.CommandLine.ValueSources;
89
using Xunit;
910

1011
namespace System.CommandLine.Subsystems.Tests;
@@ -27,8 +28,10 @@ public void SimpleValueSource_with_set_value_retrieved()
2728
{
2829
var valueSource = new SimpleValueSource<int>(42);
2930

30-
int value = valueSource.GetTypedValue(EmptyPipelineResult());
31+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult());
3132

33+
success.Should()
34+
.BeTrue();
3235
value.Should()
3336
.Be(42);
3437
}
@@ -38,8 +41,10 @@ public void SimpleValueSource_with_converted_value_retrieved()
3841
{
3942
ValueSource<int> valueSource = 42;
4043

41-
int value = valueSource.GetTypedValue(EmptyPipelineResult());
44+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult());
4245

46+
success.Should()
47+
.BeTrue();
4348
value.Should()
4449
.Be(42);
4550
}
@@ -49,19 +54,23 @@ public void SimpleValueSource_created_via_extension_value_retrieved()
4954
{
5055
var valueSource = ValueSource.Create(42);
5156

52-
int value = valueSource.GetTypedValue(EmptyPipelineResult());
57+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult());
5358

59+
success.Should()
60+
.BeTrue();
5461
value.Should()
5562
.Be(42);
5663
}
5764

5865
[Fact]
5966
public void CalculatedValueSource_produces_value()
6067
{
61-
var valueSource = new CalculatedValueSource<int>(() => 42);
68+
var valueSource = new CalculatedValueSource<int>(() => (true, 42));
6269

63-
int value = valueSource.GetTypedValue(EmptyPipelineResult());
70+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult());
6471

72+
success.Should()
73+
.BeTrue();
6574
value.Should()
6675
.Be(42);
6776
}
@@ -71,20 +80,24 @@ public void CalculatedValueSource_implicitly_converted_produces_value()
7180
{
7281
// TODO: Figure out why this doesn't work, and remove implicit operator if it does not work
7382
// ValueSource<int> valueSource2 = (() => 42);
74-
ValueSource<int> valueSource = (ValueSource<int>)(() => 42);
83+
ValueSource<int> valueSource = (ValueSource<int>)(() => (true, 42)); ;
7584

76-
int value = valueSource.GetTypedValue(EmptyPipelineResult());
85+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult());
7786

87+
success.Should()
88+
.BeTrue();
7889
value.Should()
7990
.Be(42);
8091
}
8192

8293
[Fact]
8394
public void CalculatedValueSource_from_extension_produces_value()
8495
{
85-
var valueSource = ValueSource.Create(() => 42);
86-
int value = valueSource.GetTypedValue(EmptyPipelineResult());
96+
var valueSource = ValueSource.Create(() => (true, 42));
97+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult());
8798

99+
success.Should()
100+
.BeTrue();
88101
value.Should()
89102
.Be(42);
90103
}
@@ -95,8 +108,10 @@ public void RelativeToSymbolValueSource_produces_value_that_was_set()
95108
var option = new CliOption<int>("-a");
96109
var valueSource = new RelativeToSymbolValueSource<int>(option);
97110

98-
int value = valueSource.GetTypedValue(EmptyPipelineResult("-a 42", option));
111+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult("-a 42", option));
99112

113+
success.Should()
114+
.BeTrue();
100115
value.Should()
101116
.Be(42);
102117
}
@@ -107,8 +122,10 @@ public void RelativeToSymbolValueSource_implicitly_converted_produces_value_that
107122
var option = new CliOption<int>("-a");
108123
ValueSource<int> valueSource = option;
109124

110-
int value = valueSource.GetTypedValue(EmptyPipelineResult("-a 42", option));
125+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult("-a 42", option));
111126

127+
success.Should()
128+
.BeTrue();
112129
value.Should()
113130
.Be(42);
114131
}
@@ -119,8 +136,10 @@ public void RelativeToSymbolValueSource_from_extension_produces_value_that_was_s
119136
var option = new CliOption<int>("-a");
120137
var valueSource = new RelativeToSymbolValueSource<int>(option);
121138

122-
int value = valueSource.GetTypedValue(EmptyPipelineResult("-a 42", option));
139+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult("-a 42", option));
123140

141+
success.Should()
142+
.BeTrue();
124143
value.Should()
125144
.Be(42);
126145
}
@@ -132,9 +151,11 @@ public void RelativeToEnvironmentVariableValueSource_produces_value_that_was_set
132151
var valueSource = new RelativeToEnvironmentVariableValueSource<int>(envName);
133152

134153
Environment.SetEnvironmentVariable(envName, "42");
135-
int value = valueSource.GetTypedValue(EmptyPipelineResult(""));
136-
Environment.SetEnvironmentVariable(envName, null);
154+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult(""));
155+
Environment.SetEnvironmentVariable(envName, null);
137156

157+
success.Should()
158+
.BeTrue();
138159
value.Should()
139160
.Be(42);
140161
}
@@ -147,9 +168,11 @@ public void RelativeToEnvironmentVariableValueSource_from_extension_produces_val
147168
var valueSource = ValueSource.CreateFromEnvironmentVariable<int>(envName);
148169

149170
Environment.SetEnvironmentVariable(envName, "42");
150-
int value = valueSource.GetTypedValue(EmptyPipelineResult(""));
171+
(bool success, int value) = valueSource.GetTypedValue(EmptyPipelineResult(""));
151172
Environment.SetEnvironmentVariable(envName, null);
152173

174+
success.Should()
175+
.BeTrue();
153176
value.Should()
154177
.Be(42);
155178
}

src/System.CommandLine.Subsystems/ConsoleHelpers.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Runtime.InteropServices;
5-
64
namespace System.CommandLine
75
{
86
internal static class ConsoleHelpers

src/System.CommandLine.Subsystems/Directives/DiagramSubsystem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.CommandLine.Subsystems;
55
using System.Text;
6-
using System.CommandLine.Parsing;
76

87
namespace System.CommandLine.Directives;
98

src/System.CommandLine.Subsystems/SymbolAnnotationExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.CommandLine.Subsystems;
5-
using System.CommandLine.Subsystems.Annotations;
6-
74
namespace System.CommandLine;
85

96
/// <summary>

src/System.CommandLine.Subsystems/ValidationSubsystem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.CommandLine;
54
using System.CommandLine.Parsing;
65
using System.CommandLine.Subsystems;
76
using System.CommandLine.Validation;

src/System.CommandLine.Subsystems/ValueConditionAnnotationExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.CommandLine;
2-
using System.CommandLine.Subsystems.Annotations;
1+
using System.CommandLine.Subsystems.Annotations;
32
using System.CommandLine.ValueConditions;
3+
using System.CommandLine.ValueSources;
44

55
namespace System.CommandLine;
66

src/System.CommandLine.Subsystems/ValueConditions/Range.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.CommandLine.Parsing;
55
using System.CommandLine.Validation;
6+
using System.CommandLine.ValueSources;
67

78
namespace System.CommandLine.ValueConditions;
89

@@ -34,17 +35,17 @@ public void Validate(object? value,
3435
// TODO: Replace the strings we are comparing with a diagnostic ID when we update ParseError
3536
if (LowerBound is not null)
3637
{
37-
var lowerValue = LowerBound.GetTypedValue(validationContext.PipelineResult);
38-
if (comparableValue.CompareTo(lowerValue) < 0)
38+
var lower = LowerBound.GetTypedValue(validationContext.PipelineResult);
39+
if (lower.success && comparableValue.CompareTo(lower.value) < 0)
3940
{
4041
validationContext.PipelineResult.AddError(new ParseError($"The value for '{valueSymbol.Name}' is below the lower bound of {LowerBound}"));
4142
}
4243
}
4344

4445
if (UpperBound is not null)
4546
{
46-
var upperValue = UpperBound.GetTypedValue(validationContext.PipelineResult);
47-
if (comparableValue.CompareTo(upperValue) > 0)
47+
var upper = UpperBound.GetTypedValue(validationContext.PipelineResult);
48+
if (upper.success && comparableValue.CompareTo(upper.value) > 0)
4849
{
4950
validationContext.PipelineResult.AddError(new ParseError($"The value for '{valueSymbol.Name}' is above the upper bound of {UpperBound}"));
5051
}

0 commit comments

Comments
 (0)