comparison UnitTests/CS/Reflection/TypeHelperTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
5
6 using NUnit.Framework;
7
8 using BLToolkit.Reflection;
9 using BLToolkit.EditableObjects;
10
11 namespace Reflection
12 {
13 [TestFixture]
14 public class TypeHelperTest
15 {
16 public class Attribute1 : Attribute {}
17 public class Attribute2 : Attribute {}
18 public class Attribute3 : Attribute {}
19 public class Attribute4 : Attribute {}
20
21 public class Attribute5 : Attribute2
22 {
23 public Attribute5(int n)
24 {
25 _n = n;
26 }
27
28 int _n;
29
30 public override string ToString()
31 {
32 return base.ToString() + ":" + _n;
33 }
34 }
35
36 [Attribute1]
37 [Attribute5(2)]
38 public interface IBase1
39 {
40 void Method1();
41 }
42
43 [Attribute5(1)]
44 public interface IBase2
45 {
46 void Method2();
47 }
48
49 [Attribute2]
50 [Attribute5(0)]
51 public class Base : IBase1, IBase2
52 {
53 public void Method1() {}
54 public void Method2() {}
55 }
56
57 [Attribute3]
58 public interface IObject1 : IBase1
59 {
60 }
61
62 [Attribute4]
63 public interface IObject2 : IBase1, IBase2
64 {
65 }
66
67 [Attribute5(2)]
68 public class TestObject : Base, IObject2, IObject1, IBase2
69 {
70 public new void Method2() {}
71 }
72
73 public struct TestStruct<T,V>
74 {
75 public T _t;
76 public V _v;
77 }
78
79 public class TestObject<T> : TestObject
80 {
81 public T Method2(T value)
82 {
83 return value;
84 }
85
86 public V Method2<V>()
87 {
88 return default(V);
89 }
90
91 public V Method3<V>(V value)
92 where V : IConvertible, IFormattable, new()
93 {
94 return value;
95 }
96
97 public V Method3<V>(Nullable<V> value)
98 where V : struct, IFormattable
99 {
100 return value.Value;
101 }
102
103 public V Method3<V, Q>(TestStruct<Nullable<V>, Q> value)
104 where V : struct
105 where Q : IFormattable
106 {
107 return default(V);
108 }
109
110 public V Method4<V, Q>(TestStruct<V, Q> value)
111 {
112 return default(V);
113 }
114 }
115
116 [Test]
117 public void GetAttributes()
118 {
119 object[] attrs = new TypeHelper(typeof(TestObject)).GetAttributes();
120
121 for (int i = 0; i < attrs.Length; i++)
122 {
123 object attr = attrs[i];
124 Console.WriteLine("{0}: {1} {2}", i, attr, attr.GetHashCode());
125 }
126
127 Assert.AreEqual(typeof(Attribute5), attrs[0].GetType());
128 Assert.AreEqual(typeof(Attribute5), attrs[1].GetType());
129 Assert.AreEqual(typeof(Attribute4), attrs[2].GetType());
130 Assert.AreEqual(typeof(Attribute3), attrs[3].GetType());
131
132 Assert.IsTrue(
133 typeof(Attribute2) == attrs[4].GetType() && typeof(Attribute5) == attrs[5].GetType() ||
134 typeof(Attribute2) == attrs[5].GetType() && typeof(Attribute5) == attrs[4].GetType());
135
136 Assert.IsTrue(
137 typeof(Attribute1) == attrs[6].GetType() && typeof(Attribute5) == attrs[7].GetType() ||
138 typeof(Attribute1) == attrs[7].GetType() && typeof(Attribute5) == attrs[6].GetType());
139
140 Assert.AreEqual(typeof(Attribute5), attrs[8].GetType());
141 }
142
143 [Test]
144 public void GetAttributes_ByType()
145 {
146 object[] attrs = new TypeHelper(typeof(TestObject)).GetAttributes(typeof(Attribute2));
147
148 foreach (object attr in attrs)
149 Console.WriteLine("{0} {1}", attr, attr.GetHashCode());
150
151 Assert.AreEqual(typeof(Attribute5), attrs[0].GetType());
152 Assert.AreEqual(typeof(Attribute5), attrs[1].GetType());
153
154 Assert.IsTrue(
155 typeof(Attribute2) == attrs[2].GetType() && typeof(Attribute5) == attrs[3].GetType() ||
156 typeof(Attribute2) == attrs[3].GetType() && typeof(Attribute5) == attrs[2].GetType());
157 }
158
159 [Test]
160 public void UnderlyingTypeTest()
161 {
162 Type type;
163
164 type = TypeHelper.GetUnderlyingType(typeof(int?));
165 Assert.AreEqual(typeof(int), type);
166
167 type = TypeHelper.GetUnderlyingType(typeof(DayOfWeek?));
168 Assert.AreEqual(typeof(int), type);
169
170 type = TypeHelper.GetUnderlyingType(typeof(IComparable<int>));
171 Assert.AreEqual(typeof(IComparable<int>), type);
172
173 }
174
175 [Test]
176 public void GenericsTest()
177 {
178 Type testType = typeof (TestObject<int>);
179 TypeHelper helper = new TypeHelper(testType);
180
181 Assert.IsNotNull(helper.GetMethod(true, "Method2")); // Generic
182 Assert.IsNotNull(helper.GetMethod(false, "Method2")); // Non-generic
183
184 // TestObject<T>.Method2<V>() is a generic method
185 Assert.IsNotNull(helper.GetMethod(true, "Method2", Type.EmptyTypes));
186 // TestObject.Method2() is not
187 Assert.IsNotNull(helper.GetMethod(false, "Method2", Type.EmptyTypes));
188 // TestObject<T>.Method2(T value) is neither!
189 Assert.IsNotNull(helper.GetMethod(false, "Method2", testType.GetGenericArguments()[0]));
190 // typeof(int) is same as testType.GetGenericArguments()[0]
191 Assert.IsNotNull(helper.GetMethod(false, "Method2", typeof(int)));
192 // Get TestObject<T>.Method3<V>() with constraint type hack
193 Assert.IsNotNull(helper.GetMethod(true, "Method3", typeof(int)));
194 // Get TestObject<T>.Method3<V>() with constraint violation will fail.
195 Assert.IsNull (helper.GetMethod(true, "Method3", typeof(object)));
196 // Get TestObject<T>.Method3<V>() with no types will fail
197 Assert.IsNull (helper.GetMethod(true, "Method3", Type.EmptyTypes));
198 // Nullable<> violates IFormattable constraint
199 Assert.IsNull (helper.GetMethod(true, "Method3", typeof(Nullable<>)));
200 // Method4 does not define a costraint
201 Assert.IsNotNull(helper.GetMethod(true, "Method4", typeof(TestStruct<,>)));
202
203 Assert.IsNotNull(helper.GetMethod(true, "Method3", typeof(Nullable<int>)));
204 Assert.IsNotNull(helper.GetMethod(true, "Method3", typeof(TestStruct<Nullable<int>, int>)));
205 Assert.IsNull (helper.GetMethod(true, "Method3", typeof(TestStruct<Nullable<int>, object>)));
206 Assert.IsNull (helper.GetMethod(true, "Method3", typeof(TestStruct<int, int>)));
207
208 Assert.AreEqual (15, helper.GetMethods( ).Length); // 15 total
209 Assert.AreEqual (5, helper.GetMethods(true ).Length); // 5 generic
210 Assert.AreEqual (10, helper.GetMethods(false).Length); // 10 non-generics
211
212 }
213
214 [Test, ExpectedException(typeof(AmbiguousMatchException))]
215 public void GenericsAmbiguousMatchTest()
216 {
217 // There are more then one Method2 in the TestObject<T> class
218 new TypeHelper(typeof (TestObject<int>)).GetMethod("Method2");
219 }
220
221 public class MyArrayList : ArrayList
222 {
223 public new TestObject this[int i]
224 {
225 get { return (TestObject)base[i]; }
226 }
227 }
228
229 [Test]
230 public void GetListItemType()
231 {
232 Assert.AreEqual(typeof(TestObject), TypeHelper.GetListItemType(new EditableArrayList(typeof(TestObject))));
233 Assert.AreEqual(typeof(TestObject), TypeHelper.GetListItemType(new TestObject[0]));
234 Assert.AreEqual(typeof(TestObject), TypeHelper.GetListItemType(new MyArrayList()));
235 Assert.AreEqual(typeof(TestObject), TypeHelper.GetListItemType(new List<TestObject>()));
236 }
237 }
238 }
239