diff --git a/src/Vetuviem.IntegrationTests/ReactiveUI/WPF/MahAppsMetroViewBindingModelGeneratorTests.cs b/src/Vetuviem.IntegrationTests/ReactiveUI/WPF/MahAppsMetroViewBindingModelGeneratorTests.cs new file mode 100644 index 00000000..9e9b1d23 --- /dev/null +++ b/src/Vetuviem.IntegrationTests/ReactiveUI/WPF/MahAppsMetroViewBindingModelGeneratorTests.cs @@ -0,0 +1,82 @@ +// Copyright (c) 2022 DPVreony and Contributors. All rights reserved. +// DPVreony and Contributors licenses this file to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using Vetuviem.SourceGenerator.Features.ControlBindingModels; +using Vetuviem.Testing; +using Vetuviem.WPF.SourceGenerator; +using Xunit.Abstractions; + +namespace Vetuviem.IntegrationTests.ReactiveUI.WPF +{ + /// + /// Unit Tests for the ViewBinding Model Source Generator for MahApps Metro. + /// + public static class MahAppsMetroViewBindingModelGeneratorTests + { + /// + public sealed class ExecuteMethod : BaseGeneratorTests.BaseExecuteMethod + { + /// + /// Initializes a new instance of the class. + /// + /// Test Output Helper. + public ExecuteMethod(ITestOutputHelper output) + : base(output) + { + } + + /// + protected override AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider() + { + return null; + } + + /// + protected override void AddReferenceAssemblies(IList metadataReferences) + { + if (metadataReferences == null) + { + throw new ArgumentNullException(nameof(metadataReferences)); + } + + var trustedAssembliesPaths = GetPlatformAssemblyPaths(); + if (trustedAssembliesPaths == null) + { + return; + } + + foreach (string trustedAssembliesPath in trustedAssembliesPaths) + { + var metadataReference = MetadataReference.CreateFromFile(trustedAssembliesPath); + if (metadataReference == null) + { + throw new InvalidOperationException($"Failed to create metadata reference for {trustedAssembliesPath}"); + } + metadataReferences.Add(metadataReference); + } + } + + /// + protected override Func GetFactory() + { + return () => new WpfControlBindingModelSourceGenerator(); + } + + private static string[]? GetPlatformAssemblyPaths() + { + if (AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") is string trustedPlatformAssemblies) + { + return trustedPlatformAssemblies.Split(Path.PathSeparator); + } + + return null; + } + } + } +} diff --git a/src/Vetuviem.IntegrationTests/ReactiveUI/WPF/WpfViewBindingModelGeneratorTests.cs b/src/Vetuviem.IntegrationTests/ReactiveUI/WPF/WpfViewBindingModelGeneratorTests.cs index e6a664f6..0a4871c2 100644 --- a/src/Vetuviem.IntegrationTests/ReactiveUI/WPF/WpfViewBindingModelGeneratorTests.cs +++ b/src/Vetuviem.IntegrationTests/ReactiveUI/WPF/WpfViewBindingModelGeneratorTests.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; using Vetuviem.SourceGenerator.Features.ControlBindingModels; using Vetuviem.Testing; using Vetuviem.WPF.SourceGenerator; @@ -30,6 +31,12 @@ public ExecuteMethod(ITestOutputHelper output) { } + /// + protected override AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider() + { + return null; + } + /// protected override void AddReferenceAssemblies(IList metadataReferences) { diff --git a/src/Vetuviem.IntegrationTests/ReactiveUI/WinUi3/WinUi3ViewBindingModelGeneratorTests.cs b/src/Vetuviem.IntegrationTests/ReactiveUI/WinUi3/WinUi3ViewBindingModelGeneratorTests.cs index 7d0de060..7415ae19 100644 --- a/src/Vetuviem.IntegrationTests/ReactiveUI/WinUi3/WinUi3ViewBindingModelGeneratorTests.cs +++ b/src/Vetuviem.IntegrationTests/ReactiveUI/WinUi3/WinUi3ViewBindingModelGeneratorTests.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; using Vetuviem.SourceGenerator.Features.ControlBindingModels; using Vetuviem.Testing; using Vetuviem.WinUi.SourceGenerator; @@ -29,6 +30,12 @@ public ExecuteMethod(ITestOutputHelper output) { } + /// + protected override AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider() + { + return null; + } + /// protected override void AddReferenceAssemblies(IList metadataReferences) { diff --git a/src/Vetuviem.IntegrationTests/ReactiveUI/Winforms/WinformsViewBindingModelGeneratorTests.cs b/src/Vetuviem.IntegrationTests/ReactiveUI/Winforms/WinformsViewBindingModelGeneratorTests.cs index cdfaa380..193cb4e5 100644 --- a/src/Vetuviem.IntegrationTests/ReactiveUI/Winforms/WinformsViewBindingModelGeneratorTests.cs +++ b/src/Vetuviem.IntegrationTests/ReactiveUI/Winforms/WinformsViewBindingModelGeneratorTests.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; using Vetuviem.SourceGenerator.Features.ControlBindingModels; using Vetuviem.Testing; using Vetuviem.Winforms.SourceGenerator; @@ -30,6 +31,12 @@ public ExecuteMethod(ITestOutputHelper output) { } + /// + protected override AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider() + { + return null; + } + /// protected override void AddReferenceAssemblies(IList metadataReferences) { diff --git a/src/Vetuviem.IntegrationTests/Vetuviem.IntegrationTests.csproj b/src/Vetuviem.IntegrationTests/Vetuviem.IntegrationTests.csproj index a1ec0365..726ebe9f 100644 --- a/src/Vetuviem.IntegrationTests/Vetuviem.IntegrationTests.csproj +++ b/src/Vetuviem.IntegrationTests/Vetuviem.IntegrationTests.csproj @@ -6,6 +6,11 @@ False + + + + + diff --git a/src/Vetuviem.Testing/BaseGeneratorTests.cs b/src/Vetuviem.Testing/BaseGeneratorTests.cs index 1c31cae4..3b0e2f70 100644 --- a/src/Vetuviem.Testing/BaseGeneratorTests.cs +++ b/src/Vetuviem.Testing/BaseGeneratorTests.cs @@ -10,6 +10,7 @@ using System.Reflection; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.Extensions.Logging; using Vetuviem.SourceGenerator; using Vetuviem.SourceGenerator.Features.Core; @@ -61,8 +62,11 @@ public void GeneratesCode() var comp = CreateCompilation(string.Empty, references); + var analyzerConfigOptionsProvider = GetAnalyzerConfigOptionsProvider(); + var newComp = RunGenerators( comp, + analyzerConfigOptionsProvider, out var generatorDiags, instance); @@ -79,6 +83,12 @@ public void GeneratesCode() Assert.False(hasErrors); } + /// + /// Gets the analyzer config options provider to test with. + /// + /// Analyzer Config Options. + protected abstract AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider(); + /// /// Allows addition of platform specific metadata references. Unit Tests start in an agnostic fashion /// with no specific references loaded. Source generators typically take these via MSBuild loading @@ -99,15 +109,31 @@ private static Compilation CreateCompilation(string source, IEnumerable CSharpGeneratorDriver.Create( + private static GeneratorDriver CreateDriver( + Compilation compilation, + AnalyzerConfigOptionsProvider? analyzerConfigOptionsProvider, + params ISourceGenerator[] generators) => CSharpGeneratorDriver.Create( generators: ImmutableArray.Create(generators), additionalTexts: ImmutableArray.Empty, parseOptions: (CSharpParseOptions)compilation.SyntaxTrees.First().Options, - optionsProvider: null); + optionsProvider: analyzerConfigOptionsProvider); - private static Compilation RunGenerators(Compilation compilation, out ImmutableArray diagnostics, params ISourceGenerator[] generators) + private static Compilation RunGenerators( + Compilation compilation, + AnalyzerConfigOptionsProvider? analyzerConfigOptionsProvider, + out ImmutableArray diagnostics, + params ISourceGenerator[] generators) { - CreateDriver(compilation, generators).RunGeneratorsAndUpdateCompilation(compilation, out var updatedCompilation, out diagnostics); + var driver = CreateDriver( + compilation, + analyzerConfigOptionsProvider, + generators); + + driver.RunGeneratorsAndUpdateCompilation( + compilation, + out var updatedCompilation, + out diagnostics); + return updatedCompilation; } } diff --git a/src/Vetuviem.Testing/TestAnalyzerConfigOptionsProvider.cs b/src/Vetuviem.Testing/TestAnalyzerConfigOptionsProvider.cs new file mode 100644 index 00000000..7270b9b7 --- /dev/null +++ b/src/Vetuviem.Testing/TestAnalyzerConfigOptionsProvider.cs @@ -0,0 +1,25 @@ +#if TBC +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace Vetuviem.Testing +{ + public sealed class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider + { + public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) + { + throw new NotImplementedException(); + } + + public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) + { + throw new NotImplementedException(); + } + + public override AnalyzerConfigOptions GlobalOptions { get; } + } +} +#endif