Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement support for [Fact] methods that are instance methods on IDi…
…sposable types.
  • Loading branch information
jkoritzinsky authored and trylek committed Nov 4, 2021
commit 6d602f79f79a42b558cbc0a5e9c493d108ae5850
41 changes: 41 additions & 0 deletions src/tests/Common/XUnitWrapperGenerator/TestMethodInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.CodeAnalysis;

namespace XUnitWrapperGenerator;

interface ITestMethodInfo
{
string ExecutionStatement { get; }
}

sealed class StaticFactMethod : ITestMethodInfo
{
public StaticFactMethod(IMethodSymbol method)
{
ExecutionStatement = $"{method.ContainingType.ToDisplayString()}.{method.Name}()";
}

public string ExecutionStatement { get; }

public override bool Equals(object obj)
{
return obj is StaticFactMethod other && ExecutionStatement == other.ExecutionStatement;
}
}

sealed class InstanceFactMethod : ITestMethodInfo
{
public InstanceFactMethod(IMethodSymbol method)
{
ExecutionStatement = $"using ({method.ContainingType.ToDisplayString()} obj = new()) obj.{method.Name}();";
}

public string ExecutionStatement { get; }

public override bool Equals(object obj)
{
return obj is InstanceFactMethod other && ExecutionStatement == other.ExecutionStatement;
}
}
66 changes: 42 additions & 24 deletions src/tests/Common/XUnitWrapperGenerator/XUnitWrapperGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace XUnitWrapperGenerator
namespace XUnitWrapperGenerator;

[Generator]
public sealed class XUnitWrapperGenerator : IIncrementalGenerator
{
[Generator]
public sealed class XUnitWrapperGenerator : IIncrementalGenerator
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterImplementationSourceOutput(
context.SyntaxProvider.CreateSyntaxProvider(
static (node, ct) =>
node.IsKind(SyntaxKind.MethodDeclaration)
&& node is MethodDeclarationSyntax method
&& method.AttributeLists.Count > 0,
static (context, ct) => (IMethodSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!)
.SelectMany((method, ct) => ImmutableArray.CreateRange(GetTestMethodInfosForMethod(method)))
.Collect(),
static (context, methods) =>
{
// For simplicity, we'll use top-level statements for the generated Main method.
StringBuilder builder = new();
builder.AppendLine("try {");
builder.Append(string.Join("\n", methods.Select(m => m.ExecutionStatement)));
builder.AppendLine("} catch(System.Exception) { return 101; }");
builder.AppendLine("return 100;");
context.AddSource("Main.g.cs", builder.ToString());
});
}

private static IEnumerable<ITestMethodInfo> GetTestMethodInfosForMethod(IMethodSymbol method)
{
public void Initialize(IncrementalGeneratorInitializationContext context)
bool factAttribute = false;
foreach (var attr in method.GetAttributes())
{
context.RegisterImplementationSourceOutput(
context.SyntaxProvider.CreateSyntaxProvider(
static (node, ct) =>
node.IsKind(SyntaxKind.MethodDeclaration)
&& node is MethodDeclarationSyntax method
&& method.AttributeLists.Count > 0,
static (context, ct) => (IMethodSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!)
.Where(method => method.IsStatic && method.Parameters.IsEmpty && method.GetAttributes().Any(attr => attr.AttributeClass!.ToDisplayString() == "Xunit.FactAttribute"))
.Select((method, ct) => $"{method.ContainingType.ToDisplayString()}.{method.Name}")
.Collect(),
static (context, methods) =>
{
// For simplicity, we'll use top-level statements for the generated Main method.
StringBuilder builder = new();
builder.AppendLine("try {");
builder.Append(string.Join("\n", methods.Select(m => $"{m}();")));
builder.AppendLine("} catch(System.Exception) { return 101; }");
builder.AppendLine("return 100;");
context.AddSource("Main.g.cs", builder.ToString());
});
if (attr.AttributeClass!.ToDisplayString() == "Xunit.FactAttribute")
{
factAttribute = true;
}
}
if (factAttribute && method.Parameters.IsEmpty)
{
return new[] { method.IsStatic ? (ITestMethodInfo)new StaticFactMethod(method) : new InstanceFactMethod(method) };
}

return Enumerable.Empty<ITestMethodInfo>();
}
}