Esempio n. 1
0
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        CodeNamespace nspace = new CodeNamespace("NSPC");

        nspace.Imports.Add(new CodeNamespaceImport("System"));
        cu.Namespaces.Add(nspace);

        CodeTypeDeclaration cd = new CodeTypeDeclaration("TEST");

        cd.IsClass = true;
        nspace.Types.Add(cd);


        // GENERATES (C#):
        //        public int CallingOverrideScenario(int i) {
        //            ClassWVirtualMethod t = new ClassWOverrideMethod();
        //            return t.VirtualMethod(i);
        //        }
        AddScenario("Check1CallingOverrideScenario", "Check an overridden method.");
        CodeMemberMethod cmm = new CodeMemberMethod();

        cmm.Name = "CallingOverrideScenario";
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "i"));
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Attributes = MemberAttributes.Public;
        cmm.Statements.Add(new CodeVariableDeclarationStatement("ClassWVirtualMethod", "t", new CodeCastExpression(new CodeTypeReference("ClassWVirtualMethod"), new CodeObjectCreateExpression("ClassWOverrideMethod"))));
        CodeMethodInvokeExpression methodinvoke = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("t"), "VirtualMethod");

        methodinvoke.Parameters.Add(new CodeArgumentReferenceExpression("i"));
        cmm.Statements.Add(new CodeMethodReturnStatement(methodinvoke));
        cd.Members.Add(cmm);

        // declare a method without parameters
        cmm            = new CodeMemberMethod();
        cmm.Name       = "NoParamsMethod";
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Attributes = MemberAttributes.Public | MemberAttributes.Final;
        cmm.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(16)));
        cd.Members.Add(cmm);

        // declare a method with multiple parameters
        cmm      = new CodeMemberMethod();
        cmm.Name = "MultipleParamsMethod";
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "a"));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "b"));
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Attributes = MemberAttributes.Public | MemberAttributes.Final;
        cmm.Statements.Add(new CodeMethodReturnStatement(new CodeBinaryOperatorExpression(new
                                                                                          CodeArgumentReferenceExpression("a"), CodeBinaryOperatorType.Add,
                                                                                          new CodeArgumentReferenceExpression("b"))));
        cd.Members.Add(cmm);

        // call method with no parameters, call a method with multiple parameters,
        // and call a method from a method call
        //         public virtual int CallParamsMethods() {
        //              TEST t = new TEST();
        //              int val;
        //              val = t.NoParamsMethod ();
        //              return t.MultipleParamsMethod(78, val);
        //         }
        AddScenario("CheckCallParamsMethod", "Check CheckCallParamsMethod.");
        cmm            = new CodeMemberMethod();
        cmm.Name       = "CallParamsMethods";
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Attributes = MemberAttributes.Public;
        cmm.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("TEST"), "t", new CodeObjectCreateExpression("TEST")));

        CodeVariableReferenceExpression cvre = new CodeVariableReferenceExpression();  //To increase code coverage

        cvre.VariableName = "t";

        CodeVariableReferenceExpression valCVRE = new CodeVariableReferenceExpression();

        valCVRE.VariableName = "val";

        cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "val"));
        cmm.Statements.Add(new CodeAssignStatement(valCVRE,
                                                   CDHelper.CreateMethodInvoke(new CodeVariableReferenceExpression("t"), "NoParamsMethod")));

        cmm.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(cvre,
                                                                                        "MultipleParamsMethod", new CodePrimitiveExpression(78), valCVRE)));
        cd.Members.Add(cmm);

        // method to test the 'new' scenario by calling the 'new' method
        // GENERATES (C#):
        //        public int CallingNewScenario(int i) {
        //            ClassWVirtualMethod t = new ClassWNewMethod();
        //            int x1;
        //            int x2;
        //            x1 = ((ClassWNewMethod)(t)).VirtualMethod(i);
        //            x2 = t.VirtualMethod(i);
        //            return (x1 - x2);
        //        }
        AddScenario("CheckCallingNewScenario", "Check CheckCallingNewScenario.");
        cmm      = new CodeMemberMethod();
        cmm.Name = "CallingNewScenario";
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "i"));
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Attributes = MemberAttributes.Public;

        cmm.Statements.Add(new CodeVariableDeclarationStatement("ClassWVirtualMethod", "t", new CodeCastExpression(new CodeTypeReference("ClassWVirtualMethod"), new CodeObjectCreateExpression("ClassWNewMethod"))));
        cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "x1"));
        cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "x2"));


        methodinvoke = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("t"), "VirtualMethod");
        methodinvoke.Parameters.Add(new CodeArgumentReferenceExpression("i"));

        CodeMethodInvokeExpression methodinvoke2 = new CodeMethodInvokeExpression(new CodeCastExpression("ClassWNewMethod", new
                                                                                                         CodeVariableReferenceExpression("t")), "VirtualMethod");

        methodinvoke2.Parameters.Add(new CodeArgumentReferenceExpression("i"));

        cmm.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("x1"),
                                                   methodinvoke2));

        cmm.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("x2"),
                                                   methodinvoke));

        cmm.Statements.Add(new CodeMethodReturnStatement(
                               CDHelper.CreateBinaryOperatorExpression("x1", CodeBinaryOperatorType.Subtract, "x2")));

        cd.Members.Add(cmm);


        // ***************** declare method using new ******************
        // first declare a class with a virtual method in it
        // GENERATES (C#):
        //         public class ClassWVirtualMethod {
        //                 public virtual int VirtualMethod(int a) {
        //                     return a;
        //                 }
        //         }
        cd         = new CodeTypeDeclaration("ClassWVirtualMethod");
        cd.IsClass = true;
        nspace.Types.Add(cd);
        cmm            = new CodeMemberMethod();
        cmm.Name       = "VirtualMethod";
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "a"));
        cmm.Attributes = MemberAttributes.Public;
        cmm.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("a")));
        cd.Members.Add(cmm);

        // now declare a class that inherits from the previous class and has a 'new' method with the
        // name VirtualMethod
        // GENERATES (C#):
        //    public class ClassWNewMethod : ClassWVirtualMethod {
        //         public new virtual int VirtualMethod(int a) {
        //             return (2 * a);
        //         }
        //     }
        cd = new CodeTypeDeclaration("ClassWNewMethod");
        cd.BaseTypes.Add(new CodeTypeReference("ClassWVirtualMethod"));
        cd.IsClass = true;
        nspace.Types.Add(cd);
        cmm            = new CodeMemberMethod();
        cmm.Name       = "VirtualMethod";
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "a"));
        cmm.Attributes = MemberAttributes.Public | MemberAttributes.New;
        cmm.Statements.Add(new CodeMethodReturnStatement(new CodeBinaryOperatorExpression(
                                                             new CodePrimitiveExpression(2), CodeBinaryOperatorType.Multiply
                                                             , new CodeArgumentReferenceExpression("a"))));
        cd.Members.Add(cmm);

        // now declare a class that inherits from the previous class and has a 'new' method with the
        // name VirtualMethod
        // GENERATES (C#):
        //            public class ClassWOverrideMethod : ClassWVirtualMethod {
        //                 public override int VirtualMethod(int a) {
        //                     return (2 * a);
        //                 }
        //             }
        cd = new CodeTypeDeclaration("ClassWOverrideMethod");
        cd.BaseTypes.Add(new CodeTypeReference("ClassWVirtualMethod"));
        cd.IsClass = true;
        nspace.Types.Add(cd);
        cmm            = new CodeMemberMethod();
        cmm.Name       = "VirtualMethod";
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "a"));
        cmm.Attributes = MemberAttributes.Public | MemberAttributes.Override;
        cmm.Statements.Add(new CodeMethodReturnStatement(new CodeBinaryOperatorExpression(
                                                             new CodePrimitiveExpression(2), CodeBinaryOperatorType.Multiply
                                                             , new CodeArgumentReferenceExpression("a"))));
        cd.Members.Add(cmm);

        //*************** overload member function ****************
        // new class which will include both functions
        // GENERATES (C#):
        //    public class TEST7 {
        //         public int OverloadedMethod(int a) {
        //             return a;
        //         }
        //         public int OverloadedMethod(int a, int b) {
        //             return (b + a);
        //         }
        //         public int CallingOverloadedMethods(int i) {
        //             int one = OverloadedMethod(i, i);
        //             int two = OverloadedMethod(i);
        //             return (one - two);
        //         }
        //     }
        AddScenario("CheckTEST7.CallingOverloadedMethods", "Check CallingOverloadedMethods()");
        cd         = new CodeTypeDeclaration("TEST7");
        cd.IsClass = true;
        nspace.Types.Add(cd);
        cmm            = new CodeMemberMethod();
        cmm.Name       = "OverloadedMethod";
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "a"));
        cmm.Attributes = MemberAttributes.Public;
        cmm.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("a")));
        cd.Members.Add(cmm);
        cmm            = new CodeMemberMethod();
        cmm.Name       = "OverloadedMethod";
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "a"));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "b"));
        cmm.Attributes = MemberAttributes.Public;
        cmm.Statements.Add(new CodeMethodReturnStatement(new CodeBinaryOperatorExpression(
                                                             new CodeArgumentReferenceExpression("b"), CodeBinaryOperatorType.Add,
                                                             new CodeArgumentReferenceExpression("a"))));
        cd.Members.Add(cmm);

        // declare a method that will call both OverloadedMethod functions
        cmm            = new CodeMemberMethod();
        cmm.Name       = "CallingOverloadedMethods";
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "i"));
        cmm.Attributes = MemberAttributes.Public;
        CodeMethodReferenceExpression methodref = new CodeMethodReferenceExpression();

        methodref.MethodName = "OverloadedMethod";

        cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "one",
                                                                new CodeMethodInvokeExpression(methodref, new
                                                                                               CodeArgumentReferenceExpression("i"), new CodeArgumentReferenceExpression("i"))));

        cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "two",
                                                                new CodeMethodInvokeExpression(methodref, new
                                                                                               CodeArgumentReferenceExpression("i"))));

        cmm.Statements.Add(new CodeMethodReturnStatement(
                               CDHelper.CreateBinaryOperatorExpression("one", CodeBinaryOperatorType.Subtract, "two")));
        cd.Members.Add(cmm);


        // GENERATES (C#):
        //
        //   namespace NSPC2 {
        //
        //
        //       public class TEST {
        //
        //           public virtual int CallingOverrideScenario(int i) {
        //               NSPC.ClassWVirtualMethod t = new NSPC.ClassWOverrideMethod();
        //               return t.VirtualMethod(i);
        //           }
        //       }
        //   }

        nspace = new CodeNamespace("NSPC2");
        cu.Namespaces.Add(nspace);

        cd         = new CodeTypeDeclaration("TEST");
        cd.IsClass = true;
        nspace.Types.Add(cd);

        AddScenario("Check2CallingOverrideScenario", "Check CallingOverrideScenario()");
        cmm      = new CodeMemberMethod();
        cmm.Name = "CallingOverrideScenario";
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "i"));
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Attributes = MemberAttributes.Public;
        cmm.Statements.Add(new CodeVariableDeclarationStatement("NSPC.ClassWVirtualMethod", "t", new CodeCastExpression(new CodeTypeReference("NSPC.ClassWVirtualMethod"), new CodeObjectCreateExpression("NSPC.ClassWOverrideMethod"))));
        methodinvoke = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("t"), "VirtualMethod");
        methodinvoke.Parameters.Add(new CodeArgumentReferenceExpression("i"));
        cmm.Statements.Add(new CodeMethodReturnStatement(methodinvoke));
        cd.Members.Add(cmm);
    }
