jeudi 26 septembre 2019

C# way for doing Assert.AreEqual(.,.,delta), but returning a bool

Say we have a Point class.

public class Point {
    public double x;
    public double y;
    ...
    public isEqual(Point p) {
        // here we worry about epsilons
    }
}

Some TestProject computes, say, the median of a triangle and confirms (by comparing x and y coordinates) that the point returned is correct. But we need to introduce tolerances.

Now we're damned if we do, damned if we don't:

Scenario 1

C# conveniently overloads the Assert.AreEqual() to handle tolerances. We put the comparison in the TestProject. We use Assert.AreEqual() and all is good.

But if we write ten different tests in ten different projects, we have not encapsulated point comparison. It is scattered throughout, which is bad.

Scenario 2

We put the comparison along with the Point class. Now any time we need to compare two points we just send the two points to the Point class and we get the answer.

But we can no longer use Assert.AreEqual(). All Asserts must be located in a TestProject.

We have to use our own comparator.

The question then is: what is the C# way for doing Assert.AreEqual(), but returning a bool?

Aucun commentaire:

Enregistrer un commentaire