Locates nodes in an AST.
예제 #1
0
        public void MethodName()
        {
            string[] methodExpectedNames = new string[]
            {
                TestSuite.Class11.Value["Method1Name"],
                TestSuite.Class11.Value["Method2Name"],
                TestSuite.Class11.Value["Method3Name"],
                TestSuite.Class11.Value["Method4Name"],
                TestSuite.Class11.Value["Method5Name"],
                TestSuite.Class11.Value["Method6Name"],
                TestSuite.Class11.Value["Method7Name"],
            };
            IEnumerable<KeyValuePair<SyntaxNode, string>> items =
                new NodeLocator(Class11SyntaxTree).LocateAll(typeof(MethodDeclarationSyntax))
                .Select((node, k) => new KeyValuePair<SyntaxNode, string>(node, methodExpectedNames[k]));

            foreach (KeyValuePair<SyntaxNode, string> item in items)
            {
                Assert.IsNotNull(item.Key, string.Format("Node of type `{0}` should be found!",
                    typeof(MethodDeclarationSyntax).Name));

                MethodDeclarationSyntax methodDeclarationNode = item.Key as MethodDeclarationSyntax;

                TestRetrieveMethodName(methodDeclarationNode, item.Value);
            }
        }
예제 #2
0
        public void GetOnlyProperties()
        {
            string source = @"
                public interface MyInterface {
                    int Property1 { get; }
                    string Property2 { get; }
                }
            ";

            // 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));
        }
예제 #3
0
        public void DetectVoidType()
        {
            var tree = CSharpSyntaxTree.ParseText(@"
            using System;
            public class MyClass {
                public void Method1() { }
                public int Method2() { }
            }
            ");

            // First method
            var node = new NodeLocator(tree).LocateFirst(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            var methodDeclarationNode = node as MethodDeclarationSyntax;
            var helper = new MethodDeclaration(methodDeclarationNode).ReturnType;

            Assert.IsTrue(helper.IsVoid, "Expected void type!");

            // Second method
            node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            methodDeclarationNode = node as MethodDeclarationSyntax;
            helper = new MethodDeclaration(methodDeclarationNode).ReturnType;

            Assert.IsFalse(helper.IsVoid, "Void type not expected!");
        }
예제 #4
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));
        }
예제 #5
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));
        }
예제 #6
0
        public void ConstructorWithVariableDeclarations()
        {
            string source = @"
                class Class1 {
                    public Class1() {
                        string var1;
                        int var2;
                    }
                }
            ";

            // 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.Statements);
            Assert.IsTrue(astWalker.ConstructorDeclaration.Statements.Count() > 0);
            Assert.AreEqual(2, astWalker.ConstructorDeclaration.Statements.Count());

            Assert.IsInstanceOfType(astWalker.ConstructorDeclaration.Statements.ElementAt(0), typeof(StatementTranslationUnit));
            Assert.IsInstanceOfType(astWalker.ConstructorDeclaration.Statements.ElementAt(1), typeof(StatementTranslationUnit));
        }
예제 #7
0
        public void FieldMembers()
        {
            string source = @"
                public class MyClass {
                    private int myInt;
                    public string MyString;
                }
            ";

            // 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.MemberDeclarations);
            Assert.IsTrue(astWalker.ClassDeclaration.MemberDeclarations.Count() > 0);
            Assert.AreEqual(2, astWalker.ClassDeclaration.MemberDeclarations.Count());

            Assert.IsInstanceOfType(astWalker.ClassDeclaration.MemberDeclarations.ElementAt(0), typeof(FieldDeclarationTranslationUnit));
            Assert.IsInstanceOfType(astWalker.ClassDeclaration.MemberDeclarations.ElementAt(1), typeof(FieldDeclarationTranslationUnit));
        }
        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!");
        }
예제 #9
0
        public void EmptyMethodWith2Parameters()
        {
            string source = @"
                public void Method(string param1, int param2) { }
            ";

            // 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(2, astWalker.MethodDeclaration.Arguments.Count());

            Assert.IsInstanceOfType(astWalker.MethodDeclaration.Arguments.ElementAt(0), typeof(ArgumentDefinitionTranslationUnit));
            Assert.IsInstanceOfType(astWalker.MethodDeclaration.Arguments.ElementAt(1), typeof(ArgumentDefinitionTranslationUnit));
        }
예제 #10
0
        public void BaseClassNameFromClass()
        {
            SyntaxNode node = new NodeLocator(Class1SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(ClassDeclarationSyntax).Name));

            ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax;

            TestRetrieveBaseClassName(classDeclarationNode, Class1SemanticModel, null);
        }
예제 #11
0
        public void BaseClassNameFromClassWithInheritance()
        {
            SyntaxNode node = new NodeLocator(Class2SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(ClassDeclarationSyntax).Name));

            ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax;

            TestRetrieveBaseClassName(classDeclarationNode, Class2SemanticModel, TestSuite.Class2.Value["BaseClassName"]);
        }
