Skip to content

Commit 68da99d

Browse files
authored
feat: add nunit types assertions (#296)
1 parent 849cf14 commit 68da99d

File tree

2 files changed

+212
-3
lines changed

2 files changed

+212
-3
lines changed

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

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ public void Nunit4_AssertEqualDouble_TestCodeFix(string oldAssertion, string new
429429
[Implemented]
430430
public void Nunit3_AssertEqualObject_TestAnalyzer(string assertion)
431431
=> Nunit3VerifyDiagnostic("object expected, object actual", assertion);
432-
432+
433433
[DataTestMethod]
434434
[AssertionDiagnostic("ClassicAssert.AreEqual(expected, actual{0});")]
435435
[Implemented]
@@ -479,7 +479,7 @@ public void Nunit3_AssertNotEqualObject_TestCodeFix(string oldAssertion, string
479479
[Implemented]
480480
public void Nunit4_AssertNotEqualObject_TestCodeFix(string oldAssertion, string newAssertion)
481481
=> Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion);
482-
482+
483483
[DataTestMethod]
484484
[AssertionDiagnostic("Assert.AreSame(expected, actual{0});")]
485485
[Implemented]
@@ -510,6 +510,163 @@ public void Nunit4_AssertSame_TestCodeFix(string oldAssertion, string newAsserti
510510

511511
#endregion
512512

513+
#region Assert.Types.cs
514+
515+
[DataTestMethod]
516+
[AssertionDiagnostic("Assert.IsAssignableFrom(typeof(string), actual{0});")]
517+
[AssertionDiagnostic("Assert.IsAssignableFrom(expected, actual{0});")]
518+
[AssertionDiagnostic("Assert.IsAssignableFrom<string>(actual{0});")]
519+
[Implemented]
520+
public void Nunit3_AssertIsAssignableFrom_TestAnalyzer(string assertion)
521+
=> Nunit3VerifyDiagnostic("object actual, Type expected", assertion);
522+
523+
[DataTestMethod]
524+
[AssertionDiagnostic("ClassicAssert.IsAssignableFrom(typeof(string), actual{0});")]
525+
[AssertionDiagnostic("ClassicAssert.IsAssignableFrom(expected, actual{0});")]
526+
[AssertionDiagnostic("ClassicAssert.IsAssignableFrom<string>(actual{0});")]
527+
[Implemented]
528+
public void Nunit4_AssertIsAssignableFrom_TestAnalyzer(string assertion)
529+
=> Nunit4VerifyDiagnostic("object actual, Type expected", assertion);
530+
531+
[DataTestMethod]
532+
[AssertionCodeFix(
533+
oldAssertion: "Assert.IsAssignableFrom(typeof(string), actual{0});",
534+
newAssertion: "actual.Should().BeAssignableTo<string>({0});")]
535+
[AssertionCodeFix(
536+
oldAssertion: "Assert.IsAssignableFrom(expected, actual{0});",
537+
newAssertion: "actual.Should().BeAssignableTo(expected{0});")]
538+
[Implemented]
539+
public void Nunit3_AssertIsAssignableFrom_TestCodeFix(string oldAssertion, string newAssertion)
540+
=> Nunit3VerifyFix("object actual, Type expected", oldAssertion, newAssertion);
541+
542+
[DataTestMethod]
543+
[AssertionCodeFix(
544+
oldAssertion: "ClassicAssert.IsAssignableFrom(typeof(string), actual{0});",
545+
newAssertion: "actual.Should().BeAssignableTo<string>({0});")]
546+
[AssertionCodeFix(
547+
oldAssertion: "ClassicAssert.IsAssignableFrom(expected, actual{0});",
548+
newAssertion: "actual.Should().BeAssignableTo(expected{0});")]
549+
[Implemented]
550+
public void Nunit4_AssertIsAssignableFrom_TestCodeFix(string oldAssertion, string newAssertion)
551+
=> Nunit4VerifyFix("object actual, Type expected", oldAssertion, newAssertion);
552+
553+
[DataTestMethod]
554+
[AssertionDiagnostic("Assert.IsNotAssignableFrom(typeof(string), actual{0});")]
555+
[AssertionDiagnostic("Assert.IsNotAssignableFrom(expected, actual{0});")]
556+
[AssertionDiagnostic("Assert.IsNotAssignableFrom<string>(actual{0});")]
557+
[Implemented]
558+
public void Nunit3_AssertIsNotAssignableFrom_TestAnalyzer(string assertion)
559+
=> Nunit3VerifyDiagnostic("object actual, Type expected", assertion);
560+
561+
[DataTestMethod]
562+
[AssertionDiagnostic("ClassicAssert.IsNotAssignableFrom(typeof(string), actual{0});")]
563+
[AssertionDiagnostic("ClassicAssert.IsNotAssignableFrom(expected, actual{0});")]
564+
[AssertionDiagnostic("ClassicAssert.IsNotAssignableFrom<string>(actual{0});")]
565+
[Implemented]
566+
public void Nunit4_AssertIsNotAssignableFrom_TestAnalyzer(string assertion)
567+
=> Nunit4VerifyDiagnostic("object actual, Type expected", assertion);
568+
569+
[DataTestMethod]
570+
[AssertionCodeFix(
571+
oldAssertion: "Assert.IsNotAssignableFrom(typeof(string), actual{0});",
572+
newAssertion: "actual.Should().NotBeAssignableTo<string>({0});")]
573+
[AssertionCodeFix(
574+
oldAssertion: "Assert.IsNotAssignableFrom(expected, actual{0});",
575+
newAssertion: "actual.Should().NotBeAssignableTo(expected{0});")]
576+
[Implemented]
577+
public void Nunit3_AssertIsNotAssignableFrom_TestCodeFix(string oldAssertion, string newAssertion)
578+
=> Nunit3VerifyFix("object actual, Type expected", oldAssertion, newAssertion);
579+
580+
[DataTestMethod]
581+
[AssertionCodeFix(
582+
oldAssertion: "ClassicAssert.IsNotAssignableFrom(typeof(string), actual{0});",
583+
newAssertion: "actual.Should().NotBeAssignableTo<string>({0});")]
584+
[AssertionCodeFix(
585+
oldAssertion: "ClassicAssert.IsNotAssignableFrom(expected, actual{0});",
586+
newAssertion: "actual.Should().NotBeAssignableTo(expected{0});")]
587+
[Implemented]
588+
public void Nunit4_AssertIsNotAssignableFrom_TestCodeFix(string oldAssertion, string newAssertion)
589+
=> Nunit4VerifyFix("object actual, Type expected", oldAssertion, newAssertion);
590+
591+
// void IsInstanceOf(Type expected, object? actual, string message, params object?[]? args)
592+
[DataTestMethod]
593+
[AssertionDiagnostic("Assert.IsInstanceOf(typeof(string), actual{0});")]
594+
[AssertionDiagnostic("Assert.IsInstanceOf(expected, actual{0});")]
595+
[AssertionDiagnostic("Assert.IsInstanceOf<string>(actual{0});")]
596+
[Implemented]
597+
public void Nunit3_AssertIsInstanceOf_TestAnalyzer(string assertion)
598+
=> Nunit3VerifyDiagnostic("object actual, Type expected", assertion);
599+
600+
[DataTestMethod]
601+
[AssertionDiagnostic("ClassicAssert.IsInstanceOf(typeof(string), actual{0});")]
602+
[AssertionDiagnostic("ClassicAssert.IsInstanceOf(expected, actual{0});")]
603+
[AssertionDiagnostic("ClassicAssert.IsInstanceOf<string>(actual{0});")]
604+
[Implemented]
605+
public void Nunit4_AssertIsInstanceOf_TestAnalyzer(string assertion)
606+
=> Nunit4VerifyDiagnostic("object actual, Type expected", assertion);
607+
608+
[DataTestMethod]
609+
[AssertionCodeFix(
610+
oldAssertion: "Assert.IsInstanceOf(typeof(string), actual{0});",
611+
newAssertion: "actual.Should().BeOfType<string>({0});")]
612+
[AssertionCodeFix(
613+
oldAssertion: "Assert.IsInstanceOf(expected, actual{0});",
614+
newAssertion: "actual.Should().BeOfType(expected{0});")]
615+
[Implemented]
616+
public void Nunit3_AssertIsInstanceOf_TestCodeFix(string oldAssertion, string newAssertion)
617+
=> Nunit3VerifyFix("object actual, Type expected", oldAssertion, newAssertion);
618+
619+
[DataTestMethod]
620+
[AssertionCodeFix(
621+
oldAssertion: "ClassicAssert.IsInstanceOf(typeof(string), actual{0});",
622+
newAssertion: "actual.Should().BeOfType<string>({0});")]
623+
[AssertionCodeFix(
624+
oldAssertion: "ClassicAssert.IsInstanceOf(expected, actual{0});",
625+
newAssertion: "actual.Should().BeOfType(expected{0});")]
626+
[Implemented]
627+
public void Nunit4_AssertIsInstanceOf_TestCodeFix(string oldAssertion, string newAssertion)
628+
=> Nunit4VerifyFix("object actual, Type expected", oldAssertion, newAssertion);
629+
630+
[DataTestMethod]
631+
[AssertionDiagnostic("Assert.IsNotInstanceOf(typeof(string), actual{0});")]
632+
[AssertionDiagnostic("Assert.IsNotInstanceOf(expected, actual{0});")]
633+
[AssertionDiagnostic("Assert.IsNotInstanceOf<string>(actual{0});")]
634+
[Implemented]
635+
public void Nunit3_AssertIsNotInstanceOf_TestAnalyzer(string assertion)
636+
=> Nunit3VerifyDiagnostic("object actual, Type expected", assertion);
637+
638+
[DataTestMethod]
639+
[AssertionDiagnostic("ClassicAssert.IsNotInstanceOf(typeof(string), actual{0});")]
640+
[AssertionDiagnostic("ClassicAssert.IsNotInstanceOf(expected, actual{0});")]
641+
[AssertionDiagnostic("ClassicAssert.IsNotInstanceOf<string>(actual{0});")]
642+
[Implemented]
643+
public void Nunit4_AssertIsNotInstanceOf_TestAnalyzer(string assertion)
644+
=> Nunit4VerifyDiagnostic("object actual, Type expected", assertion);
645+
646+
[DataTestMethod]
647+
[AssertionCodeFix(
648+
oldAssertion: "Assert.IsNotInstanceOf(typeof(string), actual{0});",
649+
newAssertion: "actual.Should().NotBeOfType<string>({0});")]
650+
[AssertionCodeFix(
651+
oldAssertion: "Assert.IsNotInstanceOf(expected, actual{0});",
652+
newAssertion: "actual.Should().NotBeOfType(expected{0});")]
653+
[Implemented]
654+
public void Nunit3_AssertIsNotInstanceOf_TestCodeFix(string oldAssertion, string newAssertion)
655+
=> Nunit3VerifyFix("object actual, Type expected", oldAssertion, newAssertion);
656+
657+
[DataTestMethod]
658+
[AssertionCodeFix(
659+
oldAssertion: "ClassicAssert.IsNotInstanceOf(typeof(string), actual{0});",
660+
newAssertion: "actual.Should().NotBeOfType<string>({0});")]
661+
[AssertionCodeFix(
662+
oldAssertion: "ClassicAssert.IsNotInstanceOf(expected, actual{0});",
663+
newAssertion: "actual.Should().NotBeOfType(expected{0});")]
664+
[Implemented]
665+
public void Nunit4_AssertIsNotInstanceOf_TestCodeFix(string oldAssertion, string newAssertion)
666+
=> Nunit4VerifyFix("object actual, Type expected", oldAssertion, newAssertion);
667+
668+
#endregion
669+
513670
private void Nunit3VerifyDiagnostic(string methodArguments, string assertion)
514671
=> VerifyDiagnostic(GenerateCode.Nunit3Assertion(methodArguments, assertion), PackageReference.Nunit_3_14_0);
515672
private void Nunit3VerifyFix(string methodArguments, string oldAssertion, string newAssertion)

src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected override CreateChangedDocument TryComputeFixCore(IInvocationOperation
1616
{
1717
var assertType = invocation.TargetMethod.ContainingType;
1818
var nunitVersion = assertType.ContainingAssembly.Identity.Version;
19-
19+
2020
var isNunit3 = nunitVersion >= new Version(3, 0, 0, 0) && nunitVersion < new Version(4, 0, 0, 0);
2121
var isNunit4 = nunitVersion >= new Version(4, 0, 0, 0) && nunitVersion < new Version(5, 0, 0, 0);
2222

@@ -119,6 +119,58 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper
119119
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeSameAs", subjectIndex: 1, argumentsToRemove: []);
120120
case "AreNotSame": // Assert.AreNotSame(object expected, object actual)
121121
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeSameAs", subjectIndex: 1, argumentsToRemove: []);
122+
case "IsAssignableFrom" when ArgumentsAreTypeOf(invocation, t.Type, t.Object): // Assert.IsAssignableFrom(Type expectedType, object obj)
123+
case "IsAssignableFrom" when ArgumentsAreTypeOf(invocation, t.Type, t.Object, t.String, t.ObjectArray): // Assert.IsAssignableFrom(Type expectedType, object obj, string message, params object[] parms)
124+
{
125+
if (invocation.Arguments[0].Value is not ITypeOfOperation typeOf)
126+
{
127+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeAssignableTo", subjectIndex: 1, argumentsToRemove: []);
128+
}
129+
130+
return DocumentEditorUtils.RenameMethodToSubjectShouldGenericAssertion(invocation, ImmutableArray.Create(typeOf.TypeOperand), context, "BeAssignableTo", subjectIndex: 1, argumentsToRemove: [0]);
131+
}
132+
case "IsAssignableFrom" when ArgumentsAreTypeOf(invocation, t.Object): // Assert.IsAssignableFrom<T>(object actual)
133+
case "IsAssignableFrom" when ArgumentsAreTypeOf(invocation, t.Object, t.String, t.ObjectArray): // Assert.IsAssignableFrom<T>(object actual, string message, params object[] parms)
134+
return DocumentEditorUtils.RenameGenericMethodToSubjectShouldGenericAssertion(invocation, context, "BeAssignableTo", subjectIndex: 0, argumentsToRemove: []);
135+
case "IsNotAssignableFrom" when ArgumentsAreTypeOf(invocation, t.Type, t.Object): // Assert.IsNotAssignableFrom(Type expectedType, object obj)
136+
case "IsNotAssignableFrom" when ArgumentsAreTypeOf(invocation, t.Type, t.Object, t.String, t.ObjectArray): // Assert.IsNotAssignableFrom(Type expectedType, object obj, string message, params object[] parms)
137+
{
138+
if (invocation.Arguments[0].Value is not ITypeOfOperation typeOf)
139+
{
140+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeAssignableTo", subjectIndex: 1, argumentsToRemove: []);
141+
}
142+
143+
return DocumentEditorUtils.RenameMethodToSubjectShouldGenericAssertion(invocation, ImmutableArray.Create(typeOf.TypeOperand), context, "NotBeAssignableTo", subjectIndex: 1, argumentsToRemove: [0]);
144+
}
145+
case "IsNotAssignableFrom" when ArgumentsAreTypeOf(invocation, t.Object): // Assert.IsNotAssignableFrom<T>(object actual)
146+
case "IsNotAssignableFrom" when ArgumentsAreTypeOf(invocation, t.Object, t.String, t.ObjectArray): // Assert.IsNotAssignableFrom<T>(object actual, string message, params object[] parms)
147+
return DocumentEditorUtils.RenameGenericMethodToSubjectShouldGenericAssertion(invocation, context, "NotBeAssignableTo", subjectIndex: 0, argumentsToRemove: []);
148+
case "IsInstanceOf" when ArgumentsAreTypeOf(invocation, t.Type, t.Object): // Assert.IsInstanceOf(Type expectedType, object actual)
149+
case "IsInstanceOf" when ArgumentsAreTypeOf(invocation, t.Type, t.Object, t.String, t.ObjectArray): // Assert.IsInstanceOf(Type expectedType, object actual, string message, params object[] parms)
150+
{
151+
if (invocation.Arguments[0].Value is not ITypeOfOperation typeOf)
152+
{
153+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeOfType", subjectIndex: 1, argumentsToRemove: []);
154+
}
155+
156+
return DocumentEditorUtils.RenameMethodToSubjectShouldGenericAssertion(invocation, ImmutableArray.Create(typeOf.TypeOperand), context, "BeOfType", subjectIndex: 1, argumentsToRemove: [0]);
157+
}
158+
case "IsInstanceOf" when ArgumentsAreTypeOf(invocation, t.Object): // Assert.IsInstanceOf<T>(object actual)
159+
case "IsInstanceOf" when ArgumentsAreTypeOf(invocation, t.Object, t.String, t.ObjectArray): // Assert.IsInstanceOf<T>(object actual, string message, params object[] parms)
160+
return DocumentEditorUtils.RenameGenericMethodToSubjectShouldGenericAssertion(invocation, context, "BeOfType", subjectIndex: 0, argumentsToRemove: []);
161+
case "IsNotInstanceOf" when ArgumentsAreTypeOf(invocation, t.Type, t.Object): // Assert.IsNotInstanceOf(Type expectedType, object actual)
162+
case "IsNotInstanceOf" when ArgumentsAreTypeOf(invocation, t.Type, t.Object, t.String, t.ObjectArray): // Assert.IsNotInstanceOf(Type expectedType, object actual, string message, params object[] parms)
163+
{
164+
if (invocation.Arguments[0].Value is not ITypeOfOperation typeOf)
165+
{
166+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeOfType", subjectIndex: 1, argumentsToRemove: []);
167+
}
168+
169+
return DocumentEditorUtils.RenameMethodToSubjectShouldGenericAssertion(invocation, ImmutableArray.Create(typeOf.TypeOperand), context, "NotBeOfType", subjectIndex: 1, argumentsToRemove: [0]);
170+
}
171+
case "IsNotInstanceOf" when ArgumentsAreTypeOf(invocation, t.Object): // Assert.IsNotInstanceOf<T>(object actual)
172+
case "IsNotInstanceOf" when ArgumentsAreTypeOf(invocation, t.Object, t.String, t.ObjectArray): // Assert.IsNotInstanceOf<T>(object actual, string message, params object[] parms)
173+
return DocumentEditorUtils.RenameGenericMethodToSubjectShouldGenericAssertion(invocation, context, "NotBeOfType", subjectIndex: 0, argumentsToRemove: []);
122174
}
123175
return null;
124176
}

0 commit comments

Comments
 (0)