Exemple #1
0
        private List <TestClassTemplate> GetTestTemplates(string sourceCode)
        {
            SyntaxProcessor          syntaxProcessor     = new SyntaxProcessor();
            SyntaxProcessResult      syntaxProcessResult = syntaxProcessor.Process(sourceCode);
            List <TestClassTemplate> result = new List <TestClassTemplate>();

            foreach (ClassInformation classInfo in syntaxProcessResult.Classes)
            {
                NamespaceDeclarationSyntax namespaceDeclaration = NamespaceDeclaration(
                    QualifiedName(
                        IdentifierName(classInfo.NamespaceName),
                        IdentifierName("Tests")));
                CompilationUnitSyntax testClass = CompilationUnit()
                                                  .WithUsings(GetTemplateUsings(classInfo))
                                                  .WithMembers(SingletonList <MemberDeclarationSyntax>(namespaceDeclaration
                                                                                                       .WithMembers(SingletonList <MemberDeclarationSyntax>(ClassDeclaration(classInfo.Name + "Tests")
                                                                                                                                                            .WithAttributeLists(
                                                                                                                                                                SingletonList(
                                                                                                                                                                    AttributeList(
                                                                                                                                                                        SingletonSeparatedList(
                                                                                                                                                                            Attribute(
                                                                                                                                                                                IdentifierName("TestClass"))))))
                                                                                                                                                            .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
                                                                                                                                                            .WithMembers(GetTestMethods(classInfo.Methods))))));
                string fileName  = classInfo.Name + "Tests.cs";
                string innerText = testClass.NormalizeWhitespace().ToFullString();
                result.Add(new TestClassTemplate(fileName, innerText));
            }
            return(result);
        }
        public SyntaxProcessResult Process(string sourceCode)
        {
            tree = CSharpSyntaxTree.ParseText(sourceCode);
            root = tree.GetRoot();
            SyntaxProcessResult syntaxRes = new SyntaxProcessResult(GetClasses());

            return(syntaxRes);
        }