Beispiel #1
0
        private FileModel GetEntityUserFile()
        {
            string fileNameSpace = "CsCodeGenerator.Tests";
            string userText      = "User";
            string textFirstName = "FirstName"; //nameof(User.FirstName);

            // Properties
            var userProperties = new List <Property>
            {
                new Property(BuiltInDataType.Int, "UserId"),
                new Property(BuiltInDataType.String, "FirstName"),
                new Property(BuiltInDataType.String, "FamilyName"),
                new Property(BuiltInDataType.String, "Address"),
                new Property(CommonDataType.DateTime.ToString(), "DateOfBirth"),
                new Property(BuiltInDataType.String, "FullName")
                {
                    IsGetOnly = true, IsAutoImplemented = false, GetterBody = "FirstName + FamilyName"
                }
            };

            var lastNameColumAttributeParams = new List <Parameter>
            {
                new Parameter(@"""LastName"""),
                new Parameter()
                {
                    Name = "Order", Value = "2"
                }
            };

            userProperties.Single(a => a.Name == "UserId").AddAttribute(new AttributeModel("Key"));
            userProperties.Single(a => a.Name == "FirstName").AddAttribute(new AttributeModel("Column")
            {
                SingleParameter = new Parameter {
                    Name = "Order", Value = "1"
                }
            });
            userProperties.Single(a => a.Name == "FamilyName").AddAttribute(new AttributeModel("Column")
            {
                Parameters = lastNameColumAttributeParams
            });
            userProperties.Single(a => a.Name == "FullName").AddAttribute(new AttributeModel("NotMapped"));

            var userTableAttributeParams = new List <Parameter>
            {
                new Parameter(@"""User"""),
                new Parameter()
                {
                    Name = "Schema", Value = @"""tmp"""
                }
            };

            /*var methods = new List<Method>
             * {
             *  new Method(BuiltInDataType.Bool, "TestMetod")
             *  {
             *      BodyLines = new List<string>
             *      {
             *          "var check = true;",
             *          "if(checked == true)",
             *          "{",
             *          new String(' ', CsGenerator.DefaultTabSize) + "checked = false;",
             *          "}",
             *          "return check;"
             *      }
             *  }
             * };*/

            // Class
            ClassModel userClass = new ClassModel(userText);

            userClass.SingleKeyWord = KeyWord.Partial;
            userClass.AddAttribute(new AttributeModel("Table")
            {
                Parameters = userTableAttributeParams
            });
            userClass.Properties = userProperties;
            //userClass.Methods = methods;

            var usingDirectives = new List <string>
            {
                "System;",
                "System.ComponentModel.DataAnnotations;",
                "System.ComponentModel.DataAnnotations.Schema;"
            };

            // File
            FileModel userFile = new FileModel(userText);

            userFile.LoadUsingDirectives(usingDirectives);
            userFile.Namespace = fileNameSpace;
            userFile.Classes.Add(userClass);

            return(userFile);
        }
Beispiel #2
0
        private void WriteComplexNumberFile()
        {
            var usingDirectives = new List <string>
            {
                "System;",
                "System.ComponentModel;"
            };
            string fileNameSpace     = "CsCodeGenerator.Tests";
            string complexNumberText = "ComplexNumber";

            ClassModel complexNumberClass = new ClassModel(complexNumberText);

            complexNumberClass.SingleKeyWord = KeyWord.Partial; //or: complexNumberClass.KeyWords.Add(KeyWord.Partial);

            var descriptionAttribute = new AttributeModel("Description")
            {
                SingleParameter = new Parameter(@"""Some class info""")
            };

            complexNumberClass.AddAttribute(descriptionAttribute);

            complexNumberClass.DefaultConstructor.IsVisible = true;

            Constructor secondConstructor = new Constructor(complexNumberClass.Name);

            secondConstructor.Parameters.Add(new Parameter(BuiltInDataType.Double, "real"));
            secondConstructor.Parameters.Add(new Parameter(BuiltInDataType.Double, "imaginary")
            {
                Value = "0"
            });
            secondConstructor.BodyLines.Add("Real = real;");
            secondConstructor.BodyLines.Add("Imaginary = imaginary;");
            complexNumberClass.Constructors.Add(secondConstructor);

            var fields = new List <Field>
            {
                new Field(BuiltInDataType.Double, "PI")
                {
                    SingleKeyWord = KeyWord.Const, DefaultValue = "3.14"
                },
                new Field(BuiltInDataType.String, "remark")
                {
                    AccessModifier = AccessModifier.Private
                },
            };

            var properties = new List <Property>
            {
                new Property(BuiltInDataType.String, "DefaultFormat")
                {
                    SingleKeyWord = KeyWord.Static,
                    IsGetOnly     = true,
                    DefaultValue  = @"""a + b * i"""
                },
                new Property(BuiltInDataType.Double, "Real"),
                new Property(BuiltInDataType.Double, "Imaginary"),
                new Property(BuiltInDataType.String, "Remark")
                {
                    SingleKeyWord     = KeyWord.Virtual,
                    IsAutoImplemented = false,
                    GetterBody        = "remark",
                    SetterBody        = "remark = value"
                },
            };

            var methods = new List <Method>
            {
                new Method(BuiltInDataType.Double, "Modul")
                {
                    BodyLines = new List <string> {
                        "return Math.Sqrt(Real * Real + Imaginary * Imaginary);"
                    }
                },
                new Method(complexNumberText, "Add")
                {
                    Parameters = new List <Parameter> {
                        new Parameter("ComplexNumber", "input")
                    },
                    BodyLines = new List <string>
                    {
                        "ComplexNumber result = new ComplexNumber();",
                        "result.Real = Real + input.Real;",
                        "result.Imaginary = Imaginary + input.Imaginary;",
                        "return result;"
                    }
                },
                new Method(BuiltInDataType.String, "ToString")
                {
                    Comment  = "example of 2 KeyWords(new and virtual), usually here would be just virtual",
                    KeyWords = new List <KeyWord> {
                        KeyWord.New, KeyWord.Virtual
                    },
                    BodyLines = new List <string> {
                        "return $\"({Real:0.00}, {Imaginary:0.00})\";"
                    }
                }
            };

            complexNumberClass.Fields     = fields;
            complexNumberClass.Properties = properties;
            complexNumberClass.Methods    = methods;

            FileModel complexNumberFile = new FileModel(complexNumberText);

            complexNumberFile.LoadUsingDirectives(usingDirectives);
            complexNumberFile.Namespace = fileNameSpace;
            complexNumberFile.Classes.Add(complexNumberClass);

            CsGenerator csGenerator = new CsGenerator();

            csGenerator.Files.Add(complexNumberFile);
            //csGenerator.CreateFiles(); //Console.Write(complexNumberFile);

            string result = complexNumberFile.ToString();

            string text = GetComplexNumberFileText();

            Debug.Write(result);
            Assert.Equal(result, text);
        }