File: local_test_context.Rd

package info (click to toggle)
r-cran-testthat 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,996 kB
  • sloc: cpp: 9,265; ansic: 37; sh: 15; makefile: 5
file content (97 lines) | stat: -rw-r--r-- 3,821 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/local.R
\name{local_test_context}
\alias{local_test_context}
\alias{local_reproducible_output}
\title{Locally set options for maximal test reproducibility}
\usage{
local_test_context(.env = parent.frame())

local_reproducible_output(
  width = 80,
  crayon = FALSE,
  unicode = FALSE,
  .env = parent.frame()
)
}
\arguments{
\item{.env}{Environment to use for scoping; expert use only.}

\item{width}{Value of the \code{"width"} option.}

\item{crayon}{Value of the \code{"crayon.enabled"} option.}

\item{unicode}{Value of the \code{"cli.unicode"} option.
The test is skipped if \code{l10n_info()$`UTF-8`} is \code{FALSE}.}
}
\description{
\code{local_test_context()} is run automatically by \code{test_that()} but you may
want to run it yourself if you want to replicate test results interactively.
If run inside a function, the effects are automatically reversed when the
function exits; if running in the global environment, use
\code{\link[withr:defer]{withr::deferred_run()}} to undo.

\code{local_reproducible_output()} is run automatically by \code{test_that()} in the
3rd edition. You might want to call it to override the the default settings
inside a test, if you want to test Unicode and coloured output or
non-standard width.
}
\details{
\code{local_test_context()} and \code{local_reproducible_output()}
set the following options with \code{\link[withr:with_options]{withr::local_options()}}:
\itemize{
\item \code{cli.unicode} (default: \code{FALSE}) so that the cli package never generates unicode
output (normally cli uses unicode on Linux/Mac but not Windows).
Windows can't easily save unicode output to disk, so it must be set to
false for consistency.
\item \code{crayon.enabled} (default: \code{FALSE}) suppresses ANSI colours generated by the crayon
package (normally colours are used if crayon detects that you're in a
terminal that supports colour).
\item \code{width} (default: 80) to control the width of printed output (usually this
varies with the size of your console).
}

In addition, \code{local_test_context()} sets the following options:
\itemize{
\item \code{cli.dynamic = FALSE} so that tests assume that they are not run in
a dynamic console (i.e. one where you can move the cursor around).
\item \code{lifecycle_verbosity = "warning"} so that every lifecycle problem always
generates a warning (otherwise deprecated functions don't generate a
warning every time).
\item \code{OutDec = "."} so numbers always uses \code{.} as the decimal point
(European users sometimes set \code{OutDec = ","}.)
\item \code{rlang_interactive = FALSE} so that \code{\link[rlang:is_interactive]{rlang::is_interactive()}} returns
\code{FALSE}, and code that uses it assumes you're in a non-interactive
environment.
\item \code{useFancyQuotes = FALSE} so base R functions always use regular (straight)
quotes (otherwise the default is locale dependent, see \code{\link[=sQuote]{sQuote()}} for
details).
}

And modifies the following env vars:
\itemize{
\item Unsets \code{RSTUDIO}, which ensures that RStudio is never detected as running.
\item Sets \code{TESTTHAT = "true"}, which ensures that \code{\link[=is_testing]{is_testing()}} returns \code{TRUE}.
\item Sets \code{LANGUAGE = "en"}, which ensures that no message translation occurs.
}

Finally, it sets the collation locale to "C", which ensures that character
sorting the same regardless of system locale.
}
\examples{
local({
  local_test_context()
  cat(crayon::blue("Text will not be colored"))
  cat(cli::symbol$ellipsis)
  cat("\n")
})

ellipsis <- cli::symbol$ellipsis
test_that("test ellipsis", {
  expect_equal(ellipsis, cli::symbol$ellipsis)

  local_reproducible_output(unicode = TRUE)
  expect_equal(ellipsis, cli::symbol$ellipsis)
})
}
\keyword{internal}