1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
22
using ICSharpCode.NRefactory.TypeSystem.Implementation;
23
using NUnit.Framework;
25
namespace ICSharpCode.NRefactory.TypeSystem
28
public class TypeParameterTests
31
public void TypeParameterDerivingFromOtherTypeParameterDoesNotInheritReferenceConstraint()
33
// class C<T, U> where T : class where U : T
34
var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
35
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T") { HasReferenceTypeConstraint = true });
36
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U") {
37
Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) }
40
ITypeDefinition resolvedC = TypeSystemHelper.CreateCompilationAndResolve(c);
41
// At runtime, we might have T=System.ValueType and U=int, so C# can't inherit the 'class' constraint
42
// from one type parameter to another.
43
Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType);
44
Assert.IsNull(resolvedC.TypeParameters[1].IsReferenceType);
48
public void ValueTypeParameterDerivingFromReferenceTypeParameter()
50
// class C<T, U> where T : class where U : struct, T
51
var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
52
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T") { HasReferenceTypeConstraint = true });
53
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U") {
54
HasValueTypeConstraint = true,
55
Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) }
58
ITypeDefinition resolvedC = TypeSystemHelper.CreateCompilationAndResolve(c);
59
// At runtime, we might have T=System.ValueType and U=int, so C# can't inherit the 'class' constraint
60
// from one type parameter to another.
61
Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType);
62
Assert.AreEqual(false, resolvedC.TypeParameters[1].IsReferenceType);
66
public void TypeParameterDerivingFromOtherTypeParameterInheritsEffectiveBaseClass()
68
// class C<T, U> where T : List<string> where U : T
69
var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
70
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T") {
71
Constraints = { typeof(List<string>).ToTypeReference() }
73
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U") {
74
Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) }
77
ITypeDefinition resolvedC = TypeSystemHelper.CreateCompilationAndResolve(c);
78
Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType);
79
Assert.AreEqual(true, resolvedC.TypeParameters[1].IsReferenceType);
80
Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", resolvedC.TypeParameters[0].EffectiveBaseClass.ReflectionName);
81
Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", resolvedC.TypeParameters[1].EffectiveBaseClass.ReflectionName);
85
public void ImportOpenGenericType()
87
// class C<T, U> { void M<X>() {} }
89
var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
90
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T"));
91
c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U"));
92
var m = new DefaultUnresolvedMethod(c, "M");
93
m.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.Method, 0, "X"));
96
var resolvedC1 = TypeSystemHelper.CreateCompilationAndResolve(c);
97
var resolvedM1 = resolvedC1.Methods.Single(method => method.Name == "M");
99
var resolvedC2 = TypeSystemHelper.CreateCompilationAndResolve(c);
100
var resolvedM2 = resolvedC2.Methods.Single(method => method.Name == "M");
102
// the types, methods and type parameters differ in the two compilations:
103
Assert.AreNotEqual(resolvedC1, resolvedC2);
104
Assert.AreNotEqual(resolvedM1, resolvedM2);
105
Assert.AreNotEqual(resolvedC1.TypeParameters[1], resolvedC2.TypeParameters[1]);
106
Assert.AreNotEqual(resolvedM1.TypeParameters[0], resolvedM2.TypeParameters[0]);
109
var pt1 = new ParameterizedType(resolvedC1, new[] { resolvedC1.TypeParameters[1], resolvedM1.TypeParameters[0] });
110
var pt2 = (ParameterizedType)resolvedC2.Compilation.Import(pt1);
112
// importing resulted in C<U, X> in the new compilation:
113
Assert.AreEqual(resolvedC2, pt2.GetDefinition());
114
Assert.AreEqual(resolvedC2.TypeParameters[1], pt2.TypeArguments[0]);
115
Assert.AreEqual(resolvedM2.TypeParameters[0], pt2.TypeArguments[1]);