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;
21
using System.Collections.Generic;
22
using ICSharpCode.NRefactory.Semantics;
23
using ICSharpCode.NRefactory.TypeSystem;
24
using NUnit.Framework;
26
namespace ICSharpCode.NRefactory.CSharp.Resolver
29
public class LocalTypeInferenceTests : ResolverTestBase
32
public void TypeInferenceTest()
34
string program = @"class TestClass {
41
var lrr = Resolve<LocalResolveResult>(program);
42
Assert.AreEqual("System.Int32", lrr.Type.FullName);
46
public void TypeInferenceCycleTest()
48
string program = @"class TestClass {
55
var lrr = Resolve<LocalResolveResult>(program);
56
Assert.AreSame(SpecialType.UnknownType, lrr.Type);
60
public void InvalidAnonymousTypeDeclaration()
63
string program = @"using System;
66
var contact = {id = 54321};
69
var lrr = Resolve<LocalResolveResult>(program);
70
Assert.AreEqual(SpecialType.UnknownType, lrr.Type);
74
public void Foreach_InferFromArrayType()
76
string program = @"using System;
78
static void Method(int[] arr) {
79
foreach ($var$ x in arr) {}
81
var rr = Resolve<TypeResolveResult>(program);
82
Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
86
public void Foreach_InferFromDynamic()
88
string program = @"using System;
90
static void Method(dynamic c) {
91
foreach ($var$ x in c) {}
93
var rr = Resolve<TypeResolveResult>(program);
94
Assert.AreEqual(TypeKind.Dynamic, rr.Type.Kind);
98
public void Foreach_InferFromListOfInt()
100
string program = @"using System;
101
using System.Collections.Generic;
103
static void Method(List<int> c) {
104
foreach ($var$ x in c) {}
106
var rr = Resolve<TypeResolveResult>(program);
107
Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
111
public void Foreach_InferFromICollectionOfInt()
113
string program = @"using System;
114
using System.Collections.Generic;
116
static void Method(ICollection<int> c) {
117
foreach ($var$ x in c) {}
119
var rr = Resolve<TypeResolveResult>(program);
120
Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
124
public void Foreach_InferFromCustomCollection_WithoutIEnumerable()
126
string program = @"using System;
127
using System.Collections.Generic;
129
static void Method(CustomCollection c) {
130
foreach ($var$ x in c) {}
133
class CustomCollection {
134
public MyEnumerator GetEnumerator() {}
135
public struct MyEnumerator {
136
public string Current { get { return null; } }
137
public bool MoveNext() { return false; }
141
var rr = Resolve<TypeResolveResult>(program);
142
Assert.AreEqual("System.String", rr.Type.ReflectionName);
146
public void Foreach_InferFromCustomCollection_WithIEnumerableAndPublicGetEnumerator()
148
string program = @"using System;
149
using System.Collections.Generic;
151
static void Method(CustomCollection c) {
152
foreach ($var$ x in c) {}
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; }
163
var rr = Resolve<TypeResolveResult>(program);
164
Assert.AreEqual("System.String", rr.Type.ReflectionName);
168
public void Foreach_InferFromCustomCollection_WithIEnumerableAndInternalGetEnumerator()
170
string program = @"using System;
171
using System.Collections.Generic;
173
static void Method(CustomCollection c) {
174
foreach ($var$ x in c) {}
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; }
185
var rr = Resolve<TypeResolveResult>(program);
186
Assert.AreEqual("System.Int32", rr.Type.ReflectionName);
190
public void Foreach_InferFromCustomCollection_WithIEnumerableAndGetEnumeratorExtensionMethod()
192
string program = @"using System;
193
using System.Collections.Generic;
195
static void Method(CustomCollection c) {
196
foreach ($var$ x in c) {}
199
class CustomCollection : IEnumerable<int> {
200
public struct MyEnumerator {
201
public string Current { get { return null; } }
202
public bool MoveNext() { return false; }
205
static class ExtMethods {
206
public static CustomCollection.MyEnumerator GetEnumerator(this CustomCollection c) {
207
throw new NotImplementedException();
210
var rr = Resolve<TypeResolveResult>(program);
211
Assert.AreEqual("System.Int32", rr.Type.ReflectionName);