Beispiel #1
0
        public void TestGetCompilation()
        {
            string     code  = "public Class ClassA{}";
            SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code);
            SyntaxTree tree2 = CSharpSyntaxTree.ParseText(code);
            SyntaxTree tree3 = CSharpSyntaxTree.ParseText(code);
            SyntaxTree tree4 = CSharpSyntaxTree.ParseText(code);

            SyntaxNode node1 = tree1.GetRootAsync().Result.DescendantNodes().First();
            SyntaxNode node2 = tree2.GetRootAsync().Result.DescendantNodes().First();
            SyntaxNode node3 = tree3.GetRootAsync().Result.DescendantNodes().First();
            SyntaxNode node4 = tree4.GetRootAsync().Result.DescendantNodes().First();

            List <SyntaxTree> trees1 = new List <SyntaxTree> {
                tree1, tree2
            };
            List <SyntaxTree> trees2 = new List <SyntaxTree> {
                tree3
            };

            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);
            Compilation comp2 = CSharpCompilation.Create("TestCompilation2", trees2);

            Compilation[] arr = new Compilation[] { comp1, comp2 };

            ScriptAnalyzerSymbolHelper helper = new ScriptAnalyzerSymbolHelper(arr);

            Assert.AreEqual(comp1, helper.GetCompilation(node1), "Gets right compilation");
            Assert.AreEqual(comp1, helper.GetCompilation(node2), "Gets right compilation");
            Assert.AreEqual(comp2, helper.GetCompilation(node3), "Gets right compilation");
            Assert.IsNull(helper.GetCompilation(node4), "Returns null if nothing found");
        }
Beispiel #2
0
        public void TestGetSymbolInfo()
        {
            string     code  = "public class ClassA{ ClassB test;}";
            SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code);
            SyntaxTree tree2 = CSharpSyntaxTree.ParseText(code);

            IdentifierNameSyntax   node1 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <IdentifierNameSyntax>().First();
            IdentifierNameSyntax   node2 = tree2.GetRootAsync().Result.DescendantNodesAndSelf().OfType <IdentifierNameSyntax>().First();
            ClassDeclarationSyntax node3 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().First();


            List <SyntaxTree> trees1 = new List <SyntaxTree> {
                tree1
            };

            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);


            Compilation[] arr = new Compilation[] { comp1 };

            ScriptAnalyzerSymbolHelper helper = new ScriptAnalyzerSymbolHelper(arr);

            SemanticModel eModel1      = comp1.GetSemanticModel(tree1);
            SymbolInfo    eSymbolInfo1 = eModel1.GetSymbolInfo(node1);


            SymbolInfo rSymbolInfo1 = helper.GetSymbolInfo(node1);
            SymbolInfo rSymbolInfo2 = helper.GetSymbolInfo(node2);
            SymbolInfo rSymbolInfo3 = helper.GetSymbolInfo(node2);


            Assert.AreEqual(eSymbolInfo1, rSymbolInfo1, "Gets right symbolInfo");
            Assert.AreEqual(rSymbolInfo2, new SymbolInfo(), "Returns empty symbol info if nothing found");
            Assert.AreEqual(rSymbolInfo3, new SymbolInfo(), "Returns empty symbol info if nothing found");
        }
Beispiel #3
0
        public void TestGetSyntaxNodesFromIdentifierObjectCreation()
        {
            string code1 = "public class TestCLass{public void Main(){ClassA test = new ClassA(0);}} public class ClassA{ public ClassA(){} public ClassA(int param){}}";

            SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1);
            ObjectCreationExpressionSyntax node1 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <ObjectCreationExpressionSyntax>().First();
            ClassDeclarationSyntax         node2 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().Last();
            ConstructorDeclarationSyntax   node3 = node2.DescendantNodes().OfType <ConstructorDeclarationSyntax>().First();
            ConstructorDeclarationSyntax   node4 = node2.DescendantNodes().OfType <ConstructorDeclarationSyntax>().Last();
            IdentifierNameSyntax           node5 = node1.DescendantNodes().OfType <IdentifierNameSyntax>().Last();

            List <SyntaxTree> trees1 = new List <SyntaxTree> {
                tree1
            };
            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);

            ScriptAnalyzerSymbolHelper   sHelper = new ScriptAnalyzerSymbolHelper(new Compilation[] { comp1 });
            ScriptAnalyzerResourceHelper rHelper = new ScriptAnalyzerResourceHelper(sHelper);

            List <SyntaxNode> result = rHelper.GetSyntaxNodes(node5);

            Assert.IsNotNull(result, "Returns a list");
            Assert.AreEqual(1, result.Count(), "List has one node");
            CollectionAssert.Contains(result, node2, "Node is class");
        }
