diff --git a/src/nunit.analyzers.tests/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringCodeFixTests.cs b/src/nunit.analyzers.tests/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringCodeFixTests.cs index faf9fe7c..179f3103 100644 --- a/src/nunit.analyzers.tests/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringCodeFixTests.cs +++ b/src/nunit.analyzers.tests/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringCodeFixTests.cs @@ -56,11 +56,11 @@ public void ConvertWhenMinimumParametersIsTwo() { var code = TestUtility.WrapInTestMethod(@" const int actual = 42; - ↓Assert.That(actual, Is.EqualTo(42), ""Expected actual value to be 42, but was: {0}"", actual); + ↓Assert.That(actual, Is.EqualTo(42), ""Expected actual value to be 42, but was: {0} at time {1:HH:mm:ss}"", actual, DateTime.Now); "); var fixedCode = TestUtility.WrapInTestMethod(@" const int actual = 42; - Assert.That(actual, Is.EqualTo(42), $""Expected actual value to be 42, but was: {actual}""); + Assert.That(actual, Is.EqualTo(42), $""Expected actual value to be 42, but was: {actual} at time {DateTime.Now:HH:mm:ss}""); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } @@ -82,6 +82,31 @@ public void TestConvertWithEmbeddedSpecialCharacters(string text) RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } + + [TestCase("")] + [TestCase(":N1")] + [TestCase(",5")] + [TestCase(",-5")] + [TestCase(",5:N1")] + public void TestFormatModifiers(string modifier) + { + string argument = "1.23"; + var code = TestUtility.WrapInTestMethod($"↓Assert.Pass(\"{{0{modifier}}}\", {argument});"); + + var fixedCode = TestUtility.WrapInTestMethod($"Assert.Pass($\"{{{argument}{modifier}}}\");"); + + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } + + [Test] + public void TestEscapedBraces() + { + var code = TestUtility.WrapInTestMethod("↓Assert.Pass(\"{{{0}}}\", 42);"); + + var fixedCode = TestUtility.WrapInTestMethod("Assert.Pass($\"{{{42}}}\");"); + + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } } } #endif diff --git a/src/nunit.analyzers.tests/WithinUsage/WithinUsageAnalyzerTests.cs b/src/nunit.analyzers.tests/WithinUsage/WithinUsageAnalyzerTests.cs index 7ec8a3df..6e0b7285 100644 --- a/src/nunit.analyzers.tests/WithinUsage/WithinUsageAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/WithinUsage/WithinUsageAnalyzerTests.cs @@ -373,7 +373,7 @@ public void TestMethod() {{ var instance = new UserDefinedType(1, 1.1, ""1.1""); - Assert.That(new UserDefinedType(1, 1.2, ""1.1""), Is.EqualTo(instance).Within(0.1)); + Assert.That(new UserDefinedType(1, 1.2, ""1.1""), Is.EqualTo(instance).↓Within(0.1)); }} private {kind} UserDefinedType @@ -419,7 +419,7 @@ public void TestMethod() { var instance = new SomeRecord(1, 1.1, ""1.1""); - Assert.That(new SomeRecord(1, 1.2, ""1.1""), Is.EqualTo(instance).Within(0.1)); + Assert.That(new SomeRecord(1, 1.2, ""1.1""), Is.EqualTo(instance).↓Within(0.1)); } private sealed record SomeRecord diff --git a/src/nunit.analyzers/Helpers/CodeFixHelper.cs b/src/nunit.analyzers/Helpers/CodeFixHelper.cs index 4294c334..f1ac40a5 100644 --- a/src/nunit.analyzers/Helpers/CodeFixHelper.cs +++ b/src/nunit.analyzers/Helpers/CodeFixHelper.cs @@ -159,7 +159,7 @@ internal static IEnumerable UpdateStringFormatT string formatClause = formatSpecification.Substring(++argumentFormatSpecification, argumentSpecificationEnd - argumentFormatSpecification); interpolationFormat = SyntaxFactory.InterpolationFormatClause( SyntaxFactory.Token(SyntaxKind.ColonToken), - SyntaxFactory.Literal(formatClause)); + InterpolatedStringTextToken(formatClause)); } yield return SyntaxFactory.Interpolation(formatArgument, interpolationAlignment, interpolationFormat); diff --git a/src/nunit.analyzers/Helpers/Constraints.cs b/src/nunit.analyzers/Helpers/Constraints.cs index 1d070a6f..2dc94145 100644 --- a/src/nunit.analyzers/Helpers/Constraints.cs +++ b/src/nunit.analyzers/Helpers/Constraints.cs @@ -7,7 +7,7 @@ namespace NUnit.Analyzers.Helpers { internal sealed class Constraints { - public Constraints(string staticClass, string? modifier, string? constraintMethod, string? property1 = default, string? property2 = null) + public Constraints(string staticClass, string? modifier, string? constraintMethod, string? property1 = null, string? property2 = null) { this.StaticClass = staticClass; this.Modifier = modifier;