Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Pass a pair of conversion lambdas to .WithAutoConversion #2680

@icraftsoftware

Description

@icraftsoftware

Background and motivation

When comparing a subject to its expectation via .BeEquivalentTo, the comparion may fail because two properties might have a different representation of the same value; for instance a Guid and a string. To allow the comparison to proceed, passing a pair of lambdas that would perform a round trip conversion would be a possible solution, as in what follows:

[Fact]
public void MessagesAreEquivalent()
{
    var poco = new PocoContract { Id = Guid.NewGuid(), Amount = 123.45M, Currency = "EUR" };
    var proto = new ProtobufContract { Id = poco.Id.ToString(), Amount = 123.45f, Currency = "EUR" };

    poco.Should().BeEquivalentTo(proto, static options => options
        .WithAutoConversion<Guid, string>(g => g.ToString(), s => Guid.Parse(s)));
    proto.Should().BeEquivalentTo(poco, static options => options
        .WithAutoConversion<string, Guid>(s => Guid.Parse(s) , g => g.ToString()));
}

See #2672 for a comprehensive discussion.

Alternative Concerns

Instead of overloading .WithAutoConversion offering a .WithConversion might even be more explicit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationfeature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions