示例#1
0
        public void UnusedMethodAnalyserReportsNothingIfAllMethodsAreUsed()
        {
            string code = @"
            using System;

            public class TestClass1
            {
                public void TestMethod()
                {
                    TestClass2 test = new TestClass2();
                    test.TestMethod();
                }
            }

            public class TestClass2
            {
                public void TestMethod()
                {
                    TestClass1 test = new TestClass1();
                    test.TestMethod();
                }
            }
            ";

            CompilationUnitSyntax root = CSharpSyntaxTree.ParseText(code).GetCompilationUnitRoot();

            UnusedMethodAnalyser analyser = new UnusedMethodAnalyser();

            analyser.Visit(root);
            IReadOnlyCollection <AnalyserItem> result = analyser.AnalyserItems;

            Assert.Empty(result);
        }
示例#2
0
        public void UnusedMethodAnalyserReportsUnusedMethods()
        {
            string code = @"
            using System;

            public class TestClass
            {
                public void TestMethod()
                {
                    this.TestMethod2();
                }

                private void TestMethod2()
                {
                    var result = 1 + 1;
                }
            }
            ";

            CompilationUnitSyntax root = CSharpSyntaxTree.ParseText(code).GetCompilationUnitRoot();

            UnusedMethodAnalyser analyser = new UnusedMethodAnalyser();

            analyser.Visit(root);
            IReadOnlyCollection <AnalyserItem> result = analyser.AnalyserItems;

            Assert.NotEmpty(result);
            Assert.Equal(1, result.Count);
            Assert.Equal("Unused method", result.First().Message);
            Assert.Equal(5, result.First().NodeReference.GetSyntax().GetLocation().GetMappedLineSpan().StartLinePosition.Line);
        }