Beispiel #1
0
        public void MethodWithVariableDeclarations()
        {
            string source = @"
                public void Method() {
                    string var1;
                    int var2;
                }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedMethodASTWalker.Create(methodDeclarationNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.MethodDeclaration);

            // Checking members
            Assert.IsNotNull(astWalker.MethodDeclaration.Statements);
            Assert.IsTrue(astWalker.MethodDeclaration.Statements.Count() > 0);
            Assert.AreEqual(2, astWalker.MethodDeclaration.Statements.Count());

            Assert.IsInstanceOfType(astWalker.MethodDeclaration.Statements.ElementAt(0), typeof(StatementTranslationUnit));
            Assert.IsInstanceOfType(astWalker.MethodDeclaration.Statements.ElementAt(1), typeof(StatementTranslationUnit));
        }
Beispiel #2
0
        public void EmptyMethodWithNoParameters()
        {
            string source = @"
                public void Method() { }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedMethodASTWalker.Create(methodDeclarationNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.MethodDeclaration);

            // Checking members
            Assert.IsNotNull(astWalker.MethodDeclaration.Arguments);
            Assert.AreEqual(0, astWalker.MethodDeclaration.Arguments.Count());
        }
Beispiel #3
0
        public void Properties()
        {
            string source = @"
                public interface MyInterface {
                    int Property1 { get; set; }
                    string Property2 { get; set; }
                }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(InterfaceDeclarationSyntax));
            InterfaceDeclarationSyntax interfaceDeclarationNode = node as InterfaceDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedInterfaceASTWalker.Create(interfaceDeclarationNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.InterfaceDeclaration);

            // Checking signatures
            Assert.IsNotNull(astWalker.InterfaceDeclaration.Signatures);
            Assert.IsTrue(astWalker.InterfaceDeclaration.Signatures.Count() > 0);
            Assert.AreEqual(2, astWalker.InterfaceDeclaration.Signatures.Count());

            Assert.IsInstanceOfType(astWalker.InterfaceDeclaration.Signatures.ElementAt(0), typeof(MethodSignatureDeclarationTranslationUnit));
            Assert.IsInstanceOfType(astWalker.InterfaceDeclaration.Signatures.ElementAt(1), typeof(MethodSignatureDeclarationTranslationUnit));
        }
Beispiel #4
0
        public void EmptyMethodWith3Parameters()
        {
            string source = @"
                public void Method(string param1, int param2, bool param3) { }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedMethodASTWalker.Create(methodDeclarationNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.MethodDeclaration);

            // Checking members
            Assert.IsNotNull(astWalker.MethodDeclaration.Arguments);
            Assert.IsTrue(astWalker.MethodDeclaration.Arguments.Count() > 0);
            Assert.AreEqual(3, astWalker.MethodDeclaration.Arguments.Count());

            Assert.IsInstanceOfType(astWalker.MethodDeclaration.Arguments.ElementAt(0), typeof(ArgumentDefinitionTranslationUnit));
            Assert.IsInstanceOfType(astWalker.MethodDeclaration.Arguments.ElementAt(1), typeof(ArgumentDefinitionTranslationUnit));
            Assert.IsInstanceOfType(astWalker.MethodDeclaration.Arguments.ElementAt(2), typeof(ArgumentDefinitionTranslationUnit));
        }
Beispiel #5
0
        public void WalkASTDeep()
        {
            string source = @"
                namespace MyNamespaceA {
                    public class MyClassA { }
                    public class MyClassB { }

                    namespace MyNamespaceB {
                        public class MyClassC { }
                        public class MyClassD { }
                    }
                }
            ";

            int traversedClassesCount = 0;

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);
            var node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax)) as CSharpSyntaxNode;

            // Calling the walker
            var astWalker = new MultiPurposeASTWalker(node,
                                                      syntaxNode => syntaxNode as ClassDeclarationSyntax != null,
                                                      delegate
            {
                traversedClassesCount++;
            },
                                                      false);

            astWalker.Start();

            // Checking
            Assert.AreEqual(4, traversedClassesCount, "Expected walker to deep traverse AST!");
        }
