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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/LocalTypeInferenceTests.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;
 
21
using System.Collections.Generic;
 
22
using ICSharpCode.NRefactory.Semantics;
 
23
using ICSharpCode.NRefactory.TypeSystem;
 
24
using NUnit.Framework;
 
25
 
 
26
namespace ICSharpCode.NRefactory.CSharp.Resolver
 
27
{
 
28
        [TestFixture]
 
29
        public class LocalTypeInferenceTests : ResolverTestBase
 
30
        {
 
31
                [Test]
 
32
                public void TypeInferenceTest()
 
33
                {
 
34
                        string program = @"class TestClass {
 
35
        static void Test() {
 
36
                var a = 3;
 
37
                $a$.ToString();
 
38
        }
 
39
}
 
40
";
 
41
                        var lrr = Resolve<LocalResolveResult>(program);
 
42
                        Assert.AreEqual("System.Int32", lrr.Type.FullName);
 
43
                }
 
44
                
 
45
                [Test]
 
46
                public void TypeInferenceCycleTest()
 
47
                {
 
48
                        string program = @"class TestClass {
 
49
        static void Test() {
 
50
                var a = a;
 
51
                $a$.ToString();
 
52
        }
 
53
}
 
54
";
 
55
                        var lrr = Resolve<LocalResolveResult>(program);
 
56
                        Assert.AreSame(SpecialType.UnknownType, lrr.Type);
 
57
                }
 
58
                
 
59
                [Test]
 
60
                public void InvalidAnonymousTypeDeclaration()
 
61
                {
 
62
                        // see SD-1393
 
63
                        string program = @"using System;
 
64
class TestClass {
 
65
        static void Main() {
 
66
                var contact = {id = 54321};
 
67
                $contact$.ToString();
 
68
        } }";
 
69
                        var lrr = Resolve<LocalResolveResult>(program);
 
70
                        Assert.AreEqual(SpecialType.UnknownType, lrr.Type);
 
71
                }
 
72
                
 
73
                [Test]
 
74
                public void Foreach_InferFromArrayType()
 
75
                {
 
76
                        string program = @"using System;
 
77
class TestClass {
 
78
        static void Method(int[] arr) {
 
79
                foreach ($var$ x in arr) {}
 
80
        } }";
 
81
                        var rr = Resolve<TypeResolveResult>(program);
 
82
                        Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
 
83
                }
 
84
                
 
85
                [Test]
 
86
                public void Foreach_InferFromDynamic()
 
87
                {
 
88
                        string program = @"using System;
 
89
class TestClass {
 
90
        static void Method(dynamic c) {
 
91
                foreach ($var$ x in c) {}
 
92
        } }";
 
93
                        var rr = Resolve<TypeResolveResult>(program);
 
94
                        Assert.AreEqual(TypeKind.Dynamic, rr.Type.Kind);
 
95
                }
 
96
                
 
97
                [Test]
 
98
                public void Foreach_InferFromListOfInt()
 
99
                {
 
100
                        string program = @"using System;
 
101
using System.Collections.Generic;
 
102
class TestClass {
 
103
        static void Method(List<int> c) {
 
104
                foreach ($var$ x in c) {}
 
105
        } }";
 
106
                        var rr = Resolve<TypeResolveResult>(program);
 
107
                        Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
 
108
                }
 
109
                
 
110
                [Test]
 
111
                public void Foreach_InferFromICollectionOfInt()
 
112
                {
 
113
                        string program = @"using System;
 
114
using System.Collections.Generic;
 
115
class TestClass {
 
116
        static void Method(ICollection<int> c) {
 
117
                foreach ($var$ x in c) {}
 
118
        } }";
 
119
                        var rr = Resolve<TypeResolveResult>(program);
 
120
                        Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
 
121
                }
 
122
                
 
123
                [Test]
 
124
                public void Foreach_InferFromCustomCollection_WithoutIEnumerable()
 
125
                {
 
126
                        string program = @"using System;
 
127
using System.Collections.Generic;
 
128
class TestClass {
 
129
        static void Method(CustomCollection c) {
 
130
                foreach ($var$ x in c) {}
 
131
        }
 
132
}
 
133
class CustomCollection {
 
134
        public MyEnumerator GetEnumerator() {}
 
135
        public struct MyEnumerator {
 
136
                public string Current { get { return null; } }
 
137
                public bool MoveNext() { return false; }
 
138
        }
 
139
}
 
140
";
 
141
                        var rr = Resolve<TypeResolveResult>(program);
 
142
                        Assert.AreEqual("System.String", rr.Type.ReflectionName);
 
143
                }
 
144
                
 
145
                [Test]
 
146
                public void Foreach_InferFromCustomCollection_WithIEnumerableAndPublicGetEnumerator()
 
147
                {
 
148
                        string program = @"using System;
 
149
using System.Collections.Generic;
 
150
class TestClass {
 
151
        static void Method(CustomCollection c) {
 
152
                foreach ($var$ x in c) {}
 
153
        }
 
154
}
 
155
class CustomCollection : IEnumerable<int> {
 
156
        public MyEnumerator GetEnumerator() {}
 
157
        public struct MyEnumerator {
 
158
                public string Current { get { return null; } }
 
159
                public bool MoveNext() { return false; }
 
160
        }
 
161
}
 
162
";
 
163
                        var rr = Resolve<TypeResolveResult>(program);
 
164
                        Assert.AreEqual("System.String", rr.Type.ReflectionName);
 
165
                }
 
166
                
 
167
                [Test]
 
168
                public void Foreach_InferFromCustomCollection_WithIEnumerableAndInternalGetEnumerator()
 
169
                {
 
170
                        string program = @"using System;
 
171
using System.Collections.Generic;
 
172
class TestClass {
 
173
        static void Method(CustomCollection c) {
 
174
                foreach ($var$ x in c) {}
 
175
        }
 
176
}
 
177
class CustomCollection : IEnumerable<int> {
 
178
        internal MyEnumerator GetEnumerator() {}
 
179
        public struct MyEnumerator {
 
180
                public string Current { get { return null; } }
 
181
                public bool MoveNext() { return false; }
 
182
        }
 
183
}
 
184
";
 
185
                        var rr = Resolve<TypeResolveResult>(program);
 
186
                        Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
 
187
                }
 
188
                
 
189
                [Test]
 
190
                public void Foreach_InferFromCustomCollection_WithIEnumerableAndGetEnumeratorExtensionMethod()
 
191
                {
 
192
                        string program = @"using System;
 
193
using System.Collections.Generic;
 
194
class TestClass {
 
195
        static void Method(CustomCollection c) {
 
196
                foreach ($var$ x in c) {}
 
197
        }
 
198
}
 
199
class CustomCollection : IEnumerable<int> {
 
200
        public struct MyEnumerator {
 
201
                public string Current { get { return null; } }
 
202
                public bool MoveNext() { return false; }
 
203
        }
 
204
}
 
205
static class ExtMethods {
 
206
        public static CustomCollection.MyEnumerator GetEnumerator(this CustomCollection c) {
 
207
                throw new NotImplementedException();
 
208
        }
 
209
}";
 
210
                        var rr = Resolve<TypeResolveResult>(program);
 
211
                        Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
 
212
                }
 
213
        }
 
214
}