public void Equality_Default()
        {
            var a = new TypeHierarchy();
            var b = new TypeHierarchy();

            Assert.AreEqual(a, b);
            Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
        }
        public void Equality_DifferentElement()
        {
            var a = new TypeHierarchy {
                Element = Names.Type("T1,P1")
            };
            var b = new TypeHierarchy();

            Assert.AreNotEqual(a, b);
            Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode());
        }
        public void Equality_DifferentExtends()
        {
            var a = new TypeHierarchy {
                Extends = SomeHierarchy("a")
            };
            var b = new TypeHierarchy();

            Assert.AreNotEqual(a, b);
            Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode());
        }
        public void Equality_DifferentImplements()
        {
            var a = new TypeHierarchy {
                Implements = { SomeHierarchy("i") }
            };
            var b = new TypeHierarchy();

            Assert.AreNotEqual(a, b);
            Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode());
        }
        public void DefaultValues()
        {
            var sut = new TypeHierarchy();

            Assert.AreEqual(Names.UnknownType, sut.Element);
            Assert.Null(sut.Extends);
            Assert.AreEqual(Sets.NewHashSet <ITypeHierarchy>(), sut.Implements);
            Assert.False(sut.HasSuperclass);
            Assert.False(sut.HasSupertypes);
            Assert.False(sut.IsImplementingInterfaces);
            Assert.AreNotEqual(0, sut.GetHashCode());
            Assert.AreNotEqual(1, sut.GetHashCode());
        }
