File: fail.Rd

package info (click to toggle)
r-cran-testthat 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,048 kB
  • sloc: cpp: 9,269; sh: 14; ansic: 14; makefile: 5
file content (60 lines) | stat: -rw-r--r-- 1,948 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/expect-that.R
\name{fail}
\alias{fail}
\alias{pass}
\title{Declare that an expectation either passes or fails}
\usage{
fail(
  message = "Failure has been forced",
  info = NULL,
  srcref = NULL,
  trace_env = caller_env(),
  trace = NULL
)

pass()
}
\arguments{
\item{message}{A character vector describing the failure. The
first element should describe the expected value, and the second (and
optionally subsequence) elements should describe what was actually seen.}

\item{info}{Character vector continuing additional information. Included
for backward compatibility only and new expectations should not use it.}

\item{srcref}{Location of the failure. Should only needed to be explicitly
supplied when you need to forward a srcref captured elsewhere.}

\item{trace_env}{If \code{trace} is not specified, this is used to generate an
informative traceback for failures. You should only need to set this if
you're calling \code{fail()} from a helper function; see
\code{vignette("custom-expectation")} for details.}

\item{trace}{An optional backtrace created by \code{\link[rlang:trace_back]{rlang::trace_back()}}.
When supplied, the expectation is displayed with the backtrace.
Expert use only.}
}
\description{
These are the primitives that you can use to implement your own expectations.
Every path through an expectation should either call \code{pass()}, \code{fail()},
or throw an error (e.g. if the arguments are invalid). Expectations should
always return \code{invisible(act$val)}.

Learn more about creating your own expectations in
\code{vignette("custom-expectation")}.
}
\examples{
expect_length <- function(object, n) {
  act <- quasi_label(rlang::enquo(object), arg = "object")

  act_n <- length(act$val)
  if (act_n != n) {
    fail(sprintf("\%s has length \%i, not length \%i.", act$lab, act_n, n))
  } else {
    pass()
  }

  invisible(act$val)
}
}