Esempio n. 2
0
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        CodeNamespace nspace = new CodeNamespace("NSPC");

        nspace.Imports.Add(new CodeNamespaceImport("System"));
        cu.Namespaces.Add(nspace);


        // declare class with fields

        // GENERATES (C#):
        //    public class ClassWithFields {
        //        public int NonStaticPublicField = 6;
        //        private int PrivateField = 7;
        //        public int UsePrivateField(int i) {
        //            this.PrivateField = i;
        //            return this.PrivateField;
        //        }
        //    }
        CodeTypeDeclaration cd = new CodeTypeDeclaration("ClassWithFields");

        cd.IsClass = true;
        nspace.Types.Add(cd);

        CodeMemberField field = new CodeMemberField();

        field.Name           = "NonStaticPublicField";
        field.Attributes     = MemberAttributes.Public | MemberAttributes.Final;
        field.Type           = new CodeTypeReference(typeof(int));
        field.InitExpression = new CodePrimitiveExpression(6);
        cd.Members.Add(field);

        field                = new CodeMemberField();
        field.Name           = "PrivateField";
        field.Attributes     = MemberAttributes.Private | MemberAttributes.Final;
        field.Type           = new CodeTypeReference(typeof(int));
        field.InitExpression = new CodePrimitiveExpression(7);
        cd.Members.Add(field);

        // create a method to test access to private field
        CodeMemberMethod cmm = new CodeMemberMethod();

        cmm.Name       = "UsePrivateField";
        cmm.Attributes = MemberAttributes.Public | MemberAttributes.Final;
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(int)), "i"));
        cmm.Statements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "PrivateField"), new CodeArgumentReferenceExpression("i")));
        cmm.Statements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "PrivateField")));
        cd.Members.Add(cmm);

        // GENERATES (C#):
        //    public class TestFields {
        //        public int UseFields(int i) {
        //            ClassWithFields number = new ClassWithFields();
        //            int someSum;
        //            int privateField;
        //            someSum = number.NonStaticPublicField;
        //            privateField = number.UsePrivateField (i);
        //            return someSum + privateField;
        //        }
        //    }
        cd         = new CodeTypeDeclaration("TestFields");
        cd.IsClass = true;
        nspace.Types.Add(cd);

        AddScenario("CheckTestFields");
        cmm            = new CodeMemberMethod();
        cmm.Name       = "UseFields";
        cmm.Attributes = MemberAttributes.Public;
        cmm.ReturnType = new CodeTypeReference(typeof(int));
        cmm.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(int)), "i"));
        cmm.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("ClassWithFields"), "number",
                                                                new CodeObjectCreateExpression("ClassWithFields")));

        cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int),
                                                                "someSum"));
        cmm.Statements.Add(new CodeVariableDeclarationStatement(typeof(int),
                                                                "privateField"));

        cmm.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("someSum"),
                                                   CDHelper.CreateFieldRef("number", "NonStaticPublicField")));

        cmm.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("privateField"),
                                                   CDHelper.CreateMethodInvoke(new CodeVariableReferenceExpression("number"),
                                                                               "UsePrivateField", new CodeArgumentReferenceExpression("i"))));

        cmm.Statements.Add(new CodeMethodReturnStatement(CDHelper.CreateBinaryOperatorExpression("someSum",
                                                                                                 CodeBinaryOperatorType.Add, "privateField")));
        cd.Members.Add(cmm);
    }
