/
OptionAttributeTests.cs
301 lines (255 loc) · 10.4 KB
/
OptionAttributeTests.cs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Reflection;
using System.Reflection.Emit;
using McMaster.Extensions.CommandLineUtils.Conventions;
using Xunit;
using Xunit.Abstractions;
namespace McMaster.Extensions.CommandLineUtils.Tests
{
public class OptionAttributeTests : ConventionTestBase
{
public OptionAttributeTests(ITestOutputHelper output) : base(output)
{ }
protected CommandLineApplication<T> Create<T>() where T : class
=> Create<T, OptionAttributeConvention>();
private class AppWithUnknownOptionType
{
[Option]
public OptionAttributeTests? Option { get; }
}
[Fact]
public void ThrowsWhenOptionTypeCannotBeDetermined()
{
var ex = Assert.Throws<InvalidOperationException>(
() => Create<AppWithUnknownOptionType>());
Assert.Equal(
Strings.CannotDetermineOptionType(typeof(AppWithUnknownOptionType).GetProperty("Option")),
ex.Message);
}
private class ShortNameOverride
{
[Option(ShortName = "d1")]
public string? Detail1 { get; }
[Option(ShortName = "d2")]
public string? Detail2 { get; }
}
[Fact]
public void CanOverrideShortNameOnOption()
{
var app = Create<ShortNameOverride>();
var d1 = Assert.Single(app.Options, o => o.ShortName == "d1");
Assert.Equal("d1", d1.ShortName);
Assert.Equal("detail1", d1.LongName);
Assert.Equal("DETAIL1", d1.ValueName);
var d2 = Assert.Single(app.Options, o => o.ShortName == "d2");
Assert.Equal("d2", d2.ShortName);
Assert.Equal("detail2", d2.LongName);
Assert.Equal("DETAIL2", d2.ValueName);
}
private class EmptyShortName
{
[Option(ShortName = "")]
public string? Detail1 { get; }
[Option(ShortName = "")]
public string? Detail2 { get; }
}
[Fact]
public void CanSetShortNameToEmptyString()
{
var app = Create<EmptyShortName>();
Assert.All(app.Options, o => Assert.Empty(o.ShortName));
}
private class AmbiguousShortOptionName
{
[Option]
public int Message { get; }
[Option]
public int Mode { get; }
}
[Fact]
public void ThrowsWhenShortOptionNamesAreAmbiguous()
{
var ex = Assert.Throws<InvalidOperationException>(
() => Create<AmbiguousShortOptionName>());
Assert.Equal(
Strings.OptionNameIsAmbiguous("m",
typeof(AmbiguousShortOptionName).GetProperty("Mode"),
typeof(AmbiguousShortOptionName).GetProperty("Message")),
ex.Message);
}
private class AmbiguousLongOptionName
{
[Option("--no-edit")]
public int ManuallySetToNoEdit { get; }
[Option]
public int NoEdit { get; }
}
[Fact]
public void ThrowsWhenLongOptionNamesAreAmbiguous()
{
var ex = Assert.Throws<InvalidOperationException>(
() => Create<AmbiguousLongOptionName>());
Assert.Equal(
Strings.OptionNameIsAmbiguous("no-edit",
typeof(AmbiguousLongOptionName).GetProperty("NoEdit"),
typeof(AmbiguousLongOptionName).GetProperty("ManuallySetToNoEdit")),
ex.Message);
}
private class BothOptionAndArgument
{
[Option]
[Argument(0)]
public int NotPossible { get; }
}
[Fact]
public void ThrowsWhenOptionAndArgumentAreSpecified()
{
var ex = Assert.Throws<InvalidOperationException>(
() => Create<BothOptionAndArgument>());
Assert.Equal(
Strings.BothOptionAndArgumentAttributesCannotBeSpecified(
typeof(BothOptionAndArgument).GetProperty("NotPossible")),
ex.Message);
}
private class OptionHasDefaultValues
{
[Option("-a1")]
public string Arg1 { get; } = "a";
[Option("-a2")]
public string[] Arg2 { get; } = new[] { "b", "c" };
[Option("-a3")]
public bool? Arg3 { get; }
[Option("-a4")]
public (bool hasValue, string value) Arg4 { get; } = (false, "Yellow");
}
[Fact]
public void KeepsDefaultValues()
{
var app1 = Create<OptionHasDefaultValues>();
app1.Parse("-a1", "z", "-a2", "y");
Assert.Equal("z", app1.Model.Arg1);
Assert.Equal(new[] { "y" }, app1.Model.Arg2);
var app2 = Create<OptionHasDefaultValues>();
app2.Parse("-a1", "z");
Assert.Equal("z", app2.Model.Arg1);
Assert.Equal(new[] { "b", "c" }, app2.Model.Arg2);
var app3 = Create<OptionHasDefaultValues>();
app3.Parse();
Assert.Equal("a", app3.Model.Arg1);
Assert.Equal(new[] { "b", "c" }, app3.Model.Arg2);
Assert.False(app3.Model.Arg3.HasValue, "Should not have value");
Assert.False(app3.Model.Arg4.hasValue, "Should not have value");
Assert.Equal((false, "Yellow"), app3.Model.Arg4);
var app4 = Create<OptionHasDefaultValues>();
app4.Parse("-a3", "-a4");
Assert.True(app4.Model.Arg3.HasValue);
Assert.True(app4.Model.Arg4.hasValue);
Assert.True(app4.Model.Arg3);
Assert.Equal((true, null), app4.Model.Arg4);
}
private class PrivateSetterProgram
{
public int _value;
public static int _staticvalue;
[Option]
public int Number { get; private set; }
[Option]
public int Count { get; }
[Option]
public int Value
{
get => _value;
set => _value = value + 1;
}
[Option("--static-number")]
public static int StaticNumber { get; private set; }
[Option("--static-string")]
public static string? StaticString { get; }
[Option("--static-value")]
public static int StaticValue
{
get => _staticvalue;
set => _staticvalue = value + 1;
}
}
[Fact]
public void BindsToPrivateSetProperties()
{
var parsed = CommandLineParser.ParseArgs<PrivateSetterProgram>("--number", "1");
Assert.Equal(1, parsed.Number);
}
[Fact]
public void BindsToReadOnlyProperties()
{
var parsed = CommandLineParser.ParseArgs<PrivateSetterProgram>("--count", "1");
Assert.Equal(1, parsed.Count);
}
[Fact]
public void BindsToPropertiesWithSetterMethod()
{
var parsed = CommandLineParser.ParseArgs<PrivateSetterProgram>("--value", "1");
Assert.Equal(2, parsed.Value);
}
[Fact]
public void BindsToStaticProperties()
{
CommandLineParser.ParseArgs<PrivateSetterProgram>("--static-number", "1");
Assert.Equal(1, PrivateSetterProgram.StaticNumber);
}
#if !NETCOREAPP3_0
// .NET Core 3.0 made an intentional breaking change
// see https://github.com/dotnet/coreclr/issues/21268
[Fact]
public void BindsToStaticReadOnlyProps()
{
CommandLineParser.ParseArgs<PrivateSetterProgram>("--static-string", "1");
Assert.Equal("1", PrivateSetterProgram.StaticString);
}
#endif
[Fact]
public void BindsToStaticPropertiesWithSetterMethod()
{
CommandLineParser.ParseArgs<PrivateSetterProgram>("--static-value", "1");
Assert.Equal(2, PrivateSetterProgram.StaticValue);
}
[Theory]
[InlineData("Option123", "o", "option123", "OPTION123")]
[InlineData("dWORD", "d", "d-word", "D_WORD")]
[InlineData("MSBuild", "m", "msbuild", "MSBUILD")]
[InlineData("NoEdit", "n", "no-edit", "NO_EDIT")]
[InlineData("SetUpstreamBranch", "s", "set-upstream-branch", "SET_UPSTREAM_BRANCH")]
[InlineData("lowerCaseFirst", "l", "lower-case-first", "LOWER_CASE_FIRST")]
[InlineData("_field", "f", "field", "FIELD")]
[InlineData("__field", "f", "field", "FIELD")]
[InlineData("___field", "f", "field", "FIELD")]
[InlineData("m_field", "m", "m-field", "M_FIELD")]
[InlineData("m_Field", "m", "m-field", "M_FIELD")]
public void ItDeterminesShortAndLongOptionNames(string propName, string shortName, string longName, string valueName)
{
var option = CreateOption(typeof(int), propName);
Assert.Equal(longName, option.LongName);
Assert.Equal(shortName, option.ShortName);
Assert.Equal(valueName, option.ValueName);
}
private CommandOption CreateOption(Type propType, string propName)
{
// Use ref emit to generate a simple type with one property on it
var assembly =
AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.RunAndCollect);
var mb = assembly.DefineDynamicModule("Test");
var tb = mb.DefineType("Program");
var pb = tb.DefineProperty(propName, PropertyAttributes.None, propType, Array.Empty<Type>());
var fb = tb.DefineField($"<{propName}>k__BackingField", propType, FieldAttributes.Private);
var ctor = typeof(OptionAttribute).GetConstructor(Array.Empty<Type>());
var ab = new CustomAttributeBuilder(ctor, Array.Empty<object>());
pb.SetCustomAttribute(ab);
var program = tb.CreateTypeInfo();
var appBuilder = typeof(CommandLineApplication<>).MakeGenericType(program.AsType());
var app = (CommandLineApplication)Activator.CreateInstance(appBuilder, new object[] { false });
app.Conventions.UseOptionAttributes();
return app.Options[0];
}
}
}