1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.DynamicProxy
.Tests
18 using System
.Collections
.Generic
;
19 using Castle
.DynamicProxy
.Generators
;
20 using NUnit
.Framework
;
23 public class TypeEquivalentTestCase
26 public void SimpleCases()
28 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(typeof(string), typeof(string)));
29 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(typeof(int), typeof(int)));
30 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(typeof(long), typeof(long)));
32 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(typeof(string), typeof(int)));
33 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(typeof(int), typeof(string)));
37 public void GenericTypeParameter()
39 Type
[] genericArgs
= typeof(Nested
<,>).GetGenericArguments();
41 Type T
= genericArgs
[0];
42 Type Z
= genericArgs
[1];
44 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(T
, T
));
45 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(T
, Z
));
46 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(Z
, T
));
47 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(typeof(int), genericArgs
[0]));
51 public void GenericTypesWithGenericParameter()
53 Type
[] genericArgs
= typeof(Nested
<,>).GetGenericArguments();
55 Type T
= genericArgs
[0];
56 Type Z
= genericArgs
[1];
58 Type listOfT
= typeof(List
<>).MakeGenericType(T
);
59 Type listOfZ
= typeof(List
<>).MakeGenericType(Z
);
61 Type listOfString
= typeof(List
<>).MakeGenericType(typeof(String
));
62 Type listOfInt
= typeof(List
<>).MakeGenericType(typeof(int));
64 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(listOfString
, listOfString
));
65 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(listOfInt
, listOfInt
));
66 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(listOfT
, listOfT
));
67 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(listOfZ
, listOfZ
));
69 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(listOfString
, listOfInt
));
70 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(listOfT
, listOfZ
));
71 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(listOfZ
, listOfT
));
75 public void ArrayTypes()
77 Array arrayOfInt1
= Array
.CreateInstance(typeof(int), 1);
78 Array arrayOfInt2
= Array
.CreateInstance(typeof(int), 2);
79 Array arrayOfStr
= Array
.CreateInstance(typeof(string), 2);
81 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(arrayOfInt1
.GetType(), arrayOfInt1
.GetType()));
82 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(arrayOfInt1
.GetType(), arrayOfInt2
.GetType()));
84 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(arrayOfStr
.GetType(), arrayOfInt1
.GetType()));
88 public void GenericArrayTypes()
90 Type
[] genericArgs
= typeof(Nested
<,>).GetGenericArguments();
92 Type T
= genericArgs
[0];
93 Type Z
= genericArgs
[1];
95 Type arrayOfT
= T
.MakeArrayType();
96 Type arrayOfTDiff
= T
.MakeArrayType();
97 Type arrayOfT2
= T
.MakeArrayType(2);
98 Type arrayOfZ
= Z
.MakeArrayType();
100 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(arrayOfT
, arrayOfT
));
101 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(arrayOfT
, arrayOfTDiff
));
102 Assert
.IsTrue(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(arrayOfZ
, arrayOfZ
));
104 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(arrayOfZ
, arrayOfT
));
105 Assert
.IsFalse(InterfaceProxyWithTargetGenerator
.IsTypeEquivalent(arrayOfT
, arrayOfT2
));
108 public class Nested
<T
, Z
>