Skip to content

Commit

Permalink
Removed redundant new lines in test output (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
roryprimrose authored Oct 8, 2020
1 parent a35e3e6 commit bd36354
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
48 changes: 48 additions & 0 deletions Divergic.Logging.Xunit.UnitTests/DefaultFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventId>();
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("")]
Expand Down
4 changes: 2 additions & 2 deletions Divergic.Logging.Xunit.UnitTests/TestOutputLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 12 additions & 17 deletions Divergic.Logging.Xunit/DefaultFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Divergic.Logging.Xunit
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Microsoft.Extensions.Logging;

/// <summary>
Expand Down Expand Up @@ -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<string>(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);
}
}
}

0 comments on commit bd36354

Please sign in to comment.