Beispiel #6
0
        public void Methods()
        {
            string source = @"
                public class MyClass {
                    private void Method1() {
                    }

                    protected void Method2() {
                    }
                }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode             node = new NodeLocator(tree).LocateFirst(typeof(ClassDeclarationSyntax));
            ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedClassASTWalker.Create(classDeclarationNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.ClassDeclaration);

            // Checking members
            Assert.IsNotNull(astWalker.ClassDeclaration.MethodDeclarations);
            Assert.IsTrue(astWalker.ClassDeclaration.MethodDeclarations.Count() > 0);
            Assert.AreEqual(2, astWalker.ClassDeclaration.MethodDeclarations.Count());

            Assert.IsInstanceOfType(astWalker.ClassDeclaration.MethodDeclarations.ElementAt(0), typeof(MethodDeclarationTranslationUnit));
            Assert.IsInstanceOfType(astWalker.ClassDeclaration.MethodDeclarations.ElementAt(1), typeof(MethodDeclarationTranslationUnit));
        }
Beispiel #7
0
        public void OneClass()
        {
            string source = @"
                public class Class1 {
                }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode            node        = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax));
            CompilationUnitSyntax programNode = node as CompilationUnitSyntax;

            // Creating the walker
            var astWalker = MockedProgramASTWalker.Create(programNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.Program);

            // Checking members
            Assert.IsNotNull(astWalker.Program.Content);
            Assert.IsTrue(astWalker.Program.Content.Count() > 0);
            Assert.AreEqual(1, astWalker.Program.Content.Count());

            Assert.IsInstanceOfType(astWalker.Program.Content.ElementAt(0), typeof(ClassDeclarationTranslationUnit));
        }
Beispiel #8
0
        private static void Test(string source)
        {
            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(EnumDeclarationSyntax));
            var        enumDeclarationNode = node as EnumDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedEnumASTWalker.Create(enumDeclarationNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.EnumDeclaration);

            // Checking signatures
            Assert.IsNotNull(astWalker.EnumDeclaration.Members);
            Assert.IsTrue(astWalker.EnumDeclaration.Members.Count() > 0);
            Assert.AreEqual(3, astWalker.EnumDeclaration.Members.Count());

            Assert.IsInstanceOfType(astWalker.EnumDeclaration.Members.ElementAt(0), typeof(EnumMemberTranslationUnit));
            Assert.IsInstanceOfType(astWalker.EnumDeclaration.Members.ElementAt(1), typeof(EnumMemberTranslationUnit));
            Assert.IsInstanceOfType(astWalker.EnumDeclaration.Members.ElementAt(2), typeof(EnumMemberTranslationUnit));
        }
Beispiel #9
0
        public void EmptyMethodWith2Parameters()
        {
            string source = @"
                class Class1 {
                    public Class1(string param1, int param2) { }
                }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(ConstructorDeclarationSyntax));
            ConstructorDeclarationSyntax constructorDeclarationNode = node as ConstructorDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedConstructorASTWalker.Create(constructorDeclarationNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.ConstructorDeclaration);

            // Checking members
            Assert.IsNotNull(astWalker.ConstructorDeclaration.Arguments);
            Assert.IsTrue(astWalker.ConstructorDeclaration.Arguments.Count() > 0);
            Assert.AreEqual(2, astWalker.ConstructorDeclaration.Arguments.Count());

            Assert.IsInstanceOfType(astWalker.ConstructorDeclaration.Arguments.ElementAt(0), typeof(ArgumentDefinitionTranslationUnit));
            Assert.IsInstanceOfType(astWalker.ConstructorDeclaration.Arguments.ElementAt(1), typeof(ArgumentDefinitionTranslationUnit));
        }
Beispiel #10
0
        private static MockedClassDefinitionASTWalker ParseClass(string source, bool generateTranslationUniOnProtectedMembers = false)
        {
            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(ClassDeclarationSyntax));
            var        classDeclarationNode = node as ClassDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedClassDefinitionASTWalker.Create(classDeclarationNode, generateTranslationUniOnProtectedMembers);

            // Getting the translation unit
            astWalker.Walk();

            return(astWalker);
        }
Beispiel #11
0
        private static string GetTranslation(string source)
        {
            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode            node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax));
            CompilationUnitSyntax compilationUnitNode = node as CompilationUnitSyntax;

            // Creating the walker
            var astWalker = ProgramDefinitionASTWalker.Create(compilationUnitNode);

            // Getting the translation unit
            ITranslationUnit translationUnit = astWalker.Walk();

            return(translationUnit.Translate());
        }
