Ejemplo n.º 1
0
        public void ModelGeneration_Class_Dependency()
        {
            // create syntax tree
            var personSyntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
public class Person
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public Address Address { get; set; }
}");

            var addressSyntaxTree = CSharpSyntaxTree.ParseText(@"public class Address
{
    public int Id { get; set; }
    public string Country { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string Street { get; set; }
    public string HouseNumber { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { personSyntaxTree, addressSyntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, personSyntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var expected = @"import { Address } from './address.model';

export class Person {
	id: number;
	firstName: string;
	lastName: string;
	dateOfBirth: Date;
	address: Address;
}
";

            Assert.AreEqual(expected, generated);
        }
Ejemplo n.º 2
0
        public void ModelGeneration_Class_BaseClass()
        {
            // create syntax tree
            var personSyntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
public class Person
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}");

            var studentSyntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
public class Student : Person
{
    public DateTime? YearOfGraduation { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { personSyntaxTree, studentSyntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, studentSyntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var expected = @"import { Person } from './person.model';

export class Student extends Person {
	yearOfGraduation?: Date;
}
";

            Assert.AreEqual(expected, generated);
        }
Ejemplo n.º 3
0
        public void ModelGeneration_Class_DateTime()
        {
            // create syntax tree
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
public class Person
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public System.DateTime DateOfBirth { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { syntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, syntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var expected = @"export class Person {
	id: number;
	firstName: string;
	lastName: string;
	dateOfBirth: Date;
}
";

            Assert.AreEqual(expected, generated);
        }
Ejemplo n.º 4
0
        public void ModelGeneration_Class_GenericClass()
        {
            // create syntax tree
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System.Collections.Generic;
public class PagedDataResponse<T>
{
    public List<T> Rows { get; set; }
    public long TotalCount { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { syntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, syntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var expected = @"export class PagedDataResponse<T> {
	rows: T[];
	totalCount: number;
}
";

            Assert.AreEqual(expected, generated);
        }
Ejemplo n.º 5
0
        public void ModelGeneration_Class_BuiltInTypes()
        {
            // create syntax tree
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
public class AllBuiltInTypes
{
    public bool Truth { get; set; }   
    public byte Bite { get; set; }
    public sbyte SmallBite { get; set; }
    public char Charred { get; set; }
    public decimal Decimus { get; set; }
    public double Twix { get; set; }
    public float Jinn { get; set; }
    public int Intelect { get; set; }
    public uint UnlimitedIntelect { get; set; }
    public long Earthworm { get; set; }
    public ulong Snake { get; set; }
    public object Everyone { get; set; }
    public short Shortayy { get; set; }
    public ushort VShortayy { get; set; }
    public string LoremIpsum { get; set; }
}");

            // create compilation
            var compilation = RoslynTestHelpers.CreateTestCompilation(new[] { syntaxTree });

            // analyze
            var context  = new TestAnalysisContext(compilation);
            var analyzer = new ModelAnalyzer(context);

            var csClassModel = analyzer.AnalyzeClassSymbol(RoslynTestHelpers.GetClassSymbol(compilation, syntaxTree));

            // convert
            var converter    = new ModelConverter();
            var tsClassModel = converter.ConvertClass(csClassModel);

            // generate
            var generator = new ModelGenerator();
            var generated = generator.GenerateClass(tsClassModel, false);

            // assert
            var fields = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("truth", "boolean"),
                new KeyValuePair <string, string>("bite", "number"),
                new KeyValuePair <string, string>("smallBite", "number"),
                new KeyValuePair <string, string>("charred", "string"),
                new KeyValuePair <string, string>("decimus", "number"),
                new KeyValuePair <string, string>("twix", "number"),
                new KeyValuePair <string, string>("jinn", "number"),
                new KeyValuePair <string, string>("intelect", "number"),
                new KeyValuePair <string, string>("unlimitedIntelect", "number"),
                new KeyValuePair <string, string>("earthworm", "number"),
                new KeyValuePair <string, string>("snake", "number"),
                new KeyValuePair <string, string>("everyone", "any"),
                new KeyValuePair <string, string>("shortayy", "number"),
                new KeyValuePair <string, string>("vShortayy", "number"),
                new KeyValuePair <string, string>("loremIpsum", "string")
            };

            var expected = TypeScriptTestHelpers.BuildTypeScriptClass("AllBuiltInTypes", fields);

            Assert.AreEqual(expected, generated);
        }
Ejemplo n.º 6
0
        public AnalysisResult <CSharpSyncModels> Synchronize(string path)
        {
            _context.Init(path);

            var typesForGeneration = new List <ITypeSymbol>();

            // get all controller classes
            var controllers = ControllerHelper.GetControllers(_context.Project, _context.Compilation);

            // process controllers
            foreach (var controller in controllers)
            {
                // get all controller methods that are exposed via HTTP
                var httpMethods = ControllerHelper.GetPublicMethods(controller);

                foreach (var httpMethod in httpMethods)
                {
                    // collect unique return types
                    if (!typesForGeneration.Exists(t => t.Equals(httpMethod.ReturnType)))
                    {
                        typesForGeneration.Add(httpMethod.ReturnType);
                    }

                    // collect unique parameter types
                    foreach (var parameter in httpMethod.Parameters)
                    {
                        if (!typesForGeneration.Exists(t => t.Equals(parameter.Type)))
                        {
                            typesForGeneration.Add(parameter.Type);
                        }
                    }
                }
            }

            var namedTypes = new List <INamedTypeSymbol>();

            // process collected types
            foreach (var type in typesForGeneration)
            {
                var namedType = type as INamedTypeSymbol;

                if (!namedTypes.Exists(t => t.Equals(namedType)) && TypeHelper.IsSupportedType(type))
                {
                    namedTypes.Add(namedType);
                }

                // handle type arguments
                if (!namedType.TypeArguments.IsDefaultOrEmpty)
                {
                    // TODO: get type members recursively
                    foreach (var typeArgument in namedType.TypeArguments)
                    {
                        var namedArgument = typeArgument as INamedTypeSymbol;

                        if (!namedTypes.Exists(t => t.Equals(namedArgument)) && TypeHelper.IsSupportedType(namedArgument))
                        {
                            namedTypes.Add(namedArgument);
                        }
                    }
                }
            }

            var depGraphs = new List <DirectedSparseGraph <DependencyNode> >();

            foreach (var namedType in namedTypes)
            {
                var depGraphFactory = new DependencyGraphService();

                var graph = depGraphFactory.BuildForNamedTypeSymbol(namedType);

                var readable = graph.ToReadable();

                depGraphs.Add(graph);
            }

            var nodes       = depGraphs.SelectMany(g => g.Vertices).Distinct().ToList();
            var uniqueNodes = nodes
                              .GroupBy(n => n.NamedTypeSymbol.ContainingNamespace + "." + n.NamedTypeSymbol.Name)
                              .Select(n => n.First())
                              .ToList();

            var modelAnalyzer = new ModelAnalyzer(_context);

            var classModels = new List <CSharpClassModel>();
            var enumModels  = new List <CSharpEnumModel>();

            foreach (var node in uniqueNodes)
            {
                var symbol = node.NamedTypeSymbol;

                if (symbol.TypeKind == TypeKind.Class || symbol.TypeKind == TypeKind.Struct)
                {
                    classModels.Add(modelAnalyzer.AnalyzeClassSymbol(symbol));
                }
                else if (symbol.TypeKind == TypeKind.Enum)
                {
                    enumModels.Add(modelAnalyzer.AnalyzeEnumSymbol(symbol));
                }
            }

            var webApiAnalyzer = new WebApiAnalyzer(_context);

            var controllerModels = new List <CSharpControllerModel>();

            foreach (var controller in controllers)
            {
                controllerModels.Add(webApiAnalyzer.AnalyzeController(controller, false));
            }

            return(new AnalysisResult <CSharpSyncModels>()
            {
                Success = true,
                Value = new CSharpSyncModels()
                {
                    DataModels = new CSharpDataModels()
                    {
                        Classes = classModels,
                        Enums = enumModels
                    },
                    Controllers = controllerModels
                }
            });
        }