r-lib / testthat

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

Expect_error fails with waldo #1884

Closed latot closed 8 months ago

latot commented 8 months ago

Hi, I'm trying to write a test where the expected error is generated by waldo.

library(testthat)

e <- sprintf(
  "%s\n%s",
  "type mismatch",
  waldo::compare(
    "other type",
    "sfnetworks",
    x_arg = "typeof(value)",
    y_arg = "expected"
  )
)

expect_error({stop(e, call. = FALSE)}, e)

This test should pass, instead we get a weird error:

expect_error({stop(e, call. = FALSE)}, e)
Error in grepl(regexp, cnd_message(x), ...) : 
  invalid regular expression 'type mismatch
`typeof(value)`: "other type"
`expected`:      "sfnetworks"', reason 'Missing ']''
In addition: Warning message:
In grepl(regexp, cnd_message(x), ...) :
  TRE pattern compilation error 'Missing ']''

Well, I used some very brute ways, and the text is not regex, I have tested with regex with ^foo$ or \bfoo\b, sadly still the same word.

Maybe I'm just using it bad, no idea if is a bug or I'm using waldo bad with testthat.

Thx!

hadley commented 8 months ago

The second argument to expect_error() is supposed to be a single regular expression. For this sort of problem, I'd strongly recommend expect_snapshot() instead.

latot commented 8 months ago

@hadley I check the snapshot one! thx :D they are great and so useful for a lot of things!

I just didn't know they exists, I didn't check all the types that exists.

Moving to snapshots!