Skip to content

Commit

Permalink
Excluded ILogger.Write<T1, ...> methods from method body comparison t…
Browse files Browse the repository at this point in the history
…est.
  • Loading branch information
epeshk committed Oct 2, 2023
1 parent 4aeffca commit 192b494
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/Serilog.Tests/MethodOverloadConventionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,40 @@ public class MethodOverloadConventionTests
const string MessageTemplate = "messageTemplate";

#if FEATURE_DEFAULT_INTERFACE

/// <remarks>
/// <para>
/// Some methods are excluded from the method body comparison.
/// </para>
///
/// <para>
/// Logger.Write methods with generic parameters call
/// <see cref="M:Serilog.Core.Logger.Write(Serilog.Events.LogEventLevel,System.Exception,System.String,System.ReadOnlySpan{System.Object})"/>
/// method to avoid params array allocation.
/// </para>
///
/// <para>
/// A similar <see cref="ReadOnlySpan{T}"/> accepting method does not exists in the <see cref="ILogger"/> interface,
/// and introducing it is a breaking change.
/// </para>
/// </remarks>
static MethodInfo[] ExcludedMethods =
#if FEATURE_SPAN
typeof(ILogger).GetMethods()
.Where(mi => mi.Name == nameof(ILogger.Write))
.Where(mi => mi.GetGenericArguments().Length > 0)
.Where(mi => mi.GetGenericArguments().All(x => x.GetGenericParameterConstraints().Length == 0))
.ToArray();
#else
Array.Empty<MethodInfo>();
#endif

public static IEnumerable<object[]> DefaultInterfaceMethods =>
typeof(ILogger).GetMethods()
.Where(mi => mi.GetMethodBody() != null)
.Where(mi => mi.GetCustomAttribute(typeof(CustomDefaultMethodImplementationAttribute)) == null)
.Where(mi => typeof(Logger).GetInterfaceMap(typeof(ILogger)).InterfaceMethods.Contains(mi))
.Where(mi => !ExcludedMethods.Contains(mi))
.Select(mi => new object[] { mi });

[Theory]
Expand Down

0 comments on commit 192b494

Please sign in to comment.