Beispiel #12
0
        private static string GetTranslation(string source)
        {
            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateLast(typeof(InterfaceDeclarationSyntax));
            InterfaceDeclarationSyntax interfaceDeclarationNode = node as InterfaceDeclarationSyntax;

            // Creating the walker
            var astWalker = InterfaceASTWalker.Create(interfaceDeclarationNode);

            // Getting the translation unit
            ITranslationUnit translationUnit = astWalker.Walk();

            return(translationUnit.Translate());
        }
        public void Empty2IfsElseStatementWithBlocks()
        {
            string source = @"
                public class Class1 {
                    public void Method1() {
                        if (true) {
                        } else if (false) {
                        } else {
                        }
                    }
                }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode        node            = new NodeLocator(tree).LocateFirst(typeof(IfStatementSyntax));
            IfStatementSyntax ifStatementNode = node as IfStatementSyntax;

            // Creating the walker
            var astWalker = MockedConditionalStatementASTWalker.Create(ifStatementNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.Statement);

            // Checking members
            Assert.IsNotNull(astWalker.Statement.Bodies);
            Assert.IsTrue(astWalker.Statement.Bodies.Count() > 0);
            Assert.AreEqual(2, astWalker.Statement.Bodies.Count());

            Assert.IsInstanceOfType(astWalker.Statement.Bodies.ElementAt(0), typeof(StatementsGroupTranslationUnit));
            Assert.IsInstanceOfType(astWalker.Statement.Bodies.ElementAt(1), typeof(StatementsGroupTranslationUnit));

            Assert.IsNotNull(astWalker.Statement.TestExpressions);
            Assert.IsTrue(astWalker.Statement.TestExpressions.Count() > 0);
            Assert.AreEqual(2, astWalker.Statement.TestExpressions.Count());

            Assert.IsInstanceOfType(astWalker.Statement.TestExpressions.ElementAt(0), typeof(LiteralTranslationUnit <bool>));
            Assert.IsInstanceOfType(astWalker.Statement.TestExpressions.ElementAt(1), typeof(LiteralTranslationUnit <bool>));

            Assert.IsNotNull(astWalker.Statement.LastBody);

            Assert.IsInstanceOfType(astWalker.Statement.LastBody, typeof(StatementsGroupTranslationUnit));
        }
Beispiel #14
0
        public void WalkASTOnlyRootLevel()
        {
            string source = @"
                namespace MyNamespaceA {
                    public class MyClassA { }
                    public class MyClassB { }

                    namespace MyNamespaceB {
                        public class MyClassC { }
                        public class MyClassD { }
                    }
                }
                namespace MyNamespaceC { }
            ";

            int traversedClassesCount    = 0;
            int traversedNamespacesCount = 0;

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);
            var node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax)) as CSharpSyntaxNode;

            // Calling the walker
            var astClassWalker = new MultiPurposeASTWalker(node,
                                                           syntaxNode => syntaxNode as ClassDeclarationSyntax != null,
                                                           delegate
            {
                traversedClassesCount++;
            },
                                                           true);

            astClassWalker.Start();

            //var astNamespaceWalker = new MultiPurposeASTWalker(node,
            //    syntaxNode => syntaxNode as NamespaceDeclarationSyntax != null,
            //    delegate
            //    {
            //        traversedNamespacesCount++;
            //    },
            //    true);
            //astNamespaceWalker.Start();

            // Checking
            Assert.AreEqual(0, traversedClassesCount, "Expected walker to root level traverse AST!");
            //Assert.AreEqual(2, traversedNamespacesCount, "Expected walker to root level traverse AST!");
        }
Beispiel #15
0
        public string RenderSimpleEmptyClass()
        {
            var    sourceInfo = SourceGenerator.Generate(SourceOptions.None);
            string source     = sourceInfo.Key;

            // Getting the AST node
            CSharpSyntaxTree       tree = ASTExtractor.Extract(source);
            SyntaxNode             node = new NodeLocator(tree).LocateLast(typeof(ClassDeclarationSyntax));
            ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax;

            // Creating the walker
            var astWalker = ClassASTWalker.Create(classDeclarationNode);

            // Getting the translation unit
            ITranslationUnit translationUnit = astWalker.Walk();

            return(translationUnit.Translate());
        }
