PageRenderTime 34ms CodeModel.GetById 7ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.Tests/TypeSystem/GetAllBaseTypesTest.cs

http://github.com/icsharpcode/ILSpy
C# | 256 lines | 210 code | 23 blank | 23 comment | 0 complexity | 7210303ba3b1da6915a1af90adccb32f 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.Collections;
 21using System.Collections.Generic;
 22using System.Linq;
 23using System.Runtime.Serialization;
 24using ICSharpCode.NRefactory.CSharp;
 25using ICSharpCode.NRefactory.TypeSystem.Implementation;
 26using NUnit.Framework;
 27
 28namespace ICSharpCode.NRefactory.TypeSystem
 29{
 30	using Unbound = ReflectionHelper.UnboundTypeArgument;
 31	
 32	[TestFixture]
 33	public class GetAllBaseTypesTest
 34	{
 35		const string corlib = @"
 36namespace System {
 37	class Object {}
 38	class ValueType {}
 39	class String : System.Collections.Generic.IEnumerable<char>, IComparable<string> {}
 40	class Array : System.Collections.IList, ICloneable {}
 41	
 42	interface ICloneable {}
 43	interface IComparable<in T> {}
 44	struct Int32 {}
 45	struct Char {}
 46}
 47namespace System.Collections {
 48	interface IEnumerable {}
 49	interface ICollection : IEnumerable {}
 50	interface IList : ICollection {}
 51	interface IDictionary : ICollection {}
 52}
 53namespace System.Collections.Generic {
 54	interface IEnumerable<out T> : IEnumerable {}
 55	interface ICollection<T> : IEnumerable<T> {}
 56	interface IList<T> : ICollection<T> {}
 57	interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>> {}
 58
 59	class List<T> : IList, IList<T> {}
 60	class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary {}
 61	struct KeyValuePair<TKey, TValue> {}
 62}
 63";
 64		
 65		ICompilation compilation;
 66		
 67		[SetUp]
 68		public void SetUp()
 69		{
 70			var unresolvedFile = new CSharpParser().Parse(corlib, "corlib.cs").ToTypeSystem();
 71			compilation = new CSharpProjectContent().SetAssemblyName("mscorlib").AddOrUpdateFiles(unresolvedFile).CreateCompilation();
 72		}
 73		
 74		IType[] GetAllBaseTypes(Type type)
 75		{
 76			return compilation.FindType(type).GetAllBaseTypes().OrderBy(t => t.ReflectionName).ToArray();
 77		}
 78		
 79		IType[] GetTypes(params Type[] types)
 80		{
 81			return types.Select(t => compilation.FindType(t)).OrderBy(t => t.ReflectionName).ToArray();
 82		}
 83		
 84		ITypeDefinition Resolve(IUnresolvedTypeDefinition typeDef)
 85		{
 86			return compilation.MainAssembly.GetTypeDefinition(typeDef);
 87		}
 88		
 89		[Test]
 90		public void ObjectBaseTypes()
 91		{
 92			Assert.AreEqual(GetTypes(typeof(object)), GetAllBaseTypes(typeof(object)));
 93			
 94			Assert.That(compilation.FindType(KnownTypeCode.Object).DirectBaseTypes, Is.Empty);
 95		}
 96		
 97		[Test]
 98		public void StringBaseTypes()
 99		{
100			Assert.AreEqual(GetTypes(typeof(string), typeof(object),
101			                         typeof(IComparable<string>), typeof(IEnumerable<char>), typeof(IEnumerable)),
102			                GetAllBaseTypes(typeof(string)));
103		}
104		
105		[Test]
106		public void ArrayOfString()
107		{
108			var expectedTypes = GetTypes(
109				typeof(string[]), typeof(Array), typeof(object),
110				typeof(IList), typeof(ICollection), typeof(IEnumerable), typeof(ICloneable),
111				typeof(IList<string>), typeof(ICollection<string>), typeof(IEnumerable<string>));
112			Assert.AreEqual(expectedTypes,
113			                GetAllBaseTypes(typeof(string[])));
114		}
115		
116		[Test]
117		public unsafe void ArrayOfPointers()
118		{
119			Assert.AreEqual(GetTypes(typeof(int*[]), typeof(Array), typeof(object),
120			                         typeof(IList), typeof(ICollection), typeof(IEnumerable), typeof(ICloneable)),
121			                GetAllBaseTypes(typeof(int*[])));
122		}
123		
124		[Test]
125		public void MultidimensionalArrayOfString()
126		{
127			Assert.AreEqual(GetTypes(typeof(string[,]), typeof(Array), typeof(object),
128			                         typeof(IList), typeof(ICollection), typeof(IEnumerable), typeof(ICloneable)),
129			                GetAllBaseTypes(typeof(string[,])));
130		}
131		
132		[Test]
133		public void ClassDerivingFromItself()
134		{
135			// class C : C {}
136			var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
137			c.BaseTypes.Add(c);
138			compilation = TypeSystemHelper.CreateCompilation(c);
139			ITypeDefinition resolvedC = Resolve(c);
140			Assert.AreEqual(new [] { resolvedC }, resolvedC.GetAllBaseTypes().ToArray());
141		}
142		
143		[Test]
144		public void TwoClassesDerivingFromEachOther()
145		{
146			// class C1 : C2 {} class C2 : C1 {}
147			var c1 = new DefaultUnresolvedTypeDefinition(string.Empty, "C1");
148			var c2 = new DefaultUnresolvedTypeDefinition(string.Empty, "C2");
149			c1.BaseTypes.Add(c2);
150			c2.BaseTypes.Add(c1);
151			compilation = TypeSystemHelper.CreateCompilation(c1, c2);
152			ITypeDefinition resolvedC1 = Resolve(c1);
153			ITypeDefinition resolvedC2 = Resolve(c2);
154			Assert.AreEqual(new [] { resolvedC2, resolvedC1 }, resolvedC1.GetAllBaseTypes().ToArray());
155		}
156		
157		[Test]
158		public void ClassDerivingFromParameterizedVersionOfItself()
159		{
160			// class C<X> : C<C<X>> {}
161			var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
162			c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "X"));
163			c.BaseTypes.Add(new ParameterizedTypeReference(c, new [] { new ParameterizedTypeReference(c, new [] { new TypeParameterReference(EntityType.TypeDefinition, 0) }) }));
164			compilation = TypeSystemHelper.CreateCompilation(c);
165			ITypeDefinition resolvedC = Resolve(c);
166			Assert.AreEqual(new [] { resolvedC }, resolvedC.GetAllBaseTypes().ToArray());
167		}
168		
169		[Test]
170		public void ClassDerivingFromTwoInstanciationsOfIEnumerable()
171		{
172			// class C : IEnumerable<int>, IEnumerable<uint> {}
173			var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");
174			c.BaseTypes.Add(typeof(IEnumerable<int>).ToTypeReference());
175			c.BaseTypes.Add(typeof(IEnumerable<uint>).ToTypeReference());
176			compilation = TypeSystemHelper.CreateCompilation(c);
177			ITypeDefinition resolvedC = Resolve(c);
178			IType[] expected = {
179				resolvedC,
180				compilation.FindType(typeof(IEnumerable<int>)),
181				compilation.FindType(typeof(IEnumerable<uint>)),
182				compilation.FindType(typeof(IEnumerable)),
183				compilation.FindType(typeof(object))
184			};
185			Assert.AreEqual(expected,
186			                resolvedC.GetAllBaseTypes().OrderBy(t => t.ReflectionName).ToArray());
187		}
188		
189		[Test]
190		public void StructImplementingIEquatable()
191		{
192			// struct S : IEquatable<S> {}
193			// don't use a Cecil-loaded struct for this test; we're testing the implicit addition of System.ValueType
194			var s = new DefaultUnresolvedTypeDefinition(string.Empty, "S");
195			s.Kind = TypeKind.Struct;
196			s.BaseTypes.Add(new ParameterizedTypeReference(typeof(IEquatable<>).ToTypeReference(), new[] { s }));
197			compilation = TypeSystemHelper.CreateCompilation(s);
198			ITypeDefinition resolvedS = Resolve(s);
199			IType[] expected = {
200				resolvedS,
201				s.BaseTypes[0].Resolve(new SimpleTypeResolveContext(resolvedS)),
202				compilation.FindType(typeof(object)),
203				compilation.FindType(typeof(ValueType))
204			};
205			Assert.AreEqual(expected,
206			                resolvedS.GetAllBaseTypes().OrderBy(t => t.ReflectionName).ToArray());
207		}
208		
209		[Test]
210		public void BaseTypesOfListOfString()
211		{
212			Assert.AreEqual(
213				GetTypes(typeof(List<string>), typeof(object),
214				         typeof(IList), typeof(ICollection), typeof(IEnumerable),
215				         typeof(IEnumerable<string>), typeof(ICollection<string>), typeof(IList<string>)),
216				GetAllBaseTypes(typeof(List<string>)));
217		}
218		
219		[Test]
220		public void BaseTypesOfUnboundDictionary()
221		{
222			Assert.AreEqual(
223				new [] {
224					typeof(Dictionary<,>).FullName,
225					typeof(ICollection<>).FullName + "[[" + typeof(KeyValuePair<,>).FullName + "[[`0],[`1]]]]",
226					typeof(IDictionary<,>).FullName + "[[`0],[`1]]",
227					typeof(IEnumerable<>).FullName + "[[" + typeof(KeyValuePair<,>).FullName + "[[`0],[`1]]]]",
228					typeof(ICollection).FullName,
229					typeof(IDictionary).FullName,
230					typeof(IEnumerable).FullName,
231					typeof(object).FullName
232				},
233				GetAllBaseTypes(typeof(Dictionary<,>)).Select(t => t.ReflectionName).OrderBy(n => n).ToArray());
234		}
235		
236		[Test]
237		public void BaseTypeDefinitionsOfListOfString()
238		{
239			Assert.AreEqual(
240				GetTypes(typeof(List<>), typeof(object),
241				         typeof(IList), typeof(ICollection), typeof(IEnumerable),
242				         typeof(IEnumerable<>), typeof(ICollection<>), typeof(IList<>)),
243				compilation.FindType(typeof(List<string>)).GetAllBaseTypeDefinitions().OrderBy(t => t.ReflectionName).ToArray());
244		}
245		
246		[Test]
247		public void BaseTypeDefinitionsOfStringArray()
248		{
249			Assert.AreEqual(
250				GetTypes(typeof(Array), typeof(object), typeof(ICloneable),
251				         typeof(IList), typeof(ICollection), typeof(IEnumerable),
252				         typeof(IEnumerable<>), typeof(ICollection<>), typeof(IList<>)),
253				compilation.FindType(typeof(string[])).GetAllBaseTypeDefinitions().OrderBy(t => t.ReflectionName).ToArray());
254		}
255	}
256}