Skip to content

Commit

Permalink
feat: add nunit Assert.Conditions (#298)
Browse files Browse the repository at this point in the history
* feat: add nunit IsNaN tests
* feat: add nunit IsEmpty & IsNotEmpty tests
* feat: add nunit contains assertions
  • Loading branch information
Meir017 authored Jan 24, 2024
1 parent ed6aec1 commit 2c718db
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 9 deletions.
326 changes: 326 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,332 @@ public class NunitTests
[Implemented]
public void Nunit4_AssertNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object actual", oldAssertion, newAssertion);

[DataTestMethod]
[AssertionDiagnostic("Assert.IsNaN(actual{0});")]
[Implemented]
public void Nunit3_AssertIsNaN_TestAnalyzer(string assertion)
{
Nunit3VerifyDiagnostic("double actual", assertion);
Nunit3VerifyNoFix("double actual", assertion);
}

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.IsNaN(actual{0});")]
[Implemented]
public void Nunit4_AssertIsNaN_TestAnalyzer(string assertion)
{
Nunit4VerifyDiagnostic("double actual", assertion);
Nunit4VerifyNoFix("double actual", assertion);
}

// IsEmpty
[DataTestMethod]
[AssertionDiagnostic("Assert.IsEmpty(actual{0});")]
[Implemented]
public void Nunit3_AssertIsEmpty_TestAnalyzer(string assertion)
{
Nunit3VerifyDiagnostic("object[] actual", assertion);
Nunit3VerifyDiagnostic("IEnumerable<int> actual", assertion);
Nunit3VerifyDiagnostic("IEnumerable actual", assertion);
Nunit3VerifyDiagnostic("string actual", assertion);
}

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.IsEmpty(actual{0});")]
[Implemented]
public void Nunit4_AssertIsEmpty_TestAnalyzer(string assertion)
{
Nunit4VerifyDiagnostic("object[] actual", assertion);
Nunit4VerifyDiagnostic("IEnumerable<int> actual", assertion);
Nunit4VerifyDiagnostic("IEnumerable actual", assertion);
Nunit4VerifyDiagnostic("string actual", assertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.IsEmpty(actual{0});",
newAssertion: "actual.Should().BeEmpty({0});")]
[Implemented]
public void Nunit3_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit3VerifyFix("object[] actual", oldAssertion, newAssertion);
Nunit3VerifyFix("IEnumerable<int> actual", oldAssertion, newAssertion);
Nunit3VerifyNoFix("IEnumerable actual", oldAssertion);
Nunit3VerifyFix("string actual", oldAssertion, newAssertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsEmpty(actual{0});",
newAssertion: "actual.Should().BeEmpty({0});")]
[Implemented]
public void Nunit4_AssertIsEmpty_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit4VerifyFix("object[] actual", oldAssertion, newAssertion);
Nunit4VerifyFix("IEnumerable<int> actual", oldAssertion, newAssertion);
Nunit4VerifyNoFix("IEnumerable actual", oldAssertion);
Nunit4VerifyFix("string actual", oldAssertion, newAssertion);
}

// IsNotEmpty
[DataTestMethod]
[AssertionDiagnostic("Assert.IsNotEmpty(actual{0});")]
[Implemented]
public void Nunit3_AssertIsNotEmpty_TestAnalyzer(string assertion)
{
Nunit3VerifyDiagnostic("object[] actual", assertion);
Nunit3VerifyDiagnostic("IEnumerable<int> actual", assertion);
Nunit3VerifyDiagnostic("IEnumerable actual", assertion);
Nunit3VerifyDiagnostic("string actual", assertion);
}

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.IsNotEmpty(actual{0});")]
[Implemented]
public void Nunit4_AssertIsNotEmpty_TestAnalyzer(string assertion)
{
Nunit4VerifyDiagnostic("object[] actual", assertion);
Nunit4VerifyDiagnostic("IEnumerable<int> actual", assertion);
Nunit4VerifyDiagnostic("IEnumerable actual", assertion);
Nunit4VerifyDiagnostic("string actual", assertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.IsNotEmpty(actual{0});",
newAssertion: "actual.Should().NotBeEmpty({0});")]
[Implemented]
public void Nunit3_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit3VerifyFix("object[] actual", oldAssertion, newAssertion);
Nunit3VerifyFix("IEnumerable<int> actual", oldAssertion, newAssertion);
Nunit3VerifyNoFix("IEnumerable actual", oldAssertion);
Nunit3VerifyFix("string actual", oldAssertion, newAssertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.IsNotEmpty(actual{0});",
newAssertion: "actual.Should().NotBeEmpty({0});")]
[Implemented]
public void Nunit4_AssertIsNotEmpty_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit4VerifyFix("object[] actual", oldAssertion, newAssertion);
Nunit4VerifyFix("IEnumerable<int> actual", oldAssertion, newAssertion);
Nunit4VerifyNoFix("IEnumerable actual", oldAssertion);
Nunit4VerifyFix("string actual", oldAssertion, newAssertion);
}

