Skip to content

Commit 84e5fe5

Browse files
committed
Move quasi_label to its own file
1 parent ef98a3e commit 84e5fe5

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Collate:
6969
'mock.R'
7070
'old-school.R'
7171
'praise.R'
72+
'quasi-label.R'
7273
'recover.R'
7374
'reporter-check.R'
7475
'reporter-debug.R'

R/expectation.R

-77
Original file line numberDiff line numberDiff line change
@@ -63,83 +63,6 @@ expectation <- function(type, message, srcref = NULL) {
6363
#' @param x object to test for class membership
6464
is.expectation <- function(x) inherits(x, "expectation")
6565

66-
#' Quasi-labelling
67-
#'
68-
#' The first argument to every `expect_` function can use unquoting to
69-
#' construct better labels. This makes it easy to create informative labels
70-
#' expectations are used inside a function or a for loop. `quasi_label()` wraps
71-
#' up the details, returning the expression and label.
72-
#'
73-
#' @section Limitations:
74-
#' Because all `expect_` function use unquoting to generate more informative
75-
#' labels, you can not use unquoting for other purposes. Instead, you'll need
76-
#' to perform all other unquoting outside of the expectation and only test
77-
#' the results.
78-
#'
79-
#' @param quo A quosure created by `rlang::enquo()`.
80-
#' @param label An optional label to override the default. This is
81-
#' only provided for internal usage. Modern expectations should not
82-
#' include a `label` parameter.
83-
#' @param arg Argument name shown in error message if `quo` is missing.
84-
#' @keywords internal
85-
#' @return A list containing two elements:
86-
#' \item{val}{The evaluate value of `quo`}
87-
#' \item{lab}{The quasiquoted label generated from `quo`}
88-
#' @export
89-
#' @examples
90-
#' f <- function(i) if (i > 3) i * 9 else i * 10
91-
#' i <- 10
92-
#'
93-
#' # This short of expression commonly occurs inside a for loop or function
94-
#' # And the failure isn't helpful because you can't see the value of i
95-
#' # that caused the problem:
96-
#' show_failure(expect_equal(f(i), i * 10))
97-
#'
98-
#' # To overcome this issue, testthat allows you to unquote expressions using
99-
#' # !!. This causes the failure message to show the value rather than the
100-
#' # variable name
101-
#' show_failure(expect_equal(f(!!i), !!(i * 10)))
102-
quasi_label <- function(quo, label = NULL, arg = "quo") {
103-
force(quo)
104-
if (quo_is_missing(quo)) {
105-
stop("argument `", arg, "` is missing, with no default.", call. = FALSE)
106-
}
107-
108-
list(
109-
val = eval_bare(get_expr(quo), get_env(quo)),
110-
lab = label %||% expr_label(get_expr(quo))
111-
)
112-
}
113-
114-
quasi_capture <- function(.quo, .label, .capture, ...) {
115-
act <- list()
116-
act$lab <- .label %||% quo_label(.quo)
117-
act$cap <- .capture(act$val <- eval_bare(get_expr(.quo), get_env(.quo)), ...)
118-
119-
act
120-
}
121-
122-
expr_label <- function(x) {
123-
if (is.character(x)) {
124-
encodeString(x, quote = '"')
125-
} else if (is.atomic(x)) {
126-
format(x)
127-
} else if (is.name(x)) {
128-
paste0("`", as.character(x), "`")
129-
} else {
130-
chr <- deparse(x)
131-
if (length(chr) > 1) {
132-
if (identical(x[[1]], quote(`function`))) {
133-
x[[3]] <- quote(...)
134-
chr <- paste(deparse(x), collapse = "\n")
135-
} else {
136-
chr <- paste(deparse(as.call(list(x[[1]], quote(...)))), collapse = "\n")
137-
}
138-
}
139-
chr
140-
}
141-
}
142-
14366
expectation_type <- function(exp) {
14467
stopifnot(is.expectation(exp))
14568
gsub("^expectation_", "", class(exp)[[1]])

R/quasi-label.R

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#' Quasi-labelling
2+
#'
3+
#' The first argument to every `expect_` function can use unquoting to
4+
#' construct better labels. This makes it easy to create informative labels
5+
#' expectations are used inside a function or a for loop. `quasi_label()` wraps
6+
#' up the details, returning the expression and label.
7+
#'
8+
#' @section Limitations:
9+
#' Because all `expect_` function use unquoting to generate more informative
10+
#' labels, you can not use unquoting for other purposes. Instead, you'll need
11+
#' to perform all other unquoting outside of the expectation and only test
12+
#' the results.
13+
#'
14+
#' @param quo A quosure created by `rlang::enquo()`.
15+
#' @param label An optional label to override the default. This is
16+
#' only provided for internal usage. Modern expectations should not
17+
#' include a `label` parameter.
18+
#' @param arg Argument name shown in error message if `quo` is missing.
19+
#' @keywords internal
20+
#' @return A list containing two elements:
21+
#' \item{val}{The evaluate value of `quo`}
22+
#' \item{lab}{The quasiquoted label generated from `quo`}
23+
#' @export
24+
#' @examples
25+
#' f <- function(i) if (i > 3) i * 9 else i * 10
26+
#' i <- 10
27+
#'
28+
#' # This short of expression commonly occurs inside a for loop or function
29+
#' # And the failure isn't helpful because you can't see the value of i
30+
#' # that caused the problem:
31+
#' show_failure(expect_equal(f(i), i * 10))
32+
#'
33+
#' # To overcome this issue, testthat allows you to unquote expressions using
34+
#' # !!. This causes the failure message to show the value rather than the
35+
#' # variable name
36+
#' show_failure(expect_equal(f(!!i), !!(i * 10)))
37+
quasi_label <- function(quo, label = NULL, arg = "quo") {
38+
force(quo)
39+
if (quo_is_missing(quo)) {
40+
stop("argument `", arg, "` is missing, with no default.", call. = FALSE)
41+
}
42+
43+
list(
44+
val = eval_bare(get_expr(quo), get_env(quo)),
45+
lab = label %||% expr_label(get_expr(quo))
46+
)
47+
}
48+
49+
quasi_capture <- function(.quo, .label, .capture, ...) {
50+
act <- list()
51+
act$lab <- .label %||% quo_label(.quo)
52+
act$cap <- .capture(act$val <- eval_bare(get_expr(.quo), get_env(.quo)), ...)
53+
54+
act
55+
}
56+
57+
expr_label <- function(x) {
58+
if (is.character(x)) {
59+
encodeString(x, quote = '"')
60+
} else if (is.atomic(x)) {
61+
format(x)
62+
} else if (is.name(x)) {
63+
paste0("`", as.character(x), "`")
64+
} else {
65+
chr <- deparse(x)
66+
if (length(chr) > 1) {
67+
if (identical(x[[1]], quote(`function`))) {
68+
x[[3]] <- quote(...)
69+
chr <- paste(deparse(x), collapse = "\n")
70+
} else {
71+
chr <- paste(deparse(as.call(list(x[[1]], quote(...)))), collapse = "\n")
72+
}
73+
}
74+
chr
75+
}
76+
}

man/quasi_label.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)