-
Notifications
You must be signed in to change notification settings - Fork 399
Value handling cleanup #2486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KathleenDollard
wants to merge
10
commits into
dotnet:main-powderhouse
Choose a base branch
from
KathleenDollard:m-value-handling-cleanup
base: main-powderhouse
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Value handling cleanup #2486
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
74b9cf9
Implemented CalculatedValue
KathleenDollard ac61918
Updated ValueProvider for correct TryGet handling
KathleenDollard 5ee18e3
Added reentrancy test
KathleenDollard a13bf78
Added `dotnet nuget why` test and fixed
KathleenDollard 7265345
Added support for multiple symbols and Point example
KathleenDollard 8b36b4b
General cleanup
KathleenDollard 81d2ca8
Renaming: All value sources but constants include calculations
KathleenDollard 17be3a2
Better message for CollectionValueSource parameters
KathleenDollard 9edf1cc
Remove CalculateVaue and OtherValueSymbol support
KathleenDollard 7ac56d1
Updates
KathleenDollard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
src/System.CommandLine.Subsystems.Tests/ValueProviderTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using FluentAssertions; | ||
using System.CommandLine.Parsing; | ||
using System.CommandLine.ValueSources; | ||
using Xunit; | ||
|
||
namespace System.CommandLine.Subsystems.Tests; | ||
|
||
public class ValueProviderTests | ||
{ | ||
[Fact] | ||
public void Values_that_are_entered_are_retrieved() | ||
{ | ||
var option = new CliOption<int>("--intOpt"); | ||
var rootCommand = new CliRootCommand { option }; | ||
var configuration = new CliConfiguration(rootCommand); | ||
var pipeline = Pipeline.Create(); | ||
var input = "--intOpt 42"; | ||
|
||
var parseResult = CliParser.Parse(rootCommand, input, configuration); | ||
var pipelineResult = new PipelineResult(parseResult, input, pipeline); | ||
|
||
pipelineResult.Should().NotBeNull(); | ||
var optionValueResult = pipelineResult.GetValueResult(option); | ||
var optionValue = pipelineResult.GetValue<int>(option); | ||
optionValueResult.Should().NotBeNull(); | ||
optionValue.Should().Be(42); | ||
} | ||
|
||
[Fact] | ||
public void Values_that_are_not_entered_are_type_default_with_no_default_values() | ||
{ | ||
var stringOption = new CliOption<string>("--stringOption"); | ||
var intOption = new CliOption<int>("--intOption"); | ||
var dateOption = new CliOption<DateTime>("--dateOption"); | ||
var nullableIntOption = new CliOption<int?>("--nullableIntOption"); | ||
var guidOption = new CliOption<Guid>("--guidOption"); | ||
var rootCommand = new CliRootCommand { stringOption, intOption, dateOption, nullableIntOption, guidOption }; | ||
var configuration = new CliConfiguration(rootCommand); | ||
var pipeline = Pipeline.Create(); | ||
var input = ""; | ||
|
||
var parseResult = CliParser.Parse(rootCommand, input, configuration); | ||
var pipelineResult = new PipelineResult(parseResult, input, pipeline); | ||
|
||
pipelineResult.Should().NotBeNull(); | ||
var stringOptionValue = pipelineResult.GetValue<string>(stringOption); | ||
var intOptionValue = pipelineResult.GetValue<int>(intOption); | ||
var dateOptionValue = pipelineResult.GetValue<DateTime>(dateOption); | ||
var nullableIntOptionValue = pipelineResult.GetValue<int?>(nullableIntOption); | ||
var guidOptionValue = pipelineResult.GetValue<Guid>(guidOption); | ||
stringOptionValue.Should().BeNull(); | ||
intOptionValue.Should().Be(0); | ||
dateOptionValue.Should().Be(DateTime.MinValue); | ||
nullableIntOptionValue.Should().BeNull(); | ||
guidOptionValue.Should().Be(Guid.Empty); | ||
} | ||
|
||
[Fact] | ||
public void Default_value_is_used_when_user_did_not_enter_a_value() | ||
{ | ||
var intOption = new CliOption<int>("--intOption"); | ||
intOption.SetDefault<int>(42); | ||
var rootCommand = new CliRootCommand { intOption }; | ||
var configuration = new CliConfiguration(rootCommand); | ||
var pipeline = Pipeline.Create(); | ||
var input = ""; | ||
|
||
var parseResult = CliParser.Parse(rootCommand, input, configuration); | ||
var pipelineResult = new PipelineResult(parseResult, input, pipeline); | ||
|
||
pipelineResult.Should().NotBeNull(); | ||
var intOptionValue = pipelineResult.GetValue<int>(intOption); | ||
intOptionValue.Should().Be(42); | ||
} | ||
|
||
/* Leaving calculated value tests in place, because they are examples of custom parser problems | ||
[Fact] | ||
public void Can_retrieve_calculated_value_() | ||
{ | ||
// TODO: This is problematic because we probably can't and don't want to add new types | ||
// of ValueSymbols. We need a different way to add these, which means we need to work | ||
// out where they live (probably as annotations on the root command or a special symbol | ||
// type) and how we add them (probably an extension method). | ||
var calcSymbol = new CalculatedValue<int>("calcValue", 42); | ||
var rootCommand = new CliRootCommand { calcSymbol }; | ||
var configuration = new CliConfiguration(rootCommand); | ||
var pipeline = Pipeline.Create(); | ||
var input = ""; | ||
|
||
var parseResult = CliParser.Parse(rootCommand, input, configuration); | ||
var pipelineResult = new PipelineResult(parseResult, input, pipeline); | ||
|
||
pipelineResult.Should().NotBeNull(); | ||
var intOptionValue = pipelineResult.GetValue<int>(calcSymbol); | ||
intOptionValue.Should().Be(42); | ||
} | ||
*/ | ||
|
||
[Fact] | ||
public void Circular_default_value_dependency_throw() | ||
{ | ||
var opt1 = new CliOption<int>("opt1"); | ||
var opt2 = new CliOption<int>("opt2"); | ||
var opt3 = new CliOption<int>("opt3"); | ||
opt1.SetDefault<int>(opt2); | ||
opt2.SetDefault<int>(opt3); | ||
opt3.SetDefault<int>(opt1); | ||
var rootCommand = new CliRootCommand { opt1, opt2, opt3 }; | ||
var configuration = new CliConfiguration(rootCommand); | ||
var pipeline = Pipeline.Create(); | ||
var input = ""; | ||
|
||
var parseResult = CliParser.Parse(rootCommand, input, configuration); | ||
var pipelineResult = new PipelineResult(parseResult, input, pipeline); | ||
|
||
pipelineResult.Should().NotBeNull(); | ||
Assert.Throws<InvalidOperationException>(() => _ = pipelineResult.GetValue<int>(opt1)); | ||
} | ||
|
||
[Fact] | ||
public void Missing_value_throws_on_GetValue() | ||
{ | ||
var opt1 = new CliOption<int>("opt1"); | ||
var opt2 = new CliOption<int>("opt2"); | ||
var opt3 = new CliOption<int>("opt3"); | ||
opt1.SetDefault<int>(opt2); | ||
opt2.SetDefault<int>(opt3); | ||
opt3.SetDefault<int>(opt1); | ||
var rootCommand = new CliRootCommand { opt1, opt2, opt3 }; | ||
var configuration = new CliConfiguration(rootCommand); | ||
var pipeline = Pipeline.Create(); | ||
var input = ""; | ||
|
||
var parseResult = CliParser.Parse(rootCommand, input, configuration); | ||
var pipelineResult = new PipelineResult(parseResult, input, pipeline); | ||
|
||
pipelineResult.Should().NotBeNull(); | ||
Assert.Throws<InvalidOperationException>(() => _ = pipelineResult.GetValue<int>(opt1)); | ||
} | ||
|
||
/* Leaving calculated value tests in place, because they are examples of custom parser problems | ||
// dotnet nuget why has two parameters, where the first is optional. This is historic, and we must support it | ||
// We also believe folks are using the old custom parser to create compound types - the Point tests here | ||
private (CliRootCommand root, CalculatedValue project, CalculatedValue package) CreateDotnetNugetWhyCli() | ||
{ | ||
var whyArg = new CliArgument<string[]>("whyArgs"); | ||
whyArg.Arity = new ArgumentArity(1, 2); | ||
var project = new CalculatedValue<string>("project", ValueSource.Create(whyArg, ExtractProject)); | ||
var package = new CalculatedValue<string>("package", ValueSource.Create(whyArg, ExtractPackage)); | ||
var whyCmd = new CliCommand("why") { whyArg }; | ||
var nugetCmd = new CliCommand("nuget") { whyCmd }; | ||
var dotnetCmd = new CliRootCommand() { nugetCmd }; | ||
return (dotnetCmd, project, package); | ||
|
||
(bool success, string value) ExtractProject(object? input) | ||
{ | ||
if (input is not string[] inputArray) | ||
{ | ||
return (false, string.Empty); | ||
} | ||
return (true, | ||
inputArray.Length == 1 | ||
? "" | ||
: inputArray[0]); | ||
} | ||
|
||
(bool success, string value) ExtractPackage(object? input) | ||
{ | ||
if (input is not string[] inputArray) | ||
{ | ||
return (false, string.Empty); | ||
} | ||
return (true, | ||
inputArray.Length == 1 | ||
? inputArray[0] | ||
: inputArray[1]); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData("myProject.csproj myPackage", "myProject.csproj", "myPackage")] | ||
[InlineData("myPackage", "", "myPackage")] | ||
public void dotnet_nuget_why_can_retrieve_package_without_project(string args, string projectName, string packageName) | ||
{ | ||
var (dotnetCmd, project, package) = CreateDotnetNugetWhyCli(); | ||
var configuration = new CliConfiguration(dotnetCmd); | ||
var pipeline = Pipeline.Create(); | ||
var input = $"nuget why {args}"; | ||
|
||
var parseResult = CliParser.Parse(dotnetCmd, input, configuration); | ||
var pipelineResult = new PipelineResult(parseResult, input, pipeline); | ||
|
||
pipelineResult.Should().NotBeNull(); | ||
pipelineResult.GetValue(project) | ||
.Should() | ||
.Be(projectName); | ||
pipelineResult.GetValue(package) | ||
.Should() | ||
.Be(packageName); | ||
} | ||
|
||
private record struct Point2D(int X, int Y) | ||
{ | ||
public static readonly Point2D Empty = new Point2D(0, 0); | ||
} | ||
|
||
private (CliRootCommand root, CliValueSymbol point) CreatePointCli() | ||
{ | ||
var xOpt = new CliOption<int>("-x"); | ||
var yOpt = new CliOption<int>("-y"); | ||
var point = new CalculatedValue<Point2D>("point", ValueSource.Create(MakePoint, otherSymbols: [xOpt, yOpt])); | ||
var dotnetCmd = new CliRootCommand() { xOpt, yOpt }; | ||
return (dotnetCmd, point); | ||
|
||
(bool success, Point2D point) MakePoint(IEnumerable<object?> input) | ||
{ | ||
var inputInts = input.OfType<int>().ToArray(); | ||
if (inputInts.Length != 2) | ||
{ | ||
return (false, Point2D.Empty); // since false is used, should return null, not empty point | ||
} | ||
return (true, | ||
new Point2D(inputInts[0], inputInts[1])); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData("-x 0 -y 0", "Point2D { X = 0, Y = 0 }")] | ||
[InlineData("-x 1", "")] | ||
[InlineData("-y 1", "")] | ||
[InlineData("-x 1 -y 3", "Point2D { X = 1, Y = 3 }")] | ||
[InlineData("-x -1 -y 2", "Point2D { X = -1, Y = 2 }")] | ||
[InlineData("-x -2 -y -3", "Point2D { X = -2, Y = -3 }")] | ||
public void Can_make_point_from_two_options(string input, string expected) | ||
{ | ||
var (dotnetCmd, point) = CreatePointCli(); | ||
var configuration = new CliConfiguration(dotnetCmd); | ||
var pipeline = Pipeline.Create(); | ||
|
||
var parseResult = CliParser.Parse(dotnetCmd, input, configuration); | ||
var pipelineResult = new PipelineResult(parseResult, input, pipeline); | ||
|
||
pipelineResult.Should().NotBeNull(); | ||
var pointValue = pipelineResult.GetValue(point); | ||
var output = $"{pointValue}"; | ||
output | ||
.Should() | ||
.Be(expected); | ||
} | ||
*/ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.