1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using Xunit ;
7
+
8
+ namespace Microsoft . Extensions . Logging . Abstractions . Internal
9
+ {
10
+ public class TypeNameHelperTest
11
+ {
12
+ public static TheoryData < Type , string > FullTypeNameData
13
+ {
14
+ get
15
+ {
16
+ return new TheoryData < Type , string >
17
+ {
18
+ // Predefined Types
19
+ { typeof ( int ) , "int" } ,
20
+ { typeof ( List < int > ) , "System.Collections.Generic.List" } ,
21
+ { typeof ( Dictionary < int , string > ) , "System.Collections.Generic.Dictionary" } ,
22
+ { typeof ( Dictionary < int , List < string > > ) , "System.Collections.Generic.Dictionary" } ,
23
+ { typeof ( List < List < string > > ) , "System.Collections.Generic.List" } ,
24
+
25
+ // Classes inside NonGeneric class
26
+ { typeof ( A ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.A" } ,
27
+ { typeof ( B < int > ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.B" } ,
28
+ { typeof ( C < int , string > ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.C" } ,
29
+ { typeof ( C < int , B < string > > ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.C" } ,
30
+ { typeof ( B < B < string > > ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.B" } ,
31
+
32
+ // Classes inside Generic class
33
+ { typeof ( Outer < int > . D ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.Outer.D" } ,
34
+ { typeof ( Outer < int > . E < int > ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.Outer.E" } ,
35
+ { typeof ( Outer < int > . F < int , string > ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.Outer.F" } ,
36
+ { typeof ( Outer < int > . F < int , Outer < int > . E < string > > ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.Outer.F" } ,
37
+ { typeof ( Outer < int > . E < Outer < int > . E < string > > ) , "Microsoft.Extensions.Logging.Abstractions.Internal.TypeNameHelperTest.Outer.E" }
38
+ } ;
39
+ }
40
+ }
41
+
42
+ [ Theory ]
43
+ [ MemberData ( nameof ( FullTypeNameData ) ) ]
44
+ public void Can_PrettyPrint_FullTypeName ( Type type , string expectedTypeName )
45
+ {
46
+ // Arrange & Act
47
+ var displayName = TypeNameHelper . GetTypeDisplayName ( type ) ;
48
+
49
+ // Assert
50
+ Assert . Equal ( expectedTypeName , displayName ) ;
51
+ }
52
+
53
+ public static TheoryData < Type , string > BuiltInTypesData
54
+ {
55
+ get
56
+ {
57
+ return new TheoryData < Type , string >
58
+ {
59
+ // Predefined Types
60
+ { typeof ( bool ) , "bool" } ,
61
+ { typeof ( byte ) , "byte" } ,
62
+ { typeof ( char ) , "char" } ,
63
+ { typeof ( decimal ) , "decimal" } ,
64
+ { typeof ( double ) , "double" } ,
65
+ { typeof ( float ) , "float" } ,
66
+ { typeof ( int ) , "int" } ,
67
+ { typeof ( long ) , "long" } ,
68
+ { typeof ( object ) , "object" } ,
69
+ { typeof ( sbyte ) , "sbyte" } ,
70
+ { typeof ( short ) , "short" } ,
71
+ { typeof ( string ) , "string" } ,
72
+ { typeof ( uint ) , "uint" } ,
73
+ { typeof ( ulong ) , "ulong" } ,
74
+ { typeof ( ushort ) , "ushort" } ,
75
+ } ;
76
+ }
77
+ }
78
+
79
+ [ Theory ]
80
+ [ MemberData ( nameof ( BuiltInTypesData ) ) ]
81
+ public void ReturnsCommonName_ForBuiltinTypes ( Type type , string expectedTypeName )
82
+ {
83
+ // Arrange & Act
84
+ var displayName = TypeNameHelper . GetTypeDisplayName ( type ) ;
85
+
86
+ // Assert
87
+ Assert . Equal ( expectedTypeName , displayName ) ;
88
+ }
89
+
90
+ private class A { }
91
+
92
+ private class B < T > { }
93
+
94
+ private class C < T1 , T2 > { }
95
+
96
+ private class Outer < T >
97
+ {
98
+ public class D { }
99
+
100
+ public class E < T1 > { }
101
+
102
+ public class F < T1 , T2 > { }
103
+ }
104
+ }
105
+ }
0 commit comments