Skip to content

Commit 3f69adc

Browse files
ActualValueDelegate and Func are always evaluated
We need to consider their return type. TestDelegate and Action are never evaluated for their return type as they don't have one.
1 parent 8b6b718 commit 3f69adc

6 files changed

Lines changed: 49 additions & 27 deletions

File tree

src/nunit.analyzers.tests/Constants/NUnitFrameworkConstantsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public static readonly (string Constant, Type Type)[] FullNameOfTypeSource =
160160
(nameof(NUnitFrameworkConstants.FullNameOfActualValueDelegate), typeof(ActualValueDelegate<>)),
161161
(nameof(NUnitFrameworkConstants.FullNameOfDelayedConstraint), typeof(DelayedConstraint)),
162162
(nameof(NUnitFrameworkConstants.FullNameOfTestDelegate), typeof(TestDelegate)),
163+
(nameof(NUnitFrameworkConstants.FullNameOfAsyncTestDelegate), typeof(AsyncTestDelegate)),
163164
(nameof(NUnitFrameworkConstants.FullNameOfThrows), typeof(Throws)),
164165
];
165166

src/nunit.analyzers.tests/EqualToIncompatibleTypes/EqualToIncompatibleTypesAnalyzerTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ public void AnalyzeWhenActualAndExpectedTypesAreSameWithFuncActualValue()
396396
var testCode = TestUtility.WrapInTestMethod(@"
397397
Func<string> actual = () => """";
398398
var expected = """";
399-
Assert.That(actual, Is.EqualTo(expected));");
399+
Assert.That(actual, Is.EqualTo(expected));");
400400

401-
RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode);
401+
RoslynAssert.Valid(analyzer, testCode);
402402
}
403403

404404
[Test]
@@ -1153,5 +1153,27 @@ public void AnalyzeOnNullableDouble()
11531153

11541154
RoslynAssert.Valid(analyzer, testCode);
11551155
}
1156+
1157+
[Test]
1158+
public void AnalyzeOnFuncReturningDouble()
1159+
{
1160+
var testCode = TestUtility.WrapInTestMethod(@$"
1161+
Func<double> actual = () => Math.PI;
1162+
Assert.That(actual, Is.Not.NaN);
1163+
Assert.That(actual, Is.EqualTo(Math.PI));
1164+
");
1165+
RoslynAssert.Valid(analyzer, testCode);
1166+
}
1167+
1168+
[Test]
1169+
public void AnalyzeOnAsyncFuncReturningDouble()
1170+
{
1171+
var testCode = TestUtility.WrapInTestMethod(@$"
1172+
Func<Task<double>> actual = () => Task.FromResult(Math.PI);
1173+
Assert.That(actual, Is.Not.NaN);
1174+
Assert.That(actual, Is.EqualTo(Math.PI));
1175+
");
1176+
RoslynAssert.Valid(analyzer, testCode);
1177+
}
11561178
}
11571179
}

src/nunit.analyzers.tests/NullConstraintUsage/NullConstraintUsageAnalyzerTests.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void AnalyzeWhenActualIsTestDelegate(string constraint)
138138
Action<bool> action = b => {{ }};
139139
Assert.That(() => action(true), ↓{constraint});");
140140

141-
RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode);
141+
RoslynAssert.Valid(analyzer, testCode);
142142
}
143143

144144
[Test]
@@ -156,19 +156,21 @@ public void ActualNUnitFunction()
156156

157157
Assert.Multiple(() =>
158158
{
159-
Assert.That(function, Is.Not.Null);
160-
Assert.That(functionCalled, Is.False);
161-
162-
#pragma warning disable NUnit2057 // Unnecessary DelegateCreation
163159
Assert.That(() => function, Is.Not.Null);
164-
#pragma warning restore NUnit2057 // Unnecessary DelegateCreation
165160
Assert.That(functionCalled, Is.False);
166161

167-
#pragma warning disable NUnit2057 // Unnecessary DelegateCreation
162+
#if NUNIT5
163+
Assert.That(function, Is.False);
164+
Assert.That(functionCalled, Is.True);
165+
functionCalled = false;
166+
#else
168167
#pragma warning disable NUnit2023 // Invalid NullConstraint usage
169-
Assert.That(() => function(), Is.Not.Null);
168+
Assert.That(function, Is.Not.Null);
170169
#pragma warning restore NUnit2023 // Invalid NullConstraint usage
171-
#pragma warning restore NUnit2057 // Unnecessary DelegateCreation
170+
Assert.That(functionCalled, Is.False);
171+
#endif
172+
173+
Assert.That(() => function(), Is.False);
172174
Assert.That(functionCalled, Is.True);
173175
});
174176
}
@@ -188,20 +190,18 @@ public void ActualNUnitAsyncFunction()
188190

