|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using FluentAssertions; |
| 5 | +using System.CommandLine.Parsing; |
| 6 | +using System.CommandLine.ValueSources; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace System.CommandLine.Subsystems.Tests; |
| 10 | + |
| 11 | +public class ValidationSubsystemTests |
| 12 | +{ |
| 13 | + // Running exactly the same code is important here because missing a step will result in a false positive. Ask me how I know |
| 14 | + private CliOption GetOptionWithSimpleRange<T>(T lowerBound, T upperBound) |
| 15 | + where T : IComparable<T> |
| 16 | + { |
| 17 | + var option = new CliOption<int>("--intOpt"); |
| 18 | + option.SetRange(lowerBound, upperBound); |
| 19 | + return option; |
| 20 | + } |
| 21 | + |
| 22 | + private CliOption GetOptionWithRangeBounds<T>(ValueSource<T> lowerBound, ValueSource<T> upperBound) |
| 23 | + where T : IComparable<T> |
| 24 | + { |
| 25 | + var option = new CliOption<int>("--intOpt"); |
| 26 | + option.SetRange(lowerBound, upperBound); |
| 27 | + return option; |
| 28 | + } |
| 29 | + |
| 30 | + private PipelineResult ExecutedPipelineResultForRangeOption(CliOption option, string input) |
| 31 | + { |
| 32 | + var command = new CliRootCommand { option }; |
| 33 | + return ExecutedPipelineResultForCommand(command, input); |
| 34 | + } |
| 35 | + |
| 36 | + private PipelineResult ExecutedPipelineResultForCommand(CliCommand command, string input) |
| 37 | + { |
| 38 | + var validationSubsystem = ValidationSubsystem.Create(); |
| 39 | + var parseResult = CliParser.Parse(command, input, new CliConfiguration(command)); |
| 40 | + var pipelineResult = new PipelineResult(parseResult, input, Pipeline.CreateEmpty()); |
| 41 | + validationSubsystem.Execute(pipelineResult); |
| 42 | + return pipelineResult; |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void Int_values_in_specified_range_do_not_have_errors() |
| 47 | + { |
| 48 | + var option = GetOptionWithSimpleRange(0, 50); |
| 49 | + |
| 50 | + var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42"); |
| 51 | + |
| 52 | + pipelineResult.Should().NotBeNull(); |
| 53 | + pipelineResult.GetErrors().Should().BeEmpty(); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void Int_values_above_upper_bound_report_error() |
| 58 | + { |
| 59 | + var option = GetOptionWithSimpleRange(0, 5); |
| 60 | + |
| 61 | + var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42"); |
| 62 | + |
| 63 | + pipelineResult.Should().NotBeNull(); |
| 64 | + pipelineResult.GetErrors().Should().HaveCount(1); |
| 65 | + var error = pipelineResult.GetErrors().First(); |
| 66 | + // TODO: Create test mechanism for CliDiagnostics |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void Int_below_lower_bound_report_error() |
| 71 | + { |
| 72 | + var option = GetOptionWithSimpleRange(0, 5); |
| 73 | + |
| 74 | + var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt -42"); |
| 75 | + |
| 76 | + pipelineResult.Should().NotBeNull(); |
| 77 | + pipelineResult.GetErrors().Should().HaveCount(1); |
| 78 | + var error = pipelineResult.GetErrors().First(); |
| 79 | + // TODO: Create test mechanism for CliDiagnostics |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void Int_values_on_lower_range_bound_do_not_report_error() |
| 84 | + { |
| 85 | + var option = GetOptionWithSimpleRange(42, 50); |
| 86 | + |
| 87 | + var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42"); |
| 88 | + |
| 89 | + pipelineResult.Should().NotBeNull(); |
| 90 | + pipelineResult.GetErrors().Should().BeEmpty(); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void Int_values_on_upper_range_bound_do_not_report_error() |
| 95 | + { |
| 96 | + var option = GetOptionWithSimpleRange(0, 42); |
| 97 | + |
| 98 | + var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42"); |
| 99 | + |
| 100 | + pipelineResult.Should().NotBeNull(); |
| 101 | + pipelineResult.GetErrors().Should().BeEmpty(); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void Values_below_calculated_lower_bound_report_error() |
| 106 | + { |
| 107 | + var option = GetOptionWithRangeBounds<int>(ValueSource.Create(() => (true, 1)), 50); |
| 108 | + |
| 109 | + var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 0"); |
| 110 | + |
| 111 | + pipelineResult.Should().NotBeNull(); |
| 112 | + pipelineResult.GetErrors().Should().HaveCount(1); |
| 113 | + var error = pipelineResult.GetErrors().First(); |
| 114 | + // TODO: Create test mechanism for CliDiagnostics |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void Values_within_calculated_range_do_not_report_error() |
| 120 | + { |
| 121 | + var option = GetOptionWithRangeBounds(ValueSource<int>.Create(() => (true, 1)), ValueSource<int>.Create(() => (true, 50))); |
| 122 | + |
| 123 | + var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42"); |
| 124 | + |
| 125 | + pipelineResult.Should().NotBeNull(); |
| 126 | + pipelineResult.GetErrors().Should().BeEmpty(); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void Values_above_calculated_upper_bound_report_error() |
| 131 | + { |
| 132 | + var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(() => (true, 40))); |
| 133 | + |
| 134 | + var pipelineResult = ExecutedPipelineResultForRangeOption(option, "--intOpt 42"); |
| 135 | + |
| 136 | + pipelineResult.Should().NotBeNull(); |
| 137 | + pipelineResult.GetErrors().Should().HaveCount(1); |
| 138 | + var error = pipelineResult.GetErrors().First(); |
| 139 | + // TODO: Create test mechanism for CliDiagnostics |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void Values_below_relative_lower_bound_report_error() |
| 144 | + { |
| 145 | + var otherOption = new CliOption<int>("-a"); |
| 146 | + var option = GetOptionWithRangeBounds(ValueSource.Create(otherOption, o => (true, (int)o + 1)), 50); |
| 147 | + var command = new CliCommand("cmd") { option, otherOption }; |
| 148 | + |
| 149 | + var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 0 -a 0"); |
| 150 | + |
| 151 | + pipelineResult.Should().NotBeNull(); |
| 152 | + pipelineResult.GetErrors().Should().HaveCount(1); |
| 153 | + var error = pipelineResult.GetErrors().First(); |
| 154 | + // TODO: Create test mechanism for CliDiagnostics |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public void Values_within_relative_range_do_not_report_error() |
| 160 | + { |
| 161 | + var otherOption = new CliOption<int>("-a"); |
| 162 | + var option = GetOptionWithRangeBounds(ValueSource<int>.Create(otherOption, o => (true, (int)o + 1)), ValueSource<int>.Create(otherOption, o => (true, (int)o + 10))); |
| 163 | + var command = new CliCommand("cmd") { option, otherOption }; |
| 164 | + |
| 165 | + var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 11 -a 3"); |
| 166 | + |
| 167 | + pipelineResult.Should().NotBeNull(); |
| 168 | + pipelineResult.GetErrors().Should().BeEmpty(); |
| 169 | + } |
| 170 | + |
| 171 | + [Fact] |
| 172 | + public void Values_above_relative_upper_bound_report_error() |
| 173 | + { |
| 174 | + var otherOption = new CliOption<int>("-a"); |
| 175 | + var option = GetOptionWithRangeBounds(0, ValueSource<int>.Create(otherOption, o => (true, (int)o + 10))); |
| 176 | + var command = new CliCommand("cmd") { option, otherOption }; |
| 177 | + |
| 178 | + var pipelineResult = ExecutedPipelineResultForCommand(command, "--intOpt 9 -a -2"); |
| 179 | + |
| 180 | + pipelineResult.Should().NotBeNull(); |
| 181 | + pipelineResult.GetErrors().Should().HaveCount(1); |
| 182 | + var error = pipelineResult.GetErrors().First(); |
| 183 | + // TODO: Create test mechanism for CliDiagnostics |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | +} |
0 commit comments