|
| 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.Collections.Generic; |
| 6 | +using Xunit; |
| 7 | +using Xunit.Abstractions; |
| 8 | + |
| 9 | +namespace System.CommandLine.Tests |
| 10 | +{ |
| 11 | + public class GetValueByNameParserTests : ParserTests |
| 12 | + { |
| 13 | + public GetValueByNameParserTests(ITestOutputHelper output) : base(output) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + protected override T GetValue<T>(ParseResult parseResult, Option<T> option) |
| 18 | + => parseResult.GetValue<T>(option.Name); |
| 19 | + |
| 20 | + protected override T GetValue<T>(ParseResult parseResult, Argument<T> argument) |
| 21 | + => parseResult.GetValue<T>(argument.Name); |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void In_case_of_argument_name_conflict_the_value_which_belongs_to_the_last_parsed_command_is_returned() |
| 25 | + { |
| 26 | + RootCommand command = new() |
| 27 | + { |
| 28 | + new Argument<int>("arg"), |
| 29 | + new Command("inner1") |
| 30 | + { |
| 31 | + new Argument<int>("arg"), |
| 32 | + new Command("inner2") |
| 33 | + { |
| 34 | + new Argument<int>("arg"), |
| 35 | + } |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + ParseResult parseResult = command.Parse("1 inner1 2 inner2 3"); |
| 40 | + |
| 41 | + parseResult.GetValue<int>("arg").Should().Be(3); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void In_case_of_option_name_conflict_the_value_which_belongs_to_the_last_parsed_command_is_returned() |
| 46 | + { |
| 47 | + RootCommand command = new() |
| 48 | + { |
| 49 | + new Option<int>("--integer", "-i"), |
| 50 | + new Command("inner1") |
| 51 | + { |
| 52 | + new Option<int>("--integer", "-i"), |
| 53 | + new Command("inner2") |
| 54 | + { |
| 55 | + new Option<int>("--integer", "-i") |
| 56 | + } |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + ParseResult parseResult = command.Parse("-i 1 inner1 --integer 2 inner2 -i 3"); |
| 61 | + |
| 62 | + parseResult.GetValue<int>("--integer").Should().Be(3); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void When_option_value_is_not_parsed_then_default_value_is_returned() |
| 67 | + { |
| 68 | + RootCommand command = new() |
| 69 | + { |
| 70 | + new Option<int>("--integer", "-i") |
| 71 | + }; |
| 72 | + |
| 73 | + ParseResult parseResult = command.Parse(""); |
| 74 | + |
| 75 | + parseResult.GetValue<int>("--integer").Should().Be(default); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void When_optional_argument_is_not_parsed_then_default_value_is_returned() |
| 80 | + { |
| 81 | + RootCommand command = new() |
| 82 | + { |
| 83 | + new Argument<int>("arg") |
| 84 | + { |
| 85 | + Arity = ArgumentArity.ZeroOrOne |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + ParseResult parseResult = command.Parse(""); |
| 90 | + |
| 91 | + parseResult.GetValue<int>("arg").Should().Be(default); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void When_required_option_value_is_not_parsed_then_an_exception_is_thrown() |
| 96 | + { |
| 97 | + RootCommand command = new() |
| 98 | + { |
| 99 | + new Option<int>("--required") |
| 100 | + { |
| 101 | + IsRequired = true |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + ParseResult parseResult = command.Parse(""); |
| 106 | + |
| 107 | + Action getRequired = () => parseResult.GetValue<int>("--required"); |
| 108 | + |
| 109 | + getRequired |
| 110 | + .Should() |
| 111 | + .Throw<InvalidOperationException>() |
| 112 | + .Where(ex => ex.Message == LocalizationResources.RequiredOptionWasNotProvided("--required")); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void When_required_argument_value_is_not_parsed_then_an_exception_is_thrown() |
| 117 | + { |
| 118 | + RootCommand command = new() |
| 119 | + { |
| 120 | + new Argument<int>("required") |
| 121 | + { |
| 122 | + Arity = ArgumentArity.ExactlyOne |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + ParseResult parseResult = command.Parse(""); |
| 127 | + |
| 128 | + Action getRequired = () => parseResult.GetValue<int>("required"); |
| 129 | + |
| 130 | + getRequired |
| 131 | + .Should() |
| 132 | + .Throw<InvalidOperationException>() |
| 133 | + .Where(ex => ex.Message == LocalizationResources.RequiredArgumentMissing(parseResult.FindResultFor(command.Arguments[0]))); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void When_non_existing_name_is_used_then_exception_is_thrown() |
| 138 | + { |
| 139 | + const string nonExistingName = "nonExisting"; |
| 140 | + Command command = new ("noSymbols"); |
| 141 | + ParseResult parseResult = command.Parse(""); |
| 142 | + |
| 143 | + Action getRequired = () => parseResult.GetValue<int>(nonExistingName); |
| 144 | + |
| 145 | + getRequired |
| 146 | + .Should() |
| 147 | + .Throw<ArgumentException>() |
| 148 | + .Where(ex => ex.Message == $"No symbol result found for \"{nonExistingName}\" for command \"{command.Name}\"."); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public void When_an_option_and_argument_use_same_name_on_the_same_level_of_the_tree_an_exception_is_thrown() |
| 153 | + { |
| 154 | + const string sameName = "same"; |
| 155 | + |
| 156 | + RootCommand command = new() |
| 157 | + { |
| 158 | + new Argument<int>(sameName) |
| 159 | + { |
| 160 | + Arity = ArgumentArity.ZeroOrOne |
| 161 | + }, |
| 162 | + new Option<int>(sameName) |
| 163 | + }; |
| 164 | + |
| 165 | + ParseResult parseResult = command.Parse(""); |
| 166 | + |
| 167 | + Action getConflicted = () => parseResult.GetValue<int>(sameName); |
| 168 | + |
| 169 | + getConflicted |
| 170 | + .Should() |
| 171 | + .Throw<NotSupportedException>() |
| 172 | + .Where(ex => ex.Message == $"More than one symbol uses name \"{sameName}\" for command \"{command.Name}\"."); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public void When_an_option_and_argument_use_same_name_on_different_levels_of_the_tree_the_value_which_belongs_to_parsed_command_is_returned() |
| 177 | + { |
| 178 | + const string sameName = "same"; |
| 179 | + |
| 180 | + Command command = new("outer") |
| 181 | + { |
| 182 | + new Argument<int>(sameName), |
| 183 | + new Command("inner") |
| 184 | + { |
| 185 | + new Option<int>(sameName) |
| 186 | + } |
| 187 | + }; |
| 188 | + |
| 189 | + ParseResult parseResult = command.Parse($"outer 123 inner {sameName} 456"); |
| 190 | + parseResult.GetValue<int>(sameName).Should().Be(456); |
| 191 | + |
| 192 | + parseResult = command.Parse($"outer 123"); |
| 193 | + parseResult.GetValue<int>(sameName).Should().Be(123); |
| 194 | + } |
| 195 | + |
| 196 | + [Fact] |
| 197 | + public void When_an_option_and_argument_use_same_name_on_different_levels_of_the_tree_the_default_value_which_belongs_to_parsed_command_is_returned() |
| 198 | + { |
| 199 | + const string sameName = "same"; |
| 200 | + |
| 201 | + Command command = new("outer") |
| 202 | + { |
| 203 | + new Argument<int>(sameName) |
| 204 | + { |
| 205 | + DefaultValueFactory = (_) => 123 |
| 206 | + }, |
| 207 | + new Command("inner") |
| 208 | + { |
| 209 | + new Option<int>(sameName) |
| 210 | + { |
| 211 | + DefaultValueFactory = (_) => 456 |
| 212 | + } |
| 213 | + } |
| 214 | + }; |
| 215 | + |
| 216 | + ParseResult parseResult = command.Parse($"outer inner 456"); |
| 217 | + parseResult.GetValue<int>(sameName).Should().Be(456); |
| 218 | + |
| 219 | + parseResult = command.Parse($"outer 123"); |
| 220 | + parseResult.GetValue<int>(sameName).Should().Be(123); |
| 221 | + } |
| 222 | + |
| 223 | + [Fact] |
| 224 | + public void T_can_be_casted_to_nullable_of_T() |
| 225 | + { |
| 226 | + RootCommand command = new() |
| 227 | + { |
| 228 | + new Argument<int>("name") |
| 229 | + }; |
| 230 | + |
| 231 | + ParseResult parseResult = command.Parse("123"); |
| 232 | + |
| 233 | + parseResult.GetValue<int?>("name").Should().Be(123); |
| 234 | + } |
| 235 | + |
| 236 | + [Fact] |
| 237 | + public void Array_of_T_can_be_casted_to_ienumerable_of_T() |
| 238 | + { |
| 239 | + RootCommand command = new() |
| 240 | + { |
| 241 | + new Argument<int[]>("name") |
| 242 | + }; |
| 243 | + |
| 244 | + ParseResult parseResult = command.Parse("1 2 3"); |
| 245 | + |
| 246 | + parseResult.GetValue<IEnumerable<int>>("name").Should().BeEquivalentTo(new int[] { 1, 2, 3 }); |
| 247 | + } |
| 248 | + |
| 249 | + [Fact] |
| 250 | + public void When_casting_is_not_allowed_an_exception_is_thrown() |
| 251 | + { |
| 252 | + const string Name = "name"; |
| 253 | + |
| 254 | + RootCommand command = new() |
| 255 | + { |
| 256 | + new Argument<int>(Name) |
| 257 | + }; |
| 258 | + |
| 259 | + ParseResult parseResult = command.Parse("123"); |
| 260 | + |
| 261 | + Assert(() => parseResult.GetValue<double>(Name)); |
| 262 | + Assert(() => parseResult.GetValue<int[]>(Name)); |
| 263 | + Assert(() => parseResult.GetValue<string>(Name)); |
| 264 | + |
| 265 | + static void Assert(Action invalidCast) |
| 266 | + => invalidCast.Should().Throw<InvalidCastException>(); |
| 267 | + } |
| 268 | + |
| 269 | + [Fact] |
| 270 | + public void Parse_errors_have_precedence_over_type_mismatch() |
| 271 | + { |
| 272 | + RootCommand command = new() |
| 273 | + { |
| 274 | + new Option<int>("--required") |
| 275 | + { |
| 276 | + IsRequired = true |
| 277 | + } |
| 278 | + }; |
| 279 | + |
| 280 | + ParseResult parseResult = command.Parse(""); |
| 281 | + |
| 282 | + Action getRequiredWithTypeMismatch = () => parseResult.GetValue<double>("--required"); |
| 283 | + |
| 284 | + getRequiredWithTypeMismatch |
| 285 | + .Should() |
| 286 | + .Throw<InvalidOperationException>() |
| 287 | + .Where(ex => ex.Message == LocalizationResources.RequiredOptionWasNotProvided("--required")); |
| 288 | + } |
| 289 | + } |
| 290 | +} |
0 commit comments