Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions R/expect-comparison.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,35 @@ expect_compare_ <- function(
#' @export
#' @rdname comparison-expectations
expect_lt <- function(object, expected, label = NULL, expected.label = NULL) {
act <- quasi_label(enquo(object), label, arg = "object")
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
act <- quasi_label(enquo(object), label)
exp <- quasi_label(enquo(expected), expected.label)

expect_compare_("<", act, exp)
}

#' @export
#' @rdname comparison-expectations
expect_lte <- function(object, expected, label = NULL, expected.label = NULL) {
act <- quasi_label(enquo(object), label, arg = "object")
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
act <- quasi_label(enquo(object), label)
exp <- quasi_label(enquo(expected), expected.label)

expect_compare_("<=", act, exp)
}

#' @export
#' @rdname comparison-expectations
expect_gt <- function(object, expected, label = NULL, expected.label = NULL) {
act <- quasi_label(enquo(object), label, arg = "object")
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
act <- quasi_label(enquo(object), label)
exp <- quasi_label(enquo(expected), expected.label)

expect_compare_(">", act, exp)
}

#' @export
#' @rdname comparison-expectations
expect_gte <- function(object, expected, label = NULL, expected.label = NULL) {
act <- quasi_label(enquo(object), label, arg = "object")
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
act <- quasi_label(enquo(object), label)
exp <- quasi_label(enquo(expected), expected.label)

expect_compare_(">=", act, exp)
}
Expand Down
6 changes: 3 additions & 3 deletions R/expect-constant.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ NULL
#' @export
#' @rdname logical-expectations
expect_true <- function(object, info = NULL, label = NULL) {
act <- quasi_label(enquo(object), label, arg = "object")
act <- quasi_label(enquo(object), label)
exp <- labelled_value(TRUE, "TRUE")
expect_waldo_equal_("equal", act, exp, info = info, ignore_attr = TRUE)
}

#' @export
#' @rdname logical-expectations
expect_false <- function(object, info = NULL, label = NULL) {
act <- quasi_label(enquo(object), label, arg = "object")
act <- quasi_label(enquo(object), label)
exp <- labelled_value(FALSE, "FALSE")
expect_waldo_equal_("equal", act, exp, info = info, ignore_attr = TRUE)
}
Expand All @@ -57,7 +57,7 @@ expect_false <- function(object, info = NULL, label = NULL) {
#' expect_null(x)
#' show_failure(expect_null(y))
expect_null <- function(object, info = NULL, label = NULL) {
act <- quasi_label(enquo(object), label, arg = "object")
act <- quasi_label(enquo(object), label)
exp <- labelled_value(NULL, "FALSE")
expect_waldo_equal_("equal", act, exp, info = info)
}
12 changes: 6 additions & 6 deletions R/expect-equality.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ expect_equal <- function(
label = NULL,
expected.label = NULL
) {
act <- quasi_label(enquo(object), label, arg = "object")
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
act <- quasi_label(enquo(object), label)
exp <- quasi_label(enquo(expected), expected.label)

if (edition_get() >= 3) {
expect_waldo_equal_("equal", act, exp, info, ..., tolerance = tolerance)
Expand Down Expand Up @@ -93,8 +93,8 @@ expect_identical <- function(
expected.label = NULL,
...
) {
act <- quasi_label(enquo(object), label, arg = "object")
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
act <- quasi_label(enquo(object), label)
exp <- quasi_label(enquo(expected), expected.label)

if (edition_get() >= 3) {
expect_waldo_equal_("identical", act, exp, info, ...)
Expand Down Expand Up @@ -180,8 +180,8 @@ expect_equivalent <- function(
label = NULL,
expected.label = NULL
) {
act <- quasi_label(enquo(object), label, arg = "object")
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
act <- quasi_label(enquo(object), label)
exp <- quasi_label(enquo(expected), expected.label)

edition_deprecate(
3,
Expand Down
10 changes: 5 additions & 5 deletions R/expect-inheritance.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ NULL
expect_type <- function(object, type) {
stopifnot(is.character(type), length(type) == 1)

act <- quasi_label(enquo(object), arg = "object")
act <- quasi_label(enquo(object))
act_type <- typeof(act$val)

if (!identical(act_type, type)) {
Expand All @@ -69,7 +69,7 @@ expect_type <- function(object, type) {
#' from `class`. If `TRUE`, checks that object has a class that's identical
#' to `class`.
expect_s3_class <- function(object, class, exact = FALSE) {
act <- quasi_label(enquo(object), arg = "object")
act <- quasi_label(enquo(object))
act$class <- format_class(class(act$val))
exp_lab <- format_class(class)

Expand Down Expand Up @@ -112,7 +112,7 @@ expect_s7_class <- function(object, class) {
stop_input_type(class, "an S7 class object")
}

act <- quasi_label(enquo(object), arg = "object")
act <- quasi_label(enquo(object))

if (!S7::S7_inherits(object)) {
return(fail(sprintf("%s is not an S7 object", act$lab)))
Expand All @@ -136,7 +136,7 @@ expect_s7_class <- function(object, class) {
#' @export
#' @rdname inheritance-expectations
expect_s4_class <- function(object, class) {
act <- quasi_label(enquo(object), arg = "object")
act <- quasi_label(enquo(object))
act$class <- format_class(methods::is(act$val))
exp_lab <- format_class(class)

Expand Down Expand Up @@ -194,7 +194,7 @@ expect_is <- function(object, class, info = NULL, label = NULL) {
"Use `expect_type()`, `expect_s3_class()`, or `expect_s4_class()` instead"
)

act <- quasi_label(enquo(object), label, arg = "object")
act <- quasi_label(enquo(object), label)
act$class <- format_class(class(act$val))
exp_lab <- format_class(class(class))

Expand Down
4 changes: 2 additions & 2 deletions R/expect-known.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ expect_known_value <- function(
"Please use `expect_snapshot_value()` instead"
)

act <- quasi_label(enquo(object), label, arg = "object")
act <- quasi_label(enquo(object), label)

if (!file.exists(file)) {
warning("Creating reference value", call. = FALSE)
Expand Down Expand Up @@ -214,7 +214,7 @@ expect_known_hash <- function(object, hash = NULL) {
"Please use `expect_snapshot_value()` instead"
)

act <- quasi_label(enquo(object), arg = "object")
act <- quasi_label(enquo(object))
act_hash <- digest::digest(act$val)
if (!is.null(hash)) {
act_hash <- substr(act_hash, 1, nchar(hash))
Expand Down
2 changes: 1 addition & 1 deletion R/expect-length.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
expect_length <- function(object, n) {
stopifnot(is.numeric(n), length(n) == 1)

act <- quasi_label(enquo(object), arg = "object")
act <- quasi_label(enquo(object))
act$n <- length(act$val)

if (act$n != n) {
Expand Down
4 changes: 2 additions & 2 deletions R/expect-match.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ expect_match <- function(
info = NULL,
label = NULL
) {
act <- quasi_label(enquo(object), label, arg = "object")
act <- quasi_label(enquo(object), label)

check_character(object)
check_string(regexp)
Expand Down Expand Up @@ -76,7 +76,7 @@ expect_no_match <- function(
label = NULL
) {
# Capture here to avoid environment-related messiness
act <- quasi_label(enquo(object), label, arg = "object")
act <- quasi_label(enquo(object), label)
stopifnot(is.character(regexp), length(regexp) == 1)
stopifnot(is.character(act$val))

Expand Down
2 changes: 1 addition & 1 deletion R/expect-named.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ expect_named <- function(
info = NULL,
label = NULL
) {
act <- quasi_label(enquo(object), label, arg = "object")
act <- quasi_label(enquo(object), label)
act$names <- names(act$val)

if (missing(expected)) {
Expand Down
4 changes: 2 additions & 2 deletions R/expect-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ expect_reference <- function(
) {
edition_deprecate(3, "expect_reference()")

act <- quasi_label(enquo(object), label, arg = "object")
exp <- quasi_label(enquo(expected), expected.label, arg = "expected")
act <- quasi_label(enquo(object), label)
exp <- quasi_label(enquo(expected), expected.label)

if (!is_reference(act$val, exp$val)) {
msg <- sprintf("%s not a reference to %s.", act$lab, exp$lab)
Expand Down
16 changes: 8 additions & 8 deletions R/expect-setequal.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#' show_failure(expect_mapequal(x, list(a = 1, b = "x")))
#' show_failure(expect_mapequal(x, list(a = 1, b = 2, c = 3)))
expect_setequal <- function(object, expected) {
act <- quasi_label(enquo(object), arg = "object")
exp <- quasi_label(enquo(expected), arg = "expected")
act <- quasi_label(enquo(object))
exp <- quasi_label(enquo(expected))

if (!is_vector(act$val) || !is_vector(exp$val)) {
abort("`object` and `expected` must both be vectors")
Expand Down Expand Up @@ -76,8 +76,8 @@ is_vector <- function(x) is.list(x) || (is.atomic(x) && !is.null(x))
#' @export
#' @rdname expect_setequal
expect_mapequal <- function(object, expected) {
act <- quasi_label(enquo(object), arg = "object")
exp <- quasi_label(enquo(expected), arg = "expected")
act <- quasi_label(enquo(object))
exp <- quasi_label(enquo(expected))

if (!is_vector(act$val) || !is_vector(exp$val)) {
abort("`object` and `expected` must both be vectors")
Expand Down Expand Up @@ -126,8 +126,8 @@ check_names_ok <- function(x, label) {
#' @export
#' @rdname expect_setequal
expect_contains <- function(object, expected) {
act <- quasi_label(enquo(object), arg = "object")
exp <- quasi_label(enquo(expected), arg = "expected")
act <- quasi_label(enquo(object))
exp <- quasi_label(enquo(expected))

if (!is_vector(act$val) || !is_vector(exp$val)) {
abort("`object` and `expected` must both be vectors")
Expand All @@ -152,8 +152,8 @@ expect_contains <- function(object, expected) {
#' @export
#' @rdname expect_setequal
expect_in <- function(object, expected) {
act <- quasi_label(enquo(object), arg = "object")
exp <- quasi_label(enquo(expected), arg = "expected")
act <- quasi_label(enquo(object))
exp <- quasi_label(enquo(expected))

if (!is_vector(act$val) || !is_vector(exp$val)) {
abort("`object` and `expected` must both be vectors")
Expand Down
2 changes: 1 addition & 1 deletion R/expect-shape.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
expect_shape = function(object, ..., nrow, ncol, dim) {
check_dots_empty()
check_exclusive(nrow, ncol, dim)
act <- quasi_label(enquo(object), arg = "object")
act <- quasi_label(enquo(object))

dim_object <- base::dim(object)
if (is.null(dim_object)) {
Expand Down
2 changes: 1 addition & 1 deletion R/expect-vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' show_failure(expect_vector(1:10, ptype = character(), size = 5))
expect_vector <- function(object, ptype = NULL, size = NULL) {
check_installed("vctrs")
act <- quasi_label(enquo(object), arg = "object")
act <- quasi_label(enquo(object))

message <- NULL
tryCatch(
Expand Down
15 changes: 13 additions & 2 deletions R/quasi-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@
#' # !!. This causes the failure message to show the value rather than the
#' # variable name
#' show_failure(expect_equal(f(!!i), !!(i * 10)))
quasi_label <- function(quo, label = NULL, arg = "quo") {
quasi_label <- function(quo, label = NULL, arg = NULL) {
if (is.null(arg)) {
arg <- substitute(quo)
if (is_call(arg, "enquo")) {
arg <- arg[[2]]
}
arg <- deparse1(arg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slightly icky but ok I guess?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use as_label() instead of deparse1()?

}

force(quo)
if (quo_is_missing(quo)) {
stop("argument `", arg, "` is missing, with no default.", call. = FALSE)
cli::cli_abort(
"argument {.arg {arg}} is missing, with no default.",
call = caller_env()
)
}

expr <- quo_get_expr(quo)
Expand Down
2 changes: 1 addition & 1 deletion man/quasi_label.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/quasi-label.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
Code
expect_equal()
Condition
Error:
Error in `expect_equal()`:
! argument `object` is missing, with no default.

16 changes: 8 additions & 8 deletions tests/testthat/_snaps/reporter-progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
Backtrace:
x
1. +-testthat::expect_s3_class(foo(), "foo")
2. | \-testthat::quasi_label(enquo(object), arg = "object")
2. | \-testthat::quasi_label(enquo(object))
3. | +-testthat:::labelled_value(...)
4. | \-rlang::eval_bare(expr, quo_get_env(quo))
5. \-foo()
Expand All @@ -277,13 +277,13 @@
Backtrace:
x
1. +-testthat::expect_s3_class(f(), "foo")
2. | \-testthat::quasi_label(enquo(object), arg = "object")
2. | \-testthat::quasi_label(enquo(object))
3. | +-testthat:::labelled_value(...)
4. | \-rlang::eval_bare(expr, quo_get_env(quo))
5. \-f()
6. \-g()
7. +-testthat::expect_s3_class(foo(), "foo")
8. | \-testthat::quasi_label(enquo(object), arg = "object")
8. | \-testthat::quasi_label(enquo(object))
9. | +-testthat:::labelled_value(...)
10. | \-rlang::eval_bare(expr, quo_get_env(quo))
11. \-foo()
Expand All @@ -295,7 +295,7 @@
1. \-f()
2. \-g()
3. +-testthat::expect_s3_class(foo(), "foo")
4. | \-testthat::quasi_label(enquo(object), arg = "object")
4. | \-testthat::quasi_label(enquo(object))
5. | +-testthat:::labelled_value(...)
6. | \-rlang::eval_bare(expr, quo_get_env(quo))
7. \-foo()
Expand Down Expand Up @@ -398,7 +398,7 @@
Backtrace:
x
1. +-testthat::expect_s3_class(foo(), "foo")
2. | \-testthat::quasi_label(enquo(object), arg = "object")
2. | \-testthat::quasi_label(enquo(object))
3. | +-testthat:::labelled_value(...)
4. | \-rlang::eval_bare(expr, quo_get_env(quo))
5. \-foo()
Expand All @@ -408,13 +408,13 @@
Backtrace:
x
1. +-testthat::expect_s3_class(f(), "foo")
2. | \-testthat::quasi_label(enquo(object), arg = "object")
2. | \-testthat::quasi_label(enquo(object))
3. | +-testthat:::labelled_value(...)
4. | \-rlang::eval_bare(expr, quo_get_env(quo))
5. \-f()
6. \-g()
7. +-testthat::expect_s3_class(foo(), "foo")
8. | \-testthat::quasi_label(enquo(object), arg = "object")
8. | \-testthat::quasi_label(enquo(object))
9. | +-testthat:::labelled_value(...)
10. | \-rlang::eval_bare(expr, quo_get_env(quo))
11. \-foo()
Expand All @@ -426,7 +426,7 @@
1. \-f()
2. \-g()
3. +-testthat::expect_s3_class(foo(), "foo")
4. | \-testthat::quasi_label(enquo(object), arg = "object")
4. | \-testthat::quasi_label(enquo(object))
5. | +-testthat:::labelled_value(...)
6. | \-rlang::eval_bare(expr, quo_get_env(quo))
7. \-foo()
Expand Down
2 changes: 1 addition & 1 deletion vignettes/custom-expectation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ There are three main parts to writing an expectation, as illustrated by `expect_
```{r}
expect_length <- function(object, n) {
# 1. Capture object and label
act <- quasi_label(rlang::enquo(object), arg = "object")
act <- quasi_label(rlang::enquo(object))

# 2. Verify the expectations
act_n <- length(act$val)
Expand Down
Loading