netdaemon icon indicating copy to clipboard operation
netdaemon copied to clipboard

Added Equal(object) to ServiceTarget so FluentAssertions of equality work correctly

Open edleno2 opened this issue 6 months ago • 5 comments

Proposed change

When writing unit tests using Fluent Assertion I discovered that equality checks against ServiceTarget can only be done as reference equality. This would be extremely hard to use since the creation of ServiceTargets often happens in internal code. This code adds Equal(object) which matches the signature used by Fluent Assertions when CallService is used with a target of "object?".

Type of change

  • [ ] Dependency upgrade
  • [ ] Bugfix (non-breaking change which fixes an issue)
  • [ ] New feature (which adds functionality to an existing integration)
  • [ ] Breaking change (fix/feature causing existing functionality to break)
  • [X] Code quality improvements to existing code or addition of tests

Additional information

This change did cause one regression of a unit test. The test relied on the LACK of an equals so that the comparison between a class (ServiceTarget) and a record (HassTarget) was done on a member by member basis. Once an equals was added this lead to the issue that neither the class nor the record could equate each other. Added an option to force member comparison. This is a known limitation with class to record comparisons (see answer 2 in this link)

Added unit tests to show which equal operators will work, and which will not. Keeping to the principle to make the least impactive change I did not override the == or != operator for actual ServiceTarget classes. Those type of comparisons will still default to checking object reference equality.

Checklist

  • [X] The code change is tested and works locally.
  • [X] Local tests pass. Your PR cannot be merged unless tests pass
  • [X] The code compiles without warnings (code quality check)
  • [X] Tests have been added to verify that the new code works.

edleno2 avatar Aug 01 '25 00:08 edleno2