From bd3635464be7fe8df51743f8bdc368db08d37a39 Mon Sep 17 00:00:00 2001 From: Rory Primrose Date: Thu, 8 Oct 2020 22:32:26 +1100 Subject: [PATCH] Removed redundant new lines in test output (#70) --- .../DefaultFormatterTests.cs | 48 +++++++++++++++++++ .../TestOutputLoggerTests.cs | 4 +- Divergic.Logging.Xunit/DefaultFormatter.cs | 29 +++++------ 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/Divergic.Logging.Xunit.UnitTests/DefaultFormatterTests.cs b/Divergic.Logging.Xunit.UnitTests/DefaultFormatterTests.cs index 2ada068..2aaa1c2 100644 --- a/Divergic.Logging.Xunit.UnitTests/DefaultFormatterTests.cs +++ b/Divergic.Logging.Xunit.UnitTests/DefaultFormatterTests.cs @@ -17,6 +17,54 @@ public DefaultFormatterTests(ITestOutputHelper output) _output = output; } + [Theory] + [InlineData(null, false)] + [InlineData("", false)] + [InlineData(" ", false)] + [InlineData("stuff", false)] + [InlineData(null, true)] + [InlineData("", true)] + [InlineData(" ", true)] + [InlineData("stuff", true)] + public void FormatIncludesNewLineBetweenMessageAndException(string message, bool exceptionExists) + { + var config = new LoggingConfig(); + var scopeLevel = 1; + var name = Guid.NewGuid().ToString(); + var logLevel = LogLevel.Information; + var eventId = Model.Create(); + Exception? exception = exceptionExists + ? new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()) + : null; + + var sut = new DefaultFormatter(config); + + var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, exception); + + actual.Should().NotStartWith(Environment.NewLine); + actual.Should().NotEndWith(Environment.NewLine); + + if (string.IsNullOrWhiteSpace(message)) + { + if (exception != null) + { + actual.Should().Be($" Information [{eventId.Id}]: {exception}"); + } + else + { + actual.Should().BeEmpty(); + } + } + else if (exception != null) + { + actual.Should().Be($" Information [{eventId.Id}]: stuff{Environment.NewLine} Information [{eventId.Id}]: {exception}"); + } + else + { + actual.Should().Be($" Information [{eventId.Id}]: stuff"); + } + } + [Theory] [InlineData(null)] [InlineData("")] diff --git a/Divergic.Logging.Xunit.UnitTests/TestOutputLoggerTests.cs b/Divergic.Logging.Xunit.UnitTests/TestOutputLoggerTests.cs index 0847086..868f222 100644 --- a/Divergic.Logging.Xunit.UnitTests/TestOutputLoggerTests.cs +++ b/Divergic.Logging.Xunit.UnitTests/TestOutputLoggerTests.cs @@ -97,7 +97,7 @@ public void LogUsesDefaultFormatterWhenConfigIsNull() string Formatter(string logState, Exception? error) => message; var name = Guid.NewGuid().ToString(); var expected = string.Format(CultureInfo.InvariantCulture, - "{0}{1} [{2}]: {3}" + Environment.NewLine, + "{0}{1} [{2}]: {3}", string.Empty, logLevel, eventId.Id, @@ -150,7 +150,7 @@ public void LogWritesLogLevelToOutputTest(LogLevel logLevel) string Formatter(string logState, Exception? error) => message; var name = Guid.NewGuid().ToString(); var expected = string.Format(CultureInfo.InvariantCulture, - "{0}{1} [{2}]: {3}" + Environment.NewLine, + "{0}{1} [{2}]: {3}", string.Empty, logLevel, eventId.Id, diff --git a/Divergic.Logging.Xunit/DefaultFormatter.cs b/Divergic.Logging.Xunit/DefaultFormatter.cs index 0fe820b..9227f54 100644 --- a/Divergic.Logging.Xunit/DefaultFormatter.cs +++ b/Divergic.Logging.Xunit/DefaultFormatter.cs @@ -1,8 +1,8 @@ namespace Divergic.Logging.Xunit { using System; + using System.Collections.Generic; using System.Globalization; - using System.Text; using Microsoft.Extensions.Logging; /// @@ -32,36 +32,31 @@ public string Format( string message, Exception? exception) { - const string format = "{0}{2} [{3}]: {4}"; + const string Format = "{0}{1} [{2}]: {3}"; var padding = new string(' ', scopeLevel * _config.ScopePaddingSpaces); - - var builder = new StringBuilder(); + var parts = new List(2); if (string.IsNullOrWhiteSpace(message) == false) { - builder.AppendFormat(CultureInfo.InvariantCulture, - format, - padding, - name, - logLevel, - eventId.Id, - message); - builder.AppendLine(); + var part = string.Format(CultureInfo.InvariantCulture, Format, padding, logLevel, eventId.Id, message); + + parts.Add(part); } if (exception != null) { - builder.AppendFormat(CultureInfo.InvariantCulture, - format, + var part = string.Format( + CultureInfo.InvariantCulture, + Format, padding, - name, logLevel, eventId.Id, exception); - builder.AppendLine(); + + parts.Add(part); } - return builder.ToString(); + return string.Join(Environment.NewLine, parts); } } } \ No newline at end of file