-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Infinity.Toolkit.TestUtils and enhance testing
This commit removes the "Infinity.Toolkit.TestUtils" project from the solution and its associated configurations. New test utility classes, including `MockMeterFactory` and custom logger implementations (`XunitLogger` and `XunitLoggerFactory`), have been added to improve logging capabilities in unit tests. The `Infinity.Toolkit.Tests` project has been updated to reference these new utilities, streamlining the project structure while enhancing testing functionalities.
- Loading branch information
Showing
9 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/Infinity.Toolkit.Tests/TestUtils/MockMeterFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Diagnostics.Metrics; | ||
|
||
namespace Infinity.Toolkit.Tests.TestUtils; | ||
|
||
public sealed class MockMeterFactory : IMeterFactory | ||
{ | ||
public Meter Create(MeterOptions options) | ||
{ | ||
return new Meter(options); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Infinity.Toolkit.Tests.TestUtils; | ||
|
||
internal class XunitLogger(ITestOutputHelper testOutputHelper, LoggerExternalScopeProvider scopeProvider, string categoryName) : ILogger | ||
{ | ||
private readonly LoggerExternalScopeProvider scopeProvider = scopeProvider; | ||
private readonly string categoryName = categoryName; | ||
private readonly ITestOutputHelper testOutputHelper = testOutputHelper; | ||
|
||
public IDisposable? BeginScope<TState>(TState state) | ||
where TState : notnull | ||
=> scopeProvider.Push(state); | ||
|
||
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None; | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
if (!IsEnabled(logLevel)) | ||
{ | ||
return; | ||
} | ||
|
||
var message = formatter(state, exception); | ||
try | ||
{ | ||
testOutputHelper.WriteLine(message); | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
internal sealed class XunitLogger<T>(ITestOutputHelper testOutputHelper, LoggerExternalScopeProvider scopeProvider) : XunitLogger(testOutputHelper, scopeProvider, typeof(T).Name), ILogger<T> | ||
{ | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/Infinity.Toolkit.Tests/TestUtils/XunitLoggerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Infinity.Toolkit.Tests.TestUtils; | ||
|
||
public sealed class XunitLoggerFactory(ITestOutputHelper testOutputHelper) : ILoggerFactory | ||
{ | ||
private readonly LoggerExternalScopeProvider scopeProvider = new(); | ||
|
||
public void AddProvider(ILoggerProvider provider) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new XunitLogger(testOutputHelper, scopeProvider, categoryName); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public static ILogger<T> CreateLogger<T>(ITestOutputHelper testOutputHelper) | ||
{ | ||
var loggerFactory = new LoggerFactory(); | ||
loggerFactory.AddProvider(new XUnitLoggerProvider(testOutputHelper)); | ||
return loggerFactory.CreateLogger<T>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/Infinity.Toolkit.Tests/TestUtils/XunitLoggerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Infinity.Toolkit.Tests.TestUtils; | ||
|
||
public sealed class XUnitLoggerProvider(ITestOutputHelper testOutputHelper) : ILoggerProvider | ||
{ | ||
private readonly ITestOutputHelper testOutputHelper = testOutputHelper; | ||
private readonly LoggerExternalScopeProvider scopeProvider = new(); | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new XunitLogger(testOutputHelper, scopeProvider, categoryName); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/Infinity.Toolkit.Tests/TestUtils/XunitLoggerProviderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Infinity.Toolkit.Tests.TestUtils; | ||
|
||
public static class XunitLoggerProviderExtensions | ||
{ | ||
public static ILoggingBuilder AddXunit(this ILoggingBuilder builder, ITestOutputHelper testOutputHelper) | ||
{ | ||
builder.Services.TryAddSingleton(testOutputHelper); | ||
|
||
builder.Services.AddSingleton<ILoggerProvider>(new XUnitLoggerProvider(testOutputHelper)); | ||
return builder; | ||
} | ||
} |