public void SimpleTest() { var syntaxTree = GetSimpleSyntaxTree(); var methodDeclaration = syntaxTree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Single(); string expression = null; if ( P.SingleStatement( P.ExpressionStatement( P.InvocationExpression( P.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, P.IdentifierName("WriteLine"), P.IdentifierName("Console") ), P.ArgumentList( P.Argument( expression: P.LiteralExpression( action: p => expression = p.Token.ValueText ) ) ) ) ) ).IsMatch(methodDeclaration.Body) ) { Assert.AreEqual("Hello world!", expression); } }
public void MatchAllStringInvocations() { var syntaxTree = GetSimpleSyntaxTree(); var methodDeclaration = syntaxTree.GetRoot() .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Single(); var strings = new List <string>(); int matches = P.InvocationExpression( P.MemberAccessExpression(), P.ArgumentList( P.Argument( expression: P.LiteralExpression( action: p => strings.Add(p.Token.ValueText) ) ) ) ) .MatchDescendantNodes(methodDeclaration.Body) .Count; Assert.AreEqual(2, matches); Assert.AreEqual(new List <string> { "Hello ", "world!" }, strings); }