public void FindsMethodsAndAllowsStringIndexesTest() { var cu = AstMatchHelper.ParseToCompilationUnit(@" class FooBar { void Foo() { Console.WriteLine(""Hello world""); Console.WriteLine(""2nd line""); Console.WriteLine(""3rd line""); Console.WriteLine(""fourth""); } void Template() { Console.WriteLine(""3rd line""); } }" ); IndexableMethodFinderVisitor mfv = new IndexableMethodFinderVisitor(); cu.AcceptVisitor(mfv, null); Assert.IsNotNull(mfv.Methods["Template"]); Assert.IsNotNull(mfv.Methods["Foo"]); }
private static void ExecuteDuplicationDetection(Result expectedResult, string codeText, string msgFromCodeComment) { TestLog.EmbedPlainText("The code", codeText); CompilationUnit cu = AstMatchHelper.ParseToCompilationUnit(codeText); // We require the first class in the file to have the Foo & Bar methods. var classes = cu.FindAllClasses(); Assert.That(classes.Count(), Is.GreaterThan(0), "Expected at least one class to be in the test cs file."); // Expect two methods, Foo & Bar. IndexableMethodFinderVisitor visitor = new IndexableMethodFinderVisitor(); classes.First().AcceptVisitor(visitor, null); Assert.AreEqual(visitor.FooMethod.Name, "Foo", "Expected a method named Foo."); Assert.AreEqual(visitor.BarMethod.Name, "Bar", "Expected a method named Bar."); AstComparisonVisitor cv = new AstComparisonVisitor(); visitor.BarMethod.AcceptVisitor(cv, visitor.FooMethod); if (expectedResult == Result.Match) { Assert.IsTrue(cv.Match, "Expected Foo & Bar to match: " + msgFromCodeComment); Assert.That(visitor.BarMethod.Body.Matches2(visitor.FooMethod.Body)); } else { Assert.IsFalse(cv.Match, "Expected Foo & Bar to not match: " + msgFromCodeComment); Assert.That(visitor.BarMethod.Matches2(visitor.FooMethod), Is.False); } }
public void CanBePerformedOnArbitrarilyDeepNodes() { const string codeText = @" using System; public class One { void Foo() { double w = 7; double l = 8; if (DateTime.Now.Day == 3) { Console.WriteLine(""stuff""); Console.WriteLine(""stuff""); } double area = l*w; } void Bar() { Console.WriteLine(""stuff""); } }"; var code_text_ast = AstMatchHelper.ParseToCompilationUnit(codeText); IndexableMethodFinderVisitor v = new IndexableMethodFinderVisitor(); code_text_ast.AcceptVisitor(v, null); MethodDeclaration method = v.Methods["Foo"]; FindFirstIfElseVisitor v2 = new FindFirstIfElseVisitor(); method.AcceptVisitor(v2, null); IfElseStatement ifelse_stmt = v2.IfElse; List <Statement> statements = ifelse_stmt.TrueStatement; Assert.AreEqual(1, statements.Count, "Expect TrueStatement to always return a single element, and it's a block."); Assert.IsInstanceOf <BlockStatement>(statements[0], "Expect TrueStatement to always return a single element, and it's a block."); BlockStatement block = (BlockStatement)statements[0]; CSharpMethodExtractor extractor = new CSharpMethodExtractor(); var success = extractor.Extract(method, new Window(0, 0), block.Children); Assert.IsTrue(success); Console.WriteLine(extractor.ExtractedMethod.Print()); MethodDeclaration expected_method = v.BarMethod; Assert.IsTrue(expected_method.Matches(extractor.ExtractedMethod), "The expected AST did not match the actual extracted AST.\nExpected: {0} \nActual:{1}", expected_method.Print(), extractor.ExtractedMethod.Print()); }
public void CanBePerformedOnArbitrarilyDeepNodes() { const string codeText = @" using System; public class One { void Foo() { double w = 7; double l = 8; if (DateTime.Now.Day == 3) { Console.WriteLine(""stuff""); Console.WriteLine(""stuff""); } double area = l*w; } void Bar() { Console.WriteLine(""stuff""); } }"; var code_text_ast = AstMatchHelper.ParseToCompilationUnit(codeText); IndexableMethodFinderVisitor v = new IndexableMethodFinderVisitor(); code_text_ast.AcceptVisitor(v, null); MethodDeclaration method = v.Methods["Foo"]; FindFirstIfElseVisitor v2 = new FindFirstIfElseVisitor(); method.AcceptVisitor(v2, null); IfElseStatement ifelse_stmt = v2.IfElse; List<Statement> statements = ifelse_stmt.TrueStatement; Assert.AreEqual(1, statements.Count, "Expect TrueStatement to always return a single element, and it's a block."); Assert.IsInstanceOf<BlockStatement>(statements[0], "Expect TrueStatement to always return a single element, and it's a block."); BlockStatement block = (BlockStatement) statements[0]; CSharpMethodExtractor extractor = new CSharpMethodExtractor(); var success = extractor.Extract(method, new Window(0, 0), block.Children); Assert.IsTrue(success); Console.WriteLine(extractor.ExtractedMethod.Print()); MethodDeclaration expected_method = v.BarMethod; Assert.IsTrue(expected_method.Matches(extractor.ExtractedMethod), "The expected AST did not match the actual extracted AST.\nExpected: {0} \nActual:{1}", expected_method.Print(), extractor.ExtractedMethod.Print()); }
public void FindsMethodsAndAllowsStringIndexesTest() { var cu = AstMatchHelper.ParseToCompilationUnit(@" class FooBar { void Foo() { Console.WriteLine(""Hello world""); Console.WriteLine(""2nd line""); Console.WriteLine(""3rd line""); Console.WriteLine(""fourth""); } void Template() { Console.WriteLine(""3rd line""); } }"); IndexableMethodFinderVisitor mfv = new IndexableMethodFinderVisitor(); cu.AcceptVisitor(mfv, null); Assert.IsNotNull(mfv.Methods["Template"]); Assert.IsNotNull(mfv.Methods["Foo"]); }