Skip to content

Commit e675f6b

Browse files
authored
feat: add missing nunit test cases for null assertions (#338)
1 parent fbb75e7 commit e675f6b

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

docs/NunitAnalyzer.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ object obj = null;
105105
// old assertion:
106106
Assert.IsNull(obj);
107107
Assert.Null(obj);
108+
Assert.That(obj, Is.Null);
108109

109110
// new assertion:
110111
obj.Should().BeNull();
@@ -122,6 +123,9 @@ Assert.Null(obj); /* fail message: Expected: null
122123
Assert.IsNull(obj); /* fail message: Expected: null
123124
But was: "foo"
124125
*/
126+
Assert.That(obj, Is.Null); /* fail message: Expected: null
127+
But was: "foo"
128+
*/
125129

126130
// new assertion:
127131
obj.Should().BeNull(); /* fail message: Expected obj to be <null>, but found "foo". */
@@ -136,6 +140,7 @@ object obj = "foo";
136140
// old assertion:
137141
Assert.IsNotNull(obj);
138142
Assert.NotNull(obj);
143+
Assert.That(obj, Is.Not.Null);
139144

140145
// new assertion:
141146
obj.Should().NotBeNull();
@@ -153,6 +158,9 @@ Assert.NotNull(obj); /* fail message: Expected: not null
153158
Assert.IsNotNull(obj); /* fail message: Expected: not null
154159
But was: null
155160
*/
161+
Assert.That(obj, Is.Not.Null); /* fail message: Expected: not null
162+
But was: null
163+
*/
156164

157165
// new assertion:
158166
obj.Should().NotBeNull(); /* fail message: Expected obj not to be <null>. */

src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/NunitAnalyzerTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public void AssertNull()
161161
// old assertion:
162162
Assert.IsNull(obj);
163163
Assert.Null(obj);
164+
Assert.That(obj, Is.Null);
164165

165166
// new assertion:
166167
obj.Should().BeNull();
@@ -186,6 +187,16 @@ public void AssertNull_Failure_OldAssertion_1()
186187
Assert.IsNull(obj);
187188
}
188189

190+
[TestMethod, ExpectedTestFrameworkException]
191+
public void AssertNull_Failure_OldAssertion_2()
192+
{
193+
// arrange
194+
object obj = "foo";
195+
196+
// old assertion:
197+
Assert.That(obj, Is.Null);
198+
}
199+
189200
[TestMethod, ExpectedTestFrameworkException]
190201
public void AssertNull_Failure_NewAssertion()
191202
{
@@ -205,6 +216,7 @@ public void AssertNotNull()
205216
// old assertion:
206217
Assert.IsNotNull(obj);
207218
Assert.NotNull(obj);
219+
Assert.That(obj, Is.Not.Null);
208220

209221
// new assertion:
210222
obj.Should().NotBeNull();
@@ -230,6 +242,16 @@ public void AssertNotNull_Failure_OldAssertion_1()
230242
Assert.IsNotNull(obj);
231243
}
232244

245+
[TestMethod, ExpectedTestFrameworkException]
246+
public void AssertNotNull_Failure_OldAssertion_2()
247+
{
248+
// arrange
249+
object obj = null;
250+
251+
// old assertion:
252+
Assert.That(obj, Is.Not.Null);
253+
}
254+
233255
[TestMethod, ExpectedTestFrameworkException]
234256
public void AssertNotNull_Failure_NewAssertion()
235257
{

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public class NunitTests
153153
[DataTestMethod]
154154
[AssertionDiagnostic("Assert.Null(actual{0});")]
155155
[AssertionDiagnostic("Assert.IsNull(actual{0});")]
156+
[AssertionDiagnostic("Assert.That(actual, Is.Null{0});")]
156157
[Implemented]
157158
public void Nunit3_AssertNull_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("object actual", assertion);
158159

@@ -169,6 +170,9 @@ public class NunitTests
169170
[AssertionCodeFix(
170171
oldAssertion: "Assert.IsNull(actual{0});",
171172
newAssertion: "actual.Should().BeNull({0});")]
173+
[AssertionCodeFix(
174+
oldAssertion: "Assert.That(actual, Is.Null{0});",
175+
newAssertion: "actual.Should().BeNull({0});")]
172176
[Implemented]
173177
public void Nunit3_AssertNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("object actual", oldAssertion, newAssertion);
174178

@@ -185,6 +189,7 @@ public class NunitTests
185189
[DataTestMethod]
186190
[AssertionDiagnostic("Assert.NotNull(actual{0});")]
187191
[AssertionDiagnostic("Assert.IsNotNull(actual{0});")]
192+
[AssertionDiagnostic("Assert.That(actual, Is.Not.Null{0});")]
188193
[Implemented]
189194
public void Nunit3_AssertNotNull_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("object actual", assertion);
190195

@@ -201,6 +206,9 @@ public class NunitTests
201206
[AssertionCodeFix(
202207
oldAssertion: "Assert.IsNotNull(actual{0});",
203208
newAssertion: "actual.Should().NotBeNull({0});")]
209+
[AssertionCodeFix(
210+
oldAssertion: "Assert.That(actual, Is.Not.Null{0});",
211+
newAssertion: "actual.Should().NotBeNull({0});")]
204212
[Implemented]
205213
public void Nunit3_AssertNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("object actual", oldAssertion, newAssertion);
206214

src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ private CreateChangedDocument TryComputeFixForNunitThat(IInvocationOperation inv
249249
case "True" when constraint.Instance is IPropertyReferenceOperation { Property.Name: "Not" } chainedReference && PropertyReferencedFromType(chainedReference, t.Is): // Assert.That(subject, Is.Not.True)
250250
case "False" when PropertyReferencedFromType(constraint, t.Is): // Assert.That(subject, Is.False)
251251
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeFalse", subjectIndex: 0, argumentsToRemove: [1]);
252+
case "Null" when PropertyReferencedFromType(constraint, t.Is): // Assert.That(subject, Is.Null)
253+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "BeNull", subjectIndex: 0, argumentsToRemove: [1]);
254+
case "Null" when constraint.Instance is IPropertyReferenceOperation { Property.Name: "Not" } chainedReference && PropertyReferencedFromType(chainedReference, t.Is): // Assert.That(subject, Is.Not.Null)
255+
return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotBeNull", subjectIndex: 0, argumentsToRemove: [1]);
252256

253257
default:
254258
return null;

0 commit comments

Comments
 (0)