public static ClassDeclarationSyntax Convert(ClassDeclarationSyntax node, RequiredUsings requires)
        {
            var tearDownMethod = node.FindMethodWithAttribute("TearDown").SingleOrDefault();

            // this class has no teardown method
            if (tearDownMethod == null)
            {
                return(node);
            }

            requires.System = true;

            var disposeMethod = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("Dispose"))
                                .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
                                .NormalizeWhitespace()
                                .WithTriviaFrom(tearDownMethod)
                                .WithBody(tearDownMethod.Body);

            return(node
                   .ReplaceNode(tearDownMethod, disposeMethod)
                   .WithIdentifier(
                       node.Identifier.WithTrailingTrivia(Whitespace(" "))
                       )
                   .WithBaseList(node.BaseList?.NormalizeWhitespace())
                   .AddBaseListTypes(
                       SimpleBaseType(IdentifierName("IDisposable"))
                       .WithLeadingTrivia(Whitespace(" "))
                       .WithTrailingTrivia(Whitespace(Environment.NewLine))
                       ));
        }
Esempio n. 2
0
        public static ClassDeclarationSyntax Convert(ClassDeclarationSyntax node)
        {
            var setUpMethod = node.FindMethodWithAttribute("SetUp").SingleOrDefault();

            // this class has no setup method
            if (setUpMethod == null)
            {
                return(node);
            }

            // create a constructor
            var constructor = ConstructorDeclaration(Identifier(node.Identifier.Text))
                              .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword))).NormalizeWhitespace()
                              .WithTriviaFrom(setUpMethod)
                              .WithBody(setUpMethod.Body);

            return(node.ReplaceNode(setUpMethod, constructor));
        }