Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect_message() and expect_warning() do not check class in testthat 2e? #1894

Closed
mikemahoney218 opened this issue Nov 7, 2023 · 5 comments

Comments

@mikemahoney218
Copy link

Hi all,

With current HEAD (5659da8) it seems like expect_message() and expect_warning() succeed even if the condition does not have class class. This is not the case for expect_condition() or expect_error().

Is this expected?

library(testthat)
# these succeed even though the class is not a match
expect_message(message(), class = "not a match")
expect_warning(warning(), class = "not a match")

# these work as I had expected
try(
  expect_error(stop(), class = "not a match")
)
#> Error : `stop()` threw an error with unexpected class.
#> Expected class: not a match
#> Actual class:   simpleError/error/condition
#> Message:
try(
  expect_condition(message(), class = "not a match")
)
#> Error : `message()` threw an condition with unexpected class.
#> Expected class: not a match
#> Actual class:   simpleMessage/message/condition
#> Message:
try(
  expect_condition(warning(), class = "not a match")
)
#> Error : `warning()` threw an condition with unexpected class.
#> Expected class: not a match
#> Actual class:   simpleWarning/warning/condition
#> Message:
try(
  expect_condition(stop(), class = "not a match")
)
#> Error : `stop()` threw an condition with unexpected class.
#> Expected class: not a match
#> Actual class:   simpleError/error/condition
#> Message:

Created on 2023-11-07 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16)
#>  os       Ubuntu 22.04.3 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2023-11-07
#>  pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  brio          1.1.3      2021-11-30 [1] CRAN (R 4.3.0)
#>  cli           3.6.1      2023-03-23 [1] CRAN (R 4.3.0)
#>  desc          1.4.2      2022-09-08 [1] CRAN (R 4.3.0)
#>  digest        0.6.33     2023-07-07 [1] CRAN (R 4.3.1)
#>  evaluate      0.22       2023-09-29 [1] CRAN (R 4.3.1)
#>  fansi         1.0.5      2023-10-08 [1] CRAN (R 4.3.1)
#>  fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.3.0)
#>  fs            1.6.3      2023-07-20 [1] CRAN (R 4.3.1)
#>  glue          1.6.2      2022-02-24 [1] CRAN (R 4.3.0)
#>  htmltools     0.5.6.1    2023-10-06 [1] CRAN (R 4.3.1)
#>  knitr         1.44       2023-09-11 [1] CRAN (R 4.3.1)
#>  lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.3.0)
#>  magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.3.0)
#>  pillar        1.9.0      2023-03-22 [1] CRAN (R 4.3.0)
#>  pkgload       1.3.3      2023-09-22 [1] CRAN (R 4.3.1)
#>  purrr         1.0.2      2023-08-10 [1] CRAN (R 4.3.1)
#>  R.cache       0.16.0     2022-07-21 [1] CRAN (R 4.3.0)
#>  R.methodsS3   1.8.2      2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo          1.25.0     2022-06-12 [1] CRAN (R 4.3.0)
#>  R.utils       2.12.2     2022-11-11 [1] CRAN (R 4.3.0)
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.3.0)
#>  reprex        2.0.2      2022-08-17 [1] CRAN (R 4.3.0)
#>  rlang         1.1.1      2023-04-28 [1] CRAN (R 4.3.0)
#>  rmarkdown     2.23       2023-07-01 [1] CRAN (R 4.3.1)
#>  rprojroot     2.0.3      2022-04-02 [1] CRAN (R 4.3.0)
#>  rstudioapi    0.15.0     2023-07-07 [1] CRAN (R 4.3.1)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.3.0)
#>  styler        1.10.1     2023-06-05 [1] CRAN (R 4.3.1)
#>  testthat    * 3.2.0.9000 2023-11-07 [1] Github (r-lib/testthat@5659da8)
#>  utf8          1.2.4      2023-10-22 [1] CRAN (R 4.3.1)
#>  vctrs         0.6.4      2023-10-12 [1] CRAN (R 4.3.1)
#>  withr         2.5.2      2023-10-30 [1] CRAN (R 4.3.1)
#>  xfun          0.40       2023-08-09 [1] CRAN (R 4.3.1)
#>  yaml          2.3.7      2023-01-23 [1] CRAN (R 4.3.0)
#> 
#>  [1] /home/mikemahoney218/R/x86_64-pc-linux-gnu-library/4.3
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@mikemahoney218 mikemahoney218 changed the title expect_message() and expect_warning() do not check class? expect_message() and expect_warning() do not check class in testthat 2e? Nov 7, 2023
@mikemahoney218
Copy link
Author

This is only in edition 2:

library(testthat)
testthat::local_edition(3)

try(
  expect_message(message(), class = "not a match")
)
#> 
#> Error : `message()` did not throw the expected message.
try(
  expect_warning(warning(), class = "not a match")
)
#> Warning in eval_bare(quo_get_expr(.quo), quo_get_env(.quo)):
#> Error : `warning()` did not throw the expected warning.

Created on 2023-11-07 with reprex v2.0.2

@hadley
Copy link
Member

hadley commented Nov 8, 2023

Is this is a regression?

@mikemahoney218
Copy link
Author

I'm not sure, sorry. It exists in both CRAN and dev versions so it's not a recent one.

@hadley
Copy link
Member

hadley commented Nov 8, 2023

Yeah, I think this might be a long standing bug.

@hadley
Copy link
Member

hadley commented Nov 5, 2024

And since it's in the second edition, I'm not going to fix it.

@hadley hadley closed this as completed Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants