Skip to content

Commit

Permalink
chore: Extended tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samtrion committed Jun 18, 2024
1 parent d2665d9 commit 8ff04c6
Show file tree
Hide file tree
Showing 63 changed files with 138,620 additions and 905 deletions.
40 changes: 6 additions & 34 deletions src/NetEvolve.Logging.XUnit/XUnitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,7 @@ public static XUnitLogger CreateLogger(
IMessageSink messageSink,
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
)
{
Argument.ThrowIfNull(messageSink);

return new XUnitLogger(
message => _ = messageSink.OnMessage(new DiagnosticMessage(message)),
TimeProvider.System,
scopeProvider,
options
);
}
) => CreateLogger(messageSink, TimeProvider.System, scopeProvider, options);

/// <summary>
/// Creates a new instance of <see cref="XUnitLogger"/>.
Expand Down Expand Up @@ -96,12 +86,8 @@ public static XUnitLogger<T> CreateLogger<T>(
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
)
where T : notnull
{
Argument.ThrowIfNull(messageSink);

return new XUnitLogger<T>(messageSink, TimeProvider.System, scopeProvider, options);
}
where T : notnull =>
CreateLogger<T>(messageSink, TimeProvider.System, scopeProvider, options);

/// <summary>
/// Creates a new instance of <see cref="XUnitLogger{T}"/>.
Expand Down Expand Up @@ -137,17 +123,7 @@ public static XUnitLogger CreateLogger(
ITestOutputHelper testOutputHelper,
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
)
{
Argument.ThrowIfNull(testOutputHelper);

return new XUnitLogger(
testOutputHelper.WriteLine,
TimeProvider.System,
scopeProvider,
options
);
}
) => CreateLogger(testOutputHelper, TimeProvider.System, scopeProvider, options);

/// <summary>
/// Creates a new instance of <see cref="XUnitLogger"/>.
Expand Down Expand Up @@ -183,12 +159,8 @@ public static XUnitLogger<T> CreateLogger<T>(
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
)
where T : notnull
{
Argument.ThrowIfNull(testOutputHelper);

return new XUnitLogger<T>(testOutputHelper, TimeProvider.System, scopeProvider, options);
}
where T : notnull =>
CreateLogger<T>(testOutputHelper, TimeProvider.System, scopeProvider, options);

/// <summary>
/// Creates a new instance of <see cref="XUnitLogger{T}"/>.
Expand Down
48 changes: 0 additions & 48 deletions src/NetEvolve.Logging.XUnit/XUnitLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,54 +96,6 @@ public ILogger CreateLogger<T>()
_ => new XUnitLogger<T>(_writeToAction, _timeProvider, _scopeProvider, this)
);

/// <inheritdoc cref="ILoggerProvider.CreateLogger(string)"/>
public ILogger CreateLogger(string categoryName, IMessageSink messageSink)
{
Argument.ThrowIfNullOrWhiteSpace(categoryName);
Argument.ThrowIfNull(messageSink);

return _loggers.GetOrAdd(
$"{categoryName}_IMessageSink",
name => XUnitLogger.CreateLogger(messageSink, _timeProvider, _scopeProvider, this)
);
}

/// <inheritdoc cref="ILoggerProvider.CreateLogger(string)"/>
public ILogger CreateLogger<T>(IMessageSink messageSink)
where T : notnull
{
Argument.ThrowIfNull(messageSink);

return _loggers.GetOrAdd(
$"{typeof(T).FullName}_IMessageSink",
_ => XUnitLogger.CreateLogger<T>(messageSink, _timeProvider, _scopeProvider, this)
);
}

/// <inheritdoc cref="ILoggerProvider.CreateLogger(string)"/>
public ILogger CreateLogger(string categoryName, ITestOutputHelper testOutputHelper)
{
Argument.ThrowIfNullOrWhiteSpace(categoryName);
Argument.ThrowIfNull(testOutputHelper);

return _loggers.GetOrAdd(
$"{categoryName}_ITestOutputHelper",
name => XUnitLogger.CreateLogger(testOutputHelper, _timeProvider, _scopeProvider, this)
);
}

/// <inheritdoc cref="ILoggerProvider.CreateLogger(string)"/>
public ILogger CreateLogger<T>(ITestOutputHelper testOutputHelper)
where T : notnull
{
Argument.ThrowIfNull(testOutputHelper);

return _loggers.GetOrAdd(
$"{typeof(T).FullName}_ITestOutputHelper",
_ => XUnitLogger.CreateLogger<T>(testOutputHelper, _timeProvider, _scopeProvider, this)
);
}

