Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeminutillo committed Sep 12, 2023
1 parent 20b341b commit 3bb143a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 379 deletions.
Original file line number Diff line number Diff line change
@@ -1,98 +1,25 @@
namespace NServiceBus.AzureFunctions.Analyzer.Tests
{
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using NUnit.Framework;

[TestFixture]
public class AzureFunctionsConfigurationAnalyzerTests : AnalyzerTestFixture<AzureFunctionsConfigurationAnalyzer>
{
[Test]
public Task DiagnosticIsReportedForPurgeOnStartup()
[TestCase("DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)", AzureFunctionsDiagnostics.DefineCriticalErrorActionNotAllowedId, LanguageVersion.CSharp7)]
[TestCase("LimitMessageProcessingConcurrencyTo(5)", AzureFunctionsDiagnostics.LimitMessageProcessingToNotAllowedId, LanguageVersion.CSharp7)]
[TestCase("MakeInstanceUniquelyAddressable(null)", AzureFunctionsDiagnostics.MakeInstanceUniquelyAddressableNotAllowedId, LanguageVersion.CSharp7)]
[TestCase("OverrideLocalAddress(null)", AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowedId, LanguageVersion.CSharp7)]
[TestCase("PurgeOnStartup(true)", AzureFunctionsDiagnostics.PurgeOnStartupNotAllowedId, LanguageVersion.CSharp7)]
[TestCase("SetDiagnosticsPath(null)", AzureFunctionsDiagnostics.SetDiagnosticsPathNotAllowedId, LanguageVersion.CSharp7)]
// HINT: In C# 7 this call is ambiguous with the LearningTransport version as the compiler cannot differentiate method calls via generic type constraints
[TestCase("UseTransport<AzureServiceBusTransport>()", AzureFunctionsDiagnostics.UseTransportNotAllowedId, LanguageVersion.CSharp8)]
[TestCase("UseTransport(new AzureServiceBusTransport(null))", AzureFunctionsDiagnostics.UseTransportNotAllowedId, LanguageVersion.CSharp7)]
public Task DiagnosticIsReportedForEndpointConfiguration(string configuration, string diagnosticId, LanguageVersion minimumLangVersion)
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.AdvancedConfiguration.PurgeOnStartup(true)|];
var advancedConfig = endpointConfig.AdvancedConfiguration;
[|advancedConfig.PurgeOnStartup(true)|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.PurgeOnStartupNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForLimitMessageProcessingConcurrencyTo()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.AdvancedConfiguration.LimitMessageProcessingConcurrencyTo(5)|];
var advancedConfig = endpointConfig.AdvancedConfiguration;
[|advancedConfig.LimitMessageProcessingConcurrencyTo(5)|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.LimitMessageProcessingToNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForDefineCriticalErrorAction()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.AdvancedConfiguration.DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)|];
var advancedConfig = endpointConfig.AdvancedConfiguration;
[|advancedConfig.DefineCriticalErrorAction((errorContext, cancellationToken) => Task.CompletedTask)|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.DefineCriticalErrorActionNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForSetDiagnosticsPath()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.AdvancedConfiguration.SetDiagnosticsPath(null)|];
var advancedConfig = endpointConfig.AdvancedConfiguration;
[|advancedConfig.SetDiagnosticsPath(null)|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.SetDiagnosticsPathNotAllowedId, source);
}
testSpecificLangVersion = minimumLangVersion;

[Test]
public Task DiagnosticIsReportedForMakeInstanceUniquelyAddressable()
{
var source =
$@"using NServiceBus;
using System;
Expand All @@ -101,57 +28,17 @@ class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.AdvancedConfiguration.MakeInstanceUniquelyAddressable(null)|];
[|endpointConfig.AdvancedConfiguration.{configuration}|];
var advancedConfig = endpointConfig.AdvancedConfiguration;
[|advancedConfig.MakeInstanceUniquelyAddressable(null)|];
[|advancedConfig.{configuration}|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.MakeInstanceUniquelyAddressableNotAllowedId, source);
return Assert(diagnosticId, source);
}

// TODO: Figue out how to test UseTransport<T> extensions
[Test]
public Task DiagnosticIsReportedForUseTransport()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.AdvancedConfiguration.UseTransport(new AzureServiceBusTransport(null))|];
var advancedConfig = endpointConfig.AdvancedConfiguration;
[|advancedConfig.UseTransport(new AzureServiceBusTransport(null))|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.UseTransportNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForOverrideLocalAddress()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.AdvancedConfiguration.OverrideLocalAddress(null)|];
var advancedConfig = endpointConfig.AdvancedConfiguration;
[|advancedConfig.OverrideLocalAddress(null)|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowedId, source);
}
LanguageVersion testSpecificLangVersion;
protected override LanguageVersion AnalyzerLanguageVersion => testSpecificLangVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,24 @@ namespace NServiceBus.AzureFunctions.Analyzer.Tests
[TestFixture]
public class AzureFunctionsSendReplyOptionsAnalyzerTests : AnalyzerTestFixture<AzureFunctionsConfigurationAnalyzer>
{
[Test]
public Task DiagnosticIsReportedForRouteReplyToThisInstance()
[TestCase("SendOptions", "RouteReplyToAnyInstance", AzureFunctionsDiagnostics.RouteReplyToAnyInstanceNotAllowedId)]
[TestCase("SendOptions", "RouteReplyToThisInstance", AzureFunctionsDiagnostics.RouteReplyToThisInstanceNotAllowedId)]
[TestCase("SendOptions", "RouteToThisInstance", AzureFunctionsDiagnostics.RouteToThisInstanceNotAllowedId)]
[TestCase("ReplyOptions", "RouteReplyToAnyInstance", AzureFunctionsDiagnostics.RouteReplyToAnyInstanceNotAllowedId)]
[TestCase("ReplyOptions", "RouteReplyToThisInstance", AzureFunctionsDiagnostics.RouteReplyToThisInstanceNotAllowedId)]
public Task DiagnosticIsReportedForOptions(string optionsType, string method, string diagnosticId)
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar()
void Bar({optionsType} options)
{{
var replyOptions = new ReplyOptions();
[|replyOptions.RouteReplyToThisInstance()|];
var sendOptions = new SendOptions();
[|sendOptions.RouteReplyToThisInstance()|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.RouteReplyToThisInstanceNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForRouteToThisInstance()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar()
{{
var options = new SendOptions();
[|options.RouteToThisInstance()|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.RouteToThisInstanceNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForRouteReplyToAnyInstance()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar()
{{
var options = new SendOptions();
[|options.RouteReplyToAnyInstance()|];
var replyOptions = new ReplyOptions();
[|replyOptions.RouteReplyToAnyInstance()|];
[|options.{method}()|];
}}
}}";

return Assert(AzureFunctionsDiagnostics.RouteReplyToAnyInstanceNotAllowedId, source);
return Assert(diagnosticId, source);
}
}
}
Loading

0 comments on commit 3bb143a

Please sign in to comment.