/// <summary> /// Helper method, which compiles <paramref name="syntaxTree"/> to <see cref="Compilation"/> ovject /// </summary> /// <param name="syntaxTree"><see cref="SyntaxTree"/> of code to compile</param> /// <param name="diagnostics">Diagnostic information from this compilation</param> /// <param name="compilationOptions">All compilation options</param> /// <returns>Compilation with program</returns> public static CSharpCompilation Compile(this SyntaxTree syntaxTree, CompilationOptions compilationOptions, out IEnumerable <DiagnosticHelper> diagnostics) { var compilation = syntaxTree.Compile(compilationOptions); diagnostics = compilation.GetDiagnostics() .AsDiagnosticHelper() .WithAllCustomDiagnostics(syntaxTree, compilation.GetSemanticModel(syntaxTree)); return(compilation); }
/// <summary> /// Extension method for <see cref="SyntaxNode"/> which gets static method need by tests /// </summary> /// <param name="root">Assembly in which method should be defined</param> /// <param name="className">Class name in which method should be defined</param> /// <param name="methodName">Method name needed by test</param> /// <param name="parametersType">Method parameters. If null method without parameters will be returned</param> /// <returns>Described method</returns> public static MethodDeclarationSyntax GetTestMethod(this SyntaxTree tree, string className, string methodName, Type[] parametersType = null) => tree.GetRoot() .DescendantNodes() .OfType <ClassDeclarationSyntax>() .Where(c => c.Identifier.Text == className) .SelectMany(c => c.DescendantNodes()) .OfType <MethodDeclarationSyntax>() .Where(method => method.Identifier.Text.Trim() == methodName) .Where(method => method.Modifiers.Any(t => t.Kind() == SyntaxKind.StaticKeyword)) .FirstOrDefault(method => { // If there is no given parameters type try to find method without parameters if (parametersType == null || parametersType.Length == 0) { return(method.ParameterList.Parameters.Count == 0); } // If number of parameters do not match it means this is not that function if (parametersType.Length != method.ParameterList.Parameters.Count) { return(false); } // Finds method in which all parameters are exacly the same var semanticModel = tree.Compile(DefaultCompilationOptions, out _).GetSemanticModel(tree); return(!method.ParameterList.Parameters .Where((t, i) => semanticModel .GetDeclaredSymbol(method.ParameterList .Parameters[i]) .Type.Name != parametersType[i].Name) .Any()); });