c# - Fluent Assertions: Approximately compare a classes properties -


i have class vector3d has properties x, y , z of type double (it has other properties such magnitude).

what best way of approximately comparing properties or selection of properties @ given precision using fluent assertions?

currently have been doing this:

calculated.x.should().beapproximately(expected.x, precision); calculated.y.should().beapproximately(expected.y, precision); calculated.z.should().beapproximately(expected.z, precision); 

is there single line approach achieve same thing? such using shouldbeequivalentto, or require constructing generic extension method allows properties included / excluded?

yes it's possible using shouldbeequivalentto. following code check properties of type double precision of 0.1 :

double precision = 0.1; calculated.shouldbeequivalentto(expected, option => options     .using<double>(ctx => ctx.subject.should().beapproximately(ctx.expectation, precision))     .whentypeis<double>()); 

if want compare x, y , z properties change when constraint :

double precision = 0.1; calculated.shouldbeequivalentto(b, options => options     .using<double>(ctx => ctx.subject.should().beapproximately(ctx.expectation, precision))     .when(info => info.selectedmemberpath == "x" ||                   info.selectedmemberpath == "y" ||                   info.selectedmemberpath == "z")); 

another approach explicitly tell fluentassertions wich properties should compared, it's bit less elegant :

double precision = 0.1; calculated.shouldbeequivalentto(b, options => options     .including(info => info.selectedmemberpath == "x" ||                            info.selectedmemberpath == "y" ||                            info.selectedmemberpath == "z")     .using<double>(ctx => ctx.subject.should().beapproximately(ctx.expectation, precision))     .when(info => true)); 

since using statement not return equivalencyassertionoptions<t> need hack calling when statement true expression.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -