public void AllPossiblePermutationsOfMultipleLiterals() { const string codeText = @"void Target() { foo(7, 7, 8, 8); }"; var target = AstMatchHelper.ParseToMethodDeclaration(codeText); LiteralToParameterExpansion exp = new LiteralToParameterExpansion(); var perms = exp.FindCandidatesViaRefactoringPermutations(target); foreach (var perm in perms) { Debug.WriteLine(perm.PermutatedMethod.Print()); } AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { foo(i, 7, 8, 8); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { foo(7, i, 8, 8); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { foo(7, 7, i, 8); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { foo(7, 7, 8, i); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { foo(i, i, 8, 8); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { foo(7, 7, i, i); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(i, 7, i2, 8); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(i, 7, 8, i2); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(7, i, i2, 8); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(7, i, 8, i2); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(7, i, 8, i2); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(i, 7, 8, i2); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(7, i, i2, 8); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(i, 7, i2, 8); }")); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i2) { foo(i, i, i2, i2); }")); // I think that's all of them. 2^n-1 where n=4 is 15. }
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 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"]); }
public void TestNodeCounts() { var target = AstMatchHelper.ParseToMethodDeclaration(@"void Target() { foo(7, 7, 8, 8); }"); var target2 = AstMatchHelper.ParseToMethodDeclaration(@"void Target() { foo(8, 8, 7, 7); }"); Assert.That(target.CountNodes(), Is.EqualTo(target2.CountNodes())); }
public void ShrinkingOscillatingWindowTest() { const int cl = 4; IEnumerable <Window> windows = AstMatchHelper.OscillateWindows(cl); CollectionAssert.Contains(windows, new Window(0, 0)); CollectionAssert.Contains(windows, new Window(0, 1)); CollectionAssert.Contains(windows, new Window(0, 2)); CollectionAssert.Contains(windows, new Window(0, 3), "This would be the entire function body."); CollectionAssert.Contains(windows, new Window(1, 1)); CollectionAssert.Contains(windows, new Window(1, 2)); CollectionAssert.Contains(windows, new Window(1, 3)); CollectionAssert.Contains(windows, new Window(2, 2)); CollectionAssert.Contains(windows, new Window(2, 3)); CollectionAssert.Contains(windows, new Window(3, 3)); Assert.AreEqual(10, windows.Count(), "There are more than expected."); Assert.AreEqual(5, new Window(0, 4).Size, "Size is determined inclusive with the end points."); Assert.Throws <ArgumentException>(() => { new Window(1, 0); }, "size parameters are out of order"); Assert.Throws <ArgumentException>(() => { new Window(-2, 3); }, "zero is the minimum"); Assert.Throws <ArgumentException>(() => { new Window(0, -3); }, "zero is the minimum"); }
private static void Test(string codeText) { var unit = AstMatchHelper.ParseToCompilationUnit(codeText); var meth = unit.FindMethod("Foo"); LocalVariableDeclaration child = (LocalVariableDeclaration)meth.Body.Children[0]; Assert.AreNotEqual(0, child.EndLocation.Line, "Location should be set properly, regardless of modifiers."); }
public IEnumerable <CloneDesc> GetAllPossibleExtractedMethods(MethodDeclaration right, int targetChildCount) { List <INode> children = right.Body.Children; var windows = AstMatchHelper.OscillateWindows(children.Count); return(ProcessChildren(right, children, windows, targetChildCount)); }
public void ExhaustiveCs2_0ParsesAndMatches() { var codeText = File.ReadAllText(@"..\..\Ralph.Core.Tests\AstCompareTests\ExhaustiveSampleCode.2.0.cs"); var cu = AstMatchHelper.ParseToCompilationUnit(codeText); Assert.That(cu.Matches(cu), Is.True); }
public int CountAllExtractedMethods(MethodDeclaration right, int targetChildCount) { List <INode> children = right.Body.Children; var windows = AstMatchHelper.OscillateWindows(children.Count); return(ProcessChildren(right, children, windows, targetChildCount)); }
public void ExhaustiveCsParsesAndMatches() { // This file from http://blogs.msdn.com/b/kirillosenkov/archive/2010/05/11/updated-c-all-in-one-file.aspx var codeText = File.ReadAllText(@"..\..\Ralph.Core.Tests\AstCompareTests\ExhaustiveSampleCode.cs"); var cu = AstMatchHelper.ParseToCompilationUnit(codeText); Assert.That(cu.Matches(cu), Is.True); }
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 SingleLiteralCanBeMultipleParams() { const string codeText = @"void Target() { Console.WriteLine(7); Console.Write(7); }"; var target = AstMatchHelper.ParseToMethodDeclaration(codeText); LiteralToParameterExpansion exp = new LiteralToParameterExpansion(); var perms = exp.FindCandidatesViaRefactoringPermutations(target); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i, int i1) { Console.WriteLine(i); Console.Write(i1); }"), "Expected a permutation where each literal gets it's own parameter, regardless of redundancy."); }
public void SingleLiteralInstance() { const string codeText = @"void Target() { Console.WriteLine(7); }"; var target = AstMatchHelper.ParseToMethodDeclaration(codeText); LiteralToParameterExpansion exp = new LiteralToParameterExpansion(); var perms = exp.FindCandidatesViaRefactoringPermutations(target); Assert.IsTrue(perms.Any(p => p.PermutatedMethod.MatchesPrint(AstMatchHelper.ParseToMethodDeclaration(@"void Expected(int i) { Console.WriteLine(i); }")))); }
public void MultipleInstancesOfDifferentLiterals() { const string codeText = @"void Target() { Console.WriteLine(7); Console.Write(8); Console.WriteLine(7); Console.Write(8); }"; var target = AstMatchHelper.ParseToMethodDeclaration(codeText); LiteralToParameterExpansion exp = new LiteralToParameterExpansion(); var perms = exp.FindCandidatesViaRefactoringPermutations(target); AssertExists(perms, p => p.PermutatedMethod.MatchesPrint(AstMatchHelper.ParseToMethodDeclaration(@"void Expected(int i, int i1) { Console.WriteLine(i); Console.Write(i1); Console.WriteLine(i); Console.Write(i1);}")), "Expected literals with same value to get the same parameter."); }
// Thanks to the magic of TestDriven.Net, I need not make a full on unit test or test exe. // ReSharper disable UnusedMember.Global public static void TestShowVisualizer() // ReSharper restore UnusedMember.Global { const string codeText = @"public class Foo {void Target() { Console.WriteLine(7); } }"; var target = AstMatchHelper.ParseToCompilationUnit(codeText); VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(target, typeof(INodeVisualizer)); visualizerHost.ShowVisualizer(); }
public void MultipleCopiesOfALiteral() { const string codeText = @"void Target() { Console.WriteLine(7); Console.Write(7); }"; var target = AstMatchHelper.ParseToMethodDeclaration(codeText); LiteralToParameterExpansion exp = new LiteralToParameterExpansion(); var perms = exp.FindCandidatesViaRefactoringPermutations(target); AssertExists(perms, p => p.PermutatedMethod.MatchesPrint(AstMatchHelper.ParseToMethodDeclaration(@"void Expected(int i) { Console.WriteLine(i); Console.Write(i); }")), "Expected a permutation where all possible literals were replaced with a single parameter."); }
public void AstComparisonIgnoreLiteralsVisitorTest() { var visitor = new AstComparisonIgnoreLiteralsVisitor(); var md = AstMatchHelper.ParseToMethodDeclaration(@"void foo() { int i = 1 + 2; }"); var md_different = AstMatchHelper.ParseToMethodDeclaration(@"void foo() { int i = 2 + 1; }"); md.AcceptVisitor(visitor, md_different); Assert.That(visitor.Match, Is.True); }
public void TestOrder() { const int MaxSize = 2; var windows = AstMatchHelper.OscillateWindows(MaxSize); var last_size = MaxSize; foreach (var window in windows) { Assert.LessOrEqual(window.Size, last_size); last_size = window.Size; } }
public void RenameLocalVariableExpansion_Other() { const string codeText1 = @"void Expected(int i, int i2) { foo(i, 7, i2, 8); }"; const string codeText2 = @"void Target(System.Int32 i, System.Int32 i1) { foo(i, 7, i1, 8); }"; var expected = AstMatchHelper.ParseToMethodDeclaration(codeText1); var target = AstMatchHelper.ParseToMethodDeclaration(codeText2); var r = new RenameLocalVariableExpansion(); var permutations = r.FindCandidatesViaRefactoringPermutations(new TargetInfo(expected, target), target); Util.AssertExists(permutations, x => x.MatchesPrint(expected)); }
private void RunRenameLocalVariableRefactoringTest(string codeText, string idStart, string idEnd) { var foo_meth = AstMatchHelper.ParseToMethodDeclaration(codeText); var renameTable = new Dictionary <string, string> { { idStart, idEnd } }; RenameLocalVariableRefactoring r = new RenameLocalVariableRefactoring(renameTable); foo_meth.AcceptVisitor(r, null); var expectedCode = codeText.Replace(idStart, idEnd); Assert.IsTrue(AstMatchHelper.ParseToMethodDeclaration(expectedCode).Matches(foo_meth), "Expected code: " + expectedCode + " did not match actual code: " + foo_meth.Print()); }
public void PrimitiveExpressionForNullBug() { const string codeText = @"public void Foo() { if (member != null) { } }"; var exp = new LiteralToParameterExpansion(); var md = AstMatchHelper.ParseToMethodDeclaration(codeText); exp.FindCandidatesViaRefactoringPermutations(md).ToList(); }
public static bool MatchesIgnoreVarNames(this CloneDesc p, string expectedCode) { RenameLocalVariableExpansion rename = new RenameLocalVariableExpansion(); var expected = AstMatchHelper.ParseToMethodDeclaration(expectedCode); foreach (var prd in rename.FindCandidatesViaRefactoringPermutations(new TargetInfo(expected, p.PermutatedMethod), p.PermutatedMethod)) { if (expected.Matches(prd)) { return(true); } } return(false); }
public void GotoTargetOccursBefore() { const string codeText = @"public void Switch() { fred: return; goto fred; }"; var md = AstMatchHelper.ParseToMethodDeclaration(codeText); FindJumpInstructionsVisitor v = new FindJumpInstructionsVisitor(md, new MySelection(md.Body.Children)); md.AcceptVisitor(v, null); Assert.IsTrue(v.IsOk); }
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."); }
private void TestCounterOnSnippet(string codeText) { var expansion = new OscillatingExtractMethodRefactoringExpansion(); var counter = new OscillatingExtractMethodCounter(); var md = AstMatchHelper.ParseToMethodDeclaration(codeText); var methods = expansion.GetAllPossibleExtractedMethods(md, 3); var count = counter.CountAllExtractedMethods(md, 3); Assert.AreEqual(methods.Count(), count); methods = expansion.GetAllPossibleExtractedMethods(md, 1); count = counter.CountAllExtractedMethods(md, 1); Assert.AreEqual(methods.Count(), count); }
public void RenameLocalVariableExpansion_IncludesParameters() { const string codeText = @"void Bar(string bar_str) { bar_str += ""zippy""; Console.Write(bar_str); }"; var foo_meth = AstMatchHelper.ParseToMethodDeclaration(codeText); var expected_method_text = codeText.Replace("bar_str", "jibberjab"); var expected_method = AstMatchHelper.ParseToMethodDeclaration(expected_method_text); RenameLocalVariableExpansion r = new RenameLocalVariableExpansion(); var candidates = r.FindCandidatesViaRefactoringPermutations(new TargetInfo(expected_method, foo_meth), foo_meth); Util.AssertExists(candidates, x => expected_method.MatchesPrint(x)); }
public void AllPossiblePermutationsOfASingleLiteral() { const string codeText = @"void Target() { Console.WriteLine(7); Console.Write(7); }"; var target = AstMatchHelper.ParseToMethodDeclaration(codeText); LiteralToParameterExpansion exp = new LiteralToParameterExpansion(); var perms = exp.FindCandidatesViaRefactoringPermutations(target); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { Console.WriteLine(i); Console.Write(i); }"), "Expected a permutation where all possible literals were replaced with a single parameter."); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { Console.WriteLine(i); Console.Write(7); }"), "Expected a permutation where the first literal was replaced, but not the second."); AssertExists(perms, p => p.MatchesIgnoreVarNames(@"void Expected(int i) { Console.WriteLine(7); Console.Write(i); }"), "Expected a permutation where the second literal was replaced, but not the first."); // Print out the permutations, just for reference. for (int i = 0; i < perms.ToArray().Length; i++) { var perm = perms.ToArray()[i]; TestLog.EmbedPlainText("perm" + i, perm.PermutatedMethod.Print()); } }
public void GotoDefaultCase() { const string codeText = @"public void Switch() { switch (0) { case 0: goto default; default: break; } }"; var md = AstMatchHelper.ParseToMethodDeclaration(codeText); FindJumpInstructionsVisitor v = new FindJumpInstructionsVisitor(md, new MySelection(md.Body.Children)); md.AcceptVisitor(v, null); Assert.IsTrue(v.IsOk); }
public void FindsClasses() { var cu = AstMatchHelper.ParseToCompilationUnit(@" class FooBar { void Foo() { } void Bar() { } } class BarFoo { void Foo() { } void Bar() { } }" ); IndexableClassFinderVisitor mfv = new IndexableClassFinderVisitor(); cu.AcceptVisitor(mfv, null); Assert.IsNotNull(mfv.Classes["FooBar"]); Assert.IsNotNull(mfv.Classes["BarFoo"]); }
private int ProcessChild(MethodDeclaration right, INode child, int targetChildCount) { var children1 = OscillatingExtractMethodRefactoringExpansion.GetExtractableChildren(child); return(ProcessChildren(right, children1, AstMatchHelper.OscillateWindows(children1.Count), targetChildCount)); }