Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodBuilder"/> class.
        /// </summary>
        /// <param name="name">Name of the method</param>
        public MethodBuilder(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(name));
            }

            _name       = name.Replace(" ", "_");
            _parameters = new List <ParameterSyntax>();
            _modifiers  = new List <Modifiers>();
            _parameterXmlDocumentation = new List <Parameter>();
            _body = BodyGenerator.Create();
        }
 public void Constructor_WhenCreatingConstructorWithParamterAndModifierAndAttribute_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("[Test]publicMyClass(inti){}",
                     ConstructorGenerator.Create("MyClass", BodyGenerator.Create(),
                                                 new List <Parameter> {
         new Parameter("i", typeof(int))
     }, new List <Modifiers>()
     {
         Modifiers.Public
     },
                                                 new List <Attribute> {
         new Attribute("Test")
     }).ToString());
 }
Пример #3
0
        public void Test_CreateModelClassWithBodyProperties()
        {
            var classBuilder = new ClassBuilder("Cat", "Models");
            var @class       = classBuilder
                               .WithUsings("System")
                               .WithFields(
                new Field("_name", typeof(string), new List <Modifiers>()
            {
                Modifiers.Private
            }),
                new Field("_age", typeof(int), new List <Modifiers>()
            {
                Modifiers.Private
            }))
                               .WithConstructor(
                ConstructorGenerator.Create(
                    "Cat",
                    BodyGenerator.Create(
                        Statement.Declaration.Assign("Name", ReferenceGenerator.Create(new VariableReference("name"))),
                        Statement.Declaration.Assign("Age", ReferenceGenerator.Create(new VariableReference("age")))),
                    new List <Parameter> {
                new Parameter("name", typeof(string)), new Parameter("age", typeof(int))
            },
                    new List <Modifiers> {
                Modifiers.Public
            }))
                               .WithProperties(
                PropertyGenerator.Create(
                    new BodyProperty(
                        "Name",
                        typeof(string),
                        BodyGenerator.Create(Statement.Jump.Return(new VariableReference("_name"))), BodyGenerator.Create(Statement.Declaration.Assign("_name", new ValueKeywordReference())),
                        new List <Modifiers> {
                Modifiers.Public
            })),
                PropertyGenerator.Create(
                    new BodyProperty(
                        "Age",
                        typeof(int),
                        BodyGenerator.Create(Statement.Jump.Return(new VariableReference("_age"))), BodyGenerator.Create(Statement.Declaration.Assign("_age", new ValueKeywordReference())),
                        new List <Modifiers> {
                Modifiers.Public
            })))
                               .Build();

            Assert.AreEqual(
                "usingSystem;namespaceModels{publicclassCat{privatestring_name;privateint_age;publicCat(stringname,intage){Name=name;Age=age;}publicstringName{get{return_name;}set{_name=value;}}publicintAge{get{return_age;}set{_age=value;}}}}",
                @class.ToString());
        }
Пример #4
0
        public void Test_CreateClassWithMethodThatHaveOverrideOperators()
        {
            var classBuilder = new ClassBuilder("Cat", "Models");
            var @class       = classBuilder
                               .WithUsings("System")
                               .WithMethods(new MethodBuilder("MyMethod")
                                            .WithModifiers(Modifiers.Public, Modifiers.Static)
                                            .WithOperatorOverloading(Operators.Increment)
                                            .WithParameters(new Parameter("MyParameter", typeof(string)))
                                            .WithBody(
                                                BodyGenerator.Create(
                                                    Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment above").WithComment("hej"),
                                                    Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment to the side", CommentPosition.Right)
                                                    ))
                                            .Build())
                               .Build();

            Assert.AreEqual(
                "usingSystem;namespaceModels{publicclassCat{publicstaticMyMethodoperator++(stringMyParameter){//hej\ninthello;inthello; //My comment to the side\n}}}",
                @class.ToString());
        }
