-
Notifications
You must be signed in to change notification settings - Fork 129
/
StringArgumentValueTests.cs
377 lines (306 loc) · 17.4 KB
/
StringArgumentValueTests.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Json;
using Serilog.Settings.Configuration.Tests.Support;
using System.Globalization;
using System.Reflection;
namespace Serilog.Settings.Configuration.Tests;
public class StringArgumentValueTests
{
[Fact]
public void StringValuesConvertToDefaultInstancesIfTargetIsInterface()
{
var stringArgumentValue = new StringArgumentValue("Serilog.Formatting.Json.JsonFormatter, Serilog");
var result = stringArgumentValue.ConvertTo(typeof(ITextFormatter), new ResolutionContext());
Assert.IsType<JsonFormatter>(result);
}
[Fact]
public void StringValuesConvertToDefaultInstancesIfTargetIsAbstractClass()
{
var stringArgumentValue = new StringArgumentValue("Serilog.Settings.Configuration.Tests.Support.ConcreteClass, Serilog.Settings.Configuration.Tests");
var result = stringArgumentValue.ConvertTo(typeof(AbstractClass), new ResolutionContext());
Assert.IsType<ConcreteClass>(result);
}
[Theory]
[InlineData("My.NameSpace.Class+InnerClass::Member",
"My.NameSpace.Class+InnerClass", "Member")]
[InlineData(" TrimMe.NameSpace.Class::NeedsTrimming ",
"TrimMe.NameSpace.Class", "NeedsTrimming")]
[InlineData("My.NameSpace.Class::Member",
"My.NameSpace.Class", "Member")]
[InlineData("My.NameSpace.Class::Member, MyAssembly",
"My.NameSpace.Class, MyAssembly", "Member")]
[InlineData("My.NameSpace.Class::Member, MyAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"My.NameSpace.Class, MyAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "Member")]
[InlineData("Just a random string with :: in it",
null, null)]
[InlineData("Its::a::trapWithColonsAppearingTwice",
null, null)]
[InlineData("ThereIsNoMemberHere::",
null, null)]
[InlineData(null,
null, null)]
[InlineData(" ",
null, null)]
// a full-qualified type name should not be considered a static member accessor
[InlineData("My.NameSpace.Class, MyAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
null, null)]
public void TryParseStaticMemberAccessorReturnsExpectedResults(string? input, string? expectedAccessorType, string? expectedPropertyName)
{
var actual = StringArgumentValue.TryParseStaticMemberAccessor(input,
out var actualAccessorType,
out var actualMemberName);
if (expectedAccessorType == null)
{
Assert.False(actual, $"Should not parse {input}");
}
else
{
Assert.True(actual, $"should successfully parse {input}");
Assert.Equal(expectedAccessorType, actualAccessorType);
Assert.Equal(expectedPropertyName, actualMemberName);
}
}
[Theory]
[InlineData("Serilog.Formatting.Json.JsonFormatter", typeof(JsonFormatter))]
[InlineData("Serilog.Formatting.Json.JsonFormatter, Serilog", typeof(JsonFormatter))]
[InlineData("Serilog.ConfigurationLoggerConfigurationExtensions", typeof(ConfigurationLoggerConfigurationExtensions))]
public void FindTypeSupportsSimpleNamesForSerilogTypes(string input, Type targetType)
{
var type = StringArgumentValue.FindType(input);
Assert.Equal(targetType, type);
}
[Theory]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceProperty, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::AbstractProperty, Serilog.Settings.Configuration.Tests", typeof(AnAbstractClass))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceField, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::AbstractField, Serilog.Settings.Configuration.Tests", typeof(AnAbstractClass))]
public void StaticMembersAccessorsCanBeUsedForAbstractTypes(string input, Type targetType)
{
var stringArgumentValue = new StringArgumentValue(input);
var actual = stringArgumentValue.ConvertTo(targetType, new ResolutionContext());
Assert.IsAssignableFrom(targetType, actual);
Assert.Equal(ConcreteImpl.Instance, actual);
}
[Fact]
public void StaticMembersAccessorsCanBeUsedForMethodInfoWhenThereAreNoOverloads()
{
var stringArgumentValue = new StringArgumentValue("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::IntParseMethodNoOverloads, Serilog.Settings.Configuration.Tests");
var actual = stringArgumentValue.ConvertTo(typeof(MethodInfo), new ResolutionContext());
var parser = Assert.IsAssignableFrom<MethodInfo>(actual);
Assert.Equal(100, parser.Invoke(null, ["100"]));
}
[Theory]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::FuncIntParseField, Serilog.Settings.Configuration.Tests", typeof(Func<string, int>))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::NamedIntParseField, Serilog.Settings.Configuration.Tests", typeof(NamedIntParse))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::FuncIntParseProperty, Serilog.Settings.Configuration.Tests", typeof(Func<string, int>))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::NamedIntParseProperty, Serilog.Settings.Configuration.Tests", typeof(NamedIntParse))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::IntParseMethod, Serilog.Settings.Configuration.Tests", typeof(NamedIntParse))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::IntParseMethod, Serilog.Settings.Configuration.Tests", typeof(Func<string, int>))]
public void StaticMembersAccessorsCanBeUsedForDelegateTypes(string input, Type targetType)
{
var stringArgumentValue = new StringArgumentValue(input);
var actual = stringArgumentValue.ConvertTo(targetType, new ResolutionContext());
Assert.IsAssignableFrom(targetType, actual);
var parser = (Delegate?)actual;
Assert.Equal(100, parser?.DynamicInvoke("100"));
}
[Theory]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::ConcreteClassProperty, Serilog.Settings.Configuration.Tests", typeof(AConcreteClass))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::ConcreteClassField, Serilog.Settings.Configuration.Tests", typeof(AConcreteClass))]
public void StaticMembersAccessorsCanBeUsedForConcreteReferenceTypes(string input, Type targetType)
{
var stringArgumentValue = new StringArgumentValue(input);
var actual = stringArgumentValue.ConvertTo(targetType, new ResolutionContext());
Assert.IsAssignableFrom(targetType, actual);
Assert.Equal(ConcreteImplOfConcreteClass.Instance, actual);
}
[Theory]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::IntProperty, Serilog.Settings.Configuration.Tests", typeof(int), 42)]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::StringProperty, Serilog.Settings.Configuration.Tests", typeof(string),
"Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::StringProperty, Serilog.Settings.Configuration.Tests")]
public void StaticMembersAccessorsCanBeUsedForBuiltInTypes(string input, Type targetType, object expected)
{
var stringArgumentValue = new StringArgumentValue(input);
var actual = stringArgumentValue.ConvertTo(targetType, new ResolutionContext());
Assert.Equal(expected, actual);
}
[Theory]
// unknown type
[InlineData("Namespace.ThisIsNotAKnownType::InterfaceProperty, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
// good type name, but wrong namespace
[InlineData("Random.Namespace.ClassWithStaticAccessors::InterfaceProperty, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
// good full type name, but missing or wrong assembly
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceProperty", typeof(IAmAnInterface))]
public void StaticAccessorOnUnknownTypeThrowsTypeLoadException(string input, Type targetType)
{
var stringArgumentValue = new StringArgumentValue($"{input}");
Assert.Throws<TypeLoadException>(() =>
stringArgumentValue.ConvertTo(targetType, new ResolutionContext())
);
}
[Theory]
// unknown member
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::UnknownMember, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
// static property exists but it's private
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::PrivateInterfaceProperty, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
// static field exists but it's private
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::PrivateInterfaceField, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
// public property exists but it's not static
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InstanceInterfaceProperty, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
// public field exists but it's not static
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InstanceInterfaceField, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
public void StaticAccessorWithInvalidMemberThrowsInvalidOperationException(string input, Type targetType)
{
var stringArgumentValue = new StringArgumentValue($"{input}");
var exception = Assert.Throws<InvalidOperationException>(() =>
stringArgumentValue.ConvertTo(targetType, new ResolutionContext())
);
Assert.Contains("Could not find a public static property or field ", exception.Message);
Assert.Contains("on type `Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors, Serilog.Settings.Configuration.Tests`", exception.Message);
}
[Fact]
public void LevelSwitchesCanBeLookedUpByName()
{
var @switch = new LoggingLevelSwitch(LogEventLevel.Verbose);
var switchName = "$theSwitch";
var resolutionContext = new ResolutionContext();
resolutionContext.AddLevelSwitch(switchName, @switch);
var stringArgumentValue = new StringArgumentValue(switchName);
var resolvedSwitch = stringArgumentValue.ConvertTo(typeof(LoggingLevelSwitch), resolutionContext);
Assert.IsType<LoggingLevelSwitch>(resolvedSwitch);
Assert.Same(@switch, resolvedSwitch);
}
[Fact]
public void ReferencingUndeclaredLevelSwitchThrows()
{
var resolutionContext = new ResolutionContext();
resolutionContext.AddLevelSwitch("$anotherSwitch", new LoggingLevelSwitch(LogEventLevel.Verbose));
var stringArgumentValue = new StringArgumentValue("$mySwitch");
var ex = Assert.Throws<InvalidOperationException>(() =>
stringArgumentValue.ConvertTo(typeof(LoggingLevelSwitch), resolutionContext)
);
Assert.Contains("$mySwitch", ex.Message);
Assert.Contains("\"LevelSwitches\":{\"$mySwitch\":", ex.Message);
}
[Theory]
[InlineData("Information")]
[InlineData("information")]
public void StringValuesConvertToEnumByName(string level)
{
var value = new StringArgumentValue(level);
var actual = value.ConvertTo(typeof(LogEventLevel), new());
Assert.Equal(LogEventLevel.Information, actual);
}
[Fact]
public void StringValuesConvertToEnumByValue()
{
var value = new StringArgumentValue("2");
var actual = value.ConvertTo(typeof(LogEventLevel), new());
Assert.Equal(LogEventLevel.Information, actual);
}
[Fact]
public void StringValuesConvertToUnwrappedNullable()
{
var value = new StringArgumentValue("123");
var actual = value.ConvertTo(typeof(int?), new());
Assert.Equal(123, actual);
}
[Fact]
public void StringValuesConvertToNullWhenEmptyNullable()
{
var value = new StringArgumentValue("");
var actual = value.ConvertTo(typeof(int?), new());
Assert.Null(actual);
}
[Fact]
public void StringValuesConvertToUri()
{
var stringArgumentValue = new StringArgumentValue("https://test.local");
var actual = stringArgumentValue.ConvertTo(typeof(Uri), new ResolutionContext());
Assert.Equal(new Uri("https://test.local"), actual as Uri);
}
[Fact]
public void StringValuesConvertToTimespan()
{
var stringArgumentValue = new StringArgumentValue("1.23:45:30.1234567");
var actual = stringArgumentValue.ConvertTo(typeof(TimeSpan), new ResolutionContext());
Assert.Equal(TimeSpan.Parse("1.23:45:30.1234567"), actual);
}
[Fact]
public void StringValuesConvertToTypeFromShortTypeName()
{
var shortTypeName = "System.Version";
var stringArgumentValue = new StringArgumentValue(shortTypeName);
var actual = (Type?)stringArgumentValue.ConvertTo(typeof(Type), new ResolutionContext());
Assert.Equal(typeof(Version), actual);
}
[Fact]
public void StringValuesConvertToTypeFromAssemblyQualifiedName()
{
var assemblyQualifiedName = typeof(Version).AssemblyQualifiedName!;
var stringArgumentValue = new StringArgumentValue(assemblyQualifiedName);
var actual = (Type?)stringArgumentValue.ConvertTo(typeof(Type), new ResolutionContext());
Assert.Equal(typeof(Version), actual);
}
[Theory]
[InlineData(typeof(bool), false, "False")]
[InlineData(typeof(bool), true, "True")]
[InlineData(typeof(sbyte), (sbyte)-1, "-1")]
[InlineData(typeof(byte), (byte)2, "2")]
[InlineData(typeof(short), (short)-3, "-3")]
[InlineData(typeof(ushort), (ushort)4, "4")]
[InlineData(typeof(int), -5, "-5")]
[InlineData(typeof(uint), 6U, "6")]
[InlineData(typeof(long), -7L, "-7")]
[InlineData(typeof(ulong), 8UL, "8")]
[InlineData(typeof(float), -9.1F, "-9.1")]
[InlineData(typeof(double), 10.2D, "10.2")]
public void StringValuesConvertToPrimitives(Type type, object expected, string sectionValue)
{
var value = new StringArgumentValue(sectionValue);
var actual = value.ConvertTo(type, new());
Assert.Equal(expected, actual);
}
[Fact]
public void StringValuesConvertToPrimitivesUsingAlternativeFormatProvider()
{
var value = new StringArgumentValue("1.234,56");
var formatProvider = new NumberFormatInfo
{
NumberDecimalSeparator = ",",
NumberGroupSeparator = ".",
NumberGroupSizes = [3],
};
var actual = value.ConvertTo(typeof(decimal), new(readerOptions: new() { FormatProvider = formatProvider }));
Assert.Equal(1234.56M, actual);
}
[Theory]
[InlineData("")]
[InlineData("Just some string that is hard to misinterpret")]
[InlineData("True")]
[InlineData("10")]
[InlineData("Information")]
[InlineData("1.23:45:30.1234567")]
[InlineData("https://test.local")]
[InlineData("Serilog.Formatting.Json.JsonFormatter")]
[InlineData("Serilog.Formatting.Json.JsonFormatter, Serilog")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::IntProperty, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::StringProperty, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceProperty, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::AbstractProperty, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceField, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::AbstractField, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::FuncIntParseField, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::NamedIntParseField, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::FuncIntParseProperty, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::NamedIntParseProperty, Serilog.Settings.Configuration.Tests")]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::IntParseMethod, Serilog.Settings.Configuration.Tests")]
public void StringValuesConvertToString(string expected)
{
var value = new StringArgumentValue(expected);
var actual = value.ConvertTo(typeof(string), new());
Assert.Equal(expected, actual);
}
}