|
1 | | -using System.Threading.Tasks; |
| 1 | +using System.Threading.Tasks; |
2 | 2 | using Meziantou.Analyzer.Rules; |
3 | 3 | using Meziantou.Analyzer.Test.Helpers; |
4 | 4 | using TestHelper; |
@@ -475,6 +475,251 @@ await CreateProjectBuilder() |
475 | 475 | .WithSourceCode(SourceCode) |
476 | 476 | .AddAdditionalFile("LoggerParameterTypes.txt", """ |
477 | 477 | Prop;System.Int32 |
| 478 | +""") |
| 479 | + .ValidateAsync(); |
| 480 | + } |
| 481 | + |
| 482 | + [Fact] |
| 483 | + public async Task LoggerMessageAttribute_ValidParameterTypes() |
| 484 | + { |
| 485 | + const string SourceCode = """ |
| 486 | +using Microsoft.Extensions.Logging; |
| 487 | +using System.Runtime.CompilerServices; |
| 488 | +
|
| 489 | +partial class LoggerExtensions |
| 490 | +{ |
| 491 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message with {Prop} and {Name}")] |
| 492 | + static partial void LogTestMessage(ILogger logger, string Prop, int Name); |
| 493 | +} |
| 494 | +
|
| 495 | +class Program { static void Main() { } } |
| 496 | +"""; |
| 497 | + await CreateProjectBuilder() |
| 498 | + .WithSourceCode(SourceCode) |
| 499 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 500 | +Prop;System.String |
| 501 | +Name;System.Int32 |
| 502 | +""") |
| 503 | + .ValidateAsync(); |
| 504 | + } |
| 505 | + |
| 506 | + [Fact] |
| 507 | + public async Task LoggerMessageAttribute_InvalidParameterType() |
| 508 | + { |
| 509 | + const string SourceCode = """ |
| 510 | +using Microsoft.Extensions.Logging; |
| 511 | +using System.Runtime.CompilerServices; |
| 512 | +
|
| 513 | +partial class LoggerExtensions |
| 514 | +{ |
| 515 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message with {Prop} and {Name}")] |
| 516 | + static partial void LogTestMessage(ILogger logger, int [|Prop|], string Name); |
| 517 | +} |
| 518 | +"""; |
| 519 | + await CreateProjectBuilder() |
| 520 | + .WithSourceCode(SourceCode) |
| 521 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 522 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 523 | +Prop;System.String |
| 524 | +Name;System.String |
| 525 | +""") |
| 526 | + .ValidateAsync(); |
| 527 | + } |
| 528 | + |
| 529 | + [Fact] |
| 530 | + public async Task LoggerMessageAttribute_MultipleInvalidParameterTypes() |
| 531 | + { |
| 532 | + const string SourceCode = """ |
| 533 | +using Microsoft.Extensions.Logging; |
| 534 | +using System.Runtime.CompilerServices; |
| 535 | +
|
| 536 | +partial class LoggerExtensions |
| 537 | +{ |
| 538 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message with {Prop} and {Name}")] |
| 539 | + static partial void LogTestMessage(ILogger logger, int [|Prop|], int [|Name|]); |
| 540 | +} |
| 541 | +"""; |
| 542 | + await CreateProjectBuilder() |
| 543 | + .WithSourceCode(SourceCode) |
| 544 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 545 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 546 | +Prop;System.String |
| 547 | +Name;System.String |
| 548 | +""") |
| 549 | + .ValidateAsync(); |
| 550 | + } |
| 551 | + |
| 552 | + [Fact] |
| 553 | + public async Task LoggerMessageAttribute_MissingConfiguration() |
| 554 | + { |
| 555 | + const string SourceCode = """ |
| 556 | +using Microsoft.Extensions.Logging; |
| 557 | +using System.Runtime.CompilerServices; |
| 558 | +
|
| 559 | +partial class LoggerExtensions |
| 560 | +{ |
| 561 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message with {Prop} and {Name}")] |
| 562 | + static partial void LogTestMessage(ILogger logger, string [|Prop|], int Name); |
| 563 | +} |
| 564 | +"""; |
| 565 | + await CreateProjectBuilder() |
| 566 | + .WithSourceCode(SourceCode) |
| 567 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 568 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 569 | +Name;System.Int32 |
| 570 | +""") |
| 571 | + .ShouldReportDiagnosticWithMessage("Log parameter 'Prop' has no configured type") |
| 572 | + .ValidateAsync(); |
| 573 | + } |
| 574 | + |
| 575 | + [Fact] |
| 576 | + public async Task LoggerMessageAttribute_DeniedParameter() |
| 577 | + { |
| 578 | + const string SourceCode = """ |
| 579 | +using Microsoft.Extensions.Logging; |
| 580 | +using System.Runtime.CompilerServices; |
| 581 | +
|
| 582 | +partial class LoggerExtensions |
| 583 | +{ |
| 584 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message with {Prop} and {Name}")] |
| 585 | + static partial void LogTestMessage(ILogger logger, string [|Prop|], int Name); |
| 586 | +} |
| 587 | +"""; |
| 588 | + await CreateProjectBuilder() |
| 589 | + .WithSourceCode(SourceCode) |
| 590 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 591 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 592 | +Name;System.Int32 |
| 593 | +Prop; |
| 594 | +""") |
| 595 | + .ShouldReportDiagnosticWithMessage("Log parameter 'Prop' is not allowed by configuration") |
| 596 | + .ValidateAsync(); |
| 597 | + } |
| 598 | + |
| 599 | + [Fact] |
| 600 | + public async Task LoggerMessageAttribute_SkipILoggerParameter() |
| 601 | + { |
| 602 | + const string SourceCode = """ |
| 603 | +using Microsoft.Extensions.Logging; |
| 604 | +
|
| 605 | +partial class LoggerExtensions |
| 606 | +{ |
| 607 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message with {Name}")] |
| 608 | + static partial void LogTestMessage(ILogger logger, int Name); |
| 609 | +} |
| 610 | +"""; |
| 611 | + await CreateProjectBuilder() |
| 612 | + .WithSourceCode(SourceCode) |
| 613 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 614 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 615 | +Name;System.Int32 |
| 616 | +""") |
| 617 | + .ValidateAsync(); |
| 618 | + } |
| 619 | + |
| 620 | + [Fact] |
| 621 | + public async Task LoggerMessageAttribute_WithCallerMemberName() |
| 622 | + { |
| 623 | + const string SourceCode = """ |
| 624 | +using Microsoft.Extensions.Logging; |
| 625 | +using System.Runtime.CompilerServices; |
| 626 | +
|
| 627 | +partial class LoggerExtensions |
| 628 | +{ |
| 629 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message from {Method} with {Name}")] |
| 630 | + static partial void LogTestMessage(ILogger logger, int Name, [CallerMemberName] string Method = ""); |
| 631 | +} |
| 632 | +"""; |
| 633 | + await CreateProjectBuilder() |
| 634 | + .WithSourceCode(SourceCode) |
| 635 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 636 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 637 | +Method;System.String |
| 638 | +Name;System.Int32 |
| 639 | +""") |
| 640 | + .ValidateAsync(); |
| 641 | + } |
| 642 | + |
| 643 | + [Fact] |
| 644 | + public async Task LoggerMessageAttribute_NullableParameterType() |
| 645 | + { |
| 646 | + const string SourceCode = """ |
| 647 | +using Microsoft.Extensions.Logging; |
| 648 | +
|
| 649 | +partial class LoggerExtensions |
| 650 | +{ |
| 651 | + [LoggerMessage(10_004, LogLevel.Trace, "Test with {Value}")] |
| 652 | + static partial void LogTestMessage(ILogger logger, int Value); |
| 653 | + |
| 654 | + [LoggerMessage(10_005, LogLevel.Trace, "Test with {Value}")] |
| 655 | + static partial void LogTestMessage2(ILogger logger, int? Value); |
| 656 | +} |
| 657 | +"""; |
| 658 | + await CreateProjectBuilder() |
| 659 | + .WithSourceCode(SourceCode) |
| 660 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 661 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 662 | +Value;System.Nullable{System.Int32} |
| 663 | +""") |
| 664 | + .ValidateAsync(); |
| 665 | + } |
| 666 | + |
| 667 | + [Fact] |
| 668 | + public async Task LoggerMessageAttribute_NoConfiguration() |
| 669 | + { |
| 670 | + const string SourceCode = """ |
| 671 | +using Microsoft.Extensions.Logging; |
| 672 | +
|
| 673 | +partial class LoggerExtensions |
| 674 | +{ |
| 675 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message with {Prop}")] |
| 676 | + static partial void LogTestMessage(ILogger logger, string Prop); |
| 677 | +} |
| 678 | +"""; |
| 679 | + await CreateProjectBuilder() |
| 680 | + .WithSourceCode(SourceCode) |
| 681 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 682 | + .ValidateAsync(); |
| 683 | + } |
| 684 | + |
| 685 | + [Fact] |
| 686 | + public async Task LoggerMessageAttribute_EmptyFormatString() |
| 687 | + { |
| 688 | + const string SourceCode = """ |
| 689 | +using Microsoft.Extensions.Logging; |
| 690 | +
|
| 691 | +partial class LoggerExtensions |
| 692 | +{ |
| 693 | + [LoggerMessage(10_004, LogLevel.Trace, "")] |
| 694 | + static partial void LogTestMessage(ILogger logger); |
| 695 | +} |
| 696 | +"""; |
| 697 | + await CreateProjectBuilder() |
| 698 | + .WithSourceCode(SourceCode) |
| 699 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 700 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 701 | +Name;System.Int32 |
| 702 | +""") |
| 703 | + .ValidateAsync(); |
| 704 | + } |
| 705 | + |
| 706 | + [Fact] |
| 707 | + public async Task LoggerMessageAttribute_NoFormatParameters() |
| 708 | + { |
| 709 | + const string SourceCode = """ |
| 710 | +using Microsoft.Extensions.Logging; |
| 711 | +
|
| 712 | +partial class LoggerExtensions |
| 713 | +{ |
| 714 | + [LoggerMessage(10_004, LogLevel.Trace, "Test message without parameters")] |
| 715 | + static partial void LogTestMessage(ILogger logger); |
| 716 | +} |
| 717 | +"""; |
| 718 | + await CreateProjectBuilder() |
| 719 | + .WithSourceCode(SourceCode) |
| 720 | + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary) |
| 721 | + .AddAdditionalFile("LoggerParameterTypes.txt", """ |
| 722 | +Name;System.Int32 |
478 | 723 | """) |
479 | 724 | .ValidateAsync(); |
480 | 725 | } |
|
0 commit comments