diff --git a/src/nunit.analyzers.tests/Constants/NUnitFrameworkConstantsTests.cs b/src/nunit.analyzers.tests/Constants/NUnitFrameworkConstantsTests.cs index 690726b2..48a51cd9 100644 --- a/src/nunit.analyzers.tests/Constants/NUnitFrameworkConstantsTests.cs +++ b/src/nunit.analyzers.tests/Constants/NUnitFrameworkConstantsTests.cs @@ -160,6 +160,7 @@ public static readonly (string Constant, Type Type)[] FullNameOfTypeSource = (nameof(NUnitFrameworkConstants.FullNameOfActualValueDelegate), typeof(ActualValueDelegate<>)), (nameof(NUnitFrameworkConstants.FullNameOfDelayedConstraint), typeof(DelayedConstraint)), (nameof(NUnitFrameworkConstants.FullNameOfTestDelegate), typeof(TestDelegate)), + (nameof(NUnitFrameworkConstants.FullNameOfAsyncTestDelegate), typeof(AsyncTestDelegate)), (nameof(NUnitFrameworkConstants.FullNameOfThrows), typeof(Throws)), ]; diff --git a/src/nunit.analyzers.tests/EqualToIncompatibleTypes/EqualToIncompatibleTypesAnalyzerTests.cs b/src/nunit.analyzers.tests/EqualToIncompatibleTypes/EqualToIncompatibleTypesAnalyzerTests.cs index 06c2aafc..a53984c2 100644 --- a/src/nunit.analyzers.tests/EqualToIncompatibleTypes/EqualToIncompatibleTypesAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/EqualToIncompatibleTypes/EqualToIncompatibleTypesAnalyzerTests.cs @@ -391,14 +391,14 @@ public void NoDiagnosticWhenActualAndExpectedTypesAreSameWithLambdaActualValue() } [Test] - public void AnalyzeWhenActualAndExpectedTypesAreSameWithFuncActualValue() + public void NoDiagnosticWhenActualAndExpectedTypesAreSameWithFuncActualValue() { var testCode = TestUtility.WrapInTestMethod(@" Func actual = () => """"; var expected = """"; - Assert.That(actual, Is.EqualTo(↓expected));"); + Assert.That(actual, Is.EqualTo(expected));"); - RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode); + RoslynAssert.Valid(analyzer, testCode); } [Test] @@ -1153,5 +1153,27 @@ public void AnalyzeOnNullableDouble() RoslynAssert.Valid(analyzer, testCode); } + + [Test] + public void AnalyzeOnFuncReturningDouble() + { + var testCode = TestUtility.WrapInTestMethod(@$" + Func actual = () => Math.PI; + Assert.That(actual, Is.Not.NaN); + Assert.That(actual, Is.EqualTo(Math.PI)); + "); + RoslynAssert.Valid(analyzer, testCode); + } + + [Test] + public void AnalyzeOnAsyncFuncReturningDouble() + { + var testCode = TestUtility.WrapInTestMethod(@$" + Func> actual = () => Task.FromResult(Math.PI); + Assert.That(actual, Is.Not.NaN); + Assert.That(actual, Is.EqualTo(Math.PI)); + "); + RoslynAssert.Valid(analyzer, testCode); + } } } diff --git a/src/nunit.analyzers.tests/NullConstraintUsage/NullConstraintUsageAnalyzerTests.cs b/src/nunit.analyzers.tests/NullConstraintUsage/NullConstraintUsageAnalyzerTests.cs index 0f1c63ff..ea56f6eb 100644 --- a/src/nunit.analyzers.tests/NullConstraintUsage/NullConstraintUsageAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/NullConstraintUsage/NullConstraintUsageAnalyzerTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Threading.Tasks; using Gu.Roslyn.Asserts; using Microsoft.CodeAnalysis.Diagnostics; using NUnit.Analyzers.Constants; @@ -132,109 +130,13 @@ public void ValidWhenActualIsFunc(string constraint) [TestCase("Is.Null")] [TestCase("Is.Not.Null")] - public void AnalyzeWhenActualIsTestDelegate(string constraint) + public void ValidWhenActualIsTestDelegate(string constraint) { var testCode = TestUtility.WrapInTestMethod($@" Action action = b => {{ }}; - Assert.That(() => action(true), ↓{constraint});"); + Assert.That(() => action(true), {constraint});"); - RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode); - } - - [Test] - public void ActualNUnitFunction() - { - bool functionCalled = false; - -#pragma warning disable IDE0039 // Use local function - Func function = () => - { - functionCalled = true; - return false; - }; -#pragma warning restore IDE0039 // Use local function - - Assert.Multiple(() => - { - Assert.That(function, Is.Not.Null); - Assert.That(functionCalled, Is.False); - -#pragma warning disable NUnit2057 // Unnecessary DelegateCreation - Assert.That(() => function, Is.Not.Null); -#pragma warning restore NUnit2057 // Unnecessary DelegateCreation - Assert.That(functionCalled, Is.False); - -#pragma warning disable NUnit2057 // Unnecessary DelegateCreation -#pragma warning disable NUnit2023 // Invalid NullConstraint usage - Assert.That(() => function(), Is.Not.Null); -#pragma warning restore NUnit2023 // Invalid NullConstraint usage -#pragma warning restore NUnit2057 // Unnecessary DelegateCreation - Assert.That(functionCalled, Is.True); - }); - } - - [Test] - public void ActualNUnitAsyncFunction() - { - bool functionCalled = false; - -#pragma warning disable IDE0039 // Use local function - Func> asyncFunction = () => - { - functionCalled = true; - return Task.FromResult(false); - }; -#pragma warning restore IDE0039 // Use local function - - Assert.Multiple(() => - { - Assert.That(asyncFunction, Is.Not.Null); - Assert.That(functionCalled, Is.False); - -#pragma warning disable NUnit2057 // Unnecessary DelegateCreation - Assert.That(() => asyncFunction, Is.Not.Null); -#pragma warning restore NUnit2057 // Unnecessary DelegateCreation - Assert.That(functionCalled, Is.False); - -#pragma warning disable NUnit2057 // Unnecessary DelegateCreation -#pragma warning disable NUnit2023 // Invalid NullConstraint usage - Assert.That(() => asyncFunction(), Is.Not.Null); -#pragma warning restore NUnit2023 // Invalid NullConstraint usage -#pragma warning restore NUnit2057 // Unnecessary DelegateCreation - Assert.That(functionCalled, Is.True); - }); - } - - [Test] - public void ActualNUnitForAction() - { - bool actionCalled = false; - -#pragma warning disable IDE0039 // Use local function - Action action = () => actionCalled = true; -#pragma warning restore IDE0039 // Use local function - - Assert.Multiple(() => - { - Assert.That(action, Is.Not.Null); - Assert.That(actionCalled, Is.False); - -#pragma warning disable NUnit2057 // Unnecessary DelegateCreation - Assert.That(() => action, Is.Not.Null); -#pragma warning restore NUnit2057 // Unnecessary DelegateCreation - Assert.That(actionCalled, Is.False); - -#pragma warning disable NUnit2057 // Unnecessary DelegateCreation -#pragma warning disable NUnit2023 // Invalid NullConstraint usage - Assert.That(() => action(), Is.Not.Null); -#pragma warning restore NUnit2023 // Invalid NullConstraint usage -#pragma warning restore NUnit2057 // Unnecessary DelegateCreation - -#if EXPECT_TEST_DELEGATE_TO_BE_CALLED - // The above test succeeds, but does not call the action! - Assert.That(actionCalled, Is.True); -#endif - }); + RoslynAssert.Valid(analyzer, testCode); } } } diff --git a/src/nunit.analyzers.tests/SameAsIncompatibleTypes/SameAsIncompatibleTypesAnalyzerTests.cs b/src/nunit.analyzers.tests/SameAsIncompatibleTypes/SameAsIncompatibleTypesAnalyzerTests.cs index 647a8cb5..1306808d 100644 --- a/src/nunit.analyzers.tests/SameAsIncompatibleTypes/SameAsIncompatibleTypesAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/SameAsIncompatibleTypes/SameAsIncompatibleTypesAnalyzerTests.cs @@ -284,14 +284,14 @@ public void NoDiagnosticWhenActualAndExpectedTypesAreSameWithLambdaActualValue() } [Test] - public void AnalyzeWhenActualAndExpectedTypesAreSameWithFuncActualValue() + public void NoDiagnosticWhenActualAndExpectedTypesAreSameWithFuncActualValue() { var testCode = TestUtility.WrapInTestMethod(@" Func actual = () => """"; var expected = """"; - Assert.That(actual, Is.SameAs(↓expected));"); + Assert.That(actual, Is.SameAs(expected));"); - RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode); + RoslynAssert.Valid(analyzer, testCode); } [Test] diff --git a/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs b/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs index 0a37d7aa..12db8e95 100644 --- a/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs +++ b/src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs @@ -123,6 +123,7 @@ internal abstract class NUnitFrameworkConstants public const string FullNameOfActualValueDelegate = "NUnit.Framework.Constraints.ActualValueDelegate`1"; public const string FullNameOfDelayedConstraint = "NUnit.Framework.Constraints.DelayedConstraint"; public const string FullNameOfTestDelegate = "NUnit.Framework.TestDelegate"; + public const string FullNameOfAsyncTestDelegate = "NUnit.Framework.AsyncTestDelegate"; public const string FullNameOfThrows = "NUnit.Framework.Throws"; public const string PrefixOfAllEqualToConstraints = "NUnit.Framework.Constraints.Equal"; diff --git a/src/nunit.analyzers/Helpers/AssertHelper.cs b/src/nunit.analyzers/Helpers/AssertHelper.cs index 8e5a9682..58822b4c 100644 --- a/src/nunit.analyzers/Helpers/AssertHelper.cs +++ b/src/nunit.analyzers/Helpers/AssertHelper.cs @@ -56,15 +56,16 @@ public static ITypeSymbol UnwrapActualType(ITypeSymbol actualType) if (actualType is INamedTypeSymbol namedType) { var fullTypeName = namedType.GetFullMetadataName(); - if (fullTypeName == NUnitFrameworkConstants.FullNameOfActualValueDelegate || - fullTypeName == NUnitFrameworkConstants.FullNameOfTestDelegate) + if (fullTypeName is NUnitFrameworkConstants.FullNameOfActualValueDelegate + or "System.Func`1") { ITypeSymbol returnType = namedType.DelegateInvokeMethod!.ReturnType; if (returnType.IsAwaitable(out ITypeSymbol? awaitReturnType)) returnType = awaitReturnType; - return returnType; + // Func returns Void, so don't consider that as the actual type. + return returnType.SpecialType == SpecialType.System_Void ? actualType : returnType; } }