[DataTestMethod]
[AssertionDiagnostic("Assert.Zero(actual{0});")]
[Implemented]
public void Nunit3_AssertZero_TestAnalyzer(string assertion)
{
Nunit3VerifyDiagnostic("int actual", assertion);
Nunit3VerifyDiagnostic("uint actual", assertion);
Nunit3VerifyDiagnostic("long actual", assertion);
Nunit3VerifyDiagnostic("ulong actual", assertion);
Nunit3VerifyDiagnostic("float actual", assertion);
Nunit3VerifyDiagnostic("double actual", assertion);
Nunit3VerifyDiagnostic("decimal actual", assertion);
}

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.Zero(actual{0});")]
[Implemented]
public void Nunit4_AssertZero_TestAnalyzer(string assertion)
{
Nunit4VerifyDiagnostic("int actual", assertion);
Nunit4VerifyDiagnostic("uint actual", assertion);
Nunit4VerifyDiagnostic("long actual", assertion);
Nunit4VerifyDiagnostic("ulong actual", assertion);
Nunit4VerifyDiagnostic("float actual", assertion);
Nunit4VerifyDiagnostic("double actual", assertion);
Nunit4VerifyDiagnostic("decimal actual", assertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.Zero(actual{0});",
newAssertion: "actual.Should().Be(0{0});")]
[Implemented]
public void Nunit3_AssertZero_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit3VerifyFix("int actual", oldAssertion, newAssertion);
Nunit3VerifyFix("uint actual", oldAssertion, newAssertion);
Nunit3VerifyFix("long actual", oldAssertion, newAssertion);
Nunit3VerifyFix("ulong actual", oldAssertion, newAssertion);
Nunit3VerifyFix("float actual", oldAssertion, newAssertion);
Nunit3VerifyFix("double actual", oldAssertion, newAssertion);
Nunit3VerifyFix("decimal actual", oldAssertion, newAssertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.Zero(actual{0});",
newAssertion: "actual.Should().Be(0{0});")]
[Implemented]
public void Nunit4_AssertZero_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit4VerifyFix("int actual", oldAssertion, newAssertion);
Nunit4VerifyFix("uint actual", oldAssertion, newAssertion);
Nunit4VerifyFix("long actual", oldAssertion, newAssertion);
Nunit4VerifyFix("ulong actual", oldAssertion, newAssertion);
Nunit4VerifyFix("float actual", oldAssertion, newAssertion);
Nunit4VerifyFix("double actual", oldAssertion, newAssertion);
Nunit4VerifyFix("decimal actual", oldAssertion, newAssertion);
}

[DataTestMethod]
[AssertionDiagnostic("Assert.NotZero(actual{0});")]
[Implemented]
public void Nunit3_AssertNotZero_TestAnalyzer(string assertion)
{
Nunit3VerifyDiagnostic("int actual", assertion);
Nunit3VerifyDiagnostic("uint actual", assertion);
Nunit3VerifyDiagnostic("long actual", assertion);
Nunit3VerifyDiagnostic("ulong actual", assertion);
Nunit3VerifyDiagnostic("float actual", assertion);
Nunit3VerifyDiagnostic("double actual", assertion);
Nunit3VerifyDiagnostic("decimal actual", assertion);
}

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.NotZero(actual{0});")]
[Implemented]
public void Nunit4_AssertNotZero_TestAnalyzer(string assertion)
{
Nunit4VerifyDiagnostic("int actual", assertion);
Nunit4VerifyDiagnostic("uint actual", assertion);
Nunit4VerifyDiagnostic("long actual", assertion);
Nunit4VerifyDiagnostic("ulong actual", assertion);
Nunit4VerifyDiagnostic("float actual", assertion);
Nunit4VerifyDiagnostic("double actual", assertion);
Nunit4VerifyDiagnostic("decimal actual", assertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.NotZero(actual{0});",
newAssertion: "actual.Should().NotBe(0{0});")]
[Implemented]
public void Nunit3_AssertNotZero_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit3VerifyFix("int actual", oldAssertion, newAssertion);
Nunit3VerifyFix("uint actual", oldAssertion, newAssertion);
Nunit3VerifyFix("long actual", oldAssertion, newAssertion);
Nunit3VerifyFix("ulong actual", oldAssertion, newAssertion);
Nunit3VerifyFix("float actual", oldAssertion, newAssertion);
Nunit3VerifyFix("double actual", oldAssertion, newAssertion);
Nunit3VerifyFix("decimal actual", oldAssertion, newAssertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "ClassicAssert.NotZero(actual{0});",
newAssertion: "actual.Should().NotBe(0{0});")]
[Implemented]
public void Nunit4_AssertNotZero_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit4VerifyFix("int actual", oldAssertion, newAssertion);
Nunit4VerifyFix("uint actual", oldAssertion, newAssertion);
Nunit4VerifyFix("long actual", oldAssertion, newAssertion);
Nunit4VerifyFix("ulong actual", oldAssertion, newAssertion);
Nunit4VerifyFix("float actual", oldAssertion, newAssertion);
Nunit4VerifyFix("double actual", oldAssertion, newAssertion);
Nunit4VerifyFix("decimal actual", oldAssertion, newAssertion);
}

