From acb700b566dadc85415214854192ba8f7c3f0949 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Sun, 14 Apr 2024 17:24:22 +0800 Subject: [PATCH] Add test for wrapping conditional expression in parenthesis for NUnit2050 --- ...ngFormatToInterpolatableStringCodeFixTests.cs | 16 ++++++++++++++++ src/nunit.analyzers/Helpers/CodeFixHelper.cs | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/nunit.analyzers.tests/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringCodeFixTests.cs b/src/nunit.analyzers.tests/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringCodeFixTests.cs index da3ef6f8..5ebfc285 100644 --- a/src/nunit.analyzers.tests/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringCodeFixTests.cs +++ b/src/nunit.analyzers.tests/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringCodeFixTests.cs @@ -130,6 +130,22 @@ public void TestEscapedBraces() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } + + [Test] + public void TestConditionalExpressionArgument() + { + var code = TestUtility.WrapInTestMethod(@" + string? someValue = ""Value""; + ↓Assert.Pass(""Value is {0}"", someValue is null ? ""null"" : someValue); + "); + + var fixedCode = TestUtility.WrapInTestMethod(@" + string? someValue = ""Value""; + Assert.Pass($""Value is {(someValue is null ? ""null"" : someValue)}""); + "); + + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } #endif } diff --git a/src/nunit.analyzers/Helpers/CodeFixHelper.cs b/src/nunit.analyzers/Helpers/CodeFixHelper.cs index 0fe35241..61ccd3ba 100644 --- a/src/nunit.analyzers/Helpers/CodeFixHelper.cs +++ b/src/nunit.analyzers/Helpers/CodeFixHelper.cs @@ -162,7 +162,7 @@ internal static IEnumerable UpdateStringFormatT .OfType() .Any()) { - // Colon is not allowed in an Interpolation, wrap expression in parenthesis: (expession). + // Colon is not allowed in an Interpolation, wrap expression in parenthesis: (expression). formatArgument = SyntaxFactory.ParenthesizedExpression(formatArgument); }