/// <inheritdoc cref="ISupportExternalScope.SetScopeProvider(IExternalScopeProvider)"/>
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
internal static class ModuleInitializer
{
[ModuleInitializer]
public static void Init()
{
VerifierSettings.AutoVerify();

public static void Init() =>
Verifier.DerivePathInfo(
(sourceFile, projectDirectory, method, type) =>
{
Expand All @@ -20,5 +17,4 @@ public static void Init()
return new(snapshots, type.Name, method.Name);
}
);
}
}
37 changes: 0 additions & 37 deletions tests/NetEvolve.Logging.XUnit.Tests.Integration/TestCase.cs

This file was deleted.

49 changes: 49 additions & 0 deletions tests/NetEvolve.Logging.XUnit.Tests.Integration/TestCase1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace NetEvolve.Logging.XUnit.Tests.Integration;

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

internal sealed partial class TestCase1
{
private readonly ILogger _logger;

public TestCase1(ILogger logger) => _logger = logger;

public TestCase1(ILogger<TestCase1> logger) => _logger = logger;

public void Run()
{
using var scope1 = _logger.BeginScope("Scope");
#pragma warning disable CA1848 // Use the LoggerMessage delegates
using var scope2 = _logger.BeginScope("Execution {Now}", DateTimeOffset.Now);
using var scope3 = _logger.BeginScope(
new Dictionary<string, object> { { "ExectionTime", DateTimeOffset.Now } }
);
#pragma warning restore CA1848 // Use the LoggerMessage delegates
LogTrace();
LogDebug();
LogInformation();
LogWarning();
LogError();
LogCritical();
}

[LoggerMessage(0, LogLevel.Trace, "Trace")]
private partial void LogTrace();

[LoggerMessage(1, LogLevel.Debug, "Debug")]
private partial void LogDebug();

[LoggerMessage(2, LogLevel.Information, "Information")]
private partial void LogInformation();

[LoggerMessage(3, LogLevel.Warning, "Warning")]
private partial void LogWarning();

[LoggerMessage(4, LogLevel.Error, "Error")]
private partial void LogError();

[LoggerMessage(5, LogLevel.Critical, "Critical")]
private partial void LogCritical();
}
33 changes: 33 additions & 0 deletions tests/NetEvolve.Logging.XUnit.Tests.Integration/TestCase2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace NetEvolve.Logging.XUnit.Tests.Integration;

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

internal sealed partial class TestCase2
{
private readonly ILogger _logger;

public TestCase2(ILogger logger) => _logger = logger;

public TestCase2(ILogger<TestCase2> logger) => _logger = logger;

public void Run()
{
LogBefore(1, null);
try
{
throw new InvalidOperationException();
}
catch (Exception ex)
{
LogException(ex, "Unknown exception.");
}
}

[LoggerMessage(0, LogLevel.Information, "Before {Number}: {Name}")]
private partial void LogBefore(int number, string? name);

[LoggerMessage(1, LogLevel.Error, "Exception: {Message}")]
private partial void LogException(Exception ex, string message);
}
48 changes: 3 additions & 45 deletions tests/NetEvolve.Logging.XUnit.Tests.Integration/TestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,13 @@
using Xunit;
using Xunit.Abstractions;

#pragma warning disable CA1812
public sealed partial class TestFixture : IAsyncLifetime
{
private readonly ILoggerFactory _loggerFactory;
public IMessageSink MessageSink { get; }

private readonly ILogger _logger;
private readonly IMessageSink _messageSink;

public string TimestampFormat { get; } = /*language=datetime*/
"d";

#pragma warning disable S1144 // Unused private types or members should be removed
public TestFixture(IMessageSink messageSink)
{
_messageSink = messageSink;
_loggerFactory = LoggerFactory.Create(builder => builder.AddXUnit(_messageSink));
_logger = _loggerFactory.CreateLogger<XUnitLoggerWithFixtureTests>();
}
#pragma warning restore S1144 // Unused private types or members should be removed
public TestFixture(IMessageSink messageSink) => MessageSink = messageSink;

public Task DisposeAsync() => Task.CompletedTask;

public async Task InitializeAsync() => await RunAsync();

private ValueTask RunAsync()
{
using var scopeNull = _logger.BeginScope((string)null!);
using var scopeOne = _logger.BeginScope(
new Dictionary<string, object?> { { "MethodName", nameof(RunAsync) } }
);
#pragma warning disable CA1848 // Use the LoggerMessage delegates
try
{
_logger.LogTrace("This is a Trace.");
_logger.LogDebug("This is a Debug.");
_logger.LogInformation("This is an Information.");
_logger.LogWarning("This is a Warning.");
_logger.LogError("This is an Error.");
_logger.LogCritical("This is a Critical.");

throw new NotImplementedException();
}
catch (Exception ex)
{
_logger.LogCritical(ex, "This is a Critical with exception.");
}
#pragma warning restore CA1848 // Use the LoggerMessage delegates

return ValueTask.CompletedTask;
}
public Task InitializeAsync() => Task.CompletedTask;
}
#pragma warning restore CA1812
Loading

0 comments on commit 8ff04c6

Please sign in to comment.