Skip to content

Commit

Permalink
Give DiagnosticsMessageSink to fixture if requested (#12)
Browse files Browse the repository at this point in the history
looks good.
  • Loading branch information
pergardebrink authored May 25, 2024
1 parent 21099c1 commit 4ae170c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/TestAssemblyRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ await Aggregator.RunAsync(async () =>
foreach (Type fixtureAttribute in assemblyFixtures)
{
Type fixtureType = fixtureAttribute.GetGenericArguments()[0];
var hasConstructorWithMessageSink = fixtureType.GetConstructor(new[] { typeof(IMessageSink) }) != null;
_assemblyFixtureMappings[fixtureType] = hasConstructorWithMessageSink
? Activator.CreateInstance(fixtureType, ExecutionMessageSink)
: Activator.CreateInstance(fixtureType);
var hasExecutionMessageSink = fixtureType.GetConstructor(new[] { typeof(IMessageSink) }) != null;
var hasExecutionAndDiagnosticsMessageSink = fixtureType.GetConstructor(new[] { typeof(IMessageSink), typeof(IMessageSink) }) != null;

if (hasExecutionAndDiagnosticsMessageSink)
{
_assemblyFixtureMappings[fixtureType] = Activator.CreateInstance(fixtureType, ExecutionMessageSink, DiagnosticMessageSink);
} else if (hasExecutionMessageSink)
{
_assemblyFixtureMappings[fixtureType] = Activator.CreateInstance(fixtureType, ExecutionMessageSink);
} else
{
_assemblyFixtureMappings[fixtureType] = Activator.CreateInstance(fixtureType);
}
}

// Initialize IAsyncLifetime fixtures
Expand Down
40 changes: 40 additions & 0 deletions test/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ public void EnsureThatHaveIMessageSink()
}
}

public class Sample6 : IAssemblyFixture<MyAssemblyFixtureWithDiagnosticsAndExecutionMessageSink>
{
private readonly MyAssemblyFixtureWithDiagnosticsAndExecutionMessageSink _fixture;

public Sample6(MyAssemblyFixtureWithDiagnosticsAndExecutionMessageSink fixture)
{
_fixture = fixture;
}

[Fact]
public void EnsureThatHaveIMessageSinks()
{
Assert.NotNull(_fixture.ExecutionMessageSink);
Assert.NotNull(_fixture.DiagnosticsMessageSink);
}
}


public class MyAssemblyFixture : IDisposable
{
Expand Down Expand Up @@ -127,6 +144,29 @@ public void Dispose()
}
}

public class MyAssemblyFixtureWithDiagnosticsAndExecutionMessageSink : IDisposable
{
public static int InstantiationCount;

public IMessageSink ExecutionMessageSink { get; }

public IMessageSink DiagnosticsMessageSink { get; }

public MyAssemblyFixtureWithDiagnosticsAndExecutionMessageSink(IMessageSink executionMessageSink, IMessageSink diagnosticsMessageSink)
{
ExecutionMessageSink = executionMessageSink;
DiagnosticsMessageSink = diagnosticsMessageSink;
InstantiationCount++;
}

public void Dispose()
{
// Uncomment this and it will surface as an assembly cleanup failure
//throw new DivideByZeroException();
//InstantiationCount = 0;
}
}

public class MyAssemblyFixtureWithAsyncLifetime : IAsyncLifetime
{
public static int InstantiationCount;
Expand Down

0 comments on commit 4ae170c

Please sign in to comment.