From 4d683204d10f85e8f8ed2b41dcc480475943f027 Mon Sep 17 00:00:00 2001 From: martincostello Date: Fri, 20 Dec 2024 08:41:29 +0000 Subject: [PATCH] Refactor assertions Refactor to make CodeQL happy. --- tests/Shared/XUnitLoggerExtensionsTests.cs | 104 ++++++++++++--------- 1 file changed, 59 insertions(+), 45 deletions(-) diff --git a/tests/Shared/XUnitLoggerExtensionsTests.cs b/tests/Shared/XUnitLoggerExtensionsTests.cs index 7e61a8a..bee454a 100644 --- a/tests/Shared/XUnitLoggerExtensionsTests.cs +++ b/tests/Shared/XUnitLoggerExtensionsTests.cs @@ -18,16 +18,20 @@ public static void AddXUnit_TestOutputHelper_For_ILoggerBuilder_Validates_Parame var outputHelper = Substitute.For(); var accessor = Substitute.For(); + ILoggingBuilder nullBuilder = null!; + ITestOutputHelperAccessor nullAccessor = null!; + ITestOutputHelper nullHelper = null!; + // Act and Assert - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit()); - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit(outputHelper)); - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit(outputHelper, ConfigureAction)); - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit(accessor)); - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit(accessor, ConfigureAction)); - Assert.Throws("accessor", () => builder.AddXUnit((null as ITestOutputHelperAccessor)!)); - Assert.Throws("accessor", () => builder.AddXUnit((null as ITestOutputHelperAccessor)!, ConfigureAction)); - Assert.Throws("outputHelper", () => builder.AddXUnit((null as ITestOutputHelper)!)); - Assert.Throws("outputHelper", () => builder.AddXUnit((null as ITestOutputHelper)!, ConfigureAction)); + Assert.Throws("builder", nullBuilder.AddXUnit); + Assert.Throws("builder", () => nullBuilder.AddXUnit(outputHelper)); + Assert.Throws("builder", () => nullBuilder.AddXUnit(outputHelper, ConfigureAction)); + Assert.Throws("builder", () => nullBuilder.AddXUnit(accessor)); + Assert.Throws("builder", () => nullBuilder.AddXUnit(accessor, ConfigureAction)); + Assert.Throws("accessor", () => builder.AddXUnit(nullAccessor)); + Assert.Throws("accessor", () => builder.AddXUnit(nullAccessor, ConfigureAction)); + Assert.Throws("outputHelper", () => builder.AddXUnit(nullHelper)); + Assert.Throws("outputHelper", () => builder.AddXUnit(nullHelper, ConfigureAction)); Assert.Throws("configure", () => builder.AddXUnit(outputHelper, null!)); Assert.Throws("configure", () => builder.AddXUnit(accessor, null!)); } @@ -40,15 +44,19 @@ public static void AddXUnit_MessageSink_For_ILoggerBuilder_Validates_Parameters( var messageSink = Substitute.For(); var accessor = Substitute.For(); + ILoggingBuilder nullBuilder = null!; + IMessageSinkAccessor nullAccessor = null!; + IMessageSink nullSink = null!; + // Act and Assert - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit(messageSink)); - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit(messageSink, ConfigureAction)); - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit(accessor)); - Assert.Throws("builder", () => (null as ILoggingBuilder)!.AddXUnit(accessor, ConfigureAction)); - Assert.Throws("accessor", () => builder.AddXUnit((null as IMessageSinkAccessor)!)); - Assert.Throws("accessor", () => builder.AddXUnit((null as IMessageSinkAccessor)!, ConfigureAction)); - Assert.Throws("messageSink", () => builder.AddXUnit((null as IMessageSink)!)); - Assert.Throws("messageSink", () => builder.AddXUnit((null as IMessageSink)!, ConfigureAction)); + Assert.Throws("builder", () => nullBuilder.AddXUnit(messageSink)); + Assert.Throws("builder", () => nullBuilder.AddXUnit(messageSink, ConfigureAction)); + Assert.Throws("builder", () => nullBuilder.AddXUnit(accessor)); + Assert.Throws("builder", () => nullBuilder.AddXUnit(accessor, ConfigureAction)); + Assert.Throws("accessor", () => builder.AddXUnit(nullAccessor)); + Assert.Throws("accessor", () => builder.AddXUnit(nullAccessor, ConfigureAction)); + Assert.Throws("messageSink", () => builder.AddXUnit(nullSink)); + Assert.Throws("messageSink", () => builder.AddXUnit(nullSink, ConfigureAction)); Assert.Throws("configure", () => builder.AddXUnit(messageSink, null!)); Assert.Throws("configure", () => builder.AddXUnit(accessor, null!)); } @@ -62,19 +70,22 @@ public static void AddXUnit_TestOutputHelper_For_ILoggerFactory_Validates_Parame var outputHelper = Substitute.For(); var options = new XUnitLoggerOptions(); + ILoggerFactory nullFactory = null!; + ITestOutputHelper nullHelper = null!; + // Act and Assert - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(outputHelper)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(outputHelper, options)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(outputHelper, ConfigureAction)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(outputHelper, ConfigureFunction)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(outputHelper, Filter)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(outputHelper, logLevel)); - Assert.Throws("outputHelper", () => factory.AddXUnit((null as ITestOutputHelper)!)); - Assert.Throws("outputHelper", () => factory.AddXUnit((null as ITestOutputHelper)!, ConfigureAction)); - Assert.Throws("outputHelper", () => factory.AddXUnit((null as ITestOutputHelper)!, ConfigureFunction)); - Assert.Throws("outputHelper", () => factory.AddXUnit((null as ITestOutputHelper)!, Filter)); - Assert.Throws("outputHelper", () => factory.AddXUnit((null as ITestOutputHelper)!, logLevel)); - Assert.Throws("outputHelper", () => factory.AddXUnit((null as ITestOutputHelper)!, options)); + Assert.Throws("factory", () => nullFactory.AddXUnit(outputHelper)); + Assert.Throws("factory", () => nullFactory.AddXUnit(outputHelper, options)); + Assert.Throws("factory", () => nullFactory.AddXUnit(outputHelper, ConfigureAction)); + Assert.Throws("factory", () => nullFactory.AddXUnit(outputHelper, ConfigureFunction)); + Assert.Throws("factory", () => nullFactory.AddXUnit(outputHelper, Filter)); + Assert.Throws("factory", () => nullFactory.AddXUnit(outputHelper, logLevel)); + Assert.Throws("outputHelper", () => factory.AddXUnit(nullHelper)); + Assert.Throws("outputHelper", () => factory.AddXUnit(nullHelper, ConfigureAction)); + Assert.Throws("outputHelper", () => factory.AddXUnit(nullHelper, ConfigureFunction)); + Assert.Throws("outputHelper", () => factory.AddXUnit(nullHelper, Filter)); + Assert.Throws("outputHelper", () => factory.AddXUnit(nullHelper, logLevel)); + Assert.Throws("outputHelper", () => factory.AddXUnit(nullHelper, options)); Assert.Throws("options", () => factory.AddXUnit(outputHelper, (null as XUnitLoggerOptions)!)); Assert.Throws("configure", () => factory.AddXUnit(outputHelper, (null as Action)!)); Assert.Throws("configure", () => factory.AddXUnit(outputHelper, (null as Func)!)); @@ -90,19 +101,22 @@ public static void AddXUnit_MessageSink_For_ILoggerFactory_Validates_Parameters( var messageSink = Substitute.For(); var options = new XUnitLoggerOptions(); + ILoggerFactory nullFactory = null!; + IMessageSink nullSink = null!; + // Act and Assert - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(messageSink)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(messageSink, options)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(messageSink, ConfigureAction)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(messageSink, ConfigureFunction)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(messageSink, Filter)); - Assert.Throws("factory", () => (null as ILoggerFactory)!.AddXUnit(messageSink, logLevel)); - Assert.Throws("messageSink", () => factory.AddXUnit((null as IMessageSink)!)); - Assert.Throws("messageSink", () => factory.AddXUnit((null as IMessageSink)!, ConfigureAction)); - Assert.Throws("messageSink", () => factory.AddXUnit((null as IMessageSink)!, ConfigureFunction)); - Assert.Throws("messageSink", () => factory.AddXUnit((null as IMessageSink)!, Filter)); - Assert.Throws("messageSink", () => factory.AddXUnit((null as IMessageSink)!, logLevel)); - Assert.Throws("messageSink", () => factory.AddXUnit((null as IMessageSink)!, options)); + Assert.Throws("factory", () => nullFactory.AddXUnit(messageSink)); + Assert.Throws("factory", () => nullFactory.AddXUnit(messageSink, options)); + Assert.Throws("factory", () => nullFactory.AddXUnit(messageSink, ConfigureAction)); + Assert.Throws("factory", () => nullFactory.AddXUnit(messageSink, ConfigureFunction)); + Assert.Throws("factory", () => nullFactory.AddXUnit(messageSink, Filter)); + Assert.Throws("factory", () => nullFactory.AddXUnit(messageSink, logLevel)); + Assert.Throws("messageSink", () => factory.AddXUnit(nullSink)); + Assert.Throws("messageSink", () => factory.AddXUnit(nullSink, ConfigureAction)); + Assert.Throws("messageSink", () => factory.AddXUnit(nullSink, ConfigureFunction)); + Assert.Throws("messageSink", () => factory.AddXUnit(nullSink, Filter)); + Assert.Throws("messageSink", () => factory.AddXUnit(nullSink, logLevel)); + Assert.Throws("messageSink", () => factory.AddXUnit(nullSink, options)); Assert.Throws("options", () => factory.AddXUnit(messageSink, (null as XUnitLoggerOptions)!)); Assert.Throws("configure", () => factory.AddXUnit(messageSink, (null as Action)!)); Assert.Throws("configure", () => factory.AddXUnit(messageSink, (null as Func)!)); @@ -113,12 +127,12 @@ public static void AddXUnit_MessageSink_For_ILoggerFactory_Validates_Parameters( public static void ToLoggerFactory_Validates_Parameters() { // Arrange - ITestOutputHelper? outputHelper = null; - IMessageSink? messageSink = null; + ITestOutputHelper outputHelper = null!; + IMessageSink messageSink = null!; // Act and Assert - Assert.Throws("outputHelper", () => outputHelper!.ToLoggerFactory()); - Assert.Throws("messageSink", () => messageSink!.ToLoggerFactory()); + Assert.Throws("outputHelper", outputHelper.ToLoggerFactory); + Assert.Throws("messageSink", messageSink.ToLoggerFactory); } [Fact]