Skip to content

Commit

Permalink
xunit: add IdentityAsserts (#147)
Browse files Browse the repository at this point in the history
* add BooleanAsserts

* add xunit to tests

* introduce base codeFixer for testing libraries

* xunit: add IdentityAsserts
  • Loading branch information
Meir017 authored Jan 11, 2022
1 parent 799c193 commit c499b7d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ public void AssertTrue_TestCodeFix(string oldAssertion, string newAssertion)
public void AssertFalse_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertFalseCodeFix, AssertFalseAnalyzer>("bool actual", oldAssertion, newAssertion);


[DataTestMethod]
[DataRow("Assert.Same(expected, actual);")]
[Implemented]
public void AssertSame_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<AssertSameAnalyzer>("object actual, object expected", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Same(expected, actual);",
/* newAssertion: */ "actual.Should().BeSameAs(expected);")]
[Implemented]
public void AssertSame_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertSameCodeFix, AssertSameAnalyzer>("object actual, object expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.NotSame(expected, actual);")]
[Implemented]
public void AssertNotSame_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<AssertNotSameAnalyzer>("object actual, object expected", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.NotSame(expected, actual);",
/* newAssertion: */ "actual.Should().NotBeSameAs(expected);")]
[Implemented]
public void AssertNotSame_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertNotSameCodeFix, AssertNotSameAnalyzer>("object actual, object expected", oldAssertion, newAssertion);

private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string methodArguments, string assertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
{
var source = GenerateCode.XunitAssertion(methodArguments, assertion);
Expand Down
2 changes: 2 additions & 0 deletions src/FluentAssertions.Analyzers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public static class Xunit
{
public const string AssertTrue = nameof(AssertTrue);
public const string AssertFalse = nameof(AssertFalse);
public const string AssertSame = nameof(AssertSame);
public const string AssertNotSame = nameof(AssertNotSame);
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/Xunit/AssertNotSame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;

namespace FluentAssertions.Analyzers.Xunit
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AssertNotSameAnalyzer : XunitAnalyzer
{
public const string DiagnosticId = Constants.Tips.Xunit.AssertNotSame;
public const string Category = Constants.Tips.Category;

public const string Message = "Use .Should().NotBeSameAs() instead.";

protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
{
get
{
yield return new AssertNotSameSyntaxVisitor();
}
}

public class AssertNotSameSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public AssertNotSameSyntaxVisitor() : base(new MemberValidator("NotSame"))
{
}
}
}

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertNotSameCodeFix)), Shared]
public class AssertNotSameCodeFix : XunitCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertNotSameAnalyzer.DiagnosticId);

protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
{
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "NotSame", "NotBeSameAs");
}
}
}
47 changes: 47 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/Xunit/AssertSame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;

namespace FluentAssertions.Analyzers.Xunit
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AssertSameAnalyzer : XunitAnalyzer
{
public const string DiagnosticId = Constants.Tips.Xunit.AssertSame;
public const string Category = Constants.Tips.Category;

public const string Message = "Use .Should().BeSameAs() instead.";

protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
{
get
{
yield return new AssertSameSyntaxVisitor();
}
}

public class AssertSameSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public AssertSameSyntaxVisitor() : base(new MemberValidator("Same"))
{
}
}
}

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertSameCodeFix)), Shared]
public class AssertSameCodeFix : XunitCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertSameAnalyzer.DiagnosticId);

protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
{
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Same", "BeSameAs");
}
}
}

0 comments on commit c499b7d

Please sign in to comment.