예제 #12
0
        public void InterfaceTypeNameFromBaseList()
        {
            SyntaxNode node = new NodeLocator(Class3SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(ClassDeclarationSyntax).Name));

            ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax;
            BaseTypeSyntax baseTypeNode = classDeclarationNode.BaseList.Types.FirstOrDefault();

            TestRetrieveTypeName(baseTypeNode, TestSuite.Class3.Value["Interface1Name"]);
        }
예제 #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static T GetNode(string source)
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(source);

            var node = new NodeLocator(syntaxTree).LocateFirst(typeof(T));

            if (node == null)
            {
                throw new InvalidOperationException($"Node of type `{typeof(T).Name}` should be found!");
            }

            return(node as T);
        }
예제 #14
0
파일: Enums.cs 프로젝트: andry-tino/Rosetta
        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();
        }
        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));
        }
예제 #16
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();
        }
예제 #17
0
        public void DecoratingClass()
        {
            SyntaxTree tree = CSharpSyntaxTree.ParseText(@"
            using System;
            namespace Namespace1
            {
                [Obsolete]
                public class Class1 { }
                public class Class2 {
                    public Class1 Method1() { return null; }
                }
            }"
            );

            // Second class
            var node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            // Loading MSCoreLib
            var compilation = CSharpCompilation.Create("TestAssembly")
                  .AddReferences(
                     MetadataReference.CreateFromFile(
                       typeof(object).Assembly.Location))
                  .AddSyntaxTrees(tree);
            var semanticModel = compilation.GetSemanticModel(tree);

            var methodDeclarationNode = node as MethodDeclarationSyntax;
            var helper = new MethodDeclaration(methodDeclarationNode, semanticModel).ReturnType;

            var attributes = helper.Attributes;
            Assert.IsNotNull(attributes);
            Assert.AreNotEqual(0, attributes.Count(), "Expecting attributes!");

            var attribute = attributes.First();
            Assert.AreEqual("ObsoleteAttribute", attribute.AttributeClassName, "Wrong name!");
            Assert.AreEqual("System.ObsoleteAttribute", attribute.AttributeClassFullName, "Wrong full name!");
            Assert.IsNotNull(attribute.ConstructorArguments);
            Assert.AreEqual(0, attribute.ConstructorArguments.Count(), "Expected no constructor arguments");
        }
예제 #18
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));
        }
예제 #19
0
        private static void RetrieveNumberOfBodiesAndTest(SyntaxTree syntaxTree, int expectedNumberOfBodies)
        {
            SyntaxNode node = new NodeLocator(syntaxTree).LocateFirst(typeof(IfStatementSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(IfStatementSyntax).Name));

            IfStatementSyntax conditionalNode = node as IfStatementSyntax;

            TestRetrievedNumberOfBodies(conditionalNode, expectedNumberOfBodies);
        }
예제 #20
0
        private static void DetermineLastElseAndTest(SyntaxTree syntaxTree, bool expectedElse)
        {
            SyntaxNode node = new NodeLocator(syntaxTree).LocateFirst(typeof(IfStatementSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(IfStatementSyntax).Name));

            IfStatementSyntax conditionalNode = node as IfStatementSyntax;

            TestDeterminedLastElseClause(conditionalNode, expectedElse);
        }
예제 #21
0
        public void MethodParametersAreOfCorrectType()
        {
            var source = @"
                class Class1 {
                    public void Method1(int param1) { }
                    public void Method2(int param1, string param2) { }
                    public void Method3(int param1, string param2, object param3) { }
                    public void Method4(int param1, string param2, object param3, bool param4) { }
                }
            ";

            var syntaxTree = CSharpSyntaxTree.ParseText(source);
            var semanticModel = CSharpCompilation.Create("Class").AddReferences(
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees(
                syntaxTree).GetSemanticModel(syntaxTree);

            IEnumerable<SyntaxNode> nodes = new NodeLocator(syntaxTree).LocateAll(
                typeof(MethodDeclarationSyntax));

            foreach (SyntaxNode node in nodes)
            {
                Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                    typeof(MethodDeclarationSyntax).Name));
                MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;

                MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode);

                foreach (var param in methodDeclaration.Parameters)
                {
                    Assert.IsNotNull(param, "Parameter should not be null!");
                    Assert.IsInstanceOfType(param, typeof(Parameter), "Wrong parameter type!");
                }
            }
        }
        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!");
        }
예제 #23
0
        public void MethodReturnTypeVoid()
        {
            IEnumerable<SyntaxNode> nodes = new NodeLocator(Class11SyntaxTree).LocateAll(
                typeof(MethodDeclarationSyntax)).Take(3); // The first 3 have void return type

            foreach (SyntaxNode node in nodes)
            {
                Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                    typeof(MethodDeclarationSyntax).Name));

                MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;

                TestRetrieveMethodReturnType(methodDeclarationNode);
            }
        }
