Skip to content

Commit c499b7d

Browse files
authored
xunit: add IdentityAsserts (#147)
* add BooleanAsserts * add xunit to tests * introduce base codeFixer for testing libraries * xunit: add IdentityAsserts
1 parent 799c193 commit c499b7d

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@ public void AssertTrue_TestCodeFix(string oldAssertion, string newAssertion)
5757
public void AssertFalse_TestCodeFix(string oldAssertion, string newAssertion)
5858
=> VerifyCSharpFix<AssertFalseCodeFix, AssertFalseAnalyzer>("bool actual", oldAssertion, newAssertion);
5959

60+
61+
[DataTestMethod]
62+
[DataRow("Assert.Same(expected, actual);")]
63+
[Implemented]
64+
public void AssertSame_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<AssertSameAnalyzer>("object actual, object expected", assertion);
65+
66+
[DataTestMethod]
67+
[DataRow(
68+
/* oldAssertion: */ "Assert.Same(expected, actual);",
69+
/* newAssertion: */ "actual.Should().BeSameAs(expected);")]
70+
[Implemented]
71+
public void AssertSame_TestCodeFix(string oldAssertion, string newAssertion)
72+
=> VerifyCSharpFix<AssertSameCodeFix, AssertSameAnalyzer>("object actual, object expected", oldAssertion, newAssertion);
73+
74+
[DataTestMethod]
75+
[DataRow("Assert.NotSame(expected, actual);")]
76+
[Implemented]
77+
public void AssertNotSame_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<AssertNotSameAnalyzer>("object actual, object expected", assertion);
78+
79+
[DataTestMethod]
80+
[DataRow(
81+
/* oldAssertion: */ "Assert.NotSame(expected, actual);",
82+
/* newAssertion: */ "actual.Should().NotBeSameAs(expected);")]
83+
[Implemented]
84+
public void AssertNotSame_TestCodeFix(string oldAssertion, string newAssertion)
85+
=> VerifyCSharpFix<AssertNotSameCodeFix, AssertNotSameAnalyzer>("object actual, object expected", oldAssertion, newAssertion);
86+
6087
private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string methodArguments, string assertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
6188
{
6289
var source = GenerateCode.XunitAssertion(methodArguments, assertion);

src/FluentAssertions.Analyzers/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public static class Xunit
120120
{
121121
public const string AssertTrue = nameof(AssertTrue);
122122
public const string AssertFalse = nameof(AssertFalse);
123+
public const string AssertSame = nameof(AssertSame);
124+
public const string AssertNotSame = nameof(AssertNotSame);
123125
}
124126
}
125127

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CodeFixes;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
using Microsoft.CodeAnalysis.Diagnostics;
5+
using System.Collections.Generic;
6+
using System.Collections.Immutable;
7+
using System.Composition;
8+
9+
namespace FluentAssertions.Analyzers.Xunit
10+
{
11+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
12+
public class AssertNotSameAnalyzer : XunitAnalyzer
13+
{
14+
public const string DiagnosticId = Constants.Tips.Xunit.AssertNotSame;
15+
public const string Category = Constants.Tips.Category;
16+
17+
public const string Message = "Use .Should().NotBeSameAs() instead.";
18+
19+
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
20+
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
21+
{
22+
get
23+
{
24+
yield return new AssertNotSameSyntaxVisitor();
25+
}
26+
}
27+
28+
public class AssertNotSameSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
29+
{
30+
public AssertNotSameSyntaxVisitor() : base(new MemberValidator("NotSame"))
31+
{
32+
}
33+
}
34+
}
35+
36+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertNotSameCodeFix)), Shared]
37+
public class AssertNotSameCodeFix : XunitCodeFixProvider
38+
{
39+
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertNotSameAnalyzer.DiagnosticId);
40+
41+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
42+
{
43+
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "NotSame", "NotBeSameAs");
44+
}
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CodeFixes;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Microsoft.CodeAnalysis.Diagnostics;
6+
using System.Collections.Generic;
7+
using System.Collections.Immutable;
8+
using System.Composition;
9+
10+
namespace FluentAssertions.Analyzers.Xunit
11+
{
12+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13+
public class AssertSameAnalyzer : XunitAnalyzer
14+
{
15+
public const string DiagnosticId = Constants.Tips.Xunit.AssertSame;
16+
public const string Category = Constants.Tips.Category;
17+
18+
public const string Message = "Use .Should().BeSameAs() instead.";
19+
20+
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
21+
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
22+
{
23+
get
24+
{
25+
yield return new AssertSameSyntaxVisitor();
26+
}
27+
}
28+
29+
public class AssertSameSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
30+
{
31+
public AssertSameSyntaxVisitor() : base(new MemberValidator("Same"))
32+
{
33+
}
34+
}
35+
}
36+
37+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertSameCodeFix)), Shared]
38+
public class AssertSameCodeFix : XunitCodeFixProvider
39+
{
40+
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertSameAnalyzer.DiagnosticId);
41+
42+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
43+
{
44+
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Same", "BeSameAs");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)