Skip to content

Compound expectationsΒ #2132

@hadley

Description

@hadley

We currently lack a good way to create compound expectations. The simplest approach has two problems:

expect_numeric_length_10 <- function(x) {
   expect_type(x, "numeric")
   expect_length(x, 10)
}
  1. It can generate two successes or two failures
  2. The trace_env is incorrect so you get an unnecessary backtrace

We don't suffer too much from 1. in testthat itself but we do have a lot of 2. Mostly in helpers (where we do set trace_env correctly:

  • expect_true() β†’ calls expect_waldo_constant()
  • expect_false() β†’ calls expect_waldo_constant()
  • expect_null() β†’ calls expect_waldo_constant()
  • expect_equal() β†’ calls expect_waldo_equal()
  • expect_identical() β†’ calls expect_waldo_equal()
  • expect_error() β†’ calls expect_condition_matching()
  • expect_warning() β†’ calls expect_condition_matching()
  • expect_message() β†’ calls expect_condition_matching()
  • expect_condition() β†’ calls expect_condition_matching()
  • expect_no_error() β†’ calls expect_no_()
  • expect_no_warning() β†’ calls expect_no_()
  • expect_no_message() β†’ calls expect_no_()
  • expect_no_condition() β†’ calls expect_no_()

But there are a few places where we call exported expect_ functions and hence can't control the trace_env

  • expect_mapequal() β†’ calls expect_equal()
  • expect_output() β†’ calls expect_match()
  • expect_snapshot_failure() β†’ calls expect_snapshot_error()
  • expect_snapshot_skip() β†’ calls expect_snapshot_error()
  • expect_skip() β†’ calls expect_condition()
  • expect_no_skip() β†’ calls expect_no_condition()

It would be nice to have general solution to using an existing expectation inside a new expectation.

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