Skip to content

feat: Respect UsingPropertiesComparer#883

Merged
mikkelbu merged 2 commits intomasterfrom
feat/respect-UsingPropertiesComparer
Jun 11, 2025
Merged

feat: Respect UsingPropertiesComparer#883
mikkelbu merged 2 commits intomasterfrom
feat/respect-UsingPropertiesComparer

Conversation

@mikkelbu
Copy link
Copy Markdown
Member

@mikkelbu mikkelbu commented Jun 9, 2025

As part of this bumping the tests to NUnit version 4.4.0-alpha.0.22.

Fixes #857

As part of this bumping the tests to NUnit version `4.4.0-alpha.0.22`.
@mikkelbu mikkelbu force-pushed the feat/respect-UsingPropertiesComparer branch from aa9269d to 65735a8 Compare June 9, 2025 14:48
@mikkelbu mikkelbu requested a review from Copilot June 9, 2025 14:48
@mikkelbu mikkelbu added this to the Release 4.9 milestone Jun 9, 2025

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the NUnit analyzers to support the new UsingPropertiesComparer behavior introduced for NUnit v4 and updates the tests and constants accordingly. Key changes include:

  • Updating constraint checks in SomeItemsIncompatibleTypesAnalyzer to support both the legacy and generic SomeItemsConstraint.
  • Enhancing EqualToIncompatibleTypesAnalyzer with a new helper method to properly detect UsingPropertiesComparer invocations with arguments.
  • Updating tests and project settings to reflect the NUnit v4.4.0-alpha.0.22 package upgrade.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/nunit.analyzers/SomeItemsIncompatibleTypes/SomeItemsIncompatibleTypesAnalyzer.cs Updated condition to check for both legacy and generic some-items constraint types.
src/nunit.analyzers/EqualToIncompatibleTypes/EqualToIncompatibleTypesAnalyzer.cs Added support for detecting UsingPropertiesComparer invocations with arguments.
src/nunit.analyzers/Constants/NUnitV4FrameworkConstants.cs Introduced new constants for UsingPropertiesComparer and generic constraint representation.
src/nunit.analyzers.tests/EqualToIncompatibleTypes/EqualToIncompatibleTypesAnalyzerTests.cs Updated tests to expect diagnostics and validate new analyzer behavior.
src/nunit.analyzers.tests/nunit.analyzers.tests.csproj Bumped NUnit package to the new alpha version.
build.cake Removed explicit package source configuration.
NuGet.Config Added new configuration for NuGet package sources.

Comment on lines +36 to +38
if ((!IsDoesContain(constraintPart) && !IsContainsItem(constraintPart))
|| constraintPart.Root?.Type?.GetFullMetadataName() != NUnitFrameworkConstants.FullNameOfSomeItemsConstraint)
|| (constraintPart.Root?.Type?.GetFullMetadataName() != NUnitFrameworkConstants.FullNameOfSomeItemsConstraint
&& constraintPart.Root?.Type?.GetFullMetadataName() != NUnitV4FrameworkConstants.FullNameOfSomeItemsConstraintGeneric))
Copy link

Copilot AI Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider caching the result of constraintPart.Root?.Type?.GetFullMetadataName() in a local variable to avoid multiple method calls and improve readability.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Copilot or alternatively use patterns:

                    || (constraintPart.Root?.Type?.GetFullMetadataName() is not NUnitFrameworkConstants.FullNameOfSomeItemsConstraint
                                                                         and not NUnitV4FrameworkConstants.FullNameOfSomeItemsConstraintGeneric))

Was this to make existing tests pass?
I don't see a new nunit test that uses UsingPropertiesComparer nor code treating this as matching types

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll use the pattern you suggested

Was this to make existing tests pass?

Yes - I guess due to nunit/nunit#4925 - we had the following test failures in SomeItemsIncompatibleTypesAnalyzerTests for both Does.Contain and Contains.Item

  • AnalyzeWhenActualIsCollectionTask
  • AnalyzeWhenInvalidCollectionActualArgumentProvided
  • AnalyzeWhenNonCollectionActualArgumentProvided
  • AnalyzeWhenTypedCustomComparerProvided

with something like

Error Message:
   Gu.Roslyn.Asserts.AssertException : Expected and actual diagnostics do not match.
Expected:
  NUnit2026 The 'Contains.Item' constraint cannot be used with actual argument of type 'string[]' and expected argument of type 'int'
Actual: <no diagnostics>

  Stack Trace:
     at Gu.Roslyn.Asserts.RoslynAssert.VerifyDiagnostics(DiagnosticsAndSources diagnosticsAndSources, IReadOnlyList`1 selectedDiagnostics, IReadOnlyList`1 allDiagnostics)
   at Gu.Roslyn.Asserts.RoslynAssert.Diagnostics(DiagnosticAnalyzer analyzer, DiagnosticsAndSources diagnosticsAndSources, Settings settings)
1)    at Gu.Roslyn.Asserts.RoslynAssert.VerifyDiagnostics(DiagnosticsAndSources diagnosticsAndSources, IReadOnlyList`1 selectedDiagnostics, IReadOnlyList`1 allDiagnostics)
   at Gu.Roslyn.Asserts.RoslynAssert.Diagnostics(DiagnosticAnalyzer analyzer, DiagnosticsAndSources diagnosticsAndSources, Settings settings)

Copy link
Copy Markdown
Member

@manfred-brands manfred-brands left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are on a roll @mikkelbu.
Should we add support and tests on UsingPropertiesComparer for SomeItem?

Comment thread src/nunit.analyzers/EqualToIncompatibleTypes/EqualToIncompatibleTypesAnalyzer.cs Outdated
Comment on lines +36 to +38
if ((!IsDoesContain(constraintPart) && !IsContainsItem(constraintPart))
|| constraintPart.Root?.Type?.GetFullMetadataName() != NUnitFrameworkConstants.FullNameOfSomeItemsConstraint)
|| (constraintPart.Root?.Type?.GetFullMetadataName() != NUnitFrameworkConstants.FullNameOfSomeItemsConstraint
&& constraintPart.Root?.Type?.GetFullMetadataName() != NUnitV4FrameworkConstants.FullNameOfSomeItemsConstraintGeneric))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Copilot or alternatively use patterns:

                    || (constraintPart.Root?.Type?.GetFullMetadataName() is not NUnitFrameworkConstants.FullNameOfSomeItemsConstraint
                                                                         and not NUnitV4FrameworkConstants.FullNameOfSomeItemsConstraintGeneric))

Was this to make existing tests pass?
I don't see a new nunit test that uses UsingPropertiesComparer nor code treating this as matching types

Comment thread src/nunit.analyzers.tests/nunit.analyzers.tests.csproj
Copy link
Copy Markdown
Member

@manfred-brands manfred-brands left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mikkelbu
Nothing further.

@mikkelbu mikkelbu merged commit f7e7f61 into master Jun 11, 2025
6 checks passed
@mikkelbu mikkelbu deleted the feat/respect-UsingPropertiesComparer branch June 11, 2025 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NUnit2021 Should not raise for UsingPropertiesComparer

3 participants