// Positive
[DataTestMethod]
[AssertionDiagnostic("Assert.Positive(actual{0});")]
[Implemented]
public void Nunit3_AssertPositive_TestAnalyzer(string assertion)
{
Nunit3VerifyDiagnostic("int actual", assertion);
Nunit3VerifyDiagnostic("uint actual", assertion);
Nunit3VerifyDiagnostic("long actual", assertion);
Nunit3VerifyDiagnostic("ulong actual", assertion);
Nunit3VerifyDiagnostic("float actual", assertion);
Nunit3VerifyDiagnostic("double actual", assertion);
Nunit3VerifyDiagnostic("decimal actual", assertion);
}

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.Positive(actual{0});")]
[Implemented]
public void Nunit4_AssertPositive_TestAnalyzer(string assertion)
{
Nunit4VerifyDiagnostic("int actual", assertion);
Nunit4VerifyDiagnostic("uint actual", assertion);
Nunit4VerifyDiagnostic("long actual", assertion);
Nunit4VerifyDiagnostic("ulong actual", assertion);
Nunit4VerifyDiagnostic("float actual", assertion);
Nunit4VerifyDiagnostic("double actual", assertion);
Nunit4VerifyDiagnostic("decimal actual", assertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.Positive(actual{0});",
newAssertion: "actual.Should().BePositive({0});")]
[Implemented]
public void Nunit3_AssertPositive_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit3VerifyFix("int actual", oldAssertion, newAssertion);
Nunit3VerifyFix("uint actual", oldAssertion, newAssertion);
Nunit3VerifyFix("long actual", oldAssertion, newAssertion);
Nunit3VerifyFix("ulong actual", oldAssertion, newAssertion);
Nunit3VerifyFix("float actual", oldAssertion, newAssertion);
Nunit3VerifyFix("double actual", oldAssertion, newAssertion);
Nunit3VerifyFix("decimal actual", oldAssertion, newAssertion);
}

// Negative
[DataTestMethod]
[AssertionDiagnostic("Assert.Negative(actual{0});")]
[Implemented]
public void Nunit3_AssertNegative_TestAnalyzer(string assertion)
{
Nunit3VerifyDiagnostic("int actual", assertion);
Nunit3VerifyDiagnostic("uint actual", assertion);
Nunit3VerifyDiagnostic("long actual", assertion);
Nunit3VerifyDiagnostic("ulong actual", assertion);
Nunit3VerifyDiagnostic("float actual", assertion);
Nunit3VerifyDiagnostic("double actual", assertion);
Nunit3VerifyDiagnostic("decimal actual", assertion);
}

