|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
3 | 3 |
|
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | + |
4 | 6 | public abstract class GeneratorTestBase : IDisposable, IAsyncLifetime
|
5 | 7 | {
|
6 | 8 | protected const string DefaultTFM = "netstandard2.0";
|
@@ -215,6 +217,20 @@ protected CSharpCompilation AddGeneratedCode(CSharpCompilation compilation, IGen
|
215 | 217 | return compilation.AddSyntaxTrees(syntaxTrees);
|
216 | 218 | }
|
217 | 219 |
|
| 220 | + /// <summary> |
| 221 | + /// Adds a code file to a compilation. |
| 222 | + /// </summary> |
| 223 | + /// <param name="code">The syntax file to add.</param> |
| 224 | + /// <param name="fileName">The name of the code file to add.</param> |
| 225 | + /// <param name="compilation">The compilation to add to. When omitted, <see cref="GeneratorTestBase.compilation"/> is assumed.</param> |
| 226 | + /// <returns>The modified compilation.</returns> |
| 227 | + protected CSharpCompilation AddCode([StringSyntax("c#-test")] string code, string? fileName = null, CSharpCompilation? compilation = null) |
| 228 | + { |
| 229 | + compilation ??= this.compilation; |
| 230 | + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code, this.parseOptions, fileName ?? $"AdditionalCode{compilation.SyntaxTrees.Length + 1}.cs"); |
| 231 | + return compilation.AddSyntaxTrees(syntaxTree); |
| 232 | + } |
| 233 | + |
218 | 234 | protected void CollectGeneratedCode(IGenerator generator) => this.compilation = this.AddGeneratedCode(this.compilation, generator);
|
219 | 235 |
|
220 | 236 | protected IEnumerable<MethodDeclarationSyntax> FindGeneratedMethod(string name, Compilation? compilation = null) => (compilation ?? this.compilation).SyntaxTrees.SelectMany(st => st.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>()).Where(md => md.Identifier.ValueText == name);
|
@@ -243,7 +259,7 @@ protected CSharpCompilation AddGeneratedCode(CSharpCompilation compilation, IGen
|
243 | 259 |
|
244 | 260 | protected void AssertNoDiagnostics(bool logAllGeneratedCode = true) => this.AssertNoDiagnostics(this.compilation, logAllGeneratedCode);
|
245 | 261 |
|
246 |
| - protected void AssertNoDiagnostics(CSharpCompilation compilation, bool logAllGeneratedCode = true) |
| 262 | + protected void AssertNoDiagnostics(CSharpCompilation compilation, bool logAllGeneratedCode = true, Func<Diagnostic, bool>? acceptable = null) |
247 | 263 | {
|
248 | 264 | var diagnostics = FilterDiagnostics(compilation.GetDiagnostics());
|
249 | 265 | this.logger.WriteLine($"{diagnostics.Length} diagnostics reported.");
|
@@ -274,7 +290,7 @@ protected void AssertNoDiagnostics(CSharpCompilation compilation, bool logAllGen
|
274 | 290 | }
|
275 | 291 | }
|
276 | 292 |
|
277 |
| - Assert.Empty(diagnostics); |
| 293 | + Assert.Empty(acceptable is null ? diagnostics : diagnostics.Where(d => !acceptable(d))); |
278 | 294 | if (emitSuccessful.HasValue)
|
279 | 295 | {
|
280 | 296 | Assert.Empty(emitDiagnostics);
|
|
0 commit comments