Пример #5
0
        public void Test_CreateClassWithMethodThatHaveMultipleSUmmarysAndSingleLineComments()
        {
            var classBuilder = new ClassBuilder("Cat", "Models");
            var @class       = classBuilder
                               .WithUsings("System")
                               .WithSummary("My class summary")
                               .WithConstructor(ConstructorGenerator.Create(
                                                    "Cat",
                                                    BodyGenerator.Create(
                                                        Statement.Declaration.Assign("Name", ReferenceGenerator.Create(new VariableReference("name"))),
                                                        Statement.Declaration.Assign("Age", ReferenceGenerator.Create(new VariableReference("age")))),
                                                    new List <Parameter> {
                new Parameter("name", typeof(string)), new Parameter("age", typeof(int), xmlDocumentation: "My parameter")
            },
                                                    new List <Modifiers> {
                Modifiers.Public
            },
                                                    summary: "MyConstructor summary"))
                               .WithProperties(new AutoProperty("MyProperty", typeof(int), PropertyTypes.GetAndSet, summary: "MyPropertySummary"))
                               .WithFields(
                new Field("_name", typeof(string), new List <Modifiers>()
            {
                Modifiers.Private
            }, summary: "My field summary"))
                               .WithMethods(new MethodBuilder("MyMethod")
                                            .WithParameters(new Parameter("MyParameter", typeof(string)))
                                            .WithBody(
                                                BodyGenerator.Create(
                                                    Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment above").WithComment("hej"),
                                                    Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment to the side", CommentPosition.Right)
                                                    ))
                                            .Build())
                               .Build();

            Assert.AreEqual(
                "usingSystem;namespaceModels{/// <summary>\n/// My class summary\n/// </summary>\npublicclassCat{/// <summary>\n/// MyConstructor summary\n/// </summary>\n/// <param name=\"age\">My parameter</param>\npublicCat(stringname,intage){Name=name;Age=age;}/// <summary>\n/// MyPropertySummary\n/// </summary>\nintMyProperty{get;set;}/// <summary>\n/// My field summary\n/// </summary>\nprivatestring_name;voidMyMethod(stringMyParameter){//hej\ninthello;inthello; //My comment to the side\n}}}",
                @class.ToString());
        }
