Skip to content

Commit

Permalink
style: fix nullability code smells and some other styling in test cla…
Browse files Browse the repository at this point in the history
…sses
  • Loading branch information
skwasjer committed Nov 25, 2023
1 parent cbdde6a commit 67312c9
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 62 deletions.
30 changes: 12 additions & 18 deletions test/Rebus.Correlate.Tests/CorrelateConfigurationExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ public class WithServiceProvider : CorrelateConfigurationExtensionsTests
[Fact]
public void When_configuring_instance_without_configurer_it_should_throw()
{
OptionsConfigurer configurer = null;
// ReSharper disable once ExpressionIsAlwaysNull
Action act = () => configurer.EnableCorrelate(new ServiceCollection().BuildServiceProvider());
OptionsConfigurer? configurer = null;
Action act = () => configurer!.EnableCorrelate(new ServiceCollection().BuildServiceProvider());

// Assert
act.Should()
Expand All @@ -25,12 +24,11 @@ public void When_configuring_instance_without_configurer_it_should_throw()
[Fact]
public void When_configuring_instance_without_serviceProvider_it_should_throw()
{
IServiceProvider serviceProvider = null;
IServiceProvider? serviceProvider = null;
Action act = () =>
Configure.With(new BuiltinHandlerActivator())
.Options(opts =>
// ReSharper disable once ExpressionIsAlwaysNull
opts.EnableCorrelate(serviceProvider)
opts.EnableCorrelate(serviceProvider!)
);

// Assert
Expand All @@ -45,9 +43,8 @@ public class WithBuiltIn : CorrelateConfigurationExtensionsTests
[Fact]
public void When_configuring_instance_without_configurer_it_should_throw()
{
OptionsConfigurer configurer = null;
// ReSharper disable once ExpressionIsAlwaysNull
Action act = () => configurer.EnableCorrelate(new LoggerFactory());
OptionsConfigurer? configurer = null;
Action act = () => configurer!.EnableCorrelate(new LoggerFactory());

// Assert
act.Should()
Expand All @@ -58,12 +55,11 @@ public void When_configuring_instance_without_configurer_it_should_throw()
[Fact]
public void When_configuring_instance_without_serviceProvider_it_should_throw()
{
ILoggerFactory loggerFactory = null;
ILoggerFactory? loggerFactory = null;
Action act = () =>
Configure.With(new BuiltinHandlerActivator())
.Options(opts =>
// ReSharper disable once ExpressionIsAlwaysNull
opts.EnableCorrelate(loggerFactory)
opts.EnableCorrelate(loggerFactory!)
);

// Assert
Expand All @@ -78,9 +74,8 @@ public class WithDependencyResolverAdapter : CorrelateConfigurationExtensionsTes
[Fact]
public void When_configuring_instance_without_configurer_it_should_throw()
{
OptionsConfigurer configurer = null;
// ReSharper disable once ExpressionIsAlwaysNull
Action act = () => configurer.EnableCorrelate(new DependencyResolverAdapter(_ => null));
OptionsConfigurer? configurer = null;
Action act = () => configurer!.EnableCorrelate(new DependencyResolverAdapter(_ => null));

// Assert
act.Should()
Expand All @@ -91,12 +86,11 @@ public void When_configuring_instance_without_configurer_it_should_throw()
[Fact]
public void When_configuring_instance_without_dependencyResolverAdapter_it_should_throw()
{
DependencyResolverAdapter dependencyResolverAdapter = null;
DependencyResolverAdapter? dependencyResolverAdapter = null;
Action act = () =>
Configure.With(new BuiltinHandlerActivator())
.Options(opts =>
// ReSharper disable once ExpressionIsAlwaysNull
opts.EnableCorrelate(dependencyResolverAdapter)
opts.EnableCorrelate(dependencyResolverAdapter!)
);

// Assert
Expand Down
22 changes: 11 additions & 11 deletions test/Rebus.Correlate.Tests/DependencyResolverAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class DependencyResolverAdapterTests
{
private readonly DependencyResolverAdapter _sut;
private Func<Type, object> _optionalResolve;
private Func<Type, object?> _optionalResolve = _ => null;

public DependencyResolverAdapterTests()
{
Expand All @@ -13,10 +13,10 @@ public DependencyResolverAdapterTests()
[Fact]
public void When_creating_instance_without_func_it_should_throw()
{
Func<Type, object> optionalResolve = null;
// ReSharper disable once ExpressionIsAlwaysNull
// ReSharper disable once ObjectCreationAsStatement
Action act = () => new DependencyResolverAdapter(optionalResolve);
Func<Type, object>? optionalResolve = null;

// Act
Func<DependencyResolverAdapter> act = () => new DependencyResolverAdapter(optionalResolve!);

// Assert
act.Should()
Expand All @@ -27,16 +27,16 @@ public void When_creating_instance_without_func_it_should_throw()
[Fact]
public void Given_dependency_is_not_registered_when_resolving_optional_should_return_null()
{
_optionalResolve = type => null;
_optionalResolve = _ => null;

_sut.GetOrNull<object>().Should().BeNull();
}

[Fact]
public void Given_dependency_is_registered_when_resolving_optional_should_return_null()
{
object instance = new object();
_optionalResolve = type => instance;
object instance = new();
_optionalResolve = _ => instance;

Func<object> act = () => _sut.GetOrNull<object>();

Expand All @@ -46,7 +46,7 @@ public void Given_dependency_is_registered_when_resolving_optional_should_return
[Fact]
public void Given_dependency_is_not_registered_when_resolving_should_return_throw()
{
_optionalResolve = type => null;
_optionalResolve = _ => null;

Func<object> act = () => _sut.Get<object>();

Expand All @@ -56,8 +56,8 @@ public void Given_dependency_is_not_registered_when_resolving_should_return_thro
[Fact]
public void Given_dependency_is_registered_when_resolving_should_return_instance()
{
object instance = new object();
_optionalResolve = type => instance;
object instance = new();
_optionalResolve = _ => instance;

_sut.Get<object>().Should().Be(instance);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Rebus.Correlate.Tests/Extensions/LoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static ILoggerFactory ForceEnableLogging(this ILoggerFactory loggerFactor

private class TestLoggerProvider : ILoggerProvider
{
private TestLogger _testLogger;
private TestLogger? _testLogger;

public void Dispose()
{
Expand Down
6 changes: 3 additions & 3 deletions test/Rebus.Correlate.Tests/Fixtures/RebusFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Rebus.Correlate.Fixtures;
public abstract class RebusFixture
{
private readonly List<Action<RebusConfigurer>> _configureActions = new();
private IBusStarter _busStarter;
private IBusStarter? _busStarter;

public RebusFixture()
protected RebusFixture()
{
_configureActions.Add(configurer => configurer
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "input"))
Expand All @@ -31,7 +31,7 @@ public BuiltinHandlerActivator CreateActivator()

public IBus Start()
{
return _busStarter.Start();
return _busStarter?.Start() ?? throw new InvalidOperationException("Bus starter was not created.");
}

protected void Configure(Action<RebusConfigurer> configureRebus)
Expand Down
12 changes: 7 additions & 5 deletions test/Rebus.Correlate.Tests/RebusIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class RebusIntegrationTests : IDisposable

private readonly TaskCompletionSource<string> _tcs;

public RebusIntegrationTests(RebusFixture fixture)
protected RebusIntegrationTests(RebusFixture fixture)
{
_fixture = fixture;
_activator = fixture.CreateActivator();
Expand All @@ -34,8 +34,10 @@ public RebusIntegrationTests(RebusFixture fixture)
public void Dispose()
{
_tcs.TrySetCanceled();
// ReSharper disable ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
_bus?.Dispose();
_activator?.Dispose();
// ReSharper restore ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -135,14 +137,14 @@ private void ArrangeHandler(int maxSequence, Func<TestMessage, int, Task> execut
_activator.Handle<TestMessage>(async message =>
{
IMessageContext ctx = MessageContext.Current;
ctx.Headers.TryGetValue(Headers.CorrelationId, out string cid);
ctx.Headers.TryGetValue(Headers.CorrelationSequence, out string sequenceStr);
ctx.Headers.TryGetValue(Headers.CorrelationId, out string? cid);
ctx.Headers.TryGetValue(Headers.CorrelationSequence, out string? sequenceStr);
int.TryParse(sequenceStr, out int sequence);

// Assert context.
CorrelationContext correlationContext = _correlationContextAccessor.CorrelationContext;
CorrelationContext? correlationContext = _correlationContextAccessor.CorrelationContext;
correlationContext.Should().NotBeNull();
correlationContext.CorrelationId.Should().Be(cid);
correlationContext!.CorrelationId.Should().Be(cid);

if (sequence < maxSequence)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public CorrelateIncomingMessageStepTests()
[Fact]
public void When_creating_instance_without_asyncCorrelationManager_it_should_throw()
{
IAsyncCorrelationManager asyncCorrelationManager = null;
// ReSharper disable once AssignNullToNotNullAttribute
// ReSharper disable once ObjectCreationAsStatement
Action act = () => new CorrelateIncomingMessageStep(asyncCorrelationManager, new NullLoggerFactory());
IAsyncCorrelationManager? asyncCorrelationManager = null;

// Act
Func<CorrelateIncomingMessageStep> act = () => new CorrelateIncomingMessageStep(asyncCorrelationManager!, new NullLoggerFactory());

// Assert
act.Should()
Expand All @@ -52,10 +52,10 @@ public void When_creating_instance_without_asyncCorrelationManager_it_should_thr
[Fact]
public void When_creating_instance_without_loggerFactory_it_should_not_throw()
{
IRebusLoggerFactory rebusLoggerFactory = null;
// ReSharper disable once ExpressionIsAlwaysNull
// ReSharper disable once ObjectCreationAsStatement
Action act = () => new CorrelateIncomingMessageStep(_asyncCorrelationManagerMock.Object, rebusLoggerFactory);
IRebusLoggerFactory? rebusLoggerFactory = null;

// Act
Func<CorrelateIncomingMessageStep> act = () => new CorrelateIncomingMessageStep(_asyncCorrelationManagerMock.Object, rebusLoggerFactory);

// Assert
act.Should().NotThrow();
Expand All @@ -64,7 +64,7 @@ public void When_creating_instance_without_loggerFactory_it_should_not_throw()
[Fact]
public async Task Given_no_correlation_id_is_stored_with_message_it_should_use_message_id()
{
const string expectedCorrelationId = null;
const string? expectedCorrelationId = null;
_messageHeaders.Clear();

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public CorrelateOutgoingMessageStepTests()
[Fact]
public void When_creating_instance_without_correlationContextAccessor_it_should_throw()
{
ICorrelationContextAccessor correlationContextAccessor = null;
// ReSharper disable once ExpressionIsAlwaysNull
// ReSharper disable once ObjectCreationAsStatement
Action act = () => new CorrelateOutgoingMessageStep(correlationContextAccessor, _correlationIdFactoryMock.Object, new NullLoggerFactory());
ICorrelationContextAccessor? correlationContextAccessor = null;

// Act
Func<CorrelateOutgoingMessageStep> act = () => new CorrelateOutgoingMessageStep(correlationContextAccessor!, _correlationIdFactoryMock.Object, new NullLoggerFactory());

// Assert
act.Should()
Expand All @@ -54,10 +54,10 @@ public void When_creating_instance_without_correlationContextAccessor_it_should_
[Fact]
public void When_creating_instance_without_loggerFactory_it_should_not_throw()
{
IRebusLoggerFactory rebusLoggerFactory = null;
// ReSharper disable once ExpressionIsAlwaysNull
// ReSharper disable once ObjectCreationAsStatement
Action act = () => new CorrelateOutgoingMessageStep(_correlationContextAccessor, _correlationIdFactoryMock.Object, rebusLoggerFactory);
IRebusLoggerFactory? rebusLoggerFactory = null;

// Act
Func<CorrelateOutgoingMessageStep> act = () => new CorrelateOutgoingMessageStep(_correlationContextAccessor, _correlationIdFactoryMock.Object, rebusLoggerFactory!);

// Assert
act.Should().NotThrow();
Expand All @@ -66,10 +66,10 @@ public void When_creating_instance_without_loggerFactory_it_should_not_throw()
[Fact]
public void When_creating_instance_without_correlationIdFactory_it_should_throw()
{
ICorrelationIdFactory correlationIdFactory = null;
// ReSharper disable once ExpressionIsAlwaysNull
// ReSharper disable once ObjectCreationAsStatement
Action act = () => new CorrelateOutgoingMessageStep(_correlationContextAccessor, correlationIdFactory, new NullLoggerFactory());
ICorrelationIdFactory? correlationIdFactory = null;

// Act
Func<CorrelateOutgoingMessageStep> act = () => new CorrelateOutgoingMessageStep(_correlationContextAccessor, correlationIdFactory!, new NullLoggerFactory());

// Assert
act.Should()
Expand Down Expand Up @@ -196,13 +196,13 @@ public async Task Given_incoming_step_context_has_sequence_it_should_increment(i
var incomingHeaders = new Dictionary<string, string>();
if (incomingSequenceNr.HasValue)
{
incomingHeaders.Add(Headers.CorrelationSequence, incomingSequenceNr.ToString());
incomingHeaders.Add(Headers.CorrelationSequence, incomingSequenceNr.Value.ToString());
}

var incomingStepContext = new IncomingStepContext(
new TransportMessage(
incomingHeaders,
new byte[0]),
Array.Empty<byte>()),
_transactionContextMock.Object
);
incomingStepContext.Save(new Message(incomingHeaders, new { }));
Expand Down
5 changes: 4 additions & 1 deletion test/Rebus.Correlate.Tests/TestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public TestLogger(bool isEnabled = true)
_isEnabled = isEnabled;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
}

Expand All @@ -29,6 +29,9 @@ public bool IsEnabled(LogLevel logLevel)
}

public IDisposable BeginScope<TState>(TState state)
#if NET8_0_OR_GREATER
where TState : notnull
#endif
{
return NullScope.Instance;
}
Expand Down

0 comments on commit 67312c9

Please sign in to comment.