|
3 | 3 |
|
4 | 4 | using FluentAssertions; |
5 | 5 | using System.Linq; |
| 6 | +using FluentAssertions.Execution; |
6 | 7 | using Xunit; |
7 | 8 |
|
8 | 9 | namespace System.CommandLine.Tests; |
@@ -41,6 +42,53 @@ public void When_there_is_no_default_value_then_GetDefaultValue_throws() |
41 | 42 | .Be("Argument \"the-arg\" does not have a default value"); |
42 | 43 | } |
43 | 44 |
|
| 45 | + [Fact] |
| 46 | + public void GetRequiredValue_does_not_throw_when_help_is_requested_and_DefaultValueFactory_is_set() |
| 47 | + { |
| 48 | + var argument = new Argument<string>("the-arg") |
| 49 | + { |
| 50 | + DefaultValueFactory = _ => "default" |
| 51 | + }; |
| 52 | + |
| 53 | + var result = new RootCommand { argument }.Parse("-h"); |
| 54 | + |
| 55 | + using var _ = new AssertionScope(); |
| 56 | + |
| 57 | + result.Invoking(r => r.GetRequiredValue(argument)).Should().NotThrow(); |
| 58 | + result.GetRequiredValue(argument).Should().Be("default"); |
| 59 | + |
| 60 | + result.Invoking(r => r.GetRequiredValue<string>("the-arg")).Should().NotThrow(); |
| 61 | + result.GetRequiredValue<string>("the-arg").Should().Be("default"); |
| 62 | + |
| 63 | + result.Errors.Should().BeEmpty(); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void When_there_is_no_default_value_then_GetDefaultValue_does_not_throw_for_bool() |
| 68 | + { |
| 69 | + var argument = new Argument<bool>("the-arg"); |
| 70 | + |
| 71 | + argument.GetDefaultValue().Should().Be(false); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void When_there_is_no_default_value_then_GetRequiredValue_does_not_throw_for_bool() |
| 76 | + { |
| 77 | + var argument = new Argument<bool>("the-arg"); |
| 78 | + |
| 79 | + var result = new RootCommand { argument }.Parse(""); |
| 80 | + |
| 81 | + using var _ = new AssertionScope(); |
| 82 | + |
| 83 | + result.Invoking(r => r.GetRequiredValue(argument)).Should().NotThrow(); |
| 84 | + result.GetRequiredValue(argument).Should().BeFalse(); |
| 85 | + |
| 86 | + result.Invoking(r => r.GetRequiredValue<bool>("the-arg")).Should().NotThrow(); |
| 87 | + result.GetRequiredValue<bool>("the-arg").Should().BeFalse(); |
| 88 | + |
| 89 | + result.Errors.Should().BeEmpty(); |
| 90 | + } |
| 91 | + |
44 | 92 | [Fact] |
45 | 93 | public void Argument_of_enum_can_limit_enum_members_as_valid_values() |
46 | 94 | { |
|
0 commit comments