Пример #6
0
        public void Test_ArgumentNull()
        {
            var classBuilder = new ClassBuilder("NullTest", "MyTest");
            var @class       = classBuilder
                               .WithUsings("System", "NUnit.Framework")
                               .WithModifiers(Modifiers.Public)
                               .WithMethods(
                new MethodBuilder("SetUp")
                .WithAttributes(new Attribute("SetUp"))
                .WithModifiers(Modifiers.Public)
                .Build(),
                new MethodBuilder("Test_WhenAddingNumber_ShouldBeCorrectSum")
                .WithAttributes(new Attribute("Test"))
                .WithModifiers(Modifiers.Public)
                .WithBody(
                    BodyGenerator.Create(
                        Statement.Declaration.Declare("myList", typeof(List <int>)),
                        NunitAssertGenerator.Throws(new VariableReference("myList", new MethodReference("First")), typeof(ArgumentNullException))))
                .Build())
                               .Build();

            Assert.AreEqual(@"usingSystem;usingNUnit.Framework;namespaceMyTest{publicclassNullTest{[SetUp]publicvoidSetUp(){}[Test]publicvoidTest_WhenAddingNumber_ShouldBeCorrectSum(){List<int>myList;Assert.Throws<ArgumentNullException>(()=>myList.First(),"""");}}}", @class.ToString());
        }
        public void If_WhenCreatingAnComplexBinaryExpression_ShouldGenerateCorrectIfStatement()
        {
            var leftBinaryExpression = new ConditionalBinaryExpression(
                new ConstantReference(1),
                new ConstantReference(2),
                ConditionalStatements.Equal);

            var rightBinaryExpression = new ConditionalBinaryExpression(
                new ConstantReference(1),
                new ConstantReference(2),
                ConditionalStatements.LessThan);

            var orBinaryExpression = new OrBinaryExpression(
                leftBinaryExpression,
                rightBinaryExpression);

            var binaryExpression = new OrBinaryExpression(
                leftBinaryExpression,
                orBinaryExpression);

            Assert.AreEqual("if(1==2||1==2||1<2){}",
                            conditional.If(binaryExpression, BodyGenerator.Create()).ToString());
        }
Пример #8
0
        public void Test_CreateModelClass()
        {
            var classBuilder = new ClassBuilder("Cat", "Models");
            var @class = classBuilder
                .WithUsings("System")
                .WithProperties(
                    PropertyGenerator.Create(new AutoProperty("Name", typeof(string), PropertyTypes.GetAndSet, new List<Modifiers> { Modifiers.Public } )), 
                    PropertyGenerator.Create(new AutoProperty("Age", typeof(int), PropertyTypes.GetAndSet, new List<Modifiers> { Modifiers.Public })))
                .WithConstructor(
                    ConstructorGenerator.Create(
                        "Cat",
                        BodyGenerator.Create(
                            Statement.Declaration.Assign("Name", ReferenceGenerator.Create(new VariableReference("name"))),
                            Statement.Declaration.Assign("Age", ReferenceGenerator.Create(new VariableReference("age")))),
                        new List<Parameter> { new Parameter("name", typeof(string)), new Parameter("age", typeof(int)) },
                        new List<Modifiers> { Modifiers.Public }))

                       .Build();

            Assert.AreEqual(
                "usingSystem;namespaceModels{publicclassCat{publicCat(stringname,intage){Name=name;Age=age;}publicstringName{get;set;}publicintAge{get;set;}}}",
                @class.ToString());
        }
Пример #9
0
        public void Test_CreateClassWithRegionWithMultipleMembers()
        {
            var classBuilder = new ClassBuilder("Cat", "Models");
            var @class       = classBuilder
                               .WithUsings("System")
                               .WithRegions(new RegionBuilder("MyRegion")
                                            .WithFields(
                                                new Field("_name", typeof(string), new List <Modifiers>()
            {
                Modifiers.Private
            }),
                                                new Field("_age", typeof(int), new List <Modifiers>()
            {
                Modifiers.Private
            }))
                                            .WithProperties(PropertyGenerator.Create(new AutoProperty("Name", typeof(string), PropertyTypes.GetAndSet, new List <Modifiers> {
                Modifiers.Public
            })))
                                            .WithConstructor(
                                                ConstructorGenerator.Create(
                                                    "Cat",
                                                    BodyGenerator.Create(
                                                        Statement.Declaration.Assign("Name", ReferenceGenerator.Create(new VariableReference("name"))),
                                                        Statement.Declaration.Assign("Age", ReferenceGenerator.Create(new VariableReference("age")))),
                                                    new List <Parameter> {
                new Parameter("name", typeof(string)), new Parameter("age", typeof(int))
            },
                                                    new List <Modifiers> {
                Modifiers.Public
            }))
                                            .Build())
                               .Build();

            Assert.AreEqual(
                "usingSystem;namespaceModels{publicclassCat{#region MyRegion \nprivatestring_name;privateint_age;publicstringName{get;set;}publicCat(stringname,intage){Name=name;Age=age;}#endregion}}",
                @class.ToString());
        }
Пример #10
0
        private IEnumerable <IArgument> GenerateAutoSelectedWiths(AutoSelectedWith autoSelected)
        {
            // If it's only one parent create a simple lambda expression without block
            if (autoSelected.Parent.Parent == null)
            {
                return(new List <IArgument>
                {
                    new LambdaArgument(
                        new AndBinaryExpression(
                            GetBinaryExpression(autoSelected.Parent, true, 1),
                            GetBinaryExpression(autoSelected, false, 0)).GetBinaryExpression(),
                        "n")
                });
            }

            var generatedIf = Statement.Selection.If(
                GetBinaryExpression(autoSelected, false, 0),
                BodyGenerator.Create(Statement.Jump.ReturnTrue()));

            return(new List <IArgument>
            {
                new LambdaArgument(BodyGenerator.Create(GenerateParantIfs(autoSelected.Parent, generatedIf, 1), Statement.Jump.ReturnFalse()), "n")
            });
        }
Пример #11
0
 public void While_WhenCreatingWhileLoopWithTrue_ShouldGenerateCode()
 {
     Assert.AreEqual("while(true){}", _control.WhileTrue(BodyGenerator.Create()).ToString());
 }
Пример #12
0
 public void For_WhenCreatingForeachLoopWithReference_ShouldGenerateCodeWithType()
 {
     Assert.AreEqual("foreach(intiina.MyMethod()){}",
                     _control.ForEach("i", typeof(int), new VariableReference("a", new MethodReference("MyMethod")), BodyGenerator.Create(), false).ToString());
 }
Пример #13
0
 public void For_WhenCreatingForeachLoopWithNamesAndNotVar_ShouldGenerateCodeWithType()
 {
     Assert.AreEqual("foreach(intiinmyList){}",
                     _control.ForEach("i", typeof(int), "myList", BodyGenerator.Create(), false).ToString());
 }
Пример #14
0
 public void For_WhenCreatingForLoopWithVariableReferences_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("for(inti=0;i<myClass.MyProperty;i++){}",
                     _control.For(new ConstantReference(0), new VariableReference("myClass", new MemberReference("MyProperty")), "i", BodyGenerator.Create()).ToString());
 }
Пример #15
0
 public void For_WhenCreatingForLoopWithStartAndEnd_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("for(inti=1;i<2;i++){}",
                     _control.For(1, 2, "i", BodyGenerator.Create()).ToString());
 }
 public void Constructor_WhenCreatingConstructorWithModifiers_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("publicMyClass(){}", ConstructorGenerator.Create("MyClass", BodyGenerator.Create(), modifiers: new List <Modifiers>()
     {
         Modifiers.Public
     }).ToString());
 }
 public void If_WhenCreatingAnBinaryExpression_ShouldGenerateCorrectIfStatement()
 {
     Assert.AreEqual("if(2<=3){}",
                     conditional.If(new ConditionalBinaryExpression(new ConstantReference(2), new ConstantReference(3), ConditionalStatements.LessThanOrEqual), BodyGenerator.Create()).ToString());
 }
Пример #18
0
 public void Create_WhenCreatingBodyPropertyWithGetAndSet_ShouldHaveBothGetAndSet()
 {
     Assert.AreEqual("intMyProperty{get{return1;}}", PropertyGenerator.Create(new BodyProperty("MyProperty", typeof(int), BodyGenerator.Create(Statement.Jump.Return(new ConstantReference(1))))).ToString());
 }
Пример #19
0
        private StatementSyntax GenerateParantIfs(AutoSelectedWith autoSelectedWith, StatementSyntax blockStatement, int parentDepth)
        {
            var generatedIf = Statement.Selection.If(GetBinaryExpression(autoSelectedWith, true, parentDepth), BodyGenerator.Create(blockStatement));

            if (autoSelectedWith.Parent != null)
            {
                return(GenerateParantIfs(autoSelectedWith.Parent, generatedIf, parentDepth + 1));
            }

            return(generatedIf);
        }
Пример #20
0
 public void Constructor_WhenCreatingConstructorWithBaseInitializer_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("MyClass():base(){}", ConstructorGenerator.Create("MyClass", BodyGenerator.Create(), constructorInitializer: new ConstructorInitializer(ConstructorInitializerTypes.Base, null)).ToString());
 }
Пример #21
0
 public void Constructor_WhenCreatingConstructorWithBaseInitializerWithArgument_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("MyClass():base(\"myText\"){}", ConstructorGenerator.Create("MyClass", BodyGenerator.Create(), constructorInitializer: new ConstructorInitializer(ConstructorInitializerTypes.Base, new List <Argument> {
         new ValueArgument("myText")
     })).ToString());
 }
 public void Constructor_WhenCreatingConstructor_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("MyClass(){}", ConstructorGenerator.Create("MyClass", BodyGenerator.Create()).ToString());
 }
 public void Constructor_WhenCreatingConstructorWithParameters_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("MyClass(inttest){}", ConstructorGenerator.Create("MyClass", BodyGenerator.Create(), new List <Parameter> {
         new Parameter("test", typeof(int))
     }).ToString());
 }
 public void If_WhenCreatingAnIfWithLessThanOrEqual_ShouldGenerateCorrectIfStatement()
 {
     Assert.AreEqual("if(2<=3){}",
                     conditional.If(new ValueArgument(2), new ValueArgument(3), ConditionalStatements.LessThanOrEqual, BodyGenerator.Create()).ToString());
 }
Пример #25
0
 public void Create_WhenCreatingBodyPropertyWithAttribute_ShouldHaveAttribute()
 {
     Assert.AreEqual("[Test]intMyProperty{get{return1;}}", PropertyGenerator.Create(new BodyProperty(
                                                                                        "MyProperty",
                                                                                        typeof(int),
                                                                                        BodyGenerator.Create(Statement.Jump.Return(new ConstantReference(1))),
                                                                                        attributes: new List <Attribute> {
         new Attribute("Test")
     })).ToString());
 }
Пример #26
0
 public void Create_WhenCreatingBodyPropertyWithModifier_ShouldHaveModifier()
 {
     Assert.AreEqual("publicvirtualintMyProperty{get{return1;}}", PropertyGenerator.Create(new BodyProperty(
                                                                                               "MyProperty",
                                                                                               typeof(int),
                                                                                               BodyGenerator.Create(Statement.Jump.Return(new ConstantReference(1))),
                                                                                               new List <Modifiers> {
         Modifiers.Public, Modifiers.Virtual
     })).ToString());
 }
 public void Constructor_WhenCreatingConstructorWithAttribute_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("[Test]MyClass(){}", ConstructorGenerator.Create("MyClass", BodyGenerator.Create(), attributes: new List <Attribute> {
         new Attribute("Test")
     }).ToString());
 }