-
Notifications
You must be signed in to change notification settings - Fork 340
Description
I'm currently working on some custom expectations for autogradeR at Jenny's recommendation during her workshop a few weeks ago, which involved taking a look at vignette("custom-expecatation") and then remembering it was actually in development.
I noticed that while R/expectations.R starts by pointing to vignette("custom-expecatation") for more explanation, is.expectation and new_expecation (which exist in the released version, 3.2.3), are not mentioned in the under-construction vignette (feels like a good time to mention?).
Out of curiosity, I wanted to see whether anything is currently an expectation (I had written some expectations and wanted to try it as a self-check, spooked myself for a minute, and then took a deeper look). From a regex assuming consistent naming in testthat, it looks like no expectations in testthat are currently "considered" expectations. Reprex using testthat v3.2.3 (sorry if in dev version):
library(testthat)
# Example expectation (ish) isn't an expectation:
expect_length |> is.expectation()
#> [1] FALSE
# testthat expectations (at least, most-ish)
expectations_testthat = ls("package:testthat") |>
stringr::str_subset(pattern = "expect") |>
as.list() |>
purrr::set_names() |>
lapply(FUN = \(x) paste0("testthat::", x) |> str2expression() |> eval())
# They're all functions (sanity check)
are_functions = sapply(X = expectations_testthat, FUN = is.function)
all(are_functions)
#> [1] TRUE
# But none are of class "expectation"
are_expectations = sapply(X = expectations_testthat, FUN = is.expectation)
any(are_expectations)
#> [1] FALSECreated on 2025-10-08 with reprex v2.1.1
Relatedly, it looks like testing the class of an expectation is not useful ATM (but might be if class applied)
library(testthat)
# won't work, although is.expectation uses inherits?
try(expect_s3_class(expect_length, class = "expectation"))
#> Error : `expect_length` is not an S3 object
# only base type ATM, so can't test class
sloop::otype(expect_length)
#> [1] "base"
test_that("Can test this", expect_type(expect_length, type = "closure"))
#> Test passed πCreated on 2025-10-08 with reprex v2.1.1
Tagging @hadley because he's been writing this vignette.