@@ -63,83 +63,6 @@ expectation <- function(type, message, srcref = NULL) {
63
63
# ' @param x object to test for class membership
64
64
is.expectation <- function (x ) inherits(x , " expectation" )
65
65
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
-
143
66
expectation_type <- function (exp ) {
144
67
stopifnot(is.expectation(exp ))
145
68
gsub(" ^expectation_" , " " , class(exp )[[1 ]])
0 commit comments