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 1, 2024
1 parent f2eea1e commit 64cea3f
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 82 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="NetEvolve.Arguments" Version="1.2.12" />
<PackageVersion Include="NetEvolve.Logging.Abstractions" Version="1.1.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="Verify.Xunit" Version="24.2.0" />
<PackageVersion Include="xunit" Version="2.8.1" />
<PackageVersion Include="xunit.extensibility.core" Version="2.8.1" />
Expand Down
82 changes: 54 additions & 28 deletions src/NetEvolve.Logging.XUnit/XUnitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ public static XUnitLogger CreateLogger(
IMessageSink messageSink,
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
) => CreateLogger(messageSink, TimeProvider.System, scopeProvider, options);
)
{
Argument.ThrowIfNull(messageSink);

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

Check warning on line 57 in src/NetEvolve.Logging.XUnit/XUnitLogger.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Logging.XUnit/XUnitLogger.cs#L52-L57

Added lines #L52 - L57 were not covered by tests
}

/// <summary>
/// Creates a new instance of <see cref="XUnitLogger"/>.
Expand All @@ -65,7 +75,12 @@ public static XUnitLogger CreateLogger(
Argument.ThrowIfNull(messageSink);
Argument.ThrowIfNull(timeProvider);

return new XUnitLogger(messageSink, timeProvider, scopeProvider, options);
return new XUnitLogger(
message => _ = messageSink.OnMessage(new DiagnosticMessage(message)),
timeProvider,
scopeProvider,
options
);

Check warning on line 83 in src/NetEvolve.Logging.XUnit/XUnitLogger.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Logging.XUnit/XUnitLogger.cs#L78-L83

Added lines #L78 - L83 were not covered by tests
}

/// <summary>
Expand All @@ -81,7 +96,12 @@ public static XUnitLogger<T> CreateLogger<T>(
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
)
where T : notnull => CreateLogger<T>(messageSink, scopeProvider, options);
where T : notnull
{
Argument.ThrowIfNull(messageSink);

return new XUnitLogger<T>(messageSink, TimeProvider.System, scopeProvider, options);

Check warning on line 103 in src/NetEvolve.Logging.XUnit/XUnitLogger.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Logging.XUnit/XUnitLogger.cs#L103

Added line #L103 was not covered by tests
}

/// <summary>
/// Creates a new instance of <see cref="XUnitLogger{T}"/>.
Expand All @@ -98,7 +118,13 @@ public static XUnitLogger<T> CreateLogger<T>(
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
)
where T : notnull => new XUnitLogger<T>(messageSink, timeProvider, scopeProvider, options);
where T : notnull
{
Argument.ThrowIfNull(messageSink);
Argument.ThrowIfNull(timeProvider);

return new XUnitLogger<T>(messageSink, timeProvider, scopeProvider, options);

Check warning on line 126 in src/NetEvolve.Logging.XUnit/XUnitLogger.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Logging.XUnit/XUnitLogger.cs#L126

Added line #L126 was not covered by tests
}

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

return new XUnitLogger(
testOutputHelper.WriteLine,
TimeProvider.System,
scopeProvider,
options
);

Check warning on line 149 in src/NetEvolve.Logging.XUnit/XUnitLogger.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Logging.XUnit/XUnitLogger.cs#L144-L149

Added lines #L144 - L149 were not covered by tests
}

/// <summary>
/// Creates a new instance of <see cref="XUnitLogger"/>.
Expand All @@ -131,7 +167,7 @@ public static XUnitLogger CreateLogger(
Argument.ThrowIfNull(testOutputHelper);
Argument.ThrowIfNull(timeProvider);

return new XUnitLogger(testOutputHelper, timeProvider, scopeProvider, options);
return new XUnitLogger(testOutputHelper.WriteLine, timeProvider, scopeProvider, options);

Check warning on line 170 in src/NetEvolve.Logging.XUnit/XUnitLogger.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Logging.XUnit/XUnitLogger.cs#L170

Added line #L170 was not covered by tests
}

/// <summary>
Expand All @@ -147,8 +183,12 @@ public static XUnitLogger<T> CreateLogger<T>(
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
)
where T : notnull =>
CreateLogger<T>(testOutputHelper, TimeProvider.System, scopeProvider, options);
where T : notnull
{
Argument.ThrowIfNull(testOutputHelper);

return new XUnitLogger<T>(testOutputHelper, TimeProvider.System, scopeProvider, options);

Check warning on line 190 in src/NetEvolve.Logging.XUnit/XUnitLogger.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Logging.XUnit/XUnitLogger.cs#L190

Added line #L190 was not covered by tests
}

