1
+ using System ;
2
+ using System . Reflection ;
3
+ using NHibernate . Mapping . ByCode ;
4
+ using NUnit . Framework ;
5
+ using SharpTestsEx ;
6
+ namespace NHibernate . Test . MappingByCode . TypeExtensionsTests
7
+ {
8
+ public class GetPropertyOrFieldMatchingNameTest
9
+ {
10
+ private class MyClass
11
+ {
12
+ private int pField ;
13
+ private int PrivateProperty { get ; set ; }
14
+ private int AnotherPrivateProperty { get ; set ; }
15
+ protected int ProtectedProperty { get ; set ; }
16
+ private int Method ( ) { return 0 ; }
17
+ }
18
+
19
+ private class Inherited : MyClass
20
+ {
21
+ private int pField ;
22
+ private int PrivateProperty { get ; set ; }
23
+ }
24
+
25
+ private interface IInterface
26
+ {
27
+ int Something { get ; set ; }
28
+ int SomethingElse { get ; set ; }
29
+ }
30
+
31
+ private class MyClassWithExplicitImpl : IInterface
32
+ {
33
+ int IInterface . Something
34
+ {
35
+ get
36
+ {
37
+ throw new System . NotImplementedException ( ) ;
38
+ }
39
+ set
40
+ {
41
+ throw new System . NotImplementedException ( ) ;
42
+ }
43
+ }
44
+
45
+ public int SomethingElse
46
+ {
47
+ get { throw new NotImplementedException ( ) ; }
48
+ set { throw new NotImplementedException ( ) ; }
49
+ }
50
+ }
51
+
52
+ [ Test ]
53
+ public void WhenNullTypeThenThrows ( )
54
+ {
55
+ Executing . This ( ( ) => ( ( System . Type ) null ) . GetPropertyOrFieldMatchingName ( "A" ) ) . Should ( ) . Throw < ArgumentNullException > ( ) ;
56
+ }
57
+
58
+ [ Test ]
59
+ public void WhenAskNullThenNull ( )
60
+ {
61
+ typeof ( MyClass ) . GetPropertyOrFieldMatchingName ( null ) . Should ( ) . Be . Null ( ) ;
62
+ }
63
+
64
+ [ Test ]
65
+ public void WhenAskNotExistentThenNull ( )
66
+ {
67
+ typeof ( MyClass ) . GetPropertyOrFieldMatchingName ( "NotExistent" ) . Should ( ) . Be . Null ( ) ;
68
+ }
69
+
70
+ [ Test ]
71
+ public void WhenAskPrivateFieldThenFindIt ( )
72
+ {
73
+ var memberInfo = typeof ( MyClass ) . GetPropertyOrFieldMatchingName ( "pField" ) ;
74
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
75
+ memberInfo . Name . Should ( ) . Be ( "pField" ) ;
76
+ memberInfo . Should ( ) . Be . InstanceOf < FieldInfo > ( ) . And . ValueOf . DeclaringType . Should ( ) . Be ( typeof ( MyClass ) ) ;
77
+ }
78
+
79
+ [ Test ]
80
+ public void WhenAskPrivateFieldWithBlanksThenFindIt ( )
81
+ {
82
+ var memberInfo = typeof ( MyClass ) . GetPropertyOrFieldMatchingName ( " pField " ) ;
83
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
84
+ memberInfo . Name . Should ( ) . Be ( "pField" ) ;
85
+ memberInfo . Should ( ) . Be . InstanceOf < FieldInfo > ( ) . And . ValueOf . DeclaringType . Should ( ) . Be ( typeof ( MyClass ) ) ;
86
+ }
87
+
88
+ [ Test ]
89
+ public void WhenAskPrivatePropertyThenFindIt ( )
90
+ {
91
+ var memberInfo = typeof ( MyClass ) . GetPropertyOrFieldMatchingName ( "PrivateProperty" ) ;
92
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
93
+ memberInfo . Name . Should ( ) . Be ( "PrivateProperty" ) ;
94
+ memberInfo . Should ( ) . Be . InstanceOf < PropertyInfo > ( ) . And . ValueOf . DeclaringType . Should ( ) . Be ( typeof ( MyClass ) ) ;
95
+ }
96
+
97
+ [ Test ]
98
+ public void WhenAskProtectedPropertyThenFindIt ( )
99
+ {
100
+ var memberInfo = typeof ( MyClass ) . GetPropertyOrFieldMatchingName ( "ProtectedProperty" ) ;
101
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
102
+ memberInfo . Name . Should ( ) . Be ( "ProtectedProperty" ) ;
103
+ memberInfo . Should ( ) . Be . InstanceOf < PropertyInfo > ( ) . And . ValueOf . DeclaringType . Should ( ) . Be ( typeof ( MyClass ) ) ;
104
+ }
105
+
106
+ [ Test ]
107
+ public void WhenAskMethodThenNull ( )
108
+ {
109
+ typeof ( MyClass ) . GetPropertyOrFieldMatchingName ( "Method" ) . Should ( ) . Be . Null ( ) ;
110
+ }
111
+
112
+ [ Test ]
113
+ public void WhenAskPrivateFieldOnInheritedThenFindItOnInherited ( )
114
+ {
115
+ var memberInfo = typeof ( Inherited ) . GetPropertyOrFieldMatchingName ( "pField" ) ;
116
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
117
+ memberInfo . Name . Should ( ) . Be ( "pField" ) ;
118
+ memberInfo . DeclaringType . Should ( ) . Be ( typeof ( Inherited ) ) ;
119
+ memberInfo . ReflectedType . Should ( ) . Be ( typeof ( Inherited ) ) ;
120
+ memberInfo . Should ( ) . Be . InstanceOf < FieldInfo > ( ) ;
121
+ }
122
+
123
+ [ Test ]
124
+ public void WhenAskPrivatePropertyOnInheritedThenFindItOnInherited ( )
125
+ {
126
+ var memberInfo = typeof ( Inherited ) . GetPropertyOrFieldMatchingName ( "PrivateProperty" ) ;
127
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
128
+ memberInfo . Name . Should ( ) . Be ( "PrivateProperty" ) ;
129
+ memberInfo . DeclaringType . Should ( ) . Be ( typeof ( Inherited ) ) ;
130
+ memberInfo . ReflectedType . Should ( ) . Be ( typeof ( Inherited ) ) ;
131
+ memberInfo . Should ( ) . Be . InstanceOf < PropertyInfo > ( ) ;
132
+ }
133
+
134
+ [ Test ]
135
+ public void WhenAskPrivatePropertyOfBaseOnInheritedThenFindItOnBase ( )
136
+ {
137
+ var memberInfo = typeof ( Inherited ) . GetPropertyOrFieldMatchingName ( "AnotherPrivateProperty" ) ;
138
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
139
+ memberInfo . Name . Should ( ) . Be ( "AnotherPrivateProperty" ) ;
140
+ memberInfo . DeclaringType . Should ( ) . Be ( typeof ( MyClass ) ) ;
141
+ memberInfo . ReflectedType . Should ( ) . Be ( typeof ( MyClass ) ) ;
142
+ memberInfo . Should ( ) . Be . InstanceOf < PropertyInfo > ( ) ;
143
+ }
144
+
145
+ [ Test ]
146
+ public void WhenAskPropertyOfInterfaceThenFindIt ( )
147
+ {
148
+ var memberInfo = typeof ( IInterface ) . GetPropertyOrFieldMatchingName ( "Something" ) ;
149
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
150
+ memberInfo . Name . Should ( ) . Be ( "Something" ) ;
151
+ memberInfo . DeclaringType . Should ( ) . Be ( typeof ( IInterface ) ) ;
152
+ memberInfo . ReflectedType . Should ( ) . Be ( typeof ( IInterface ) ) ;
153
+ memberInfo . Should ( ) . Be . InstanceOf < PropertyInfo > ( ) ;
154
+ }
155
+
156
+ [ Test ]
157
+ public void WhenAskPropertyOfExplicitInterfaceThenFindItOnInterface ( )
158
+ {
159
+ var memberInfo = typeof ( MyClassWithExplicitImpl ) . GetPropertyOrFieldMatchingName ( "Something" ) ;
160
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
161
+ memberInfo . Name . Should ( ) . Be ( "Something" ) ;
162
+ memberInfo . DeclaringType . Should ( ) . Be ( typeof ( IInterface ) ) ;
163
+ memberInfo . ReflectedType . Should ( ) . Be ( typeof ( IInterface ) ) ;
164
+ memberInfo . Should ( ) . Be . InstanceOf < PropertyInfo > ( ) ;
165
+ }
166
+
167
+ [ Test ]
168
+ public void WhenAskPropertyOfImplementedInterfaceThenFindItOnType ( )
169
+ {
170
+ var memberInfo = typeof ( MyClassWithExplicitImpl ) . GetPropertyOrFieldMatchingName ( "SomethingElse" ) ;
171
+ memberInfo . Should ( ) . Not . Be . Null ( ) ;
172
+ memberInfo . Name . Should ( ) . Be ( "SomethingElse" ) ;
173
+ memberInfo . DeclaringType . Should ( ) . Be ( typeof ( MyClassWithExplicitImpl ) ) ;
174
+ memberInfo . ReflectedType . Should ( ) . Be ( typeof ( MyClassWithExplicitImpl ) ) ;
175
+ memberInfo . Should ( ) . Be . InstanceOf < PropertyInfo > ( ) ;
176
+ }
177
+ }
178
+ }
0 commit comments