~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.ConsistencyCheck/TypeSystemTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 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
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
using ICSharpCode.NRefactory.Documentation;
 
23
using ICSharpCode.NRefactory.TypeSystem;
 
24
 
 
25
namespace ICSharpCode.NRefactory.ConsistencyCheck
 
26
{
 
27
        public class TypeSystemTests
 
28
        {
 
29
                public static void IDStringConsistencyCheck(Solution solution)
 
30
                {
 
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);
 
39
                                        }
 
40
                                }
 
41
                        }
 
42
                }
 
43
                
 
44
                static void Check(IEntity entity, ITypeResolveContext context, HashSet<string> idStrings)
 
45
                {
 
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);
 
52
                }
 
53
                
 
54
                public static void ResolvedUnresolvedMembers(Solution solution)
 
55
                {
 
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();
 
63
                                        }
 
64
                                        foreach (var member in IncludeAccessors(typeDef.Members)) {
 
65
                                                var resolvedMember = member.UnresolvedMember.Resolve(assemblyContext);
 
66
                                                if (!member.Equals(resolvedMember))
 
67
                                                        throw new InvalidOperationException();
 
68
                                        }
 
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);
 
74
                                        else
 
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();
 
81
                                        }
 
82
                                }
 
83
                        }
 
84
                }
 
85
                
 
86
                static IEnumerable<IMember> IncludeAccessors(IEnumerable<IMember> members)
 
87
                {
 
88
                        foreach (var member in members) {
 
89
                                yield return member;
 
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;
 
102
                        }
 
103
                }
 
104
        }
 
105
}