Beispiel #16
0
        private static string GetTranslation(string source)
        {
            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            var node = new NodeLocator(tree).LocateLast(typeof(CompilationUnitSyntax)) as CSharpSyntaxNode;

            // Transforming
            new ScriptNamespaceBasedASTTransformer().Transform(ref tree);

            var programNode = node as CompilationUnitSyntax;

            // Creating the walker
            var astWalker = ProgramDefinitionASTWalker.Create(programNode);

            // Getting the translation unit
            ITranslationUnit translationUnit = astWalker.Walk();

            return(translationUnit.Translate());
        }
Beispiel #17
0
        public void ClassDeclarations()
        {
            string source = @"
                namespace MyNamespace {
                    public class Class1 {
                    }
                    private class Class2 {
                    }
                    internal class Class3 {
                    }
                }
            ";

            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

            SyntaxNode node = new NodeLocator(tree).LocateFirst(typeof(NamespaceDeclarationSyntax));
            NamespaceDeclarationSyntax namespaceNode = node as NamespaceDeclarationSyntax;

            // Creating the walker
            var astWalker = MockedNamespaceASTWalker.Create(namespaceNode);

            // Getting the translation unit
            astWalker.Walk();

            // Checking
            Assert.IsNotNull(astWalker.Module);

            // Checking members
            Assert.IsNotNull(astWalker.Module.Classes);
            Assert.IsTrue(astWalker.Module.Classes.Count() > 0);
            Assert.AreEqual(3, astWalker.Module.Classes.Count());

            Assert.IsInstanceOfType(astWalker.Module.Classes.ElementAt(0), typeof(ClassDeclarationTranslationUnit));
            Assert.IsInstanceOfType(astWalker.Module.Classes.ElementAt(1), typeof(ClassDeclarationTranslationUnit));
            Assert.IsInstanceOfType(astWalker.Module.Classes.ElementAt(2), typeof(ClassDeclarationTranslationUnit));
        }
Beispiel #18
0
        protected override void InitializeCore()
        {
            // TODO: In order to target #41, add an option for using the reflector when requested

            // Getting the AST node
            this.tree = ASTExtractor.Extract(this.source);

            // Loading the semantic model
            CSharpCompilation compilation = null;

            if (this.assemblyPath != null)
            {
                compilation = this.GetCompilation(this.assemblyPath, this.tree);
            }

            IASTTransformer transformer = new ScriptNamespaceBasedASTTransformer();

            if (compilation != null)
            {
                transformer.Transform(ref this.tree, ref compilation);
                this.semanticModel = SemanticUtils.RetrieveSemanticModel(compilation, this.tree);
            }
            else
            {
                transformer.Transform(ref this.tree);
            }

            // Creating the walker
            // If no semantic model was loaded, null will just be passed
            var node = this.tree.GetRoot();

            this.walker = ProgramDefinitionASTWalker.Create(node, null, this.semanticModel);
            (this.walker as ProgramDefinitionASTWalker).Logger = this.Logger;

            // Translating
            this.output = this.walker.Walk().Translate();
        }
Beispiel #19
0
        private void Initialize()
        {
            // Getting the AST node
            this.tree = ASTExtractor.Extract(this.source);

            // Loading the semantic model
            CSharpCompilation compilation = null;

            if (this.assemblyPath != null)
            {
                compilation = this.GetCompilation(this.assemblyPath, this.tree);
            }

            IASTTransformer transformer = new ScriptNamespaceBasedASTTransformer();

            if (compilation != null)
            {
                transformer.Transform(ref this.tree, ref compilation);
                this.semanticModel = SemanticUtils.RetrieveSemanticModel(compilation, this.tree);
            }
            else
            {
                transformer.Transform(ref this.tree);
            }

            // Creating the walker
            // If no semantic model was loaded, null will just be passed
            var node = this.tree.GetRoot();

            this.walker = ProgramDefinitionASTWalker.Create(node, null, this.semanticModel);

            // Translating
            this.output = this.walker.Walk().Translate();

            this.initialized = true;
        }