PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionCSharp3Tests.cs

https://github.com/EdwardWu99/ILSpy
C# | 474 lines | 393 code | 17 blank | 64 comment | 6 complexity | 05fc1bafd4c0cf7134478d917614efd6 MD5 | raw file
  1//
  2// CodeCompletionCSharp3Tests.cs
  3//
  4// Author:
  5//   Mike Krüger <mkrueger@novell.com>
  6//
  7// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8//
  9// Permission is hereby granted, free of charge, to any person obtaining
 10// a copy of this software and associated documentation files (the
 11// "Software"), to deal in the Software without restriction, including
 12// without limitation the rights to use, copy, modify, merge, publish,
 13// distribute, sublicense, and/or sell copies of the Software, and to
 14// permit persons to whom the Software is furnished to do so, subject to
 15// the following conditions:
 16// 
 17// The above copyright notice and this permission notice shall be
 18// included in all copies or substantial portions of the Software.
 19// 
 20// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 21// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 23// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 24// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 25// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27//
 28
 29using System;
 30using NUnit.Framework;
 31
 32namespace ICSharpCode.NRefactory.CSharp.CodeCompletion
 33{
 34	[TestFixture()]
 35	public class CodeCompletionCSharp3Tests
 36	{
 37		/* Currently fails but works in monodevelop. Seems to be a bug in the unit test somewhere.
 38		[Test()]
 39		public void TestExtensionMethods ()
 40		{
 41			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
 42@"using System;
 43
 44public static class EMClass
 45{
 46	public static int ToInt32Ext (this Program s)
 47	{
 48		return Int32.Parse (s);
 49	}
 50}
 51
 52class Program
 53{
 54	static void Main (string[] args)
 55	{
 56		Program s;
 57		int i = s.$
 58	}
 59}
 60");
 61			Assert.IsNotNull (provider, "provider == null");
 62			Assert.IsNotNull (provider.Find ("ToInt32Ext"), "extension method 'ToInt32Ext' not found.");
 63		}
 64		*/
 65		[Test()]
 66		public void TestVarLocalVariables ()
 67		{
 68			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
 69@"using System;
 70
 71class Test
 72{
 73	public void TestMethod ()
 74	{
 75	}
 76}
 77
 78class Program
 79{
 80	static void Main (string[] args)
 81	{
 82		var t = new Test ();
 83		$t.$
 84	}
 85}
 86");
 87			Assert.IsNotNull (provider, "provider == null");
 88			Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found.");
 89		}
 90		
 91		[Test()]
 92		public void TestVarLoopVariable ()
 93		{
 94			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
 95@"using System;
 96
 97class Test
 98{
 99	public void TestMethod ()
100	{
101	}
102}
103
104class Program
105{
106	static void Main (string[] args)
107	{
108		var t = new Test[] {};
109		foreach (var loopVar in t) {
110			$loopVar.$
111		}
112	}
113}
114");
115			Assert.IsNotNull (provider, "provider == null");
116			Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found.");
117		}
118
119		[Test()]
120		public void TestAnonymousType ()
121		{
122			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
123@"
124class Program
125{
126	static void Main (string[] args)
127	{
128		var t = new { TestInt = 6, TestChar='e', TestString =""Test""};
129		$t.$
130	}
131}
132");
133			Assert.IsNotNull (provider, "provider == null");
134			Assert.IsNotNull (provider.Find ("TestInt"), "property 'TestInt' not found.");
135			Assert.IsNotNull (provider.Find ("TestChar"), "property 'TestChar' not found.");
136			Assert.IsNotNull (provider.Find ("TestString"), "property 'TestString' not found.");
137		}
138		
139		[Test()]
140		public void TestQueryExpression ()
141		{
142			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
143@"
144using System;
145using System.Collections.Generic;
146
147static class Linq
148{
149	public static IEnumerable<T> Select<S, T> (this IEnumerable<S> collection, Func<S, T> func)
150	{
151	}
152}
153
154class Program
155{
156	public void TestMethod ()
157	{
158	}
159	
160	static void Main (string[] args)
161	{
162		Program[] numbers;
163		foreach (var x in from n in numbers select n) {
164			$x.$
165		}
166	}
167}
168");
169			Assert.IsNotNull (provider, "provider == null");
170			Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found.");
171		}
172		
173		[Test()]
174		public void TestLambdaExpressionCase1 ()
175		{
176			CompletionDataList provider = CodeCompletionBugTests.CreateCtrlSpaceProvider (
177@"
178using System;
179class Test
180{	
181	public void Foo ()
182	{
183		$Func<Test,int> x = s => s.$
184	}
185}
186");
187			Assert.IsNotNull (provider, "provider == null");
188			Assert.IsNotNull (provider.Find ("Foo"), "method 'Foo' not found.");
189		}
190		
191		[Test()]
192		public void TestLambdaExpressionCase2 ()
193		{
194			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
195@"
196
197namespace System {
198	public class Array {
199		public Test this[int i] {
200			get {
201			}
202			set {
203			}
204		}
205	}
206}
207
208static class ExtMethods
209{
210	public static T Where<T>(this T[] t, Func<T, bool> pred)
211	{
212		return t;
213	}
214}
215
216class Test
217{
218	public void TestMethod ()
219	{
220		Test[] en = new Test[0];
221		var x = en.Where (t => t != null);
222		$x.$
223	}
224}
225");
226			Assert.IsNotNull (provider, "provider == null");
227			Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found.");
228		}
229		
230		/// <summary>
231		/// Bug 487237 - Broken lambda intellisense 
232		/// </summary>
233		[Test()]
234		public void TestBug487237 ()
235		{
236			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
237@"
238public interface IHelper
239{
240    void DoIt ();
241}
242
243public class Program
244{
245	delegate T MyDelegate <T> (T t);
246    
247	static int Main ()
248    {
249        $MyDelegate<IHelper> e = helper => helper.$
250        return 0;
251    }
252}
253");
254			Assert.IsNotNull (provider, "provider == null");
255			Assert.IsNotNull (provider.Find ("DoIt"), "method 'DoIt' not found.");
256		}
257		
258		/// <summary>
259		/// Bug 491016 - No intellisense for lambdas inside linq query
260		/// </summary>
261		[Test()]
262		public void TestBug491016 ()
263		{
264			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
265@"
266using System;
267using System.Collections.Generic;
268
269namespace System.Collections.Generic {
270	public interface IEnumerable<T>
271	{
272	
273	}
274}
275namespace Foo
276{
277    class Data
278    {
279        public int Value = 5;
280    }
281
282    static class Ex
283    {
284        public static System.Collections.Generic.IEnumerable<TR> Foo<T, TR> (this System.Collections.Generic.IEnumerable<T> t, Func<T, TR> f)
285        {
286            yield return f (t.First ());
287        }
288    }
289
290    public class C
291    {
292        public static void Main ()
293        {
294            System.Collections.Generic.IEnumerable<Data> i = new Data [0];
295            $var prods = from pe in i.Foo (p2 => p2.$
296        }
297    }
298}
299");
300			Assert.IsNotNull (provider, "provider == null");
301			Assert.IsNotNull (provider.Find ("Value"), "field 'Value' not found.");
302		}
303		
304		/// <summary>
305		/// Bug 491017 - No intellisense for static LINQ queries
306		/// </summary>
307		[Test()]
308		public void TestBug491017 ()
309		{
310			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
311@"
312using System.Linq;
313using System.Linq.Expressions;
314
315class Test
316{
317    $object e = from entity in ""olololcolc"" select entity.$
318}
319");
320			Assert.IsNotNull (provider, "provider == null");
321			Assert.IsNotNull (provider.Find ("ToString"), "method 'ToString' not found.");
322			Assert.IsNull (provider.Find ("Length"), "property 'Length' found, but shouldn't (indicates wrong return type).");
323		}
324		
325		[Test()]
326		public void TestDefaultParameterBug ()
327		{
328			CompletionDataList provider = CodeCompletionBugTests.CreateProvider (
329@"
330namespace Foo
331{
332    class Data
333    {
334        public int Value = 5;
335    }
336
337    public class C
338    {
339        public void Foo (bool aBool = false)
340        {
341			Data data;
342            $data.$
343        }
344    }
345}
346");
347			Assert.IsNotNull (provider, "provider == null");
348			Assert.IsNotNull (provider.Find ("Value"), "field 'Value' not found.");
349		}
350		
351		[Test()]
352		public void TestLinqWhere() {
353			CompletionDataList provider = CodeCompletionBugTests.CreateProvider(
354@"
355using System.Collections.Generic;
356using System.Linq;
357class A
358{
359	public static void Method1()
360	{
361    	int[] enumerable =  new int[]{1,2,3};
362		$IEnumerable<int> q = from i in enumerable where i.$
363	}
364}
365
366");
367			Assert.IsNotNull(provider); // <--- here 0 item in the completion list
368			Assert.IsNotNull(provider.Find("ToString"));
369		}
370		
371		[Test()]
372		public void TestLinqSelectContext () 
373		{
374			var provider = CodeCompletionBugTests.CreateProvider(
375@"
376using System.Collections.Generic;
377using System.Linq;
378class A
379{
380	public static void Main (string[] args)
381	{
382		$from a in args select n$
383	}
384}
385
386");
387			Assert.IsNotNull(provider); // <--- here 0 item in the completion list
388			Assert.IsNotNull(provider.Find("new"), "'new' not found");
389			Assert.IsNotNull(provider.Find("args"), "'args' not found");
390			Assert.IsNotNull(provider.Find("a"), "'a' not found");
391		}
392		
393		[Test()]
394		public void TestLinqAnonymousTypeContext () 
395		{
396			var provider = CodeCompletionBugTests.CreateProvider(
397@"
398using System.Collections.Generic;
399using System.Linq;
400class A
401{
402	public static void Main (string[] args)
403	{
404		Test($from a in args select new { t$);
405	}
406}
407
408");
409			Assert.IsNotNull(provider);
410			Assert.IsFalse (provider.AutoSelect );
411		}
412		
413		[Test()]
414		public void TestLinqAnonymousTypeContextCase2 () 
415		{
416			var provider = CodeCompletionBugTests.CreateProvider(
417@"
418using System.Collections.Generic;
419using System.Linq;
420class A
421{
422	public static void Main (string[] args)
423	{
424		$from a in args select new { test = a$
425	}
426}
427
428");
429			Assert.IsNotNull(provider); // <--- here 0 item in the completion list
430			Assert.IsTrue (provider.AutoSelect );
431			Assert.IsNotNull(provider.Find("a"), "'a' not found");
432			Assert.IsNotNull(provider.Find("new"), "'new' not found");
433			Assert.IsNotNull(provider.Find("args"), "'args' not found");
434		}
435		
436		[Test()]
437		public void TestLinqAnonymousTypeContextCase3 () 
438		{
439			var provider = CodeCompletionBugTests.CreateProvider(
440@"
441using System.Collections.Generic;
442using System.Linq;
443class A
444{
445	public static void Main (string[] args)
446	{
447		$from a in args select new { test = a }$
448	}
449}
450
451");
452			Assert.IsTrue(provider == null || provider.Count == 0); // <--- here 0 item in the completion list
453		}
454		
455		[Test()]
456		public void TestLinqExpressionContext () 
457		{
458			var provider = CodeCompletionBugTests.CreateProvider(
459@"
460using System.Collections.Generic;
461using System.Linq;
462class A
463{
464	public static void Main (string[] args)
465	{
466		$from a in args where a !$
467	}
468}
469
470");
471			Assert.IsTrue(provider == null || provider.Count == 0); // <--- here 0 item in the completion list
472		}
473	}
474}