From e809f3bdeada5821bec9a0b2b64e2e2804a12b71 Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Sun, 31 Mar 2024 16:47:59 -0400 Subject: [PATCH 01/17] Fix #712: AreEqualClassicModelAssertUsageCodeFix no longer assumes that the first argument is expected and the second argument is actual --- ...qualClassicModelAssertUsageCodeFixTests.cs | 16 ++++++++++++++ .../AreEqualClassicModelAssertUsageCodeFix.cs | 21 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs index e1ddfdc6..c01680f5 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs @@ -40,6 +40,22 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyAreEqualFixWithNamedParametersInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + ↓ClassicAssert.AreEqual(actual: 3d, expected: 2d); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: 3d, Is.EqualTo(expected: 2d)); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void VerifyAreEqualFixWithMessage() { diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs index 2c0b7105..a35e3cca 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs @@ -18,6 +18,15 @@ public sealed class AreEqualClassicModelAssertUsageCodeFix protected override void UpdateArguments(Diagnostic diagnostic, List arguments) { + // If named parameters are used, we can't assume that the first argument is the expected one and the second is the actual. + // Therefore, start by finding the expected argument and the actual argument. + var expectedPosition = arguments.FindIndex(arg => arg.NameColon?.Name.Identifier.Text == NUnitFrameworkConstants.NameOfExpectedParameter); + var actualPosition = arguments.FindIndex(arg => arg.NameColon?.Name.Identifier.Text == NUnitFrameworkConstants.NameOfActualParameter); + + // If named arguments are not used, the first argument is expected, and the second is actual. + var expectedArgument = expectedPosition != -1 ? arguments[expectedPosition] : arguments[0]; + var actualArgument = actualPosition != -1 ? arguments[actualPosition] : arguments[1]; + // Note that if there's a 3rd argument and it's a double, // it has to be added to the "Is.EqualTo(1st argument)" with ".Within(3rd argument)" var equalToInvocationNode = SyntaxFactory.InvocationExpression( @@ -26,7 +35,7 @@ protected override void UpdateArguments(Diagnostic diagnostic, List Date: Mon, 1 Apr 2024 20:39:54 -0400 Subject: [PATCH 02/17] Add a new overload for UpdateArguments --- ...qualClassicModelAssertUsageCodeFixTests.cs | 16 +++++++ .../AreEqualClassicModelAssertUsageCodeFix.cs | 48 +++++++------------ .../ClassicModelAssertUsageCodeFix.cs | 23 ++++++++- 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs index c01680f5..8c4c79d1 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs @@ -40,6 +40,22 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyAreEqualFixWhenToleranceExistsWithNamedArgumentButInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + ↓ClassicAssert.AreEqual(delta: 0.0000001d, actual: 3d, expected: 2d); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: 3d, Is.EqualTo(expected: 2d).Within(0.0000001d)); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void VerifyAreEqualFixWithNamedParametersInNonstandardOrder() { diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs index a35e3cca..37401efe 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Composition; +using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp; @@ -16,16 +17,10 @@ public sealed class AreEqualClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.AreEqualUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override List UpdateArguments(Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - // If named parameters are used, we can't assume that the first argument is the expected one and the second is the actual. - // Therefore, start by finding the expected argument and the actual argument. - var expectedPosition = arguments.FindIndex(arg => arg.NameColon?.Name.Identifier.Text == NUnitFrameworkConstants.NameOfExpectedParameter); - var actualPosition = arguments.FindIndex(arg => arg.NameColon?.Name.Identifier.Text == NUnitFrameworkConstants.NameOfActualParameter); - - // If named arguments are not used, the first argument is expected, and the second is actual. - var expectedArgument = expectedPosition != -1 ? arguments[expectedPosition] : arguments[0]; - var actualArgument = actualPosition != -1 ? arguments[actualPosition] : arguments[1]; + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; // Note that if there's a 3rd argument and it's a double, // it has to be added to the "Is.EqualTo(1st argument)" with ".Within(3rd argument)" @@ -37,13 +32,14 @@ protected override void UpdateArguments(Diagnostic diagnostic, List() { actualArgument, SyntaxFactory.Argument(equalToInvocationNode) }; + var handledParameterNames = new[] { - arguments[0] = SyntaxFactory.Argument( - SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter), - actualArgument.RefKindKeyword, - actualArgument.Expression); - } - - // ... and if the 3rd argument was a double, that has to go too. - if (hasToleranceValue) - { - arguments.RemoveAt(2); - } + NUnitFrameworkConstants.NameOfExpectedParameter, + NUnitFrameworkConstants.NameOfActualParameter, + NameOfDeltaParameter, + }; + return arguments + .Concat(argumentNamesToArguments + .Where(kvp => !handledParameterNames.Contains(kvp.Key)) + .Select(kvp => kvp.Value)) + .ToList(); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs index 881c679d..591d5813 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs @@ -60,9 +60,13 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) if (arguments.Count > 1) arguments[1] = CastIfNecessary(arguments[1]); + var argumentNamesToArguments = arguments.ToDictionary( + argument => GetParameterName(argument), + argument => argument); + // Do the rule specific conversion if (typeArguments is null) - this.UpdateArguments(diagnostic, arguments); + arguments = this.UpdateArguments(diagnostic, argumentNamesToArguments); else this.UpdateArguments(diagnostic, arguments, typeArguments); @@ -101,6 +105,16 @@ ArgumentSyntax CastIfNecessary(ArgumentSyntax argument) argument.Expression)); } + string GetParameterName(ArgumentSyntax argument) + { + if (argument.NameColon?.Name.Identifier.Text is { } argumentName) + return argumentName; + + var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(invocationNode).Symbol!; + var index = invocationNode.ArgumentList.Arguments.IndexOf(argument); + return methodSymbol!.Parameters[index].Name; + } + string? GetUserDefinedImplicitTypeConversion(ExpressionSyntax expression) { var typeInfo = semanticModel.GetTypeInfo(expression, context.CancellationToken); @@ -121,6 +135,13 @@ ArgumentSyntax CastIfNecessary(ArgumentSyntax argument) } } + protected virtual List UpdateArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) + { + throw new NotImplementedException($"Class must override {nameof(UpdateArguments)}"); + } + protected virtual void UpdateArguments(Diagnostic diagnostic, List arguments, TypeArgumentListSyntax typeArguments) { throw new InvalidOperationException($"Class must override {nameof(UpdateArguments)} accepting {nameof(TypeArgumentListSyntax)}"); From 59f012677e35b9b00ef565176874364e53e281ae Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Thu, 4 Apr 2024 22:09:09 -0400 Subject: [PATCH 03/17] Make all code functional but not yet readable/maintainable --- ...qualClassicModelAssertUsageCodeFixTests.cs | 46 +++++++++-- .../AreEqualClassicModelAssertUsageCodeFix.cs | 18 +---- .../ClassicModelAssertUsageCodeFix.cs | 81 ++++++++++++------- .../Constants/NUnitFrameworkConstants.cs | 1 + src/nunit.analyzers/Helpers/CodeFixHelper.cs | 36 +++++++++ ...ingFormatToInterpolatableStringAnalyzer.cs | 2 +- 6 files changed, 134 insertions(+), 50 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs index 8c4c79d1..c224c837 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs @@ -43,11 +43,11 @@ public void TestMethod() [Test] public void VerifyAreEqualFixWhenToleranceExistsWithNamedArgumentButInNonstandardOrder() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreEqual(delta: 0.0000001d, actual: 3d, expected: 2d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -59,11 +59,11 @@ public void TestMethod() [Test] public void VerifyAreEqualFixWithNamedParametersInNonstandardOrder() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreEqual(actual: 3d, expected: 2d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -120,6 +120,24 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyAreEqualFixWithMessageAnd2ParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var actual = 3d; + ↓ClassicAssert.AreEqual(delta: 0.0000001d, expected: 2d, actual: actual, args: new[] { ""Guid.NewGuid()"", Guid.NewGuid().ToString() }, message: ""message-id: {0}, {1}""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var actual = 3d; + Assert.That(actual: actual, Is.EqualTo(expected: 2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid().ToString() }""); + }"); // TODO: Fix whitespace + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void VerifyAreEqualFixWhenToleranceExists() { @@ -200,6 +218,22 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyAreEqualFixWhenToleranceExistsWithMessageAndParamsLiteralExpressionSyntax() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message-id: {0}"", ""mystring""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d), $""message-id: {""mystring""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void CodeFixPreservesLineBreakBeforeMessage() { diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs index 37401efe..656ca8a2 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Composition; -using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp; @@ -17,7 +16,9 @@ public sealed class AreEqualClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.AreEqualUsage); - protected override List UpdateArguments(Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgument) UpdateArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; @@ -50,18 +51,7 @@ protected override List UpdateArguments(Diagnostic diagnostic, I SyntaxFactory.SingletonSeparatedList(toleranceArgumentNoColon))); } - var arguments = new List() { actualArgument, SyntaxFactory.Argument(equalToInvocationNode) }; - var handledParameterNames = new[] - { - NUnitFrameworkConstants.NameOfExpectedParameter, - NUnitFrameworkConstants.NameOfActualParameter, - NameOfDeltaParameter, - }; - return arguments - .Concat(argumentNamesToArguments - .Where(kvp => !handledParameterNames.Contains(kvp.Key)) - .Select(kvp => kvp.Value)) - .ToList(); + return (actualArgument, SyntaxFactory.Argument(equalToInvocationNode)); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs index 591d5813..fcf3ec36 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; +using NUnit.Analyzers.Constants; using NUnit.Analyzers.Extensions; using NUnit.Analyzers.Helpers; @@ -52,34 +54,65 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) if (newInvocationNode is null) return; - // Now, replace the arguments. - List arguments = invocationNode.ArgumentList.Arguments.ToList(); + var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(invocationNode).Symbol!; - // See if we need to cast the arguments when they were using a specific classic overload. - arguments[0] = CastIfNecessary(arguments[0]); - if (arguments.Count > 1) - arguments[1] = CastIfNecessary(arguments[1]); + Dictionary argumentNamesToArguments = new(); + const string NameOfArgsParameter = "args"; - var argumentNamesToArguments = arguments.ToDictionary( - argument => GetParameterName(argument), - argument => argument); + // There can be 0 to any number of message and parameters. + List messageAndParams = new(); + var arguments = invocationNode.ArgumentList.Arguments.ToList(); + for (var i = 0; i < arguments.Count; ++i) + { + var argument = arguments[i]; + if (i < methodSymbol.Parameters.Length + && (argument.NameColon?.Name.Identifier.Text ?? methodSymbol.Parameters[i].Name) is { } argumentName + && argumentName is not (NUnitFrameworkConstants.NameOfMessageParameter or NameOfArgsParameter)) + { + // See if we need to cast the arguments when they were using a specific classic overload. + argumentNamesToArguments[argumentName] = + argumentName is NUnitFrameworkConstants.NameOfExpectedParameter or NUnitFrameworkConstants.NameOfActualParameter + ? CastIfNecessary(argument) + : argument; + } + else + { + messageAndParams.Add(argument); + } + } + + // Remove null message to avoid ambiguous calls. + if (argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfMessageParameter, out var messageArgument) + && messageArgument.Expression.IsKind(SyntaxKind.NullLiteralExpression)) + { + argumentNamesToArguments.Remove(NUnitFrameworkConstants.NameOfMessageParameter); + } + + // Now, replace the arguments. + List newArguments = new(); + ArgumentSyntax actualArgument, constraintArgument; // Do the rule specific conversion if (typeArguments is null) - arguments = this.UpdateArguments(diagnostic, argumentNamesToArguments); + { + (actualArgument, constraintArgument) = this.UpdateArguments(diagnostic, argumentNamesToArguments); + newArguments.Add(actualArgument); + newArguments.Add(constraintArgument); + if (CodeFixHelper.GetInterpolatedMessageArgumentOrDefault(messageAndParams) is { } interpolatedMessageArgument) + { + newArguments.Add(interpolatedMessageArgument); + } + } else + { this.UpdateArguments(diagnostic, arguments, typeArguments); - // Remove null message to avoid ambiguous calls. - if (arguments.Count == 3 && arguments[2].Expression.IsKind(SyntaxKind.NullLiteralExpression)) - { - arguments.RemoveAt(2); + // Do the format spec, params to formattable string conversion + CodeFixHelper.UpdateStringFormatToFormattableString(arguments, this.MinimumNumberOfParameters); + newArguments = arguments; } - // Do the format spec, params to formattable string conversion - CodeFixHelper.UpdateStringFormatToFormattableString(arguments, this.MinimumNumberOfParameters); - - var newArgumentsList = invocationNode.ArgumentList.WithArguments(arguments); + var newArgumentsList = invocationNode.ArgumentList.WithArguments(newArguments); newInvocationNode = newInvocationNode.WithArgumentList(newArgumentsList); context.CancellationToken.ThrowIfCancellationRequested(); @@ -105,16 +138,6 @@ ArgumentSyntax CastIfNecessary(ArgumentSyntax argument) argument.Expression)); } - string GetParameterName(ArgumentSyntax argument) - { - if (argument.NameColon?.Name.Identifier.Text is { } argumentName) - return argumentName; - - var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(invocationNode).Symbol!; - var index = invocationNode.ArgumentList.Arguments.IndexOf(argument); - return methodSymbol!.Parameters[index].Name; - } - string? GetUserDefinedImplicitTypeConversion(ExpressionSyntax expression) { var typeInfo = semanticModel.GetTypeInfo(expression, context.CancellationToken); @@ -135,7 +158,7 @@ string GetParameterName(ArgumentSyntax argument) } } - protected virtual List UpdateArguments( + protected virtual (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgument) UpdateArguments( Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { diff --git a/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs b/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs index 067c147b..57dfa23c 100644 --- a/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs +++ b/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs @@ -192,6 +192,7 @@ public static class NUnitFrameworkConstants public const string NameOfConditionParameter = "condition"; public const string NameOfExpectedParameter = "expected"; public const string NameOfExpressionParameter = "expression"; + public const string NameOfMessageParameter = "message"; public const string NameOfConstraintExpressionAnd = "And"; public const string NameOfConstraintExpressionOr = "Or"; diff --git a/src/nunit.analyzers/Helpers/CodeFixHelper.cs b/src/nunit.analyzers/Helpers/CodeFixHelper.cs index f1ac40a5..c20b2af2 100644 --- a/src/nunit.analyzers/Helpers/CodeFixHelper.cs +++ b/src/nunit.analyzers/Helpers/CodeFixHelper.cs @@ -35,6 +35,42 @@ internal static class CodeFixHelper return invocationExpression.ReplaceNode(invocationTargetNode, newInvocationTargetNode); } + /// + /// This is assumed to be arguments for an 'Assert.That(actual, constraint, "...: {0} - {1}", param0, param1)` + /// which needs converting into 'Assert.That(actual, constraint, $"...: {param0} - {param1}"). + /// + /// The arguments passed to the 'Assert' method. + /// The argument needed for the actual method, any more are assumed messages. + public static ArgumentSyntax? GetInterpolatedMessageArgumentOrDefault(List messageAndParams) + { + if (messageAndParams.Count == 0) + return null; + + var messageArgument = messageAndParams.SingleOrDefault(a => a.NameColon?.Name.Identifier.Text == NUnitFrameworkConstants.NameOfMessageParameter) + ?? messageAndParams.First(); + var formatSpecificationArgument = messageArgument.Expression; + if (formatSpecificationArgument.IsKind(SyntaxKind.NullLiteralExpression)) + return null; + + // We only support converting if the format specification is a constant string. + if (messageAndParams.Count == 1 || formatSpecificationArgument is not LiteralExpressionSyntax literalExpression) + return messageAndParams[0]; + + var formatSpecification = literalExpression.Token.ValueText; + + // var formatArgumentExpressions = new List(capacity: messageAndParams.Count - 1); + var argsExpression = messageAndParams.Single(a => a != messageArgument).Expression; + var formatArgumentExpressions = argsExpression is ImplicitArrayCreationExpressionSyntax args + ? args.Initializer.Expressions.ToArray() + : new[] { argsExpression }; + + var interpolatedStringContent = UpdateStringFormatToFormattableString(formatSpecification, formatArgumentExpressions); + var interpolatedString = SyntaxFactory.InterpolatedStringExpression( + SyntaxFactory.Token(SyntaxKind.InterpolatedStringStartToken), + SyntaxFactory.List(interpolatedStringContent)); + return SyntaxFactory.Argument(interpolatedString); + } + /// /// This is assumed to be arguments for an 'Assert.That(actual, constraint, "...: {0} - {1}", param0, param1)` /// which needs converting into 'Assert.That(actual, constraint, $"...: {param0} - {param1}"). diff --git a/src/nunit.analyzers/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringAnalyzer.cs b/src/nunit.analyzers/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringAnalyzer.cs index a1f6371f..093ce1a1 100644 --- a/src/nunit.analyzers/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringAnalyzer.cs +++ b/src/nunit.analyzers/UpdateStringFormatToInterpolatableString/UpdateStringFormatToInterpolatableStringAnalyzer.cs @@ -107,7 +107,7 @@ private static void AnalyzeNUnit4AssertInvocation(OperationAnalysisContext conte IParameterSymbol parameter = parameters[formatParameterIndex]; if (parameter.IsOptional) { - if (parameter.Name == "message") + if (parameter.Name == NUnitFrameworkConstants.NameOfMessageParameter) break; // Overload with FormattableString or Func overload From f1005770e52faa6356c120ff046ffa7a196f5d84 Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Fri, 5 Apr 2024 16:21:51 -0400 Subject: [PATCH 04/17] Refactoring --- .../AreEqualClassicModelAssertUsageCodeFix.cs | 4 +- .../ClassicModelAssertUsageCodeFix.cs | 61 +++++++++++-------- .../Constants/NUnitFrameworkConstants.cs | 1 + src/nunit.analyzers/Helpers/CodeFixHelper.cs | 17 +++--- 4 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs index 656ca8a2..43a3f668 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs @@ -34,12 +34,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgu SyntaxFactory.SingletonSeparatedList(expectedArgument))); const string NameOfDeltaParameter = "delta"; - var hasToleranceValue = diagnostic.Properties[AnalyzerPropertyKeys.HasToleranceValue] == true.ToString(); - if (hasToleranceValue) + if (argumentNamesToArguments.TryGetValue(NameOfDeltaParameter, out var toleranceArgument)) { // The tolerance argument should be renamed from 'delta' to 'amount' but with the model constraint the // argument is moved to Within which makes it way more explicit so we can just drop the name colon. - var toleranceArgument = argumentNamesToArguments[NameOfDeltaParameter]; var toleranceArgumentNoColon = toleranceArgument.WithNameColon(null); equalToInvocationNode = SyntaxFactory.InvocationExpression( diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs index fcf3ec36..7cfb09c2 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs @@ -56,33 +56,11 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(invocationNode).Symbol!; - Dictionary argumentNamesToArguments = new(); - const string NameOfArgsParameter = "args"; - - // There can be 0 to any number of message and parameters. - List messageAndParams = new(); + var (argumentNamesToArguments, args) = SplitUpNamedParametersAndArgs(); var arguments = invocationNode.ArgumentList.Arguments.ToList(); - for (var i = 0; i < arguments.Count; ++i) - { - var argument = arguments[i]; - if (i < methodSymbol.Parameters.Length - && (argument.NameColon?.Name.Identifier.Text ?? methodSymbol.Parameters[i].Name) is { } argumentName - && argumentName is not (NUnitFrameworkConstants.NameOfMessageParameter or NameOfArgsParameter)) - { - // See if we need to cast the arguments when they were using a specific classic overload. - argumentNamesToArguments[argumentName] = - argumentName is NUnitFrameworkConstants.NameOfExpectedParameter or NUnitFrameworkConstants.NameOfActualParameter - ? CastIfNecessary(argument) - : argument; - } - else - { - messageAndParams.Add(argument); - } - } // Remove null message to avoid ambiguous calls. - if (argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfMessageParameter, out var messageArgument) + if (argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfMessageParameter, out ArgumentSyntax? messageArgument) && messageArgument.Expression.IsKind(SyntaxKind.NullLiteralExpression)) { argumentNamesToArguments.Remove(NUnitFrameworkConstants.NameOfMessageParameter); @@ -90,15 +68,14 @@ argumentName is NUnitFrameworkConstants.NameOfExpectedParameter or NUnitFramewor // Now, replace the arguments. List newArguments = new(); - ArgumentSyntax actualArgument, constraintArgument; // Do the rule specific conversion if (typeArguments is null) { - (actualArgument, constraintArgument) = this.UpdateArguments(diagnostic, argumentNamesToArguments); + var (actualArgument, constraintArgument) = this.UpdateArguments(diagnostic, argumentNamesToArguments); newArguments.Add(actualArgument); newArguments.Add(constraintArgument); - if (CodeFixHelper.GetInterpolatedMessageArgumentOrDefault(messageAndParams) is { } interpolatedMessageArgument) + if (CodeFixHelper.GetInterpolatedMessageArgumentOrDefault(messageArgument, args) is ArgumentSyntax interpolatedMessageArgument) { newArguments.Add(interpolatedMessageArgument); } @@ -125,6 +102,36 @@ argumentName is NUnitFrameworkConstants.NameOfExpectedParameter or NUnitFramewor _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)), this.Title), diagnostic); + (Dictionary argumentNamesToArguments, List args) SplitUpNamedParametersAndArgs() + { + Dictionary argumentNamesToArguments = new(); + + // There can be 0 to any number of arguments mapped to args. + List args = new(); + + var arguments = invocationNode.ArgumentList.Arguments.ToList(); + for (var i = 0; i < arguments.Count; ++i) + { + var argument = arguments[i]; + if (i < methodSymbol.Parameters.Length + && (argument.NameColon?.Name.Identifier.Text ?? methodSymbol.Parameters[i].Name) is string argumentName + && argumentName != NUnitFrameworkConstants.NameOfArgsParameter) + { + // See if we need to cast the arguments when they were using a specific classic overload. + argumentNamesToArguments[argumentName] = + argumentName is NUnitFrameworkConstants.NameOfExpectedParameter or NUnitFrameworkConstants.NameOfActualParameter + ? CastIfNecessary(argument) + : argument; + } + else + { + args.Add(argument); + } + } + + return (argumentNamesToArguments, args); + } + ArgumentSyntax CastIfNecessary(ArgumentSyntax argument) { string? implicitTypeConversion = GetUserDefinedImplicitTypeConversion(argument.Expression); diff --git a/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs b/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs index 57dfa23c..04f8ae4a 100644 --- a/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs +++ b/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs @@ -193,6 +193,7 @@ public static class NUnitFrameworkConstants public const string NameOfExpectedParameter = "expected"; public const string NameOfExpressionParameter = "expression"; public const string NameOfMessageParameter = "message"; + public const string NameOfArgsParameter = "args"; public const string NameOfConstraintExpressionAnd = "And"; public const string NameOfConstraintExpressionOr = "Or"; diff --git a/src/nunit.analyzers/Helpers/CodeFixHelper.cs b/src/nunit.analyzers/Helpers/CodeFixHelper.cs index c20b2af2..3c256c42 100644 --- a/src/nunit.analyzers/Helpers/CodeFixHelper.cs +++ b/src/nunit.analyzers/Helpers/CodeFixHelper.cs @@ -41,27 +41,24 @@ internal static class CodeFixHelper /// /// The arguments passed to the 'Assert' method. /// The argument needed for the actual method, any more are assumed messages. - public static ArgumentSyntax? GetInterpolatedMessageArgumentOrDefault(List messageAndParams) + public static ArgumentSyntax? GetInterpolatedMessageArgumentOrDefault(ArgumentSyntax? messageArgument, List args) { - if (messageAndParams.Count == 0) + if (messageArgument is null) return null; - var messageArgument = messageAndParams.SingleOrDefault(a => a.NameColon?.Name.Identifier.Text == NUnitFrameworkConstants.NameOfMessageParameter) - ?? messageAndParams.First(); var formatSpecificationArgument = messageArgument.Expression; if (formatSpecificationArgument.IsKind(SyntaxKind.NullLiteralExpression)) return null; // We only support converting if the format specification is a constant string. - if (messageAndParams.Count == 1 || formatSpecificationArgument is not LiteralExpressionSyntax literalExpression) - return messageAndParams[0]; + if (args.Count == 0 || formatSpecificationArgument is not LiteralExpressionSyntax literalExpression) + return messageArgument; var formatSpecification = literalExpression.Token.ValueText; - // var formatArgumentExpressions = new List(capacity: messageAndParams.Count - 1); - var argsExpression = messageAndParams.Single(a => a != messageArgument).Expression; - var formatArgumentExpressions = argsExpression is ImplicitArrayCreationExpressionSyntax args - ? args.Initializer.Expressions.ToArray() + var argsExpression = args.Single(a => a != messageArgument).Expression; + var formatArgumentExpressions = argsExpression is ImplicitArrayCreationExpressionSyntax argsArrayExpression + ? argsArrayExpression.Initializer.Expressions.ToArray() : new[] { argsExpression }; var interpolatedStringContent = UpdateStringFormatToFormattableString(formatSpecification, formatArgumentExpressions); From 62f46d145e7939c77aa256d6b125184c3dfea084 Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Sat, 6 Apr 2024 15:28:42 -0400 Subject: [PATCH 05/17] Handle out-of-order arguments in all ClassicModelAssertUsageCodeFixes --- ...qualClassicModelAssertUsageCodeFixTests.cs | 2 +- .../ClassicModelAssertUsageCodeFixTests.cs | 7 +- .../CollectionAssertUsageCodeFixTests.cs | 17 +-- .../AreEqualClassicModelAssertUsageCodeFix.cs | 11 +- ...eNotEqualClassicModelAssertUsageCodeFix.cs | 13 +- ...reNotSameClassicModelAssertUsageCodeFix.cs | 13 +- .../AreSameClassicModelAssertUsageCodeFix.cs | 13 +- .../ClassicModelAssertUsageCodeFix.cs | 75 +++++------ .../ContainsClassicModelAssertUsageCodeFix.cs | 13 +- .../GreaterClassicModelAssertUsageCodeFix.cs | 13 +- ...erOrEqualClassicModelAssertUsageCodeFix.cs | 13 +- .../IsEmptyClassicModelAssertUsageCodeFix.cs | 13 +- ...eAndFalseClassicModelAssertUsageCodeFix.cs | 10 +- ...nstanceOfClassicModelAssertUsageCodeFix.cs | 24 ++-- .../IsNaNClassicModelAssertUsageCodeFix.cs | 10 +- ...sNotEmptyClassicModelAssertUsageCodeFix.cs | 12 +- ...nstanceOfClassicModelAssertUsageCodeFix.cs | 24 ++-- ...ndNotNullClassicModelAssertUsageCodeFix.cs | 10 +- ...llAndNullClassicModelAssertUsageCodeFix.cs | 10 +- ...ueAndTrueClassicModelAssertUsageCodeFix.cs | 10 +- ...ClassicModelAssertUsageCondensedCodeFix.cs | 7 +- .../LessClassicModelAssertUsageCodeFix.cs | 13 +- ...ssOrEqualClassicModelAssertUsageCodeFix.cs | 13 +- .../NotZeroClassicModelAssertUsageCodeFix.cs | 10 +- .../ZeroClassicModelAssertUsageCodeFix.cs | 10 +- .../CollectionAssertUsageCodeFix.cs | 116 ++++++++++++------ .../Constants/NUnitFrameworkConstants.cs | 14 ++- .../StringAssertUsageCodeFix.cs | 33 +++-- 28 files changed, 332 insertions(+), 197 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs index c224c837..66fef9c4 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs @@ -134,7 +134,7 @@ public void TestMethod() { var actual = 3d; Assert.That(actual: actual, Is.EqualTo(expected: 2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid().ToString() }""); - }"); // TODO: Fix whitespace + }"); // TODO: Fix trailing trivia if possible RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFixTests.cs index 17c47c31..d756df5a 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFixTests.cs @@ -22,9 +22,10 @@ private sealed class TestableClassicModelAssertUsageCodeFix : ClassicModelAssert { public override ImmutableArray FixableDiagnosticIds => ImmutableArray.Empty; - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) - { - } + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) => + (default!, default); } } } diff --git a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs index b0cd749a..b0fd9b7f 100644 --- a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs @@ -196,19 +196,10 @@ public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsed(string m private static string GetAdjustedTwoCollectionConstraint(string method) { - string actualArgument; - string constraintArgument; - - if (CollectionAssertUsageCodeFix.CollectionAssertToOneUnswappedParameterConstraints.ContainsKey(method)) - { - actualArgument = "collection1"; - constraintArgument = "collection2"; - } - else - { - actualArgument = "collection2"; - constraintArgument = "collection1"; - } + (string actualArgument, string constraintArgument) = + CollectionAssertUsageCodeFix.CollectionAssertToOneUnswappedParameterConstraints.ContainsKey(method) + ? ("collection1", "collection2") + : ("collection2", "collection1"); string constraint = CollectionAssertUsageAnalyzer.TwoCollectionParameterAsserts[method] .Replace("expected", constraintArgument); diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs index 43a3f668..44595226 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs @@ -16,15 +16,11 @@ public sealed class AreEqualClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.AreEqualUsage); - protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgument) UpdateArguments( + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; - - // Note that if there's a 3rd argument and it's a double, - // it has to be added to the "Is.EqualTo(1st argument)" with ".Within(3rd argument)" var equalToInvocationNode = SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -33,8 +29,8 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgu .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList(expectedArgument))); - const string NameOfDeltaParameter = "delta"; - if (argumentNamesToArguments.TryGetValue(NameOfDeltaParameter, out var toleranceArgument)) + // The tolerance argument has to be added to the "Is.EqualTo(expected)" as ".Within(tolerance)" + if (argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfDeltaParameter, out var toleranceArgument)) { // The tolerance argument should be renamed from 'delta' to 'amount' but with the model constraint the // argument is moved to Within which makes it way more explicit so we can just drop the name colon. @@ -49,6 +45,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgu SyntaxFactory.SingletonSeparatedList(toleranceArgumentNoColon))); } + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; return (actualArgument, SyntaxFactory.Argument(equalToInvocationNode)); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFix.cs index 34e1d63d..9b5985dd 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFix.cs @@ -16,9 +16,12 @@ public sealed class AreNotEqualClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.AreNotEqualUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -28,10 +31,10 @@ protected override void UpdateArguments(Diagnostic diagnostic, List FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.AreNotSameUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -28,10 +31,10 @@ protected override void UpdateArguments(Diagnostic diagnostic, List FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.AreSameUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsSameAs))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arguments[0]))))); + SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - // Then we have to remove the 1st argument because that's now in the "Is.SameAs()" - arguments.RemoveAt(0); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs index 7cfb09c2..08bf3c67 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs @@ -42,22 +42,19 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) var diagnostic = context.Diagnostics.First(); var node = root.FindNode(diagnostic.Location.SourceSpan); var invocationNode = node as InvocationExpressionSyntax; - if (invocationNode is null) return; var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); + if (semanticModel is null) + return; // Replace the original ClassicAssert. invocation name into Assert.That var newInvocationNode = invocationNode.UpdateClassicAssertToAssertThat(out TypeArgumentListSyntax? typeArguments); - if (newInvocationNode is null) return; - var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(invocationNode).Symbol!; - - var (argumentNamesToArguments, args) = SplitUpNamedParametersAndArgs(); - var arguments = invocationNode.ArgumentList.Arguments.ToList(); + var (argumentNamesToArguments, args) = SplitUpOtherParametersAndParamParameter(semanticModel, invocationNode); // Remove null message to avoid ambiguous calls. if (argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfMessageParameter, out ArgumentSyntax? messageArgument) @@ -70,24 +67,16 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) List newArguments = new(); // Do the rule specific conversion - if (typeArguments is null) - { - var (actualArgument, constraintArgument) = this.UpdateArguments(diagnostic, argumentNamesToArguments); - newArguments.Add(actualArgument); + var (actualArgument, constraintArgument) = typeArguments is null + ? this.ConstructActualAndConstraintArguments(diagnostic, argumentNamesToArguments) + : this.ConstructActualAndConstraintArguments(diagnostic, argumentNamesToArguments, typeArguments); + newArguments.Add(actualArgument); + if (constraintArgument is not null) newArguments.Add(constraintArgument); - if (CodeFixHelper.GetInterpolatedMessageArgumentOrDefault(messageArgument, args) is ArgumentSyntax interpolatedMessageArgument) - { - newArguments.Add(interpolatedMessageArgument); - } - } - else - { - this.UpdateArguments(diagnostic, arguments, typeArguments); - // Do the format spec, params to formattable string conversion - CodeFixHelper.UpdateStringFormatToFormattableString(arguments, this.MinimumNumberOfParameters); - newArguments = arguments; - } + // Do the format spec, params to formattable string conversion + if (CodeFixHelper.GetInterpolatedMessageArgumentOrDefault(messageArgument, args) is ArgumentSyntax interpolatedMessageArgument) + newArguments.Add(interpolatedMessageArgument); var newArgumentsList = invocationNode.ArgumentList.WithArguments(newArguments); newInvocationNode = newInvocationNode.WithArgumentList(newArgumentsList); @@ -100,10 +89,14 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) CodeAction.Create( this.Title, _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)), - this.Title), diagnostic); + this.Title), + diagnostic); - (Dictionary argumentNamesToArguments, List args) SplitUpNamedParametersAndArgs() + (Dictionary argumentNamesToArguments, List args) SplitUpOtherParametersAndParamParameter( + SemanticModel semanticModel, + InvocationExpressionSyntax invocationNode) { + var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(invocationNode).Symbol!; Dictionary argumentNamesToArguments = new(); // There can be 0 to any number of arguments mapped to args. @@ -118,10 +111,9 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) && argumentName != NUnitFrameworkConstants.NameOfArgsParameter) { // See if we need to cast the arguments when they were using a specific classic overload. - argumentNamesToArguments[argumentName] = - argumentName is NUnitFrameworkConstants.NameOfExpectedParameter or NUnitFrameworkConstants.NameOfActualParameter - ? CastIfNecessary(argument) - : argument; + argumentNamesToArguments[argumentName] = argumentName is NUnitFrameworkConstants.NameOfMessageParameter + ? argument + : CastIfNecessary(argument); } else { @@ -134,7 +126,7 @@ argumentName is NUnitFrameworkConstants.NameOfExpectedParameter or NUnitFramewor ArgumentSyntax CastIfNecessary(ArgumentSyntax argument) { - string? implicitTypeConversion = GetUserDefinedImplicitTypeConversion(argument.Expression); + string? implicitTypeConversion = GetUserDefinedImplicitTypeConversionOrDefault(argument.Expression); if (implicitTypeConversion is null) return argument; @@ -145,7 +137,7 @@ ArgumentSyntax CastIfNecessary(ArgumentSyntax argument) argument.Expression)); } - string? GetUserDefinedImplicitTypeConversion(ExpressionSyntax expression) + string? GetUserDefinedImplicitTypeConversionOrDefault(ExpressionSyntax expression) { var typeInfo = semanticModel.GetTypeInfo(expression, context.CancellationToken); var convertedType = typeInfo.ConvertedType; @@ -165,21 +157,16 @@ ArgumentSyntax CastIfNecessary(ArgumentSyntax argument) } } - protected virtual (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgument) UpdateArguments( + // ConstraintArgument is nullable because Assert.True and Assert.IsTrue are transformed into Assert.That without a constraint. + protected virtual (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( Diagnostic diagnostic, - IReadOnlyDictionary argumentNamesToArguments) - { - throw new NotImplementedException($"Class must override {nameof(UpdateArguments)}"); - } + IReadOnlyDictionary argumentNamesToArguments) => + throw new NotImplementedException($"Class must override {nameof(ConstructActualAndConstraintArguments)}"); - protected virtual void UpdateArguments(Diagnostic diagnostic, List arguments, TypeArgumentListSyntax typeArguments) - { - throw new InvalidOperationException($"Class must override {nameof(UpdateArguments)} accepting {nameof(TypeArgumentListSyntax)}"); - } - - protected virtual void UpdateArguments(Diagnostic diagnostic, List arguments) - { - throw new InvalidOperationException($"Class must override {nameof(UpdateArguments)}"); - } + protected virtual (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments, + TypeArgumentListSyntax typeArguments) => + throw new InvalidOperationException($"Class must override {nameof(ConstructActualAndConstraintArguments)} accepting {nameof(TypeArgumentListSyntax)}"); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFix.cs index c0a2f24d..b6fc8acf 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFix.cs @@ -16,19 +16,22 @@ public sealed class ContainsClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.ContainsUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfDoes), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfDoesContain))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arguments[0]))))); + SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - // Then we have to remove the 1st argument because that's now in the "Does.Contain()" - arguments.RemoveAt(0); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs index 86598365..2121d051 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs @@ -16,19 +16,22 @@ public sealed class GreaterClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.GreaterUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsGreaterThan))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arguments[1]))))); + SyntaxFactory.SingletonSeparatedList(arg2)))); - // Then we have to remove the 2nd argument because that's now in the "Is.GreaterThan()" - arguments.RemoveAt(1); + var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; + return (arg1, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs index d6dc6a44..a1cff0f2 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs @@ -16,19 +16,22 @@ public sealed class GreaterOrEqualClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.GreaterOrEqualUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsGreaterThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arguments[1]))))); + SyntaxFactory.SingletonSeparatedList(arg2)))); - // Then we have to remove the 2nd argument because that's now in the "Is.GreaterThanOrEqualTo()" - arguments.RemoveAt(1); + var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; + return (arg1, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs index 4ab7f095..69b0ccda 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs @@ -17,13 +17,20 @@ public sealed class IsEmptyClassicModelAssertUsageCodeFix public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( AnalyzerIdentifiers.IsEmptyUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + // different overloads have different "actual" arguments + var actualArgument = argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfCollectionParameter, out var collectionArgument) + ? collectionArgument + : argumentNamesToArguments[NUnitFrameworkConstants.NameOfAStringParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs index 5cf2113d..263c8165 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs @@ -18,13 +18,17 @@ public sealed class IsFalseAndFalseClassicModelAssertUsageCodeFix AnalyzerIdentifiers.IsFalseUsage, AnalyzerIdentifiers.FalseUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsFalse)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsFalse))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs index 66d1e857..d2415ec6 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs @@ -16,30 +16,38 @@ public sealed class IsInstanceOfClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.IsInstanceOfUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments, TypeArgumentListSyntax typeArguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments, + TypeArgumentListSyntax typeArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.GenericName(NUnitFrameworkConstants.NameOfIsInstanceOf) - .WithTypeArgumentList(typeArguments))))); + .WithTypeArgumentList(typeArguments)))); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + return (actualArgument, constraintArgument); } - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsInstanceOf))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arguments[0]))))); + SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - // Then we have to remove the 1st argument because that's now in the "Is.InstanceOf()" - arguments.RemoveAt(0); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs index 5144ae7d..8498336c 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs @@ -17,13 +17,17 @@ public sealed class IsNaNClassicModelAssertUsageCodeFix public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( AnalyzerIdentifiers.IsNaNUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfADoubleParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNaN)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNaN))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs index edfb8cc2..fe31976c 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs @@ -17,16 +17,22 @@ public sealed class IsNotEmptyClassicModelAssertUsageCodeFix public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( AnalyzerIdentifiers.IsNotEmptyUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var actualArgument = argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfCollectionParameter, out var collectionArgument) + ? collectionArgument + : argumentNamesToArguments[NUnitFrameworkConstants.NameOfAStringParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs index d6e05367..a1f77439 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs @@ -16,9 +16,12 @@ public sealed class IsNotInstanceOfClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.IsNotInstanceOfUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments, TypeArgumentListSyntax typeArguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments, + TypeArgumentListSyntax typeArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -27,12 +30,17 @@ protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -42,10 +50,10 @@ protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs index 0df23b15..b3a22285 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs @@ -18,13 +18,17 @@ public sealed class IsNullAndNullClassicModelAssertUsageCodeFix AnalyzerIdentifiers.IsNullUsage, AnalyzerIdentifiers.NullUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs index 1bf9e10f..70ec01f2 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs @@ -18,13 +18,17 @@ public sealed class IsTrueAndTrueClassicModelAssertUsageCodeFix AnalyzerIdentifiers.IsTrueUsage, AnalyzerIdentifiers.TrueUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsTrue)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsTrue))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs index e37f7f9c..71ba76a4 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs @@ -23,9 +23,12 @@ public sealed class IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix protected override string Title => base.Title + Suffix; - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - // Nothing to do + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; + return (actualArgument, null); // The condensed form doesn't have the constraint argument. } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs index 70ae561d..ebba1422 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs @@ -16,19 +16,22 @@ public sealed class LessClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.LessUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThan))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arguments[1]))))); + SyntaxFactory.SingletonSeparatedList(arg2)))); - // Then we have to remove the 2nd argument because that's now in the "Is.LessThan()" - arguments.RemoveAt(1); + var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; + return (arg1, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs index ca3f85e7..aba711e6 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs @@ -16,19 +16,22 @@ public sealed class LessOrEqualClassicModelAssertUsageCodeFix { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(AnalyzerIdentifiers.LessOrEqualUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(2, SyntaxFactory.Argument( + var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arguments[1]))))); + SyntaxFactory.SingletonSeparatedList(arg2)))); - // Then we have to remove the 2nd argument because that's now in the "Is.LessThanOrEqualTo()" - arguments.RemoveAt(1); + var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; + return (arg1, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFix.cs index 0069865b..97562884 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFix.cs @@ -17,16 +17,20 @@ public sealed class NotZeroClassicModelAssertUsageCodeFix public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( AnalyzerIdentifiers.NotZeroUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsZero)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsZero))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFix.cs index cb4e7591..158006b3 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFix.cs @@ -17,13 +17,17 @@ public sealed class ZeroClassicModelAssertUsageCodeFix public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( AnalyzerIdentifiers.ZeroUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - arguments.Insert(1, SyntaxFactory.Argument( + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), - SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsZero)))); + SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsZero))); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs b/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs index cd932db5..e04c4e66 100644 --- a/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Composition; @@ -48,47 +49,55 @@ internal class CollectionAssertUsageCodeFix : ClassicModelAssertUsageCodeFix { NameOfCollectionAssertIsSupersetOf, new Constraints(NameOfIs, default(string), NameOfIsSupersetOf) }, }.ToImmutableDictionary(); - public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( - AnalyzerIdentifiers.CollectionAssertUsage); - - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) - { - string methodName = diagnostic.Properties[AnalyzerPropertyKeys.ModelName]!; - - int comparerParameterIndex = diagnostic.Properties[AnalyzerPropertyKeys.ComparerParameterIndex] switch + private static readonly ImmutableDictionary CollectionAssertToFirstParameterName = + new Dictionary() { - "2" => 2, - "1" => 1, - _ => 0, - }; + { NameOfCollectionAssertAllItemsAreNotNull, NameOfCollectionParameter }, + { NameOfCollectionAssertAllItemsAreUnique, NameOfCollectionParameter }, + { NameOfCollectionAssertIsEmpty, NameOfCollectionParameter }, + { NameOfCollectionAssertIsNotEmpty, NameOfCollectionParameter }, + { NameOfCollectionAssertIsOrdered, NameOfCollectionParameter }, + { NameOfCollectionAssertAreEqual, NameOfExpectedParameter }, + { NameOfCollectionAssertAreEquivalent, NameOfExpectedParameter }, + { NameOfCollectionAssertAreNotEqual, NameOfExpectedParameter }, + { NameOfCollectionAssertAreNotEquivalent, NameOfExpectedParameter }, + { NameOfCollectionAssertAllItemsAreInstancesOfType, NameOfCollectionParameter }, + { NameOfCollectionAssertContains, NameOfCollectionParameter }, + { NameOfCollectionAssertDoesNotContain, NameOfCollectionParameter }, + { NameOfCollectionAssertIsNotSubsetOf, NameOfSubsetParameter }, + { NameOfCollectionAssertIsSubsetOf, NameOfSubsetParameter }, + { NameOfCollectionAssertIsNotSupersetOf, NameOfSupersetParameter }, + { NameOfCollectionAssertIsSupersetOf, NameOfSupersetParameter }, + }.ToImmutableDictionary(); - ArgumentSyntax? comparerArgument = null; - if (comparerParameterIndex > 0) + private static readonly ImmutableDictionary CollectionAssertToSecondParameterName = + new Dictionary() { - // Remember 'comparer' parameter to be added as an 'Using' suffix. - comparerArgument = arguments[comparerParameterIndex]; - arguments.RemoveAt(comparerParameterIndex); - } + { NameOfCollectionAssertAreEqual, NameOfActualParameter }, + { NameOfCollectionAssertAreEquivalent, NameOfActualParameter }, + { NameOfCollectionAssertAreNotEqual, NameOfActualParameter }, + { NameOfCollectionAssertAreNotEquivalent, NameOfActualParameter }, + { NameOfCollectionAssertAllItemsAreInstancesOfType, NameOfExpectedTypeParameter }, + { NameOfCollectionAssertContains, NameOfActualParameter }, + { NameOfCollectionAssertDoesNotContain, NameOfActualParameter }, + { NameOfCollectionAssertIsNotSubsetOf, NameOfSupersetParameter }, + { NameOfCollectionAssertIsSubsetOf, NameOfSupersetParameter }, + { NameOfCollectionAssertIsNotSupersetOf, NameOfSubsetParameter }, + { NameOfCollectionAssertIsSupersetOf, NameOfSubsetParameter }, + }.ToImmutableDictionary(); - if (CollectionAssertToParameterlessConstraints.TryGetValue(methodName, out Constraints? constraints)) - { - arguments.Insert(1, Argument(constraints.CreateConstraint())); - } - else if (CollectionAssertToOneSwappedParameterConstraints.TryGetValue(methodName, out constraints)) - { - arguments.Insert(2, Argument(constraints.CreateConstraint(arguments[0]))); + public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( + AnalyzerIdentifiers.CollectionAssertUsage); - // Then we have to remove the 1st argument because that's now in the "constaint" - arguments.RemoveAt(0); - } - else if (CollectionAssertToOneUnswappedParameterConstraints.TryGetValue(methodName, out constraints)) - { - arguments[1] = Argument(constraints.CreateConstraint(arguments[1])); - } + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) + { + var (actualArgument, constraintArgument) = GetActualAndConstraintArguments(diagnostic, argumentNamesToArguments); - if (comparerArgument is not null) + if (argumentNamesToArguments.TryGetValue(NameOfComparerParameter, out ArgumentSyntax? comparerArgument)) { - ExpressionSyntax expression = arguments[1].Expression; + ExpressionSyntax expression = constraintArgument.Expression; // We need to drop the 'AsCollection' when using an IComparer. if (expression is MemberAccessExpressionSyntax memberAccessExpression && @@ -97,7 +106,7 @@ protected override void UpdateArguments(Diagnostic diagnostic, List argumentNamesToArguments) + { + var methodName = diagnostic.Properties[AnalyzerPropertyKeys.ModelName]!; + var firstParameterName = CollectionAssertToFirstParameterName[methodName]; + var firstArgument = argumentNamesToArguments[firstParameterName]; + + if (CollectionAssertToParameterlessConstraints.TryGetValue(methodName, out Constraints? constraints)) + { + var constraintArgument = Argument(constraints.CreateConstraint()); + return (firstArgument, constraintArgument); + } + else if (CollectionAssertToOneSwappedParameterConstraints.TryGetValue(methodName, out constraints)) + { + var secondParameterName = CollectionAssertToSecondParameterName[methodName]; + var secondArgument = argumentNamesToArguments[secondParameterName]; + + var constraintArgument = Argument(constraints.CreateConstraint(firstArgument)); + return (secondArgument, constraintArgument); + } + else if (CollectionAssertToOneUnswappedParameterConstraints.TryGetValue(methodName, out constraints)) + { + var secondParameterName = CollectionAssertToSecondParameterName[methodName]; + var secondArgument = argumentNamesToArguments[secondParameterName]; + var constraintArgument = Argument(constraints.CreateConstraint(secondArgument)); + + return (firstArgument, constraintArgument); + } + else + { + throw new InvalidOperationException($"Unknown method name: {methodName}"); + } } } } diff --git a/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs b/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs index 04f8ae4a..300763e9 100644 --- a/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs +++ b/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs @@ -189,11 +189,23 @@ public static class NUnitFrameworkConstants public const string NameOfExpectedResult = "ExpectedResult"; public const string NameOfActualParameter = "actual"; + public const string NameOfADoubleParameter = "aDouble"; + public const string NameOfAnObjectParameter = "anObject"; + public const string NameOfArg1Parameter = "arg1"; + public const string NameOfArg2Parameter = "arg2"; + public const string NameOfArgsParameter = "args"; + public const string NameOfAStringParameter = "aString"; + public const string NameOfCollectionParameter = "collection"; + public const string NameOfComparerParameter = "comparer"; public const string NameOfConditionParameter = "condition"; + public const string NameOfDeltaParameter = "delta"; public const string NameOfExpectedParameter = "expected"; + public const string NameOfExpectedTypeParameter = "expectedType"; public const string NameOfExpressionParameter = "expression"; public const string NameOfMessageParameter = "message"; - public const string NameOfArgsParameter = "args"; + public const string NameOfPatternParameter = "pattern"; + public const string NameOfSubsetParameter = "subset"; + public const string NameOfSupersetParameter = "superset"; public const string NameOfConstraintExpressionAnd = "And"; public const string NameOfConstraintExpressionOr = "Or"; diff --git a/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs b/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs index f9da94a4..a0898fb9 100644 --- a/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs @@ -31,19 +31,36 @@ internal class StringAssertUsageCodeFix : ClassicModelAssertUsageCodeFix { NameOfStringAssertDoesNotMatch, new Constraints(NameOfDoes, NameOfDoesNot, NameOfDoesMatch) }, }.ToImmutableDictionary(); + private static readonly ImmutableDictionary StringAssertToExpectedParameterName = + new Dictionary() + { + { NameOfStringAssertContains, NameOfExpectedParameter }, + { NameOfStringAssertDoesNotContain, NameOfExpectedParameter }, + { NameOfStringAssertStartsWith, NameOfExpectedParameter }, + { NameOfStringAssertDoesNotStartWith, NameOfExpectedParameter }, + { NameOfStringAssertEndsWith, NameOfExpectedParameter }, + { NameOfStringAssertDoesNotEndWith, NameOfExpectedParameter }, + { NameOfStringAssertAreEqualIgnoringCase, NameOfExpectedParameter }, + { NameOfStringAssertAreNotEqualIgnoringCase, NameOfExpectedParameter }, + { NameOfStringAssertIsMatch, NameOfPatternParameter }, + { NameOfStringAssertDoesNotMatch, NameOfPatternParameter }, + }.ToImmutableDictionary(); + public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( AnalyzerIdentifiers.StringAssertUsage); - protected override void UpdateArguments(Diagnostic diagnostic, List arguments) + protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( + Diagnostic diagnostic, + IReadOnlyDictionary argumentNamesToArguments) { - string methodName = diagnostic.Properties[AnalyzerPropertyKeys.ModelName]!; - if (StringAssertToConstraints.TryGetValue(methodName, out Constraints? constraints)) - { - arguments.Insert(2, Argument(constraints.CreateConstraint(arguments[0]))); + var methodName = diagnostic.Properties[AnalyzerPropertyKeys.ModelName]!; + var expectedParameterName = StringAssertToExpectedParameterName[methodName]; + var expectedArgument = argumentNamesToArguments[expectedParameterName]; + var constraints = StringAssertToConstraints[methodName]; + var constraintArgument = Argument(constraints.CreateConstraint(expectedArgument)); - // Then we have to remove the 1st argument because that's now in the "constaint" - arguments.RemoveAt(0); - } + var actualArgument = argumentNamesToArguments[NameOfActualParameter]; + return (actualArgument, constraintArgument); } } } From e0376ae9656720d43ac826bd205de847bccfa70c Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Sat, 6 Apr 2024 17:37:26 -0400 Subject: [PATCH 06/17] Add out-of-order test cases to CollectionAssertUsageCodeFixTests --- .../CollectionAssertUsageCodeFixTests.cs | 49 +++++++++++++++++++ .../CollectionAssertUsageCodeFix.cs | 17 ++++--- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs index b0fd9b7f..5838ce5a 100644 --- a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs @@ -63,6 +63,20 @@ public void AnalyzeOneCollectionWhenFormatAndParamsArgumentsAreUsed(string metho RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } + [TestCaseSource(nameof(OneCollectionParameterAsserts))] + public void AnalyzeOneCollectionWhenFormatAndParamsArgumentsAreUsedOutOfOrder(string method) + { + var code = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ 1, 2, 3 }}; + ↓CollectionAssert.{method}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", collection: collection); + "); + var fixedCode = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ 1, 2, 3 }}; + Assert.That(collection, {CollectionAssertUsageAnalyzer.OneCollectionParameterAsserts[method]}, $""{{""first""}}, {{""second"" }}""); + "); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } + [TestCase(NUnitFrameworkConstants.NameOfCollectionAssertIsOrdered)] public void AnalyzeOneCollectionWithComparerWhenFormatAndParamsArgumentsAreUsed(string method) { @@ -127,6 +141,24 @@ public void AnalyzeTwoCollectionWhenFormatAndParamsArgumentsAreUsed(string metho RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } + [TestCaseSource(nameof(TwoCollectionParameterAsserts))] + public void AnalyzeTwoCollectionWhenFormatAndParamsArgumentsAreUsedOutOfOrder(string method) + { + var firstParameterName = CollectionAssertUsageCodeFix.CollectionAssertToFirstParameterName[method]; + var secondParameterName = CollectionAssertUsageCodeFix.CollectionAssertToSecondParameterName[method]; + var code = TestUtility.WrapInTestMethod(@$" + var collection1 = new[] {{ 1, 2, 3 }}; + var collection2 = new[] {{ 2, 4, 6 }}; + ↓CollectionAssert.{method}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", {secondParameterName}: collection2, {firstParameterName}: collection1); + "); + var fixedCode = TestUtility.WrapInTestMethod(@$" + var collection1 = new[] {{ 1, 2, 3 }}; + var collection2 = new[] {{ 2, 4, 6 }}; + Assert.That({GetAdjustedTwoCollectionConstraint(method)}, $""{{""first""}}, {{""second"" }}""); + "); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } + [TestCase(NUnitFrameworkConstants.NameOfCollectionAssertAreEqual)] [TestCase(NUnitFrameworkConstants.NameOfCollectionAssertAreNotEqual)] public void AnalyzeTwoCollectionWithComparerWhenFormatAndParamsArgumentsAreUsed(string method) @@ -194,6 +226,23 @@ public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsed(string m RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } + [TestCaseSource(nameof(CollectionAndItemParameterAsserts))] + public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsedOutOfOrder(string method) + { + var secondParameterName = CollectionAssertUsageCodeFix.CollectionAssertToSecondParameterName[method]; + var code = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ typeof(byte), typeof(char) }}; + var expected = typeof(byte); + ↓CollectionAssert.{method}({secondParameterName}: expected, args: new[] {{ ""first"", ""second"" }}, collection: collection, message: ""{{0}}, {{1}}""); + "); + var fixedCode = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ typeof(byte), typeof(char) }}; + var expected = typeof(byte); + Assert.That(collection, {CollectionAssertUsageAnalyzer.CollectionAndItemParameterAsserts[method]}, $""{{""first""}}, {{""second"" }}""); + "); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } + private static string GetAdjustedTwoCollectionConstraint(string method) { (string actualArgument, string constraintArgument) = diff --git a/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs b/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs index e04c4e66..13e7679f 100644 --- a/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs @@ -49,7 +49,7 @@ internal class CollectionAssertUsageCodeFix : ClassicModelAssertUsageCodeFix { NameOfCollectionAssertIsSupersetOf, new Constraints(NameOfIs, default(string), NameOfIsSupersetOf) }, }.ToImmutableDictionary(); - private static readonly ImmutableDictionary CollectionAssertToFirstParameterName = + internal static readonly ImmutableDictionary CollectionAssertToFirstParameterName = new Dictionary() { { NameOfCollectionAssertAllItemsAreNotNull, NameOfCollectionParameter }, @@ -70,7 +70,7 @@ internal class CollectionAssertUsageCodeFix : ClassicModelAssertUsageCodeFix { NameOfCollectionAssertIsSupersetOf, NameOfSupersetParameter }, }.ToImmutableDictionary(); - private static readonly ImmutableDictionary CollectionAssertToSecondParameterName = + internal static readonly ImmutableDictionary CollectionAssertToSecondParameterName = new Dictionary() { { NameOfCollectionAssertAreEqual, NameOfActualParameter }, @@ -128,24 +128,27 @@ private static (ArgumentSyntax actualArgument, ArgumentSyntax constraintArgument if (CollectionAssertToParameterlessConstraints.TryGetValue(methodName, out Constraints? constraints)) { + var actualArgument = firstArgument.WithNameColon(null); var constraintArgument = Argument(constraints.CreateConstraint()); - return (firstArgument, constraintArgument); + return (actualArgument, constraintArgument); } else if (CollectionAssertToOneSwappedParameterConstraints.TryGetValue(methodName, out constraints)) { var secondParameterName = CollectionAssertToSecondParameterName[methodName]; var secondArgument = argumentNamesToArguments[secondParameterName]; + var actualArgument = secondArgument.WithNameColon(null); - var constraintArgument = Argument(constraints.CreateConstraint(firstArgument)); - return (secondArgument, constraintArgument); + var constraintArgument = Argument(constraints.CreateConstraint(firstArgument.WithNameColon(null))); + return (actualArgument, constraintArgument); } else if (CollectionAssertToOneUnswappedParameterConstraints.TryGetValue(methodName, out constraints)) { var secondParameterName = CollectionAssertToSecondParameterName[methodName]; var secondArgument = argumentNamesToArguments[secondParameterName]; - var constraintArgument = Argument(constraints.CreateConstraint(secondArgument)); + var constraintArgument = Argument(constraints.CreateConstraint(secondArgument.WithNameColon(null))); - return (firstArgument, constraintArgument); + var actualArgument = firstArgument.WithNameColon(null); + return (actualArgument, constraintArgument); } else { From 2ea686012c251855a0d7bd0b4b6432e12c39a00f Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Sat, 6 Apr 2024 23:45:35 -0400 Subject: [PATCH 07/17] Update all other CodeFixTests, fixing CodeFixes to account for bugs found --- ...qualClassicModelAssertUsageCodeFixTests.cs | 16 ++++ ...SameClassicModelAssertUsageCodeFixTests.cs | 22 ++++++ ...SameClassicModelAssertUsageCodeFixTests.cs | 22 ++++++ ...ainsClassicModelAssertUsageCodeFixTests.cs | 22 ++++++ ...aterClassicModelAssertUsageCodeFixTests.cs | 74 +++++++++++++++--- ...qualClassicModelAssertUsageCodeFixTests.cs | 74 +++++++++++++++--- ...mptyClassicModelAssertUsageCodeFixTests.cs | 40 +++++++--- ...alseClassicModelAssertUsageCodeFixTests.cs | 19 +++++ ...ceOfClassicModelAssertUsageCodeFixTests.cs | 72 +++++++++++++----- ...sNaNClassicModelAssertUsageCodeFixTests.cs | 40 +++++++--- ...mptyClassicModelAssertUsageCodeFixTests.cs | 76 ++++++++++++++++--- ...ceOfClassicModelAssertUsageCodeFixTests.cs | 72 +++++++++++++----- ...NullClassicModelAssertUsageCodeFixTests.cs | 21 +++++ ...NullClassicModelAssertUsageCodeFixTests.cs | 21 +++++ ...TrueClassicModelAssertUsageCodeFixTests.cs | 58 ++++++++++++++ ...icModelAssertUsageCondensedCodeFixTests.cs | 20 +++++ ...LessClassicModelAssertUsageCodeFixTests.cs | 76 ++++++++++++++++--- ...qualClassicModelAssertUsageCodeFixTests.cs | 76 ++++++++++++++++--- ...ZeroClassicModelAssertUsageCodeFixTests.cs | 40 +++++++--- ...ZeroClassicModelAssertUsageCodeFixTests.cs | 40 +++++++--- .../CollectionAssertUsageCodeFixTests.cs | 2 +- .../StringAssertUsageCodeFixTests.cs | 24 ++++-- .../GreaterClassicModelAssertUsageCodeFix.cs | 12 ++- ...erOrEqualClassicModelAssertUsageCodeFix.cs | 12 ++- .../IsEmptyClassicModelAssertUsageCodeFix.cs | 6 +- ...eAndFalseClassicModelAssertUsageCodeFix.cs | 6 +- ...nstanceOfClassicModelAssertUsageCodeFix.cs | 12 ++- .../IsNaNClassicModelAssertUsageCodeFix.cs | 6 +- ...sNotEmptyClassicModelAssertUsageCodeFix.cs | 6 +- ...nstanceOfClassicModelAssertUsageCodeFix.cs | 11 ++- ...ndNotNullClassicModelAssertUsageCodeFix.cs | 6 +- ...llAndNullClassicModelAssertUsageCodeFix.cs | 6 +- ...ueAndTrueClassicModelAssertUsageCodeFix.cs | 6 +- .../LessClassicModelAssertUsageCodeFix.cs | 14 +++- ...ssOrEqualClassicModelAssertUsageCodeFix.cs | 14 +++- .../StringAssertUsageCodeFix.cs | 30 ++++---- 36 files changed, 904 insertions(+), 170 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs index b7d21257..33098298 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs @@ -71,5 +71,21 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyAreNotEqualFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.AreNotEqual(args: new[] { ""first"", ""second"" }, actual: ""actual"", message: ""{0}, {1}"", expected: ""expected""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: ""actual"", Is.Not.EqualTo(expected: ""expected""), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs index 62bf0e68..84a345af 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs @@ -89,5 +89,27 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyAreNotSameFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = new object(); + var actual = new object(); + + ↓ClassicAssert.AreNotSame(args: new[] { ""first"", ""second"" }, actual: actual, message: ""{0}, {1}"", expected: expected); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = new object(); + var actual = new object(); + + Assert.That(actual: actual, Is.Not.SameAs(expected: expected), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs index cd6b5a38..4f856df8 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs @@ -89,5 +89,27 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyAreSameFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = new object(); + var actual = new object(); + + ↓ClassicAssert.AreSame(args: new[] { ""first"", ""second"" }, actual: actual, message: ""{0}, {1}"", expected: expected); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = new object(); + var actual = new object(); + + Assert.That(actual: actual, Is.SameAs(expected: expected), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs index a491763f..a72171a1 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs @@ -89,5 +89,27 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, instanceDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyContainsFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var instance = new object(); + var collection = Array.Empty(); + + ↓ClassicAssert.Contains(args: new[] { ""first"", ""second"" }, actual: collection, message: ""{0}, {1}"", expected: instance); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var instance = new object(); + var collection = Array.Empty(); + + Assert.That(actual: collection, Does.Contain(expected: instance), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, instanceDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs index 75b3568b..631181a8 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs @@ -27,11 +27,11 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyGreaterFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.Greater(2d, 3d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -43,11 +43,11 @@ public void TestMethod() [Test] public void VerifyGreaterFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.Greater(2d, 3d, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -59,11 +59,11 @@ public void TestMethod() [Test] public void VerifyGreaterFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ - ↓ClassicAssert.Greater(2d, 3d, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + { + ↓ClassicAssert.Greater(2d, 3d, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -72,6 +72,22 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyGreaterFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.Greater(args: new[] { ""first"", ""second"" }, arg2: 3d, message: ""{0}, {1}"", arg1: 2d); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: 2d, Is.GreaterThan(expected: 3d), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void CodeFixPreservesLineBreakBeforeMessage() { @@ -161,5 +177,43 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyGreaterWithImplicitConversionFixInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyFloat + { + private readonly float _value; + + public MyFloat(float value) => _value = value; + + public static implicit operator float(MyFloat value) => value._value; + public static implicit operator MyFloat(float value) => new MyFloat(value); + } + public void TestMethod() + { + MyFloat x = 1; + MyFloat y = 2; + ↓ClassicAssert.Greater(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", arg2: y, arg1: x); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyFloat + { + private readonly float _value; + + public MyFloat(float value) => _value = value; + + public static implicit operator float(MyFloat value) => value._value; + public static implicit operator MyFloat(float value) => new MyFloat(value); + } + public void TestMethod() + { + MyFloat x = 1; + MyFloat y = 2; + Assert.That((float)x, Is.GreaterThan((float)y), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs index 05f77920..7962f782 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs @@ -27,11 +27,11 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyGreaterOrEqualFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.GreaterOrEqual(2d, 3d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -43,11 +43,11 @@ public void TestMethod() [Test] public void VerifyGreaterOrEqualFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.GreaterOrEqual(2d, 3d, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -59,11 +59,11 @@ public void TestMethod() [Test] public void VerifyGreaterOrEqualFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ - ↓ClassicAssert.GreaterOrEqual(2d, 3d, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + { + ↓ClassicAssert.GreaterOrEqual(2d, 3d, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -72,6 +72,22 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyGreaterOrEqualFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.GreaterOrEqual(args: new[] { ""first"", ""second"" }, arg2: 3d, message: ""{0}, {1}"", arg1: 2d); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: 2d, Is.GreaterThanOrEqualTo(expected: 3d), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void CodeFixPreservesLineBreakBeforeMessage() { @@ -161,5 +177,43 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyGreaterOrEqualWithImplicitConversionFixInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyFloat + { + private readonly float _value; + + public MyFloat(float value) => _value = value; + + public static implicit operator float(MyFloat value) => value._value; + public static implicit operator MyFloat(float value) => new MyFloat(value); + } + public void TestMethod() + { + MyFloat x = 1; + MyFloat y = 2; + ↓ClassicAssert.GreaterOrEqual(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", arg2: y, arg1: x); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyFloat + { + private readonly float _value; + + public MyFloat(float value) => _value = value; + + public static implicit operator float(MyFloat value) => value._value; + public static implicit operator MyFloat(float value) => new MyFloat(value); + } + public void TestMethod() + { + MyFloat x = 1; + MyFloat y = 2; + Assert.That((float)x, Is.GreaterThanOrEqualTo((float)y), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs index aa031eb0..e1c4703e 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs @@ -27,13 +27,13 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyIsEmptyFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var collection = Array.Empty(); ↓ClassicAssert.IsEmpty(collection); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -47,13 +47,13 @@ public void TestMethod() [Test] public void VerifyIsEmptyFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var collection = Array.Empty(); ↓ClassicAssert.IsEmpty(collection, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -67,13 +67,13 @@ public void TestMethod() [Test] public void VerifyIsEmptyFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var collection = Array.Empty(); - ↓ClassicAssert.IsEmpty(collection, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.IsEmpty(collection, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -84,6 +84,26 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyIsEmptyFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var collection = Array.Empty(); + + ↓ClassicAssert.IsEmpty(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", collection: collection); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var collection = Array.Empty(); + + Assert.That(actual: collection, Is.Empty, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void VerifyIsEmptyWithImplicitTypeConversionFix() { diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs index 26c1f2f1..0e7c246b 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs @@ -79,6 +79,25 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [TestCase("IsFalse", AnalyzerIdentifiers.IsFalseUsage)] + [TestCase("False", AnalyzerIdentifiers.FalseUsage)] + public void VerifyIsFalseAndFalseFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + ↓ClassicAssert.{assertion}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", condition: false); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: false, Is.False, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [TestCase("IsFalse", AnalyzerIdentifiers.IsFalseUsage)] [TestCase("False", AnalyzerIdentifiers.FalseUsage)] public void VerifyIsFalseAndFalseWithImplicitTypeConversionFixes(string assertion, string diagnosticId) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs index cc8fa63d..11fb745a 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -27,14 +27,14 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyIsInstanceOfFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = typeof(int); var actual = 42; ↓ClassicAssert.IsInstanceOf(expected, actual); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -49,14 +49,14 @@ public void TestMethod() [Test] public void VerifyIsInstanceOfFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = typeof(int); var actual = 42; ↓ClassicAssert.IsInstanceOf(expected, actual, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -71,14 +71,14 @@ public void TestMethod() [Test] public void VerifyIsInstanceOfFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = typeof(int); var actual = 42; - ↓ClassicAssert.IsInstanceOf(expected, actual, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.IsInstanceOf(expected, actual, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -90,16 +90,38 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyIsInstanceOfFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = typeof(int); + var actual = 42; + + ↓ClassicAssert.IsInstanceOf(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", actual: actual, expected: expected); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = typeof(int); + var actual = 42; + + Assert.That(actual: actual, Is.InstanceOf(expectedType: expected), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void VerifyIsInstanceOfGenericFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var actual = 42; ↓ClassicAssert.IsInstanceOf(actual); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -199,13 +221,13 @@ public Wrapped(T value) [Test] public void VerifyIsInstanceOfGenericFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var actual = 42; ↓ClassicAssert.IsInstanceOf(actual, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -219,15 +241,29 @@ public void TestMethod() [Test] public void VerifyIsInstanceOfGenericFixWithMessageAndParams() { - var code = TestUtility.WrapInTestMethod($@" + var code = TestUtility.WrapInTestMethod(@" var actual = 42; - ↓ClassicAssert.IsInstanceOf(actual, ""message-id: {{0}}"", Guid.NewGuid());"); + ↓ClassicAssert.IsInstanceOf(actual, ""message-id: {0}"", Guid.NewGuid());"); var fixedCode = TestUtility.WrapInTestMethod(@" var actual = 42; Assert.That(actual, Is.InstanceOf(), $""message-id: {Guid.NewGuid()}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyIsInstanceOfGenericFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapInTestMethod(@" + var actual = 42; + + ↓ClassicAssert.IsInstanceOf(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", actual: actual);"); + var fixedCode = TestUtility.WrapInTestMethod(@" + var actual = 42; + + Assert.That(actual: actual, Is.InstanceOf(), $""{""first""}, {""second"" }"");"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs index cc5a8649..c88e4def 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs @@ -27,13 +27,13 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyIsNaNFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = double.NaN; ↓ClassicAssert.IsNaN(expr); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -47,13 +47,13 @@ public void TestMethod() [Test] public void VerifyIsNaNFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = double.NaN; ↓ClassicAssert.IsNaN(expr, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -67,13 +67,13 @@ public void TestMethod() [Test] public void VerifyIsNaNFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = double.NaN; - ↓ClassicAssert.IsNaN(expr, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.IsNaN(expr, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -83,5 +83,25 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyIsNaNFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = double.NaN; + + ↓ClassicAssert.IsNaN(args: new[] { ""first"", ""second"" }, aDouble: expr, message: ""{0}, {1}""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = double.NaN; + + Assert.That(actual: expr, Is.NaN, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs index f24d963f..b5b59fe4 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs @@ -27,13 +27,13 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyIsNotEmptyFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var collection = Array.Empty(); ↓ClassicAssert.IsNotEmpty(collection); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -47,13 +47,13 @@ public void TestMethod() [Test] public void VerifyIsNotEmptyFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var collection = Array.Empty(); ↓ClassicAssert.IsNotEmpty(collection, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -67,13 +67,13 @@ public void TestMethod() [Test] public void VerifyIsNotEmptyFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var collection = Array.Empty(); - ↓ClassicAssert.IsNotEmpty(collection, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.IsNotEmpty(collection, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -84,6 +84,26 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyIsNotEmptyFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var collection = Array.Empty(); + + ↓ClassicAssert.IsNotEmpty(args: new[] { ""first"", ""second"" }, collection: collection, message: ""{0}, {1}""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var collection = Array.Empty(); + + Assert.That(actual: collection, Is.Not.Empty, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void VerifyIsNotEmptyWithImplicitTypeConversionFix() { @@ -119,5 +139,41 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyIsNotEmptyWithImplicitTypeConversionFixInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyString + { + private readonly string _value; + + public MyString(string value) => _value = value; + + public static implicit operator string(MyString value) => value._value; + public static implicit operator MyString(string value) => new MyString(value); + } + public void TestMethod() + { + MyString s = ""Hello NUnit""; + ↓ClassicAssert.IsNotEmpty(args: new[] { ""first"", ""second"" }, aString: s, message: ""{0}, {1}""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyString + { + private readonly string _value; + + public MyString(string value) => _value = value; + + public static implicit operator string(MyString value) => value._value; + public static implicit operator MyString(string value) => new MyString(value); + } + public void TestMethod() + { + MyString s = ""Hello NUnit""; + Assert.That((string)s, Is.Not.Empty, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs index 2529f035..1ef0592e 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -27,14 +27,14 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyIsNotInstanceOfFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = typeof(int); var actual = 42; ↓ClassicAssert.IsNotInstanceOf(expected, actual); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -49,14 +49,14 @@ public void TestMethod() [Test] public void VerifyIsNotInstanceOfFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = typeof(int); var actual = 42; ↓ClassicAssert.IsNotInstanceOf(expected, actual, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -71,14 +71,14 @@ public void TestMethod() [Test] public void VerifyIsNotInstanceOfFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = typeof(int); var actual = 42; - ↓ClassicAssert.IsNotInstanceOf(expected, actual, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.IsNotInstanceOf(expected, actual, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -90,16 +90,38 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyIsNotInstanceOfFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = typeof(int); + var actual = 42; + + ↓ClassicAssert.IsNotInstanceOf(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", actual: actual, expected: expected); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = typeof(int); + var actual = 42; + + Assert.That(actual: actual, Is.Not.InstanceOf(expectedType: expected), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void VerifyIsNotInstanceOfGenericFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var actual = 42; ↓ClassicAssert.IsNotInstanceOf(actual); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -199,13 +221,13 @@ public Wrapped(T value) [Test] public void VerifyIsNotInstanceOfGenericFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var actual = 42; ↓ClassicAssert.IsNotInstanceOf(actual, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -219,15 +241,29 @@ public void TestMethod() [Test] public void VerifyIsNotInstanceOfGenericFixWithMessageAndParams() { - var code = TestUtility.WrapInTestMethod($@" + var code = TestUtility.WrapInTestMethod(@" var actual = 42; - ↓ClassicAssert.IsNotInstanceOf(actual, ""message-id: {{0}}"", Guid.NewGuid());"); + ↓ClassicAssert.IsNotInstanceOf(actual, ""message-id: {0}"", Guid.NewGuid());"); var fixedCode = TestUtility.WrapInTestMethod(@" var actual = 42; Assert.That(actual, Is.Not.InstanceOf(), $""message-id: {Guid.NewGuid()}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyIsNotInstanceOfGenericFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapInTestMethod(@" + var actual = 42; + + ↓ClassicAssert.IsNotInstanceOf(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", actual: actual);"); + var fixedCode = TestUtility.WrapInTestMethod(@" + var actual = 42; + + Assert.That(actual: actual, Is.Not.InstanceOf(), $""{""first""}, {""second"" }"");"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs index b270b290..55836845 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs @@ -84,5 +84,26 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [TestCase("IsNotNull", AnalyzerIdentifiers.IsNotNullUsage)] + [TestCase("NotNull", AnalyzerIdentifiers.NotNullUsage)] + public void VerifyIsNotNullAndNotNullFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + object? obj = null; + ↓ClassicAssert.{assertion}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", anObject: obj); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + object? obj = null; + Assert.That(actual: obj, Is.Not.Null, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs index 638b821e..88ea98af 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs @@ -84,5 +84,26 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [TestCase("IsNull", AnalyzerIdentifiers.IsNullUsage)] + [TestCase("Null", AnalyzerIdentifiers.NullUsage)] + public void VerifyIsNullAndNullFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + object? obj = null; + ↓ClassicAssert.{assertion}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", anObject: obj); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + object? obj = null; + Assert.That(actual: obj, Is.Null, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs index 9f093c0b..827aeb91 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs @@ -79,6 +79,25 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] + [TestCase("True", AnalyzerIdentifiers.TrueUsage)] + public void VerifyIsTrueAndTrueFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + ↓ClassicAssert.{assertion}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", condition: true); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: true, Is.True, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] [TestCase("True", AnalyzerIdentifiers.TrueUsage)] public void VerifyIsTrueAndTrueWithImplicitTypeConversionFixes(string assertion, string diagnosticId) @@ -117,5 +136,44 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] + [TestCase("True", AnalyzerIdentifiers.TrueUsage)] + public void VerifyIsTrueAndTrueWithImplicitTypeConversionFixesInNonstandardOrder(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + private struct MyBool + {{ + private readonly bool _value; + + public MyBool(bool value) => _value = value; + + public static implicit operator bool(MyBool value) => value._value; + public static implicit operator MyBool(bool value) => new MyBool(value); + }} + public void TestMethod() + {{ + MyBool x = true; + ↓ClassicAssert.{assertion}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", condition: x); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyBool + { + private readonly bool _value; + + public MyBool(bool value) => _value = value; + + public static implicit operator bool(MyBool value) => value._value; + public static implicit operator MyBool(bool value) => new MyBool(value); + } + public void TestMethod() + { + MyBool x = true; + Assert.That((bool)x, Is.True, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs index 2505d814..0086514f 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs @@ -81,5 +81,25 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription + IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.Suffix); } + + [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] + [TestCase("True", AnalyzerIdentifiers.TrueUsage)] + public void VerifyIsTrueAndTrueFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + ↓ClassicAssert.{assertion}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", condition: true); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(condition: true, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, + fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription + IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.Suffix); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs index 76a802f5..4aebc5d2 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs @@ -27,11 +27,11 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyLessFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.Less(2d, 3d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -43,11 +43,11 @@ public void TestMethod() [Test] public void VerifyLessFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.Less(2d, 3d, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -59,11 +59,11 @@ public void TestMethod() [Test] public void VerifyLessFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ - ↓ClassicAssert.Less(2d, 3d, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + { + ↓ClassicAssert.Less(2d, 3d, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -72,10 +72,26 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyLessFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.Less(args: new[] { ""first"", ""second"" }, arg2: 3d, message: ""{0}, {1}"", arg1: 2d); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: 2d, Is.LessThan(expected: 3d), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void CodeFixPreservesLineBreakBeforeMessage() { - var code = TestUtility.WrapInTestMethod($@" + var code = TestUtility.WrapInTestMethod(@" ClassicAssert.Less(2d, 3d, ""message"");"); @@ -161,5 +177,43 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyLessWithImplicitConversionFixInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyFloat + { + private readonly float _value; + + public MyFloat(float value) => _value = value; + + public static implicit operator float(MyFloat value) => value._value; + public static implicit operator MyFloat(float value) => new MyFloat(value); + } + public void TestMethod() + { + MyFloat x = 1; + MyFloat y = 2; + ↓ClassicAssert.Less(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", arg2: y, arg1: x); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyFloat + { + private readonly float _value; + + public MyFloat(float value) => _value = value; + + public static implicit operator float(MyFloat value) => value._value; + public static implicit operator MyFloat(float value) => new MyFloat(value); + } + public void TestMethod() + { + MyFloat x = 1; + MyFloat y = 2; + Assert.That((float)x, Is.LessThan((float)y), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs index 22654092..aaa3f8d7 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs @@ -27,11 +27,11 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyLessOrEqualFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.LessOrEqual(2d, 3d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -43,11 +43,11 @@ public void TestMethod() [Test] public void VerifyLessOrEqualFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.LessOrEqual(2d, 3d, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -59,11 +59,11 @@ public void TestMethod() [Test] public void VerifyLessOrEqualFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ - ↓ClassicAssert.LessOrEqual(2d, 3d, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + { + ↓ClassicAssert.LessOrEqual(2d, 3d, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -72,10 +72,26 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyLessOrEqualFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.LessOrEqual(args: new[] { ""first"", ""second"" }, arg2: 3d, message: ""{0}, {1}"", arg1: 2d); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(actual: 2d, Is.LessThanOrEqualTo(expected: 3d), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void CodeFixPreservesLineBreakBeforeMessage() { - var code = TestUtility.WrapInTestMethod($@" + var code = TestUtility.WrapInTestMethod(@" ClassicAssert.LessOrEqual(2d, 3d, ""message"");"); @@ -161,5 +177,43 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyLessOrEqualWithImplicitConversionFixInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyFloat + { + private readonly float _value; + + public MyFloat(float value) => _value = value; + + public static implicit operator float(MyFloat value) => value._value; + public static implicit operator MyFloat(float value) => new MyFloat(value); + } + public void TestMethod() + { + MyFloat x = 1; + MyFloat y = 2; + ↓ClassicAssert.LessOrEqual(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", arg2: y, arg1: x); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + private struct MyFloat + { + private readonly float _value; + + public MyFloat(float value) => _value = value; + + public static implicit operator float(MyFloat value) => value._value; + public static implicit operator MyFloat(float value) => new MyFloat(value); + } + public void TestMethod() + { + MyFloat x = 1; + MyFloat y = 2; + Assert.That((float)x, Is.LessThanOrEqualTo((float)y), $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs index db0cca19..5b29eb9e 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs @@ -27,13 +27,13 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyNotZeroFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = default(int); ↓ClassicAssert.NotZero(expr); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -47,13 +47,13 @@ public void TestMethod() [Test] public void VerifyNotZeroFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = default(int); ↓ClassicAssert.NotZero(expr, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -67,13 +67,13 @@ public void TestMethod() [Test] public void VerifyNotZeroFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = default(int); - ↓ClassicAssert.NotZero(expr, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.NotZero(expr, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -83,5 +83,25 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyNotZeroFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = default(int); + + ↓ClassicAssert.NotZero(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", actual: expr); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = default(int); + + Assert.That(actual: expr, Is.Not.Zero, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs index 6f326729..9d664517 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs @@ -27,13 +27,13 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyZeroFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = default(int); ↓ClassicAssert.Zero(expr); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -47,13 +47,13 @@ public void TestMethod() [Test] public void VerifyZeroFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = default(int); ↓ClassicAssert.Zero(expr, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -67,13 +67,13 @@ public void TestMethod() [Test] public void VerifyZeroFixWithMessageAndParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expr = default(int); - ↓ClassicAssert.Zero(expr, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.Zero(expr, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -83,5 +83,25 @@ public void TestMethod() }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + + [Test] + public void VerifyZeroFixWithMessageAndParamsInNonstandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = default(int); + + ↓ClassicAssert.Zero(args: new[] { ""first"", ""second"" }, message: ""{0}, {1}"", actual: expr); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = default(int); + + Assert.That(actual: expr, Is.Zero, $""{""first""}, {""second"" }""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } } } diff --git a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs index 5838ce5a..66c6e635 100644 --- a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs @@ -233,7 +233,7 @@ public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsedOutOfOrde var code = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ typeof(byte), typeof(char) }}; var expected = typeof(byte); - ↓CollectionAssert.{method}({secondParameterName}: expected, args: new[] {{ ""first"", ""second"" }}, collection: collection, message: ""{{0}}, {{1}}""); + ↓CollectionAssert.{method}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", {secondParameterName}: expected, collection: collection); "); var fixedCode = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ typeof(byte), typeof(char) }}; diff --git a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs index 92b4c161..8836c23a 100644 --- a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs @@ -24,7 +24,7 @@ public void AnalyzeWhenNoArgumentsAreUsed(string method) ↓StringAssert.{method}(""expected"", ""actual""); "); var fixedCode = TestUtility.WrapInTestMethod(@$" - Assert.That(""actual"", {GetAdjustedConstraint(method)}); + Assert.That(""actual"", {GetAdjustedConstraint(method, useNamedParameter: false)}); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } @@ -36,7 +36,7 @@ public void AnalyzeWhenOnlyMessageArgumentIsUsed(string method) ↓StringAssert.{method}(""expected"", ""actual"", ""message""); "); var fixedCode = TestUtility.WrapInTestMethod(@$" - Assert.That(""actual"", {GetAdjustedConstraint(method)}, ""message""); + Assert.That(""actual"", {GetAdjustedConstraint(method, useNamedParameter: false)}, ""message""); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } @@ -48,15 +48,29 @@ public void AnalyzeWhenFormatAndArgumentsAreUsed(string method) ↓StringAssert.{method}(""expected"", ""actual"", ""Because of {{0}}"", ""message""); "); var fixedCode = TestUtility.WrapInTestMethod(@$" - Assert.That(""actual"", {GetAdjustedConstraint(method)}, $""Because of {{""message""}}""); + Assert.That(""actual"", {GetAdjustedConstraint(method, useNamedParameter: false)}, $""Because of {{""message""}}""); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } - private static string GetAdjustedConstraint(string method) + [TestCaseSource(nameof(StringAsserts))] + public void AnalyzeWhenFormatAndArgumentsAreUsedOutOfOrder(string method) + { + var firstParameterName = StringAssertUsageCodeFix.StringAssertToExpectedParameterName[method]; + var code = TestUtility.WrapInTestMethod(@$" + ↓StringAssert.{method}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", actual: ""actual"", {firstParameterName}: ""expected"");"); + var fixedCode = TestUtility.WrapInTestMethod(@$" + Assert.That(actual: ""actual"", {GetAdjustedConstraint(method, useNamedParameter: true)}, $""{{""first""}}, {{""second"" }}"");"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } + + private static string GetAdjustedConstraint(string method, bool useNamedParameter) { + var expectedParameterName = StringAssertUsageCodeFix.StringAssertToExpectedParameterName[method]; return StringAssertUsageAnalyzer.StringAssertToConstraint[method] - .Replace("expected", "\"expected\""); + .Replace( + "expected", + useNamedParameter ? $"{expectedParameterName}: \"expected\"" : "\"expected\""); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs index 2121d051..a6f07587 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs @@ -21,6 +21,9 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var expectedArgumentNameColon = arg2.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedParameter); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -28,10 +31,15 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsGreaterThan))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arg2)))); + SyntaxFactory.SingletonSeparatedList( + arg2.WithNameColon(expectedArgumentNameColon))))); var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - return (arg1, constraintArgument); + var actualArgumentNameColon = arg1.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var actualArgument = arg1.WithNameColon(actualArgumentNameColon); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs index a1cff0f2..5e868c18 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs @@ -21,6 +21,9 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var expectedArgumentNameColon = arg2.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedParameter); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -28,10 +31,15 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsGreaterThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arg2)))); + SyntaxFactory.SingletonSeparatedList( + arg2.WithNameColon(expectedArgumentNameColon))))); var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - return (arg1, constraintArgument); + var actualArgumentNameColon = arg1.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var actualArgument = arg1.WithNameColon(actualArgumentNameColon); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs index 69b0ccda..7fae66e7 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs @@ -25,12 +25,16 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg var actualArgument = argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfCollectionParameter, out var collectionArgument) ? collectionArgument : argumentNamesToArguments[NUnitFrameworkConstants.NameOfAStringParameter]; + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty))); - return (actualArgument, constraintArgument); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs index 263c8165..224da970 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs @@ -23,12 +23,16 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsFalse))); - return (actualArgument, constraintArgument); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs index d2415ec6..27702c1d 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs @@ -37,6 +37,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgumentNameColon = expectedArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedTypeParameter); + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -44,10 +48,14 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsInstanceOf))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(expectedArgument)))); + SyntaxFactory.SingletonSeparatedList( + expectedArgument.WithNameColon(expectedArgumentNameColon))))); var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; - return (actualArgument, constraintArgument); + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs index 8498336c..beed2199 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs @@ -22,12 +22,16 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfADoubleParameter]; + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNaN))); - return (actualArgument, constraintArgument); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs index fe31976c..1ccf0629 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs @@ -24,6 +24,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg var actualArgument = argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfCollectionParameter, out var collectionArgument) ? collectionArgument : argumentNamesToArguments[NUnitFrameworkConstants.NameOfAStringParameter]; + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -32,7 +36,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty))); - return (actualArgument, constraintArgument); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs index a1f77439..b915fc81 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs @@ -40,6 +40,9 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgumentNameColon = expectedArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedTypeParameter); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -50,10 +53,14 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsInstanceOf))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(expectedArgument)))); + SyntaxFactory.SingletonSeparatedList( + expectedArgument.WithNameColon(expectedArgumentNameColon))))); var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; - return (actualArgument, constraintArgument); + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs index 6b66cef7..8a8d08c5 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs @@ -23,6 +23,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -31,7 +35,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull))); - return (actualArgument, constraintArgument); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs index b3a22285..9a70db9f 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs @@ -23,12 +23,16 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull))); - return (actualArgument, constraintArgument); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs index 70ec01f2..1f837957 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs @@ -23,12 +23,16 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; + var actualArgumentNameColon = actualArgument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsTrue))); - return (actualArgument, constraintArgument); + return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs index ebba1422..ba31805b 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs @@ -21,6 +21,9 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var expectedArgumentNameColon = arg2.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedParameter); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -28,10 +31,15 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThan))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arg2)))); + SyntaxFactory.SingletonSeparatedList( + arg2.WithNameColon(expectedArgumentNameColon))))); - var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - return (arg1, constraintArgument); + var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; + var actualArgumentNameColon = arg1Argument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var actualArgument = arg1Argument.WithNameColon(actualArgumentNameColon); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs index aba711e6..4450e294 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs @@ -21,6 +21,9 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var expectedArgumentNameColon = arg2.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedParameter); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -28,10 +31,15 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList(arg2)))); + SyntaxFactory.SingletonSeparatedList( + arg2.WithNameColon(expectedArgumentNameColon))))); - var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - return (arg1, constraintArgument); + var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; + var actualArgumentNameColon = arg1Argument.NameColon is null + ? null + : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); + var actualArgument = arg1Argument.WithNameColon(actualArgumentNameColon); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs b/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs index a0898fb9..4bc12350 100644 --- a/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs @@ -16,6 +16,21 @@ namespace NUnit.Analyzers.StringAssertUsage [Shared] internal class StringAssertUsageCodeFix : ClassicModelAssertUsageCodeFix { + internal static readonly ImmutableDictionary StringAssertToExpectedParameterName = + new Dictionary() + { + { NameOfStringAssertContains, NameOfExpectedParameter }, + { NameOfStringAssertDoesNotContain, NameOfExpectedParameter }, + { NameOfStringAssertStartsWith, NameOfExpectedParameter }, + { NameOfStringAssertDoesNotStartWith, NameOfExpectedParameter }, + { NameOfStringAssertEndsWith, NameOfExpectedParameter }, + { NameOfStringAssertDoesNotEndWith, NameOfExpectedParameter }, + { NameOfStringAssertAreEqualIgnoringCase, NameOfExpectedParameter }, + { NameOfStringAssertAreNotEqualIgnoringCase, NameOfExpectedParameter }, + { NameOfStringAssertIsMatch, NameOfPatternParameter }, + { NameOfStringAssertDoesNotMatch, NameOfPatternParameter }, + }.ToImmutableDictionary(); + private static readonly ImmutableDictionary StringAssertToConstraints = new Dictionary { @@ -31,21 +46,6 @@ internal class StringAssertUsageCodeFix : ClassicModelAssertUsageCodeFix { NameOfStringAssertDoesNotMatch, new Constraints(NameOfDoes, NameOfDoesNot, NameOfDoesMatch) }, }.ToImmutableDictionary(); - private static readonly ImmutableDictionary StringAssertToExpectedParameterName = - new Dictionary() - { - { NameOfStringAssertContains, NameOfExpectedParameter }, - { NameOfStringAssertDoesNotContain, NameOfExpectedParameter }, - { NameOfStringAssertStartsWith, NameOfExpectedParameter }, - { NameOfStringAssertDoesNotStartWith, NameOfExpectedParameter }, - { NameOfStringAssertEndsWith, NameOfExpectedParameter }, - { NameOfStringAssertDoesNotEndWith, NameOfExpectedParameter }, - { NameOfStringAssertAreEqualIgnoringCase, NameOfExpectedParameter }, - { NameOfStringAssertAreNotEqualIgnoringCase, NameOfExpectedParameter }, - { NameOfStringAssertIsMatch, NameOfPatternParameter }, - { NameOfStringAssertDoesNotMatch, NameOfPatternParameter }, - }.ToImmutableDictionary(); - public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create( AnalyzerIdentifiers.StringAssertUsage); From e6036e1a25bd23ef9bcebf3e4ae39957e251ecc9 Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Sun, 7 Apr 2024 13:38:00 -0400 Subject: [PATCH 08/17] Remove an extra whitespace --- .../AreEqualClassicModelAssertUsageCodeFixTests.cs | 4 ++-- .../AreNotEqualClassicModelAssertUsageCodeFixTests.cs | 2 +- .../AreNotSameClassicModelAssertUsageCodeFixTests.cs | 2 +- .../AreSameClassicModelAssertUsageCodeFixTests.cs | 2 +- .../ContainsClassicModelAssertUsageCodeFixTests.cs | 2 +- .../GreaterClassicModelAssertUsageCodeFixTests.cs | 4 ++-- .../GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs | 4 ++-- .../IsEmptyClassicModelAssertUsageCodeFixTests.cs | 2 +- .../IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs | 2 +- .../IsInstanceOfClassicModelAssertUsageCodeFixTests.cs | 4 ++-- .../IsNaNClassicModelAssertUsageCodeFixTests.cs | 2 +- .../IsNotEmptyClassicModelAssertUsageCodeFixTests.cs | 4 ++-- .../IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs | 4 ++-- ...sNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs | 2 +- .../IsNullAndNullClassicModelAssertUsageCodeFixTests.cs | 2 +- .../IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs | 4 ++-- ...ueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs | 2 +- .../LessClassicModelAssertUsageCodeFixTests.cs | 4 ++-- .../LessOrEqualClassicModelAssertUsageCodeFixTests.cs | 4 ++-- .../NotZeroClassicModelAssertUsageCodeFixTests.cs | 2 +- .../ZeroClassicModelAssertUsageCodeFixTests.cs | 2 +- .../CollectionAssertUsageCodeFixTests.cs | 6 +++--- .../StringAssertUsage/StringAssertUsageCodeFixTests.cs | 2 +- src/nunit.analyzers/Helpers/CodeFixHelper.cs | 4 +++- 24 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs index 66fef9c4..8d4d37be 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs @@ -133,8 +133,8 @@ public void TestMethod() public void TestMethod() { var actual = 3d; - Assert.That(actual: actual, Is.EqualTo(expected: 2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid().ToString() }""); - }"); // TODO: Fix trailing trivia if possible + Assert.That(actual: actual, Is.EqualTo(expected: 2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid().ToString()}""); + }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs index 33098298..b14d9bd8 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: ""actual"", Is.Not.EqualTo(expected: ""expected""), $""{""first""}, {""second"" }""); + Assert.That(actual: ""actual"", Is.Not.EqualTo(expected: ""expected""), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs index 84a345af..60681b24 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs @@ -107,7 +107,7 @@ public void TestMethod() var expected = new object(); var actual = new object(); - Assert.That(actual: actual, Is.Not.SameAs(expected: expected), $""{""first""}, {""second"" }""); + Assert.That(actual: actual, Is.Not.SameAs(expected: expected), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs index 4f856df8..9cdfc487 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs @@ -107,7 +107,7 @@ public void TestMethod() var expected = new object(); var actual = new object(); - Assert.That(actual: actual, Is.SameAs(expected: expected), $""{""first""}, {""second"" }""); + Assert.That(actual: actual, Is.SameAs(expected: expected), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs index a72171a1..4e1c18e0 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs @@ -107,7 +107,7 @@ public void TestMethod() var instance = new object(); var collection = Array.Empty(); - Assert.That(actual: collection, Does.Contain(expected: instance), $""{""first""}, {""second"" }""); + Assert.That(actual: collection, Does.Contain(expected: instance), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, instanceDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs index 631181a8..3d0d94b3 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 2d, Is.GreaterThan(expected: 3d), $""{""first""}, {""second"" }""); + Assert.That(actual: 2d, Is.GreaterThan(expected: 3d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -211,7 +211,7 @@ public void TestMethod() { MyFloat x = 1; MyFloat y = 2; - Assert.That((float)x, Is.GreaterThan((float)y), $""{""first""}, {""second"" }""); + Assert.That((float)x, Is.GreaterThan((float)y), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs index 7962f782..ff33fb98 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 2d, Is.GreaterThanOrEqualTo(expected: 3d), $""{""first""}, {""second"" }""); + Assert.That(actual: 2d, Is.GreaterThanOrEqualTo(expected: 3d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -211,7 +211,7 @@ public void TestMethod() { MyFloat x = 1; MyFloat y = 2; - Assert.That((float)x, Is.GreaterThanOrEqualTo((float)y), $""{""first""}, {""second"" }""); + Assert.That((float)x, Is.GreaterThanOrEqualTo((float)y), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs index e1c4703e..0480963b 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() { var collection = Array.Empty(); - Assert.That(actual: collection, Is.Empty, $""{""first""}, {""second"" }""); + Assert.That(actual: collection, Is.Empty, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs index 0e7c246b..ceac77df 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs @@ -93,7 +93,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: false, Is.False, $""{""first""}, {""second"" }""); + Assert.That(actual: false, Is.False, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs index 11fb745a..fc19fdf2 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -107,7 +107,7 @@ public void TestMethod() var expected = typeof(int); var actual = 42; - Assert.That(actual: actual, Is.InstanceOf(expectedType: expected), $""{""first""}, {""second"" }""); + Assert.That(actual: actual, Is.InstanceOf(expectedType: expected), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -262,7 +262,7 @@ public void VerifyIsInstanceOfGenericFixWithMessageAndParamsInNonstandardOrder() var fixedCode = TestUtility.WrapInTestMethod(@" var actual = 42; - Assert.That(actual: actual, Is.InstanceOf(), $""{""first""}, {""second"" }"");"); + Assert.That(actual: actual, Is.InstanceOf(), $""{""first""}, {""second""}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs index c88e4def..23f4f51c 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() { var expr = double.NaN; - Assert.That(actual: expr, Is.NaN, $""{""first""}, {""second"" }""); + Assert.That(actual: expr, Is.NaN, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs index b5b59fe4..4d9e85e3 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() { var collection = Array.Empty(); - Assert.That(actual: collection, Is.Not.Empty, $""{""first""}, {""second"" }""); + Assert.That(actual: collection, Is.Not.Empty, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -171,7 +171,7 @@ private struct MyString public void TestMethod() { MyString s = ""Hello NUnit""; - Assert.That((string)s, Is.Not.Empty, $""{""first""}, {""second"" }""); + Assert.That((string)s, Is.Not.Empty, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs index 1ef0592e..5d66e255 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -107,7 +107,7 @@ public void TestMethod() var expected = typeof(int); var actual = 42; - Assert.That(actual: actual, Is.Not.InstanceOf(expectedType: expected), $""{""first""}, {""second"" }""); + Assert.That(actual: actual, Is.Not.InstanceOf(expectedType: expected), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -262,7 +262,7 @@ public void VerifyIsNotInstanceOfGenericFixWithMessageAndParamsInNonstandardOrde var fixedCode = TestUtility.WrapInTestMethod(@" var actual = 42; - Assert.That(actual: actual, Is.Not.InstanceOf(), $""{""first""}, {""second"" }"");"); + Assert.That(actual: actual, Is.Not.InstanceOf(), $""{""first""}, {""second""}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs index 55836845..c851b535 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs @@ -101,7 +101,7 @@ public void TestMethod() public void TestMethod() { object? obj = null; - Assert.That(actual: obj, Is.Not.Null, $""{""first""}, {""second"" }""); + Assert.That(actual: obj, Is.Not.Null, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs index 88ea98af..704d7006 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs @@ -101,7 +101,7 @@ public void TestMethod() public void TestMethod() { object? obj = null; - Assert.That(actual: obj, Is.Null, $""{""first""}, {""second"" }""); + Assert.That(actual: obj, Is.Null, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs index 827aeb91..f8ffb562 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs @@ -93,7 +93,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: true, Is.True, $""{""first""}, {""second"" }""); + Assert.That(actual: true, Is.True, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -171,7 +171,7 @@ private struct MyBool public void TestMethod() { MyBool x = true; - Assert.That((bool)x, Is.True, $""{""first""}, {""second"" }""); + Assert.That((bool)x, Is.True, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs index 0086514f..70f935fc 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs @@ -96,7 +96,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(condition: true, $""{""first""}, {""second"" }""); + Assert.That(condition: true, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription + IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.Suffix); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs index 4aebc5d2..774b03ef 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 2d, Is.LessThan(expected: 3d), $""{""first""}, {""second"" }""); + Assert.That(actual: 2d, Is.LessThan(expected: 3d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -211,7 +211,7 @@ public void TestMethod() { MyFloat x = 1; MyFloat y = 2; - Assert.That((float)x, Is.LessThan((float)y), $""{""first""}, {""second"" }""); + Assert.That((float)x, Is.LessThan((float)y), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs index aaa3f8d7..23e5e076 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 2d, Is.LessThanOrEqualTo(expected: 3d), $""{""first""}, {""second"" }""); + Assert.That(actual: 2d, Is.LessThanOrEqualTo(expected: 3d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -211,7 +211,7 @@ public void TestMethod() { MyFloat x = 1; MyFloat y = 2; - Assert.That((float)x, Is.LessThanOrEqualTo((float)y), $""{""first""}, {""second"" }""); + Assert.That((float)x, Is.LessThanOrEqualTo((float)y), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs index 5b29eb9e..4771fa5d 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() { var expr = default(int); - Assert.That(actual: expr, Is.Not.Zero, $""{""first""}, {""second"" }""); + Assert.That(actual: expr, Is.Not.Zero, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs index 9d664517..bcec0150 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() { var expr = default(int); - Assert.That(actual: expr, Is.Zero, $""{""first""}, {""second"" }""); + Assert.That(actual: expr, Is.Zero, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs index 66c6e635..9cef1f59 100644 --- a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs @@ -72,7 +72,7 @@ public void AnalyzeOneCollectionWhenFormatAndParamsArgumentsAreUsedOutOfOrder(st "); var fixedCode = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ 1, 2, 3 }}; - Assert.That(collection, {CollectionAssertUsageAnalyzer.OneCollectionParameterAsserts[method]}, $""{{""first""}}, {{""second"" }}""); + Assert.That(collection, {CollectionAssertUsageAnalyzer.OneCollectionParameterAsserts[method]}, $""{{""first""}}, {{""second""}}""); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } @@ -154,7 +154,7 @@ public void AnalyzeTwoCollectionWhenFormatAndParamsArgumentsAreUsedOutOfOrder(st var fixedCode = TestUtility.WrapInTestMethod(@$" var collection1 = new[] {{ 1, 2, 3 }}; var collection2 = new[] {{ 2, 4, 6 }}; - Assert.That({GetAdjustedTwoCollectionConstraint(method)}, $""{{""first""}}, {{""second"" }}""); + Assert.That({GetAdjustedTwoCollectionConstraint(method)}, $""{{""first""}}, {{""second""}}""); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } @@ -238,7 +238,7 @@ public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsedOutOfOrde var fixedCode = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ typeof(byte), typeof(char) }}; var expected = typeof(byte); - Assert.That(collection, {CollectionAssertUsageAnalyzer.CollectionAndItemParameterAsserts[method]}, $""{{""first""}}, {{""second"" }}""); + Assert.That(collection, {CollectionAssertUsageAnalyzer.CollectionAndItemParameterAsserts[method]}, $""{{""first""}}, {{""second""}}""); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } diff --git a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs index 8836c23a..a58373c5 100644 --- a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs @@ -60,7 +60,7 @@ public void AnalyzeWhenFormatAndArgumentsAreUsedOutOfOrder(string method) var code = TestUtility.WrapInTestMethod(@$" ↓StringAssert.{method}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", actual: ""actual"", {firstParameterName}: ""expected"");"); var fixedCode = TestUtility.WrapInTestMethod(@$" - Assert.That(actual: ""actual"", {GetAdjustedConstraint(method, useNamedParameter: true)}, $""{{""first""}}, {{""second"" }}"");"); + Assert.That(actual: ""actual"", {GetAdjustedConstraint(method, useNamedParameter: true)}, $""{{""first""}}, {{""second""}}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } diff --git a/src/nunit.analyzers/Helpers/CodeFixHelper.cs b/src/nunit.analyzers/Helpers/CodeFixHelper.cs index 3c256c42..40883e23 100644 --- a/src/nunit.analyzers/Helpers/CodeFixHelper.cs +++ b/src/nunit.analyzers/Helpers/CodeFixHelper.cs @@ -61,7 +61,9 @@ internal static class CodeFixHelper ? argsArrayExpression.Initializer.Expressions.ToArray() : new[] { argsExpression }; - var interpolatedStringContent = UpdateStringFormatToFormattableString(formatSpecification, formatArgumentExpressions); + var interpolatedStringContent = UpdateStringFormatToFormattableString( + formatSpecification, + formatArgumentExpressions.Select(e => e.WithoutTrivia()).ToArray()); var interpolatedString = SyntaxFactory.InterpolatedStringExpression( SyntaxFactory.Token(SyntaxKind.InterpolatedStringStartToken), SyntaxFactory.List(interpolatedStringContent)); From 5e3abfd83d42eea8dc7184ebe042589121197002 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Mon, 8 Apr 2024 13:39:35 +0800 Subject: [PATCH 09/17] Code review changes --- ...EqualClassicModelAssertUsageCodeFixTests.cs | 18 ++++++++++++++++++ .../ClassicModelAssertUsageCodeFixTests.cs | 3 ++- .../StringAssertUsageCodeFixTests.cs | 6 ++++-- .../ClassicModelAssertUsageCodeFix.cs | 16 ++++++---------- src/nunit.analyzers/Helpers/CodeFixHelper.cs | 9 ++++----- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs index 8d4d37be..cf89c5fc 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs @@ -138,6 +138,24 @@ public void TestMethod() RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } + [Test] + public void VerifyAreEqualFixWithMessageAnd2ParamsInStandardOrder() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var actual = 3d; + ↓ClassicAssert.AreEqual(expected: 2d, actual: actual, delta: 0.0000001d, ""message-id: {0}, {1}"", ""Guid.NewGuid()"", Guid.NewGuid()); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var actual = 3d; + Assert.That(actual: actual, Is.EqualTo(expected: 2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid()}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + [Test] public void VerifyAreEqualFixWhenToleranceExists() { diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFixTests.cs index d756df5a..63c73667 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFixTests.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Collections.Immutable; using Microsoft.CodeAnalysis; @@ -25,7 +26,7 @@ private sealed class TestableClassicModelAssertUsageCodeFix : ClassicModelAssert protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArgument) ConstructActualAndConstraintArguments( Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) => - (default!, default); + throw new NotImplementedException(); } } } diff --git a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs index a58373c5..3e73bb06 100644 --- a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs @@ -66,11 +66,13 @@ public void AnalyzeWhenFormatAndArgumentsAreUsedOutOfOrder(string method) private static string GetAdjustedConstraint(string method, bool useNamedParameter) { + const string Expected = "expected"; + var expectedParameterName = StringAssertUsageCodeFix.StringAssertToExpectedParameterName[method]; return StringAssertUsageAnalyzer.StringAssertToConstraint[method] .Replace( - "expected", - useNamedParameter ? $"{expectedParameterName}: \"expected\"" : "\"expected\""); + Expected, + useNamedParameter ? $"{expectedParameterName}: \"{Expected}\"" : $"\"{Expected}\""); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs index 08bf3c67..5eb68206 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageCodeFix.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; @@ -54,14 +53,11 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) if (newInvocationNode is null) return; - var (argumentNamesToArguments, args) = SplitUpOtherParametersAndParamParameter(semanticModel, invocationNode); + var methodSymbol = semanticModel.GetSymbolInfo(invocationNode).Symbol as IMethodSymbol; + if (methodSymbol is null) + return; - // Remove null message to avoid ambiguous calls. - if (argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfMessageParameter, out ArgumentSyntax? messageArgument) - && messageArgument.Expression.IsKind(SyntaxKind.NullLiteralExpression)) - { - argumentNamesToArguments.Remove(NUnitFrameworkConstants.NameOfMessageParameter); - } + var (argumentNamesToArguments, args) = SplitUpOtherParametersAndParamParameter(methodSymbol, invocationNode); // Now, replace the arguments. List newArguments = new(); @@ -75,6 +71,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) newArguments.Add(constraintArgument); // Do the format spec, params to formattable string conversion + argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfMessageParameter, out ArgumentSyntax? messageArgument); if (CodeFixHelper.GetInterpolatedMessageArgumentOrDefault(messageArgument, args) is ArgumentSyntax interpolatedMessageArgument) newArguments.Add(interpolatedMessageArgument); @@ -93,10 +90,9 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) diagnostic); (Dictionary argumentNamesToArguments, List args) SplitUpOtherParametersAndParamParameter( - SemanticModel semanticModel, + IMethodSymbol methodSymbol, InvocationExpressionSyntax invocationNode) { - var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(invocationNode).Symbol!; Dictionary argumentNamesToArguments = new(); // There can be 0 to any number of arguments mapped to args. diff --git a/src/nunit.analyzers/Helpers/CodeFixHelper.cs b/src/nunit.analyzers/Helpers/CodeFixHelper.cs index 40883e23..20f15269 100644 --- a/src/nunit.analyzers/Helpers/CodeFixHelper.cs +++ b/src/nunit.analyzers/Helpers/CodeFixHelper.cs @@ -55,11 +55,10 @@ internal static class CodeFixHelper return messageArgument; var formatSpecification = literalExpression.Token.ValueText; - - var argsExpression = args.Single(a => a != messageArgument).Expression; - var formatArgumentExpressions = argsExpression is ImplicitArrayCreationExpressionSyntax argsArrayExpression - ? argsArrayExpression.Initializer.Expressions.ToArray() - : new[] { argsExpression }; + var formatArgumentExpressions = + (args.Count == 1 && args[0].Expression is ImplicitArrayCreationExpressionSyntax argsArrayExpression) + ? argsArrayExpression.Initializer.Expressions + : args.Select(x => x.Expression); var interpolatedStringContent = UpdateStringFormatToFormattableString( formatSpecification, From 19e3e4035034a3323bdcd4c42bea59c472d2cccf Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Mon, 8 Apr 2024 22:15:58 -0400 Subject: [PATCH 10/17] Omit named parameters for the constraint model --- .../GreaterClassicModelAssertUsageCodeFixTests.cs | 2 +- ...erOrEqualClassicModelAssertUsageCodeFixTests.cs | 2 +- .../IsEmptyClassicModelAssertUsageCodeFixTests.cs | 2 +- ...eAndFalseClassicModelAssertUsageCodeFixTests.cs | 2 +- ...nstanceOfClassicModelAssertUsageCodeFixTests.cs | 2 +- .../IsNaNClassicModelAssertUsageCodeFixTests.cs | 2 +- ...sNotEmptyClassicModelAssertUsageCodeFixTests.cs | 2 +- ...nstanceOfClassicModelAssertUsageCodeFixTests.cs | 2 +- ...ndNotNullClassicModelAssertUsageCodeFixTests.cs | 2 +- ...llAndNullClassicModelAssertUsageCodeFixTests.cs | 2 +- ...ueAndTrueClassicModelAssertUsageCodeFixTests.cs | 2 +- .../LessClassicModelAssertUsageCodeFixTests.cs | 2 +- ...ssOrEqualClassicModelAssertUsageCodeFixTests.cs | 2 +- .../GreaterClassicModelAssertUsageCodeFix.cs | 14 ++++---------- ...GreaterOrEqualClassicModelAssertUsageCodeFix.cs | 14 ++++---------- .../IsEmptyClassicModelAssertUsageCodeFix.cs | 5 +---- ...sFalseAndFalseClassicModelAssertUsageCodeFix.cs | 6 +----- .../IsInstanceOfClassicModelAssertUsageCodeFix.cs | 11 ++--------- .../IsNaNClassicModelAssertUsageCodeFix.cs | 6 +----- .../IsNotEmptyClassicModelAssertUsageCodeFix.cs | 5 +---- ...sNotInstanceOfClassicModelAssertUsageCodeFix.cs | 10 ++-------- ...NullAndNotNullClassicModelAssertUsageCodeFix.cs | 9 +++------ .../IsNullAndNullClassicModelAssertUsageCodeFix.cs | 9 +++------ .../IsTrueAndTrueClassicModelAssertUsageCodeFix.cs | 9 +++------ .../LessClassicModelAssertUsageCodeFix.cs | 13 +++---------- .../LessOrEqualClassicModelAssertUsageCodeFix.cs | 12 +++--------- 26 files changed, 44 insertions(+), 105 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs index 3d0d94b3..7eae5605 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 2d, Is.GreaterThan(expected: 3d), $""{""first""}, {""second""}""); + Assert.That(2d, Is.GreaterThan(3d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs index ff33fb98..dc373672 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 2d, Is.GreaterThanOrEqualTo(expected: 3d), $""{""first""}, {""second""}""); + Assert.That(2d, Is.GreaterThanOrEqualTo(3d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs index 0480963b..a8c95c4a 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() { var collection = Array.Empty(); - Assert.That(actual: collection, Is.Empty, $""{""first""}, {""second""}""); + Assert.That(collection, Is.Empty, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs index ceac77df..042d7ccb 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs @@ -93,7 +93,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: false, Is.False, $""{""first""}, {""second""}""); + Assert.That(false, Is.False, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs index fc19fdf2..f31c79be 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -107,7 +107,7 @@ public void TestMethod() var expected = typeof(int); var actual = 42; - Assert.That(actual: actual, Is.InstanceOf(expectedType: expected), $""{""first""}, {""second""}""); + Assert.That(actual, Is.InstanceOf(expected), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs index 23f4f51c..3d8822b9 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() { var expr = double.NaN; - Assert.That(actual: expr, Is.NaN, $""{""first""}, {""second""}""); + Assert.That(expr, Is.NaN, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs index 4d9e85e3..f303b45d 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() { var collection = Array.Empty(); - Assert.That(actual: collection, Is.Not.Empty, $""{""first""}, {""second""}""); + Assert.That(collection, Is.Not.Empty, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs index 5d66e255..38e050b9 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -107,7 +107,7 @@ public void TestMethod() var expected = typeof(int); var actual = 42; - Assert.That(actual: actual, Is.Not.InstanceOf(expectedType: expected), $""{""first""}, {""second""}""); + Assert.That(actual, Is.Not.InstanceOf(expected), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs index c851b535..2bfa91b9 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs @@ -101,7 +101,7 @@ public void TestMethod() public void TestMethod() { object? obj = null; - Assert.That(actual: obj, Is.Not.Null, $""{""first""}, {""second""}""); + Assert.That(obj, Is.Not.Null, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs index 704d7006..2952605d 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs @@ -101,7 +101,7 @@ public void TestMethod() public void TestMethod() { object? obj = null; - Assert.That(actual: obj, Is.Null, $""{""first""}, {""second""}""); + Assert.That(obj, Is.Null, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs index f8ffb562..9b02c989 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs @@ -93,7 +93,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: true, Is.True, $""{""first""}, {""second""}""); + Assert.That(true, Is.True, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs index 774b03ef..a5d6d985 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 2d, Is.LessThan(expected: 3d), $""{""first""}, {""second""}""); + Assert.That(2d, Is.LessThan(3d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs index 23e5e076..80abaaf5 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs @@ -83,7 +83,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 2d, Is.LessThanOrEqualTo(expected: 3d), $""{""first""}, {""second""}""); + Assert.That(2d, Is.LessThanOrEqualTo(3d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs index a6f07587..425187e8 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs @@ -20,10 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; - var expectedArgumentNameColon = arg2.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedParameter); + var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -32,13 +29,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsGreaterThan))) .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( - arg2.WithNameColon(expectedArgumentNameColon))))); + arg2Argument.WithNameColon(null))))); - var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - var actualArgumentNameColon = arg1.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var actualArgument = arg1.WithNameColon(actualArgumentNameColon); + var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; + var actualArgument = arg1Argument.WithNameColon(null); return (actualArgument, constraintArgument); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs index 5e868c18..b60b116f 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs @@ -20,10 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; - var expectedArgumentNameColon = arg2.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedParameter); + var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -32,13 +29,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsGreaterThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( - arg2.WithNameColon(expectedArgumentNameColon))))); + arg2Argument.WithNameColon(null))))); - var arg1 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - var actualArgumentNameColon = arg1.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var actualArgument = arg1.WithNameColon(actualArgumentNameColon); + var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; + var actualArgument = arg1Argument.WithNameColon(null); return (actualArgument, constraintArgument); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs index 7fae66e7..2c393708 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs @@ -25,16 +25,13 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg var actualArgument = argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfCollectionParameter, out var collectionArgument) ? collectionArgument : argumentNamesToArguments[NUnitFrameworkConstants.NameOfAStringParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty))); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs index 224da970..a872c6f9 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs @@ -23,16 +23,12 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsFalse))); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs index 27702c1d..c36b6b0f 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs @@ -37,10 +37,6 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; - var expectedArgumentNameColon = expectedArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedTypeParameter); - var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -49,13 +45,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsInstanceOf))) .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( - expectedArgument.WithNameColon(expectedArgumentNameColon))))); + expectedArgument.WithNameColon(null))))); var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs index beed2199..70d78d4a 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs @@ -22,16 +22,12 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfADoubleParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNaN))); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs index 1ccf0629..4fca764a 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs @@ -24,9 +24,6 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg var actualArgument = argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfCollectionParameter, out var collectionArgument) ? collectionArgument : argumentNamesToArguments[NUnitFrameworkConstants.NameOfAStringParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( @@ -36,7 +33,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty))); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs index b915fc81..920f41cc 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs @@ -40,9 +40,6 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg IReadOnlyDictionary argumentNamesToArguments) { var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; - var expectedArgumentNameColon = expectedArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedTypeParameter); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -54,13 +51,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsInstanceOf))) .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( - expectedArgument.WithNameColon(expectedArgumentNameColon))))); + expectedArgument.WithNameColon(null))))); var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs index 8a8d08c5..a5f403fb 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs @@ -22,11 +22,6 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -35,7 +30,9 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull))); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs index 9a70db9f..f634e3b4 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs @@ -22,17 +22,14 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull))); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs index 1f837957..0e359dbb 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs @@ -22,17 +22,14 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; - var actualArgumentNameColon = actualArgument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsTrue))); - return (actualArgument.WithNameColon(actualArgumentNameColon), constraintArgument); + + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; + return (actualArgument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs index ba31805b..7c53fbba 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs @@ -20,10 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; - var expectedArgumentNameColon = arg2.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedParameter); + var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -32,14 +29,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThan))) .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( - arg2.WithNameColon(expectedArgumentNameColon))))); + arg2Argument.WithNameColon(null))))); var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - var actualArgumentNameColon = arg1Argument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var actualArgument = arg1Argument.WithNameColon(actualArgumentNameColon); - return (actualArgument, constraintArgument); + return (arg1Argument.WithNameColon(null), constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs index 4450e294..f61a40aa 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs @@ -20,10 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var arg2 = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; - var expectedArgumentNameColon = arg2.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfExpectedParameter); + var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -32,13 +29,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( - arg2.WithNameColon(expectedArgumentNameColon))))); + arg2Argument.WithNameColon(null))))); var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - var actualArgumentNameColon = arg1Argument.NameColon is null - ? null - : SyntaxFactory.NameColon(NUnitFrameworkConstants.NameOfActualParameter); - var actualArgument = arg1Argument.WithNameColon(actualArgumentNameColon); + var actualArgument = arg1Argument.WithNameColon(null); return (actualArgument, constraintArgument); } } From 0f92e74f27963d45fe9a294281c40b458a260f63 Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Mon, 8 Apr 2024 23:12:31 -0400 Subject: [PATCH 11/17] Add test case: two arguments for params in all ClassicAssert tests --- ...qualClassicModelAssertUsageCodeFixTests.cs | 58 +++++++++---------- ...qualClassicModelAssertUsageCodeFixTests.cs | 40 +++++++++---- ...SameClassicModelAssertUsageCodeFixTests.cs | 46 +++++++++++---- ...SameClassicModelAssertUsageCodeFixTests.cs | 46 +++++++++++---- ...ainsClassicModelAssertUsageCodeFixTests.cs | 46 +++++++++++---- ...aterClassicModelAssertUsageCodeFixTests.cs | 22 ++++++- ...qualClassicModelAssertUsageCodeFixTests.cs | 22 ++++++- ...mptyClassicModelAssertUsageCodeFixTests.cs | 24 +++++++- ...alseClassicModelAssertUsageCodeFixTests.cs | 23 +++++++- ...ceOfClassicModelAssertUsageCodeFixTests.cs | 44 ++++++++++++-- ...sNaNClassicModelAssertUsageCodeFixTests.cs | 24 +++++++- ...mptyClassicModelAssertUsageCodeFixTests.cs | 24 +++++++- ...ceOfClassicModelAssertUsageCodeFixTests.cs | 44 ++++++++++++-- ...NullClassicModelAssertUsageCodeFixTests.cs | 25 +++++++- ...NullClassicModelAssertUsageCodeFixTests.cs | 25 +++++++- ...TrueClassicModelAssertUsageCodeFixTests.cs | 23 +++++++- ...icModelAssertUsageCondensedCodeFixTests.cs | 24 +++++++- ...LessClassicModelAssertUsageCodeFixTests.cs | 20 ++++++- ...qualClassicModelAssertUsageCodeFixTests.cs | 20 ++++++- ...ZeroClassicModelAssertUsageCodeFixTests.cs | 24 +++++++- ...ZeroClassicModelAssertUsageCodeFixTests.cs | 24 +++++++- .../CollectionAssertUsageAnalyzerTests.cs | 43 ++++++++++++-- .../CollectionAssertUsageCodeFixTests.cs | 58 +++++++++++++++++-- 23 files changed, 622 insertions(+), 127 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs index cf89c5fc..aa26df33 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs @@ -27,11 +27,11 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyAreEqualFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreEqual(2d, 3d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -75,11 +75,11 @@ public void TestMethod() [Test] public void VerifyAreEqualFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreEqual(2d, 3d, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -105,13 +105,13 @@ public void TestMethod() } [Test] - public void VerifyAreEqualFixWithMessageAndParams() + public void VerifyAreEqualFixWithMessageAndOneArgumentForParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ - ↓ClassicAssert.AreEqual(2d, 3d, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + { + ↓ClassicAssert.AreEqual(2d, 3d, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -159,11 +159,11 @@ public void TestMethod() [Test] public void VerifyAreEqualFixWhenToleranceExists() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -175,11 +175,11 @@ public void TestMethod() [Test] public void VerifyAreEqualFixWhenToleranceExistsWithNamedArgument() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreEqual(expected: 2d, actual: 3d, delta: 0.0000001d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -191,11 +191,11 @@ public void TestMethod() [Test] public void VerifyAreEqualFixWhenToleranceExistsWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -221,13 +221,13 @@ public void TestMethod() } [Test] - public void VerifyAreEqualFixWhenToleranceExistsWithMessageAndParams() + public void VerifyAreEqualFixWhenToleranceExistsWithMessageAndOneArgumentForParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ - ↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + { + ↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -237,17 +237,17 @@ public void TestMethod() } [Test] - public void VerifyAreEqualFixWhenToleranceExistsWithMessageAndParamsLiteralExpressionSyntax() + public void VerifyAreEqualFixWhenToleranceExistsWithMessageAndTwoArgumentsForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - ↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message-id: {0}"", ""mystring""); + ↓ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""{0}, {1}"", ""first"", ""second""); }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d), $""message-id: {""mystring""}""); + Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -255,7 +255,7 @@ public void TestMethod() [Test] public void CodeFixPreservesLineBreakBeforeMessage() { - var code = TestUtility.WrapInTestMethod($@" + var code = TestUtility.WrapInTestMethod(@" ClassicAssert.AreEqual(2d, 3d, 0.0000001d, ""message"");"); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs index b14d9bd8..c49a2f6c 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs @@ -27,11 +27,11 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyAreNotEqualFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreNotEqual(2d, 3d); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -43,11 +43,11 @@ public void TestMethod() [Test] public void VerifyAreNotEqualFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { ↓ClassicAssert.AreNotEqual(2d, 3d, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -57,13 +57,13 @@ public void TestMethod() } [Test] - public void VerifyAreNotEqualFixWithMessageAndParams() + public void VerifyAreNotEqualFixWithMessageAndOneArgumentForParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ - ↓ClassicAssert.AreNotEqual(2d, 3d, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + { + ↓ClassicAssert.AreNotEqual(2d, 3d, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -73,7 +73,23 @@ public void TestMethod() } [Test] - public void VerifyAreNotEqualFixWithMessageAndParamsInNonstandardOrder() + public void VerifyAreNotEqualFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.AreNotEqual(2d, 3d, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(3d, Is.Not.EqualTo(2d), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyAreNotEqualFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs index 60681b24..c9cf7f85 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs @@ -27,14 +27,14 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyAreNotSameFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = new object(); var actual = new object(); ↓ClassicAssert.AreNotSame(expected, actual); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -49,14 +49,14 @@ public void TestMethod() [Test] public void VerifyAreNotSameFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = new object(); var actual = new object(); ↓ClassicAssert.AreNotSame(expected, actual, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -69,16 +69,16 @@ public void TestMethod() } [Test] - public void VerifyAreNotSameFixWithMessageAndParams() + public void VerifyAreNotSameFixWithMessageAndOneArgumentForParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = new object(); var actual = new object(); - ↓ClassicAssert.AreNotSame(expected, actual, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.AreNotSame(expected, actual, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -91,7 +91,29 @@ public void TestMethod() } [Test] - public void VerifyAreNotSameFixWithMessageAndParamsInNonstandardOrder() + public void VerifyAreNotSameFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = new object(); + var actual = new object(); + + ↓ClassicAssert.AreNotSame(expected, actual, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = new object(); + var actual = new object(); + + Assert.That(actual, Is.Not.SameAs(expected), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyAreNotSameFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs index 9cdfc487..6bfa0d7e 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs @@ -27,14 +27,14 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyAreSameFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = new object(); var actual = new object(); ↓ClassicAssert.AreSame(expected, actual); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -49,14 +49,14 @@ public void TestMethod() [Test] public void VerifyAreSameFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = new object(); var actual = new object(); ↓ClassicAssert.AreSame(expected, actual, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -69,16 +69,16 @@ public void TestMethod() } [Test] - public void VerifyAreSameFixWithMessageAndParams() + public void VerifyAreSameFixWithMessageAndOneArgumentForParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var expected = new object(); var actual = new object(); - ↓ClassicAssert.AreSame(expected, actual, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.AreSame(expected, actual, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -91,7 +91,29 @@ public void TestMethod() } [Test] - public void VerifyAreSameFixWithMessageAndParamsInNonstandardOrder() + public void VerifyAreSameFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = new object(); + var actual = new object(); + + ↓ClassicAssert.AreSame(expected, actual, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = new object(); + var actual = new object(); + + Assert.That(actual, Is.SameAs(expected), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyAreSameFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs index 4e1c18e0..afeb3dc8 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs @@ -27,14 +27,14 @@ public void VerifyGetFixableDiagnosticIds() [Test] public void VerifyContainsFix() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var instance = new object(); var collection = Array.Empty(); ↓ClassicAssert.Contains(instance, collection); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -49,14 +49,14 @@ public void TestMethod() [Test] public void VerifyContainsFixWithMessage() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var instance = new object(); var collection = Array.Empty(); ↓ClassicAssert.Contains(instance, collection, ""message""); - }}"); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -69,16 +69,16 @@ public void TestMethod() } [Test] - public void VerifyContainsFixWithMessageAndParams() + public void VerifyContainsFixWithMessageAndOneArgumentForParams() { - var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() - {{ + { var instance = new object(); var collection = Array.Empty(); - ↓ClassicAssert.Contains(instance, collection, ""message-id: {{0}}"", Guid.NewGuid()); - }}"); + ↓ClassicAssert.Contains(instance, collection, ""message-id: {0}"", Guid.NewGuid()); + }"); var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { @@ -91,7 +91,29 @@ public void TestMethod() } [Test] - public void VerifyContainsFixWithMessageAndParamsInNonstandardOrder() + public void VerifyContainsFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var instance = new object(); + var collection = Array.Empty(); + + ↓ClassicAssert.Contains(instance, collection, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var instance = new object(); + var collection = Array.Empty(); + + Assert.That(collection, Does.Contain(instance), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, instanceDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyContainsFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs index 7eae5605..13c4cb2d 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFixTests.cs @@ -57,7 +57,7 @@ public void TestMethod() } [Test] - public void VerifyGreaterFixWithMessageAndParams() + public void VerifyGreaterFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -73,7 +73,23 @@ public void TestMethod() } [Test] - public void VerifyGreaterFixWithMessageAndParamsInNonstandardOrder() + public void VerifyGreaterFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.Greater(2d, 3d, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(2d, Is.GreaterThan(3d), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyGreaterFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -91,7 +107,7 @@ public void TestMethod() [Test] public void CodeFixPreservesLineBreakBeforeMessage() { - var code = TestUtility.WrapInTestMethod($@" + var code = TestUtility.WrapInTestMethod(@" ClassicAssert.Greater(2d, 3d, ""message"");"); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs index dc373672..b5823959 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFixTests.cs @@ -57,7 +57,7 @@ public void TestMethod() } [Test] - public void VerifyGreaterOrEqualFixWithMessageAndParams() + public void VerifyGreaterOrEqualFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -73,7 +73,23 @@ public void TestMethod() } [Test] - public void VerifyGreaterOrEqualFixWithMessageAndParamsInNonstandardOrder() + public void VerifyGreaterOrEqualFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.GreaterOrEqual(2d, 3d, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(2d, Is.GreaterThanOrEqualTo(3d), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyGreaterOrEqualFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -91,7 +107,7 @@ public void TestMethod() [Test] public void CodeFixPreservesLineBreakBeforeMessage() { - var code = TestUtility.WrapInTestMethod($@" + var code = TestUtility.WrapInTestMethod(@" ClassicAssert.GreaterOrEqual(2d, 3d, ""message"");"); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs index a8c95c4a..756b3b47 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFixTests.cs @@ -65,7 +65,7 @@ public void TestMethod() } [Test] - public void VerifyIsEmptyFixWithMessageAndParams() + public void VerifyIsEmptyFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -85,7 +85,27 @@ public void TestMethod() } [Test] - public void VerifyIsEmptyFixWithMessageAndParamsInNonstandardOrder() + public void VerifyIsEmptyFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var collection = Array.Empty(); + + ↓ClassicAssert.IsEmpty(collection, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var collection = Array.Empty(); + + Assert.That(collection, Is.Empty, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyIsEmptyFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs index 042d7ccb..65a03a34 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFixTests.cs @@ -62,7 +62,7 @@ public void TestMethod() [TestCase("IsFalse", AnalyzerIdentifiers.IsFalseUsage)] [TestCase("False", AnalyzerIdentifiers.FalseUsage)] - public void VerifyIsFalseAndFalseFixesWithMessageAndParams(string assertion, string diagnosticId) + public void VerifyIsFalseAndFalseFixesWithMessageAndOneArgumentForParams(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); @@ -81,7 +81,26 @@ public void TestMethod() [TestCase("IsFalse", AnalyzerIdentifiers.IsFalseUsage)] [TestCase("False", AnalyzerIdentifiers.FalseUsage)] - public void VerifyIsFalseAndFalseFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + public void VerifyIsFalseAndFalseFixesWithMessageAndTwoArgumentsForParams(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + ↓ClassicAssert.{assertion}(false, ""{{0}}, {{1}}"", ""first"", ""second""); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(false, Is.False, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [TestCase("IsFalse", AnalyzerIdentifiers.IsFalseUsage)] + [TestCase("False", AnalyzerIdentifiers.FalseUsage)] + public void VerifyIsFalseAndFalseFixesWithMessageAndArrayParamsInNonstandardOrder(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs index f31c79be..ba2ca75f 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -69,7 +69,7 @@ public void TestMethod() } [Test] - public void VerifyIsInstanceOfFixWithMessageAndParams() + public void VerifyIsInstanceOfFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -91,7 +91,29 @@ public void TestMethod() } [Test] - public void VerifyIsInstanceOfFixWithMessageAndParamsInNonstandardOrder() + public void VerifyIsInstanceOfFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = typeof(int); + var actual = 42; + + ↓ClassicAssert.IsInstanceOf(expected, actual, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = typeof(int); + var actual = 42; + + Assert.That(actual, Is.InstanceOf(expected), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyIsInstanceOfFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -239,7 +261,7 @@ public void TestMethod() } [Test] - public void VerifyIsInstanceOfGenericFixWithMessageAndParams() + public void VerifyIsInstanceOfGenericFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapInTestMethod(@" var actual = 42; @@ -253,7 +275,21 @@ public void VerifyIsInstanceOfGenericFixWithMessageAndParams() } [Test] - public void VerifyIsInstanceOfGenericFixWithMessageAndParamsInNonstandardOrder() + public void VerifyIsInstanceOfGenericFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapInTestMethod(@" + var actual = 42; + + ↓ClassicAssert.IsInstanceOf(actual, ""{0}, {1}"", ""first"", ""second"");"); + var fixedCode = TestUtility.WrapInTestMethod(@" + var actual = 42; + + Assert.That(actual, Is.InstanceOf(), $""{""first""}, {""second""}"");"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyIsInstanceOfGenericFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapInTestMethod(@" var actual = 42; diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs index 3d8822b9..d474e249 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFixTests.cs @@ -65,7 +65,7 @@ public void TestMethod() } [Test] - public void VerifyIsNaNFixWithMessageAndParams() + public void VerifyIsNaNFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -85,7 +85,27 @@ public void TestMethod() } [Test] - public void VerifyIsNaNFixWithMessageAndParamsInNonstandardOrder() + public void VerifyIsNaNFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = double.NaN; + + ↓ClassicAssert.IsNaN(expr, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = double.NaN; + + Assert.That(expr, Is.NaN, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyIsNaNFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs index f303b45d..b410f513 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFixTests.cs @@ -65,7 +65,7 @@ public void TestMethod() } [Test] - public void VerifyIsNotEmptyFixWithMessageAndParams() + public void VerifyIsNotEmptyFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -85,7 +85,27 @@ public void TestMethod() } [Test] - public void VerifyIsNotEmptyFixWithMessageAndParamsInNonstandardOrder() + public void VerifyIsNotEmptyFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var collection = Array.Empty(); + + ↓ClassicAssert.IsNotEmpty(collection, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var collection = Array.Empty(); + + Assert.That(collection, Is.Not.Empty, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyIsNotEmptyFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs index 38e050b9..a2833620 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -69,7 +69,7 @@ public void TestMethod() } [Test] - public void VerifyIsNotInstanceOfFixWithMessageAndParams() + public void VerifyIsNotInstanceOfFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -91,7 +91,29 @@ public void TestMethod() } [Test] - public void VerifyIsNotInstanceOfFixWithMessageAndParamsInNonstandardOrder() + public void VerifyIsNotInstanceOfFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = typeof(int); + var actual = 42; + + ↓ClassicAssert.IsNotInstanceOf(expected, actual, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expected = typeof(int); + var actual = 42; + + Assert.That(actual, Is.Not.InstanceOf(expected), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyIsNotInstanceOfFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -239,7 +261,7 @@ public void TestMethod() } [Test] - public void VerifyIsNotInstanceOfGenericFixWithMessageAndParams() + public void VerifyIsNotInstanceOfGenericFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapInTestMethod(@" var actual = 42; @@ -253,7 +275,21 @@ public void VerifyIsNotInstanceOfGenericFixWithMessageAndParams() } [Test] - public void VerifyIsNotInstanceOfGenericFixWithMessageAndParamsInNonstandardOrder() + public void VerifyIsNotInstanceOfGenericFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapInTestMethod(@" + var actual = 42; + + ↓ClassicAssert.IsNotInstanceOf(actual, ""{0}, {1}"", ""first"", ""second"");"); + var fixedCode = TestUtility.WrapInTestMethod(@" + var actual = 42; + + Assert.That(actual, Is.Not.InstanceOf(), $""{""first""}, {""second""}"");"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyIsNotInstanceOfGenericFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapInTestMethod(@" var actual = 42; diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs index 2bfa91b9..f1c37fdc 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFixTests.cs @@ -66,7 +66,7 @@ public void TestMethod() [TestCase("IsNotNull", AnalyzerIdentifiers.IsNotNullUsage)] [TestCase("NotNull", AnalyzerIdentifiers.NotNullUsage)] - public void VerifyIsNotNullAndNotNullFixesWithMessageAndParams(string assertion, string diagnosticId) + public void VerifyIsNotNullAndNotNullFixesWithMessageAndOneArgumentForParams(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); @@ -87,7 +87,28 @@ public void TestMethod() [TestCase("IsNotNull", AnalyzerIdentifiers.IsNotNullUsage)] [TestCase("NotNull", AnalyzerIdentifiers.NotNullUsage)] - public void VerifyIsNotNullAndNotNullFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + public void VerifyIsNotNullAndNotNullFixesWithMessageAndTwoArgumentsForParams(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + object? obj = null; + ↓ClassicAssert.{assertion}(obj, ""{{0}}, {{1}}"", ""first"", ""second""); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + object? obj = null; + Assert.That(obj, Is.Not.Null, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [TestCase("IsNotNull", AnalyzerIdentifiers.IsNotNullUsage)] + [TestCase("NotNull", AnalyzerIdentifiers.NotNullUsage)] + public void VerifyIsNotNullAndNotNullFixesWithMessageAndArrayParamsInNonstandardOrder(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs index 2952605d..6c517696 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFixTests.cs @@ -66,7 +66,7 @@ public void TestMethod() [TestCase("IsNull", AnalyzerIdentifiers.IsNullUsage)] [TestCase("Null", AnalyzerIdentifiers.NullUsage)] - public void VerifyIsNullAndNullFixesWithMessageAndParams(string assertion, string diagnosticId) + public void VerifyIsNullAndNullFixesWithMessageAndOneArgumentForParams(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); @@ -87,7 +87,28 @@ public void TestMethod() [TestCase("IsNull", AnalyzerIdentifiers.IsNullUsage)] [TestCase("Null", AnalyzerIdentifiers.NullUsage)] - public void VerifyIsNullAndNullFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + public void VerifyIsNullAndNullFixesWithMessageAndTwoArgumentsForParams(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + object? obj = null; + ↓ClassicAssert.{assertion}(obj, ""{{0}}, {{1}}"", ""first"", ""second""); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + object? obj = null; + Assert.That(obj, Is.Null, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [TestCase("IsNull", AnalyzerIdentifiers.IsNullUsage)] + [TestCase("Null", AnalyzerIdentifiers.NullUsage)] + public void VerifyIsNullAndNullFixesWithMessageAndArrayParamsInNonstandardOrder(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs index 9b02c989..be20d208 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFixTests.cs @@ -62,7 +62,7 @@ public void TestMethod() [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] [TestCase("True", AnalyzerIdentifiers.TrueUsage)] - public void VerifyIsTrueAndTrueFixesWithMessageAndParams(string assertion, string diagnosticId) + public void VerifyIsTrueAndTrueFixesWithMessageAndOneArgumentForParams(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); @@ -81,7 +81,26 @@ public void TestMethod() [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] [TestCase("True", AnalyzerIdentifiers.TrueUsage)] - public void VerifyIsTrueAndTrueFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + public void VerifyIsTrueAndTrueFixesWithMessageAndTwoArgumentsForParams(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + ↓ClassicAssert.{assertion}(true, ""{{0}}, {{1}}"", ""first"", ""second""); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(true, Is.True, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] + [TestCase("True", AnalyzerIdentifiers.TrueUsage)] + public void VerifyIsTrueAndTrueFixesWithMessageAndArrayParamsInNonstandardOrder(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs index 70f935fc..a1949ecb 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs @@ -64,7 +64,7 @@ public void TestMethod() [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] [TestCase("True", AnalyzerIdentifiers.TrueUsage)] - public void VerifyIsTrueAndTrueFixesWithMessageAndParams(string assertion, string diagnosticId) + public void VerifyIsTrueAndTrueFixesWithMessageAndOneArgumentForParams(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); @@ -84,7 +84,27 @@ public void TestMethod() [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] [TestCase("True", AnalyzerIdentifiers.TrueUsage)] - public void VerifyIsTrueAndTrueFixesWithMessageAndParamsInNonstandardOrder(string assertion, string diagnosticId) + public void VerifyIsTrueAndTrueFixesWithMessageAndTwoArgumentsForParams(string assertion, string diagnosticId) + { + var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); + + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings($@" + public void TestMethod() + {{ + ↓ClassicAssert.{assertion}(true, ""{{0}}, {{1}}"", ""first"", ""second""); + }}"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(true, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, + fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription + IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.Suffix); + } + + [TestCase("IsTrue", AnalyzerIdentifiers.IsTrueUsage)] + [TestCase("True", AnalyzerIdentifiers.TrueUsage)] + public void VerifyIsTrueAndTrueFixesWithMessageAndArrayParamsInNonstandardOrder(string assertion, string diagnosticId) { var expectedDiagnostic = ExpectedDiagnostic.Create(diagnosticId); diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs index a5d6d985..f1198094 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFixTests.cs @@ -57,7 +57,7 @@ public void TestMethod() } [Test] - public void VerifyLessFixWithMessageAndParams() + public void VerifyLessFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -73,7 +73,23 @@ public void TestMethod() } [Test] - public void VerifyLessFixWithMessageAndParamsInNonstandardOrder() + public void VerifyLessFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.Less(2d, 3d, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(2d, Is.LessThan(3d), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyLessFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs index 80abaaf5..c54be504 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFixTests.cs @@ -57,7 +57,7 @@ public void TestMethod() } [Test] - public void VerifyLessOrEqualFixWithMessageAndParams() + public void VerifyLessOrEqualFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -73,7 +73,23 @@ public void TestMethod() } [Test] - public void VerifyLessOrEqualFixWithMessageAndParamsInNonstandardOrder() + public void VerifyLessOrEqualFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + ↓ClassicAssert.LessOrEqual(2d, 3d, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + Assert.That(2d, Is.LessThanOrEqualTo(3d), $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyLessOrEqualFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs index 4771fa5d..9f413331 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs @@ -65,7 +65,7 @@ public void TestMethod() } [Test] - public void VerifyNotZeroFixWithMessageAndParams() + public void VerifyNotZeroFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -85,7 +85,27 @@ public void TestMethod() } [Test] - public void VerifyNotZeroFixWithMessageAndParamsInNonstandardOrder() + public void VerifyNotZeroFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = default(int); + + ↓ClassicAssert.NotZero(expr, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = default(int); + + Assert.That(expr, Is.Not.Zero, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyNotZeroFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs index bcec0150..78afbbde 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs @@ -65,7 +65,7 @@ public void TestMethod() } [Test] - public void VerifyZeroFixWithMessageAndParams() + public void VerifyZeroFixWithMessageAndOneArgumentForParams() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() @@ -85,7 +85,27 @@ public void TestMethod() } [Test] - public void VerifyZeroFixWithMessageAndParamsInNonstandardOrder() + public void VerifyZeroFixWithMessageAndTwoArgumentsForParams() + { + var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = default(int); + + ↓ClassicAssert.Zero(expr, ""{0}, {1}"", ""first"", ""second""); + }"); + var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" + public void TestMethod() + { + var expr = default(int); + + Assert.That(expr, Is.Zero, $""{""first""}, {""second""}""); + }"); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); + } + + [Test] + public void VerifyZeroFixWithMessageAndArrayParamsInNonstandardOrder() { var code = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() diff --git a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageAnalyzerTests.cs b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageAnalyzerTests.cs index b2ae923d..96d13729 100644 --- a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageAnalyzerTests.cs @@ -30,7 +30,7 @@ public void AnalyzeOneCollectionWhenNoMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(OneCollectionParameterAsserts))] - public void AnalyzeOneCollectionWhenOnlyMessageArgumentsAreUsed(string method) + public void AnalyzeOneCollectionWhenOnlyMessageArgumentIsUsed(string method) { var testCode = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ 1, 2, 3 }}; @@ -40,7 +40,7 @@ public void AnalyzeOneCollectionWhenOnlyMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(OneCollectionParameterAsserts))] - public void AnalyzeOneCollectionWhenFormatAndParamsArgumentsAreUsed(string method) + public void AnalyzeOneCollectionWhenFormatAndOneParamsArgumentAreUsed(string method) { var testCode = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ 1, 2, 3 }}; @@ -49,6 +49,16 @@ public void AnalyzeOneCollectionWhenFormatAndParamsArgumentsAreUsed(string metho RoslynAssert.Diagnostics(this.analyzer, this.diagnostic, testCode); } + [TestCaseSource(nameof(OneCollectionParameterAsserts))] + public void AnalyzeOneCollectionWhenFormatAndTwoParamsArgumentsAreUsed(string method) + { + var testCode = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ 1, 2, 3 }}; + ↓CollectionAssert.{method}(collection, ""{{0}}, {{1}}"", ""first"", ""second""); + "); + RoslynAssert.Diagnostics(this.analyzer, this.diagnostic, testCode); + } + [TestCaseSource(nameof(TwoCollectionParameterAsserts))] public void AnalyzeTwoCollectionWhenNoMessageArgumentsAreUsed(string method) { @@ -61,7 +71,7 @@ public void AnalyzeTwoCollectionWhenNoMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(TwoCollectionParameterAsserts))] - public void AnalyzeTwoCollectionWhenOnlyMessageArgumentsAreUsed(string method) + public void AnalyzeTwoCollectionWhenOnlyMessageArgumentIsUsed(string method) { var testCode = TestUtility.WrapInTestMethod(@$" var collection1 = new[] {{ 1, 2, 3 }}; @@ -72,7 +82,7 @@ public void AnalyzeTwoCollectionWhenOnlyMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(TwoCollectionParameterAsserts))] - public void AnalyzeTwoCollectionWhenFormatAndParamsArgumentsAreUsed(string method) + public void AnalyzeTwoCollectionWhenFormatAndOneParamsArgumentAreUsed(string method) { var testCode = TestUtility.WrapInTestMethod(@$" var collection1 = new[] {{ 1, 2, 3 }}; @@ -82,6 +92,17 @@ public void AnalyzeTwoCollectionWhenFormatAndParamsArgumentsAreUsed(string metho RoslynAssert.Diagnostics(this.analyzer, this.diagnostic, testCode); } + [TestCaseSource(nameof(TwoCollectionParameterAsserts))] + public void AnalyzeTwoCollectionWhenFormatAndTwoParamsArgumentsAreUsed(string method) + { + var testCode = TestUtility.WrapInTestMethod(@$" + var collection1 = new[] {{ 1, 2, 3 }}; + var collection2 = new[] {{ 2, 4, 6 }}; + ↓CollectionAssert.{method}(collection1, collection2, ""{{0}}, {{1}}"", ""first"", ""second""); + "); + RoslynAssert.Diagnostics(this.analyzer, this.diagnostic, testCode); + } + [TestCase(NUnitFrameworkConstants.NameOfCollectionAssertAreEqual)] [TestCase(NUnitFrameworkConstants.NameOfCollectionAssertAreNotEqual)] public void AnalyzeTwoCollectionWithComparerWhenFormatAndParamsArgumentsAreUsed(string method) @@ -115,7 +136,7 @@ public void AnalyzeCollectionAndItemWhenNoMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(CollectionAndItemParameterAsserts))] - public void AnalyzeCollectionAndItemWhenOnlyMessageArgumentsAreUsed(string method) + public void AnalyzeCollectionAndItemWhenOnlyMessageArgumentIsUsed(string method) { var testCode = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ typeof(byte), typeof(char) }}; @@ -125,7 +146,7 @@ public void AnalyzeCollectionAndItemWhenOnlyMessageArgumentsAreUsed(string metho } [TestCaseSource(nameof(CollectionAndItemParameterAsserts))] - public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsed(string method) + public void AnalyzeCollectionAndItemWhenFormatAndOneParamsArgumentAreUsed(string method) { var testCode = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ typeof(byte), typeof(char) }}; @@ -133,5 +154,15 @@ public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsed(string m "); RoslynAssert.Diagnostics(this.analyzer, this.diagnostic, testCode); } + + [TestCaseSource(nameof(CollectionAndItemParameterAsserts))] + public void AnalyzeCollectionAndItemWhenFormatAndTwoParamsArgumentsAreUsed(string method) + { + var testCode = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ typeof(byte), typeof(char) }}; + ↓CollectionAssert.{method}(collection, typeof(byte), ""{{0}}, {{1}}"", ""first"", ""second""); + "); + RoslynAssert.Diagnostics(this.analyzer, this.diagnostic, testCode); + } } } diff --git a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs index 9cef1f59..847992bd 100644 --- a/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/CollectionAssertUsage/CollectionAssertUsageCodeFixTests.cs @@ -36,7 +36,7 @@ public void AnalyzeOneCollectionWhenNoMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(OneCollectionParameterAsserts))] - public void AnalyzeOneCollectionWhenOnlyMessageArgumentsAreUsed(string method) + public void AnalyzeOneCollectionWhenOnlyMessageArgumentIsUsed(string method) { var code = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ 1, 2, 3 }}; @@ -50,7 +50,7 @@ public void AnalyzeOneCollectionWhenOnlyMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(OneCollectionParameterAsserts))] - public void AnalyzeOneCollectionWhenFormatAndParamsArgumentsAreUsed(string method) + public void AnalyzeOneCollectionWhenFormatAndOneParamsArgumentAreUsed(string method) { var code = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ 1, 2, 3 }}; @@ -63,6 +63,20 @@ public void AnalyzeOneCollectionWhenFormatAndParamsArgumentsAreUsed(string metho RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } + [TestCaseSource(nameof(OneCollectionParameterAsserts))] + public void AnalyzeOneCollectionWhenFormatAndTwoParamsArgumentsAreUsed(string method) + { + var code = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ 1, 2, 3 }}; + ↓CollectionAssert.{method}(collection, ""{{0}}, {{1}}"", ""first"", ""second""); + "); + var fixedCode = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ 1, 2, 3 }}; + Assert.That(collection, {CollectionAssertUsageAnalyzer.OneCollectionParameterAsserts[method]}, $""{{""first""}}, {{""second""}}""); + "); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } + [TestCaseSource(nameof(OneCollectionParameterAsserts))] public void AnalyzeOneCollectionWhenFormatAndParamsArgumentsAreUsedOutOfOrder(string method) { @@ -110,7 +124,7 @@ public void AnalyzeTwoCollectionWhenNoMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(TwoCollectionParameterAsserts))] - public void AnalyzeTwoCollectionWhenOnlyMessageArgumentsAreUsed(string method) + public void AnalyzeTwoCollectionWhenOnlyMessageArgumentIsUsed(string method) { var code = TestUtility.WrapInTestMethod(@$" var collection1 = new[] {{ 1, 2, 3 }}; @@ -126,7 +140,7 @@ public void AnalyzeTwoCollectionWhenOnlyMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(TwoCollectionParameterAsserts))] - public void AnalyzeTwoCollectionWhenFormatAndParamsArgumentsAreUsed(string method) + public void AnalyzeTwoCollectionWhenFormatAndOneParamsArgumentAreUsed(string method) { var code = TestUtility.WrapInTestMethod(@$" var collection1 = new[] {{ 1, 2, 3 }}; @@ -141,6 +155,22 @@ public void AnalyzeTwoCollectionWhenFormatAndParamsArgumentsAreUsed(string metho RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } + [TestCaseSource(nameof(TwoCollectionParameterAsserts))] + public void AnalyzeTwoCollectionWhenFormatAndTwoParamsArgumentsAreUsed(string method) + { + var code = TestUtility.WrapInTestMethod(@$" + var collection1 = new[] {{ 1, 2, 3 }}; + var collection2 = new[] {{ 2, 4, 6 }}; + ↓CollectionAssert.{method}(collection1, collection2, ""{{0}}, {{1}}"", ""first"", ""second""); + "); + var fixedCode = TestUtility.WrapInTestMethod(@$" + var collection1 = new[] {{ 1, 2, 3 }}; + var collection2 = new[] {{ 2, 4, 6 }}; + Assert.That({GetAdjustedTwoCollectionConstraint(method)}, $""{{""first""}}, {{""second""}}""); + "); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } + [TestCaseSource(nameof(TwoCollectionParameterAsserts))] public void AnalyzeTwoCollectionWhenFormatAndParamsArgumentsAreUsedOutOfOrder(string method) { @@ -195,7 +225,7 @@ public void AnalyzeCollectionAndItemWhenNoMessageArgumentsAreUsed(string method) } [TestCaseSource(nameof(CollectionAndItemParameterAsserts))] - public void AnalyzeCollectionAndItemWhenOnlyMessageArgumentsAreUsed(string method) + public void AnalyzeCollectionAndItemWhenOnlyMessageArgumentIsUsed(string method) { var code = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ typeof(byte), typeof(char) }}; @@ -211,7 +241,7 @@ public void AnalyzeCollectionAndItemWhenOnlyMessageArgumentsAreUsed(string metho } [TestCaseSource(nameof(CollectionAndItemParameterAsserts))] - public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsed(string method) + public void AnalyzeCollectionAndItemWhenFormatAndOneParamsArgumentAreUsed(string method) { var code = TestUtility.WrapInTestMethod(@$" var collection = new[] {{ typeof(byte), typeof(char) }}; @@ -226,6 +256,22 @@ public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsed(string m RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } + [TestCaseSource(nameof(CollectionAndItemParameterAsserts))] + public void AnalyzeCollectionAndItemWhenFormatAndTwoParamsArgumentsAreUsed(string method) + { + var code = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ typeof(byte), typeof(char) }}; + var expected = typeof(byte); + ↓CollectionAssert.{method}(collection, expected, ""{{0}}, {{1}}"", ""first"", ""second""); + "); + var fixedCode = TestUtility.WrapInTestMethod(@$" + var collection = new[] {{ typeof(byte), typeof(char) }}; + var expected = typeof(byte); + Assert.That(collection, {CollectionAssertUsageAnalyzer.CollectionAndItemParameterAsserts[method]}, $""{{""first""}}, {{""second""}}""); + "); + RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); + } + [TestCaseSource(nameof(CollectionAndItemParameterAsserts))] public void AnalyzeCollectionAndItemWhenFormatAndParamsArgumentsAreUsedOutOfOrder(string method) { From c12275637da606bb3490419dded24f5b45300a9f Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Mon, 8 Apr 2024 23:23:57 -0400 Subject: [PATCH 12/17] Omit named parameters in StringAssertUsageCodeFix --- .../StringAssertUsageCodeFixTests.cs | 21 +++++++------------ .../StringAssertUsageCodeFix.cs | 4 ++-- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs index 3e73bb06..c1b3fd74 100644 --- a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs @@ -24,7 +24,7 @@ public void AnalyzeWhenNoArgumentsAreUsed(string method) ↓StringAssert.{method}(""expected"", ""actual""); "); var fixedCode = TestUtility.WrapInTestMethod(@$" - Assert.That(""actual"", {GetAdjustedConstraint(method, useNamedParameter: false)}); + Assert.That(""actual"", {GetAdjustedConstraint(method)}); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } @@ -36,7 +36,7 @@ public void AnalyzeWhenOnlyMessageArgumentIsUsed(string method) ↓StringAssert.{method}(""expected"", ""actual"", ""message""); "); var fixedCode = TestUtility.WrapInTestMethod(@$" - Assert.That(""actual"", {GetAdjustedConstraint(method, useNamedParameter: false)}, ""message""); + Assert.That(""actual"", {GetAdjustedConstraint(method)}, ""message""); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } @@ -48,7 +48,7 @@ public void AnalyzeWhenFormatAndArgumentsAreUsed(string method) ↓StringAssert.{method}(""expected"", ""actual"", ""Because of {{0}}"", ""message""); "); var fixedCode = TestUtility.WrapInTestMethod(@$" - Assert.That(""actual"", {GetAdjustedConstraint(method, useNamedParameter: false)}, $""Because of {{""message""}}""); + Assert.That(""actual"", {GetAdjustedConstraint(method)}, $""Because of {{""message""}}""); "); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } @@ -60,19 +60,12 @@ public void AnalyzeWhenFormatAndArgumentsAreUsedOutOfOrder(string method) var code = TestUtility.WrapInTestMethod(@$" ↓StringAssert.{method}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", actual: ""actual"", {firstParameterName}: ""expected"");"); var fixedCode = TestUtility.WrapInTestMethod(@$" - Assert.That(actual: ""actual"", {GetAdjustedConstraint(method, useNamedParameter: true)}, $""{{""first""}}, {{""second""}}"");"); + Assert.That(""actual"", {GetAdjustedConstraint(method)}, $""{{""first""}}, {{""second""}}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); } - private static string GetAdjustedConstraint(string method, bool useNamedParameter) - { - const string Expected = "expected"; - - var expectedParameterName = StringAssertUsageCodeFix.StringAssertToExpectedParameterName[method]; - return StringAssertUsageAnalyzer.StringAssertToConstraint[method] - .Replace( - Expected, - useNamedParameter ? $"{expectedParameterName}: \"{Expected}\"" : $"\"{Expected}\""); - } + private static string GetAdjustedConstraint(string method) => + StringAssertUsageAnalyzer.StringAssertToConstraint[method] + .Replace("expected", "\"expected\""); } } diff --git a/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs b/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs index 4bc12350..034798f0 100644 --- a/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs @@ -57,10 +57,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg var expectedParameterName = StringAssertToExpectedParameterName[methodName]; var expectedArgument = argumentNamesToArguments[expectedParameterName]; var constraints = StringAssertToConstraints[methodName]; - var constraintArgument = Argument(constraints.CreateConstraint(expectedArgument)); + var constraintArgument = Argument(constraints.CreateConstraint(expectedArgument.WithNameColon(null))); var actualArgument = argumentNamesToArguments[NameOfActualParameter]; - return (actualArgument, constraintArgument); + return (actualArgument.WithNameColon(null), constraintArgument); } } } From f99187e3d90e1ecf750a2c06e85b1e0ca9d52b29 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Tue, 9 Apr 2024 17:33:51 +0800 Subject: [PATCH 13/17] Consistently use WithNameColon Update Tests to remove named parameters --- ...qualClassicModelAssertUsageCodeFixTests.cs | 10 +++++----- ...qualClassicModelAssertUsageCodeFixTests.cs | 2 +- ...SameClassicModelAssertUsageCodeFixTests.cs | 2 +- ...SameClassicModelAssertUsageCodeFixTests.cs | 2 +- ...ainsClassicModelAssertUsageCodeFixTests.cs | 2 +- ...ceOfClassicModelAssertUsageCodeFixTests.cs | 2 +- ...ceOfClassicModelAssertUsageCodeFixTests.cs | 2 +- ...ZeroClassicModelAssertUsageCodeFixTests.cs | 2 +- ...ZeroClassicModelAssertUsageCodeFixTests.cs | 2 +- .../AreEqualClassicModelAssertUsageCodeFix.cs | 4 ++-- ...eNotEqualClassicModelAssertUsageCodeFix.cs | 4 ++-- ...reNotSameClassicModelAssertUsageCodeFix.cs | 4 ++-- .../AreSameClassicModelAssertUsageCodeFix.cs | 4 ++-- .../ContainsClassicModelAssertUsageCodeFix.cs | 4 ++-- .../GreaterClassicModelAssertUsageCodeFix.cs | 10 ++++------ ...erOrEqualClassicModelAssertUsageCodeFix.cs | 10 ++++------ .../IsEmptyClassicModelAssertUsageCodeFix.cs | 3 ++- ...eAndFalseClassicModelAssertUsageCodeFix.cs | 4 ++-- ...nstanceOfClassicModelAssertUsageCodeFix.cs | 11 +++++----- .../IsNaNClassicModelAssertUsageCodeFix.cs | 4 ++-- ...sNotEmptyClassicModelAssertUsageCodeFix.cs | 3 ++- ...nstanceOfClassicModelAssertUsageCodeFix.cs | 11 +++++----- ...ndNotNullClassicModelAssertUsageCodeFix.cs | 4 ++-- ...llAndNullClassicModelAssertUsageCodeFix.cs | 4 ++-- ...ueAndTrueClassicModelAssertUsageCodeFix.cs | 4 ++-- .../LessClassicModelAssertUsageCodeFix.cs | 9 ++++----- ...ssOrEqualClassicModelAssertUsageCodeFix.cs | 9 ++++----- .../NotZeroClassicModelAssertUsageCodeFix.cs | 2 +- .../ZeroClassicModelAssertUsageCodeFix.cs | 2 +- .../CollectionAssertUsageCodeFix.cs | 20 ++++++++----------- .../StringAssertUsageCodeFix.cs | 8 ++++---- 31 files changed, 77 insertions(+), 87 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs index aa26df33..b3fb803d 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFixTests.cs @@ -51,7 +51,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 3d, Is.EqualTo(expected: 2d).Within(0.0000001d)); + Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d)); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -67,7 +67,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 3d, Is.EqualTo(expected: 2d)); + Assert.That(3d, Is.EqualTo(2d)); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -133,7 +133,7 @@ public void TestMethod() public void TestMethod() { var actual = 3d; - Assert.That(actual: actual, Is.EqualTo(expected: 2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid().ToString()}""); + Assert.That(actual, Is.EqualTo(2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid().ToString()}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -151,7 +151,7 @@ public void TestMethod() public void TestMethod() { var actual = 3d; - Assert.That(actual: actual, Is.EqualTo(expected: 2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid()}""); + Assert.That(actual, Is.EqualTo(2d).Within(0.0000001d), $""message-id: {""Guid.NewGuid()""}, {Guid.NewGuid()}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } @@ -183,7 +183,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: 3d, Is.EqualTo(expected: 2d).Within(0.0000001d)); + Assert.That(3d, Is.EqualTo(2d).Within(0.0000001d)); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs index c49a2f6c..e49697fa 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFixTests.cs @@ -99,7 +99,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(actual: ""actual"", Is.Not.EqualTo(expected: ""expected""), $""{""first""}, {""second""}""); + Assert.That(""actual"", Is.Not.EqualTo(""expected""), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs index c9cf7f85..25e26060 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFixTests.cs @@ -129,7 +129,7 @@ public void TestMethod() var expected = new object(); var actual = new object(); - Assert.That(actual: actual, Is.Not.SameAs(expected: expected), $""{""first""}, {""second""}""); + Assert.That(actual, Is.Not.SameAs(expected), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs index 6bfa0d7e..ca0dc975 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFixTests.cs @@ -129,7 +129,7 @@ public void TestMethod() var expected = new object(); var actual = new object(); - Assert.That(actual: actual, Is.SameAs(expected: expected), $""{""first""}, {""second""}""); + Assert.That(actual, Is.SameAs(expected), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs index afeb3dc8..2e137c37 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFixTests.cs @@ -129,7 +129,7 @@ public void TestMethod() var instance = new object(); var collection = Array.Empty(); - Assert.That(actual: collection, Does.Contain(expected: instance), $""{""first""}, {""second""}""); + Assert.That(collection, Does.Contain(instance), $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, instanceDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs index ba2ca75f..9c26ada2 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -298,7 +298,7 @@ public void VerifyIsInstanceOfGenericFixWithMessageAndArrayParamsInNonstandardOr var fixedCode = TestUtility.WrapInTestMethod(@" var actual = 42; - Assert.That(actual: actual, Is.InstanceOf(), $""{""first""}, {""second""}"");"); + Assert.That(actual, Is.InstanceOf(), $""{""first""}, {""second""}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs index a2833620..259cd1bd 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFixTests.cs @@ -298,7 +298,7 @@ public void VerifyIsNotInstanceOfGenericFixWithMessageAndArrayParamsInNonstandar var fixedCode = TestUtility.WrapInTestMethod(@" var actual = 42; - Assert.That(actual: actual, Is.Not.InstanceOf(), $""{""first""}, {""second""}"");"); + Assert.That(actual, Is.Not.InstanceOf(), $""{""first""}, {""second""}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs index 9f413331..d05fbe00 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFixTests.cs @@ -119,7 +119,7 @@ public void TestMethod() { var expr = default(int); - Assert.That(actual: expr, Is.Not.Zero, $""{""first""}, {""second""}""); + Assert.That(expr, Is.Not.Zero, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs index 78afbbde..5b8fdbe2 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFixTests.cs @@ -119,7 +119,7 @@ public void TestMethod() { var expr = default(int); - Assert.That(actual: expr, Is.Zero, $""{""first""}, {""second""}""); + Assert.That(expr, Is.Zero, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription); } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs index 44595226..fc46671b 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreEqualClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter].WithNameColon(null); var equalToInvocationNode = SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, @@ -45,7 +45,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.SingletonSeparatedList(toleranceArgumentNoColon))); } - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); return (actualArgument, SyntaxFactory.Argument(equalToInvocationNode)); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFix.cs index 9b5985dd..55c2f91d 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreNotEqualClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -33,7 +33,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); return (actualArgument, constraintArgument); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFix.cs index b4964b5f..f632ea51 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreNotSameClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -33,7 +33,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); return (actualArgument, constraintArgument); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFix.cs index 27520f93..d1340ab1 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/AreSameClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -30,7 +30,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); return (actualArgument, constraintArgument); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFix.cs index b6fc8acf..94876d81 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ContainsClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -30,7 +30,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); return (actualArgument, constraintArgument); } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs index 425187e8..2045d853 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -28,12 +28,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsGreaterThan))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList( - arg2Argument.WithNameColon(null))))); + SyntaxFactory.SingletonSeparatedList(arg2Argument)))); - var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - var actualArgument = arg1Argument.WithNameColon(null); - return (actualArgument, constraintArgument); + var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter].WithNameColon(null); + return (arg1Argument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs index b60b116f..0fab1795 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/GreaterOrEqualClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -28,12 +28,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsGreaterThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList( - arg2Argument.WithNameColon(null))))); + SyntaxFactory.SingletonSeparatedList(arg2Argument)))); - var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - var actualArgument = arg1Argument.WithNameColon(null); - return (actualArgument, constraintArgument); + var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter].WithNameColon(null); + return (arg1Argument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs index 2c393708..3adb8b76 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsEmptyClassicModelAssertUsageCodeFix.cs @@ -25,13 +25,14 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg var actualArgument = argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfCollectionParameter, out var collectionArgument) ? collectionArgument : argumentNamesToArguments[NUnitFrameworkConstants.NameOfAStringParameter]; + actualArgument = actualArgument.WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty))); - return (actualArgument.WithNameColon(null), constraintArgument); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs index a872c6f9..e52debad 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsFalseAndFalseClassicModelAssertUsageCodeFix.cs @@ -22,13 +22,13 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsFalse))); - return (actualArgument.WithNameColon(null), constraintArgument); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs index c36b6b0f..dc3a59ae 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsInstanceOfClassicModelAssertUsageCodeFix.cs @@ -28,7 +28,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgu SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.GenericName(NUnitFrameworkConstants.NameOfIsInstanceOf) .WithTypeArgumentList(typeArguments)))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); return (actualArgument, constraintArgument); } @@ -36,7 +36,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -44,11 +44,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsInstanceOf))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList( - expectedArgument.WithNameColon(null))))); + SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; - return (actualArgument.WithNameColon(null), constraintArgument); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs index 70d78d4a..81295efc 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNaNClassicModelAssertUsageCodeFix.cs @@ -21,13 +21,13 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfADoubleParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfADoubleParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNaN))); - return (actualArgument.WithNameColon(null), constraintArgument); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs index 4fca764a..3f0a7724 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotEmptyClassicModelAssertUsageCodeFix.cs @@ -24,6 +24,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg var actualArgument = argumentNamesToArguments.TryGetValue(NUnitFrameworkConstants.NameOfCollectionParameter, out var collectionArgument) ? collectionArgument : argumentNamesToArguments[NUnitFrameworkConstants.NameOfAStringParameter]; + actualArgument = actualArgument.WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( @@ -33,7 +34,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsEmpty))); - return (actualArgument.WithNameColon(null), constraintArgument); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs index 920f41cc..39cb57c1 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotInstanceOfClassicModelAssertUsageCodeFix.cs @@ -31,7 +31,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax ConstraintArgu SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.GenericName(NUnitFrameworkConstants.NameOfIsInstanceOf) .WithTypeArgumentList(typeArguments)))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); return (actualArgument, constraintArgument); } @@ -39,7 +39,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter]; + var expectedArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfExpectedParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -50,11 +50,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsInstanceOf))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList( - expectedArgument.WithNameColon(null))))); + SyntaxFactory.SingletonSeparatedList(expectedArgument)))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; - return (actualArgument.WithNameColon(null), constraintArgument); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs index a5f403fb..8d5f9ea9 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNotNullAndNotNullClassicModelAssertUsageCodeFix.cs @@ -31,8 +31,8 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNot)), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; - return (actualArgument.WithNameColon(null), constraintArgument); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter].WithNameColon(null); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs index f634e3b4..908ee2cb 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsNullAndNullClassicModelAssertUsageCodeFix.cs @@ -28,8 +28,8 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsNull))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter]; - return (actualArgument.WithNameColon(null), constraintArgument); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfAnObjectParameter].WithNameColon(null); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs index 0e359dbb..2eb25e67 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCodeFix.cs @@ -28,8 +28,8 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsTrue))); - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; - return (actualArgument.WithNameColon(null), constraintArgument); + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter].WithNameColon(null); + return (actualArgument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs index 7c53fbba..e0cb12ca 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -28,11 +28,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThan))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList( - arg2Argument.WithNameColon(null))))); + SyntaxFactory.SingletonSeparatedList(arg2Argument)))); - var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - return (arg1Argument.WithNameColon(null), constraintArgument); + var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter].WithNameColon(null); + return (arg1Argument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs index f61a40aa..abf5e6ca 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs @@ -20,7 +20,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter]; + var arg2Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg2Parameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( @@ -29,11 +29,10 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( - arg2Argument.WithNameColon(null))))); + arg2Argument)))); - var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter]; - var actualArgument = arg1Argument.WithNameColon(null); - return (actualArgument, constraintArgument); + var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter].WithNameColon(null); + return (arg1Argument, constraintArgument); } } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFix.cs index 97562884..c76db4cf 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/NotZeroClassicModelAssertUsageCodeFix.cs @@ -21,7 +21,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFix.cs index 158006b3..4eb547d9 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ZeroClassicModelAssertUsageCodeFix.cs @@ -21,7 +21,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfActualParameter].WithNameColon(null); var constraintArgument = SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, diff --git a/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs b/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs index 13e7679f..043bc667 100644 --- a/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageCodeFix.cs @@ -124,31 +124,27 @@ private static (ArgumentSyntax actualArgument, ArgumentSyntax constraintArgument { var methodName = diagnostic.Properties[AnalyzerPropertyKeys.ModelName]!; var firstParameterName = CollectionAssertToFirstParameterName[methodName]; - var firstArgument = argumentNamesToArguments[firstParameterName]; + var firstArgument = argumentNamesToArguments[firstParameterName].WithNameColon(null); if (CollectionAssertToParameterlessConstraints.TryGetValue(methodName, out Constraints? constraints)) { - var actualArgument = firstArgument.WithNameColon(null); var constraintArgument = Argument(constraints.CreateConstraint()); - return (actualArgument, constraintArgument); + return (firstArgument, constraintArgument); } else if (CollectionAssertToOneSwappedParameterConstraints.TryGetValue(methodName, out constraints)) { var secondParameterName = CollectionAssertToSecondParameterName[methodName]; - var secondArgument = argumentNamesToArguments[secondParameterName]; - var actualArgument = secondArgument.WithNameColon(null); - - var constraintArgument = Argument(constraints.CreateConstraint(firstArgument.WithNameColon(null))); - return (actualArgument, constraintArgument); + var secondArgument = argumentNamesToArguments[secondParameterName].WithNameColon(null); + var constraintArgument = Argument(constraints.CreateConstraint(firstArgument)); + return (secondArgument, constraintArgument); } else if (CollectionAssertToOneUnswappedParameterConstraints.TryGetValue(methodName, out constraints)) { var secondParameterName = CollectionAssertToSecondParameterName[methodName]; - var secondArgument = argumentNamesToArguments[secondParameterName]; - var constraintArgument = Argument(constraints.CreateConstraint(secondArgument.WithNameColon(null))); + var secondArgument = argumentNamesToArguments[secondParameterName].WithNameColon(null); + var constraintArgument = Argument(constraints.CreateConstraint(secondArgument)); - var actualArgument = firstArgument.WithNameColon(null); - return (actualArgument, constraintArgument); + return (firstArgument, constraintArgument); } else { diff --git a/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs b/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs index 034798f0..672d73dd 100644 --- a/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/StringAssertUsage/StringAssertUsageCodeFix.cs @@ -55,12 +55,12 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg { var methodName = diagnostic.Properties[AnalyzerPropertyKeys.ModelName]!; var expectedParameterName = StringAssertToExpectedParameterName[methodName]; - var expectedArgument = argumentNamesToArguments[expectedParameterName]; + var expectedArgument = argumentNamesToArguments[expectedParameterName].WithNameColon(null); var constraints = StringAssertToConstraints[methodName]; - var constraintArgument = Argument(constraints.CreateConstraint(expectedArgument.WithNameColon(null))); + var constraintArgument = Argument(constraints.CreateConstraint(expectedArgument)); - var actualArgument = argumentNamesToArguments[NameOfActualParameter]; - return (actualArgument.WithNameColon(null), constraintArgument); + var actualArgument = argumentNamesToArguments[NameOfActualParameter].WithNameColon(null); + return (actualArgument, constraintArgument); } } } From cd0bec35f5bb719faf75f4dd26abdb881dc107c6 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Wed, 10 Apr 2024 08:15:22 +0800 Subject: [PATCH 14/17] Code review changes --- ...sTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs | 2 +- .../StringAssertUsage/StringAssertUsageCodeFixTests.cs | 2 +- .../IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs | 2 +- .../LessOrEqualClassicModelAssertUsageCodeFix.cs | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs index a1949ecb..2f840b1d 100644 --- a/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs +++ b/src/nunit.analyzers.tests/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFixTests.cs @@ -116,7 +116,7 @@ public void TestMethod() var fixedCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@" public void TestMethod() { - Assert.That(condition: true, $""{""first""}, {""second""}""); + Assert.That(true, $""{""first""}, {""second""}""); }"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode, fixTitle: ClassicModelAssertUsageCodeFix.TransformToConstraintModelDescription + IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.Suffix); diff --git a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs index c1b3fd74..6e65380d 100644 --- a/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs +++ b/src/nunit.analyzers.tests/StringAssertUsage/StringAssertUsageCodeFixTests.cs @@ -58,7 +58,7 @@ public void AnalyzeWhenFormatAndArgumentsAreUsedOutOfOrder(string method) { var firstParameterName = StringAssertUsageCodeFix.StringAssertToExpectedParameterName[method]; var code = TestUtility.WrapInTestMethod(@$" - ↓StringAssert.{method}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", actual: ""actual"", {firstParameterName}: ""expected"");"); + ↓StringAssert.{method}(args: new[] {{ ""first"", ""second"" }}, message: ""{{0}}, {{1}}"", actual: ""actual"", {firstParameterName}: ""expected"");"); var fixedCode = TestUtility.WrapInTestMethod(@$" Assert.That(""actual"", {GetAdjustedConstraint(method)}, $""{{""first""}}, {{""second""}}"");"); RoslynAssert.CodeFix(analyzer, fix, expectedDiagnostic, code, fixedCode); diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs index 71ba76a4..e2401aba 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/IsTrueAndTrueClassicModelAssertUsageCondensedCodeFix.cs @@ -27,7 +27,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg Diagnostic diagnostic, IReadOnlyDictionary argumentNamesToArguments) { - var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter]; + var actualArgument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfConditionParameter].WithNameColon(null); return (actualArgument, null); // The condensed form doesn't have the constraint argument. } } diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs index abf5e6ca..4c69a932 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/LessOrEqualClassicModelAssertUsageCodeFix.cs @@ -28,8 +28,7 @@ protected override (ArgumentSyntax ActualArgument, ArgumentSyntax? ConstraintArg SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIsLessThanOrEqualTo))) .WithArgumentList(SyntaxFactory.ArgumentList( - SyntaxFactory.SingletonSeparatedList( - arg2Argument)))); + SyntaxFactory.SingletonSeparatedList(arg2Argument)))); var arg1Argument = argumentNamesToArguments[NUnitFrameworkConstants.NameOfArg1Parameter].WithNameColon(null); return (arg1Argument, constraintArgument); From 7700c2069781879efbd1eba7dd4f5bf16a32e721 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Wed, 10 Apr 2024 12:15:24 +0800 Subject: [PATCH 15/17] We no longer need HasToleranceValue --- .../ClassicModelAssertUsageAnalyzer.cs | 4 ---- src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs | 1 - 2 files changed, 5 deletions(-) diff --git a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageAnalyzer.cs b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageAnalyzer.cs index 60213141..c2d479b3 100644 --- a/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageAnalyzer.cs +++ b/src/nunit.analyzers/ClassicModelAssertUsage/ClassicModelAssertUsageAnalyzer.cs @@ -252,10 +252,6 @@ protected override void AnalyzeAssertInvocation(OperationAnalysisContext context return new Dictionary { [AnalyzerPropertyKeys.ModelName] = invocationSymbol.Name, - [AnalyzerPropertyKeys.HasToleranceValue] = - (invocationSymbol.Name == NameOfAssertAreEqual && - invocationSymbol.Parameters.Length >= 3 && - invocationSymbol.Parameters[2].Type.SpecialType == SpecialType.System_Double).ToString(), }.ToImmutableDictionary(); } } diff --git a/src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs b/src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs index 0a4e1056..c0882331 100644 --- a/src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs +++ b/src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs @@ -2,7 +2,6 @@ namespace NUnit.Analyzers.Constants { internal static class AnalyzerPropertyKeys { - internal const string HasToleranceValue = nameof(AnalyzerPropertyKeys.HasToleranceValue); internal const string ModelName = nameof(AnalyzerPropertyKeys.ModelName); internal const string MinimumNumberOfArguments = nameof(AnalyzerPropertyKeys.MinimumNumberOfArguments); internal const string ComparerParameterIndex = nameof(AnalyzerPropertyKeys.ComparerParameterIndex); From cb1df204b06c0aa3aae7e0a269a62fcd989bbcec Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Wed, 10 Apr 2024 13:16:50 +0800 Subject: [PATCH 16/17] We no longer need ComparerParameterIndex --- .../CollectionAssertUsageAnalyzer.cs | 10 ---------- src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs | 1 - 2 files changed, 11 deletions(-) diff --git a/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageAnalyzer.cs b/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageAnalyzer.cs index 6cd1bb4a..b4504399 100644 --- a/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageAnalyzer.cs +++ b/src/nunit.analyzers/CollectionAssertUsage/CollectionAssertUsageAnalyzer.cs @@ -67,26 +67,16 @@ protected override void AnalyzeAssertInvocation(OperationAnalysisContext context TwoCollectionParameterAsserts.TryGetValue(methodSymbol.Name, out constraint) || CollectionAndItemParameterAsserts.TryGetValue(methodSymbol.Name, out constraint)) { - var parameters = methodSymbol.Parameters; - string comparerParameterIndex = parameters.Length > 1 && IsIComparer(parameters[1]) ? "1" : - (parameters.Length > 2 && IsIComparer(parameters[2]) ? "2" : "0"); - context.ReportDiagnostic(Diagnostic.Create( collectionAssertDescriptor, assertOperation.Syntax.GetLocation(), new Dictionary { [AnalyzerPropertyKeys.ModelName] = methodSymbol.Name, - [AnalyzerPropertyKeys.ComparerParameterIndex] = comparerParameterIndex, }.ToImmutableDictionary(), constraint, methodSymbol.Name)); } } - - private static bool IsIComparer(IParameterSymbol parameterSymbol) - { - return parameterSymbol.Type.Name == "IComparer"; - } } } diff --git a/src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs b/src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs index c0882331..10b5bdc0 100644 --- a/src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs +++ b/src/nunit.analyzers/Constants/AnalyzerPropertyKeys.cs @@ -4,6 +4,5 @@ internal static class AnalyzerPropertyKeys { internal const string ModelName = nameof(AnalyzerPropertyKeys.ModelName); internal const string MinimumNumberOfArguments = nameof(AnalyzerPropertyKeys.MinimumNumberOfArguments); - internal const string ComparerParameterIndex = nameof(AnalyzerPropertyKeys.ComparerParameterIndex); } } From 7beabc00a378e3c774339f3b8aaed2ae23c20a83 Mon Sep 17 00:00:00 2001 From: Jihoon Park Date: Fri, 12 Apr 2024 17:42:00 -0400 Subject: [PATCH 17/17] Update param tags for GetInterpolatedMessageArgumentOrDefault --- src/nunit.analyzers/Helpers/CodeFixHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nunit.analyzers/Helpers/CodeFixHelper.cs b/src/nunit.analyzers/Helpers/CodeFixHelper.cs index 20f15269..0fe35241 100644 --- a/src/nunit.analyzers/Helpers/CodeFixHelper.cs +++ b/src/nunit.analyzers/Helpers/CodeFixHelper.cs @@ -39,8 +39,8 @@ internal static class CodeFixHelper /// This is assumed to be arguments for an 'Assert.That(actual, constraint, "...: {0} - {1}", param0, param1)` /// which needs converting into 'Assert.That(actual, constraint, $"...: {param0} - {param1}"). /// - /// The arguments passed to the 'Assert' method. - /// The argument needed for the actual method, any more are assumed messages. + /// The argument that corresponds to the composite format string. + /// The list of arguments that correspond to format items. public static ArgumentSyntax? GetInterpolatedMessageArgumentOrDefault(ArgumentSyntax? messageArgument, List args) { if (messageArgument is null)