public void AddsUsingsForErrorTypesForRequestExecutor()
    {
        var requestBuilder = root.AddClass(new CodeClass {
            Name = "somerequestbuilder",
            Kind = CodeClassKind.RequestBuilder,
        }).First();
        var subNS      = root.AddNamespace($"{root.Name}.subns"); // otherwise the import gets trimmed
        var errorClass = subNS.AddClass(new CodeClass {
            Name = "Error4XX",
            Kind = CodeClassKind.Model,
            IsErrorDefinition = true,
        }).First();
        var requestExecutor = requestBuilder.AddMethod(new CodeMethod {
            Name       = "get",
            Kind       = CodeMethodKind.RequestExecutor,
            ReturnType = new CodeType {
                Name = "string"
            },
        }).First();

        requestExecutor.AddErrorMapping("4XX", new CodeType {
            Name           = "Error4XX",
            TypeDefinition = errorClass,
        });
        ILanguageRefiner.Refine(new GenerationConfiguration {
            Language = GenerationLanguage.CSharp
        }, root);

        var declaration = requestBuilder.StartBlock as ClassDeclaration;

        Assert.Contains("Error4XX", declaration.Usings.Select(x => x.Declaration?.Name));
    }
示例#2
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));
    }
示例#3
0
        private void AddSerializationProperties()
        {
            var addData = parentClass.AddProperty(new CodeProperty {
                Name = "additionalData",
                Kind = CodePropertyKind.AdditionalData,
            }).First();

            addData.Type = new CodeType {
                Name = "string"
            };
            var dummyProp = parentClass.AddProperty(new CodeProperty {
                Name = "dummyProp",
            }).First();

            dummyProp.Type = new CodeType {
                Name = "string"
            };
            var dummyCollectionProp = parentClass.AddProperty(new CodeProperty {
                Name = "dummyColl",
            }).First();

            dummyCollectionProp.Type = new CodeType {
                Name           = "string",
                CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array,
            };
            var dummyComplexCollection = parentClass.AddProperty(new CodeProperty {
                Name = "dummyComplexColl"
            }).First();

            dummyComplexCollection.Type = new CodeType {
                Name           = "Complex",
                CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array,
                TypeDefinition = new CodeClass {
                    Name   = "SomeComplexType",
                    Parent = root.AddNamespace("models")
                }
            };
            var dummyEnumProp = parentClass.AddProperty(new CodeProperty {
                Name = "dummyEnumCollection",
            }).First();

            dummyEnumProp.Type = new CodeType {
                Name           = "SomeEnum",
                TypeDefinition = new CodeEnum {
                    Name   = "EnumType",
                    Parent = root.AddNamespace("models")
                }
            };
        }
示例#4
0
    public void WritesContainerCommands()
    {
        method.Kind       = CodeMethodKind.CommandBuilder;
        method.SimpleName = "User";
        // Types: A.B.C.T2
        //        A.B.C.D.T1
        var ns1 = root.AddNamespace("Test.Name");
        var ns2 = ns1.AddNamespace("Test.Name.Sub1");
        var ns3 = ns2.AddNamespace("Test.Name.Sub1.Sub2");
        var t2  = parentClass;

        ns2.AddClass(t2);
        var t1Sub = new CodeClass {
            Name = "TestClass1", Kind = CodeClassKind.RequestBuilder
        };

        ns3.AddClass(t1Sub);
        t1Sub.AddMethod(new CodeMethod {
            Kind = CodeMethodKind.CommandBuilder, Name = "BuildTestMethod1", ReturnType = new CodeType()
        });
        t1Sub.AddMethod(new CodeMethod {
            Kind = CodeMethodKind.CommandBuilder, Name = "BuildTestMethod2", ReturnType = new CodeType {
                CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array
            }
        });
        method.AccessedProperty = new CodeProperty
        {
            Type = new CodeType
            {
                Name           = "TestRequestBuilder",
                TypeDefinition = t1Sub
            }
        };

        AddRequestProperties();

        writer.Write(method);
        var result = tw.ToString();

        Assert.Contains("var command = new Command(\"user\");", result);
        Assert.Contains("var builder = new TestRequestBuilder", result);
        Assert.Contains("command.AddCommand(builder.BuildTestMethod1());", result);
        Assert.Contains("foreach (var cmd in builder.BuildTestMethod2()) {", result);
        Assert.Contains("command.AddCommand(cmd);", result);
        Assert.Contains("return command;", result);
    }
示例#5
0
 public TypeScriptLanguageRefinerTests()
 {
     root        = CodeNamespace.InitRootNamespace();
     graphNS     = root.AddNamespace("graph");
     parentClass = new () {
         Name = "parentClass"
     };
     graphNS.AddClass(parentClass);
 }
示例#6
0
        public CodeNamespaceWriterTests()
        {
            codeElementWriter = new CodeNamespaceWriter(new RubyConventionService());
            writer            = LanguageWriter.GetLanguageWriter(GenerationLanguage.Ruby, DefaultPath, DefaultName);
            tw = new StringWriter();
            writer.SetTextWriter(tw);
            var root = CodeNamespace.InitRootNamespace();

            parentNamespace = root.AddNamespace("parentNamespace");
            childNamespace  = parentNamespace.AddNamespace("childNamespace");
        }
示例#7
0
    public void AddsUsingsForCommandTypesUsedInCommandBuilder()
    {
        var requestBuilder = root.AddClass(new CodeClass {
            Name = "somerequestbuilder",
            Kind = CodeClassKind.RequestBuilder,
        }).First();
        var subNS          = root.AddNamespace($"{root.Name}.subns"); // otherwise the import gets trimmed
        var commandBuilder = requestBuilder.AddMethod(new CodeMethod {
            Name       = "GetCommand",
            Kind       = CodeMethodKind.CommandBuilder,
            ReturnType = new CodeType {
                Name       = "Command",
                IsExternal = true
            }
        }).First();

        ILanguageRefiner.Refine(new GenerationConfiguration {
            Language = GenerationLanguage.Shell
        }, root);

        var declaration = requestBuilder.StartBlock as ClassDeclaration;

        Assert.Contains("System.CommandLine", declaration.Usings.Select(x => x.Declaration?.Name));
    }
        public void AddNamespaceModuleImports()
        {
            var declaration     = parentClass.StartBlock as ClassDeclaration;
            var subNS           = graphNS.AddNamespace($"{graphNS.Name}.messages");
            var messageClassDef = new CodeClass {
                Name = "Message",
            };

            subNS.AddClass(messageClassDef);
            declaration.AddUsings(new CodeUsing()
            {
                Name        = messageClassDef.Name,
                Declaration = new() {
                    Name           = messageClassDef.Name,
                    TypeDefinition = messageClassDef,
                }
            });
        public void ReplacesImportsSubNamespace()
        {
            var rootNS = parentClass.Parent as CodeNamespace;

            rootNS.RemoveChildElement(parentClass);
            graphNS.AddClass(parentClass);
            var declaration     = parentClass.StartBlock as CodeClass.Declaration;
            var subNS           = graphNS.AddNamespace($"{graphNS.Name}.messages");
            var messageClassDef = new CodeClass(subNS)
            {
                Name = "Message",
            };

            declaration.Usings.Add(new (parentClass) {
                Name        = "graph",
                Declaration = new(parentClass) {
                    Name           = "Message",
                    TypeDefinition = messageClassDef,
                }
            });
示例#10
0
    public void AccessorsTargetingEscapedPropertiesAreNotEscapedThemselves()
    {
        var model = root.AddClass(new CodeClass {
            Name = "SomeClass",
            Kind = CodeClassKind.Model
        }).First();

        model.AddProperty(new CodeProperty {
            Name = "select",
            Type = new CodeType {
                Name = "string"
            },
            Access = AccessModifier.Public,
            Kind   = CodePropertyKind.Custom,
        });
        root.AddNamespace("ApiSdk/models"); // so the interface copy refiner goes through
        ILanguageRefiner.Refine(new GenerationConfiguration {
            Language = GenerationLanguage.Go
        }, root);
        var getter     = model.Methods.First(x => x.IsOfKind(CodeMethodKind.Getter));
        var setter     = model.Methods.First(x => x.IsOfKind(CodeMethodKind.Setter));
        var tempWriter = LanguageWriter.GetLanguageWriter(GenerationLanguage.Go, DefaultPath, DefaultName);

        tempWriter.SetTextWriter(tw);
        tempWriter.Write(getter);
        var result = tw.ToString();

        Assert.Contains("GetSelect", result);
        Assert.DoesNotContain("GetSelect_escaped", result);

        using var tw2 = new StringWriter();
        tempWriter.SetTextWriter(tw2);
        tempWriter.Write(setter);
        result = tw2.ToString();
        Assert.Contains("SetSelect", result);
        Assert.DoesNotContain("SetSelect_escaped", result);
    }
示例#11
0
    public void TrimsCircularDiscriminatorReferences()
    {
        var modelsNS  = root.AddNamespace("models");
        var baseModel = modelsNS.AddClass(new CodeClass {
            Kind = CodeClassKind.Model,
            Name = "BaseModel",
        }).First();

        baseModel.AddProperty(new CodeProperty {
            Name = "Discriminator",
            Type = new CodeType {
                Name = "string"
            },
        });
        var subNamespace = modelsNS.AddNamespace($"{modelsNS.Name}.sub");
        var derivedModel = subNamespace.AddClass(new CodeClass {
            Kind = CodeClassKind.Model,
            Name = "DerivedModel",
        }).First();

        derivedModel.StartBlock.Inherits = new CodeType {
            Name           = baseModel.Name,
            TypeDefinition = baseModel,
        };
        var factoryMethod = baseModel.AddMethod(new CodeMethod {
            Kind       = CodeMethodKind.Factory,
            Name       = "factory",
            ReturnType = new CodeType {
                Name           = baseModel.Name,
                TypeDefinition = baseModel,
            },
        }).First();

        factoryMethod.DiscriminatorPropertyName = "Discriminator";
        factoryMethod.AddDiscriminatorMapping("DerivedModel", new CodeType {
            Name = derivedModel.Name, TypeDefinition = derivedModel
        });
        ILanguageRefiner.Refine(new GenerationConfiguration {
            Language = GenerationLanguage.Go
        }, root);
        Assert.Empty(factoryMethod.DiscriminatorMappings);
        Assert.Empty(baseModel.Usings.Where(x => x.Name.Equals("models.sub", StringComparison.OrdinalIgnoreCase)));
    }
 public ShellCodeNamespace AddNamespace(string Name, object Position)
 {
     return(new ShellCodeNamespace(_namespace.AddNamespace(Name, Position)));
 }
示例#13
0
 protected override object NewNamespace(NewCodeElementItemParams newItemParams, string path)
 {
     return(_namespace.AddNamespace(path,
                                    newItemParams.Position));
 }