Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions src/DependencyModules.Testing/Impl/TestParameterResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ namespace DependencyModules.Testing.Impl;
public sealed class TestParameterResolver
{
private readonly ITestMethodContext _testMethod;

// ParameterInfo compares by reference, and two threads that call GetParameters on the same
// method for the first time can each get a different array. Both phases use this one array.
private readonly ParameterInfo[] _parameters;
private readonly Dictionary<ParameterInfo, List<ITestParameterValueProvider>> _valueProviders =
new();
private bool _setupRan;
Expand All @@ -34,6 +38,7 @@ public sealed class TestParameterResolver
public TestParameterResolver(ITestMethodContext testMethod)
{
_testMethod = testMethod;
_parameters = testMethod.Method.GetParameters();
}

/// <summary>
Expand All @@ -47,7 +52,7 @@ public TestParameterResolver(ITestMethodContext testMethod)
/// <param name="serviceCollection">The collection backing the test's container.</param>
public void SetupServiceCollection(IServiceCollection serviceCollection)
{
foreach (var parameterInfo in _testMethod.Method.GetParameters())
foreach (var parameterInfo in _parameters)
{
var providers = parameterInfo
.GetCustomAttributes()
Expand Down Expand Up @@ -98,7 +103,7 @@ public void SetupServiceCollection(IServiceCollection serviceCollection)
);
}

var parameterList = _testMethod.Method.GetParameters();
var parameterList = _parameters;
var arguments = new List<object?>(data);

for (var i = data.Length; i < parameterList.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
using DependencyModules.Testing.Attributes.Interfaces;
using DependencyModules.Testing.Impl;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace DependencyModules.Tests.TestingTests;
namespace DependencyModules.Specs.TestingTests;

/// <summary>
/// Which services a test pins, decided without a container or a test framework in the way.
Expand All @@ -14,7 +13,7 @@ namespace DependencyModules.Tests.TestingTests;
/// One test per row of the rule, because the rule has been wrong once already and the way it was
/// wrong was a case nobody had written down.
/// </remarks>
public class SharedRegistrationsTests
public partial class SharedRegistrationsTests
{
private interface IThing;

Expand Down Expand Up @@ -75,80 +74,80 @@ private static void None() { }
/// The row the first version of this rule got wrong, and the one a scaffolded project hits
/// first: a plain application service the handler writes to and the test reads.
/// </summary>
[Fact]
[VerifyTest]
public void AnUnmarkedParameterIsPinned()
{
Assert.Equal([typeof(IThing)], Collect(nameof(Plain)));
Verify(Collect(nameof(Plain)).SequenceEqual([typeof(IThing)]));
}

[Fact]
[VerifyTest]
public void EveryParameterIsPinned()
{
Assert.Equal([typeof(IThing), typeof(IOther)], Collect(nameof(Bare)));
Verify(Collect(nameof(Bare)).SequenceEqual([typeof(IThing), typeof(IOther)]));
}

/// <summary>A mock needs no attribute to be pinned, which is what makes the rule general.</summary>
[Fact]
[VerifyTest]
public void AMockIsPinnedLikeAnythingElse()
{
Assert.Equal([typeof(IThing)], Collect(nameof(Mocked)));
Verify(Collect(nameof(Mocked)).SequenceEqual([typeof(IThing)]));
}

/// <summary>Redundant now, and still allowed: it is how a driving parameter asks for reuse.</summary>
[Fact]
[VerifyTest]
public void SharedOnAValueParameterChangesNothing()
{
Assert.Equal(Collect(nameof(Plain)), Collect(nameof(Marked)));
Verify(Collect(nameof(Marked)).SequenceEqual(Collect(nameof(Plain))));
}

[Fact]
[VerifyTest]
public void ATestWithNoParametersPinsNothing()
{
Assert.Empty(Collect(nameof(None)));
Verify(Collect(nameof(None)).Count == 0);
}

// ---------------------------------------------------------------- the exceptions

/// <summary>
/// The parameter that drives the application is not pinned, because it builds the containers.
/// </summary>
[Fact]
[VerifyTest]
public void AParameterTheHarnessDrivesWithIsNotPinned()
{
var pinned = Collect(nameof(Driving), new DrivenByAttribute());

Assert.Equal([typeof(IThing)], pinned);
Verify(pinned.SequenceEqual([typeof(IThing)]));
}

/// <summary>
/// And it stays unpinned even asked to be. Pinning a driver is not a preference that could go
/// either way; it turns the isolation off while the test believes it is on.
/// </summary>
[Fact]
[VerifyTest]
public void IsolatedWinsOverAnExplicitShared()
{
Assert.Empty(Collect(nameof(DrivingAndMarked), new DrivenByAttribute()));
Verify(Collect(nameof(DrivingAndMarked), new DrivenByAttribute()).Count == 0);
}

[Fact]
[VerifyTest]
public void AnAttributeDecliningUnpinsItsParameter()
{
Assert.Empty(Collect(nameof(Declined)));
Verify(Collect(nameof(Declined)).Count == 0);
}

/// <summary>The container itself is the one question pinning cannot answer.</summary>
[Fact]
[VerifyTest]
public void TheServiceProviderIsNeverPinned()
{
Assert.Equal([typeof(IThing)], Collect(nameof(Container)));
Verify(Collect(nameof(Container)).SequenceEqual([typeof(IThing)]));
}

// ---------------------------------------------------------------- what no parameter holds

/// <summary>
/// A harness keeps its own per-test services by naming them, since no parameter holds them.
/// </summary>
[Fact]
[VerifyTest]
public void AnAttributeCanPinWhatNoParameterHolds()
{
var shared = new TestExportAttribute(typeof(IOther))
Expand All @@ -157,27 +156,27 @@ public void AnAttributeCanPinWhatNoParameterHolds()
Shared = true,
};

Assert.Equal([typeof(IThing), typeof(IOther)], Collect(nameof(Plain), shared));
Verify(Collect(nameof(Plain), shared).SequenceEqual([typeof(IThing), typeof(IOther)]));
}

/// <summary>An export nothing holds stays per container until it asks.</summary>
[Fact]
[VerifyTest]
public void AnExportIsNotPinnedUnlessItAsks()
{
var isolated = new TestExportAttribute(typeof(IOther)) { Implementation = typeof(Thing) };

Assert.Equal([typeof(IThing)], Collect(nameof(Plain), isolated));
Verify(Collect(nameof(Plain), isolated).SequenceEqual([typeof(IThing)]));
}

/// <summary>
/// The parameter is the narrower statement and wins, which is how every other precedence in the
/// harness resolves. Isolating it would recreate the bug the rule exists to fix.
/// </summary>
[Fact]
[VerifyTest]
public void AnExportTheTestHoldsIsPinnedEvenWhenItDeclined()
{
var isolated = new TestExportAttribute(typeof(IThing)) { Implementation = typeof(Thing) };

Assert.Equal([typeof(IThing)], Collect(nameof(Plain), isolated));
Verify(Collect(nameof(Plain), isolated).SequenceEqual([typeof(IThing)]));
}
}
Loading
Loading