~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/TypeSystem/TypeParameterTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
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:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
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.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
23
using NUnit.Framework;
 
24
 
 
25
namespace ICSharpCode.NRefactory.TypeSystem
 
26
{
 
27
        [TestFixture]
 
28
        public class TypeParameterTests
 
29
        {
 
30
                [Test]
 
31
                public void TypeParameterDerivingFromOtherTypeParameterDoesNotInheritReferenceConstraint()
 
32
                {
 
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) }
 
38
                                             });
 
39
                        
 
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);
 
45
                }
 
46
                
 
47
                [Test]
 
48
                public void ValueTypeParameterDerivingFromReferenceTypeParameter()
 
49
                {
 
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) }
 
56
                                             });
 
57
                        
 
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);
 
63
                }
 
64
                
 
65
                [Test]
 
66
                public void TypeParameterDerivingFromOtherTypeParameterInheritsEffectiveBaseClass()
 
67
                {
 
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() }
 
72
                                             });
 
73
                        c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U") {
 
74
                                                Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) }
 
75
                                             });
 
76
                        
 
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);
 
82
                }
 
83
                
 
84
                [Test]
 
85
                public void ImportOpenGenericType()
 
86
                {
 
87
                        // class C<T, U> { void M<X>() {} }
 
88
                        
 
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"));
 
94
                        c.Members.Add(m);
 
95
                        
 
96
                        var resolvedC1 = TypeSystemHelper.CreateCompilationAndResolve(c);
 
97
                        var resolvedM1 = resolvedC1.Methods.Single(method => method.Name == "M");
 
98
                        
 
99
                        var resolvedC2 = TypeSystemHelper.CreateCompilationAndResolve(c);
 
100
                        var resolvedM2 = resolvedC2.Methods.Single(method => method.Name == "M");
 
101
                        
 
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]);
 
107
                        
 
108
                        // C<U, X>
 
109
                        var pt1 = new ParameterizedType(resolvedC1, new[] { resolvedC1.TypeParameters[1], resolvedM1.TypeParameters[0] });
 
110
                        var pt2 = (ParameterizedType)resolvedC2.Compilation.Import(pt1);
 
111
                        
 
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]);
 
116
                }
 
117
        }
 
118
}