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); } }
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 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 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()); }
// 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 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"]); }