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.Generic;
22
using ICSharpCode.NRefactory.Documentation;
23
using ICSharpCode.NRefactory.TypeSystem;
25
namespace ICSharpCode.NRefactory.ConsistencyCheck
27
public class TypeSystemTests
29
public static void IDStringConsistencyCheck(Solution solution)
31
foreach (var project in solution.Projects) {
32
var compilation = project.Compilation;
33
HashSet<string> idStrings = new HashSet<string>();
34
var context = compilation.TypeResolveContext;
35
foreach (var typeDef in compilation.MainAssembly.GetAllTypeDefinitions()) {
36
Check(typeDef, context, idStrings);
37
foreach (var member in typeDef.Members) {
38
Check(member, context, idStrings);
44
static void Check(IEntity entity, ITypeResolveContext context, HashSet<string> idStrings)
46
string id = IdStringProvider.GetIdString(entity);
47
if (!idStrings.Add(id))
48
throw new InvalidOperationException("Duplicate ID string " + id);
49
IEntity resolvedEntity = IdStringProvider.FindEntity(id, context);
50
if (resolvedEntity != entity)
51
throw new InvalidOperationException(id);
54
public static void ResolvedUnresolvedMembers(Solution solution)
56
foreach (var project in solution.Projects) {
57
var compilation = project.Compilation;
58
var assemblyContext = new SimpleTypeResolveContext(compilation.MainAssembly);
59
foreach (var typeDef in compilation.MainAssembly.GetAllTypeDefinitions()) {
60
foreach (var part in typeDef.Parts) {
61
if (!typeDef.Equals(part.Resolve(assemblyContext)))
62
throw new InvalidOperationException();
64
foreach (var member in IncludeAccessors(typeDef.Members)) {
65
var resolvedMember = member.UnresolvedMember.Resolve(assemblyContext);
66
if (!member.Equals(resolvedMember))
67
throw new InvalidOperationException();
69
// ToMemberReference() requires an appropriate generic context if the member
70
// contains open generics; otherwise the main context of the compilation is sufficient.
71
ITypeResolveContext context;
72
if (typeDef.TypeParameterCount > 0)
73
context = new SimpleTypeResolveContext(typeDef);
75
context = compilation.TypeResolveContext;
76
// Include (potentially specialized) inherited members when testing ToMemberReference()
77
foreach (var member in IncludeAccessors(typeDef.GetMembers())) {
78
var resolvedMember = member.ToMemberReference().Resolve(context);
79
if (!member.Equals(resolvedMember))
80
throw new InvalidOperationException();
86
static IEnumerable<IMember> IncludeAccessors(IEnumerable<IMember> members)
88
foreach (var member in members) {
90
IProperty p = member as IProperty;
91
if (p != null && p.CanGet)
92
yield return p.Getter;
93
if (p != null && p.CanSet)
94
yield return p.Setter;
95
IEvent e = member as IEvent;
96
if (e != null && e.CanAdd)
97
yield return e.AddAccessor;
98
if (e != null && e.CanRemove)
99
yield return e.RemoveAccessor;
100
if (e != null && e.CanInvoke)
101
yield return e.InvokeAccessor;