mirror of
https://github.com/AlexMacocian/WpfExtended.git
synced 2026-07-15 14:59:30 +00:00
Create unit tests (#6)
* Create unit tests Rearrange test project structure Implement ICVLoggerProvider interface for mocking/extensibility * Fix UTs project path
This commit is contained in:
@@ -21,6 +21,7 @@ jobs:
|
||||
env:
|
||||
Solution_Path: WpfExtended.sln
|
||||
Actions_Allow_Unsecure_Commands: true
|
||||
Test_Project_Path: WpfExtended.Test\WpfExtended.Tests.csproj
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Windows.Extensions;
|
||||
|
||||
namespace WpfExtended.Tests.Configuration
|
||||
{
|
||||
[TestClass]
|
||||
public class DefaultOptionsManagerTests
|
||||
{
|
||||
private readonly DefaultOptionsManager optionsManager = new();
|
||||
|
||||
[TestMethod]
|
||||
public void GetOptions_ReturnsDefault()
|
||||
{
|
||||
var options = this.optionsManager.GetOptions<string>();
|
||||
|
||||
options.Should().BeNull();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UpdateOptions_Succeeds()
|
||||
{
|
||||
this.optionsManager.UpdateOptions(string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Windows.Extensions;
|
||||
|
||||
namespace WpfExtended.Tests.Configuration
|
||||
{
|
||||
[TestClass]
|
||||
public class LiveOptionsResolverTests
|
||||
{
|
||||
private readonly LiveOptionsResolver liveOptionsResolver = new();
|
||||
private readonly Mock<Slim.IServiceProvider> serviceProviderMock = new();
|
||||
private readonly Mock<IOptionsManager> optionsManagerMock = new();
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_ILiveOptions_ReturnsTrue()
|
||||
{
|
||||
var type = typeof(ILiveOptions<string>);
|
||||
|
||||
var canResolve = this.liveOptionsResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
||||
{
|
||||
var types = new Type[] { typeof(object), typeof(string), typeof(LiveOptionsResolverTests), typeof(int) };
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var canResolve = this.liveOptionsResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_ILiveOptions_ReturnsILiveOptions()
|
||||
{
|
||||
this.SetupServiceProvider();
|
||||
|
||||
var liveOptions = this.liveOptionsResolver.Resolve(this.serviceProviderMock.Object, typeof(ILiveOptions<string>));
|
||||
|
||||
liveOptions.Should().BeAssignableTo<ILiveOptions<string>>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_AnythingElse_Throws()
|
||||
{
|
||||
this.SetupServiceProvider();
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.liveOptionsResolver.Resolve(this.serviceProviderMock.Object, typeof(string));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
private void SetupServiceProvider()
|
||||
{
|
||||
this.serviceProviderMock
|
||||
.Setup(u => u.GetService<IOptionsManager>())
|
||||
.Returns(this.optionsManagerMock.Object);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Windows.Extensions;
|
||||
|
||||
namespace WpfExtended.Tests.Configuration
|
||||
{
|
||||
[TestClass]
|
||||
public class LiveUpdateableOptionsResolverTests
|
||||
{
|
||||
private readonly LiveUpdateableOptionsResolver liveUpdateableOptionsResolver = new();
|
||||
private readonly Mock<Slim.IServiceProvider> serviceProviderMock = new();
|
||||
private readonly Mock<IOptionsManager> optionsManagerMock = new();
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_ILiveUpdateableOptions_ReturnsTrue()
|
||||
{
|
||||
var type = typeof(ILiveUpdateableOptions<string>);
|
||||
|
||||
var canResolve = this.liveUpdateableOptionsResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
||||
{
|
||||
var types = new Type[] { typeof(object), typeof(string), typeof(LiveOptionsResolverTests), typeof(int) };
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var canResolve = this.liveUpdateableOptionsResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_ILiveUpdateableOptions_ReturnsILiveUpdateableOptions()
|
||||
{
|
||||
this.SetupServiceProvider();
|
||||
|
||||
var liveOptions = this.liveUpdateableOptionsResolver.Resolve(this.serviceProviderMock.Object, typeof(ILiveUpdateableOptions<string>));
|
||||
|
||||
liveOptions.Should().BeAssignableTo<ILiveUpdateableOptions<string>>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_AnythingElse_Throws()
|
||||
{
|
||||
this.SetupServiceProvider();
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.liveUpdateableOptionsResolver.Resolve(this.serviceProviderMock.Object, typeof(string));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
private void SetupServiceProvider()
|
||||
{
|
||||
this.serviceProviderMock
|
||||
.Setup(u => u.GetService<IOptionsManager>())
|
||||
.Returns(this.optionsManagerMock.Object);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System.Windows.Extensions;
|
||||
|
||||
namespace WpfExtended.Tests.Configuration
|
||||
{
|
||||
[TestClass]
|
||||
public class LiveUpdateableOptionsWrapperTests
|
||||
{
|
||||
private LiveUpdateableOptionsWrapper<string> optionsWrapper;
|
||||
private readonly Mock<IOptionsManager> optionsManagerMock = new();
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
this.optionsWrapper = new LiveUpdateableOptionsWrapper<string>(optionsManagerMock.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetValue_ReturnsValue()
|
||||
{
|
||||
this.optionsManagerMock
|
||||
.Setup(u => u.GetOptions<string>())
|
||||
.Returns("hello");
|
||||
|
||||
var value = this.optionsWrapper.Value;
|
||||
|
||||
value.Should().Be("hello");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UpdateOption_CallsOptionsManager()
|
||||
{
|
||||
this.optionsManagerMock
|
||||
.Setup(u => u.UpdateOptions<string>(It.IsAny<string>()))
|
||||
.Verifiable();
|
||||
|
||||
this.optionsWrapper.UpdateOption();
|
||||
|
||||
this.optionsManagerMock.Verify();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Windows.Extensions;
|
||||
|
||||
namespace WpfExtended.Tests.Configuration
|
||||
{
|
||||
[TestClass]
|
||||
public class OptionsResolverTests
|
||||
{
|
||||
private readonly OptionsResolver optionsResolver = new();
|
||||
private readonly Mock<Slim.IServiceProvider> serviceProviderMock = new();
|
||||
private readonly Mock<IOptionsManager> optionsManagerMock = new();
|
||||
|
||||
public Mock<IOptionsManager> OptionsManagerMock => optionsManagerMock;
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_ILiveOptions_ReturnsTrue()
|
||||
{
|
||||
var type = typeof(IOptions<string>);
|
||||
|
||||
var canResolve = this.optionsResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
||||
{
|
||||
var types = new Type[] { typeof(object), typeof(string), typeof(OptionsResolverTests), typeof(int) };
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var canResolve = this.optionsResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_IOptions_ReturnsIOptions()
|
||||
{
|
||||
this.SetupServiceProvider();
|
||||
|
||||
var liveOptions = this.optionsResolver.Resolve(this.serviceProviderMock.Object, typeof(IOptions<string>));
|
||||
|
||||
liveOptions.Should().BeAssignableTo<IOptions<string>>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_AnythingElse_Throws()
|
||||
{
|
||||
this.SetupServiceProvider();
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.optionsResolver.Resolve(this.serviceProviderMock.Object, typeof(string));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
private void SetupServiceProvider()
|
||||
{
|
||||
this.serviceProviderMock
|
||||
.Setup(u => u.GetService<IOptionsManager>())
|
||||
.Returns(this.OptionsManagerMock.Object);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Extensions;
|
||||
|
||||
namespace WpfExtended.Tests.Configuration
|
||||
{
|
||||
[TestClass]
|
||||
public class UpdateableOptionsResolverTests
|
||||
{
|
||||
private readonly UpdateableOptionsResolver updateableOptionsResolver = new();
|
||||
private readonly Mock<Slim.IServiceProvider> serviceProviderMock = new();
|
||||
private readonly Mock<IOptionsManager> optionsManagerMock = new();
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_ILiveOptions_ReturnsTrue()
|
||||
{
|
||||
var type = typeof(IUpdateableOptions<string>);
|
||||
|
||||
var canResolve = this.updateableOptionsResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
||||
{
|
||||
var types = new Type[] { typeof(object), typeof(string), typeof(UpdateableOptionsResolverTests), typeof(int) };
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var canResolve = this.updateableOptionsResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_IUpdateableOptions_ReturnsIUpdateableOptions()
|
||||
{
|
||||
this.SetupServiceProvider();
|
||||
|
||||
var liveOptions = this.updateableOptionsResolver.Resolve(this.serviceProviderMock.Object, typeof(IUpdateableOptions<string>));
|
||||
|
||||
liveOptions.Should().BeAssignableTo<IUpdateableOptions<string>>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_AnythingElse_Throws()
|
||||
{
|
||||
this.SetupServiceProvider();
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.updateableOptionsResolver.Resolve(this.serviceProviderMock.Object, typeof(string));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
private void SetupServiceProvider()
|
||||
{
|
||||
this.serviceProviderMock
|
||||
.Setup(u => u.GetService<IOptionsManager>())
|
||||
.Returns(this.optionsManagerMock.Object);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Windows.Extensions;
|
||||
|
||||
namespace WpfExtended.Tests.Configuration
|
||||
{
|
||||
[TestClass]
|
||||
public class UpdateableOptionsWrapperTests
|
||||
{
|
||||
private const string Value = "hello";
|
||||
|
||||
private UpdateableOptionsWrapper<string> optionsWrapper;
|
||||
private readonly Mock<IOptionsManager> optionsManagerMock = new();
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
this.optionsWrapper = new UpdateableOptionsWrapper<string>(optionsManagerMock.Object, Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetValue_ReturnsValue()
|
||||
{
|
||||
this.optionsManagerMock
|
||||
.Setup(u => u.GetOptions<string>())
|
||||
.Throws<Exception>();
|
||||
|
||||
var value = this.optionsWrapper.Value;
|
||||
|
||||
value.Should().Be(Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UpdateOption_CallsOptionsManager()
|
||||
{
|
||||
this.optionsManagerMock
|
||||
.Setup(u => u.UpdateOptions<string>(It.IsAny<string>()))
|
||||
.Verifiable();
|
||||
|
||||
this.optionsWrapper.UpdateOption();
|
||||
|
||||
this.optionsManagerMock.Verify();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Debug;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Http;
|
||||
using System.Net.Http;
|
||||
using System.Windows.Extensions.Http;
|
||||
using WpfExtended.Logging;
|
||||
|
||||
namespace WpfExtended.Tests.Http
|
||||
{
|
||||
[TestClass]
|
||||
public class HttpClientResolverTests
|
||||
{
|
||||
private readonly HttpClientResolver httpClientResolver = new();
|
||||
private readonly Mock<Slim.IServiceProvider> serviceProviderMock = new();
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_IHttpClient_ReturnsTrue()
|
||||
{
|
||||
var type = typeof(IHttpClient<>);
|
||||
|
||||
var canResolve = this.httpClientResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
||||
{
|
||||
var types = new Type[] { typeof(HttpClient), typeof(object), typeof(string), typeof(HttpClientResolverTests), typeof(int) };
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var canResolve = this.httpClientResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_TypedClient_ReturnsIHttpClient()
|
||||
{
|
||||
this.ServiceProviderMockReturnLogger();
|
||||
|
||||
var client = this.httpClientResolver.Resolve(this.serviceProviderMock.Object, typeof(IHttpClient<string>));
|
||||
|
||||
client.Should().BeAssignableTo<IHttpClient<string>>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_NonGenericType_Throws()
|
||||
{
|
||||
this.ServiceProviderMockReturnLogger();
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.httpClientResolver.Resolve(this.serviceProviderMock.Object, typeof(IHttpClient<>));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_RandomType_Throws()
|
||||
{
|
||||
this.ServiceProviderMockReturnLogger();
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.httpClientResolver.Resolve(this.serviceProviderMock.Object, typeof(string));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
private void ServiceProviderMockReturnLogger()
|
||||
{
|
||||
this.serviceProviderMock
|
||||
.Setup(u => u.GetService(It.IsAny<Type>()))
|
||||
.Returns(new CVLogger(string.Empty, new Mock<ICVLoggerProvider>().Object));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Extensions;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WpfExtended.Tests.Http
|
||||
{
|
||||
public class HttpMessageLogger : DelegatingHandler
|
||||
{
|
||||
private readonly ILogger logger;
|
||||
public HttpMessageLogger(ILogger logger, HttpMessageHandler innerHandler) : base(innerHandler)
|
||||
{
|
||||
this.logger = logger.ThrowIfNull(nameof(logger));
|
||||
}
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
this.logger.LogInformation($"{request.Method} - {request.RequestUri}");
|
||||
var response = await base.SendAsync(request, cancellationToken);
|
||||
this.logger.LogInformation($"{response.RequestMessage.RequestUri} - {response.StatusCode}");
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using WpfExtended.Logging;
|
||||
using WpfExtended.Models;
|
||||
|
||||
namespace WpfExtended.Tests.Logging
|
||||
{
|
||||
[TestClass]
|
||||
public class CVLoggerProviderTests
|
||||
{
|
||||
private readonly Mock<ILogsWriter> logsWriterMock = new();
|
||||
private CVLoggerProvider cVLoggerProvider;
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
this.cVLoggerProvider = new CVLoggerProvider(this.logsWriterMock.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CreateLogger_CreatesNewLogger()
|
||||
{
|
||||
var logger = this.cVLoggerProvider.CreateLogger(string.Empty);
|
||||
|
||||
logger.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void LogEntry_CallsLogWriter()
|
||||
{
|
||||
this.cVLoggerProvider.LogEntry(new Log());
|
||||
|
||||
this.logsWriterMock.Verify();
|
||||
}
|
||||
|
||||
private void SetupLogsWriter()
|
||||
{
|
||||
this.logsWriterMock
|
||||
.Setup(u => u.WriteLog(It.IsAny<Log>()))
|
||||
.Verifiable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System;
|
||||
using WpfExtended.Logging;
|
||||
using WpfExtended.Models;
|
||||
|
||||
namespace WpfExtended.Tests.Logging
|
||||
{
|
||||
[TestClass]
|
||||
public class CVLoggerTests
|
||||
{
|
||||
private readonly Mock<ICVLoggerProvider> cvLoggerProviderMock = new();
|
||||
private CVLogger cVLogger;
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
this.cVLogger = new CVLogger(string.Empty, this.cvLoggerProviderMock.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void BeginScope_ReturnsNull()
|
||||
{
|
||||
var scope = this.cVLogger.BeginScope(string.Empty);
|
||||
|
||||
scope.Should().BeNull();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[DataRow(LogLevel.Debug)]
|
||||
[DataRow(LogLevel.Trace)]
|
||||
[DataRow(LogLevel.Information)]
|
||||
[DataRow(LogLevel.Warning)]
|
||||
[DataRow(LogLevel.Error)]
|
||||
[DataRow(LogLevel.Critical)]
|
||||
public void IsEnabled_OnAllLogLevels_ReturnsTrue(LogLevel logLevel)
|
||||
{
|
||||
var enabled = this.cVLogger.IsEnabled(logLevel);
|
||||
|
||||
enabled.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Log_CallsFormatter()
|
||||
{
|
||||
var called = false;
|
||||
Func<string, Exception, string> messageFormatter = new((state, exception) =>
|
||||
{
|
||||
called = true;
|
||||
return string.Empty;
|
||||
});
|
||||
|
||||
this.cVLogger.Log(LogLevel.Debug, new EventId(), "Some message", new Exception(), messageFormatter);
|
||||
|
||||
called.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Log_CallsLogsProvider()
|
||||
{
|
||||
this.SetupLoggerProvider();
|
||||
|
||||
this.cVLogger.Log(LogLevel.Debug, new EventId(), "Some message", new Exception(), new Func<string, Exception, string>((s, e) => string.Empty));
|
||||
|
||||
this.cvLoggerProviderMock.Verify();
|
||||
}
|
||||
|
||||
private void SetupLoggerProvider()
|
||||
{
|
||||
this.cvLoggerProviderMock
|
||||
.Setup(u => u.LogEntry(It.IsAny<Log>()))
|
||||
.Verifiable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using WpfExtended.Logging;
|
||||
using WpfExtended.Models;
|
||||
|
||||
namespace WpfExtended.Tests.Logging
|
||||
{
|
||||
[TestClass]
|
||||
public class DebugLogsWriterTests
|
||||
{
|
||||
private readonly DebugLogsWriter logsWriter = new();
|
||||
|
||||
[TestMethod]
|
||||
public void WriteLog_Succeeds()
|
||||
{
|
||||
this.logsWriter.WriteLog(new Log());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WriteNullLog_Throws()
|
||||
{
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.logsWriter.WriteLog(null);
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Windows.Extensions.Logging;
|
||||
using WpfExtended.Logging;
|
||||
|
||||
namespace WpfExtended.Tests.Logging
|
||||
{
|
||||
[TestClass]
|
||||
public class LoggerResolverTests
|
||||
{
|
||||
private readonly Mock<Slim.IServiceProvider> serviceProviderMock = new();
|
||||
private readonly Mock<ILoggerFactory> loggerFactoryMock = new();
|
||||
private readonly Mock<ILogger> loggerMock = new();
|
||||
private readonly LoggerResolver loggerResolver = new();
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_ILogger_ReturnsTrue()
|
||||
{
|
||||
var type = typeof(ILogger);
|
||||
|
||||
var canResolve = this.loggerResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_GenericILogger_ReturnsTrue()
|
||||
{
|
||||
var type = typeof(ILogger<string>);
|
||||
|
||||
var canResolve = this.loggerResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
||||
{
|
||||
var types = new Type[] { typeof(CVLogger), typeof(object), typeof(string), typeof(LoggerResolverTests), typeof(int) };
|
||||
|
||||
foreach(var type in types)
|
||||
{
|
||||
var canResolve = this.loggerResolver.CanResolve(type);
|
||||
|
||||
canResolve.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_ILogger_ReturnsILogger()
|
||||
{
|
||||
this.SetupIServiceProvider();
|
||||
|
||||
var logger = this.loggerResolver.Resolve(this.serviceProviderMock.Object, typeof(ILogger));
|
||||
|
||||
logger.Should().BeAssignableTo<ILogger>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_TypedILogger_ReturnsTypedILogger()
|
||||
{
|
||||
this.SetupIServiceProvider();
|
||||
|
||||
var logger = this.loggerResolver.Resolve(this.serviceProviderMock.Object, typeof(ILogger<string>));
|
||||
|
||||
logger.Should().BeAssignableTo<ILogger<string>>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_RandomType_Throws()
|
||||
{
|
||||
this.SetupIServiceProvider();
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.loggerResolver.Resolve(this.serviceProviderMock.Object, typeof(string));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Resolve_NonTypedGeneric_Throws()
|
||||
{
|
||||
this.SetupIServiceProvider();
|
||||
|
||||
Action action = new(() =>
|
||||
{
|
||||
this.loggerResolver.Resolve(this.serviceProviderMock.Object, typeof(ILogger<>));
|
||||
});
|
||||
|
||||
action.Should().Throw<Exception>();
|
||||
}
|
||||
|
||||
private void SetupIServiceProvider()
|
||||
{
|
||||
this.serviceProviderMock
|
||||
.Setup(u => u.GetService<ILoggerFactory>())
|
||||
.Returns(this.loggerFactoryMock.Object);
|
||||
|
||||
this.loggerFactoryMock
|
||||
.Setup(u => u.CreateLogger(It.IsAny<string>()))
|
||||
.Returns(this.loggerMock.Object);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Http.Logging;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Slim;
|
||||
using System;
|
||||
using System.Extensions;
|
||||
@@ -15,22 +16,17 @@ namespace WpfExtended.Tests
|
||||
{
|
||||
private static Launcher Instance { get; } = new Launcher();
|
||||
|
||||
[STAThread]
|
||||
public static int Main()
|
||||
{
|
||||
return Instance.Run();
|
||||
}
|
||||
|
||||
protected override void SetupServiceManager(IServiceManager serviceManager)
|
||||
{
|
||||
serviceManager.RegisterDebugLoggerFactory();
|
||||
serviceManager.RegisterResolver(
|
||||
new HttpClientResolver()
|
||||
.WithHttpMessageHandlerFactory((sp, category) =>
|
||||
.WithHttpMessageHandlerFactory((serviceProvider, categoryType) =>
|
||||
{
|
||||
var loggerType = typeof(ILogger<>).MakeGenericType(category);
|
||||
var logger = sp.GetService(loggerType).As<ILogger>();
|
||||
return new HttpMessageLogger(logger, new HttpClientHandler());
|
||||
var loggerType = typeof(ILogger<>).MakeGenericType(categoryType);
|
||||
var logger = serviceProvider.GetService(loggerType).As<ILogger>();
|
||||
var handler = new LoggingHttpMessageHandler(logger) { InnerHandler = new HttpClientHandler() };
|
||||
return handler;
|
||||
}));
|
||||
serviceManager.RegisterOptionsManager();
|
||||
}
|
||||
@@ -1,12 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FlaUI.UIA3" Version="3.2.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.0.0-beta0001" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
|
||||
<PackageReference Include="Microsoft.TestPlatform" Version="16.10.0" />
|
||||
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="16.10.0" />
|
||||
<PackageReference Include="Moq" Version="4.16.1" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.4" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.4" />
|
||||
<PackageReference Include="SystemExtensions.NetStandard" Version="1.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -8,12 +8,12 @@ namespace WpfExtended.Logging
|
||||
public sealed class CVLogger : ILogger
|
||||
{
|
||||
private readonly string category;
|
||||
private readonly CVLoggerProvider cvLoggerProvider;
|
||||
private readonly ICVLoggerProvider cvLoggerProvider;
|
||||
|
||||
public CVLogger(string category, CVLoggerProvider debugLoggerProvider)
|
||||
public CVLogger(string category, ICVLoggerProvider cvLoggerProvider)
|
||||
{
|
||||
this.category = category;
|
||||
this.cvLoggerProvider = debugLoggerProvider.ThrowIfNull(nameof(debugLoggerProvider));
|
||||
this.cvLoggerProvider = cvLoggerProvider.ThrowIfNull(nameof(cvLoggerProvider));
|
||||
}
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
|
||||
@@ -5,7 +5,7 @@ using WpfExtended.Models;
|
||||
|
||||
namespace WpfExtended.Logging
|
||||
{
|
||||
public sealed class CVLoggerProvider : ILoggerProvider
|
||||
public sealed class CVLoggerProvider : ICVLoggerProvider
|
||||
{
|
||||
private readonly ILogsWriter logsManager;
|
||||
private CorrelationVector correlationVector;
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WpfExtended.Models;
|
||||
|
||||
namespace WpfExtended.Logging
|
||||
{
|
||||
public interface ICVLoggerProvider : ILoggerProvider
|
||||
{
|
||||
void LogEntry(Log log);
|
||||
}
|
||||
}
|
||||
@@ -23,10 +23,14 @@ namespace System.Windows.Extensions.Logging
|
||||
{
|
||||
return ResolveScopedLogger(serviceProvider, type);
|
||||
}
|
||||
else
|
||||
else if (type == typeof(ILogger))
|
||||
{
|
||||
return ResolveLogger(serviceProvider, type);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"{nameof(LoggerResolver)} cannot resolve an object of type {type.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
private static object ResolveScopedLogger(Slim.IServiceProvider serviceProvider, Type type)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<PackageLicenseFile>License.txt</PackageLicenseFile>
|
||||
<Version>0.5</Version>
|
||||
<Version>0.5.1</Version>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Description>Extension library for Windows Presentation Platform.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user