Skip to content

Commit

Permalink
v1.5.1 (#21)
Browse files Browse the repository at this point in the history
- Assert the log messages when using templated messages (fixes #20).
  • Loading branch information
GillesTourreau authored Mar 15, 2024
1 parent 4d5fdea commit 91b7877
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build/azure-pipelines-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ parameters:
- name: VersionPrefix
displayName: The version of the library
type: string
default: 1.5.0
default: 1.5.1
- name: VersionSuffix
displayName: The version suffix of the library (rc.1). Use a space ' ' if no suffix.
type: string
Expand Down
9 changes: 7 additions & 2 deletions src/Logging.Assertions/LoggerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,16 @@ public void Assert<TState>(LogLevel logLevel, TState state, Exception? exception
{
var stateAsList = (IReadOnlyList<KeyValuePair<string, object?>>)state!;

var originalMessage = stateAsList.Single(kv => kv.Key == "{OriginalFormat}");
var originalMessage = (string)stateAsList.Single(kv => kv.Key == "{OriginalFormat}").Value!;

if (originalMessage != this.message)
{
Services.ThrowException($"Wrong log message for the Log({logLevel}) method call. (Expected: '{this.message}', Actual: '{originalMessage}')");
}

if (stateAsList.Count - 1 != this.arguments.Count)
{
Services.ThrowException($"Incorrect template message argument count for the '{originalMessage.Value}' template message. (Expected: '{this.arguments.Count}', Actual: '{stateAsList.Count - 1}')");
Services.ThrowException($"Incorrect template message argument count for the '{originalMessage}' template message. (Expected: '{this.arguments.Count}', Actual: '{stateAsList.Count - 1}')");
}

var messageArguments = new LogMessageTemplateArguments(
Expand Down
3 changes: 3 additions & 0 deletions src/Logging.Assertions/Logging.Assertions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.Logging.Assertions</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>
1.5.1
- Fix a bug to check the log messages when using message templates assertions.

1.5.0
- Add the support to mock the ILogger.IsEnabled(LogLevel) method.

Expand Down
37 changes: 37 additions & 0 deletions tests/Logging.Assertions.Tests/LoggerMockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,27 @@ public void LogWithMessageTemplate_DelegateAssertion_WrongExpectedArgumentCount(
.WithMessage("Incorrect template message argument count for the 'Log information with parameters {Id}, {Name} and {Object}' template message. (Expected: '100', Actual: '3')");
}

[Fact]
public void LogWithMessageTemplate_DelegateAssertion_DifferentMessage()
{
var logger = this.CreateLogger();
logger.SetupSequence()
.LogInformation("DIFFERENT MESSAGE {Id}, {Name} and {Object}")
.WithArguments(3, args =>
{
args["Id"].Should().Be(1234);
args["Name"].Should().Be("The name");
args["Object"].Should().BeEquivalentTo(new { Property = "I am object" });
})
.LogError("Log error after message template");

var objectToLog = new ObjectToLog(logger.Object);

objectToLog.Invoking(o => o.InvokeWithMessageTemplate())
.Should().ThrowExactly<XunitException>()
.WithMessage("Wrong log message for the Log(Information) method call. (Expected: 'DIFFERENT MESSAGE {Id}, {Name} and {Object}', Actual: 'Log information with parameters {Id}, {Name} and {Object}')");
}

[Fact]
public void LogWithMessageTemplate_ParametersAssertion()
{
Expand Down Expand Up @@ -354,6 +375,22 @@ public void LogWithMessageTemplate_ParametersAssertion_WrongExpectedArgumentCoun
.WithMessage("Incorrect template message argument count for the 'Log information with parameters {Id}, {Name} and {Object}' template message. (Expected: '9', Actual: '3')");
}

[Fact]
public void LogWithMessageTemplate_ParametersAssertion_DifferentMessage()
{
var logger = this.CreateLogger();
logger.SetupSequence()
.LogInformation("DIFFERENT MESSAGE {Id}, {Name} and {Object}")
.WithArguments(1234, "The name", new { Property = "I am object" })
.LogError("Log error after message template");

var objectToLog = new ObjectToLog(logger.Object);

objectToLog.Invoking(o => o.InvokeWithMessageTemplate())
.Should().ThrowExactly<XunitException>()
.WithMessage("Wrong log message for the Log(Information) method call. (Expected: 'DIFFERENT MESSAGE {Id}, {Name} and {Object}', Actual: 'Log information with parameters {Id}, {Name} and {Object}')");
}

[Fact]
public void LoggerCalledToManyTimes()
{
Expand Down

0 comments on commit 91b7877

Please sign in to comment.