0
|
1 using System;
|
|
2
|
|
3 using NUnit.Framework;
|
|
4
|
|
5 using BLToolkit.Aspects;
|
|
6 using BLToolkit.Reflection;
|
|
7 using BLToolkit.TypeBuilder;
|
|
8
|
|
9 namespace Aspects
|
|
10 {
|
|
11 [TestFixture]
|
|
12 public class OverloadAspectTest
|
|
13 {
|
|
14 public abstract class TestObject<T>
|
|
15 {
|
|
16 public T Test(T inVal, out T outVal)
|
|
17 {
|
|
18 outVal = inVal;
|
|
19 return inVal;
|
|
20 }
|
|
21
|
|
22 public string Test(DateTime dateVal, string inVal)
|
|
23 {
|
|
24 return inVal.ToUpper();
|
|
25 }
|
|
26
|
|
27 [Overload] abstract public T Test(T inVal);
|
|
28 [Overload] abstract public T Test(T inVal, out T outVal, ref T refVal);
|
|
29 [Overload] abstract public T Test(T inVal, DateTime dateVal);
|
|
30 }
|
|
31
|
|
32 public abstract class TestObject
|
|
33 {
|
|
34 public int IntValue;
|
|
35 public string StrValue;
|
|
36 public Guid GuidValue;
|
|
37
|
|
38 public void Test(int intVal, Guid guidVal)
|
|
39 {
|
|
40 IntValue = intVal;
|
|
41 GuidValue = guidVal;
|
|
42 StrValue = "(default)";
|
|
43 }
|
|
44
|
|
45 public void Test(int intVal, string strVal)
|
|
46 {
|
|
47 IntValue = intVal;
|
|
48 StrValue = strVal;
|
|
49 GuidValue = Guid.Empty;
|
|
50 }
|
|
51
|
|
52 public void OutRefTest(int inVal, out int outVal, ref int refVal)
|
|
53 {
|
|
54 outVal = inVal;
|
|
55 refVal += inVal;
|
|
56 }
|
|
57
|
|
58 public void OutRefStructTest(int? inVal, out int? outVal, ref int? refVal)
|
|
59 {
|
|
60 outVal = inVal;
|
|
61 refVal += inVal;
|
|
62 }
|
|
63
|
|
64 protected static int StaticMethod(int intVal, ref Guid guidVal)
|
|
65 {
|
|
66 return intVal;
|
|
67 }
|
|
68
|
|
69 public T Generic<T>(T inVal, out T outVal)
|
|
70 {
|
|
71 outVal = inVal;
|
|
72 return inVal;
|
|
73 }
|
|
74
|
|
75 // Becomes
|
|
76 // public override void Test(Guid guidVal)
|
|
77 // {
|
|
78 // Test(default(int), guidVal);
|
|
79 // }
|
|
80 //
|
|
81 [Overload] public abstract void Test(Guid guidVal);
|
|
82
|
|
83 // Becomes
|
|
84 // public override void Test(Guid guidVal, int intVal)
|
|
85 // {
|
|
86 // Test(intVal, guidVal);
|
|
87 // }
|
|
88 //
|
|
89 [Overload] public abstract void Test(Guid guidVal, int intVal);
|
|
90
|
|
91 // Becomes
|
|
92 // public override void Test(string strVal)
|
|
93 // {
|
|
94 // Test(default(int), strVal);
|
|
95 // }
|
|
96 //
|
|
97 [Overload]
|
|
98 public abstract void Test(string strVal);
|
|
99
|
|
100 // Overload method name may be altered.
|
|
101 //
|
|
102 [Overload("Test")]
|
|
103 public abstract void GuidTest(Guid guidVal, DateTime dateVal);
|
|
104
|
|
105 // Parameter types of the method to overload.
|
|
106 //
|
|
107 [Overload(typeof(int), typeof(Guid))]
|
|
108 public abstract void Test(DateTime strVal);
|
|
109
|
|
110 // There may be more or less parameters in the overloaded method.
|
|
111 //
|
|
112 [Overload] public abstract void OutRefTest(int inVal, out int outVal, ref int refVal, out string strVal);
|
|
113 [Overload] public abstract void OutRefTest(int inVal);
|
|
114
|
|
115 // Value types and ref value types also works.
|
|
116 //
|
|
117 [Overload] public abstract void OutRefStructTest(int? inVal, out int? outVal, ref int? refVal, out Guid guidVal);
|
|
118 [Overload] public abstract void OutRefStructTest(int? inVal);
|
|
119
|
|
120 // We can overload static methods.
|
|
121 //
|
|
122 [Overload] public abstract int StaticMethod(int intVal);
|
|
123
|
|
124 // We can overload methods declared in a base type.
|
|
125 //
|
|
126 [Overload] public abstract string ToString(int intVal);
|
|
127
|
|
128 // A generic method can be overloaded by an other generic method.
|
|
129 //
|
|
130 [Overload] public abstract T Generic<T>(T inVal);
|
|
131 }
|
|
132
|
|
133 [Test]
|
|
134 public void OverloadTest()
|
|
135 {
|
|
136 TestObject o = TypeAccessor<TestObject>.CreateInstance();
|
|
137
|
|
138 o.Test(12345, "str");
|
|
139
|
|
140 Assert.AreEqual(12345, o.IntValue);
|
|
141 Assert.AreEqual("str", o.StrValue);
|
|
142 Assert.AreEqual(Guid.Empty, o.GuidValue);
|
|
143
|
|
144 o.Test(Guid.NewGuid(), 123);
|
|
145
|
|
146 Assert.AreEqual(123, o.IntValue);
|
|
147 Assert.AreEqual("(default)", o.StrValue);
|
|
148 Assert.AreNotEqual(Guid.Empty, o.GuidValue);
|
|
149
|
|
150 o.Test("foo");
|
|
151
|
|
152 Assert.AreEqual(0, o.IntValue);
|
|
153 Assert.AreEqual("foo", o.StrValue);
|
|
154 Assert.AreEqual(Guid.Empty, o.GuidValue);
|
|
155
|
|
156 o.Test(Guid.NewGuid());
|
|
157
|
|
158 Assert.AreEqual(0, o.IntValue);
|
|
159 Assert.AreEqual("(default)", o.StrValue);
|
|
160 Assert.AreNotEqual(Guid.Empty, o.GuidValue);
|
|
161
|
|
162 Assert.AreEqual(1, o.Generic(1));
|
|
163 }
|
|
164
|
|
165 [Test]
|
|
166 public void AnyNameTest()
|
|
167 {
|
|
168 TestObject o = TypeAccessor<TestObject>.CreateInstance();
|
|
169
|
|
170 o.GuidTest(Guid.NewGuid(), DateTime.Now);
|
|
171
|
|
172 Assert.AreEqual(0, o.IntValue);
|
|
173 Assert.AreEqual("(default)", o.StrValue);
|
|
174 Assert.AreNotEqual(Guid.Empty, o.GuidValue);
|
|
175 }
|
|
176
|
|
177 [Test]
|
|
178 public void ExplicitParameterTypesTest()
|
|
179 {
|
|
180 TestObject o = TypeAccessor<TestObject>.CreateInstance();
|
|
181
|
|
182 o.Test(DateTime.Now);
|
|
183
|
|
184 Assert.AreEqual(0, o.IntValue);
|
|
185 Assert.AreEqual("(default)", o.StrValue);
|
|
186 Assert.AreEqual(Guid.Empty, o.GuidValue);
|
|
187 }
|
|
188
|
|
189 [Test]
|
|
190 public void StaticMethodTest()
|
|
191 {
|
|
192 TestObject o = TypeAccessor<TestObject>.CreateInstance();
|
|
193
|
|
194 int intVal = o.StaticMethod(123);
|
|
195
|
|
196 Assert.AreEqual(123, intVal);
|
|
197 }
|
|
198
|
|
199 [Test]
|
|
200 public void BaseTypeMethodTest()
|
|
201 {
|
|
202 TestObject o = TypeAccessor<TestObject>.CreateInstance();
|
|
203
|
|
204 Assert.AreEqual(o.ToString(), o.ToString(123));
|
|
205 }
|
|
206
|
|
207 [Test]
|
|
208 public void OutRefTest()
|
|
209 {
|
|
210 TestObject o = TypeAccessor<TestObject>.CreateInstance();
|
|
211
|
|
212 const int inVal = 123;
|
|
213 int refVal = 99;
|
|
214 int outVal;
|
|
215 string strVal;
|
|
216 o.OutRefTest(inVal, out outVal, ref refVal, out strVal);
|
|
217
|
|
218 Assert.AreEqual(inVal, outVal);
|
|
219 Assert.AreEqual(222, refVal);
|
|
220 Assert.AreEqual(string.Empty, strVal);
|
|
221
|
|
222 o.OutRefTest(inVal);
|
|
223 }
|
|
224
|
|
225 [Test]
|
|
226 public void OutRefStructTest()
|
|
227 {
|
|
228 TestObject o = TypeAccessor<TestObject>.CreateInstance();
|
|
229
|
|
230 int? inVal = 123;
|
|
231 int? refVal = 99;
|
|
232 int? outVal;
|
|
233 Guid guidVal;
|
|
234 o.OutRefStructTest(inVal, out outVal, ref refVal, out guidVal);
|
|
235
|
|
236 Assert.AreEqual(inVal, outVal);
|
|
237 Assert.AreEqual(222, refVal);
|
|
238 Assert.AreEqual(Guid.Empty, guidVal);
|
|
239
|
|
240 o.OutRefStructTest(inVal);
|
|
241 }
|
|
242
|
|
243 [Test]
|
|
244 public void GenericTypeTest()
|
|
245 {
|
|
246 TestObject<int?> o = TypeAccessor<TestObject<int?>>.CreateInstance();
|
|
247
|
|
248 int? inVal = 123;
|
|
249 int? refVal = 99;
|
|
250 int? outVal;
|
|
251 o.Test(inVal, out outVal, ref refVal);
|
|
252
|
|
253 Assert.AreEqual(inVal, outVal);
|
|
254 Assert.AreEqual(refVal, 99);
|
|
255
|
|
256 Assert.AreEqual(inVal, o.Test(inVal));
|
|
257 Assert.AreEqual(12, o.Test(12, DateTime.Now));
|
|
258
|
|
259 TestObject<string> o2 = TypeAccessor<TestObject<string>>.CreateInstance();
|
|
260
|
|
261 // When T is a string, method Test(DateTime, string) becomes the best match.
|
|
262 //
|
|
263 Assert.AreEqual("STR", o2.Test("str", DateTime.Now));
|
|
264 }
|
|
265
|
|
266 }
|
|
267 } |