Skip to content

Commit 90a6261

Browse files
committed
Fixes issue with wildcards used in string interpolations
or other methods calls
1 parent 1fe4303 commit 90a6261

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/Moq.ILogger/VerifyLogExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ Expression<Func<object, Type, bool>> CreateCompareLambdaFrom(Expression methodCa
617617
var verifyLogConstantExpression = Expression.Constant(verifyLogExpression, typeof(VerifyLogExpression));
618618

619619
var compareMessagesCallExpression = Expression.Call(typeof(VerifyLogExtensions), nameof(CompareMessages), null,
620-
vParamToStringExpression, verifyLogConstantExpression, methodCallExpression);
620+
methodCallExpression, verifyLogConstantExpression, vParamToStringExpression);
621621
compareExpression = Expression.Lambda<Func<object, Type, bool>>(compareMessagesCallExpression, vParam, tParam);
622622
return compareExpression;
623623
}

tests/Moq.ILogger.Tests/Samples/SomeClassTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public void LoggingInformation()
1616
public void LoggingWarning(string name)
1717
=> _logger.LogWarning(new ArgumentException("The given name is not ok", nameof(name)), "This operation failed, but let's log an warning only");
1818

19+
public void LoggingError(string name)
20+
=> _logger.LogError(new ArgumentException("The given name is not ok", nameof(name)), $"This operation failed for name {name}");
21+
1922
public void SemanticLogging()
2023
{
2124
var position = new { Latitude = 25, Longitude = 134 };
@@ -45,7 +48,7 @@ public void Verify_log_information_with_a_message()
4548
sut.LoggingInformation();
4649

4750
loggerMock.VerifyLog(logger => logger.LogInformation("This operation is successful."));
48-
loggerMock.VerifyLog(logger => logger.LogInformation((EventId)0,"This operation is successful."));
51+
loggerMock.VerifyLog(logger => logger.LogInformation((EventId)0, "This operation is successful."));
4952
loggerMock.VerifyLog(logger => logger.LogInformation("This * is successful."));
5053
loggerMock.VerifyLog(logger => logger.LogInformation(It.Is<string>(msg => msg.Length > 5)));
5154
loggerMock.VerifyLog(logger => logger.LogInformation(It.IsAny<string>()));
@@ -70,6 +73,18 @@ public void Verify_errors()
7073
loggerMock.VerifyLog(logger => logger.LogWarning(It.IsAny<EventId>(), new ArgumentException("The given name is not ok", "name"), "*failed*"));
7174
}
7275

76+
[Fact]
77+
public void Verify_exceptions_logged_as_errors()
78+
{
79+
var name = "Adrian";
80+
var loggerMock = new Mock<ILogger<SomeClass>>();
81+
var sut = new SomeClass(loggerMock.Object);
82+
83+
sut.LoggingError(name);
84+
85+
loggerMock.VerifyLog(logger => logger.LogError(It.Is<ArgumentException>(ex => ex.ParamName == "name"), $"*{name}*"));
86+
}
87+
7388
[Fact]
7489
public void Verify_semantic_logging()
7590
{

tests/Moq.ILogger.Tests/VerifyLogExtensionsTests.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,32 @@ public void Verify_a_structured_message_using_wildcards_with_parameters_names_an
299299
.WithMessage("*.LogInformation*");
300300
}
301301

