Skip to content

Commit

Permalink
Merge branch 'feature-864' of https://github.com/simpleinjector/Simpl…
Browse files Browse the repository at this point in the history
…eInjector into v5.3.x
  • Loading branch information
dotnetjunkie committed Jan 7, 2021
2 parents 4dc1274 + 09cc59b commit ad212a6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ public void Analyze_TransientRegistrationForDisposableComponent_Warning()

container.Verify(VerificationOption.VerifyOnly);

// Act
var results = Analyzer.Analyze(container).OfType<DisposableTransientComponentDiagnosticResult>()
.ToArray();

// Assert
Assert.AreEqual(1, results.Length, Actual(results));
Assert.AreEqual(
"DisposablePlugin is registered for IPlugin as transient, but implements IDisposable.",
results.Single().Description,
Actual(results));
}

[TestMethod]
public void Analyze_TransientRegistrationForConcreteDisposableComponent_Warning()
{
// Arrange
var container = new Container();

container.Register<DisposablePlugin>();

container.Verify(VerificationOption.VerifyOnly);

// Act
var results = Analyzer.Analyze(container).OfType<DisposableTransientComponentDiagnosticResult>()
.ToArray();
Expand Down Expand Up @@ -54,7 +76,7 @@ public void Analyze_TransientRegistrationForAsyncDisposableComponent_Warning()
// Assert
Assert.AreEqual(1, results.Length, Actual(results));
Assert.AreEqual(
"AsyncDisposablePlugin is registered as transient, but implements IAsyncDisposable.",
"AsyncDisposablePlugin is registered for IPlugin as transient, but implements IAsyncDisposable.",
results.Single().Description,
Actual(results));
}
Expand All @@ -76,7 +98,7 @@ public void Analyze_TransientRegistrationForComponentImplementingBothIDisposable
// Assert
Assert.AreEqual(1, results.Length, Actual(results));
Assert.AreEqual(
"DisposableAsyncDisposablePlugin is registered as transient, but implements IDisposable.",
"DisposableAsyncDisposablePlugin is registered for IPlugin as transient, but implements IDisposable.",
results.Single().Description,
Actual(results));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,22 @@ where registration.ShouldNotBeSuppressed(this.DiagnosticType)
var results =
from producer in invalidProducers
select new DisposableTransientComponentDiagnosticResult(
producer.ServiceType, producer, BuildDescription(producer.Registration.ImplementationType));
producer.ServiceType, producer, BuildDescription(producer));

return results.ToArray();
}

private static string BuildDescription(Type implementationType) =>
private static string BuildDescription(InstanceProducer producer) =>
string.Format(
CultureInfo.InvariantCulture,
"{0} is registered as transient, but implements {1}.",
implementationType.FriendlyName(),
typeof(IDisposable).IsAssignableFrom(implementationType) ? "IDisposable" : "IAsyncDisposable");
"{0} is registered {1}as transient, but implements {2}.",
producer.Registration.ImplementationType.FriendlyName(),
producer.ServiceType == producer.Registration.ImplementationType
? string.Empty
: $"for {producer.ServiceType.FriendlyName()} ",
typeof(IDisposable).IsAssignableFrom(producer.Registration.ImplementationType)
? "IDisposable"
: "IAsyncDisposable");

private static string ComponentPlural(int number) => number == 1 ? "component" : "components";
}
Expand Down

0 comments on commit ad212a6

Please sign in to comment.