Skip to content

Commit

Permalink
Fixup for format bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaben committed Sep 20, 2024
1 parent fe9e2f5 commit 7b10cbf
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 396 deletions.
23 changes: 9 additions & 14 deletions Common.Logging.Serilog.Tests/Common.Logging.Serilog.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Castle.Core" Version="4.1.1.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="nunit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Castle.Core" Version="5.1.1" />
<PackageReference Include="Common.Logging" Version="3.4.1.0" />
<PackageReference Include="Common.Logging.Core" Version="3.4.1.0" />
<PackageReference Include="Serilog" Version="2.0.0.0" />
<PackageReference Include="Moq" Version="4.7.99.0" />

<PackageReference Include="Moq" Version="4.20.72" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Common.Logging.Serilog.csproj" />
<ProjectReference Include="..\src\Common.Logging.Serilog.csproj" />
</ItemGroup>

</Project>
</Project>
184 changes: 94 additions & 90 deletions Common.Logging.Serilog.Tests/SerilogCommonLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,96 +6,100 @@
using Serilog.Core;
using Serilog.Events;

namespace Common.Logging.Serilog.Tests
namespace Common.Logging.Serilog.Tests;

[TestFixture]
public class SerilogCommonLoggerTests
{
[TestFixture]
public class SerilogCommonLoggerTests
[SetUp]
public void Setup()
{
_seriLogger = new Mock<ILogger>();

_seriLogger.Setup(s => s.ForContext(It.IsAny<ILogEventEnricher>()))
.Returns(_seriLogger.Object);
_seriLogger.Setup(l => l.IsEnabled(It.IsAny<LogEventLevel>())).Returns(true);

_commonLogger = new SerilogCommonLogger(_seriLogger.Object);
}

private Mock<ILogger> _seriLogger;
private SerilogCommonLogger _commonLogger;

[Test]
public void Should_Leave_String_As_Is_If_Formatted_With_Serilog_Syntax()
{
private Mock<ILogger> _seriLogger;
private SerilogCommonLogger _commonLogger;

[SetUp]
public void Setup()
{
_seriLogger = new Mock<ILogger>();

this._seriLogger.Setup(s => s.ForContext(It.IsAny<ILogEventEnricher>())).Returns(this._seriLogger.Object);
this._seriLogger.Setup(l => l.IsEnabled(It.IsAny<LogEventLevel>())).Returns(true);

_commonLogger = new SerilogCommonLogger(_seriLogger.Object);
}

[Test]
public void Should_Leave_String_As_Is_If_Formatted_With_Serilog_Syntax()
{
/* Setup */
const string templateString = "This is a {@serilog} formatted string";
const string expectedString = templateString;

var arg = new { Type = "Serilog"};
var expectedArgs = new object[] {arg};
_seriLogger
.Setup(l => l.Write(
It.IsAny<LogEventLevel>(),
It.IsAny<Exception>(),
expectedString,
expectedArgs
))
.Verifiable();

/* Test */
_commonLogger.DebugFormat(templateString, arg);

/* Assert */
_seriLogger.VerifyAll();
}

[Test]
public void Should_Preformat_String_If_Numerical_Formatted()
{
/* Setup */
const string templateString = "This is a {0} formatted string";
var arg = "nummeric";
var expectedString = string.Format(templateString, arg);

_seriLogger
.Setup(l => l.Write(
It.IsAny<LogEventLevel>(),
It.IsAny<Exception>(),
expectedString,
It.Is<object[]>(s => !s.Any())
))
.Verifiable();

/* Test */
_commonLogger.DebugFormat(templateString, arg);

/* Assert */
_seriLogger.VerifyAll();
}

[Test]
public void Should_Preformat_Numeric_Formatting_But_Leave_Serilog_Formating()
{
/* Setup */
const string templateString = "This is a {0} formatted string with {@serilog} args, too";
var args = new object[] { "nummeric", new { type = "Serilog"} };
const string expectedString = "This is a nummeric formatted string with {@serilog} args, too";

_seriLogger
.Setup(l => l.Write(
It.IsAny<LogEventLevel>(),
It.IsAny<Exception>(),
expectedString,
It.Is<object[]>(s => s.Count() == 1 && s[0] == args[1])
))
.Verifiable();

/* Test */
_commonLogger.DebugFormat(templateString, args);

/* Assert */
_seriLogger.VerifyAll();
}
/* Setup */
const string templateString = "This is a {@serilog} formatted string";
const string expectedString = templateString;

var arg = new { Type = "Serilog" };
var expectedArgs = new object[] { arg };
_seriLogger
.Setup(l => l.Write(
It.IsAny<LogEventLevel>(),
It.IsAny<Exception>(),
expectedString,
expectedArgs
))
.Verifiable();

/* Test */
_commonLogger.DebugFormat(templateString, arg);

/* Should be same result */
_commonLogger.Debug(m => m(templateString, arg));

/* Assert */
_seriLogger.VerifyAll();
}

[Test]
public void Should_Preformat_String_If_Numerical_Formatted()
{
/* Setup */
const string templateString = "This is a {0} formatted string";
var arg = "nummeric";
var expectedString = string.Format(templateString, arg);

_seriLogger
.Setup(l => l.Write(
It.IsAny<LogEventLevel>(),
It.IsAny<Exception>(),
expectedString,
It.Is<object[]>(s => !s.Any())
))
.Verifiable();

/* Test */
_commonLogger.DebugFormat(templateString, arg);

/* Assert */
_seriLogger.VerifyAll();
}

[Test]
public void Should_Preformat_Numeric_Formatting_But_Leave_Serilog_Formating()
{
/* Setup */
const string templateString = "This is a {0} formatted string with {@serilog} args, too";
var args = new object[] { "nummeric", new { type = "Serilog" } };
const string expectedString =
"This is a nummeric formatted string with {@serilog} args, too";

_seriLogger
.Setup(l => l.Write(
It.IsAny<LogEventLevel>(),
It.IsAny<Exception>(),
expectedString,
It.Is<object[]>(s => s.Count() == 1 && s[0] == args[1])
))
.Verifiable();

/* Test */
_commonLogger.DebugFormat(templateString, args);

/* Assert */
_seriLogger.VerifyAll();
}
}
}
Loading

0 comments on commit 7b10cbf

Please sign in to comment.