PageRenderTime 1ms CodeModel.GetById 2ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/ExtensionMethodTests.cs

https://github.com/EdwardWu99/ILSpy
C# | 239 lines | 200 code | 19 blank | 20 comment | 0 complexity | 337799c1e7cc082954acb0837daf012c MD5 | raw file
  1// Copyright (c) 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
 19using System;
 20using System.Linq;
 21using ICSharpCode.NRefactory.Semantics;
 22using ICSharpCode.NRefactory.TypeSystem;
 23using ICSharpCode.NRefactory.TypeSystem.Implementation;
 24using NUnit.Framework;
 25
 26namespace ICSharpCode.NRefactory.CSharp.Resolver
 27{
 28	[TestFixture]
 29	public class ExtensionMethodTests : ResolverTestBase
 30	{
 31		[Test]
 32		public void ExtensionMethodsTest()
 33		{
 34			string program = @"using XN;
 35class TestClass {
 36	static void Test(A a, B b, C c) {
 37		$;
 38	}
 39}
 40class A { }
 41class B {
 42	public void F(int i) { }
 43}
 44class C {
 45	public void F(object obj) { }
 46}
 47namespace XN {
 48	public static class XC {
 49		public static void F(this object obj, int i) { }
 50		public static void F(this object obj, string s) { }
 51	}
 52}
 53";
 54			InvocationResolveResult mrr;
 55			
 56			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$a.F(1)$"));
 57			Assert.AreEqual("XN.XC.F", mrr.Member.FullName);
 58			Assert.AreEqual("System.Int32", mrr.Member.Parameters[1].Type.FullName);
 59			
 60			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$a.F(\"text\")$"));
 61			Assert.AreEqual("XN.XC.F", mrr.Member.FullName);
 62			Assert.AreEqual("System.String", mrr.Member.Parameters[1].Type.FullName);
 63			
 64			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$b.F(1)$"));
 65			Assert.AreEqual("B.F", mrr.Member.FullName);
 66			
 67			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$b.F(\"text\")$"));
 68			Assert.AreEqual("XN.XC.F", mrr.Member.FullName);
 69			Assert.AreEqual("System.String", mrr.Member.Parameters[1].Type.FullName);
 70			
 71			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$c.F(1)$"));
 72			Assert.AreEqual("C.F", mrr.Member.FullName);
 73			
 74			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$c.F(\"text\")$"));
 75			Assert.AreEqual("C.F", mrr.Member.FullName);
 76		}
 77		
 78		[Test]
 79		public void ExtensionMethodsTest2()
 80		{
 81			string program = @"using System; using System.Collections.Generic;
 82class TestClass {
 83	static void Test(string[] args) {
 84		$;
 85	}
 86}
 87public static class XC {
 88	public static int ToInt32(this string s) { return int.Parse(s); }
 89	public static T[] Slice<T>(this T[] source, int index, int count) { throw new NotImplementedException(); }
 90	public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Predicate<T> predicate) { throw new NotImplementedException(); }
 91}
 92";
 93			CSharpInvocationResolveResult mrr;
 94			
 95			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$\"text\".ToInt32()$"));
 96			Assert.AreEqual("XC.ToInt32", mrr.Member.FullName);
 97			
 98			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$args.Slice(1, 2)$"));
 99			Assert.AreEqual("XC.Slice", mrr.Member.FullName);
100			Assert.AreEqual("System.String[]", mrr.Type.ReflectionName);
101			
102			mrr = Resolve<CSharpInvocationResolveResult>(program.Replace("$", "$args.Filter(delegate { return true; })$"));
103			Assert.AreEqual("XC.Filter", mrr.Member.FullName);
104			Assert.AreEqual("System.Collections.Generic.IEnumerable`1[[System.String]]", mrr.Type.ReflectionName);
105		}
106		
107		[Test]
108		public void FirstIsEligibleExtensionMethod()
109		{
110			string program = @"using System; using System.Collections.Generic;
111public static class XC {
112	$public static TSource First<TSource>(this IEnumerable<TSource> source) {}$
113}
114";
115			var mrr = Resolve<MemberResolveResult>(program);
116			var targetType = compilation.FindType(typeof(string[]));
117			IType[] inferredTypes;
118			bool isEligible = CSharpResolver.IsEligibleExtensionMethod(targetType, (IMethod)mrr.Member, true, out inferredTypes);
119			Assert.IsTrue(isEligible);
120			Assert.AreEqual(1, inferredTypes.Length);
121			Assert.AreEqual("System.String", inferredTypes[0].ReflectionName);
122		}
123		
124		
125		[Test]
126		public void InferTypeFromOverwrittenMethodArguments()
127		{
128			string program = @"using System.Collections.Generic;
129		using System.Linq;
130		
131		public class A { }
132		
133		public class B : A { }
134		
135		class Program
136		{
137			static void Main(string[] args)
138			{
139				IEnumerable<B> list = new List<B>();
140				var arr = $list.ToArray<A>()$;
141			}
142		}
143";
144			var rr = Resolve<CSharpInvocationResolveResult>(program);
145			Assert.AreEqual("A[]", rr.Type.ReflectionName);
146			Assert.AreEqual("System.Linq.Enumerable.ToArray", rr.Member.FullName);
147			Assert.AreEqual("A", ((SpecializedMethod)rr.Member).TypeArguments.Single().ReflectionName);
148		}
149		
150		[Test]
151		public void TypeInferenceBasedOnTargetTypeAndArgumentType()
152		{
153			string program = @"using System.Collections.Generic;
154		using System.Linq;
155		
156		public class A { }
157		public class B : A { }
158		
159		static class Program
160		{
161			static void Main(A a, B b)
162			{
163				var x = $b.Choose(a)$;
164			}
165			
166			public static T Choose<T>(this T a, T b) { }
167		}
168";
169			var rr = Resolve<CSharpInvocationResolveResult>(program);
170			Assert.AreEqual("A", rr.Type.ReflectionName);
171		}
172		
173		[Test]
174		public void PartiallySpecializedMethod()
175		{
176			string program = @"using System.Collections.Generic;
177		using System.Linq;
178		
179		public class A { }
180		public class B : A { }
181		
182		static class Program
183		{
184			static void Main(A a, B b)
185			{
186				var x = $b.Choose$(a);
187			}
188			
189			public static T Choose<T>(this T a, T b) { }
190		}
191";
192			var rr = Resolve<MethodGroupResolveResult>(program);
193			Assert.IsFalse(rr.Methods.Any());
194			// We deliberately do not specialize the method unless partial specialization is requested explicitly.
195			// This is because the actual type (when considering the whole invocation, not just the method group)
196			// is actually A.
197			Assert.AreEqual("``0", rr.GetExtensionMethods().Single().Single().ReturnType.ReflectionName);
198			Assert.AreEqual("``0", rr.GetEligibleExtensionMethods(false).Single().Single().ReturnType.ReflectionName);
199			Assert.AreEqual("B", rr.GetEligibleExtensionMethods(true).Single().Single().ReturnType.ReflectionName);
200		}
201		
202		[Test]
203		public void CreateDelegateFromExtensionMethod()
204		{
205			string program = @"using System;
206	static class Program
207	{
208		static void Main() {
209			Func<string> f = $"""".id$;
210		}
211		static string id(this string x) { return x; }
212	}
213";
214			Conversion c = GetConversion(program);
215			Assert.IsTrue(c.IsValid);
216			Assert.IsTrue(c.IsMethodGroupConversion);
217			Assert.AreEqual("Program.id", c.Method.FullName);
218		}
219		
220		[Test]
221		public void InferDelegateTypeFromExtensionMethod()
222		{
223			string program = @"using System;
224	static class Program
225	{
226		static void Main() {
227			$call("""".id)$;
228		}
229		
230		static string id(this string x) { return x; }
231		static T call<T>(Func<T> f) { }
232	}
233";
234			var rr = Resolve<CSharpInvocationResolveResult>(program);
235			Assert.IsFalse(rr.IsError);
236			Assert.AreEqual("System.String", rr.Type.FullName);
237		}
238	}
239}