示例#1
0
        private void AddGeneratedMethod(CodeTypeDeclaration generatedClass, MultiDirectionTestMethod testMethod)
        {
            foreach (var transferDirection in testMethod.GetTransferDirections())
            {
                string generatedMethodName = this.GetGeneratedMethodName(testMethod, transferDirection);

                CodeMemberMethod generatedMethod = new CodeMemberMethod();
                generatedMethod.Name       = generatedMethodName;
                generatedMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;

                // Add TestCategoryAttribute to the generated method
                this.AddTestCategoryAttributes(generatedMethod, testMethod);
                this.AddTestCategoryAttribute(generatedMethod, MultiDirectionTag.MultiDirection);
                foreach (var tag in transferDirection.GetTags())
                {
                    this.AddTestCategoryAttribute(generatedMethod, tag);
                }

                CodeAttributeDeclaration testMethodAttribute = new CodeAttributeDeclaration(
                    new CodeTypeReference(typeof(TestMethodAttribute)));

                generatedMethod.CustomAttributes.Add(testMethodAttribute);

                foreach (var statement in transferDirection.EnumerateUpdateContextStatements())
                {
                    generatedMethod.Statements.Add(statement);
                }

                CodeMethodReferenceExpression callee = new CodeMethodReferenceExpression(
                    new CodeBaseReferenceExpression(), testMethod.MethodInfoObj.Name);
                CodeMethodInvokeExpression invokeExp = new CodeMethodInvokeExpression(callee);
                generatedMethod.Statements.Add(invokeExp);
                generatedClass.Members.Add(generatedMethod);
            }
        }
        private void AddTestCategoryAttributes(CodeMemberMethod method, MultiDirectionTestMethod testMethod)
        {
            foreach (var customAttribute in testMethod.MethodInfoObj.CustomAttributes)
            {
                if (customAttribute.AttributeType == typeof(MSUnittest.TestCategoryAttribute))
                {
                    if (customAttribute.ConstructorArguments.Count != 1)
                    {
                        // Unrecognized attribute, skip
                        continue;
                    }

                    this.AddTestCategoryAttribute(
                        method,
                        new CodeSnippetExpression(customAttribute.ConstructorArguments[0].ToString()));
                }
            }
        }
        private void AddGeneratedMethod(CodeTypeDeclaration generatedClass, MultiDirectionTestMethod testMethod)
        {
            foreach (var transferDirection in testMethod.GetTransferDirections())
            {
                string generatedMethodName = this.GetGeneratedMethodName(testMethod, transferDirection);

                CodeMemberMethod generatedMethod = new CodeMemberMethod();
                generatedMethod.Name       = generatedMethodName;
                generatedMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;

                // Add TestCategoryAttribute to the generated method
                this.AddTestCategoryAttributes(generatedMethod, testMethod);
                this.AddTestCategoryAttribute(generatedMethod, MultiDirectionTag.MultiDirection);
                foreach (var tag in transferDirection.GetTags())
                {
                    this.AddTestCategoryAttribute(generatedMethod, tag);
                }

                CodeAttributeDeclaration testMethodAttribute = null;

                testMethodAttribute = new CodeAttributeDeclaration(
                    new CodeTypeReference(typeof(MSUnittest.TestMethodAttribute)));

                generatedMethod.CustomAttributes.Add(testMethodAttribute);

                if (Program.FrameWorkType == FrameworkType.DNetCore)
                {
                    // add TestStartEndAttribute to ensure Test.Start and Test.End will be called as expected
                    generatedMethod.CustomAttributes.Add(new CodeAttributeDeclaration("TestStartEnd"));
                }

                foreach (var statement in TransferDirectionExtensions.EnumerateUpdateContextStatements(transferDirection as DMLibTransferDirection))
                {
                    generatedMethod.Statements.Add(statement);
                }

                CodeMethodReferenceExpression callee = new CodeMethodReferenceExpression(
                    new CodeBaseReferenceExpression(), testMethod.MethodInfoObj.Name);
                CodeMethodInvokeExpression invokeExp = new CodeMethodInvokeExpression(callee);
                generatedMethod.Statements.Add(invokeExp);
                generatedClass.Members.Add(generatedMethod);
            }
        }
 private string GetGeneratedMethodName(MultiDirectionTestMethod testMethod, TestMethodDirection transferDirection)
 {
     // [MethodName]_[DirectionSuffix]
     return(String.Format("{0}_{1}", testMethod.MethodInfoObj.Name, transferDirection.GetTestMethodNameSuffix()));
 }
 private string GetGeneratedMethodName(MultiDirectionTestMethod testMethod, TestMethodDirection transferDirection)
 {
     // [MethodName]_[DirectionSuffix]
     return String.Format("{0}_{1}", testMethod.MethodInfoObj.Name, transferDirection.GetTestMethodNameSuffix());
 }
        private void AddTestCategoryAttributes(CodeMemberMethod method, MultiDirectionTestMethod testMethod)
        {
            foreach (var customAttribute in testMethod.MethodInfoObj.CustomAttributes)
            {
                if (customAttribute.AttributeType == typeof(TestCategoryAttribute))
                {
                    if (customAttribute.ConstructorArguments.Count != 1)
                    {
                        // Unrecognized attribute, skip
                        continue;
                    }

                    this.AddTestCategoryAttribute(
                        method,
                        new CodeSnippetExpression(customAttribute.ConstructorArguments[0].ToString()));
                }
            }
        }
        private void AddGeneratedMethod(CodeTypeDeclaration generatedClass, MultiDirectionTestMethod testMethod)
        {
            foreach (var transferDirection in testMethod.GetTransferDirections())
            {
                string generatedMethodName = this.GetGeneratedMethodName(testMethod, transferDirection);

                CodeMemberMethod generatedMethod = new CodeMemberMethod();
                generatedMethod.Name = generatedMethodName;
                generatedMethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;

                // Add TestCategoryAttribute to the generated method
                this.AddTestCategoryAttributes(generatedMethod, testMethod);
                this.AddTestCategoryAttribute(generatedMethod, MultiDirectionTag.MultiDirection);
                foreach (var tag in transferDirection.GetTags())
                {
                    this.AddTestCategoryAttribute(generatedMethod, tag);
                }

                CodeAttributeDeclaration testMethodAttribute = new CodeAttributeDeclaration(
                    new CodeTypeReference(typeof(TestMethodAttribute)));

                generatedMethod.CustomAttributes.Add(testMethodAttribute);

                foreach (var statement in transferDirection.EnumerateUpdateContextStatements())
                {
                    generatedMethod.Statements.Add(statement);
                }

                CodeMethodReferenceExpression callee = new CodeMethodReferenceExpression(
                    new CodeBaseReferenceExpression(), testMethod.MethodInfoObj.Name);
                CodeMethodInvokeExpression invokeExp = new CodeMethodInvokeExpression(callee);
                generatedMethod.Statements.Add(invokeExp);
                generatedClass.Members.Add(generatedMethod);
            }
        }