mercredi 4 mai 2016

Fluent Assertions: Approximately compare the properties of objects stored in Lists

This question builds on one I previously asked:

Fluent Assertions: Approximately compare a classes properties

If I have a class, say Vector3

public class Vector3
{
    public double X { get; }
    public double Y { get; }
    public double Z { get; }

    public Vector3(double x, double y, double z)
    {
        this.X = x;
        this.Y = y;
        this.Z = z;
    }
}

and it is formed into two lists, how can I approximately compare the properties of the Vector3 objects in the two lists to see if they are the same. This is what I have so far (I'm using the xUnit framework, but that shouldn't make any difference):

[Fact]
public void ApproximatelyCompareVector3List()
{
    // Arrange
    var expectedList = new List<Vector3>
    {
        new Vector3(0.5, 1, 3),
        new Vector3(0, 2, 4)
    };

    // Act
    var calculatedList = List<Vector3>
    {
        new Vector3(0.4999999, 1.0000001, 3),
        new Vector3(0.0000001, 2.0000001, 4)
    };

    //Assert
    calculatedList.ShouldBeEquivalentTo(expectedList, options => options
        .Using<double>(ctx => ctx.Subject.Should().BeApproximately(ctx.Expectation, precision))
        .When(info => info.SelectedMemberPath == "X" ||
                      info.SelectedMemberPath == "Y" ||
                      info.SelectedMemberPath == "Z" ));
}

However, this seems to skip the approximately test and require exact ordering. Is it possible to have either exact ordering or any ordering for approximately comparing the properties of objects contained in a list?

Aucun commentaire:

Enregistrer un commentaire