[DataTestMethod]
[AssertionDiagnostic("ClassicAssert.Negative(actual{0});")]
[Implemented]
public void Nunit4_AssertNegative_TestAnalyzer(string assertion)
{
Nunit4VerifyDiagnostic("int actual", assertion);
Nunit4VerifyDiagnostic("uint actual", assertion);
Nunit4VerifyDiagnostic("long actual", assertion);
Nunit4VerifyDiagnostic("ulong actual", assertion);
Nunit4VerifyDiagnostic("float actual", assertion);
Nunit4VerifyDiagnostic("double actual", assertion);
Nunit4VerifyDiagnostic("decimal actual", assertion);
}

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "Assert.Negative(actual{0});",
newAssertion: "actual.Should().BeNegative({0});")]
[Implemented]
public void Nunit3_AssertNegative_TestCodeFix(string oldAssertion, string newAssertion)
{
Nunit3VerifyFix("int actual", oldAssertion, newAssertion);
Nunit3VerifyFix("uint actual", oldAssertion, newAssertion);
Nunit3VerifyFix("long actual", oldAssertion, newAssertion);
Nunit3VerifyFix("ulong actual", oldAssertion, newAssertion);
Nunit3VerifyFix("float actual", oldAssertion, newAssertion);
Nunit3VerifyFix("double actual", oldAssertion, newAssertion);
Nunit3VerifyFix("decimal actual", oldAssertion, newAssertion);
}

#endregion

#region Assert.Comparisons.cs
Expand Down
11 changes: 7 additions & 4 deletions src/FluentAssertions.Analyzers/Tips/DocumentEditorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DocumentEditorUtils
{
public static CreateChangedDocument RenameMethodToSubjectShouldAssertion(IInvocationOperation invocation, CodeFixContext context, string newName, int subjectIndex, int[] argumentsToRemove)
{
return async ctx => await RewriteExpression(invocation, [
return ctx => RewriteExpressionCore(invocation, [
..Array.ConvertAll(argumentsToRemove, arg => EditAction.RemoveInvocationArgument(arg)),
EditAction.SubjectShouldAssertion(subjectIndex, newName)
], context, ctx);
Expand All @@ -25,21 +25,24 @@ public static CreateChangedDocument RenameGenericMethodToSubjectShouldGenericAss
=> RenameMethodToSubjectShouldGenericAssertion(invocation, invocation.TargetMethod.TypeArguments, context, newName, subjectIndex, argumentsToRemove);
public static CreateChangedDocument RenameMethodToSubjectShouldGenericAssertion(IInvocationOperation invocation, ImmutableArray<ITypeSymbol> genericTypes, CodeFixContext context, string newName, int subjectIndex, int[] argumentsToRemove)
{
return async ctx => await RewriteExpression(invocation, [
return ctx => RewriteExpressionCore(invocation, [
..Array.ConvertAll(argumentsToRemove, arg => EditAction.RemoveInvocationArgument(arg)),
EditAction.SubjectShouldGenericAssertion(subjectIndex, newName, genericTypes)
], context, ctx);
}

public static CreateChangedDocument RenameMethodToSubjectShouldAssertionWithOptionsLambda(IInvocationOperation invocation, CodeFixContext context, string newName, int subjectIndex, int optionsIndex)
{
return async ctx => await RewriteExpression(invocation, [
return ctx => RewriteExpressionCore(invocation, [
EditAction.SubjectShouldAssertion(subjectIndex, newName),
EditAction.CreateEquivalencyAssertionOptionsLambda(optionsIndex)
], context, ctx);
}

public static async Task<Document> RewriteExpression(IInvocationOperation invocation, Action<EditActionContext>[] actions, CodeFixContext context, CancellationToken cancellationToken)
public static CreateChangedDocument RewriteExpression(IInvocationOperation invocation, Action<EditActionContext>[] actions, CodeFixContext context)
=> ctx => RewriteExpressionCore(invocation, actions, context, ctx);

private static async Task<Document> RewriteExpressionCore(IInvocationOperation invocation, Action<EditActionContext>[] actions, CodeFixContext context, CancellationToken cancellationToken)
{
var invocationExpression = (InvocationExpressionSyntax)invocation.Syntax;

Expand Down
8 changes: 8 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/Editing/EditAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ public static Action<EditActionContext> SubjectShouldGenericAssertion(int argume

public static Action<EditActionContext> CreateEquivalencyAssertionOptionsLambda(int optionsIndex)
=> context => new CreateEquivalencyAssertionOptionsLambdaAction(optionsIndex).Apply(context.Editor, context.InvocationExpression);

public static Action<EditActionContext> AddArgumentToAssertionArguments(int index, Func<SyntaxGenerator, SyntaxNode> expressionFactory)
=> context =>
{
var argument = (ArgumentSyntax)context.Editor.Generator.Argument(expressionFactory(context.Editor.Generator));
var arguments = context.FluentAssertion.ArgumentList.Arguments.Insert(index, argument);
context.Editor.ReplaceNode(context.InvocationExpression.ArgumentList, context.InvocationExpression.ArgumentList.WithArguments(arguments));
};
}
Loading

0 comments on commit 2c718db

Please sign in to comment.