From 817a9baa0999ace7b6bde4aaba0dba68b6aff4b4 Mon Sep 17 00:00:00 2001 From: Tyson Williams Date: Mon, 2 Nov 2020 10:05:44 -0600 Subject: [PATCH] added test for case when socket error code is unknown --- .../Destructurers/LogJsonOutputUtils.cs | 27 +++++++++++++++++++ .../SocketExceptionDestructurerTest.cs | 7 +++++ 2 files changed, 34 insertions(+) diff --git a/Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs b/Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs index 82f73bf6..174f5f10 100644 --- a/Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs +++ b/Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs @@ -42,6 +42,12 @@ public static void Test_LoggedExceptionContainsProperty(Exception exception, str Assert_JObjectContainsPropertiesExceptionDetailsWithProperty(rootObject, propertyKey, propertyValue); } + public static void Test_LoggedExceptionDoesNotContainProperty(Exception exception, string propertyKey) + { + var rootObject = LogAndDestructureException(exception); + Assert_JObjectContainsPropertiesExceptionDetailsWithoutProperty(rootObject, propertyKey); + } + public static JArray ExtractInnerExceptionsProperty(JObject jObject) { var exceptionDetailValue = ExtractExceptionDetails(jObject); @@ -89,6 +95,19 @@ public static void Assert_ContainsPropertyWithValue( } } + public static void Assert_DoesNotContainProperty( + JObject jObject, + string propertyKey) + { + if (jObject is null) + { + throw new ArgumentNullException(nameof(jObject)); + } + + jObject.Properties().Should() + .NotContain(x => x.Name == propertyKey, $"property with name {propertyKey} was not expected"); + } + public static JProperty ExtractProperty(JObject jObject, string propertyKey) { if (jObject is null) @@ -110,6 +129,14 @@ public static void Assert_JObjectContainsPropertiesExceptionDetailsWithProperty( Assert_ContainsPropertyWithValue(exceptionDetailValue, propertyKey, propertyValue); } + public static void Assert_JObjectContainsPropertiesExceptionDetailsWithoutProperty( + JObject jObject, + string propertyKey) + { + var exceptionDetailValue = ExtractExceptionDetails(jObject); + Assert_DoesNotContainProperty(exceptionDetailValue, propertyKey); + } + internal class TestTextWriterSink : ILogEventSink { private readonly TextWriter textWriter; diff --git a/Tests/Serilog.Exceptions.Test/Destructurers/SocketExceptionDestructurerTest.cs b/Tests/Serilog.Exceptions.Test/Destructurers/SocketExceptionDestructurerTest.cs index 9916e9d2..37d9ad4c 100644 --- a/Tests/Serilog.Exceptions.Test/Destructurers/SocketExceptionDestructurerTest.cs +++ b/Tests/Serilog.Exceptions.Test/Destructurers/SocketExceptionDestructurerTest.cs @@ -19,5 +19,12 @@ public void SocketException_SocketErrorCodeDocumentationIsAttachedAsProperty() var socketException = new SocketException((int)SocketError.ConnectionRefused); Test_LoggedExceptionContainsProperty(socketException, nameof(SocketException.SocketErrorCode) + "Message", "The remote host is actively refusing a connection."); } + + [Fact] + public void SocketException_SocketErrorCodeDocumentationWhenSocketErrorCodeUnknown() + { + var socketException = new SocketException(42); + Test_LoggedExceptionDoesNotContainProperty(socketException, nameof(SocketException.SocketErrorCode) + "Message"); + } } }