|
5 | 5 | using System.CommandLine.Directives;
|
6 | 6 | using System.CommandLine.Parsing;
|
7 | 7 | using Xunit;
|
| 8 | +using static System.CommandLine.Subsystems.Tests.TestData; |
8 | 9 |
|
9 | 10 | namespace System.CommandLine.Subsystems.Tests;
|
10 | 11 |
|
11 | 12 | public class ValueSubsystemTests
|
12 | 13 | {
|
| 14 | + [Fact] |
| 15 | + public void Values_that_are_entered_are_retrieved() |
| 16 | + { |
| 17 | + var option = new CliOption<int>("--intOpt"); |
| 18 | + var rootCommand = new CliRootCommand { option }; |
| 19 | + var configuration = new CliConfiguration(rootCommand); |
| 20 | + var pipeline = Pipeline.Create(); |
| 21 | + var consoleHack = new ConsoleHack().RedirectToBuffer(true); |
| 22 | + var input = "--intOpt 42"; |
| 23 | + |
| 24 | + var parseResult = CliParser.Parse(rootCommand, input, configuration); |
| 25 | + var pipelineResult = new PipelineResult(parseResult, input, pipeline); |
| 26 | + |
| 27 | + pipelineResult.Should().NotBeNull(); |
| 28 | + var optionValueResult = pipelineResult.GetValueResult(option); |
| 29 | + var optionValue = pipelineResult.GetValue<int>(option); |
| 30 | + optionValueResult.Should().NotBeNull(); |
| 31 | + optionValue.Should().Be(42); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void Values_that_are_not_entered_are_type_default_with_no_default_values() |
| 36 | + { |
| 37 | + var stringOption = new CliOption<string>("--stringOption"); |
| 38 | + var intOption = new CliOption<int>("--intOption"); |
| 39 | + var dateOption = new CliOption<DateTime>("--dateOption"); |
| 40 | + var nullableIntOption = new CliOption<int?>("--nullableIntOption"); |
| 41 | + var guidOption = new CliOption<Guid>("--guidOption"); |
| 42 | + var rootCommand = new CliRootCommand { stringOption, intOption, dateOption, nullableIntOption, guidOption }; |
| 43 | + var configuration = new CliConfiguration(rootCommand); |
| 44 | + var pipeline = Pipeline.Create(); |
| 45 | + var input = ""; |
| 46 | + |
| 47 | + var parseResult = CliParser.Parse(rootCommand, input, configuration); |
| 48 | + var pipelineResult = new PipelineResult(parseResult, input, pipeline); |
| 49 | + |
| 50 | + pipelineResult.Should().NotBeNull(); |
| 51 | + var stringOptionValue = pipelineResult.GetValue<string>(stringOption); |
| 52 | + var intOptionValue = pipelineResult.GetValue<int>(intOption); |
| 53 | + var dateOptionValue = pipelineResult.GetValue<DateTime>(dateOption); |
| 54 | + var nullableIntOptionValue = pipelineResult.GetValue<int?>(nullableIntOption); |
| 55 | + var guidOptionValue = pipelineResult.GetValue<Guid>(guidOption); |
| 56 | + stringOptionValue.Should().BeNull(); |
| 57 | + intOptionValue.Should().Be(0); |
| 58 | + dateOptionValue.Should().Be(DateTime.MinValue); |
| 59 | + nullableIntOptionValue.Should().BeNull(); |
| 60 | + guidOptionValue.Should().Be(Guid.Empty); |
| 61 | + } |
| 62 | + |
13 | 63 | // TODO: Add various default value tests
|
14 | 64 |
|
15 | 65 | /* Hold these tests until we determine if ValueSubsystem is replaceable
|
|
0 commit comments