-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathArgumentTests.cs
More file actions
145 lines (110 loc) · 4.27 KB
/
ArgumentTests.cs
File metadata and controls
145 lines (110 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// 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.Linq;
using FluentAssertions.Execution;
using Xunit;
namespace System.CommandLine.Tests;
public class ArgumentTests
{
[Fact]
public void By_default_there_is_no_default_value()
{
var argument = new Argument<string>("arg");
argument.HasDefaultValue.Should().BeFalse();
}
[Fact]
public void When_default_value_factory_is_set_then_HasDefaultValue_is_true()
{
var argument = new Argument<string[]>("arg");
argument.DefaultValueFactory = _ => null;
argument.HasDefaultValue.Should().BeTrue();
}
[Fact]
public void When_there_is_no_default_value_then_GetDefaultValue_throws()
{
var argument = new Argument<string>("the-arg");
argument.Invoking(a => a.GetDefaultValue())
.Should()
.Throw<InvalidOperationException>()
.Which
.Message
.Should()
.Be("Argument \"the-arg\" does not have a default value");
}
[Fact]
public void GetRequiredValue_does_not_throw_when_help_is_requested_and_DefaultValueFactory_is_set()
{
var argument = new Argument<string>("the-arg")
{
DefaultValueFactory = _ => "default"
};
var result = new RootCommand { argument }.Parse("-h");
using var _ = new AssertionScope();
result.Invoking(r => r.GetRequiredValue(argument)).Should().NotThrow();
result.GetRequiredValue(argument).Should().Be("default");
result.Invoking(r => r.GetRequiredValue<string>("the-arg")).Should().NotThrow();
result.GetRequiredValue<string>("the-arg").Should().Be("default");
result.Errors.Should().BeEmpty();
}
[Fact]
public void When_there_is_no_default_value_then_GetDefaultValue_does_not_throw_for_bool()
{
var argument = new Argument<bool>("the-arg");
argument.GetDefaultValue().Should().Be(false);
}
[Fact]
public void When_there_is_no_default_value_then_GetRequiredValue_does_not_throw_for_bool()
{
var argument = new Argument<bool>("the-arg");
var result = new RootCommand { argument }.Parse("");
using var _ = new AssertionScope();
result.Invoking(r => r.GetRequiredValue(argument)).Should().NotThrow();
result.GetRequiredValue(argument).Should().BeFalse();
result.Invoking(r => r.GetRequiredValue<bool>("the-arg")).Should().NotThrow();
result.GetRequiredValue<bool>("the-arg").Should().BeFalse();
result.Errors.Should().BeEmpty();
}
[Fact]
public void Argument_of_enum_can_limit_enum_members_as_valid_values()
{
var argument = new Argument<ConsoleColor>("color");
argument.AcceptOnlyFromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
Command command = new("set-color")
{
argument
};
var result = command.Parse("set-color Fuschia");
result.Errors
.Select(e => e.Message)
.Should()
.BeEquivalentTo(new[] { $"Argument 'Fuschia' not recognized. Must be one of:\n\t'Red'\n\t'Green'" });
}
[Fact]
public void AcceptOnlyFromAmong_with_comparer_is_case_insensitive()
{
var argument = new Argument<string>("name");
argument.AcceptOnlyFromAmong(StringComparer.OrdinalIgnoreCase, "NAME1", "NAME2");
Command command = new("run")
{
argument
};
var result = command.Parse("run name1");
result.Errors.Should().BeEmpty();
}
[Fact]
public void AcceptOnlyFromAmong_with_comparer_rejects_invalid_values()
{
var argument = new Argument<string>("name");
argument.AcceptOnlyFromAmong(StringComparer.OrdinalIgnoreCase, "NAME1", "NAME2");
Command command = new("run")
{
argument
};
var result = command.Parse("run NAME3");
result.Errors
.Select(e => e.Message)
.Should()
.BeEquivalentTo(new[] { $"Argument 'NAME3' not recognized. Must be one of:\n\t'NAME1'\n\t'NAME2'" });
}
}