302+
[Fact]
303+
public void Verify_a_message_using_wildcards_with_interpolated_message_it_verifies()
304+
{
305+
var loggerMock = new Mock<ILogger<object>>();
306+
var interpolatedMessageArg = "Arg";
307+
loggerMock.Object.LogInformation($"Test message {interpolatedMessageArg}");
308+
309+
Action act = () => loggerMock.VerifyLog(c => c.LogInformation($"*{interpolatedMessageArg}*"));
310+
311+
act.Should().NotThrow();
312+
}
313+
314+
[Fact]
315+
public void Verify_a_message_using_wildcards_with_interpolated_message_it_throws()
316+
{
317+
var loggerMock = new Mock<ILogger<object>>();
318+
var interpolatedMessageArg1 = "Arg 1";
319+
var interpolatedMessageArg2 = "Arg 2";
320+
loggerMock.Object.LogInformation($"Test message {interpolatedMessageArg1}");
321+
322+
Action act = () => loggerMock.VerifyLog(c => c.LogInformation($"*{interpolatedMessageArg2}*"));
323+
324+
act.Should().ThrowExactly<VerifyLogException>()
325+
.WithMessage("*.LogInformation*");
326+
}
327+
302328
[Fact]
303329
public void Verify_a_structured_message_when_parameters_differ_it_throws()
304330
{
@@ -390,10 +416,36 @@ public void Verify_a_message_with_a_different_one_from_a_method_call_it_throws()
390416

391417
act.Should().ThrowExactly<VerifyLogException>()
392418
.WithMessage("*.LogInformation(VerifyLogExtensionsTests.GetNotAMessage(), new[] { })*")
393-
.Which.InnerException!.Message.Should().Match("*logger => logger.Log<It.IsAnyType>(LogLevel.Information, It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => VerifyLogExtensions.CompareMessages(v.ToString(), VerifyLogExpression, \"Not a test message\")), It.IsAny<Exception>(), (Func<It.IsAnyType, Exception, string>)It.IsAny<object>())*");
419+
.Which.InnerException!.Message.Should().Match("*logger => logger.Log<It.IsAnyType>(LogLevel.Information, It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => VerifyLogExtensions.CompareMessages(\"Not a test message\", VerifyLogExpression, v.ToString())), It.IsAny<Exception>(), (Func<It.IsAnyType, Exception, string>)It.IsAny<object>())*");
394420
}
395421
string GetNotAMessage() => "Not a test message";
396422

423+
[Fact]
424+
public void Verify_a_wildcard_message_with_method_call_it_verifies()
425+
{
426+
var loggerMock = new Mock<ILogger<object>>();
427+
loggerMock.Object.LogInformation("Test message");
428+
429+
Action act = () => loggerMock.VerifyLog(c => c.LogInformation(GetWildcardMessage()));
430+
431+
act.Should().NotThrow();
432+
}
433+
string GetWildcardMessage() => "*message*";
434+
435+
[Fact]
436+
public void Verify_a_wildcard_message_with_a_different_one_from_a_method_call_it_throws()
437+
{
438+
var loggerMock = new Mock<ILogger<object>>();
439+
loggerMock.Object.LogInformation("Test message");
440+
441+
Action act = () => loggerMock.VerifyLog(c => c.LogInformation(GetWildcardNotAMessage()));
442+
443+
act.Should().ThrowExactly<VerifyLogException>()
444+
.WithMessage("*.LogInformation(VerifyLogExtensionsTests.GetWildcardNotAMessage(), new[] { })*")
445+
.Which.InnerException!.Message.Should().Match("*logger => logger.Log<It.IsAnyType>(LogLevel.Information, It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => VerifyLogExtensions.CompareMessages(\"*no match*\", VerifyLogExpression, v.ToString())), It.IsAny<Exception>(), (Func<It.IsAnyType, Exception, string>)It.IsAny<object>())*");
446+
}
447+
string GetWildcardNotAMessage() => "*no match*";
448+
397449
[Fact]
398450
public void Verify_a_message_one_constructed_call_it_throws()
399451
{
@@ -404,7 +456,7 @@ public void Verify_a_message_one_constructed_call_it_throws()
404456

405457
act.Should().ThrowExactly<VerifyLogException>()
406458
.WithMessage("*.LogInformation(new string(new[] { a }), new[] { })*")
407-
.Which.InnerException!.Message.Should().Match("*logger => logger.Log<It.IsAnyType>(LogLevel.Information, It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => VerifyLogExtensions.CompareMessages(v.ToString(), VerifyLogExpression, \"a\")), It.IsAny<Exception>(), (Func<It.IsAnyType, Exception, string>)It.IsAny<object>())*");
459+
.Which.InnerException!.Message.Should().Match("*logger => logger.Log<It.IsAnyType>(LogLevel.Information, It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => VerifyLogExtensions.CompareMessages(\"a\", VerifyLogExpression, v.ToString())), It.IsAny<Exception>(), (Func<It.IsAnyType, Exception, string>)It.IsAny<object>())*");
408460
}
409461

410462
[Fact]

0 commit comments

Comments
 (0)