File: expect-invisible.R

package info (click to toggle)
r-cran-testthat 3.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,452 kB
  • sloc: cpp: 9,261; ansic: 37; sh: 14; makefile: 5
file content (45 lines) | stat: -rw-r--r-- 1,270 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
#' Does code return a visible or invisible object?
#'
#' Use this to test whether a function returns a visible or invisible
#' output. Typically you'll use this to check that functions called primarily
#' for their side-effects return their data argument invisibly.
#'
#' @param call A function call.
#' @inheritParams expect_that
#' @return The evaluated `call`, invisibly.
#' @export
#' @examples
#' expect_invisible(x <- 10)
#' expect_visible(x)
#'
#' # Typically you'll assign the result of the expectation so you can
#' # also check that the value is as you expect.
#' greet <- function(name) {
#'   message("Hi ", name)
#'   invisible(name)
#' }
#' out <- expect_invisible(greet("Hadley"))
#' expect_equal(out, "Hadley")
expect_invisible <- function(call, label = NULL) {
  lab <- label %||% expr_label(enexpr(call))
  vis <- withVisible(call)

  expect(
    identical(vis$visible, FALSE),
    sprintf("%s returns visibly, not invisibly.", lab)
  )
  invisible(vis$value)
}

#' @export
#' @rdname expect_invisible
expect_visible <- function(call, label = NULL) {
  lab <- label %||% expr_label(enexpr(call))
  vis <- withVisible(call)

  expect(
    identical(vis$visible, TRUE),
    sprintf("%s returns invisibly, not visibly.", lab)
  )
  invisible(vis$value)
}