Skip to content

Commit

Permalink
chore: Added first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samtrion committed May 19, 2024
1 parent 77e3c5a commit 5b6ece0
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 128 deletions.
22 changes: 0 additions & 22 deletions src/NetEvolve.Logging.XUnit/LoggedMessage.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/NetEvolve.Logging.XUnit/NetEvolve.Logging.XUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" />
<PackageReference Include="NetEvolve.Arguments" />
<PackageReference Include="NetEvolve.Logging.Abstractions" />
<PackageReference Include="xunit.extensibility.core" />
<PackageReference Include="xunit.extensibility.execution" />
</ItemGroup>

</Project>
44 changes: 18 additions & 26 deletions src/NetEvolve.Logging.XUnit/XUnitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
using System.Globalization;
using System.Text;
using Microsoft.Extensions.Logging;
using NetEvolve.Logging.Abstractions;
using Xunit.Abstractions;
using Xunit.Sdk;

public class XUnitLogger : ILogger
public class XUnitLogger : ILogger, ISupportExternalScope
{
private readonly string? _categoryName;
private readonly XUnitLoggerOptions _options;
Expand All @@ -20,9 +20,9 @@ public class XUnitLogger : ILogger
[ThreadStatic]
private static StringBuilder? _builder;

public IReadOnlyList<LoggedMessage> LoggedMessages => _loggedMessages.AsReadOnly();
public IExternalScopeProvider ScopeProvider { get; private set; }

internal IExternalScopeProvider? ScopeProvider { get; set; }
public IReadOnlyList<LoggedMessage> LoggedMessages => _loggedMessages.AsReadOnly();

public static XUnitLogger CreateLogger(
ITestOutputHelper testOutputHelper,
Expand All @@ -40,40 +40,24 @@ public static XUnitLogger<T> CreateLogger<T>(
internal XUnitLogger(
ITestOutputHelper testOutputHelper,
IExternalScopeProvider? scopeProvider,
string? categoryName,
string? name,
XUnitLoggerOptions? options
)
{
ArgumentNullException.ThrowIfNull(testOutputHelper);
ArgumentNullException.ThrowIfNull(name);

_writeToLogger = testOutputHelper.WriteLine;
ScopeProvider = scopeProvider;
_categoryName = categoryName;
_options = options ?? XUnitLoggerOptions.Default;

_loggedMessages = [];
}

internal XUnitLogger(
IMessageSink messageSink,
IExternalScopeProvider? scopeProvider,
string? categoryName,
XUnitLoggerOptions? options
)
{
ArgumentNullException.ThrowIfNull(messageSink);

_writeToLogger = message => messageSink.OnMessage(new DiagnosticMessage(message));
ScopeProvider = scopeProvider;
_categoryName = categoryName;
ScopeProvider = scopeProvider ?? NullExternalScopeProvider.Instance;
_categoryName = name;
_options = options ?? XUnitLoggerOptions.Default;

_loggedMessages = [];
}

/// <inheritdoc cref="ILogger.BeginScope{TState}(TState)"/>
public IDisposable? BeginScope<TState>(TState state)
where TState : notnull => ScopeProvider?.Push(state);
where TState : notnull => ScopeProvider.Push(state);

/// <inheritdoc cref="ILogger.IsEnabled(LogLevel)"/>
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
Expand Down Expand Up @@ -170,7 +154,7 @@ DateTimeOffset now
}
}

ScopeProvider?.ForEachScope(
ScopeProvider.ForEachScope(
(scope, state) =>
{
scopes.Add(scope);
Expand Down Expand Up @@ -230,4 +214,12 @@ private static string LogLevelToString(LogLevel logLevel) =>
LogLevel.None => "NONE",
_ => throw new ArgumentOutOfRangeException(nameof(logLevel))
};

/// <inheritdoc/>
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
{
ArgumentNullException.ThrowIfNull(scopeProvider);

ScopeProvider = scopeProvider;
}
}
21 changes: 0 additions & 21 deletions src/NetEvolve.Logging.XUnit/XUnitLoggerOfT.cs

This file was deleted.

58 changes: 17 additions & 41 deletions src/NetEvolve.Logging.XUnit/XUnitLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,54 @@

using System;
using System.Collections.Concurrent;
using System.Collections.Immutable;
using Microsoft.Extensions.Logging;
using NetEvolve.Arguments;
using NetEvolve.Logging.Abstractions;
using Xunit.Abstractions;

internal sealed class XUnitLoggerProvider : ILoggerProvider, ISupportExternalScope
{
private readonly ITestOutputHelper? _testOutputHelper;
private readonly IMessageSink? _messageSink;
private readonly IExternalScopeProvider _scopeProvider; // = NullExternalScopeProvider.Instance;
private readonly ITestOutputHelper _testOutputHelper;
private readonly IExternalScopeProvider _scopeProvider = NullExternalScopeProvider.Instance;
private readonly XUnitLoggerOptions _options;
private readonly ConcurrentDictionary<string, XUnitLogger> _loggers;

internal ImmutableList<XUnitLogger> Loggers => _loggers.Values.ToImmutableList();

public XUnitLoggerProvider(
ITestOutputHelper testOutputHelper,
IExternalScopeProvider? scopeProvider = null,
XUnitLoggerOptions? options = null
)
: this(scopeProvider, options)
{
ArgumentNullException.ThrowIfNull(testOutputHelper);

_testOutputHelper = testOutputHelper;
}

public XUnitLoggerProvider(
IMessageSink messageSink,
IExternalScopeProvider? scopeProvider = null,
XUnitLoggerOptions? options = null
)
: this(scopeProvider, options)
{
ArgumentNullException.ThrowIfNull(messageSink);

_messageSink = messageSink;
}

private XUnitLoggerProvider(
IExternalScopeProvider? scopeProvider = null,
XUnitLoggerOptions? options = null
)
{
_scopeProvider = scopeProvider ?? new LoggerExternalScopeProvider();
_options = options ?? new XUnitLoggerOptions();

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

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

return _loggers.GetOrAdd(categoryName, CreateLoggerInternal);
return _loggers.GetOrAdd(
categoryName,
name => new XUnitLogger(_testOutputHelper, _scopeProvider, name, _options)
);
}

/// <inheritdoc cref="ILoggerProvider.CreateLogger(string)"/>
public ILogger CreateLogger<T>()
where T : notnull => _loggers.GetOrAdd(typeof(T).FullName!, CreateLoggerInternal);

private XUnitLogger CreateLoggerInternal(string name)
{
if (_testOutputHelper is not null)
{
return new XUnitLogger(_testOutputHelper, _scopeProvider, name, _options);
}
else if (_messageSink is not null)
{
return new XUnitLogger(_messageSink, _scopeProvider, name, _options);
}

throw new InvalidOperationException("No output destination was provided.");
}
where T : notnull =>
_loggers.GetOrAdd(
typeof(T).FullName!,
_ => new XUnitLogger<T>(_testOutputHelper, _scopeProvider, _options)
);

/// <inheritdoc cref="ISupportExternalScope.SetScopeProvider(IExternalScopeProvider)"/>
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
Expand All @@ -87,7 +63,7 @@ public void SetScopeProvider(IExternalScopeProvider scopeProvider)

foreach (var logger in _loggers.Values)
{
logger.ScopeProvider = scopeProvider;
logger.SetScopeProvider(scopeProvider);
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/NetEvolve.Logging.XUnit/XUnitLogger`T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,4 @@ internal XUnitLogger(
XUnitLoggerOptions? options
)
: base(testOutputHelper, scopeProvider, typeof(T).FullName, options) { }

internal XUnitLogger(
IMessageSink messageSink,
IExternalScopeProvider? scopeProvider,
XUnitLoggerOptions? options
)
: base(messageSink, scopeProvider, typeof(T).FullName, options) { }
}

This file was deleted.

4 changes: 3 additions & 1 deletion tests/NetEvolve.Logging.XUnit.Tests.Integration/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NetEvolve.Logging.XUnit.Tests.Integration;
namespace NetEvolve.Logging.XUnit.Tests.Integration;

using Xunit;

public class UnitTest1
{
Expand Down
1 change: 0 additions & 1 deletion tests/NetEvolve.Logging.XUnit.Tests.Unit/GlobalUsings.cs

This file was deleted.

7 changes: 0 additions & 7 deletions tests/NetEvolve.Logging.XUnit.Tests.Unit/UnitTest1.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace NetEvolve.Logging.XUnit.Tests.Unit;

using System;
using Microsoft.Extensions.Logging;
using NetEvolve.Logging.Abstractions;
using Xunit;
using Xunit.Abstractions;

public class XUnitLoggerProviderTests
{
private readonly ITestOutputHelper _testOutputHelper;

public XUnitLoggerProviderTests(ITestOutputHelper testOutputHelper) =>
_testOutputHelper = testOutputHelper;

[Fact]
public void CreateLogger_WithTestOutputHelper()
{
// Arrange
using var provider = new XUnitLoggerProvider(_testOutputHelper);

// Act
var logger = provider.CreateLogger(nameof(XUnitLoggerProviderTests));

// Assert
Assert.NotNull(logger);
_ = Assert.IsType<XUnitLogger>(logger);
}

[Fact]
public void CreateLoggerGeneric_WithTestOutputHelper()
{
// Arrange
using var provider = new XUnitLoggerProvider(_testOutputHelper);

// Act
var logger = provider.CreateLogger<XUnitLoggerProviderTests>();

// Assert
Assert.NotNull(logger);
_ = Assert.IsType<XUnitLogger<XUnitLoggerProviderTests>>(logger);
}

[Fact]
public void SetScopeProvider_Null_ThrowArgumentNullException()
{
// Arrange
using var provider = new XUnitLoggerProvider(_testOutputHelper);

// Act
void Act() => provider.SetScopeProvider(null!);

// Assert
_ = Assert.Throws<ArgumentNullException>("scopeProvider", Act);
}

[Fact]
public void SetScopeProvider_WithNullScopeProvider_NoExceptionThrown()
{
// Arrange
using var provider = new XUnitLoggerProvider(_testOutputHelper);

// Act
var ex = Record.Exception(
() => provider.SetScopeProvider(NullExternalScopeProvider.Instance)
);

// Assert
Assert.Null(ex);
}

[Fact]
public void SetScopeProvider_WithScopeProvider_Expected()
{
// Arrange
using var provider = new XUnitLoggerProvider(_testOutputHelper);
var scopeProvider = new LoggerExternalScopeProvider();

// Act
provider.SetScopeProvider(scopeProvider);

// Assert
foreach (var logger in provider.Loggers)
{
Assert.Same(scopeProvider, logger.ScopeProvider);
}
}
}

0 comments on commit 5b6ece0

Please sign in to comment.