Skip to content

custom-expectation vignette: is.expectation and new_expectation unexplained and unusedΒ #2264

@math-mcshane

Description

@math-mcshane

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] FALSE

Created 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions