Skip to content

Commit

Permalink
prep to inject test config + mahapps
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony committed Jun 3, 2024
1 parent 43011dd commit c3b186f
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Unit Tests for the ViewBinding Model Source Generator for MahApps Metro.
/// </summary>
public static class MahAppsMetroViewBindingModelGeneratorTests
{
/// <inheritdoc />
public sealed class ExecuteMethod : BaseGeneratorTests.BaseExecuteMethod<WpfControlBindingModelSourceGenerator, ControlBindingModelGeneratorProcessor>
{
/// <summary>
/// Initializes a new instance of the <see cref="ExecuteMethod"/> class.
/// </summary>
/// <param name="output">Test Output Helper.</param>
public ExecuteMethod(ITestOutputHelper output)
: base(output)
{
}

/// <inheritdoc />
protected override AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider()
{
return null;
}

/// <inheritdoc />
protected override void AddReferenceAssemblies(IList<MetadataReference> metadataReferences)
{
if (metadataReferences == null)

Check warning on line 43 in src/Vetuviem.IntegrationTests/ReactiveUI/WPF/MahAppsMetroViewBindingModelGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)

Check warning on line 43 in src/Vetuviem.IntegrationTests/ReactiveUI/WPF/MahAppsMetroViewBindingModelGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)

Check warning on line 43 in src/Vetuviem.IntegrationTests/ReactiveUI/WPF/MahAppsMetroViewBindingModelGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)

Check warning on line 43 in src/Vetuviem.IntegrationTests/ReactiveUI/WPF/MahAppsMetroViewBindingModelGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)
{
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);
}
}

/// <inheritdoc />
protected override Func<WpfControlBindingModelSourceGenerator> GetFactory()
{
return () => new WpfControlBindingModelSourceGenerator();
}

private static string[]? GetPlatformAssemblyPaths()
{
if (AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") is string trustedPlatformAssemblies)
{
return trustedPlatformAssemblies.Split(Path.PathSeparator);
}

return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,12 @@ public ExecuteMethod(ITestOutputHelper output)
{
}

/// <inheritdoc />
protected override AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider()
{
return null;
}

/// <inheritdoc />
protected override void AddReferenceAssemblies(IList<MetadataReference> metadataReferences)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,12 @@ public ExecuteMethod(ITestOutputHelper output)
{
}

/// <inheritdoc />
protected override AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider()
{
return null;
}

/// <inheritdoc />
protected override void AddReferenceAssemblies(IList<MetadataReference> metadataReferences)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,12 @@ public ExecuteMethod(ITestOutputHelper output)
{
}

/// <inheritdoc />
protected override AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider()
{
return null;
}

/// <inheritdoc />
protected override void AddReferenceAssemblies(IList<MetadataReference> metadataReferences)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<IsPackable>False</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MahApps.Metro" Version="2.4.10" />
<PackageReference Include="MahApps.Metro.SimpleChildWindow" Version="2.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Vetuviem.Testing\Vetuviem.Testing.csproj" />
<ProjectReference Include="..\Vetuviem.Winforms.SourceGenerator\Vetuviem.Winforms.SourceGenerator.csproj" />
Expand Down
34 changes: 30 additions & 4 deletions src/Vetuviem.Testing/BaseGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -79,6 +83,12 @@ public void GeneratesCode()
Assert.False(hasErrors);
}

/// <summary>
/// Gets the analyzer config options provider to test with.
/// </summary>
/// <returns>Analyzer Config Options.</returns>
protected abstract AnalyzerConfigOptionsProvider? GetAnalyzerConfigOptionsProvider();

/// <summary>
/// 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
Expand All @@ -99,15 +109,31 @@ private static Compilation CreateCompilation(string source, IEnumerable<Metadata
references: reference,
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication));

private static GeneratorDriver CreateDriver(Compilation compilation, params ISourceGenerator[] generators) => CSharpGeneratorDriver.Create(
private static GeneratorDriver CreateDriver(
Compilation compilation,
AnalyzerConfigOptionsProvider? analyzerConfigOptionsProvider,
params ISourceGenerator[] generators) => CSharpGeneratorDriver.Create(
generators: ImmutableArray.Create(generators),
additionalTexts: ImmutableArray<AdditionalText>.Empty,
parseOptions: (CSharpParseOptions)compilation.SyntaxTrees.First().Options,
optionsProvider: null);
optionsProvider: analyzerConfigOptionsProvider);

private static Compilation RunGenerators(Compilation compilation, out ImmutableArray<Diagnostic> diagnostics, params ISourceGenerator[] generators)
private static Compilation RunGenerators(
Compilation compilation,
AnalyzerConfigOptionsProvider? analyzerConfigOptionsProvider,
out ImmutableArray<Diagnostic> 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;
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/Vetuviem.Testing/TestAnalyzerConfigOptionsProvider.cs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c3b186f

Please sign in to comment.