Skip to content

Commit

Permalink
Remove Infinity.Toolkit.TestUtils and enhance testing
Browse files Browse the repository at this point in the history
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
penyland committed Jan 29, 2025
1 parent 5bab019 commit 529b850
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 9 deletions.
7 changes: 0 additions & 7 deletions Infinity.Toolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infinity.Toolkit.Experiment
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infinity.Toolkit.Azure", "src\Infinity.Toolkit.Azure\Infinity.Toolkit.Azure.csproj", "{71D6D75F-97AF-41CA-A986-7BBAC0F791B5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infinity.Toolkit.TestUtils", "src\Infinity.Toolkit.TestUtils\Infinity.Toolkit.TestUtils.csproj", "{DE7E0862-0ED5-2819-6DBB-39738980A912}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infinity.Toolkit.Tests", "tests\Infinity.Toolkit.Tests\Infinity.Toolkit.Tests.csproj", "{FE5D6722-33D0-4E3F-B908-B5CE85986938}"
Expand Down Expand Up @@ -98,10 +96,6 @@ Global
{71D6D75F-97AF-41CA-A986-7BBAC0F791B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71D6D75F-97AF-41CA-A986-7BBAC0F791B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71D6D75F-97AF-41CA-A986-7BBAC0F791B5}.Release|Any CPU.Build.0 = Release|Any CPU
{DE7E0862-0ED5-2819-6DBB-39738980A912}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE7E0862-0ED5-2819-6DBB-39738980A912}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE7E0862-0ED5-2819-6DBB-39738980A912}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE7E0862-0ED5-2819-6DBB-39738980A912}.Release|Any CPU.Build.0 = Release|Any CPU
{FE5D6722-33D0-4E3F-B908-B5CE85986938}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE5D6722-33D0-4E3F-B908-B5CE85986938}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE5D6722-33D0-4E3F-B908-B5CE85986938}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -150,7 +144,6 @@ Global
{EA3ADDB3-E77F-4AB7-B4FE-89A04D519F4A} = {E93DA4F4-345E-4567-BFB8-52FADCB86917}
{23A73C55-36A2-44F7-B75B-520AFF5C5C46} = {FE5B1A6C-B3A0-4074-81FD-F799A031102F}
{71D6D75F-97AF-41CA-A986-7BBAC0F791B5} = {FE5B1A6C-B3A0-4074-81FD-F799A031102F}
{DE7E0862-0ED5-2819-6DBB-39738980A912} = {FE5B1A6C-B3A0-4074-81FD-F799A031102F}
{FE5D6722-33D0-4E3F-B908-B5CE85986938} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{E6F5AB32-7512-48DD-9266-B86DFB26EB38} = {E93DA4F4-345E-4567-BFB8-52FADCB86917}
{DF3C4519-9F07-F61F-7F89-A7FA2EE6A345} = {FE5B1A6C-B3A0-4074-81FD-F799A031102F}
Expand Down
1 change: 0 additions & 1 deletion tests/Infinity.Toolkit.Tests/Infinity.Toolkit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Infinity.Toolkit.Messaging\Infinity.Toolkit.Messaging.csproj" />
<ProjectReference Include="..\..\src\Infinity.Toolkit.TestUtils\Infinity.Toolkit.TestUtils.csproj" />
<ProjectReference Include="..\..\src\Infinity.Toolkit\Infinity.Toolkit.csproj" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions tests/Infinity.Toolkit.Tests/Messaging/MessageBusTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Infinity.Toolkit.Tests.TestUtils;
using Microsoft.Extensions.Logging;

namespace Infinity.Toolkit.Tests.Messaging;
Expand Down
3 changes: 2 additions & 1 deletion tests/Infinity.Toolkit.Tests/Messaging/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using Infinity.Toolkit.Tests.TestUtils;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace Infinity.Toolkit.Tests.Messaging;
Expand Down
15 changes: 15 additions & 0 deletions tests/Infinity.Toolkit.Tests/TestUtils/MockMeterFactory.cs
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()
{
}
}
37 changes: 37 additions & 0 deletions tests/Infinity.Toolkit.Tests/TestUtils/XunitLogger.cs
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 tests/Infinity.Toolkit.Tests/TestUtils/XunitLoggerFactory.cs
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 tests/Infinity.Toolkit.Tests/TestUtils/XunitLoggerProvider.cs
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()
{
}
}
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;
}
}

0 comments on commit 529b850

Please sign in to comment.