Exemplo n.º 1
0
    public void ReplacesModelsByInterfaces()
    {
        var model = root.AddClass(new CodeClass {
            Name = "somemodel",
            Kind = CodeClassKind.Model,
        }).First();
        var requestBuilder = root.AddClass(new CodeClass {
            Name = "somerequestbuilder",
            Kind = CodeClassKind.RequestBuilder,
        }).First();

        var executorMethod = requestBuilder.AddMethod(new CodeMethod {
            Name       = "Execute",
            Kind       = CodeMethodKind.RequestExecutor,
            ReturnType = new CodeType {
                Name           = model.Name,
                TypeDefinition = model,
            },
        }).First();
        var executorParameter = new CodeParameter {
            Name = "requestBody",
            Kind = CodeParameterKind.RequestBody,
            Type = new CodeType {
                Name           = model.Name,
                TypeDefinition = model,
            },
        };

        executorMethod.AddParameter(executorParameter);
        var property = model.AddProperty(new CodeProperty {
            Name = "someProp",
            Kind = CodePropertyKind.Custom,
            Type = new CodeType {
                Name           = model.Name,
                TypeDefinition = model,
            },
        }).First();

        Assert.Empty(root.GetChildElements(true).OfType <CodeInterface>());
        ILanguageRefiner.Refine(new GenerationConfiguration {
            Language = GenerationLanguage.Go
        }, root);
        Assert.Single(root.GetChildElements(true).OfType <CodeInterface>());
        var inter = root.GetChildElements(true).OfType <CodeInterface>().First();

        Assert.NotEqual(model.Name, inter.Name);
        var propertyType = property.Type as CodeType;

        Assert.NotNull(propertyType);
        Assert.Equal(inter, propertyType.TypeDefinition);
        var executorParameterType = executorParameter.Type as CodeType;

        Assert.NotNull(executorParameterType);
        Assert.Equal(inter, executorParameterType.TypeDefinition);
        var executorMethodReturnType = executorMethod.ReturnType as CodeType;

        Assert.NotNull(executorMethodReturnType);
        Assert.Equal(inter, executorMethodReturnType.TypeDefinition);
    }
Exemplo n.º 2
0
        // We created barrells for codenamespaces. Skipping for empty namespaces, ones created for users, and ones with same namspace as class name.
        public async Task RenderCodeNamespaceToFilePerClassAsync(LanguageWriter writer, CodeNamespace root, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            foreach (var codeElement in root.GetChildElements(true))
            {
                switch (codeElement)
                {
                case CodeClass:
                case CodeEnum:
                case CodeFunction:
                case CodeInterface:
                    await RenderCodeNamespaceToSingleFileAsync(writer, codeElement, writer.PathSegmenter.GetPath(root, codeElement), cancellationToken);

                    break;

                case CodeNamespace codeNamespace:
                    await RenderBarrel(writer, root, codeNamespace, cancellationToken);
                    await RenderCodeNamespaceToFilePerClassAsync(writer, codeNamespace, cancellationToken);

                    break;
                }
            }
        }
Exemplo n.º 3
0
    public void AddStaticMethodsUsingsForDeserializer()
    {
        var model = graphNS.AddClass(new CodeClass {
            Name = "somemodel",
            Kind = CodeClassKind.Model,
            IsErrorDefinition = true,
        }).First();

        model.AddMethod(new CodeMethod {
            Name       = "Deserialize",
            Kind       = CodeMethodKind.Deserializer,
            IsAsync    = false,
            ReturnType = new CodeType {
                Name       = "void",
                IsExternal = true,
            },
        });

        var subNs = graphNS.AddNamespace($"{graphNS.Name}.subns");

        var propertyModel = subNs.AddClass(new CodeClass {
            Name = "somepropertyModel",
            Kind = CodeClassKind.Model,
            IsErrorDefinition = true,
        }).First();

        propertyModel.AddMethod(new CodeMethod {
            Name       = "factory",
            Kind       = CodeMethodKind.Factory,
            IsAsync    = false,
            IsStatic   = true,
            ReturnType = new CodeType {
                Name       = "void",
                IsExternal = true,
            },
        });

        model.AddProperty(new CodeProperty {
            Name = "someProperty",
            Type = new CodeType {
                Name           = "somepropertyModel",
                TypeDefinition = propertyModel,
            },
        });

        Assert.Empty(graphNS.GetChildElements(true).OfType <CodeFunction>());
        Assert.Single(model.GetChildElements(true).OfType <CodeMethod>());

        ILanguageRefiner.Refine(new GenerationConfiguration {
            Language = GenerationLanguage.TypeScript
        }, graphNS);
        Assert.Empty(model.GetChildElements(true).OfType <CodeMethod>().Where(x => x.IsOfKind(CodeMethodKind.Factory)));
        Assert.Single(subNs.GetChildElements(true).OfType <CodeFunction>());

        var function = subNs.GetChildElements(true).OfType <CodeFunction>().First();

        Assert.Single(model.Usings.Where(x => !x.IsExternal && x.Declaration.TypeDefinition == function));
    }
Exemplo n.º 4
0
    public void MovesClassesWithNamespaceNamesUnderNamespace()
    {
        var graphNS = root.AddNamespace("graph");
        var modelNS = graphNS.AddNamespace("graph.model");
        var model   = graphNS.AddClass(new CodeClass {
            Name = "model",
            Kind = CodeClassKind.Model
        }).First();

        ILanguageRefiner.Refine(new GenerationConfiguration {
            Language = GenerationLanguage.CSharp
        }, root);
        Assert.Single(root.GetChildElements(true));
        Assert.Single(graphNS.GetChildElements(true));
        Assert.Single(modelNS.GetChildElements(true));
        Assert.Equal(modelNS, model.Parent);
    }