Exemple #6
0
        private static TypeHierarchy CreateTypeHierarchy(ITypeElement type,
                                                         ISubstitution substitution,
                                                         IKaVEList <ITypeName> seenTypes,
                                                         bool shouldIgnoreRootTypes)
        {
            if (shouldIgnoreRootTypes && (type == null || IsRootType(type)))
            {
                // ignore implicite extensions in type hierarchy
                return(null);
            }
            var typeName = type.GetName <ITypeName>(substitution);

            seenTypes.Add(typeName);
            var enclosingClassHierarchy = new TypeHierarchy(typeName.Identifier);

            foreach (var superType in type.GetSuperTypes())
            {
                var resolveResult     = superType.Resolve();
                var declElem          = resolveResult.DeclaredElement;
                var isUnresolvedAlias = declElem is IUsingAliasDirective;
                // TODO NameUpdate: "isUnknownOrUnResolvedUntested" required by one analyzed solution, still untested
                var isUnknownOrUnResolvedUntested = superType.IsUnknown || !superType.IsResolved;
                if (!resolveResult.IsValid() || declElem == null || isUnresolvedAlias || isUnknownOrUnResolvedUntested)
                {
                    enclosingClassHierarchy.Implements.Add(new TypeHierarchy());
                    continue;
                }

                var superName = declElem.GetName <ITypeName>(substitution);
                if (seenTypes.Contains(superName))
                {
                    continue;
                }

                var superTypeElement      = superType.GetTypeElement();
                var superTypeSubstitution = superType.GetSubstitution();
                var superHierarchy        = CreateTypeHierarchy(superTypeElement, superTypeSubstitution, seenTypes, true);

                if (declElem is IClass || declElem is IStruct)
                {
                    enclosingClassHierarchy.Extends = superHierarchy;
                }
                else if (declElem is IInterface)
                {
                    enclosingClassHierarchy.Implements.Add(superHierarchy);
                }
            }
            return(enclosingClassHierarchy);
        }
        public void SettingValues()
        {
            var sut = new TypeHierarchy
            {
                Element    = Names.Type("T1,P1"),
                Extends    = SomeHierarchy("x"),
                Implements = { SomeHierarchy("y") }
            };

            Assert.AreEqual(Names.Type("T1,P1"), sut.Element);
            Assert.AreEqual(SomeHierarchy("x"), sut.Extends);
            Assert.AreEqual(Sets.NewHashSet <ITypeHierarchy>(SomeHierarchy("y")), sut.Implements);
            Assert.True(sut.HasSuperclass);
            Assert.True(sut.HasSupertypes);
            Assert.True(sut.IsImplementingInterfaces);
        }
        public void Equality_ReallyTheSame()
        {
            var a = new TypeHierarchy
            {
                Element    = Names.Type("T1,P1"),
                Extends    = SomeHierarchy("x"),
                Implements = { SomeHierarchy("y") }
            };
            var b = new TypeHierarchy
            {
                Element    = Names.Type("T1,P1"),
                Extends    = SomeHierarchy("x"),
                Implements = { SomeHierarchy("y") }
            };

            Assert.AreEqual(a, b);
            Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
        }
 public static bool GetTypeSymbol(ISymbol symbol, out ITypeSymbol result)
 {
     if (symbol != null &&
         !(symbol is IMethodSymbol) && !(symbol is INamespaceOrTypeSymbol) &&
         !(symbol is IPreprocessingSymbol) && !(symbol is ITypeSymbol) &&
         !(symbol is ILabelSymbol) &&
         TypeHierarchy.ComputeTypeForSymbol(symbol, out var typeSym))
     {
         result = typeSym;
         return(true);
     }
     else if (symbol is IMethodSymbol methodSymbol)
     {
         result = methodSymbol.ReturnType; // methods' symbols are their return types, for now.
         return(true);
     }
     result = null;
     return(false);
 }
        public void TryingToEmulateAnStackOverflowExample()
        {
            CompleteInCSharpFile(@"
                namespace N
                {
                    class G1 {}
                    class G2 : G1 {}

                    class T1 {}
                    class T2<TG> : T1 where TG : G1 {}
                    class T3 : T2<G2> {}
                    class T4 : T3
                    {
                        void M()
                        {
                            object.$

                            GetHashCode();
                        }
                    }
                }");

            var actual   = ResultContext.TypeShape.TypeHierarchy;
            var expected = new TypeHierarchy
            {
                Element = Type("T4"),
                Extends = new TypeHierarchy
                {
                    Element = Type("T3"),
                    Extends = new TypeHierarchy
                    {
                        Element = Type("T2`1[[TG -> " + Type("G2") + "]]"),
                        Extends = new TypeHierarchy
                        {
                            Element = Type("T1")
                        }
                    }
                }
            };

            Assert.AreEqual(expected, actual);
        }
Exemple #11
0
        public void TypeHierarchy()
        {
            CompleteInNamespace(_src);

            var actual   = ResultContext.TypeShape.TypeHierarchy;
            var expected = new TypeHierarchy
            {
                Element = Names.Type("N.BasicCases.TypeShapeElem, TestProject"),
                Extends = new TypeHierarchy
                {
                    Element = Names.Type("N.BasicCases.TypeShapeSuper, TestProject"),
                    Extends = new TypeHierarchy
                    {
                        Element = Names.Type("N.BasicCases.TypeShapeFirst, TestProject"),
                        Extends = new TypeHierarchy
                        {
                            Element = Names.Type("N.BasicCases.TypeShapeOther, TestProject")
                        }
                    }
                }
            };

            Assert.AreEqual(expected, actual);
        }
        public void DefaultValues_CustomConstructor()
        {
            var sut = new TypeHierarchy("T,P");

            Assert.AreEqual(Names.Type("T,P"), sut.Element);
        }
        public void ShouldRetrieveLargeHierarchy()
        {
            CompleteInCSharpFile(@"
                namespace N
                {
                    interface I0
                    {
                         
                    }
                
                    interface IA : I0
                    {
                         
                    }
                
                    interface IB<TB>
                    {
                        
                    }
                
                    interface IC
                    {
                        
                    }
                
                    class A : IA
                    {
                        
                    }
                
                    class B : A, IB<int>, IC
                    {
                        public void m()
                        {
                            {caret}
                        }
                    }
                }");

            var actual   = ResultContext.TypeShape.TypeHierarchy;
            var expected = new TypeHierarchy("N.B, TestProject")
            {
                Extends = new TypeHierarchy("N.A, TestProject")
                {
                    Implements =
                    {
                        new TypeHierarchy("i:N.IA, TestProject")
                        {
                            Implements =
                            {
                                new TypeHierarchy("i:N.I0, TestProject")
                            }
                        }
                    }
                },
                Implements =
                {
                    new TypeHierarchy("i:N.IB`1[[TB -> {0}]], TestProject".FormatEx(Fix.Int)),
                    new TypeHierarchy("i:N.IC, TestProject")
                }
            };

            Assert.AreEqual(expected, actual);
        }