189191
Assert.Multiple(() =>
190192
{
193+
#pragma warning disable NUnit2023 // Invalid NullConstraint usage
191194
Assert.That(asyncFunction, Is.Not.Null);
192195
Assert.That(functionCalled, Is.False);
193196

194197
#pragma warning disable NUnit2057 // Unnecessary DelegateCreation
195198
Assert.That(() => asyncFunction, Is.Not.Null);
196-
#pragma warning restore NUnit2057 // Unnecessary DelegateCreation
197199
Assert.That(functionCalled, Is.False);
198200

199-
#pragma warning disable NUnit2057 // Unnecessary DelegateCreation
200-
#pragma warning disable NUnit2023 // Invalid NullConstraint usage
201201
Assert.That(() => asyncFunction(), Is.Not.Null);
202+
Assert.That(functionCalled, Is.True);
202203
#pragma warning restore NUnit2023 // Invalid NullConstraint usage
203204
#pragma warning restore NUnit2057 // Unnecessary DelegateCreation
204-
Assert.That(functionCalled, Is.True);
205205
});
206206
}
207207

@@ -216,24 +216,21 @@ public void ActualNUnitForAction()
216216

217217
Assert.Multiple(() =>
218218
{
219+
#pragma warning disable NUnit2023 // Invalid NullConstraint usage
219220
Assert.That(action, Is.Not.Null);
220221
Assert.That(actionCalled, Is.False);
221222

222223
#pragma warning disable NUnit2057 // Unnecessary DelegateCreation
223224
Assert.That(() => action, Is.Not.Null);
224-
#pragma warning restore NUnit2057 // Unnecessary DelegateCreation
225225
Assert.That(actionCalled, Is.False);
226226

227-
#pragma warning disable NUnit2057 // Unnecessary DelegateCreation
228-
#pragma warning disable NUnit2023 // Invalid NullConstraint usage
229227
Assert.That(() => action(), Is.Not.Null);
230-
#pragma warning restore NUnit2023 // Invalid NullConstraint usage
231-
#pragma warning restore NUnit2057 // Unnecessary DelegateCreation
232-
233228
#if EXPECT_TEST_DELEGATE_TO_BE_CALLED
234-
// The above test succeeds, but does not call the action!
235-
Assert.That(actionCalled, Is.True);
229+
// The above test succeeds, but does not call the action!
230+
Assert.That(actionCalled, Is.True);
236231
#endif
232+
#pragma warning restore NUnit2057 // Unnecessary DelegateCreation
233+
#pragma warning restore NUnit2023 // Invalid NullConstraint usage
237234
});
238235
}
239236
}

src/nunit.analyzers.tests/SameAsIncompatibleTypes/SameAsIncompatibleTypesAnalyzerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ public void AnalyzeWhenActualAndExpectedTypesAreSameWithFuncActualValue()
289289
var testCode = TestUtility.WrapInTestMethod(@"
290290
Func<string> actual = () => """";
291291
var expected = """";
292-
Assert.That(actual, Is.SameAs(expected));");
292+
Assert.That(actual, Is.SameAs(expected));");
293293

294-
RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode);
294+
RoslynAssert.Valid(analyzer, testCode);
295295
}
296296

297297
[Test]

src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ internal abstract class NUnitFrameworkConstants
123123
public const string FullNameOfActualValueDelegate = "NUnit.Framework.Constraints.ActualValueDelegate`1";
124124
public const string FullNameOfDelayedConstraint = "NUnit.Framework.Constraints.DelayedConstraint";
125125
public const string FullNameOfTestDelegate = "NUnit.Framework.TestDelegate";
126+
public const string FullNameOfAsyncTestDelegate = "NUnit.Framework.AsyncTestDelegate";
126127
public const string FullNameOfThrows = "NUnit.Framework.Throws";
127128

128129
public const string PrefixOfAllEqualToConstraints = "NUnit.Framework.Constraints.Equal";

src/nunit.analyzers/Helpers/AssertHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public static ITypeSymbol UnwrapActualType(ITypeSymbol actualType)
5656
if (actualType is INamedTypeSymbol namedType)
5757
{
5858
var fullTypeName = namedType.GetFullMetadataName();
59-
if (fullTypeName == NUnitFrameworkConstants.FullNameOfActualValueDelegate ||
60-
fullTypeName == NUnitFrameworkConstants.FullNameOfTestDelegate)
59+
if (fullTypeName is NUnitFrameworkConstants.FullNameOfActualValueDelegate
60+
or NUnitFrameworkConstants.FullNameOfAsyncTestDelegate
61+
or "System.Func`1")
6162
{
6263
ITypeSymbol returnType = namedType.DelegateInvokeMethod!.ReturnType;
6364

0 commit comments

Comments
 (0)