/// <summary>
        /// Creates new <see cref="CSharpSyntaxTree"/>s from the marked <c>static readonly</c> or <c>const</c> <see cref="string"/> fields in the specified <paramref name="sourceType"/>
        /// and returns a new <see cref="CSharpCompilation"/> that contains them.
        /// </summary>
        /// <param name="sourceType"><see cref="Type"/> to collect the <see cref="string"/> fields from.</param>
        /// <param name="compilation">Original <see cref="CSharpCompilation"/> to be used as a base for the newly created <see cref="CSharpCompilation"/>.</param>
        public static CSharpCompilation Collect(Type?sourceType, CSharpCompilation?compilation)
        {
            CSharpCompilation c;

            if (compilation is null)
            {
                c = RoslynUtilities.CreateCompilationWithAssemblies(sources: null, null);
            }
            else
            {
                c = compilation;
            }

            if (sourceType is null)
            {
                return(c);
            }

            Type attributeType = typeof(AddToCompilationAttribute);
            Type stringType    = typeof(string);

            return(c.AddSyntaxTrees(sourceType.GetFields(
                                        BindingFlags.Static |
                                        BindingFlags.NonPublic |
                                        BindingFlags.Public |
                                        BindingFlags.FlattenHierarchy)
                                    .Where(f => (f.IsInitOnly || f.IsLiteral) && f.FieldType == stringType && IsDefined(f, attributeType))
                                    .Select(f => CSharpSyntaxTree.ParseText((string)f.GetValue(null) !))
                                    .ToArray()));
        }
Example #2
0
        /// <summary>
        /// Runs the specified <paramref name="analyzer"/> and returns an <see cref="ImmutableArray{T}"/> of produced <see cref="Diagnostic"/>s.
        /// </summary>
        /// <param name="analyzer"><see cref="DiagnosticAnalyzer"/> to run.</param>
        /// <param name="tree">A <see cref="CSharpSyntaxTree"/> the analysis should be performed on.</param>
        public static Task <ImmutableArray <Diagnostic> > RunAnalyzer(this DiagnosticAnalyzer analyzer, CSharpSyntaxTree?tree)
        {
            CSharpCompilation compilation = RoslynUtilities.CreateBaseCompilation();

            if (tree is not null)
            {
                compilation = compilation.AddSyntaxTrees(tree);
            }

            return(RunAnalyzer(analyzer, compilation));
        }
Example #3
0
        /// <summary>
        /// Runs the specified <paramref name="analyzer"/> and returns an <see cref="ImmutableArray{T}"/> of produced <see cref="Diagnostic"/>s.
        /// </summary>
        /// <param name="analyzer"><see cref="DiagnosticAnalyzer"/> to run.</param>
        /// <param name="source">A <see cref="string"/> representing a <see cref="CSharpSyntaxTree"/> the analysis should be performed on.</param>
        public static Task <ImmutableArray <Diagnostic> > RunAnalyzer(this DiagnosticAnalyzer analyzer, string?source)
        {
            CSharpCompilation compilation = RoslynUtilities.CreateBaseCompilation();

            if (!string.IsNullOrWhiteSpace(source))
            {
                compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(source !));
            }

            return(RunAnalyzer(analyzer, compilation));
        }
        /// <summary>
        /// Performs a test of the specified <paramref name="generator"/>.
        /// </summary>
        /// <param name="generator"><see cref="ISourceGenerator"/> to run a test of.</param>
        /// <param name="resultProvider">A delegate that creates the target <see cref="IGeneratorTestResult"/> from the data provided by the tested <paramref name="generator"/>.</param>
        /// <param name="source">A <see cref="string"/> that will be parsed as a <see cref="CSharpSyntaxTree"/> and added to the input <see cref="CSharpCompilation"/> of the tested <paramref name="generator"/>.</param>
        /// <param name="assemblies">An array of <see cref="Assembly"/> instances to be referenced by the input <see cref="CSharpCompilation"/> of the tested <paramref name="generator"/>.</param>
        /// <typeparam name="TResult">Type of <see cref="IGeneratorTestResult"/> to create after the test is run.</typeparam>
        /// <exception cref="ArgumentNullException"><paramref name="generator"/> is <see langword="null"/>. -or- <paramref name="resultProvider"/> is <see langword="null"/>.</exception>
        public static TResult RunTest <TResult>(this ISourceGenerator generator, GeneratorTestResultProvider <TResult> resultProvider, string?source, params Assembly[]?assemblies) where TResult : IGeneratorTestResult
        {
            if (generator is null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            CSharpCompilation compilation = RoslynUtilities.CreateCompilationWithAssemblies(source, assemblies);

            return(CSharpGeneratorDriver.Create(generator).RunTest(resultProvider, compilation));
        }
        /// <summary>
        /// Performs a test of the specified <paramref name="generator"/>.
        /// </summary>
        /// <param name="generator"><see cref="ISourceGenerator"/> to run a test of.</param>
        /// <param name="resultProvider">A delegate that creates the target <see cref="IGeneratorTestResult"/> from the data provided by the tested <paramref name="generator"/>.</param>
        /// <param name="sources">A collection of <see cref="string"/>s that will be parsed as <see cref="CSharpSyntaxTree"/>s and added to the input <see cref="CSharpCompilation"/> of the tested <paramref name="generator"/>.</param>
        /// <param name="assemblies">An array of <see cref="Assembly"/> instances to be referenced by the input <see cref="CSharpCompilation"/> of the tested <paramref name="generator"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="generator"/> is <see langword="null"/>. -or- <paramref name="resultProvider"/> is <see langword="null"/>.</exception>
        public static IGeneratorTestResult RunTest(this ISourceGenerator generator, GeneratorTestResultProvider resultProvider, IEnumerable <string>?sources, params Assembly[]?assemblies)
        {
            if (generator is null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            CSharpCompilation compilation = RoslynUtilities.CreateCompilationWithAssemblies(sources, assemblies);

            return(CSharpGeneratorDriver.Create(generator).RunTest(resultProvider, compilation));
        }
 /// <summary>
 /// Creates new <see cref="CSharpSyntaxTree"/>s from the marked <c>static readonly</c> or <c>const</c> <see cref="string"/> fields in the specified <paramref name="sourceType"/>
 /// and returns a new <see cref="CSharpCompilation"/> that contains them.
 /// </summary>
 /// <param name="sourceType"><see cref="Type"/> to collect the <see cref="string"/> fields from.</param>
 public static CSharpCompilation Collect(Type?sourceType)
 {
     return(Collect(sourceType, RoslynUtilities.CreateCompilationWithAssemblies(sources: null, null)));
 }
Example #7
0
 /// <summary>
 /// Performs a test on all <see cref="ISourceGenerator"/>s registered in the provided <paramref name="generatorDriver"/>.
 /// </summary>
 /// <param name="generatorDriver"><see cref="CSharpGeneratorDriver"/> to run a tests on all registered <see cref="ISourceGenerator"/> of.</param>
 /// <param name="resultProvider">A delegate that creates the target <see cref="IGeneratorTestResult"/> from the data provided by the tested <paramref name="generatorDriver"/>.</param>
 /// <param name="sources">A collection of <see cref="string"/>s that will be parsed as <see cref="CSharpSyntaxTree"/>s and added to the input <see cref="CSharpCompilation"/> of the tested <paramref name="generatorDriver"/>.</param>
 /// <param name="assemblies">An array of <see cref="Assembly"/> instances to be referenced by the input <see cref="CSharpCompilation"/> of the tested <paramref name="generatorDriver"/>.</param>
 /// <typeparam name="TResult">Type of <see cref="IGeneratorTestResult"/> to create after the test is run.</typeparam>
 /// <exception cref="ArgumentNullException"><paramref name="generatorDriver"/> is <see langword="null"/>. -or- <paramref name="resultProvider"/> is <see langword="null"/>.</exception>
 public static TResult RunTest <TResult>(this CSharpGeneratorDriver generatorDriver, GeneratorTestResultProvider <TResult> resultProvider, IEnumerable <string>?sources, params Assembly[]?assemblies) where TResult : IGeneratorTestResult
 {
     return(RunTest(generatorDriver, resultProvider, RoslynUtilities.CreateCompilationWithAssemblies(sources, assemblies)));
 }
Example #8
0
 /// <summary>
 /// Performs a test on all <see cref="ISourceGenerator"/>s registered in the provided <paramref name="generatorDriver"/>.
 /// </summary>
 /// <param name="generatorDriver"><see cref="CSharpGeneratorDriver"/> to run a tests on all registered <see cref="ISourceGenerator"/> of.</param>
 /// <param name="resultProvider">A delegate that creates the target <see cref="IGeneratorTestResult"/> from the data provided by the tested <paramref name="generatorDriver"/>.</param>
 /// <param name="source">A <see cref="string"/> that will be parsed as a <see cref="CSharpSyntaxTree"/> and added to the input <see cref="CSharpCompilation"/> of the tested <paramref name="generatorDriver"/>.</param>
 /// <param name="assemblies">An array of <see cref="Assembly"/> instances to be referenced by the input <see cref="CSharpCompilation"/> of the tested <paramref name="generatorDriver"/>.</param>
 /// <exception cref="ArgumentNullException"><paramref name="generatorDriver"/> is <see langword="null"/>. -or- <paramref name="resultProvider"/> is <see langword="null"/>.</exception>
 public static IGeneratorTestResult RunTest(this CSharpGeneratorDriver generatorDriver, GeneratorTestResultProvider resultProvider, string?source, params Assembly[]?assemblies)
 {
     return(RunTest(generatorDriver, resultProvider, RoslynUtilities.CreateCompilationWithAssemblies(source, assemblies)));
 }