From ea7dec36242042f6f701bdc4fdacc3e727755315 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Tue, 13 Oct 2020 23:17:45 +1100 Subject: [PATCH] housekeeping: fix unit tests --- .../Splat.TestRunner.Uwp.csproj | 5 +++- ...ovalTests.SplatProject.net472.approved.txt | 2 ++ ...ts.SplatProject.netcoreapp3.1.approved.txt | 2 ++ ...alTests.SplatUIProject.net472.approved.txt | 2 ++ ....SplatUIProject.netcoreapp3.1.approved.txt | 2 ++ src/Splat.Tests/BitmapLoaderTests.cs | 6 ++--- .../AllocateFreeErrorLoggerTestBase.cs | 8 +++--- .../WrappingFullLoggers/Log4NetLoggerTests.cs | 27 ++++++++++++------- 8 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/Splat.TestRunner.Uwp/Splat.TestRunner.Uwp.csproj b/src/Splat.TestRunner.Uwp/Splat.TestRunner.Uwp.csproj index 4e81fb06d..fb670ae5b 100644 --- a/src/Splat.TestRunner.Uwp/Splat.TestRunner.Uwp.csproj +++ b/src/Splat.TestRunner.Uwp/Splat.TestRunner.Uwp.csproj @@ -133,7 +133,10 @@ - + + + + diff --git a/src/Splat.Tests/API/ApiApprovalTests.SplatProject.net472.approved.txt b/src/Splat.Tests/API/ApiApprovalTests.SplatProject.net472.approved.txt index 87f87aa75..81c4edcc6 100644 --- a/src/Splat.Tests/API/ApiApprovalTests.SplatProject.net472.approved.txt +++ b/src/Splat.Tests/API/ApiApprovalTests.SplatProject.net472.approved.txt @@ -1,3 +1,5 @@ +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Android")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Uwp")] [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Tests")] [assembly: System.Runtime.Versioning.TargetFramework(".NETFramework,Version=v4.6.1", FrameworkDisplayName=".NET Framework 4.6.1")] namespace Splat diff --git a/src/Splat.Tests/API/ApiApprovalTests.SplatProject.netcoreapp3.1.approved.txt b/src/Splat.Tests/API/ApiApprovalTests.SplatProject.netcoreapp3.1.approved.txt index 21e5c5a30..20c8d3ce3 100644 --- a/src/Splat.Tests/API/ApiApprovalTests.SplatProject.netcoreapp3.1.approved.txt +++ b/src/Splat.Tests/API/ApiApprovalTests.SplatProject.netcoreapp3.1.approved.txt @@ -1,3 +1,5 @@ +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Android")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Uwp")] [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Tests")] [assembly: System.Runtime.Versioning.TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName="")] namespace Splat diff --git a/src/Splat.Tests/API/ApiApprovalTests.SplatUIProject.net472.approved.txt b/src/Splat.Tests/API/ApiApprovalTests.SplatUIProject.net472.approved.txt index 57430fa31..2ba1cea19 100644 --- a/src/Splat.Tests/API/ApiApprovalTests.SplatUIProject.net472.approved.txt +++ b/src/Splat.Tests/API/ApiApprovalTests.SplatUIProject.net472.approved.txt @@ -1,4 +1,6 @@ [assembly: System.Resources.NeutralResourcesLanguage("en-US")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Android")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Uwp")] [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Tests")] [assembly: System.Runtime.Versioning.TargetFramework(".NETFramework,Version=v4.6.1", FrameworkDisplayName=".NET Framework 4.6.1")] namespace Splat diff --git a/src/Splat.Tests/API/ApiApprovalTests.SplatUIProject.netcoreapp3.1.approved.txt b/src/Splat.Tests/API/ApiApprovalTests.SplatUIProject.netcoreapp3.1.approved.txt index a96339298..62edf5b4f 100644 --- a/src/Splat.Tests/API/ApiApprovalTests.SplatUIProject.netcoreapp3.1.approved.txt +++ b/src/Splat.Tests/API/ApiApprovalTests.SplatUIProject.netcoreapp3.1.approved.txt @@ -1,4 +1,6 @@ [assembly: System.Resources.NeutralResourcesLanguage("en-US")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Android")] +[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.TestRunner.Uwp")] [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Splat.Tests")] [assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v3.1", FrameworkDisplayName="")] namespace Splat diff --git a/src/Splat.Tests/BitmapLoaderTests.cs b/src/Splat.Tests/BitmapLoaderTests.cs index 0e50e6c12..cbc49441a 100644 --- a/src/Splat.Tests/BitmapLoaderTests.cs +++ b/src/Splat.Tests/BitmapLoaderTests.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Reflection; using System.Text; using Xunit; @@ -98,9 +99,8 @@ private static Stream GetStream(string imageName) #if ANDROID return Android.App.Application.Context.Assets.Open(imageName); #else - var cwd = Path.GetDirectoryName(typeof(BitmapLoaderTests).Assembly.Location); - var path = Path.Combine(cwd, imageName); - return File.OpenRead(path); + var assembly = Assembly.GetExecutingAssembly(); + return assembly.GetManifestResourceStream(imageName); #endif } } diff --git a/src/Splat.Tests/Logging/BaseTests/AllocateFreeErrorLoggerTestBase.cs b/src/Splat.Tests/Logging/BaseTests/AllocateFreeErrorLoggerTestBase.cs index afdffb2d4..efaadb599 100644 --- a/src/Splat.Tests/Logging/BaseTests/AllocateFreeErrorLoggerTestBase.cs +++ b/src/Splat.Tests/Logging/BaseTests/AllocateFreeErrorLoggerTestBase.cs @@ -530,7 +530,7 @@ public void WarnExceptionThreeArgumentsMethod_Should_Not_Write_If_Higher_Level() public void WarnExceptionFourArgumentsMethod_Should_Write_Message() { var (logger, target) = GetLogger(LogLevel.Debug); - logger.Info(FormatHelper.Exception, "{0}, {1}, {2}, {3}", 1, 2, 3, 4); + logger.Warn(FormatHelper.Exception, "{0}, {1}, {2}, {3}", 1, 2, 3, 4); Assert.Equal("1, 2, 3, 4 System.Exception: Exception of type 'System.Exception' was thrown.", target.Logs.Last().message.Trim(FormatHelper.NewLine).Trim()); } @@ -541,7 +541,7 @@ public void WarnExceptionFourArgumentsMethod_Should_Write_Message() public void WarnExceptionFourArgumentsMethod_Should_Not_Write_If_Higher_Level() { var (logger, target) = GetLogger(LogLevel.Fatal); - logger.Info(FormatHelper.Exception, "{0}, {1}, {2}, {3}", 1, 2, 3, 4); + logger.Warn(FormatHelper.Exception, "{0}, {1}, {2}, {3}", 1, 2, 3, 4); Assert.Empty(target.Logs); } @@ -574,7 +574,7 @@ public void WarnExceptionFiveArgumentsMethod_Should_Not_Write_If_Higher_Level() public void WarnExceptionSixArgumentsMethod_Should_Write_Message() { var (logger, target) = GetLogger(LogLevel.Debug); - logger.Info(FormatHelper.Exception, "{0}, {1}, {2}, {3}, {4}, {5}", 1, 2, 3, 4, 5, 6); + logger.Warn(FormatHelper.Exception, "{0}, {1}, {2}, {3}, {4}, {5}", 1, 2, 3, 4, 5, 6); Assert.Equal("1, 2, 3, 4, 5, 6 System.Exception: Exception of type 'System.Exception' was thrown.", target.Logs.Last().message.Trim(FormatHelper.NewLine).Trim()); } @@ -585,7 +585,7 @@ public void WarnExceptionSixArgumentsMethod_Should_Write_Message() public void WarnExceptionSixArgumentsMethod_Should_Not_Write_If_Higher_Level() { var (logger, target) = GetLogger(LogLevel.Fatal); - logger.Info(FormatHelper.Exception, "{0}, {1}, {2}, {3}, {4}, {5}", 1, 2, 3, 4, 5, 6); + logger.Warn(FormatHelper.Exception, "{0}, {1}, {2}, {3}, {4}, {5}", 1, 2, 3, 4, 5, 6); Assert.Empty(target.Logs); } diff --git a/src/Splat.Tests/Logging/WrappingFullLoggers/Log4NetLoggerTests.cs b/src/Splat.Tests/Logging/WrappingFullLoggers/Log4NetLoggerTests.cs index d849c81b6..dc9a77c40 100644 --- a/src/Splat.Tests/Logging/WrappingFullLoggers/Log4NetLoggerTests.cs +++ b/src/Splat.Tests/Logging/WrappingFullLoggers/Log4NetLoggerTests.cs @@ -8,10 +8,12 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; + using log4net; using log4net.Core; using log4net.Layout; using log4net.Repository.Hierarchy; + using Splat.Log4Net; using Splat.Tests.Mocks; @@ -43,7 +45,7 @@ public class Log4NetLoggerTests : FullLoggerTestBase /// protected override (IFullLogger logger, IMockLogTarget mockTarget) GetLogger(LogLevel minimumLogLevel) { - var logger = LogManager.GetLogger(typeof(Log4NetLoggerTests)); + var logger = LogManager.GetLogger(Guid.NewGuid().ToString()); var hierarchyLogger = (log4net.Repository.Hierarchy.Logger)logger.Logger; hierarchyLogger.Level = _splat2log4net[minimumLogLevel]; @@ -90,17 +92,24 @@ public MemoryTargetWrapper(global::log4net.Appender.MemoryAppender memoryTarget) public global::log4net.Appender.MemoryAppender MemoryTarget { get; } - public ICollection<(LogLevel logLevel, string message)> Logs => MemoryTarget.GetEvents().Select(x => + public ICollection<(LogLevel logLevel, string message)> Logs { - var currentLevel = _log4Net2Splat[x.Level]; - - if (x.ExceptionObject != null) + get { - return (currentLevel, $"{x.MessageObject} {x.ExceptionObject}"); + MemoryTarget.Flush(0); + return MemoryTarget.GetEvents().Select(x => + { + var currentLevel = _log4Net2Splat[x.Level]; + + if (x.ExceptionObject != null) + { + return (currentLevel, $"{x.MessageObject} {x.ExceptionObject}"); + } + + return (currentLevel, x.MessageObject.ToString()); + }).ToList(); } - - return (currentLevel, x.MessageObject.ToString()); - }).ToList(); + } } } }