예제 #24
0
        public void InterfaceNameFromClassWithInterface()
        {
            SyntaxNode node = new NodeLocator(Class3SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(ClassDeclarationSyntax).Name));

            ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax;

            TestRetrieveInterfacesName(classDeclarationNode, Class3SemanticModel,
                new string[] { TestSuite.Class3.Value["Interface1Name"] });
        }
예제 #25
0
        public void ClassNameFromSimpleClass()
        {
            SyntaxNode node = new NodeLocator(Class1SyntaxTree).LocateLast(typeof(ClassDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(ClassDeclarationSyntax).Name));

            ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax;

            TestRetrieveClassName(classDeclarationNode, TestSuite.Class1.Value["ClassName"]);
        }
예제 #26
0
        public void TypeFullNameRenderingSemanticModel()
        {
            var tree = CSharpSyntaxTree.ParseText(@"
            using System;
            public class MyClass : IDisposable {
                public void Dispose() { }
            }
            ");

            var node = new NodeLocator(tree).LocateLast(typeof(ClassDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(ClassDeclarationSyntax).Name));

            // Loading MSCoreLib
            var compilation = CSharpCompilation.Create("TestAssembly")
                  .AddReferences(
                     MetadataReference.CreateFromFile(
                       typeof(object).Assembly.Location))
                  .AddSyntaxTrees(tree);
            var semanticModel = compilation.GetSemanticModel(tree);

            ClassDeclarationSyntax classDeclarationNode = node as ClassDeclarationSyntax;
            BaseTypeSyntax baseTypeNode = classDeclarationNode.BaseList.Types.FirstOrDefault();

            TestRetrieveTypeName(baseTypeNode, null, "IDisposable");
            TestRetrieveTypeName(baseTypeNode, semanticModel, "IDisposable");
            TestRetrieveTypeFullName(baseTypeNode, null, "IDisposable");
            TestRetrieveTypeFullName(baseTypeNode, semanticModel, "System.IDisposable");
        }
예제 #27
0
        public void MethodWithNoParameters()
        {
            var source = @"
                class Class1 {
                    public void Method1() { }
                }
            ";

            var syntaxTree = CSharpSyntaxTree.ParseText(source);
            var semanticModel = CSharpCompilation.Create("Class").AddReferences(
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees(
                syntaxTree).GetSemanticModel(syntaxTree);

            SyntaxNode node = new NodeLocator(syntaxTree).LocateFirst(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;
            MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode);

            Assert.IsNotNull(methodDeclaration.Parameters, "Expecting list of parameters not to be null!");
            Assert.AreEqual(0, methodDeclaration.Parameters.Count(), "Expecting no parameters!");
        }
        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();
        }
예제 #29
0
        public void MethodWithParameters()
        {
            var source = @"
                class Class1 {
                    public void Method1(int param1) { }
                    public void Method2(int param1, string param2) { }
                    public void Method3(int param1, string param2, object param3) { }
                    public void Method4(int param1, string param2, object param3, bool param4) { }
                }
            ";

            var syntaxTree = CSharpSyntaxTree.ParseText(source);
            var semanticModel = CSharpCompilation.Create("Class").AddReferences(
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees(
                syntaxTree).GetSemanticModel(syntaxTree);

            IEnumerable<SyntaxNode> nodes = new NodeLocator(syntaxTree).LocateAll(
                typeof(MethodDeclarationSyntax));

            foreach (SyntaxNode node in nodes)
            {
                Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                    typeof(MethodDeclarationSyntax).Name));
                MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;

                MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode);
                Assert.IsNotNull(methodDeclaration.Parameters, "Expecting list of parameters not to be null!");
                Assert.IsTrue(methodDeclaration.Parameters.Count() > 0, "Expecting parameters!");
            }

            // 1 parameter
            MethodDeclaration method1Declaration = new MethodDeclaration(nodes.ElementAt(0) as MethodDeclarationSyntax);
            Assert.AreEqual(1, method1Declaration.Parameters.Count(), "Expecting different number of paramters!");

            // 2 parameters
            MethodDeclaration method2Declaration = new MethodDeclaration(nodes.ElementAt(1) as MethodDeclarationSyntax);
            Assert.AreEqual(2, method2Declaration.Parameters.Count(), "Expecting different number of paramters!");

            // 3 parameters
            MethodDeclaration method3Declaration = new MethodDeclaration(nodes.ElementAt(2) as MethodDeclarationSyntax);
            Assert.AreEqual(3, method3Declaration.Parameters.Count(), "Expecting different number of paramters!");

            // 4 parameters
            MethodDeclaration method4Declaration = new MethodDeclaration(nodes.ElementAt(3) as MethodDeclarationSyntax);
            Assert.AreEqual(4, method4Declaration.Parameters.Count(), "Expecting different number of paramters!");
        }
예제 #30
0
        private static string GetTranslation(string source)
        {
            // Getting the AST node
            CSharpSyntaxTree tree = ASTExtractor.Extract(source);

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

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

            // Getting the translation unit
            ITranslationUnit translationUnit = astWalker.Walk();
            return translationUnit.Translate();
        }
예제 #31
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;
        }