/// <summary>
/// Creates a new instance of <see cref="XUnitLogger{T}"/>.
Expand All @@ -165,36 +205,22 @@ public static XUnitLogger<T> CreateLogger<T>(
IExternalScopeProvider? scopeProvider = null,
IXUnitLoggerOptions? options = null
)
where T : notnull =>
new XUnitLogger<T>(testOutputHelper, timeProvider, scopeProvider, options);

private protected XUnitLogger(
ITestOutputHelper testOutputHelper,
TimeProvider timeProvider,
IExternalScopeProvider? scopeProvider,
IXUnitLoggerOptions? options
)
where T : notnull
{
Argument.ThrowIfNull(testOutputHelper);
Argument.ThrowIfNull(timeProvider);

ScopeProvider = scopeProvider ?? NullExternalScopeProvider.Instance;
_timeProvider = timeProvider;
_options = options ?? XUnitLoggerOptions.Default;

_loggedMessages = [];

_writeToLog = testOutputHelper.WriteLine;
return new XUnitLogger<T>(testOutputHelper, timeProvider, scopeProvider, options);
}

private protected XUnitLogger(
IMessageSink messageSink,
internal XUnitLogger(
Action<string> writeToAction,
TimeProvider timeProvider,
IExternalScopeProvider? scopeProvider,
IXUnitLoggerOptions? options
)
{
Argument.ThrowIfNull(messageSink);
Argument.ThrowIfNull(writeToAction);
Argument.ThrowIfNull(timeProvider);

ScopeProvider = scopeProvider ?? NullExternalScopeProvider.Instance;
Expand All @@ -203,7 +229,7 @@ private protected XUnitLogger(

_loggedMessages = [];

_writeToLog = message => _ = messageSink.OnMessage(new DiagnosticMessage(message));
_writeToLog = writeToAction;
}

/// <inheritdoc cref="ILogger.BeginScope{TState}(TState)"/>
Expand Down
58 changes: 6 additions & 52 deletions src/NetEvolve.Logging.XUnit/XUnitLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
using NetEvolve.Arguments;
using NetEvolve.Logging.Abstractions;
using Xunit.Abstractions;
using Xunit.Sdk;

[ProviderAlias("XUnit")]
internal sealed class XUnitLoggerProvider
: ILoggerProvider,
ISupportExternalScope,
IXUnitLoggerOptions
{
private readonly ITestOutputHelper? _testOutputHelper;
private readonly IMessageSink? _messageSink;
private readonly Action<string?> _writeToAction;

private readonly IExternalScopeProvider _scopeProvider = NullExternalScopeProvider.Instance;
private readonly XUnitLoggerOptions _options;
Expand Down Expand Up @@ -55,7 +55,7 @@ public XUnitLoggerProvider(
_timeProvider = timeProvider;

_loggers = new ConcurrentDictionary<string, XUnitLogger>(StringComparer.Ordinal);
_messageSink = messageSink;
_writeToAction = message => messageSink.OnMessage(new DiagnosticMessage(message));
}

public XUnitLoggerProvider(
Expand All @@ -74,7 +74,7 @@ public XUnitLoggerProvider(
_timeProvider = timeProvider;

_loggers = new ConcurrentDictionary<string, XUnitLogger>(StringComparer.Ordinal);
_testOutputHelper = testOutputHelper;
_writeToAction = testOutputHelper.WriteLine;
}

/// <inheritdoc cref="ILoggerProvider.CreateLogger(string)"/>
Expand All @@ -84,30 +84,7 @@ public ILogger CreateLogger(string categoryName)

return _loggers.GetOrAdd(
$"{categoryName}_Default",
name =>
{
if (_testOutputHelper is not null)
{
return XUnitLogger.CreateLogger(
_testOutputHelper,
_timeProvider,
_scopeProvider,
this
);
}

if (_messageSink is not null)
{
return XUnitLogger.CreateLogger(
_messageSink,
_timeProvider,
_scopeProvider,
this
);
}

throw new InvalidOperationException();
}
name => new XUnitLogger(_writeToAction, _timeProvider, _scopeProvider, this)
);
}

Expand All @@ -116,30 +93,7 @@ public ILogger CreateLogger<T>()
where T : notnull =>
_loggers.GetOrAdd(
$"{typeof(T).FullName}_Default",
_ =>
{
if (_testOutputHelper is not null)
{
return XUnitLogger.CreateLogger<T>(
_testOutputHelper,
_timeProvider,
_scopeProvider,
this
);
}

if (_messageSink is not null)
{
return XUnitLogger.CreateLogger<T>(
_messageSink,
_timeProvider,
_scopeProvider,
this
);
}

throw new InvalidOperationException();
}
_ => new XUnitLogger<T>(_writeToAction, _timeProvider, _scopeProvider, this)
);

/// <inheritdoc cref="ILoggerProvider.CreateLogger(string)"/>
Expand Down
27 changes: 25 additions & 2 deletions src/NetEvolve.Logging.XUnit/XUnitLogger`T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
using Xunit.Sdk;

/// <inheritdoc cref="XUnitLogger" />
public sealed class XUnitLogger<T> : XUnitLogger, ILogger<T>
Expand All @@ -14,13 +15,35 @@ internal XUnitLogger(
IExternalScopeProvider? scopeProvider,
IXUnitLoggerOptions? options
)
: base(messageSink, timeProvider, scopeProvider, options) { }
: base(
messageSink is not null
? message => messageSink.OnMessage(new DiagnosticMessage(message))

Check warning on line 20 in src/NetEvolve.Logging.XUnit/XUnitLogger`T.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Logging.XUnit/XUnitLogger`T.cs#L20

Added line #L20 was not covered by tests
: throw new ArgumentNullException(nameof(messageSink)),
timeProvider,
scopeProvider,
options
) { }

Check failure on line 25 in src/NetEvolve.Logging.XUnit/XUnitLogger`T.cs

View workflow job for this annotation

GitHub Actions / Build & Tests / Format / Validate Code Formatting

Fix whitespace formatting. Replace 1 characters with '\n\s\s\s\s'.

internal XUnitLogger(
ITestOutputHelper testOutputHelper,
TimeProvider timeProvider,
IExternalScopeProvider? scopeProvider,
IXUnitLoggerOptions? options
)
: base(testOutputHelper, timeProvider, scopeProvider, options) { }
: base(
testOutputHelper is not null
? testOutputHelper.WriteLine
: throw new ArgumentNullException(nameof(testOutputHelper)),
timeProvider,
scopeProvider,
options
) { }

Check failure on line 40 in src/NetEvolve.Logging.XUnit/XUnitLogger`T.cs

View workflow job for this annotation

GitHub Actions / Build & Tests / Format / Validate Code Formatting

Fix whitespace formatting. Replace 1 characters with '\n\s\s\s\s'.

internal XUnitLogger(
Action<string> writeToAction,
TimeProvider timeProvider,
IExternalScopeProvider? scopeProvider,
IXUnitLoggerOptions? options
)
: base(writeToAction, timeProvider, scopeProvider, options) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
Expand Down
41 changes: 41 additions & 0 deletions tests/NetEvolve.Logging.XUnit.Tests.Unit/XUnitLoggerOfTTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace NetEvolve.Logging.XUnit.Tests.Unit;

using System;
using Xunit;
using Xunit.Abstractions;

public partial class XUnitLoggerOfTTests
{
[Fact]
public void Constructor_WithMessageSinkNull_ThrowArgumentNullException()
{
IMessageSink messageSink = null!;

_ = Assert.Throws<ArgumentNullException>(
"messageSink",
() => _ = new XUnitLogger<XUnitLoggerOfTTests>(messageSink, null!, null!, null!)
);
}

[Fact]
public void Constructor_WithTestOutputHelperNull_ThrowArgumentNullException()
{
ITestOutputHelper testOutputHelper = null!;

_ = Assert.Throws<ArgumentNullException>(
"testOutputHelper",
() => _ = new XUnitLogger<XUnitLoggerOfTTests>(testOutputHelper, null!, null!, null!)
);
}

[Fact]
public void Constructor_WithWriteToActionNull_ThrowArgumentNullException()
{
Action<string> writeToAction = null!;

_ = Assert.Throws<ArgumentNullException>(
"writeToAction",
() => _ = new XUnitLogger<XUnitLoggerOfTTests>(writeToAction, null!, null!, null!)
);
}
}
Loading

0 comments on commit 64cea3f

Please sign in to comment.