public static void ExtractMethodTest(string codeText) { if (ParseDesc(codeText).StartsWith("Ignore", true, null)) // TODO: Would be better to assign TestOutcome.Ignored to the TestCase instance directly. { Assert.Ignore(); } var parser = ParseCSharp(codeText); var targetMethod = parser.CompilationUnit.FindMethod("Target"); var expectedMethod = parser.CompilationUnit.FindMethod("Expected"); var markedUpSection = FindMarkedUpAstSection(targetMethod, parser.Lexer.SpecialTracker.RetrieveSpecials()); CSharpMethodExtractor extractor = new CSharpMethodExtractor(); var success = extractor.Extract(targetMethod, markedUpSection.Window); Assert.IsTrue(success, "The extract method operation failed."); TestLog.EmbedPlainText("Target", targetMethod.Print()); TestLog.EmbedPlainText("Expected", expectedMethod.Print()); TestLog.EmbedPlainText("Actual extracted", extractor.ExtractedMethod.Print()); Assert.IsTrue(expectedMethod.Matches(extractor.ExtractedMethod), "Expected the extracted method to match method named 'Expected'"); }
public static MethodDeclaration ExtractMethod(MethodDeclaration md, Window window) { CSharpMethodExtractor extractor = new CSharpMethodExtractor(); extractor.Extract(md, window); return(extractor.ExtractedMethod); }
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 static MethodDeclaration ExtractMethod(MethodDeclaration md, Window window, List <INode> children) { try { CSharpMethodExtractor extractor = new CSharpMethodExtractor(); extractor.Extract(md, window, children); return(extractor.ExtractedMethod); } catch (Exception ex) { throw new Exception("Window: " + window + " Method: " + md.Print(), ex); } }
private void RunWhitespaceTest(string whitespace) { var target = AstMatchHelper.ParseToMethodDeclaration( string.Format(@"public void Target() {{ int x = 7; Console.WriteLine(x); Console.WriteLine(x); {0}}}", whitespace)); CSharpMethodExtractor extractor = new CSharpMethodExtractor(); extractor.Extract(target, new Window(1, 2)); Assert.AreEqual(1, extractor.ExtractedMethod.Parameters.Count, "Expected a new method with exactly one parameter."); }
internal static CloneDesc ExtractMethodAsCloneDesc(Window window, MethodDeclaration right, List <INode> children) { CSharpMethodExtractor extractor = new CSharpMethodExtractor(); extractor.Extract(right, window, children); MethodDeclaration md = extractor.ExtractedMethod; if (md != null) { return new CloneDesc(md, window, children) { ReplacementInvocation = extractor.GetCall(right, md, new VariableDeclaration("heyman")) } } ; return(null); }