r-lib / testthat

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

Deprecated advice for expect_equivalent() is not equivalent #1946

Open jarad opened 3 months ago

jarad commented 3 months ago

In testthat 3.1.10 and waldo 0.5.1, the helpfile for expect_equivalent() says that this function is deprecated and suggests you to use expect_equal(ignore_attr = TRUE) instead. Unfortunately, these two are apparently not equivalent.

a <- 1
names(a) <- "a"
expect_equivalent(a, unname(a))                     # passes
expect_equal(a, unname(a), ignore_attr = TRUE) # fails

In addition, in the helpfile for waldo::compare() under ignore_attr, it says "For backward compatibility with all.equal(), you can also use TRUE, to all ignore differences in all attributes. This is not generally recommended as it is a blunt tool that will ignore many important functional differences."

The expect_equivalent() function was useful and the suggested alternative does not work in the use-case I am interested in.

hadley commented 2 months ago

Minimal reprex:

library(testthat)
a <- 1
names(a) <- "a"
expect_equivalent(a, unname(a))
expect_equal(a, unname(a), ignore_attr = TRUE)
#> Error: `a` not equal to unname(a).
#> names for target but not for current

Created on 2024-04-17 with reprex v2.1.0

Hmmm, this is because waldo doesn't consider names to an attribute to be ignored. This needs to be documented better.