Beispiel #4
0
        public void TestGetSyntaxNodesFromIdentifierFunc()
        {
            string code1 = "public class TestCLass{public void Main(){Test(\"test\");} public void Test(){} public void Test(string param){}}";

            SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1);
            InvocationExpressionSyntax node1 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <InvocationExpressionSyntax>().First();
            MethodDeclarationSyntax    node2 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <MethodDeclarationSyntax>().ElementAt(1);
            MethodDeclarationSyntax    node3 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <MethodDeclarationSyntax>().Last();
            IdentifierNameSyntax       node4 = node1.DescendantNodes().OfType <IdentifierNameSyntax>().Last();

            List <SyntaxTree> trees1 = new List <SyntaxTree> {
                tree1
            };
            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);

            ScriptAnalyzerSymbolHelper   sHelper = new ScriptAnalyzerSymbolHelper(new Compilation[] { comp1 });
            ScriptAnalyzerResourceHelper rHelper = new ScriptAnalyzerResourceHelper(sHelper);

            List <SyntaxNode> result = rHelper.GetSyntaxNodes(node4);

            Assert.IsNotNull(result, "Returns a list");
            Assert.AreEqual(1, result.Count(), "List has one node");
            CollectionAssert.Contains(result, node3, "Node is expected node");
            CollectionAssert.DoesNotContain(result, node2, "Node is not other unused function");
        }
Beispiel #5
0
        public void TestGetSyntaxNodesFromClassBaseInterface()
        {
            string code1 = "public class ClassA:ClassB, IClassC{}" +
                           "public class ClassB{} public interface IClassC { }";

            SyntaxTree                 tree1 = CSharpSyntaxTree.ParseText(code1);
            ClassDeclarationSyntax     node1 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().First();
            ClassDeclarationSyntax     node2 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().Last();
            InterfaceDeclarationSyntax node3 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <InterfaceDeclarationSyntax>().First();

            List <SyntaxTree> trees1 = new List <SyntaxTree> {
                tree1
            };
            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);

            ScriptAnalyzerSymbolHelper   sHelper = new ScriptAnalyzerSymbolHelper(new Compilation[] { comp1 });
            ScriptAnalyzerResourceHelper rHelper = new ScriptAnalyzerResourceHelper(sHelper);

            List <SyntaxNode> result = rHelper.GetSyntaxNodes(node1);

            Assert.IsNotNull(result, "Returns a list");
            Assert.AreEqual(2, result.Count(), "List has two nodes");
            CollectionAssert.Contains(result, node2, "Node is expected node");
            CollectionAssert.Contains(result, node3, "Node is expected node");
        }
Beispiel #6
0
        private ScriptAnalyzer createAnalyzer(Compilation[] comps)
        {
            ScriptAnalyzerSymbolHelper   sHelper = new ScriptAnalyzerSymbolHelper(comps);
            ScriptAnalyzerResourceHelper rHelper = new ScriptAnalyzerResourceHelper(sHelper);

            ScriptAnalyzer analyzer = new ScriptAnalyzer(rHelper);

            return(analyzer);
        }
Beispiel #7
0
        public void TestConstructor()
        {
            Compilation comp = CSharpCompilation.Create("test");
            ScriptAnalyzerSymbolHelper   sHelper = new ScriptAnalyzerSymbolHelper(new Compilation[] { comp });
            ScriptAnalyzerResourceHelper rHelper = new ScriptAnalyzerResourceHelper(sHelper);
            ScriptAnalyzer analyzer = new ScriptAnalyzer(rHelper);

            Assert.AreEqual(rHelper, analyzer.ResourceHelper, "Resource helper gest set");
        }
Beispiel #8
0
        public void TestConstructor()
        {
            Compilation comp = CSharpCompilation.Create("TestCompilation");

            Compilation[]              arr      = new Compilation[] { comp };
            List <Compilation>         expected = arr.ToList();
            ScriptAnalyzerSymbolHelper helper   = new ScriptAnalyzerSymbolHelper(arr);
            List <Compilation>         result   = helper.Compilations;

            Assert.IsNotNull(result, "Compilations get set");
            CollectionAssert.AreEqual(expected, result, "Are equal lists");
        }
Beispiel #9
0
        public void TestGetModel()
        {
            string     code  = "public Class ClassA{}";
            SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code);
            SyntaxTree tree2 = CSharpSyntaxTree.ParseText(code);
            SyntaxTree tree3 = CSharpSyntaxTree.ParseText(code);
            SyntaxTree tree4 = CSharpSyntaxTree.ParseText(code);

            SyntaxNode node1 = tree1.GetRootAsync().Result.DescendantNodes().First();
            SyntaxNode node2 = tree2.GetRootAsync().Result.DescendantNodes().First();
            SyntaxNode node3 = tree3.GetRootAsync().Result.DescendantNodes().First();
            SyntaxNode node4 = tree4.GetRootAsync().Result.DescendantNodes().First();

            List <SyntaxTree> trees1 = new List <SyntaxTree> {
                tree1, tree2
            };
            List <SyntaxTree> trees2 = new List <SyntaxTree> {
                tree3
            };

            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);
            Compilation comp2 = CSharpCompilation.Create("TestCompilation2", trees2);

            Compilation[] arr = new Compilation[] { comp1, comp2 };

            ScriptAnalyzerSymbolHelper helper = new ScriptAnalyzerSymbolHelper(arr);

            SemanticModel eModel1 = comp1.GetSemanticModel(tree1);
            SemanticModel eModel2 = comp1.GetSemanticModel(tree2);
            SemanticModel eModel3 = comp2.GetSemanticModel(tree3);

            SemanticModel rModel1 = helper.GetModel(node1);
            SemanticModel rModel2 = helper.GetModel(node2);
            SemanticModel rModel3 = helper.GetModel(node3);
            SemanticModel rModel4 = helper.GetModel(node4);

            Assert.AreEqual(eModel1.SyntaxTree, rModel1.SyntaxTree, "Gets right model");
            Assert.AreEqual(eModel2.SyntaxTree, rModel2.SyntaxTree, "Gets right model");
            Assert.AreEqual(eModel3.SyntaxTree, rModel3.SyntaxTree, "Gets right model");
            Assert.IsNull(rModel4, "Returns null if nothing found");

            SemanticModel rModel5 = helper.GetModel(node1);

            Assert.AreEqual(rModel1, rModel5, "Only 1 model is created and reused");
        }
Beispiel #10
0
        public void TestGetSyntaxNodesFromClassNone()
        {
            string                 code1 = "public class ClassA{}";
            SyntaxTree             tree1 = CSharpSyntaxTree.ParseText(code1);
            ClassDeclarationSyntax node1 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().First();

            List <SyntaxTree> trees1 = new List <SyntaxTree> {
                tree1
            };
            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);

            ScriptAnalyzerSymbolHelper   sHelper = new ScriptAnalyzerSymbolHelper(new Compilation[] { comp1 });
            ScriptAnalyzerResourceHelper rHelper = new ScriptAnalyzerResourceHelper(sHelper);

            List <SyntaxNode> result = rHelper.GetSyntaxNodes(node1);

            Assert.IsNotNull(result, "Returns a list");
            Assert.AreEqual(0, result.Count(), "List is empty");
        }
Beispiel #11
0
        public void TestGetSyntaxNodesFromIdentifierVar()
        {
            string code1 = "public class TestCLass{ int prop; public void Main(){ prop = 1;}}";

            SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1);
            VariableDeclarationSyntax node1  = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <VariableDeclarationSyntax>().First();
            MethodDeclarationSyntax   node2  = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <MethodDeclarationSyntax>().Last();
            IdentifierNameSyntax      node3  = node2.DescendantNodes().OfType <IdentifierNameSyntax>().First();
            VariableDeclaratorSyntax  eNode  = node1.DescendantNodes().OfType <VariableDeclaratorSyntax>().First();
            List <SyntaxTree>         trees1 = new List <SyntaxTree> {
                tree1
            };
            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);

            ScriptAnalyzerSymbolHelper   sHelper = new ScriptAnalyzerSymbolHelper(new Compilation[] { comp1 });
            ScriptAnalyzerResourceHelper rHelper = new ScriptAnalyzerResourceHelper(sHelper);

            List <SyntaxNode> result = rHelper.GetSyntaxNodes(node3);

            Assert.IsNotNull(result, "Returns a list");
            Assert.AreEqual(1, result.Count(), "List has one node");
            CollectionAssert.Contains(result, eNode, "Node is expected node");
        }