1
- using Serilog . Formatting ;
1
+ using System ;
2
+ using Serilog . Formatting ;
2
3
using Serilog . Formatting . Json ;
4
+
3
5
using Serilog . Settings . Configuration . Tests . Support ;
6
+
4
7
using Xunit ;
5
8
6
9
namespace Serilog . Settings . Configuration . Tests
@@ -20,11 +23,105 @@ public void StringValuesConvertToDefaultInstancesIfTargetIsInterface()
20
23
[ Fact ]
21
24
public void StringValuesConvertToDefaultInstancesIfTargetIsAbstractClass ( )
22
25
{
23
- var stringArgumentValue = new StringArgumentValue ( ( ) => "Serilog.Settings.Configuration.Tests.Support.ConcreteClass, Serilog.Settings.Configuration.Tests" ) ;
26
+ var stringArgumentValue = new StringArgumentValue ( ( ) => "Serilog.Settings.Configuration.Tests.Support.ConcreteClass, Serilog.Settings.Configuration.Tests" ) ;
27
+
28
+ var result = stringArgumentValue . ConvertTo ( typeof ( AbstractClass ) ) ;
29
+
30
+ Assert . IsType < ConcreteClass > ( result ) ;
31
+ }
32
+
33
+ [ Theory ]
34
+ [ InlineData ( "My.NameSpace.Class+InnerClass::Member" ,
35
+ "My.NameSpace.Class+InnerClass" , "Member" ) ]
36
+ [ InlineData ( " TrimMe.NameSpace.Class::NeedsTrimming " ,
37
+ "TrimMe.NameSpace.Class" , "NeedsTrimming" ) ]
38
+ [ InlineData ( "My.NameSpace.Class::Member" ,
39
+ "My.NameSpace.Class" , "Member" ) ]
40
+ [ InlineData ( "My.NameSpace.Class::Member, MyAssembly" ,
41
+ "My.NameSpace.Class, MyAssembly" , "Member" ) ]
42
+ [ InlineData ( "My.NameSpace.Class::Member, MyAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ,
43
+ "My.NameSpace.Class, MyAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" , "Member" ) ]
44
+ [ InlineData ( "Just a random string with :: in it" ,
45
+ null , null ) ]
46
+ [ InlineData ( "Its::a::trapWithColonsAppearingTwice" ,
47
+ null , null ) ]
48
+ [ InlineData ( "ThereIsNoMemberHere::" ,
49
+ null , null ) ]
50
+ [ InlineData ( null ,
51
+ null , null ) ]
52
+ [ InlineData ( " " ,
53
+ null , null ) ]
54
+ // a full-qualified type name should not be considered a static member accessor
55
+ [ InlineData ( "My.NameSpace.Class, MyAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ,
56
+ null , null ) ]
57
+ public void TryParseStaticMemberAccessorReturnsExpectedResults ( string input , string expectedAccessorType , string expectedPropertyName )
58
+ {
59
+ var actual = StringArgumentValue . TryParseStaticMemberAccessor ( input ,
60
+ out var actualAccessorType ,
61
+ out var actualMemberName ) ;
62
+
63
+ if ( expectedAccessorType == null )
64
+ {
65
+ Assert . False ( actual , $ "Should not parse { input } ") ;
66
+ }
67
+ else
68
+ {
69
+ Assert . True ( actual , $ "should successfully parse { input } ") ;
70
+ Assert . Equal ( expectedAccessorType , actualAccessorType ) ;
71
+ Assert . Equal ( expectedPropertyName , actualMemberName ) ;
72
+ }
73
+ }
74
+
75
+ [ Theory ]
76
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceProperty, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
77
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::AbstractProperty, Serilog.Settings.Configuration.Tests" , typeof ( AnAbstractClass ) ) ]
78
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceField, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
79
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::AbstractField, Serilog.Settings.Configuration.Tests" , typeof ( AnAbstractClass ) ) ]
80
+ private void StaticMembersAccessorsCanBeUsedForReferenceTypes ( string input , Type targetType )
81
+ {
82
+ var stringArgumentValue = new StringArgumentValue ( ( ) => $ "{ input } ") ;
83
+
84
+ var actual = stringArgumentValue . ConvertTo ( targetType ) ;
85
+
86
+ Assert . IsAssignableFrom ( targetType , actual ) ;
87
+ Assert . Equal ( ConcreteImpl . Instance , actual ) ;
88
+ }
24
89
25
- var result = stringArgumentValue . ConvertTo ( typeof ( AbstractClass ) ) ;
90
+ [ Theory ]
91
+ // unknown type
92
+ [ InlineData ( "Namespace.ThisIsNotAKnownType::InterfaceProperty, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
93
+ // good type name, but wrong namespace
94
+ [ InlineData ( "Random.Namespace.ClassWithStaticAccessors::InterfaceProperty, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
95
+ // good full type name, but missing or wrong assembly
96
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceProperty" , typeof ( IAmAnInterface ) ) ]
97
+ public void StaticAccessorOnUnknownTypeThrowsTypeLoadException ( string input , Type targetType )
98
+ {
99
+ var stringArgumentValue = new StringArgumentValue ( ( ) => $ "{ input } ") ;
100
+ Assert . Throws < TypeLoadException > ( ( ) =>
101
+ stringArgumentValue . ConvertTo ( targetType )
102
+ ) ;
103
+ }
104
+
105
+ [ Theory ]
106
+ // unknown member
107
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::UnknownMember, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
108
+ // static property exists but it's private
109
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::PrivateInterfaceProperty, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
110
+ // static field exists but it's private
111
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::PrivateInterfaceField, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
112
+ // public property exists but it's not static
113
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InstanceInterfaceProperty, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
114
+ // public field exists but it's not static
115
+ [ InlineData ( "Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InstanceInterfaceField, Serilog.Settings.Configuration.Tests" , typeof ( IAmAnInterface ) ) ]
116
+ public void StaticAccessorWithInvalidMemberThrowsInvalidOperationException ( string input , Type targetType )
117
+ {
118
+ var stringArgumentValue = new StringArgumentValue ( ( ) => $ "{ input } ") ;
119
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) =>
120
+ stringArgumentValue . ConvertTo ( targetType )
121
+ ) ;
26
122
27
- Assert . IsType < ConcreteClass > ( result ) ;
123
+ Assert . Contains ( "Could not find a public static property or field " , exception . Message ) ;
124
+ Assert . Contains ( "on type `Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors, Serilog.Settings.Configuration.Tests`" , exception . Message ) ;
28
125
}
29
- }
30
- }
126
+ }
127
+ }
0 commit comments