Skip to content

[Feature]: IsEqualTo() & co with implicitly convertible wrappers #5720

Description

@Timovzl

Problem Statement

I'm doing assertions on wrapper Value Objects, i.e. single-valued Value Objects wrapping a primitive. For example, we might have a ProductCode wrapping string. These have an implicit conversion to the primitive, and an explicit conversion to the wrapper.

Before 1.39.0, the following worked for string wrappers:

await Assert.That(updatedStockItem.ProductCode).IsEqualTo("Example");

As of 1.39.0, the string overload is gone, and a ProductCode is required. Since the conversion in that direction is (rightfully) explicit, we have to make it manually, or resolve ProductCode.Value.

I considered filing a bug, but realized that the wrapper scenario only worked with string wrappers. An int wrapper, for example, already did not benefit. As such, a feature request seems more suitable.

Proposed Solution

await Assert.That(updatedStockItem.ProductCode).IsEqualTo("Example"); // T = ProductCode
await Assert.That(updatedStockItem.SomeWrappedNumber).IsEqualTo(1); // T = MyIntWrapper

Could such equality checks (and perhaps also other comparisons) be made flexible enough to work, for wrappers?

For example, if the T in Assert.That(T) has an implicit conversion operator to some type X, it would be fantastic if IsEqualTo and other comparison overloads were made available accepting an X. In that case, the left hand would be converted to X and the result compared to the right hand's X.

Alternatives Considered

await Assert.That(updatedStockItem.ProductCode.Value).IsEqualTo("Example");
await Assert.That(updatedStockItem.ProductCode).IsEqualTo((ProductCode)"Example");

Since we type these very often, the simplification has merit.

Feature Category

Assertions

How important is this feature to you?

Nice to have - would improve my experience

Additional Context

No response

Contribution

  • I'm willing to submit a pull request for this feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions