Skip to content

Commit

Permalink
Cover mutators
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBoike committed Feb 15, 2021
1 parent e946ab7 commit 91a923c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace NServiceBus.Core.Analyzer.Tests
using Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using NServiceBus.MessageMutator;
using NServiceBus.Pipeline;
using NServiceBus.Sagas;
using NUnit.Framework;
Expand All @@ -20,6 +21,7 @@ public class ForwardFromPipelineTests : DiagnosticVerifier
[SuppressMessage("Style", "IDE0001:Simplify Names", Justification = "Clarity of different type names")]
public void EachTypeHasABasicTest()
{
// These abstract classes are only extended internally
var ignoredTypes = new[] {
typeof(NServiceBus.Pipeline.ForkConnector<,>),
typeof(NServiceBus.Pipeline.PipelineTerminator<>),
Expand All @@ -42,21 +44,23 @@ public void EachTypeHasABasicTest()
.OrderBy(t => t.FullName)
.ToArray();

TestContext.WriteLine("Types that should have analyzer support:");
foreach (var t in pipelineTypes)
var coveredTypes = pipelineTypes.Intersect(typesCoveredByThisTest).ToArray();
var missingTestCases = pipelineTypes.Except(typesCoveredByThisTest).ToArray();

TestContext.WriteLine($"Types covered by a {nameof(RunTestOnType)} TestCase to ensure analyzer support:");
foreach (var t in coveredTypes)
{
TestContext.WriteLine(t.FullName);
}

TestContext.WriteLine();
TestContext.WriteLine($"Types covered by {nameof(RunTestOnType)}:");
foreach (var t in typesCoveredByThisTest)
TestContext.WriteLine($"Types missing a {nameof(RunTestOnType)} TestCase:");
foreach (var t in missingTestCases)
{
TestContext.WriteLine(t.FullName);
}

Assert.True(pipelineTypes.Intersect(typesCoveredByThisTest).Count() == pipelineTypes.Length,
$"One or more pipeline type(s) are not covered by the {nameof(RunTestOnType)} test in this class.");
Assert.AreEqual(0, missingTestCases.Length, $"One or more pipeline type(s) are not covered by the {nameof(RunTestOnType)} test in this class.");
}

[TestCase(typeof(IHandleMessages<>), "TestMessage", "Handle", "TestMessage message, IMessageHandlerContext context")]
Expand All @@ -65,11 +69,16 @@ public void EachTypeHasABasicTest()
[TestCase(typeof(IHandleSagaNotFound), null, "Handle", "object message, IMessageProcessingContext context")]
[TestCase(typeof(Behavior<>), "IIncomingLogicalMessageContext", "Invoke", "IIncomingLogicalMessageContext context, Func<Task> next")]
[TestCase(typeof(IBehavior<,>), "IIncomingPhysicalMessageContext, IIncomingLogicalMessageContext", "Invoke", "IIncomingPhysicalMessageContext context, Func<IIncomingLogicalMessageContext, Task> next")]
[TestCase(typeof(IMutateIncomingTransportMessages), null, "MutateIncoming", "MutateIncomingTransportMessageContext context")]
[TestCase(typeof(IMutateIncomingMessages), null, "MutateIncoming", "MutateIncomingMessageContext context")]
[TestCase(typeof(IMutateOutgoingTransportMessages), null, "MutateOutgoing", "MutateOutgoingTransportMessageContext context")]
[TestCase(typeof(IMutateOutgoingMessages), null, "MutateOutgoing", "MutateOutgoingMessageContext context")]
public Task RunTestOnType(Type type, string genericTypeArgs, string methodName, string methodArguments)
{
const string sourceFormat =
@"
using NServiceBus;
using NServiceBus.MessageMutator;
using NServiceBus.Pipeline;
using NServiceBus.Sagas;
using System;
Expand Down Expand Up @@ -101,7 +110,7 @@ public class TestTimeout {}
code = code.Replace("public Task", "public override Task");
}

var expected = NotForwardedAt(12, 16);
var expected = NotForwardedAt(13, 16);

TestContext.WriteLine($"Source Code for test case: {type.FullName}:");
TestContext.WriteLine(code);
Expand All @@ -114,18 +123,13 @@ static bool HasMethodWithContextParameter(Type type)
var typeList = type.GetInterfaces().ToList();
typeList.Add(type);

if (type == typeof(IAmStartedByMessages<>))
{

}

foreach (var typeOrInterface in typeList.Distinct())
{
foreach (var method in typeOrInterface.GetMethods())
{
foreach (var p in method.GetParameters())
{
if (typeof(IPipelineContext).IsAssignableFrom(p.ParameterType) || typeof(IBehaviorContext).IsAssignableFrom(p.ParameterType))
if (typeof(ICancellableContext).IsAssignableFrom(p.ParameterType))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ static ForwardCancellationTokenAnalyzer()
new AnalysisTarget("NServiceBus.Sagas.IHandleSagaNotFound", "Handle", "IMessageProcessingContext"),
new AnalysisTarget("NServiceBus.Pipeline.Behavior`1", "Invoke", null), // Context based on generic type argument
new AnalysisTarget("NServiceBus.Pipeline.IBehavior`2", "Invoke", null), // Context based on generic type argument
new AnalysisTarget("NServiceBus.MessageMutator.IMutateIncomingMessages", "MutateIncoming", "MutateIncomingMessageContext"),
new AnalysisTarget("NServiceBus.MessageMutator.IMutateIncomingTransportMessages", "MutateIncoming", "MutateIncomingTransportMessageContext"),
new AnalysisTarget("NServiceBus.MessageMutator.IMutateOutgoingMessages", "MutateOutgoing", "MutateOutgoingMessageContext"),
new AnalysisTarget("NServiceBus.MessageMutator.IMutateOutgoingTransportMessages", "MutateOutgoing", "MutateOutgoingTransportMessageContext"),
};

targetMethodNames = new HashSet<string>(targets.Select(target => target.MethodName).Distinct());
Expand Down

0 comments on commit 91a923c

Please sign in to comment.