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):
public double precision = 1e-5;
[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?
I know it is a bit old question, but anyway if someone stumbles upon the same problem:
The code inside Using<double>
is not executed because selected member path comparison is wrong. You're comparing lists, that's why the path will look like something like [0].X
So you can fix it with:
.When(info => info.SelectedMemberPath.EndsWith("X") ||
info.SelectedMemberPath.EndsWith("Y") ||
info.SelectedMemberPath.EndsWith("Z")));
Or just:
.WhenTypeIs<double>());
Exact ordering is not required in ShouldBeEquivalentTo
by default.
UPDATE: SelectedMemeberPath
was renamed to Path
in v.6.0.0.