// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Threading; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Testing.Verifiers; using Microsoft.CodeAnalysis.Testing; using Microsoft.CodeAnalysis; #if NET472 using System.ComponentModel.DataAnnotations; #endif namespace CommunityToolkit.Mvvm.SourceGenerators.UnitTests.Helpers; /// /// A custom that uses a specific C# language version to parse code. /// /// The type of the analyzer to test. internal sealed class CSharpAnalyzerWithLanguageVersionTest : CSharpAnalyzerTest where TAnalyzer : DiagnosticAnalyzer, new() { /// /// The C# language version to use to parse code. /// private readonly LanguageVersion languageVersion; /// /// Creates a new instance with the specified paramaters. /// /// The C# language version to use to parse code. private CSharpAnalyzerWithLanguageVersionTest(LanguageVersion languageVersion) { this.languageVersion = languageVersion; } /// protected override ParseOptions CreateParseOptions() { return new CSharpParseOptions(this.languageVersion, DocumentationMode.Diagnose); } /// /// The language version to use to run the test. public static Task VerifyAnalyzerAsync(string source, LanguageVersion languageVersion, params DiagnosticResult[] expected) { CSharpAnalyzerWithLanguageVersionTest test = new(languageVersion) { TestCode = source }; #if NET6_0_OR_GREATER test.TestState.ReferenceAssemblies = ReferenceAssemblies.Net.Net60; #else test.TestState.ReferenceAssemblies = ReferenceAssemblies.NetFramework.Net472.Default; test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(RequiredAttribute).Assembly.Location)); #endif test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(ObservableObject).Assembly.Location)); test.ExpectedDiagnostics.AddRange(expected); return test.RunAsync(CancellationToken.None); } }