r-lib / testthat

An R 📦 to make testing 😀
https://testthat.r-lib.org
Other
868 stars 313 forks source link

Expect equality using `==` #1951

Open stla opened 2 months ago

stla commented 2 months ago

Hello,

My package qspray defines the qspray objects which represent polynomials.

An equality is defined for qspray objects and it is possible that qspray1 == qspray2 returns TRUE but both tests expect_identical(qspray1, qspray2) and expect_equal(qspray1, qspray2) fail.

Why? Such an object is an S4 object with two slots, powers and coeffs, the list of powers and the list of coefficients of the polynomial. It is possible that two qspray objects qspray1 and qspray2 are equal in the sense that qspray1 == qspray2 returns TRUE while they are not identical, e.g. x² + x == x + x². That's because the order of the terms of a polynomial has no importance for the equality, so in order for qspray1 == qspray2 to be TRUE, it is enough that their lists of powers and coefficients are equal up to the order. Thus expect_identical(qspray1, qspray2) fails. The same for expect_equal(qspray1, qspray2) since this test compares the slots.

So I'd like to have a test expect_equality(qspray1, qspray2) (or another name) which would do the same as expect_true(qspray1 == qspray2) but with a notable difference: expect_true(qspray1 == qspray2) does not print any useful information when it fails, whereas expect_equality(qspray1, qspray2) could print qspray1 and qspray2.

Of course, if I take the liberty to do such a feature request, that's because I think it could be useful not only for me. E.g. there are other packages dealing with polynomials, and there certainly are many packages defining the equality test == for some custom objects (e.g. the quaternions in the onion package).

Cheers.