Esempio n. 3
0
    public override void BuildTree(CodeDomProvider provider, CodeCompileUnit cu)
    {
        // create a namespace
        CodeNamespace ns = new CodeNamespace("NS");

        ns.Imports.Add(new CodeNamespaceImport("System"));
        ns.Imports.Add(new CodeNamespaceImport("System.Windows.Forms"));
        cu.ReferencedAssemblies.Add("System.Windows.Forms.dll");
        cu.Namespaces.Add(ns);

        // create a class
        CodeTypeDeclaration class1 = new CodeTypeDeclaration();

        class1.Name    = "Test";
        class1.IsClass = true;
        ns.Types.Add(class1);


        // create method to test casting enum -> int
        //     GENERATE (C#):
        //        public int EnumToInt(System.Windows.Forms.AnchorStyles enum1) {
        //            return ((int)(enum1));
        //        }
        AddScenario("CheckEnumToInt1", "Check the return value of EnumToInt() with a single flag");
        AddScenario("CheckEnumToInt2", "Check the return value of EnumToInt() with multiple flags");
        CodeMemberMethod enumToInt = new CodeMemberMethod();

        enumToInt.Name       = "EnumToInt";
        enumToInt.ReturnType = new CodeTypeReference(typeof(int));
        enumToInt.Attributes = MemberAttributes.Public;
        CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(System.Windows.Forms.AnchorStyles), "enum1");

        enumToInt.Parameters.Add(param);
        enumToInt.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(typeof(int), new CodeArgumentReferenceExpression("enum1"))));
        class1.Members.Add(enumToInt);

        // create method to test casting enum -> int
        //     GENERATE (C#):
        //       public virtual int CastReturnValue(string value) {
        //          float val = System.Single.Parse(value, System.Globalization.CultureInfo.InvariantCulture);
        //          return ((int) val);
        //       }
        AddScenario("CheckCastReturnValue", "Check the return value of CastReturnValue()");
        CodeMemberMethod castReturnValue = new CodeMemberMethod();

        castReturnValue.Name       = "CastReturnValue";
        castReturnValue.ReturnType = new CodeTypeReference(typeof(int));
        castReturnValue.Attributes = MemberAttributes.Public;
        CodeParameterDeclarationExpression strParam = new CodeParameterDeclarationExpression(typeof(string), "value");

        castReturnValue.Parameters.Add(strParam);
        castReturnValue.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "val",
                                                                            CDHelper.CreateMethodInvoke(new CodeTypeReferenceExpression(new CodeTypeReference("System.Int32")), // F#: Type conversion "int -> float" is not a type-cast!
                                                                                                        "Parse", new CodeArgumentReferenceExpression("value"),
                                                                                                        new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("System.Globalization.CultureInfo"),
                                                                                                                                            "InvariantCulture"))));
        castReturnValue.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(typeof(int), new CodeVariableReferenceExpression("val"))));
        class1.Members.Add(castReturnValue);


        // create method to test casting interface -> class
        //     GENERATE (C#):
        //        public string CastInterface(System.ICloneable value) {
        //            return ((string)(value));
        //        }
        AddScenario("CheckCastInterface", "Check the return value of CastInterface()");
        CodeMemberMethod castInterface = new CodeMemberMethod();

        castInterface.Name       = "CastInterface";
        castInterface.ReturnType = new CodeTypeReference(typeof(string));
        castInterface.Attributes = MemberAttributes.Public;
        CodeParameterDeclarationExpression interfaceParam = new CodeParameterDeclarationExpression(typeof(System.ICloneable), "value");

        castInterface.Parameters.Add(interfaceParam);
        castInterface.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(typeof(string), new CodeArgumentReferenceExpression("value"))));
        class1.Members.Add(castInterface);

        // create method to test casting value type -> reference type
        //     GENERATE (C#):
        //         public object ValueToReference(int value) {
        //             return ((object)(value));
        //         }
        AddScenario("CheckValueToReference", "Check the return value of ValueToReference()");
        CodeMemberMethod valueToReference = new CodeMemberMethod();

        valueToReference.Name       = "ValueToReference";
        valueToReference.ReturnType = new CodeTypeReference(typeof(System.Object));
        valueToReference.Attributes = MemberAttributes.Public;
        CodeParameterDeclarationExpression valueParam = new CodeParameterDeclarationExpression(typeof(int), "value");

        valueToReference.Parameters.Add(valueParam);
        valueToReference.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(typeof(System.Object), new CodeArgumentReferenceExpression("value"))));
        class1.Members.Add(valueToReference);
    }