|
| 1 | +// © 2024 Koninklijke Philips N.V. See License.md in the project root for license information. |
| 2 | + |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | +using Philips.CodeAnalysis.MaintainabilityAnalyzers.Maintainability; |
| 7 | +using Philips.CodeAnalysis.Test.Helpers; |
| 8 | +using Philips.CodeAnalysis.Test.Verifiers; |
| 9 | + |
| 10 | +namespace Philips.CodeAnalysis.Test.Maintainability.Maintainability |
| 11 | +{ |
| 12 | + [TestClass] |
| 13 | + public class AvoidStringFormatInInterpolatedStringAnalyzerTest : DiagnosticVerifier |
| 14 | + { |
| 15 | + protected override DiagnosticAnalyzer GetDiagnosticAnalyzer() |
| 16 | + { |
| 17 | + return new AvoidStringFormatInInterpolatedStringAnalyzer(); |
| 18 | + } |
| 19 | + |
| 20 | + [TestMethod] |
| 21 | + [TestCategory(TestDefinitions.UnitTests)] |
| 22 | + public async Task StringFormatInInterpolatedStringTriggersWarning() |
| 23 | + { |
| 24 | + var code = @" |
| 25 | +using System; |
| 26 | +
|
| 27 | +class Test |
| 28 | +{ |
| 29 | + public void Method() |
| 30 | + { |
| 31 | + var firstName = ""John""; |
| 32 | + var lastName = ""Doe""; |
| 33 | + var result = $""Hello {string.Format(""{0} {1}"", firstName, lastName)}""; |
| 34 | + } |
| 35 | +} |
| 36 | +"; |
| 37 | + await VerifyDiagnostic(code).ConfigureAwait(false); |
| 38 | + } |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + [TestCategory(TestDefinitions.UnitTests)] |
| 42 | + public async Task StringFormatWithMultipleArgumentsTriggersWarning() |
| 43 | + { |
| 44 | + var code = @" |
| 45 | +using System; |
| 46 | +
|
| 47 | +class Test |
| 48 | +{ |
| 49 | + public void Method() |
| 50 | + { |
| 51 | + var name = ""John""; |
| 52 | + var age = 30; |
| 53 | + var result = $""User info: {string.Format(""Name: {0}, Age: {1}"", name, age)}""; |
| 54 | + } |
| 55 | +} |
| 56 | +"; |
| 57 | + await VerifyDiagnostic(code).ConfigureAwait(false); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + [TestCategory(TestDefinitions.UnitTests)] |
| 62 | + public async Task InterpolatedStringWithoutStringFormatIsOk() |
| 63 | + { |
| 64 | + var code = @" |
| 65 | +using System; |
| 66 | +
|
| 67 | +class Test |
| 68 | +{ |
| 69 | + public void Method() |
| 70 | + { |
| 71 | + var firstName = ""John""; |
| 72 | + var lastName = ""Doe""; |
| 73 | + var result = $""Hello {firstName} {lastName}""; |
| 74 | + } |
| 75 | +} |
| 76 | +"; |
| 77 | + await VerifySuccessfulCompilation(code).ConfigureAwait(false); |
| 78 | + } |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + [TestCategory(TestDefinitions.UnitTests)] |
| 82 | + public async Task StringFormatOutsideInterpolatedStringIsOk() |
| 83 | + { |
| 84 | + var code = @" |
| 85 | +using System; |
| 86 | +
|
| 87 | +class Test |
| 88 | +{ |
| 89 | + public void Method() |
| 90 | + { |
| 91 | + var firstName = ""John""; |
| 92 | + var lastName = ""Doe""; |
| 93 | + var result = string.Format(""Hello {0} {1}"", firstName, lastName); |
| 94 | + } |
| 95 | +} |
| 96 | +"; |
| 97 | + await VerifySuccessfulCompilation(code).ConfigureAwait(false); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments