/// <summary> /// Visits the type symbol. /// </summary> /// <param name="typeSymbol">The type symbol.</param> protected virtual void VisitTypeSymbol(TsTypeSymbol typeSymbol) { if (typeSymbol.IsArray) { this.VisitTypeSymbol(typeSymbol.ElementType); } if (typeSymbol.IsPrimitive) { return; } if (typeSymbol.HasDtoInterface) { this.VisitDtoInterfaceTypeSymbol(typeSymbol.DtoInterface); } if (typeSymbol.Base != null) { this.VisitBaseTypeSymbol(typeSymbol.Base); } foreach (TsTypeSymbol interfaceType in typeSymbol.Interfaces) { this.VisitInterfaceTypeSymbol(interfaceType); } foreach (TsPropertySymbol property in typeSymbol.Properties) { this.VisitPropertySymbol(property); } }
/// <summary> /// Gathers the import symbols that the specified <see cref="TsTypeSymbol"/> requires. /// </summary> /// <param name="typeSymbol">The type symbol.</param> /// <returns><see cref="IReadOnlyCollection{T}"/> of <see cref="TsTypeSymbol"/>.</returns> public IReadOnlyCollection <TsTypeSymbol> GatherImportSymbols(TsTypeSymbol typeSymbol) { try { HashSet <TsTypeSymbol> importSymbols = this.dependencyVisitor.GetDependencies(typeSymbol) as HashSet <TsTypeSymbol>; if ((typeSymbol.IsInterface || typeSymbol.IsAbstractClass) && typeSymbol.HasDtoInterface) { // An interface or abstract class has no need to depend on its own dto symbol interface. importSymbols.Remove(typeSymbol.DtoInterface); } if (typeSymbol.IsClass && typeSymbol.HasDtoInterface) { // Get all of the dto interface's interface properties that require transform. this.symbols = new HashSet <TsTypeSymbol>(); foreach (TsPropertySymbol dtoProperty in typeSymbol.Properties) { this.VisitPropertySymbol(dtoProperty); } importSymbols.AddRange(this.symbols); } return(importSymbols); } finally { this.symbols = null; } }
public void LoadClassWithNonArrayGenericPropertySymbol() { Assert.Throws <NotSupportedException>(() => TsTypeSymbol.LoadFrom( typeof(ClassWithNonArrayGenericProperty), new TsSymbolLookup(), TsTypeManagerOptions.Default)); }
public void LoadGenericClassSymbol() { Assert.Throws <NotSupportedException>(() => TsTypeSymbol.LoadFrom( typeof(GenericClass <int>), new TsSymbolLookup(), TsTypeManagerOptions.Default)); }
public void ResolveSymbolsFromReferenceProject() { TsTypeManager manager = new TsTypeManager(); IReadOnlyList <Type> discoveredTypes = manager.DiscoverTypes(typeof(ScenarioReferenceProject.AssemblyClassToken).Assembly); IReadOnlyList <TsTypeSymbol> symbols = manager.ResolveTypeSymbols(discoveredTypes); // Check the flattened symbol. Assert.Contains(symbols, s => s.Name == nameof(ScenarioReferenceProject.Shapes.Triangle)); TsTypeSymbol triangle = symbols.First(s => s.Name == nameof(ScenarioReferenceProject.Shapes.Triangle)); Assert.True(triangle.IsClass); Assert.Equal(3, triangle.Properties.Count); Assert.Null(triangle.Base); Assert.Empty(triangle.Interfaces); Assert.NotNull(symbols.First(s => s.Name == nameof(ScenarioReferenceProject.ShapeViewModel)).DtoInterface); Assert.NotNull(symbols.First(s => s.Name == nameof(ScenarioReferenceProject.Shapes.IShape)).DtoInterface); Assert.NotNull(symbols.First(s => s.Name == nameof(ScenarioReferenceProject.Shapes.Circle)).DtoInterface); Assert.NotNull(symbols.First(s => s.Name == nameof(ScenarioReferenceProject.Shapes.Rectangle)).DtoInterface); IReadOnlyList <TsTypeSymbol> dtoInterfaces = symbols .Where(s => s.HasDtoInterface) .Select(s => s.DtoInterface).Apply(); Assert.Equal(discoveredTypes.Count + dtoInterfaces.Count, symbols.Count); }
public void UnwrapArrayForNonArray() { TsTypeSymbol symbol = TsTypeSymbol.Number; TsTypeSymbol unwrapped = symbol.UnwrapArray(out int rank); Assert.Equal(symbol, unwrapped); Assert.Equal(0, rank); }
/// <summary> /// Visits the type symbol. /// </summary> /// <param name="typeSymbol">The type symbol.</param> protected override void VisitTypeSymbol(TsTypeSymbol typeSymbol) { if (typeSymbol.IsInterface && typeSymbol.HasDtoInterface && typeSymbol.RequiresDtoTransform()) { this.symbols.Add(typeSymbol.DtoInterface); } base.VisitTypeSymbol(typeSymbol); }
public void LoadClassWithObjectPropertySymbol() { TsTypeSymbol actualSymbol = TsTypeSymbol.LoadFrom( typeof(ClassWithObjectProperty), new TsSymbolLookup(), TsTypeManagerOptions.Default); Assert.Single(actualSymbol.Properties); Assert.Equal(TsTypeSymbol.Any, actualSymbol.Properties.Single().Type); }
public void UnwrapArrayForRank1() { TsTypeSymbol expectedSymbol = TsTypeSymbol.Number; TsTypeSymbol symbol = TsTypeSymbol.CreateArraySymbol(expectedSymbol); TsTypeSymbol unwrapped = symbol.UnwrapArray(out int rank); Assert.Equal(expectedSymbol, unwrapped); Assert.Equal(1, rank); }
public void LoadUnannotatedInterfaceSymbol() { Type type = typeof(DiscoveryReferenceProject.IUnannotatedInterface); TsTypeSymbol symbol = TsTypeSymbol.LoadFrom(type, new TsSymbolLookup(), TsTypeManagerOptions.Default); Assert.True(symbol.IsInterface); Assert.Equal(type.Name, symbol.Name); Assert.Null(symbol.Base); Assert.False(symbol.ExplicitOptIn); Assert.Equal(TsSymbolKind.Interface, symbol.Kind); Assert.Single(symbol.Properties); }
public void LoadFlatClassWithBaseSymbol() { TsSymbolLookup lookup = new TsSymbolLookup(); Type type = typeof(FlatTestClassWithBase); TsTypeSymbol symbol = TsTypeSymbol.LoadFrom(type, lookup, TsTypeManagerOptions.Default); Assert.True(symbol.IsClass); Assert.Equal(type.Name, symbol.Name); Assert.Null(symbol.Base); Assert.True(symbol.ExplicitOptIn); Assert.Equal(TsSymbolKind.Class, symbol.Kind); Assert.Single(symbol.Properties); }
/// <summary> /// Gets the dependencies required by the specified symbol. /// </summary> /// <param name="typeSymbol">The type symbol.</param> /// <returns><see cref="IReadOnlyCollection{T}"/> of <see cref="TsTypeSymbol"/>.</returns> public IReadOnlyCollection <TsTypeSymbol> GetDependencies(TsTypeSymbol typeSymbol) { try { this.dependencies = new HashSet <TsTypeSymbol>(); this.Visit(typeSymbol); return(this.dependencies); } finally { this.dependencies = null; this.depth = 0; } }
public void LoadAbstractClassSymbol() { Type type = typeof(DiscoveryReferenceProject.ClassWithUnannotatedBaseBase); TsTypeSymbol symbol = TsTypeSymbol.LoadFrom( type, new TsSymbolLookup(), TsTypeManagerOptions.Default); Assert.True(symbol.IsClass); Assert.True(symbol.IsAbstractClass); Assert.Equal(type.Name, symbol.Name); Assert.Null(symbol.Base); Assert.False(symbol.ExplicitOptIn); Assert.Equal(TsSymbolKind.Class, symbol.Kind); }
public void LoadClassWithBaseSymbol() { TsSymbolLookup lookup = new TsSymbolLookup(); Type type = typeof(DiscoveryReferenceProject.ClassWithUnannotatedBase); TsTypeSymbol baseSymbol = TsTypeSymbol.LoadFrom(type.BaseType, lookup, TsTypeManagerOptions.Default); lookup.Add(type.BaseType, baseSymbol); TsTypeSymbol symbol = TsTypeSymbol.LoadFrom(type, lookup, TsTypeManagerOptions.Default); Assert.True(symbol.IsClass); Assert.Equal(type.Name, symbol.Name); Assert.Equal(baseSymbol, symbol.Base); Assert.True(symbol.ExplicitOptIn); Assert.Equal(TsSymbolKind.Class, symbol.Kind); Assert.Empty(symbol.Properties); }
public void LoadInterfaceWithUnannotatedInterfaceSymbol() { TsSymbolLookup lookup = new TsSymbolLookup(); Type type = typeof(DiscoveryReferenceProject.InterfaceWithUnannotatedInterface); TsTypeSymbol expectedInterfaceSymbol = TsTypeSymbol.LoadFrom(typeof(DiscoveryReferenceProject.InterfaceWithUnannotatedInterfaceInterface), lookup, TsTypeManagerOptions.Default); lookup.Add(expectedInterfaceSymbol.TypeMetadata.Type, expectedInterfaceSymbol); TsTypeSymbol symbol = TsTypeSymbol.LoadFrom(type, lookup, TsTypeManagerOptions.Default); Assert.True(symbol.IsInterface); Assert.Equal(type.Name, symbol.Name); Assert.Null(symbol.Base); TsTypeSymbol actualInterfaceSymbol = Assert.Single(symbol.Interfaces); Assert.Equal(expectedInterfaceSymbol, actualInterfaceSymbol); Assert.True(symbol.ExplicitOptIn); Assert.Equal(TsSymbolKind.Interface, symbol.Kind); }
public void LoadImplicitEnumSymbol() { Type type = typeof(Color); TsTypeSymbol symbol = TsTypeSymbol.LoadFrom(type, new TsSymbolLookup(), TsTypeManagerOptions.Default); Assert.True(symbol.IsEnum); Assert.Equal(type.Name, symbol.Name); Assert.Null(symbol.Base); Assert.False(symbol.ExplicitOptIn); Assert.Equal(TsSymbolKind.Enum, symbol.Kind); Assert.False(symbol.IsConstantEnum); string[] enumNames = Enum.GetNames(type); IReadOnlyList <TsEnumItemSymbol> enumItems = symbol.GetEnumItemSymbols().Apply(); Assert.NotNull(enumItems); Assert.NotEmpty(enumItems); Assert.Equal(enumNames, enumItems.Select(i => i.Name)); }
/// <summary> /// Visits the type symbol. /// </summary> /// <param name="typeSymbol">The type symbol.</param> protected override void VisitTypeSymbol(TsTypeSymbol typeSymbol) { if (this.depth > 0 && typeSymbol.IsArray && !typeSymbol.ElementType.IsPrimitive) { this.VisitTypeSymbol(typeSymbol.ElementType); return; } this.depth++; if (typeSymbol.IsPrimitive) { this.depth--; return; } if (this.depth < 2) { if (typeSymbol.Base != null) { // A symbol will need to pass constructor parameters // to the base constructor via 'super', so it will need // to know all of the types for base type's properties. foreach (TsPropertySymbol baseProperty in typeSymbol.Base.Properties) { this.VisitPropertySymbol(baseProperty); } } base.VisitTypeSymbol(typeSymbol); } else { this.dependencies.Add(typeSymbol); } this.depth--; }
public void ResolveSymbol(Type type, string expectedSymbolName) { TsTypeSymbol typeSymbol = this.lookup.ResolveSymbol(type); Assert.Equal(expectedSymbolName, typeSymbol.Name); }
public void UnwrapArrayNull() { TsTypeSymbol symbol = null; Assert.Throws <ArgumentNullException>(() => symbol.UnwrapArray()); }
/// <summary> /// Visits the base type symbol. /// </summary> /// <param name="typeSymbol">The type symbol.</param> protected virtual void VisitBaseTypeSymbol(TsTypeSymbol typeSymbol) { this.VisitTypeSymbol(typeSymbol); }
/// <summary> /// Visits the interface type symbol. /// </summary> /// <param name="typeSymbol">The type symbol.</param> protected virtual void VisitInterfaceTypeSymbol(TsTypeSymbol typeSymbol) { this.VisitTypeSymbol(typeSymbol); }
/// <summary> /// Visits the property type symbol. /// </summary> /// <param name="propertyTypeSymbol">The property type symbol.</param> protected virtual void VisitPropertyTypeSymbol(TsTypeSymbol propertyTypeSymbol) { this.VisitTypeSymbol(propertyTypeSymbol); }
/// <summary> /// Visits the specified type symbol. /// </summary> /// <param name="typeSymbol">The type symbol.</param> public void Visit(TsTypeSymbol typeSymbol) { this.VisitTypeSymbol(typeSymbol); }