diff --git a/R/append.R b/R/append.R index 6d4e36019..a308372fa 100644 --- a/R/append.R +++ b/R/append.R @@ -38,7 +38,8 @@ df_append <- function(x, y, after = NULL, remove = FALSE) { after <- match(after, x_names) } - check_number_whole(after, min = 0L, max = n, .internal = TRUE) + # r-lib/rlang#1702 + check_number_whole(after, min = 0, max = as.numeric(n), .internal = TRUE) if (remove) { lhs <- seq2(1L, after - 1L) diff --git a/R/compat-lazyeval.R b/R/import-standalone-lazyeval.R similarity index 81% rename from R/compat-lazyeval.R rename to R/import-standalone-lazyeval.R index 67fc4936a..f3e94f02c 100644 --- a/R/compat-lazyeval.R +++ b/R/import-standalone-lazyeval.R @@ -1,8 +1,19 @@ -# nocov start - compat-lazyeval (last updated: rlang 0.3.0) - +# Standalone file: do not edit by hand +# Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-lazyeval.R +# Generated by: usethis::use_standalone("r-lib/rlang", "lazyeval") +# ---------------------------------------------------------------------- +# +# --- +# repo: r-lib/rlang +# file: standalone-lazyeval.R +# last-updated: 2018-09-18 +# license: https://unlicense.org +# imports: rlang +# --- +# # This file serves as a reference for compatibility functions for lazyeval. -# Please find the most recent version in rlang's repository. - +# +# nocov start warn_underscored <- function() { return(NULL) @@ -48,7 +59,7 @@ compat_lazy <- function(lazy, env = caller_env(), warn = TRUE) { }, list = if (inherits(lazy, "lazy")) { - lazy <- new_quosure(lazy$expr, lazy$env) + lazy = new_quosure(lazy$expr, lazy$env) } ) diff --git a/R/compat-obj-type.R b/R/import-standalone-obj-type.R similarity index 89% rename from R/compat-obj-type.R rename to R/import-standalone-obj-type.R index d86dca51b..47268d620 100644 --- a/R/compat-obj-type.R +++ b/R/import-standalone-obj-type.R @@ -1,7 +1,26 @@ -# nocov start --- r-lib/rlang compat-obj-type +# Standalone file: do not edit by hand +# Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-obj-type.R +# Generated by: usethis::use_standalone("r-lib/rlang", "obj-type") +# ---------------------------------------------------------------------- # -# Changelog -# ========= +# --- +# repo: r-lib/rlang +# file: standalone-obj-type.R +# last-updated: 2024-02-14 +# license: https://unlicense.org +# imports: rlang (>= 1.1.0) +# --- +# +# ## Changelog +# +# 2024-02-14: +# - `obj_type_friendly()` now works for S7 objects. +# +# 2023-05-01: +# - `obj_type_friendly()` now only displays the first class of S3 objects. +# +# 2023-03-30: +# - `stop_input_type()` now handles `I()` input literally in `arg`. # # 2022-10-04: # - `obj_type_friendly(value = TRUE)` now shows numeric scalars @@ -36,7 +55,8 @@ # - Added support for matrices and arrays (#141). # - Added documentation. # - Added changelog. - +# +# nocov start #' Return English-friendly type #' @param x Any R object. @@ -55,7 +75,7 @@ obj_type_friendly <- function(x, value = TRUE) { if (inherits(x, "quosure")) { type <- "quosure" } else { - type <- paste(class(x), collapse = "/") + type <- class(x)[[1L]] } return(sprintf("a <%s> object", type)) } @@ -251,19 +271,19 @@ vec_type_friendly <- function(x, length = FALSE) { #' Return OO type #' @param x Any R object. #' @return One of `"bare"` (for non-OO objects), `"S3"`, `"S4"`, -#' `"R6"`, or `"R7"`. +#' `"R6"`, or `"S7"`. #' @noRd obj_type_oo <- function(x) { if (!is.object(x)) { return("bare") } - class <- inherits(x, c("R6", "R7_object"), which = TRUE) + class <- inherits(x, c("R6", "S7_object"), which = TRUE) if (class[[1]]) { "R6" } else if (class[[2]]) { - "R7" + "S7" } else if (isS4(x)) { "S4" } else { @@ -288,7 +308,7 @@ stop_input_type <- function(x, show_value = TRUE, arg = caller_arg(x), call = caller_env()) { - # From compat-cli.R + # From standalone-cli.R cli <- env_get_list( nms = c("format_arg", "format_code"), last = topenv(), @@ -305,10 +325,15 @@ stop_input_type <- function(x, if (length(what)) { what <- oxford_comma(what) } + if (inherits(arg, "AsIs")) { + format_arg <- identity + } else { + format_arg <- cli$format_arg + } message <- sprintf( "%s must be %s, not %s.", - cli$format_arg(arg), + format_arg(arg), what, obj_type_friendly(x, value = show_value) ) diff --git a/R/compat-types-check.R b/R/import-standalone-types-check.R similarity index 65% rename from R/compat-types-check.R rename to R/import-standalone-types-check.R index 3689ca5f2..ef8c5a1d5 100644 --- a/R/compat-types-check.R +++ b/R/import-standalone-types-check.R @@ -1,12 +1,40 @@ -# nocov start --- r-lib/rlang compat-types-check +# Standalone file: do not edit by hand +# Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-types-check.R +# Generated by: usethis::use_standalone("r-lib/rlang", "types-check") +# ---------------------------------------------------------------------- # -# Dependencies -# ============ +# --- +# repo: r-lib/rlang +# file: standalone-types-check.R +# last-updated: 2023-03-13 +# license: https://unlicense.org +# dependencies: standalone-obj-type.R +# imports: rlang (>= 1.1.0) +# --- # -# - compat-obj-type.R +# ## Changelog # -# Changelog -# ========= +# 2024-08-15: +# - `check_character()` gains an `allow_na` argument (@martaalcalde, #1724) +# +# 2023-03-13: +# - Improved error messages of number checkers (@teunbrand) +# - Added `allow_infinite` argument to `check_number_whole()` (@mgirlich). +# - Added `check_data_frame()` (@mgirlich). +# +# 2023-03-07: +# - Added dependency on rlang (>= 1.1.0). +# +# 2023-02-15: +# - Added `check_logical()`. +# +# - `check_bool()`, `check_number_whole()`, and +# `check_number_decimal()` are now implemented in C. +# +# - For efficiency, `check_number_whole()` and +# `check_number_decimal()` now take a `NULL` default for `min` and +# `max`. This makes it possible to bypass unnecessary type-checking +# and comparisons in the default case of no bounds checks. # # 2022-10-07: # - `check_number_whole()` and `_decimal()` no longer treat @@ -30,25 +58,21 @@ # # 2022-08-11: # - Added changelog. +# +# nocov start # Scalars ----------------------------------------------------------------- +.standalone_types_check_dot_call <- .Call + check_bool <- function(x, ..., allow_na = FALSE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { - if (!missing(x)) { - if (is_bool(x)) { - return(invisible(NULL)) - } - if (allow_null && is_null(x)) { - return(invisible(NULL)) - } - if (allow_na && identical(x, NA)) { - return(invisible(NULL)) - } + if (!missing(x) && .standalone_types_check_dot_call(ffi_standalone_is_bool_1.0.7, x, allow_na, allow_null)) { + return(invisible(NULL)) } stop_input_type( @@ -141,22 +165,41 @@ check_name <- function(x, ) } +IS_NUMBER_true <- 0 +IS_NUMBER_false <- 1 +IS_NUMBER_oob <- 2 + check_number_decimal <- function(x, ..., - min = -Inf, - max = Inf, + min = NULL, + max = NULL, allow_infinite = TRUE, allow_na = FALSE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { - .rlang_types_check_number( + if (missing(x)) { + exit_code <- IS_NUMBER_false + } else if (0 == (exit_code <- .standalone_types_check_dot_call( + ffi_standalone_check_number_1.0.7, + x, + allow_decimal = TRUE, + min, + max, + allow_infinite, + allow_na, + allow_null + ))) { + return(invisible(NULL)) + } + + .stop_not_number( x, ..., + exit_code = exit_code, + allow_decimal = TRUE, min = min, max = max, - allow_decimal = TRUE, - allow_infinite = allow_infinite, allow_na = allow_na, allow_null = allow_null, arg = arg, @@ -166,19 +209,35 @@ check_number_decimal <- function(x, check_number_whole <- function(x, ..., - min = -Inf, - max = Inf, + min = NULL, + max = NULL, + allow_infinite = FALSE, allow_na = FALSE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { - .rlang_types_check_number( + if (missing(x)) { + exit_code <- IS_NUMBER_false + } else if (0 == (exit_code <- .standalone_types_check_dot_call( + ffi_standalone_check_number_1.0.7, + x, + allow_decimal = FALSE, + min, + max, + allow_infinite, + allow_na, + allow_null + ))) { + return(invisible(NULL)) + } + + .stop_not_number( x, ..., + exit_code = exit_code, + allow_decimal = FALSE, min = min, max = max, - allow_decimal = FALSE, - allow_infinite = FALSE, allow_na = allow_na, allow_null = allow_null, arg = arg, @@ -186,23 +245,38 @@ check_number_whole <- function(x, ) } -.rlang_types_check_number <- function(x, - ..., - min = -Inf, - max = Inf, - allow_decimal = FALSE, - allow_infinite = FALSE, - allow_na = FALSE, - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +.stop_not_number <- function(x, + ..., + exit_code, + allow_decimal, + min, + max, + allow_na, + allow_null, + arg, + call) { if (allow_decimal) { what <- "a number" } else { what <- "a whole number" } - .stop <- function(x, what, ...) stop_input_type( + if (exit_code == IS_NUMBER_oob) { + min <- min %||% -Inf + max <- max %||% Inf + + if (min > -Inf && max < Inf) { + what <- sprintf("%s between %s and %s", what, min, max) + } else if (x < min) { + what <- sprintf("%s larger than or equal to %s", what, min) + } else if (x > max) { + what <- sprintf("%s smaller than or equal to %s", what, max) + } else { + abort("Unexpected state in OOB check", .internal = TRUE) + } + } + + stop_input_type( x, what, ..., @@ -211,66 +285,6 @@ check_number_whole <- function(x, arg = arg, call = call ) - - if (!missing(x)) { - is_number <- is_number( - x, - allow_decimal = allow_decimal, - allow_infinite = allow_infinite - ) - - if (is_number) { - if (min > -Inf && max < Inf) { - what <- sprintf("a number between %s and %s", min, max) - } else { - what <- NULL - } - if (x < min) { - what <- what %||% sprintf("a number larger than %s", min) - .stop(x, what, ...) - } - if (x > max) { - what <- what %||% sprintf("a number smaller than %s", max) - .stop(x, what, ...) - } - return(invisible(NULL)) - } - - if (allow_null && is_null(x)) { - return(invisible(NULL)) - } - if (allow_na && (identical(x, NA) || - identical(x, na_dbl) || - identical(x, na_int))) { - return(invisible(NULL)) - } - } - - .stop(x, what, ...) -} - -is_number <- function(x, - allow_decimal = FALSE, - allow_infinite = FALSE) { - if (!typeof(x) %in% c("integer", "double")) { - return(FALSE) - } - if (!is.numeric(x)) { - return(FALSE) - } - if (length(x) != 1) { - return(FALSE) - } - if (is.na(x)) { - return(FALSE) - } - if (!allow_decimal && !is_integerish(x)) { - return(FALSE) - } - if (!allow_infinite && is.infinite(x)) { - return(FALSE) - } - TRUE } check_symbol <- function(x, @@ -291,6 +305,7 @@ check_symbol <- function(x, x, "a symbol", ..., + allow_na = FALSE, allow_null = allow_null, arg = arg, call = call @@ -315,6 +330,7 @@ check_arg <- function(x, x, "an argument name", ..., + allow_na = FALSE, allow_null = allow_null, arg = arg, call = call @@ -339,6 +355,7 @@ check_call <- function(x, x, "a defused call", ..., + allow_na = FALSE, allow_null = allow_null, arg = arg, call = call @@ -363,6 +380,7 @@ check_environment <- function(x, x, "an environment", ..., + allow_na = FALSE, allow_null = allow_null, arg = arg, call = call @@ -387,6 +405,7 @@ check_function <- function(x, x, "a function", ..., + allow_na = FALSE, allow_null = allow_null, arg = arg, call = call @@ -411,6 +430,7 @@ check_closure <- function(x, x, "an R function", ..., + allow_na = FALSE, allow_null = allow_null, arg = arg, call = call @@ -435,6 +455,7 @@ check_formula <- function(x, x, "a formula", ..., + allow_na = FALSE, allow_null = allow_null, arg = arg, call = call @@ -444,15 +465,28 @@ check_formula <- function(x, # Vectors ----------------------------------------------------------------- +# TODO: Figure out what to do with logical `NA` and `allow_na = TRUE` + check_character <- function(x, ..., + allow_na = TRUE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { + if (!missing(x)) { if (is_character(x)) { + if (!allow_na && any(is.na(x))) { + abort( + sprintf("`%s` can't contain NA values.", arg), + arg = arg, + call = call + ) + } + return(invisible(NULL)) } + if (allow_null && is_null(x)) { return(invisible(NULL)) } @@ -468,4 +502,53 @@ check_character <- function(x, ) } +check_logical <- function(x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env()) { + if (!missing(x)) { + if (is_logical(x)) { + return(invisible(NULL)) + } + if (allow_null && is_null(x)) { + return(invisible(NULL)) + } + } + + stop_input_type( + x, + "a logical vector", + ..., + allow_na = FALSE, + allow_null = allow_null, + arg = arg, + call = call + ) +} + +check_data_frame <- function(x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env()) { + if (!missing(x)) { + if (is.data.frame(x)) { + return(invisible(NULL)) + } + if (allow_null && is_null(x)) { + return(invisible(NULL)) + } + } + + stop_input_type( + x, + "a data frame", + ..., + allow_null = allow_null, + arg = arg, + call = call + ) +} + # nocov end diff --git a/R/separate-longer.R b/R/separate-longer.R index dd5830124..879c6c6b3 100644 --- a/R/separate-longer.R +++ b/R/separate-longer.R @@ -54,7 +54,7 @@ separate_longer_position <- function(data, cols, width, ..., keep_empty = FALSE) check_installed("stringr") check_data_frame(data) check_required(cols) - check_number_whole(width, min = 1L) + check_number_whole(width, min = 1) check_dots_empty() map_unchop( diff --git a/tests/testthat/_snaps/append.md b/tests/testthat/_snaps/append.md index c41737a6b..553b27950 100644 --- a/tests/testthat/_snaps/append.md +++ b/tests/testthat/_snaps/append.md @@ -1,9 +1,8 @@ # after must be integer or character Code - (expect_error(df_append(df1, df2, after = 1.5))) - Output - + df_append(df1, df2, after = 1.5) + Condition Error in `df_append()`: ! `after` must be a whole number, not the number 1.5. i This is an internal error that was detected in the tidyr package. diff --git a/tests/testthat/_snaps/chop.md b/tests/testthat/_snaps/chop.md index a6d581dad..9292d4532 100644 --- a/tests/testthat/_snaps/chop.md +++ b/tests/testthat/_snaps/chop.md @@ -5,6 +5,9 @@ Condition Error in `chop()`: ! `data` must be a data frame, not an integer vector. + +--- + Code chop(df) Condition @@ -22,18 +25,16 @@ # incompatible sizes are caught Code - (expect_error(unchop(df, c(x, y)))) - Output - + unchop(df, c(x, y)) + Condition Error in `unchop()`: ! In row 1, can't recycle input of size 2 to size 3. # empty typed inputs are considered in common size, but NULLs aren't Code - (expect_error(unchop(df, c(x, y)))) - Output - + unchop(df, c(x, y)) + Condition Error in `unchop()`: ! In row 1, can't recycle input of size 0 to size 2. @@ -52,16 +53,25 @@ Condition Error in `unchop()`: ! `data` must be a data frame, not an integer vector. + +--- + Code unchop(df) Condition Error in `unchop()`: ! `cols` is absent but must be supplied. + +--- + Code unchop(df, col, keep_empty = 1) Condition Error in `unchop()`: ! `keep_empty` must be `TRUE` or `FALSE`, not the number 1. + +--- + Code unchop(df, col, ptype = 1) Condition diff --git a/tests/testthat/_snaps/drop-na.md b/tests/testthat/_snaps/drop-na.md index 3b5302b26..72405450c 100644 --- a/tests/testthat/_snaps/drop-na.md +++ b/tests/testthat/_snaps/drop-na.md @@ -1,9 +1,8 @@ # errors are raised Code - (expect_error(drop_na(df, list()))) - Output - + drop_na(df, list()) + Condition Error in `drop_na()`: ! Can't select columns with `list()`. x `list()` must be numeric or character, not an empty list. @@ -11,9 +10,8 @@ --- Code - (expect_error(drop_na(df, "z"))) - Output - + drop_na(df, "z") + Condition Error in `drop_na()`: ! Can't select columns that don't exist. x Column `z` doesn't exist. diff --git a/tests/testthat/_snaps/expand.md b/tests/testthat/_snaps/expand.md index 24cf35a1b..7af571108 100644 --- a/tests/testthat/_snaps/expand.md +++ b/tests/testthat/_snaps/expand.md @@ -1,9 +1,8 @@ # crossing checks for bad inputs Code - (expect_error(crossing(x = 1:10, y = quote(a)))) - Output - + crossing(x = 1:10, y = quote(a)) + Condition Error in `crossing()`: ! `..2` must be a vector, not a symbol. @@ -37,9 +36,8 @@ # expand_grid() can control name_repair Code - (expect_error(expand_grid(x = x, x = x))) - Output - + expand_grid(x = x, x = x) + Condition Error in `expand_grid()`: ! Names must be unique. x These names are duplicated: @@ -66,9 +64,8 @@ # grid_dots() reject non-vector input Code - (expect_error(grid_dots(lm(1 ~ 1)))) - Output - + grid_dots(lm(1 ~ 1)) + Condition Error: ! `..1` must be a vector, not a object. diff --git a/tests/testthat/_snaps/extract.md b/tests/testthat/_snaps/extract.md index bb8f779ed..2c33e1746 100644 --- a/tests/testthat/_snaps/extract.md +++ b/tests/testthat/_snaps/extract.md @@ -1,24 +1,24 @@ # informative error message if wrong number of groups Code - (expect_error(extract(df, x, "y", "."))) - Output - + extract(df, x, "y", ".") + Condition Error in `extract()`: ! `regex` should define 1 groups; 0 found. + +--- + Code - (expect_error(extract(df, x, c("y", "z"), "."))) - Output - + extract(df, x, c("y", "z"), ".") + Condition Error in `extract()`: ! `regex` should define 2 groups; 0 found. # informative error if using stringr modifier functions (#693) Code - (expect_error(extract(df, x, "x", regex = regex))) - Output - + extract(df, x, "x", regex = regex) + Condition Error in `extract()`: ! `regex` can't use modifiers from stringr. @@ -29,16 +29,25 @@ Condition Error in `extract()`: ! `col` is absent but must be supplied. + +--- + Code df %>% extract(x, regex = 1) Condition Error in `extract()`: ! `regex` must be a single string, not the number 1. + +--- + Code df %>% extract(x, into = 1:3) Condition Error in `extract()`: ! `into` must be a character vector, not an integer vector. + +--- + Code df %>% extract(x, into = "x", convert = 1) Condition diff --git a/tests/testthat/_snaps/gather.md b/tests/testthat/_snaps/gather.md index 3322d1115..ff77d31be 100644 --- a/tests/testthat/_snaps/gather.md +++ b/tests/testthat/_snaps/gather.md @@ -1,35 +1,42 @@ # gather throws error for POSIXlt Code - (expect_error(gather(df, key, val, -x))) - Output - + gather(df, key, val, -x) + Condition + Error: + ! 'x' is a POSIXlt. Please convert to POSIXct. + +--- + Code - (expect_error(gather(df, key, val, -y))) - Output - + gather(df, key, val, -y) + Condition + Error: + ! Column 1 is a POSIXlt. Please convert to POSIXct. # gather throws error for weird objects Code - (expect_error(gather(df, key, val, -x))) - Output - - Code - (expect_error(gather(df, key, val, -y))) - Output - + gather(df, key, val, -y) + Condition + Error: + ! All columns be atomic vectors or lists (not expression) --- Code - (expect_error(gather(df, key, val, -x))) - Output - + gather(df, key, val, -x) + Condition + Error: + ! All columns must be atomic vectors or lists. Problem with 'x' + +--- + Code - (expect_error(gather(df, key, val, -y))) - Output - + gather(df, key, val, -y) + Condition + Error: + ! All columns must be atomic vectors or lists. Problem with column 2. # factors coerced to characters, not integers diff --git a/tests/testthat/_snaps/hoist.md b/tests/testthat/_snaps/hoist.md index 3291a2d91..942c69fb3 100644 --- a/tests/testthat/_snaps/hoist.md +++ b/tests/testthat/_snaps/hoist.md @@ -1,48 +1,48 @@ # nested lists generate a cast error if they can't be cast to the ptype Code - (expect_error(hoist(df, x, "b", .ptype = list(b = double())))) - Output - + hoist(df, x, "b", .ptype = list(b = double())) + Condition Error in `hoist()`: ! Can't convert `..1` to . # non-vectors generate a cast error if a ptype is supplied Code - (expect_error(hoist(df, x, "b", .ptype = list(b = integer())))) - Output - + hoist(df, x, "b", .ptype = list(b = integer())) + Condition Error in `hoist()`: ! `..1` must be a vector, not a symbol. # input validation catches problems Code - (expect_error(df %>% hoist(y))) - Output - + df %>% hoist(y) + Condition Error in `hoist()`: ! `.data[[.col]]` must be a list, not the number 1. + +--- + Code - (expect_error(df %>% hoist(x, 1))) - Output - + df %>% hoist(x, 1) + Condition Error in `hoist()`: ! All elements of `...` must be named. + +--- + Code - (expect_error(df %>% hoist(x, a = "a", a = "b"))) - Output - + df %>% hoist(x, a = "a", a = "b") + Condition Error in `hoist()`: ! The names of `...` must be unique. # can't hoist() from a data frame column Code - (expect_error(hoist(df, a, xx = 1))) - Output - + hoist(df, a, xx = 1) + Condition Error in `hoist()`: ! `.data[[.col]]` must be a list, not a object. @@ -53,26 +53,41 @@ Condition Error in `hoist()`: ! `.data` must be a data frame, not a number. + +--- + Code hoist(df) Condition Error in `hoist()`: ! `.col` is absent but must be supplied. + +--- + Code hoist(df, a, .remove = 1) Condition Error in `hoist()`: ! `.remove` must be `TRUE` or `FALSE`, not the number 1. + +--- + Code hoist(df, a, .ptype = 1) Condition Error in `hoist()`: ! `.ptype` must be `NULL`, an empty ptype, or a named list of ptypes. + +--- + Code hoist(df, a, .transform = 1) Condition Error in `hoist()`: ! `.transform` must be `NULL`, a function, or a named list of functions. + +--- + Code hoist(df, a, .simplify = 1) Condition diff --git a/tests/testthat/_snaps/nest-legacy.md b/tests/testthat/_snaps/nest-legacy.md index a9694b0c6..4f2dfa813 100644 --- a/tests/testthat/_snaps/nest-legacy.md +++ b/tests/testthat/_snaps/nest-legacy.md @@ -1,9 +1,8 @@ # can't combine vectors and data frames Code - (expect_error(unnest_legacy(df))) - Output - + unnest_legacy(df) + Condition Error in `unnest_legacy()`: ! Each column must either be a list of vectors or a list of data frames. i Problems in: `x` @@ -11,18 +10,16 @@ # multiple columns must be same length Code - (expect_error(unnest_legacy(df))) - Output - + unnest_legacy(df) + Condition Error in `unnest_legacy()`: ! All nested columns must have the same number of elements. --- Code - (expect_error(unnest_legacy(df))) - Output - + unnest_legacy(df) + Condition Error in `unnest_legacy()`: ! All nested columns must have the same number of elements. diff --git a/tests/testthat/_snaps/pack.md b/tests/testthat/_snaps/pack.md index 2f304f7fc..846a6e5cc 100644 --- a/tests/testthat/_snaps/pack.md +++ b/tests/testthat/_snaps/pack.md @@ -117,11 +117,17 @@ Condition Error in `unpack()`: ! `data` must be a data frame, not a number. + +--- + Code unpack(df) Condition Error in `unpack()`: ! `cols` is absent but must be supplied. + +--- + Code unpack(df, y, names_sep = 1) Condition diff --git a/tests/testthat/_snaps/pivot-long.md b/tests/testthat/_snaps/pivot-long.md index 477545174..0f9081fbc 100644 --- a/tests/testthat/_snaps/pivot-long.md +++ b/tests/testthat/_snaps/pivot-long.md @@ -1,29 +1,24 @@ # when `values_ptypes` is provided, the type error uses variable names (#1364) Code - (expect_error(pivot_longer(df, x, values_ptypes = character()))) - Output - + pivot_longer(df, x, values_ptypes = character()) + Condition Error in `pivot_longer()`: ! Can't convert `x` to . # when `names_ptypes` is provided, the type error uses `names_to` names (#1364) Code - (expect_error({ - pivot_longer(df, cols = x, names_to = "name", names_ptypes = double()) - })) - Output - + pivot_longer(df, cols = x, names_to = "name", names_ptypes = double()) + Condition Error in `pivot_longer()`: ! Can't convert `name` to . # error when overwriting existing column Code - (expect_error(pivot_longer(df, y, names_to = "x"))) - Output - + pivot_longer(df, y, names_to = "x") + Condition Error in `pivot_longer()`: ! Names must be unique. x These names are duplicated: @@ -42,34 +37,33 @@ # multiple names requires names_sep/names_pattern Code - (expect_error(build_longer_spec(df, x_y, names_to = c("a", "b")))) - Output - + build_longer_spec(df, x_y, names_to = c("a", "b")) + Condition Error in `build_longer_spec()`: ! If you supply multiple names in `names_to` you must also supply one of `names_sep` or `names_pattern`. + +--- + Code - (expect_error(build_longer_spec(df, x_y, names_to = c("a", "b"), names_sep = "x", - names_pattern = "x"))) - Output - + build_longer_spec(df, x_y, names_to = c("a", "b"), names_sep = "x", + names_pattern = "x") + Condition Error in `build_longer_spec()`: ! If you supply multiple names in `names_to` you must also supply one of `names_sep` or `names_pattern`. # names_sep fails with single name Code - (expect_error(build_longer_spec(df, x_y, names_to = "x", names_sep = "_"))) - Output - + build_longer_spec(df, x_y, names_to = "x", names_sep = "_") + Condition Error in `build_longer_spec()`: ! `names_sep` can't be used with a length 1 `names_to`. # Error if the `col` can't be selected. Code - (expect_error(pivot_longer(iris, matches("foo")))) - Output - + pivot_longer(iris, matches("foo")) + Condition Error in `pivot_longer()`: ! `cols` must select at least one column. @@ -84,116 +78,125 @@ # `names_to` is validated Code - (expect_error(build_longer_spec(df, x, names_to = 1))) - Output - + build_longer_spec(df, x, names_to = 1) + Condition Error in `build_longer_spec()`: ! `names_to` must be a character vector or `NULL`, not the number 1. + +--- + Code - (expect_error(build_longer_spec(df, x, names_to = c("x", "y")))) - Output - + build_longer_spec(df, x, names_to = c("x", "y")) + Condition Error in `build_longer_spec()`: ! If you supply multiple names in `names_to` you must also supply one of `names_sep` or `names_pattern`. + +--- + Code - (expect_error(build_longer_spec(df, x, names_to = c("x", "y"), names_sep = "_", - names_pattern = "x"))) - Output - + build_longer_spec(df, x, names_to = c("x", "y"), names_sep = "_", + names_pattern = "x") + Condition Error in `build_longer_spec()`: ! If you supply multiple names in `names_to` you must also supply one of `names_sep` or `names_pattern`. # `names_ptypes` is validated Code - (expect_error(build_longer_spec(df, x, names_ptypes = 1))) - Output - + build_longer_spec(df, x, names_ptypes = 1) + Condition Error in `build_longer_spec()`: ! `names_ptypes` must be `NULL`, an empty ptype, or a named list of ptypes. + +--- + Code - (expect_error(build_longer_spec(df, x, names_ptypes = list(integer())))) - Output - + build_longer_spec(df, x, names_ptypes = list(integer())) + Condition Error in `build_longer_spec()`: ! All elements of `names_ptypes` must be named. # `names_transform` is validated Code - (expect_error(build_longer_spec(df, x, names_transform = 1))) - Output - + build_longer_spec(df, x, names_transform = 1) + Condition Error in `build_longer_spec()`: ! `names_transform` must be `NULL`, a function, or a named list of functions. + +--- + Code - (expect_error(build_longer_spec(df, x, names_transform = list(~.x)))) - Output - + build_longer_spec(df, x, names_transform = list(~.x)) + Condition Error in `build_longer_spec()`: ! All elements of `names_transform` must be named. # `values_ptypes` is validated Code - (expect_error(pivot_longer(df, x, values_ptypes = 1))) - Output - + pivot_longer(df, x, values_ptypes = 1) + Condition Error in `pivot_longer()`: ! `values_ptypes` must be `NULL`, an empty ptype, or a named list of ptypes. + +--- + Code - (expect_error(pivot_longer(df, x, values_ptypes = list(integer())))) - Output - + pivot_longer(df, x, values_ptypes = list(integer())) + Condition Error in `pivot_longer()`: ! All elements of `values_ptypes` must be named. # `values_transform` is validated Code - (expect_error(pivot_longer(df, x, values_transform = 1))) - Output - + pivot_longer(df, x, values_transform = 1) + Condition Error in `pivot_longer()`: ! `values_transform` must be `NULL`, a function, or a named list of functions. + +--- + Code - (expect_error(pivot_longer(df, x, values_transform = list(~.x)))) - Output - + pivot_longer(df, x, values_transform = list(~.x)) + Condition Error in `pivot_longer()`: ! All elements of `values_transform` must be named. # `cols_vary` is validated Code - (expect_error(pivot_longer(df, x, cols_vary = "fast"))) - Output - + pivot_longer(df, x, cols_vary = "fast") + Condition Error in `pivot_longer()`: ! `cols_vary` must be one of "fastest" or "slowest", not "fast". i Did you mean "fastest"? + +--- + Code - (expect_error(pivot_longer(df, x, cols_vary = 1))) - Output - + pivot_longer(df, x, cols_vary = 1) + Condition Error in `pivot_longer()`: ! `cols_vary` must be a string or character vector. # `pivot_longer()` catches unused input passed through the dots Code - (expect_error(pivot_longer(df, c(x, y), 1))) - Output - + pivot_longer(df, c(x, y), 1) + Condition Error in `pivot_longer()`: ! Arguments in `...` must be used. x Problematic argument: * ..1 = 1 i Did you misspell an argument name? + +--- + Code - (expect_error(pivot_longer(df, c(x, y), col_vary = "slowest"))) - Output - + pivot_longer(df, c(x, y), col_vary = "slowest") + Condition Error in `pivot_longer()`: ! Arguments in `...` must be used. x Problematic argument: @@ -203,18 +206,19 @@ # `build_longer_spec()` requires empty dots Code - (expect_error(build_longer_spec(df, c(x, y), 1))) - Output - + build_longer_spec(df, c(x, y), 1) + Condition Error in `build_longer_spec()`: ! `...` must be empty. x Problematic argument: * ..1 = 1 i Did you forget to name an argument? + +--- + Code - (expect_error(build_longer_spec(df, c(x, y), name_to = "name"))) - Output - + build_longer_spec(df, c(x, y), name_to = "name") + Condition Error in `build_longer_spec()`: ! `...` must be empty. x Problematic argument: @@ -223,18 +227,19 @@ # `pivot_longer_spec()` requires empty dots Code - (expect_error(pivot_longer_spec(df, spec, 1))) - Output - + pivot_longer_spec(df, spec, 1) + Condition Error in `pivot_longer_spec()`: ! `...` must be empty. x Problematic argument: * ..1 = 1 i Did you forget to name an argument? + +--- + Code - (expect_error(pivot_longer_spec(df, spec, col_vary = "slowest"))) - Output - + pivot_longer_spec(df, spec, col_vary = "slowest") + Condition Error in `pivot_longer_spec()`: ! `...` must be empty. x Problematic argument: diff --git a/tests/testthat/_snaps/pivot-wide.md b/tests/testthat/_snaps/pivot-wide.md index 889a3a45c..2658004c1 100644 --- a/tests/testthat/_snaps/pivot-wide.md +++ b/tests/testthat/_snaps/pivot-wide.md @@ -9,9 +9,8 @@ # error when overwriting existing column Code - (expect_error(pivot_wider(df, names_from = key, values_from = val))) - Output - + pivot_wider(df, names_from = key, values_from = val) + Condition Error in `pivot_wider()`: ! Names must be unique. x These names are duplicated: @@ -30,9 +29,8 @@ # `names_from` must be supplied if `name` isn't in `data` (#1240) Code - (expect_error(pivot_wider(df, values_from = val))) - Output - + pivot_wider(df, values_from = val) + Condition Error in `pivot_wider()`: ! Can't select columns that don't exist. x Column `name` doesn't exist. @@ -40,9 +38,8 @@ # `values_from` must be supplied if `value` isn't in `data` (#1240) Code - (expect_error(pivot_wider(df, names_from = key))) - Output - + pivot_wider(df, names_from = key) + Condition Error in `pivot_wider()`: ! Can't select columns that don't exist. x Column `value` doesn't exist. @@ -50,29 +47,24 @@ # `names_from` must identify at least 1 column (#1240) Code - (expect_error(pivot_wider(df, names_from = starts_with("foo"), values_from = val)) - ) - Output - + pivot_wider(df, names_from = starts_with("foo"), values_from = val) + Condition Error in `pivot_wider()`: ! Must select at least one item. # `values_from` must identify at least 1 column (#1240) Code - (expect_error(pivot_wider(df, names_from = key, values_from = starts_with("foo"))) - ) - Output - + pivot_wider(df, names_from = key, values_from = starts_with("foo")) + Condition Error in `pivot_wider()`: ! Must select at least one item. # `values_fn` emits an informative error when it doesn't result in unique values (#1238) Code - (expect_error(pivot_wider(df, values_fn = list(value = ~.x)))) - Output - + pivot_wider(df, values_fn = list(value = ~.x)) + Condition Error in `pivot_wider()`: ! Applying `values_fn` to `value` must result in a single summary value per key. i Applying `values_fn` resulted in a vector of length 2. @@ -80,18 +72,19 @@ # `build_wider_spec()` requires empty dots Code - (expect_error(build_wider_spec(df, 1))) - Output - + build_wider_spec(df, 1) + Condition Error in `build_wider_spec()`: ! `...` must be empty. x Problematic argument: * ..1 = 1 i Did you forget to name an argument? + +--- + Code - (expect_error(build_wider_spec(df, name_prefix = ""))) - Output - + build_wider_spec(df, name_prefix = "") + Condition Error in `build_wider_spec()`: ! `...` must be empty. x Problematic argument: @@ -100,18 +93,19 @@ # `pivot_wider_spec()` requires empty dots Code - (expect_error(pivot_wider_spec(df, spec, 1))) - Output - + pivot_wider_spec(df, spec, 1) + Condition Error in `pivot_wider_spec()`: ! `...` must be empty. x Problematic argument: * ..1 = 1 i Did you forget to name an argument? + +--- + Code - (expect_error(pivot_wider_spec(df, spec, name_repair = "check_unique"))) - Output - + pivot_wider_spec(df, spec, name_repair = "check_unique") + Condition Error in `pivot_wider_spec()`: ! `...` must be empty. x Problematic argument: @@ -120,48 +114,49 @@ # `names_vary` is validated Code - (expect_error(build_wider_spec(df, names_vary = 1))) - Output - + build_wider_spec(df, names_vary = 1) + Condition Error in `build_wider_spec()`: ! `names_vary` must be a string or character vector. + +--- + Code - (expect_error(build_wider_spec(df, names_vary = "x"))) - Output - + build_wider_spec(df, names_vary = "x") + Condition Error in `build_wider_spec()`: ! `names_vary` must be one of "fastest" or "slowest", not "x". # `names_expand` is validated Code - (expect_error(build_wider_spec(df, names_expand = 1))) - Output - + build_wider_spec(df, names_expand = 1) + Condition Error in `build_wider_spec()`: ! `names_expand` must be `TRUE` or `FALSE`, not the number 1. + +--- + Code - (expect_error(build_wider_spec(df, names_expand = "x"))) - Output - + build_wider_spec(df, names_expand = "x") + Condition Error in `build_wider_spec()`: ! `names_expand` must be `TRUE` or `FALSE`, not the string "x". # `id_cols` can't select columns from `names_from` or `values_from` (#1318) Code - (expect_error(pivot_wider(df, id_cols = name, names_from = name, values_from = value)) - ) - Output - + pivot_wider(df, id_cols = name, names_from = name, values_from = value) + Condition Error in `pivot_wider()`: ! `id_cols` can't select a column already selected by `names_from`. i Column `name` has already been selected. + +--- + Code - (expect_error(pivot_wider(df, id_cols = value, names_from = name, values_from = value)) - ) - Output - + pivot_wider(df, id_cols = value, names_from = name, values_from = value) + Condition Error in `pivot_wider()`: ! `id_cols` can't select a column already selected by `values_from`. i Column `value` has already been selected. @@ -169,9 +164,8 @@ # `id_cols` returns a tidyselect error if a column selection is OOB (#1318) Code - (expect_error(pivot_wider(df, id_cols = foo))) - Output - + pivot_wider(df, id_cols = foo) + Condition Error in `pivot_wider()`: ! Can't select columns that don't exist. x Column `foo` doesn't exist. @@ -187,15 +181,16 @@ # `id_expand` is validated Code - (expect_error(pivot_wider(df, id_expand = 1))) - Output - + pivot_wider(df, id_expand = 1) + Condition Error in `pivot_wider()`: ! `id_expand` must be `TRUE` or `FALSE`, not the number 1. + +--- + Code - (expect_error(pivot_wider(df, id_expand = "x"))) - Output - + pivot_wider(df, id_expand = "x") + Condition Error in `pivot_wider()`: ! `id_expand` must be `TRUE` or `FALSE`, not the string "x". @@ -268,18 +263,16 @@ # values_fn is validated Code - (expect_error(pivot_wider(df, values_fn = 1))) - Output - + pivot_wider(df, values_fn = 1) + Condition Error in `pivot_wider()`: ! `values_fn` must be `NULL`, a function, or a named list of functions. # `unused_fn` must result in single summary values Code - (expect_error(pivot_wider(df, id_cols = id, unused_fn = identity))) - Output - + pivot_wider(df, id_cols = id, unused_fn = identity) + Condition Error in `pivot_wider()`: ! Applying `unused_fn` to `unused` must result in a single summary value per key. i Applying `unused_fn` resulted in a vector of length 2. @@ -287,18 +280,16 @@ # `values_fill` is validated Code - (expect_error(pivot_wider(df, values_fill = 1:2))) - Output - + pivot_wider(df, values_fill = 1:2) + Condition Error in `pivot_wider()`: ! `values_fill` must be `NULL`, a scalar, or a named list, not an integer vector. # `unused_fn` is validated Code - (expect_error(pivot_wider(df, id_cols = id, unused_fn = 1))) - Output - + pivot_wider(df, id_cols = id, unused_fn = 1) + Condition Error in `pivot_wider()`: ! `unused_fn` must be `NULL`, a function, or a named list of functions. diff --git a/tests/testthat/_snaps/pivot.md b/tests/testthat/_snaps/pivot.md index 86eb8a787..d861c3e3a 100644 --- a/tests/testthat/_snaps/pivot.md +++ b/tests/testthat/_snaps/pivot.md @@ -1,42 +1,37 @@ # basic sanity checks for spec occur Code - (expect_error(check_pivot_spec(1))) - Output - + check_pivot_spec(1) + Condition Error: ! `spec` must be a data frame, not a number. Code - (expect_error(check_pivot_spec(mtcars))) - Output - + check_pivot_spec(mtcars) + Condition Error: ! `spec` must have `.name` and `.value` columns. # `.name` column must be a character vector Code - (expect_error(check_pivot_spec(df))) - Output - + check_pivot_spec(df) + Condition Error: ! `spec$.name` must be a character vector, not an integer vector. # `.value` column must be a character vector Code - (expect_error(check_pivot_spec(df))) - Output - + check_pivot_spec(df) + Condition Error: ! `spec$.value` must be a character vector, not an integer vector. # `.name` column must be unique Code - (expect_error(check_pivot_spec(df))) - Output - + check_pivot_spec(df) + Condition Error: ! `spec$.name` must be unique. diff --git a/tests/testthat/_snaps/replace_na.md b/tests/testthat/_snaps/replace_na.md index 1614e0288..f25e4f42a 100644 --- a/tests/testthat/_snaps/replace_na.md +++ b/tests/testthat/_snaps/replace_na.md @@ -1,18 +1,16 @@ # can only be length 0 Code - (expect_error(replace_na(1, 1:10))) - Output - + replace_na(1, 1:10) + Condition Error in `replace_na()`: ! Replacement for `data` must be length 1, not length 10. # replacement must be castable to `data` Code - (expect_error(replace_na(x, 1.5))) - Output - + replace_na(x, 1.5) + Condition Error in `vec_assign()`: ! Can't convert from `replace` to `data` due to loss of precision. * Locations: 1 @@ -20,9 +18,8 @@ # replacement must be castable to corresponding column Code - (expect_error(replace_na(df, list(a = 1.5)))) - Output - + replace_na(df, list(a = 1.5)) + Condition Error in `vec_assign()`: ! Can't convert from `replace$a` to `data$a` due to loss of precision. * Locations: 1 diff --git a/tests/testthat/_snaps/separate-longer.md b/tests/testthat/_snaps/separate-longer.md index 53911b282..63d6d13f3 100644 --- a/tests/testthat/_snaps/separate-longer.md +++ b/tests/testthat/_snaps/separate-longer.md @@ -5,6 +5,9 @@ Condition Error in `separate_longer_delim()`: ! `cols` is absent but must be supplied. + +--- + Code df %>% separate_longer_delim(x, sep = 1) Condition @@ -18,12 +21,18 @@ Condition Error in `separate_longer_position()`: ! `cols` is absent but must be supplied. + +--- + Code df %>% separate_longer_position(y, width = 1) Condition Error in `separate_longer_position()`: ! Can't select columns that don't exist. x Column `y` doesn't exist. + +--- + Code df %>% separate_longer_position(x, width = 1.5) Condition diff --git a/tests/testthat/_snaps/separate-rows.md b/tests/testthat/_snaps/separate-rows.md index 568a67680..6079566df 100644 --- a/tests/testthat/_snaps/separate-rows.md +++ b/tests/testthat/_snaps/separate-rows.md @@ -5,6 +5,9 @@ Condition Error in `separate_rows()`: ! `sep` must be a single string, not the number 1. + +--- + Code separate_rows(df, x, convert = 1) Condition diff --git a/tests/testthat/_snaps/separate-wider.md b/tests/testthat/_snaps/separate-wider.md index bf9dcf537..daad98e8f 100644 --- a/tests/testthat/_snaps/separate-wider.md +++ b/tests/testthat/_snaps/separate-wider.md @@ -28,36 +28,57 @@ Condition Error in `separate_wider_delim()`: ! `cols` is absent but must be supplied. + +--- + Code df %>% separate_wider_delim(x) Condition Error in `separate_wider_delim()`: ! `delim` must be a single string, not absent. + +--- + Code df %>% separate_wider_delim(x, 1) Condition Error in `separate_wider_delim()`: ! `delim` must be a single string, not the number 1. + +--- + Code df %>% separate_wider_delim(x, "") Condition Error in `separate_wider_delim()`: ! `delim` must be a single string, not the empty string "". + +--- + Code df %>% separate_wider_delim(x, "-") Condition Error in `separate_wider_delim()`: ! Must specify at least one of `names` or `names_sep`. + +--- + Code df %>% separate_wider_delim(x, "-", names = 1) Condition Error in `separate_wider_delim()`: ! `names` must be a character vector or `NULL`, not the number 1. + +--- + Code df %>% separate_wider_delim(x, "-", names = c(x = "x")) Condition Error in `separate_wider_delim()`: ! `names` must be an unnamed character vector. + +--- + Code df %>% separate_wider_delim(x, "-", names_sep = "_", too_many = "merge") Condition @@ -94,21 +115,33 @@ Condition Error in `separate_wider_position()`: ! `cols` is absent but must be supplied. + +--- + Code df %>% separate_wider_position(x) Condition Error in `separate_wider_position()`: ! `widths` is absent but must be supplied. + +--- + Code df %>% separate_wider_position(x, widths = 1.5) Condition Error in `separate_wider_position()`: ! `widths` must be a (partially) named integer vector. + +--- + Code df %>% separate_wider_position(x, widths = 1L) Condition Error in `separate_wider_position()`: ! `widths` must be a (partially) named integer vector. + +--- + Code df %>% separate_wider_position(x, widths = c(x = 0)) Condition @@ -175,17 +208,26 @@ Condition Error in `separate_wider_regex()`: ! `cols` is absent but must be supplied. + +--- + Code df %>% separate_wider_regex(x) Condition Error in `separate_wider_regex()`: ! `patterns` must be a character vector, not absent. + +--- + Code df %>% separate_wider_regex(y, patterns = c(x = "-")) Condition Error in `separate_wider_regex()`: ! Can't select columns that don't exist. x Column `y` doesn't exist. + +--- + Code df %>% separate_wider_regex(x, patterns = ".") Condition diff --git a/tests/testthat/_snaps/separate.md b/tests/testthat/_snaps/separate.md index 33c9e861e..7037b2ed5 100644 --- a/tests/testthat/_snaps/separate.md +++ b/tests/testthat/_snaps/separate.md @@ -49,21 +49,33 @@ Condition Error in `separate()`: ! `col` is absent but must be supplied. + +--- + Code separate(df, x, into = 1) Condition Error in `separate()`: ! `into` must be a character vector, not the number 1. + +--- + Code separate(df, x, into = "x", sep = c("a", "b")) Condition Error in `separate()`: ! `sep` must be a string or numeric vector, not a character vector + +--- + Code separate(df, x, into = "x", remove = 1) Condition Error in `separate()`: ! `remove` must be `TRUE` or `FALSE`, not the number 1. + +--- + Code separate(df, x, into = "x", convert = 1) Condition @@ -73,9 +85,8 @@ # informative error if using stringr modifier functions (#693) Code - (expect_error(separate(df, x, "x", sep = sep))) - Output - + separate(df, x, "x", sep = sep) + Condition Error in `separate()`: ! `sep` can't use modifiers from stringr. diff --git a/tests/testthat/_snaps/seq.md b/tests/testthat/_snaps/seq.md index 954eb6d91..c9ff7a27c 100644 --- a/tests/testthat/_snaps/seq.md +++ b/tests/testthat/_snaps/seq.md @@ -1,15 +1,13 @@ # full_seq errors if sequence isn't regular Code - (expect_error(full_seq(c(1, 3, 4), 2))) - Output - + full_seq(c(1, 3, 4), 2) + Condition Error in `full_seq()`: ! `x` is not a regular sequence. Code - (expect_error(full_seq(c(0, 10, 20), 11, tol = 1.8))) - Output - + full_seq(c(0, 10, 20), 11, tol = 1.8) + Condition Error in `full_seq()`: ! `x` is not a regular sequence. diff --git a/tests/testthat/_snaps/spread.md b/tests/testthat/_snaps/spread.md index 593d5d7a0..470ed45cd 100644 --- a/tests/testthat/_snaps/spread.md +++ b/tests/testthat/_snaps/spread.md @@ -1,9 +1,8 @@ # duplicate values for one key is an error Code - (expect_error(spread(df, x, y))) - Output - + spread(df, x, y) + Condition Error in `spread()`: ! Each row of output must be identified by a unique combination of keys. i Keys are shared for 2 rows diff --git a/tests/testthat/_snaps/unnest-helper.md b/tests/testthat/_snaps/unnest-helper.md index 578f23846..f100a45d7 100644 --- a/tests/testthat/_snaps/unnest-helper.md +++ b/tests/testthat/_snaps/unnest-helper.md @@ -1,91 +1,77 @@ # `simplify` is validated Code - (expect_error(df_simplify(data.frame(), simplify = 1))) - Output - + df_simplify(data.frame(), simplify = 1) + Condition Error: ! `simplify` must be a list or a single `TRUE` or `FALSE`. Code - (expect_error(df_simplify(data.frame(), simplify = NA))) - Output - + df_simplify(data.frame(), simplify = NA) + Condition Error: ! `simplify` must be a list or a single `TRUE` or `FALSE`. Code - (expect_error(df_simplify(data.frame(), simplify = c(TRUE, FALSE)))) - Output - + df_simplify(data.frame(), simplify = c(TRUE, FALSE)) + Condition Error: ! `simplify` must be a list or a single `TRUE` or `FALSE`. Code - (expect_error(df_simplify(data.frame(), simplify = list(1)))) - Output - + df_simplify(data.frame(), simplify = list(1)) + Condition Error: ! All elements of `simplify` must be named. Code - (expect_error(df_simplify(data.frame(), simplify = list(x = 1, x = 1)))) - Output - + df_simplify(data.frame(), simplify = list(x = 1, x = 1)) + Condition Error: ! The names of `simplify` must be unique. # `ptype` is validated Code - (expect_error(df_simplify(data.frame(), ptype = 1))) - Output - + df_simplify(data.frame(), ptype = 1) + Condition Error: ! `ptype` must be `NULL`, an empty ptype, or a named list of ptypes. Code - (expect_error(df_simplify(data.frame(), ptype = list(1)))) - Output - + df_simplify(data.frame(), ptype = list(1)) + Condition Error: ! All elements of `ptype` must be named. Code - (expect_error(df_simplify(data.frame(), ptype = list(x = 1, x = 1)))) - Output - + df_simplify(data.frame(), ptype = list(x = 1, x = 1)) + Condition Error: ! The names of `ptype` must be unique. # `transform` is validated Code - (expect_error(df_simplify(data.frame(), transform = list(~.x)))) - Output - + df_simplify(data.frame(), transform = list(~.x)) + Condition Error: ! All elements of `transform` must be named. Code - (expect_error(df_simplify(data.frame(x = 1), transform = 1))) - Output - + df_simplify(data.frame(x = 1), transform = 1) + Condition Error: ! `transform` must be `NULL`, a function, or a named list of functions. Code - (expect_error(df_simplify(data.frame(), transform = list(x = 1)))) - Output - + df_simplify(data.frame(), transform = list(x = 1)) + Condition Error: ! Can't convert `transform$x`, a double vector, to a function. Code - (expect_error(df_simplify(data.frame(), transform = list(x = 1, x = 1)))) - Output - + df_simplify(data.frame(), transform = list(x = 1, x = 1)) + Condition Error: ! The names of `transform` must be unique. # ptype is applied after transform Code - (expect_error(col_simplify(list(1, 2, 3), ptype = integer(), transform = ~ .x + - 1.5))) - Output - + col_simplify(list(1, 2, 3), ptype = integer(), transform = ~ .x + 1.5) + Condition Error: ! Can't convert from `..1` to due to loss of precision. * Locations: 1 diff --git a/tests/testthat/_snaps/unnest-longer.md b/tests/testthat/_snaps/unnest-longer.md index 6ed330d51..7b0aab833 100644 --- a/tests/testthat/_snaps/unnest-longer.md +++ b/tests/testthat/_snaps/unnest-longer.md @@ -1,9 +1,8 @@ # unnest_longer - bad inputs generate errors Code - (expect_error(unnest_longer(df, y))) - Output - + unnest_longer(df, y) + Condition Error in `unnest_longer()`: ! List-column `y` must contain only vectors or `NULL`. @@ -18,10 +17,8 @@ # can't mix `indices_to` with `indices_include = FALSE` Code - (expect_error(unnest_longer(mtcars, mpg, indices_to = "x", indices_include = FALSE)) - ) - Output - + unnest_longer(mtcars, mpg, indices_to = "x", indices_include = FALSE) + Condition Error in `unnest_longer()`: ! Can't use `indices_include = FALSE` when `indices_to` is supplied. @@ -56,60 +53,52 @@ # `values_to` is validated Code - (expect_error(unnest_longer(mtcars, mpg, values_to = 1))) - Output - + unnest_longer(mtcars, mpg, values_to = 1) + Condition Error in `unnest_longer()`: ! `values_to` must be a valid name or `NULL`, not the number 1. Code - (expect_error(unnest_longer(mtcars, mpg, values_to = c("x", "y")))) - Output - + unnest_longer(mtcars, mpg, values_to = c("x", "y")) + Condition Error in `unnest_longer()`: ! `values_to` must be a valid name or `NULL`, not a character vector. # `indices_to` is validated Code - (expect_error(unnest_longer(mtcars, mpg, indices_to = 1))) - Output - + unnest_longer(mtcars, mpg, indices_to = 1) + Condition Error in `unnest_longer()`: ! `indices_to` must be a valid name or `NULL`, not the number 1. Code - (expect_error(unnest_longer(mtcars, mpg, indices_to = c("x", "y")))) - Output - + unnest_longer(mtcars, mpg, indices_to = c("x", "y")) + Condition Error in `unnest_longer()`: ! `indices_to` must be a valid name or `NULL`, not a character vector. # `indices_include` is validated Code - (expect_error(unnest_longer(mtcars, mpg, indices_include = 1))) - Output - + unnest_longer(mtcars, mpg, indices_include = 1) + Condition Error in `unnest_longer()`: ! `indices_include` must be `TRUE`, `FALSE`, or `NULL`, not the number 1. Code - (expect_error(unnest_longer(mtcars, mpg, indices_include = c(TRUE, FALSE)))) - Output - + unnest_longer(mtcars, mpg, indices_include = c(TRUE, FALSE)) + Condition Error in `unnest_longer()`: ! `indices_include` must be `TRUE`, `FALSE`, or `NULL`, not a logical vector. # `keep_empty` is validated Code - (expect_error(unnest_longer(mtcars, mpg, keep_empty = 1))) - Output - + unnest_longer(mtcars, mpg, keep_empty = 1) + Condition Error in `unnest_longer()`: ! `keep_empty` must be `TRUE` or `FALSE`, not the number 1. Code - (expect_error(unnest_longer(mtcars, mpg, keep_empty = c(TRUE, FALSE)))) - Output - + unnest_longer(mtcars, mpg, keep_empty = c(TRUE, FALSE)) + Condition Error in `unnest_longer()`: ! `keep_empty` must be `TRUE` or `FALSE`, not a logical vector. diff --git a/tests/testthat/_snaps/unnest-wider.md b/tests/testthat/_snaps/unnest-wider.md index c516c4fb3..7e2f6f37a 100644 --- a/tests/testthat/_snaps/unnest-wider.md +++ b/tests/testthat/_snaps/unnest-wider.md @@ -1,9 +1,8 @@ # unnest_wider - bad inputs generate errors Code - (expect_error(unnest_wider(df, y))) - Output - + unnest_wider(df, y) + Condition Error in `unnest_wider()`: i In column: `y`. i In row: 1. @@ -109,16 +108,25 @@ Condition Error in `unnest_wider()`: ! `data` must be a data frame, not a number. + +--- + Code unnest_wider(df) Condition Error in `unnest_wider()`: ! `col` is absent but must be supplied. + +--- + Code unnest_wider(df, x, names_sep = 1) Condition Error in `unnest_wider()`: ! `names_sep` must be a single string or `NULL`, not the number 1. + +--- + Code unnest_wider(df, x, strict = 1) Condition diff --git a/tests/testthat/_snaps/unnest.md b/tests/testthat/_snaps/unnest.md index d470b3867..cbeb77acc 100644 --- a/tests/testthat/_snaps/unnest.md +++ b/tests/testthat/_snaps/unnest.md @@ -1,36 +1,32 @@ # bad inputs generate errors Code - (expect_error(unnest(df, y))) - Output - + unnest(df, y) + Condition Error in `list_sizes()`: ! `x[[1]]` must be a vector, not a function. # multiple columns must be same length Code - (expect_error(unnest(df, c(x, y)))) - Output - + unnest(df, c(x, y)) + Condition Error in `unnest()`: ! In row 1, can't recycle input of size 2 to size 3. --- Code - (expect_error(unnest(df, c(x, y)))) - Output - + unnest(df, c(x, y)) + Condition Error in `unnest()`: ! In row 1, can't recycle input of size 2 to size 3. # unnesting column of mixed vector / data frame input is an error Code - (expect_error(unnest(df, x))) - Output - + unnest(df, x) + Condition Error in `unnest()`: ! Can't combine `x[[1]]` and `x[[2]]` . diff --git a/tests/testthat/test-append.R b/tests/testthat/test-append.R index 7385992dd..88b32c81c 100644 --- a/tests/testthat/test-append.R +++ b/tests/testthat/test-append.R @@ -31,7 +31,7 @@ test_that("after must be integer or character", { df1 <- data.frame(x = 1) df2 <- data.frame(x = 2) - expect_snapshot((expect_error(df_append(df1, df2, after = 1.5)))) + expect_snapshot(df_append(df1, df2, after = 1.5), error = TRUE) }) test_that("always returns a bare data frame", { diff --git a/tests/testthat/test-chop.R b/tests/testthat/test-chop.R index 6490dca6e..95017c86d 100644 --- a/tests/testthat/test-chop.R +++ b/tests/testthat/test-chop.R @@ -24,6 +24,8 @@ test_that("chop() validates its input `cols` (#1205)", { df <- tibble(x = 1:2) expect_snapshot(error = TRUE, { chop(df$x) + }) + expect_snapshot(error = TRUE, { chop(df) }) }) @@ -288,15 +290,15 @@ test_that("unchop works with record columns (treating them like vectors)", { test_that("incompatible sizes are caught", { df <- tibble(x = list(1:2), y = list(1:3)) - expect_snapshot((expect_error(unchop(df, c(x, y))))) + expect_snapshot(unchop(df, c(x, y)), error = TRUE) }) test_that("empty typed inputs are considered in common size, but NULLs aren't", { df <- tibble(x = list(NULL), y = list(1:2)) - expect_error(unchop(df, c(x, y)), NA) + expect_no_error(unchop(df, c(x, y))) df <- tibble(x = list(integer()), y = list(1:2)) - expect_snapshot((expect_error(unchop(df, c(x, y))))) + expect_snapshot(unchop(df, c(x, y)), error = TRUE) }) test_that("unchopping retains inner names from tibble elements", { @@ -357,8 +359,14 @@ test_that("unchop validates its inputs", { expect_snapshot(error = TRUE, { unchop(1:10) + }) + expect_snapshot(error = TRUE, { unchop(df) + }) + expect_snapshot(error = TRUE, { unchop(df, col, keep_empty = 1) + }) + expect_snapshot(error = TRUE, { unchop(df, col, ptype = 1) }) }) diff --git a/tests/testthat/test-drop-na.R b/tests/testthat/test-drop-na.R index 1795cb940..d0c04cb8b 100644 --- a/tests/testthat/test-drop-na.R +++ b/tests/testthat/test-drop-na.R @@ -37,8 +37,12 @@ test_that("groups are preserved", { test_that("errors are raised", { df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b")) - expect_snapshot((expect_error(drop_na(df, list())))) - expect_snapshot((expect_error(drop_na(df, "z")))) + expect_snapshot(error = TRUE, { + drop_na(df, list()) + }) + expect_snapshot(error = TRUE, { + drop_na(df, "z") + }) }) test_that("single variable data.frame doesn't lose dimension", { @@ -74,11 +78,6 @@ test_that("works with df-cols", { }) test_that("works with rcrd cols", { - skip_if( - packageVersion("vctrs") <= "0.3.8", - "vec_detect_complete() treated rcrds differently" - ) - # if any rcrd field contains a missing value, it is incomplete col <- new_rcrd(list(x = c(1, 1, NA, NA), y = c(1, NA, 1, NA))) df <- tibble(col = col) diff --git a/tests/testthat/test-expand.R b/tests/testthat/test-expand.R index 0601a53ff..bf4caf70d 100644 --- a/tests/testthat/test-expand.R +++ b/tests/testthat/test-expand.R @@ -174,7 +174,7 @@ test_that("expand() retains `NA` data in factors (#1275)", { # ------------------------------------------------------------------------------ test_that("crossing checks for bad inputs", { - expect_snapshot((expect_error(crossing(x = 1:10, y = quote(a))))) + expect_snapshot(crossing(x = 1:10, y = quote(a)), error = TRUE) }) test_that("preserves NAs", { @@ -320,7 +320,7 @@ test_that("crossing() / nesting() retain `NA` data in factors (#1275)", { test_that("expand_grid() can control name_repair", { x <- 1:2 - expect_snapshot((expect_error(expand_grid(x = x, x = x)))) + expect_snapshot(expand_grid(x = x, x = x), error = TRUE) expect_snapshot( out <- expand_grid(x = x, x = x, .name_repair = "unique") @@ -460,7 +460,7 @@ test_that("grid_dots() drops `NULL`s", { }) test_that("grid_dots() reject non-vector input", { - expect_snapshot((expect_error(grid_dots(lm(1 ~ 1))))) + expect_snapshot(grid_dots(lm(1 ~ 1)), error = TRUE) }) # ------------------------------------------------------------------------------ diff --git a/tests/testthat/test-extract.R b/tests/testthat/test-extract.R index ca87632ae..6618d7282 100644 --- a/tests/testthat/test-extract.R +++ b/tests/testthat/test-extract.R @@ -47,9 +47,11 @@ test_that("groups are preserved", { test_that("informative error message if wrong number of groups", { df <- tibble(x = "a") - expect_snapshot({ - (expect_error(extract(df, x, "y", "."))) - (expect_error(extract(df, x, c("y", "z"), "."))) + expect_snapshot(error = TRUE, { + extract(df, x, "y", ".") + }) + expect_snapshot(error = TRUE, { + extract(df, x, c("y", "z"), ".") }) }) @@ -57,7 +59,9 @@ test_that("informative error if using stringr modifier functions (#693)", { df <- tibble(x = "a") regex <- structure("a", class = "pattern") - expect_snapshot((expect_error(extract(df, x, "x", regex = regex)))) + expect_snapshot(error = TRUE, { + extract(df, x, "x", regex = regex) + }) }) test_that("str_match_first handles edge cases", { @@ -88,8 +92,14 @@ test_that("validates its inputs", { expect_snapshot(error = TRUE, { df %>% extract() + }) + expect_snapshot(error = TRUE, { df %>% extract(x, regex = 1) + }) + expect_snapshot(error = TRUE, { df %>% extract(x, into = 1:3) + }) + expect_snapshot(error = TRUE, { df %>% extract(x, into = "x", convert = 1) }) }) diff --git a/tests/testthat/test-gather.R b/tests/testthat/test-gather.R index bccba7f2d..158f433ab 100644 --- a/tests/testthat/test-gather.R +++ b/tests/testthat/test-gather.R @@ -124,9 +124,11 @@ test_that("gather throws error for POSIXlt", { df <- data.frame(y = 1) df$x <- as.POSIXlt(Sys.time()) - expect_snapshot({ - (expect_error(gather(df, key, val, -x))) - (expect_error(gather(df, key, val, -y))) + expect_snapshot(error = TRUE, { + gather(df, key, val, -x) + }) + expect_snapshot(error = TRUE, { + gather(df, key, val, -y) }) }) @@ -134,9 +136,10 @@ test_that("gather throws error for weird objects", { df <- data.frame(y = 1) df$x <- expression(x) - expect_snapshot({ - (expect_error(gather(df, key, val, -x))) - (expect_error(gather(df, key, val, -y))) + # Can't use snapshot, it changes between versions of R + expect_error(gather(d, key, val, -x)) + expect_snapshot(error = TRUE, { + gather(df, key, val, -y) }) e <- new.env(parent = emptyenv()) @@ -144,9 +147,11 @@ test_that("gather throws error for weird objects", { df <- data.frame(y = 1) df$x <- e - expect_snapshot({ - (expect_error(gather(df, key, val, -x))) - (expect_error(gather(df, key, val, -y))) + expect_snapshot(error = TRUE, { + gather(df, key, val, -x) + }) + expect_snapshot(error = TRUE, { + gather(df, key, val, -y) }) }) diff --git a/tests/testthat/test-hoist.R b/tests/testthat/test-hoist.R index 7323a1e8f..dc5fd3a22 100644 --- a/tests/testthat/test-hoist.R +++ b/tests/testthat/test-hoist.R @@ -33,17 +33,19 @@ test_that("can check check/transform values", { test_that("nested lists generate a cast error if they can't be cast to the ptype", { df <- tibble(x = list(list(b = list(1)))) - expect_snapshot((expect_error( - hoist(df, x, "b", .ptype = list(b = double())) - ))) + expect_snapshot( + hoist(df, x, "b", .ptype = list(b = double())), + error = TRUE + ) }) test_that("non-vectors generate a cast error if a ptype is supplied", { df <- tibble(x = list(list(b = quote(a)))) - expect_snapshot((expect_error( - hoist(df, x, "b", .ptype = list(b = integer())) - ))) + expect_snapshot( + hoist(df, x, "b", .ptype = list(b = integer())), + error = TRUE + ) }) test_that("a ptype generates a list-of if the col can't be simplified (#998)", { @@ -99,10 +101,14 @@ test_that("can hoist out scalars", { test_that("input validation catches problems", { df <- tibble(x = list(list(1, b = "b")), y = 1) - expect_snapshot({ - (expect_error(df %>% hoist(y))) - (expect_error(df %>% hoist(x, 1))) - (expect_error(df %>% hoist(x, a = "a", a = "b"))) + expect_snapshot(error = TRUE, { + df %>% hoist(y) + }) + expect_snapshot(error = TRUE, { + df %>% hoist(x, 1) + }) + expect_snapshot(error = TRUE, { + df %>% hoist(x, a = "a", a = "b") }) }) @@ -114,9 +120,10 @@ test_that("string pluckers are automatically named", { test_that("can't hoist() from a data frame column", { df <- tibble(a = tibble(x = 1)) - expect_snapshot((expect_error( - hoist(df, a, xx = 1) - ))) + expect_snapshot( + hoist(df, a, xx = 1), + error = TRUE + ) }) test_that("can hoist() without any pluckers", { @@ -163,10 +170,20 @@ test_that("hoist() validates its inputs (#1224)", { expect_snapshot(error = TRUE, { hoist(1) + }) + expect_snapshot(error = TRUE, { hoist(df) + }) + expect_snapshot(error = TRUE, { hoist(df, a, .remove = 1) + }) + expect_snapshot(error = TRUE, { hoist(df, a, .ptype = 1) + }) + expect_snapshot(error = TRUE, { hoist(df, a, .transform = 1) + }) + expect_snapshot(error = TRUE, { hoist(df, a, .simplify = 1) }) }) diff --git a/tests/testthat/test-nest-legacy.R b/tests/testthat/test-nest-legacy.R index 3551728bc..a865945eb 100644 --- a/tests/testthat/test-nest-legacy.R +++ b/tests/testthat/test-nest-legacy.R @@ -143,20 +143,20 @@ test_that("elements must all be of same type", { test_that("can't combine vectors and data frames", { df <- tibble(x = list(1, tibble(1))) - expect_snapshot((expect_error(unnest_legacy(df)))) + expect_snapshot(unnest_legacy(df), error = TRUE) }) test_that("multiple columns must be same length", { df <- tibble(x = list(1), y = list(1:2)) - expect_snapshot((expect_error(unnest_legacy(df)))) + expect_snapshot(unnest_legacy(df), error = TRUE) df <- tibble(x = list(1), y = list(tibble(x = 1:2))) - expect_snapshot((expect_error(unnest_legacy(df)))) + expect_snapshot(unnest_legacy(df), error = TRUE) }) test_that("nested is split as a list (#84)", { df <- tibble(x = 1:3, y = list(1, 2:3, 4), z = list(5, 6:7, 8)) - expect_warning(out <- unnest_legacy(df, y, z), NA) + expect_no_warning(out <- unnest_legacy(df, y, z)) expect_equal(out$x, c(1, 2, 2, 3)) expect_equal(out$y, unlist(df$y)) expect_equal(out$z, unlist(df$z)) diff --git a/tests/testthat/test-pack.R b/tests/testthat/test-pack.R index fce41a553..35d3b5377 100644 --- a/tests/testthat/test-pack.R +++ b/tests/testthat/test-pack.R @@ -184,7 +184,11 @@ test_that("unpack() validates its inputs", { expect_snapshot(error = TRUE, { unpack(1) + }) + expect_snapshot(error = TRUE, { unpack(df) + }) + expect_snapshot(error = TRUE, { unpack(df, y, names_sep = 1) }) }) diff --git a/tests/testthat/test-pivot-long.R b/tests/testthat/test-pivot-long.R index 8bb7936b0..04131344c 100644 --- a/tests/testthat/test-pivot-long.R +++ b/tests/testthat/test-pivot-long.R @@ -147,32 +147,28 @@ test_that("type error message use variable names", { test_that("when `values_ptypes` is provided, the type error uses variable names (#1364)", { df <- tibble(x = 1) - expect_snapshot({ - (expect_error(pivot_longer(df, x, values_ptypes = character()))) + expect_snapshot(error = TRUE, { + pivot_longer(df, x, values_ptypes = character()) }) }) test_that("when `names_ptypes` is provided, the type error uses `names_to` names (#1364)", { df <- tibble(x = 1) - expect_snapshot({ - (expect_error({ - pivot_longer( - df, - cols = x, - names_to = "name", - names_ptypes = double() - ) - })) + expect_snapshot(error = TRUE, { + pivot_longer( + df, + cols = x, + names_to = "name", + names_ptypes = double() + ) }) }) test_that("error when overwriting existing column", { df <- tibble(x = 1, y = 2) - expect_snapshot( - (expect_error(pivot_longer(df, y, names_to = "x"))) - ) + expect_snapshot(pivot_longer(df, y, names_to = "x"), error = TRUE) expect_snapshot( out <- pivot_longer(df, y, names_to = "x", names_repair = "unique") @@ -267,18 +263,17 @@ test_that("no names doesn't generate names (#1120)", { test_that("multiple names requires names_sep/names_pattern", { df <- tibble(x_y = 1) - expect_snapshot({ - (expect_error(build_longer_spec(df, x_y, names_to = c("a", "b")))) - - (expect_error( - build_longer_spec( - df, - x_y, - names_to = c("a", "b"), - names_sep = "x", - names_pattern = "x" - ) - )) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x_y, names_to = c("a", "b")) + }) + expect_snapshot(error = TRUE, { + build_longer_spec( + df, + x_y, + names_to = c("a", "b"), + names_sep = "x", + names_pattern = "x" + ) }) }) @@ -293,8 +288,8 @@ test_that("names_sep generates correct spec", { test_that("names_sep fails with single name", { df <- tibble(x_y = 1) - expect_snapshot({ - (expect_error(build_longer_spec(df, x_y, names_to = "x", names_sep = "_"))) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x_y, names_to = "x", names_sep = "_") }) }) @@ -435,8 +430,8 @@ test_that("`values_transform` works with single functions (#1284)", { }) test_that("Error if the `col` can't be selected.", { - expect_snapshot({ - (expect_error(pivot_longer(iris, matches("foo")))) + expect_snapshot(error = TRUE, { + pivot_longer(iris, matches("foo")) }) }) @@ -448,73 +443,91 @@ test_that("named `cols` gives clear error (#1104)", { test_that("`names_to` is validated", { df <- tibble(x = 1) - expect_snapshot({ - (expect_error(build_longer_spec(df, x, names_to = 1))) - (expect_error(build_longer_spec(df, x, names_to = c("x", "y")))) - (expect_error(build_longer_spec(df, x, names_to = c("x", "y"), names_sep = "_", names_pattern = "x"))) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x, names_to = 1) + }) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x, names_to = c("x", "y")) + }) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x, names_to = c("x", "y"), names_sep = "_", names_pattern = "x") }) }) test_that("`names_ptypes` is validated", { df <- tibble(x = 1) - expect_snapshot({ - (expect_error(build_longer_spec(df, x, names_ptypes = 1))) - (expect_error(build_longer_spec(df, x, names_ptypes = list(integer())))) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x, names_ptypes = 1) + }) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x, names_ptypes = list(integer())) }) }) test_that("`names_transform` is validated", { df <- tibble(x = 1) - expect_snapshot({ - (expect_error(build_longer_spec(df, x, names_transform = 1))) - (expect_error(build_longer_spec(df, x, names_transform = list(~.x)))) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x, names_transform = 1) + }) + expect_snapshot(error = TRUE, { + build_longer_spec(df, x, names_transform = list(~.x)) }) }) test_that("`values_ptypes` is validated", { df <- tibble(x = 1) - expect_snapshot({ - (expect_error(pivot_longer(df, x, values_ptypes = 1))) - (expect_error(pivot_longer(df, x, values_ptypes = list(integer())))) + expect_snapshot(error = TRUE, { + pivot_longer(df, x, values_ptypes = 1) + }) + expect_snapshot(error = TRUE, { + pivot_longer(df, x, values_ptypes = list(integer())) }) }) test_that("`values_transform` is validated", { df <- tibble(x = 1) - expect_snapshot({ - (expect_error(pivot_longer(df, x, values_transform = 1))) - (expect_error(pivot_longer(df, x, values_transform = list(~.x)))) + expect_snapshot(error = TRUE, { + pivot_longer(df, x, values_transform = 1) + }) + expect_snapshot(error = TRUE, { + pivot_longer(df, x, values_transform = list(~.x)) }) }) test_that("`cols_vary` is validated", { df <- tibble(x = 1) - expect_snapshot({ - (expect_error(pivot_longer(df, x, cols_vary = "fast"))) - (expect_error(pivot_longer(df, x, cols_vary = 1))) + expect_snapshot(error = TRUE, { + pivot_longer(df, x, cols_vary = "fast") + }) + expect_snapshot(error = TRUE, { + pivot_longer(df, x, cols_vary = 1) }) }) test_that("`pivot_longer()` catches unused input passed through the dots", { df <- tibble(id = c("a", "b"), x = c(1, 2), y = c(3, 4)) - expect_snapshot({ - (expect_error(pivot_longer(df, c(x, y), 1))) - (expect_error(pivot_longer(df, c(x, y), col_vary = "slowest"))) + expect_snapshot(error = TRUE, { + pivot_longer(df, c(x, y), 1) + }) + expect_snapshot(error = TRUE, { + pivot_longer(df, c(x, y), col_vary = "slowest") }) }) test_that("`build_longer_spec()` requires empty dots", { df <- tibble(id = c("a", "b"), x = c(1, 2), y = c(3, 4)) - expect_snapshot({ - (expect_error(build_longer_spec(df, c(x, y), 1))) - (expect_error(build_longer_spec(df, c(x, y), name_to = "name"))) + expect_snapshot(error = TRUE, { + build_longer_spec(df, c(x, y), 1) + }) + expect_snapshot(error = TRUE, { + build_longer_spec(df, c(x, y), name_to = "name") }) }) @@ -522,8 +535,10 @@ test_that("`pivot_longer_spec()` requires empty dots", { df <- tibble(id = c("a", "b"), x = c(1, 2), y = c(3, 4)) spec <- build_longer_spec(df, c(x, y)) - expect_snapshot({ - (expect_error(pivot_longer_spec(df, spec, 1))) - (expect_error(pivot_longer_spec(df, spec, col_vary = "slowest"))) + expect_snapshot(error = TRUE, { + pivot_longer_spec(df, spec, 1) + }) + expect_snapshot(error = TRUE, { + pivot_longer_spec(df, spec, col_vary = "slowest") }) }) diff --git a/tests/testthat/test-pivot-wide.R b/tests/testthat/test-pivot-wide.R index 224a11ffa..1b8c8cd6c 100644 --- a/tests/testthat/test-pivot-wide.R +++ b/tests/testthat/test-pivot-wide.R @@ -38,9 +38,9 @@ test_that("error when overwriting existing column", { val = c(1, 2) ) - expect_snapshot( - (expect_error(pivot_wider(df, names_from = key, values_from = val))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, names_from = key, values_from = val) + }) expect_snapshot( out <- pivot_wider(df, names_from = key, values_from = val, names_repair = "unique") @@ -115,33 +115,37 @@ test_that("works with data.table and empty key_vars", { test_that("`names_from` must be supplied if `name` isn't in `data` (#1240)", { df <- tibble(key = "x", val = 1) - expect_snapshot((expect_error(pivot_wider(df, values_from = val)))) + expect_snapshot(error = TRUE, { + pivot_wider(df, values_from = val) + }) }) test_that("`values_from` must be supplied if `value` isn't in `data` (#1240)", { df <- tibble(key = "x", val = 1) - expect_snapshot((expect_error(pivot_wider(df, names_from = key)))) + expect_snapshot(error = TRUE, { + pivot_wider(df, names_from = key) + }) }) test_that("`names_from` must identify at least 1 column (#1240)", { df <- tibble(key = "x", val = 1) - expect_snapshot( - (expect_error(pivot_wider(df, names_from = starts_with("foo"), values_from = val))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, names_from = starts_with("foo"), values_from = val) + }) }) test_that("`values_from` must identify at least 1 column (#1240)", { df <- tibble(key = "x", val = 1) - expect_snapshot( - (expect_error(pivot_wider(df, names_from = key, values_from = starts_with("foo")))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, names_from = key, values_from = starts_with("foo")) + }) }) test_that("`values_fn` emits an informative error when it doesn't result in unique values (#1238)", { df <- tibble(name = c("a", "a"), value = c(1, 2)) - expect_snapshot( - (expect_error(pivot_wider(df, values_fn = list(value = ~.x)))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, values_fn = list(value = ~.x)) + }) }) test_that("can pivot a manual spec with spec columns that don't identify any rows (#1250)", { @@ -217,9 +221,11 @@ test_that("expansion with `id_expand` and `names_expand` works with zero row dat test_that("`build_wider_spec()` requires empty dots", { df <- tibble(name = c("x", "y", "z"), value = 1:3) - expect_snapshot({ - (expect_error(build_wider_spec(df, 1))) - (expect_error(build_wider_spec(df, name_prefix = ""))) + expect_snapshot(error = TRUE, { + build_wider_spec(df, 1) + }) + expect_snapshot(error = TRUE, { + build_wider_spec(df, name_prefix = "") }) }) @@ -227,9 +233,11 @@ test_that("`pivot_wider_spec()` requires empty dots", { df <- tibble(name = c("x", "y", "z"), value = 1:3) spec <- build_wider_spec(df) - expect_snapshot({ - (expect_error(pivot_wider_spec(df, spec, 1))) - (expect_error(pivot_wider_spec(df, spec, name_repair = "check_unique"))) + expect_snapshot(error = TRUE, { + pivot_wider_spec(df, spec, 1) + }) + expect_snapshot(error = TRUE, { + pivot_wider_spec(df, spec, name_repair = "check_unique") }) }) @@ -290,9 +298,11 @@ test_that("can vary `names_from` values slowest (#839)", { test_that("`names_vary` is validated", { df <- tibble(name = c("a", "b"), value = c(1, 2)) - expect_snapshot({ - (expect_error(build_wider_spec(df, names_vary = 1))) - (expect_error(build_wider_spec(df, names_vary = "x"))) + expect_snapshot(error = TRUE, { + build_wider_spec(df, names_vary = 1) + }) + expect_snapshot(error = TRUE, { + build_wider_spec(df, names_vary = "x") }) }) @@ -318,9 +328,11 @@ test_that("`names_expand` expands all levels of a factor `names_from` column (#7 test_that("`names_expand` is validated", { df <- tibble(name = c("a", "b"), value = c(1, 2)) - expect_snapshot({ - (expect_error(build_wider_spec(df, names_expand = 1))) - (expect_error(build_wider_spec(df, names_expand = "x"))) + expect_snapshot(error = TRUE, { + build_wider_spec(df, names_expand = 1) + }) + expect_snapshot(error = TRUE, { + build_wider_spec(df, names_expand = "x") }) }) @@ -358,18 +370,20 @@ test_that("`id_cols` can't select columns from `names_from` or `values_from` (#1 df <- tibble(name = c("x", "y"), value = c(1, 2)) # And gives a nice error message! - expect_snapshot({ - (expect_error(pivot_wider(df, id_cols = name, names_from = name, values_from = value))) - (expect_error(pivot_wider(df, id_cols = value, names_from = name, values_from = value))) + expect_snapshot(error = TRUE, { + pivot_wider(df, id_cols = name, names_from = name, values_from = value) + }) + expect_snapshot(error = TRUE, { + pivot_wider(df, id_cols = value, names_from = name, values_from = value) }) }) test_that("`id_cols` returns a tidyselect error if a column selection is OOB (#1318)", { df <- tibble(name = c("x", "y"), value = c(1, 2)) - expect_snapshot( - (expect_error(pivot_wider(df, id_cols = foo))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, id_cols = foo) + }) }) test_that("named `id_cols` gives clear error (#1104)", { @@ -470,9 +484,11 @@ test_that("`id_expand` with `values_fill` can't accidentally fill missings in `i test_that("`id_expand` is validated", { df <- tibble(name = c("a", "b"), value = c(1, 2)) - expect_snapshot({ - (expect_error(pivot_wider(df, id_expand = 1))) - (expect_error(pivot_wider(df, id_expand = "x"))) + expect_snapshot(error = TRUE, { + pivot_wider(df, id_expand = 1) + }) + expect_snapshot(error = TRUE, { + pivot_wider(df, id_expand = "x") }) }) @@ -526,13 +542,12 @@ test_that("duplicated key warning backticks non-syntactic names", { test_that("warning suppressed by supplying values_fn", { df <- tibble(a = c(1, 1, 2), key = c("x", "x", "x"), val = 1:3) - expect_warning( + expect_no_warning( pv <- pivot_wider(df, names_from = key, values_from = val, values_fn = list(val = list) - ), - NA + ) ) expect_equal(pv$a, c(1, 2)) expect_equal(as.list(pv$x), list(c(1L, 2L), 3L)) @@ -564,9 +579,9 @@ test_that("values_fn applied even when no-duplicates", { test_that("values_fn is validated", { df <- tibble(name = "x", value = 1L) - expect_snapshot( - (expect_error(pivot_wider(df, values_fn = 1))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, values_fn = 1) + }) }) # can fill missing cells -------------------------------------------------- @@ -687,9 +702,9 @@ test_that("`unused_fn` must result in single summary values", { value = c(1, 2, 3, 4) ) - expect_snapshot( - (expect_error(pivot_wider(df, id_cols = id, unused_fn = identity))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, id_cols = id, unused_fn = identity) + }) }) test_that("`unused_fn` works with expanded key from `id_expand`", { @@ -745,17 +760,17 @@ test_that("can't fill implicit missings in unused column with `values_fill`", { test_that("`values_fill` is validated", { df <- tibble(name = "a", value = 1) - expect_snapshot( - (expect_error(pivot_wider(df, values_fill = 1:2))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, values_fill = 1:2) + }) }) test_that("`unused_fn` is validated", { df <- tibble(id = 1, unused = 1, name = "a", value = 1) - expect_snapshot( - (expect_error(pivot_wider(df, id_cols = id, unused_fn = 1))) - ) + expect_snapshot(error = TRUE, { + pivot_wider(df, id_cols = id, unused_fn = 1) + }) }) # deprecated --------------------------------------------------------------- diff --git a/tests/testthat/test-pivot.R b/tests/testthat/test-pivot.R index 790afef75..b7528d9d3 100644 --- a/tests/testthat/test-pivot.R +++ b/tests/testthat/test-pivot.R @@ -1,21 +1,21 @@ test_that("basic sanity checks for spec occur", { - expect_snapshot({ - (expect_error(check_pivot_spec(1))) - (expect_error(check_pivot_spec(mtcars))) + expect_snapshot(error = TRUE, { + check_pivot_spec(1) + check_pivot_spec(mtcars) }) }) test_that("`.name` column must be a character vector", { df <- tibble(.name = 1:2, .value = c("a", "b")) - expect_snapshot((expect_error(check_pivot_spec(df)))) + expect_snapshot(check_pivot_spec(df), error = TRUE) }) test_that("`.value` column must be a character vector", { df <- tibble(.name = c("x", "y"), .value = 1:2) - expect_snapshot((expect_error(check_pivot_spec(df)))) + expect_snapshot(check_pivot_spec(df), error = TRUE) }) test_that("`.name` column must be unique", { df <- tibble(.name = c("x", "x"), .value = c("a", "b")) - expect_snapshot((expect_error(check_pivot_spec(df)))) + expect_snapshot(check_pivot_spec(df), error = TRUE) }) diff --git a/tests/testthat/test-replace_na.R b/tests/testthat/test-replace_na.R index 9254f41ac..58a80e939 100644 --- a/tests/testthat/test-replace_na.R +++ b/tests/testthat/test-replace_na.R @@ -11,7 +11,7 @@ test_that("missing values are replaced", { }) test_that("can only be length 0", { - expect_snapshot((expect_error(replace_na(1, 1:10)))) + expect_snapshot(replace_na(1, 1:10), error = TRUE) }) test_that("can replace missing rows in arrays", { @@ -34,7 +34,7 @@ test_that("can replace missing values in rcrds", { test_that("replacement must be castable to `data`", { x <- c(1L, NA) - expect_snapshot((expect_error(replace_na(x, 1.5)))) + expect_snapshot(replace_na(x, 1.5), error = TRUE) }) test_that("empty atomic elements are not replaced in lists (#1168)", { @@ -95,7 +95,7 @@ test_that("df-col rows must be completely missing to be replaceable", { test_that("replacement must be castable to corresponding column", { df <- tibble(a = c(1L, NA)) - expect_snapshot((expect_error(replace_na(df, list(a = 1.5))))) + expect_snapshot(replace_na(df, list(a = 1.5)), error = TRUE) }) test_that("validates its inputs", { diff --git a/tests/testthat/test-separate-longer.R b/tests/testthat/test-separate-longer.R index cd96b2377..0683b02c4 100644 --- a/tests/testthat/test-separate-longer.R +++ b/tests/testthat/test-separate-longer.R @@ -9,6 +9,8 @@ test_that("separate_longer_delim() validates its inputs", { df <- tibble(x = "x") expect_snapshot(error = TRUE, { df %>% separate_longer_delim() + }) + expect_snapshot(error = TRUE, { df %>% separate_longer_delim(x, sep = 1) }) }) @@ -41,7 +43,11 @@ test_that("separate_longer_position() validates its inputs", { df <- tibble(x = "x") expect_snapshot(error = TRUE, { df %>% separate_longer_position() + }) + expect_snapshot(error = TRUE, { df %>% separate_longer_position(y, width = 1) + }) + expect_snapshot(error = TRUE, { df %>% separate_longer_position(x, width = 1.5) }) }) diff --git a/tests/testthat/test-separate-rows.R b/tests/testthat/test-separate-rows.R index 8a096fbca..69123e0c8 100644 --- a/tests/testthat/test-separate-rows.R +++ b/tests/testthat/test-separate-rows.R @@ -75,6 +75,8 @@ test_that("it validates its inputs", { expect_snapshot(error = TRUE, { separate_rows(df, x, sep = 1) + }) + expect_snapshot(error = TRUE, { separate_rows(df, x, convert = 1) }) }) diff --git a/tests/testthat/test-separate-wider.R b/tests/testthat/test-separate-wider.R index 41cb20f9f..ed02a603e 100644 --- a/tests/testthat/test-separate-wider.R +++ b/tests/testthat/test-separate-wider.R @@ -79,12 +79,26 @@ test_that("separate_wider_delim() validates its inputs", { df <- tibble(x = "x") expect_snapshot(error = TRUE, { df %>% separate_wider_delim() + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_delim(x) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_delim(x, 1) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_delim(x, "") + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_delim(x, "-") + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_delim(x, "-", names = 1) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_delim(x, "-", names = c(x = "x")) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_delim(x, "-", names_sep = "_", too_many = "merge") }) }) @@ -153,9 +167,17 @@ test_that("separate_wider_position() validates its inputs", { df <- tibble(x = "x") expect_snapshot(error = TRUE, { df %>% separate_wider_position() + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_position(x) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_position(x, widths = 1.5) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_position(x, widths = 1L) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_position(x, widths = c(x = 0)) }) }) @@ -254,8 +276,14 @@ test_that("separate_wider_regex() validates its inputs", { df <- tibble(x = "x") expect_snapshot(error = TRUE, { df %>% separate_wider_regex() + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_regex(x) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_regex(y, patterns = c(x = "-")) + }) + expect_snapshot(error = TRUE, { df %>% separate_wider_regex(x, patterns = ".") }) }) diff --git a/tests/testthat/test-separate.R b/tests/testthat/test-separate.R index 660a7d2c3..7be7076eb 100644 --- a/tests/testthat/test-separate.R +++ b/tests/testthat/test-separate.R @@ -110,9 +110,17 @@ test_that("validates inputs", { expect_snapshot(error = TRUE, { separate(df) + }) + expect_snapshot(error = TRUE, { separate(df, x, into = 1) + }) + expect_snapshot(error = TRUE, { separate(df, x, into = "x", sep = c("a", "b")) + }) + expect_snapshot(error = TRUE, { separate(df, x, into = "x", remove = 1) + }) + expect_snapshot(error = TRUE, { separate(df, x, into = "x", convert = 1) }) }) @@ -121,7 +129,7 @@ test_that("informative error if using stringr modifier functions (#693)", { df <- tibble(x = "a") sep <- structure("a", class = "pattern") - expect_snapshot((expect_error(separate(df, x, "x", sep = sep)))) + expect_snapshot(separate(df, x, "x", sep = sep), error = TRUE) }) # helpers ----------------------------------------------------------------- diff --git a/tests/testthat/test-seq.R b/tests/testthat/test-seq.R index 10e0f0123..c58ed3616 100644 --- a/tests/testthat/test-seq.R +++ b/tests/testthat/test-seq.R @@ -23,9 +23,9 @@ test_that("preserves attributes", { }) test_that("full_seq errors if sequence isn't regular", { - expect_snapshot({ - (expect_error(full_seq(c(1, 3, 4), 2))) - (expect_error(full_seq(c(0, 10, 20), 11, tol = 1.8))) + expect_snapshot(error = TRUE, { + full_seq(c(1, 3, 4), 2) + full_seq(c(0, 10, 20), 11, tol = 1.8) }) }) diff --git a/tests/testthat/test-spread.R b/tests/testthat/test-spread.R index 503e65fae..bfab97c5f 100644 --- a/tests/testthat/test-spread.R +++ b/tests/testthat/test-spread.R @@ -22,7 +22,7 @@ test_that("convert turns strings into integers", { test_that("duplicate values for one key is an error", { df <- tibble(x = factor(c("a", "b", "b")), y = c(1, 2, 2), z = c(1, 2, 2)) - expect_snapshot((expect_error(spread(df, x, y)))) + expect_snapshot(spread(df, x, y), error = TRUE) }) test_that("factors are spread into columns (#35)", { diff --git a/tests/testthat/test-unnest-helper.R b/tests/testthat/test-unnest-helper.R index b96e79236..07b81df37 100644 --- a/tests/testthat/test-unnest-helper.R +++ b/tests/testthat/test-unnest-helper.R @@ -1,29 +1,29 @@ # df_simplify ------------------------------------------------------------ test_that("`simplify` is validated", { - expect_snapshot({ - (expect_error(df_simplify(data.frame(), simplify = 1))) - (expect_error(df_simplify(data.frame(), simplify = NA))) - (expect_error(df_simplify(data.frame(), simplify = c(TRUE, FALSE)))) - (expect_error(df_simplify(data.frame(), simplify = list(1)))) - (expect_error(df_simplify(data.frame(), simplify = list(x = 1, x = 1)))) + expect_snapshot(error = TRUE, { + df_simplify(data.frame(), simplify = 1) + df_simplify(data.frame(), simplify = NA) + df_simplify(data.frame(), simplify = c(TRUE, FALSE)) + df_simplify(data.frame(), simplify = list(1)) + df_simplify(data.frame(), simplify = list(x = 1, x = 1)) }) }) test_that("`ptype` is validated", { - expect_snapshot({ - (expect_error(df_simplify(data.frame(), ptype = 1))) - (expect_error(df_simplify(data.frame(), ptype = list(1)))) - (expect_error(df_simplify(data.frame(), ptype = list(x = 1, x = 1)))) + expect_snapshot(error = TRUE, { + df_simplify(data.frame(), ptype = 1) + df_simplify(data.frame(), ptype = list(1)) + df_simplify(data.frame(), ptype = list(x = 1, x = 1)) }) }) test_that("`transform` is validated", { - expect_snapshot({ - (expect_error(df_simplify(data.frame(), transform = list(~.x)))) - (expect_error(df_simplify(data.frame(x = 1), transform = 1))) - (expect_error(df_simplify(data.frame(), transform = list(x = 1)))) - (expect_error(df_simplify(data.frame(), transform = list(x = 1, x = 1)))) + expect_snapshot(error = TRUE, { + df_simplify(data.frame(), transform = list(~.x)) + df_simplify(data.frame(x = 1), transform = 1) + df_simplify(data.frame(), transform = list(x = 1)) + df_simplify(data.frame(), transform = list(x = 1, x = 1)) }) }) @@ -140,9 +140,9 @@ test_that("ptype is applied after transform", { c(2L, 3L, 4L) ) - expect_snapshot((expect_error( + expect_snapshot(error = TRUE, { col_simplify(list(1, 2, 3), ptype = integer(), transform = ~ .x + 1.5) - ))) + }) }) test_that("lists of lists aren't simplified", { diff --git a/tests/testthat/test-unnest-longer.R b/tests/testthat/test-unnest-longer.R index 7ee6a8beb..ae052dac1 100644 --- a/tests/testthat/test-unnest-longer.R +++ b/tests/testthat/test-unnest-longer.R @@ -46,9 +46,7 @@ test_that("can unnest dates", { test_that("unnest_longer - bad inputs generate errors", { df <- tibble(x = 1, y = list(mean)) - expect_snapshot((expect_error( - unnest_longer(df, y) - ))) + expect_snapshot(unnest_longer(df, y), error = TRUE) }) test_that("list_of columns can be unnested", { @@ -378,9 +376,9 @@ test_that("can't currently retain names when simplification isn't done and a pty }) test_that("can't mix `indices_to` with `indices_include = FALSE`", { - expect_snapshot((expect_error( + expect_snapshot(error = TRUE, { unnest_longer(mtcars, mpg, indices_to = "x", indices_include = FALSE) - ))) + }) }) test_that("unnest_longer() validates its inputs", { @@ -402,29 +400,29 @@ test_that("`values_to` and `indices_to` glue can't reach into surrounding env", }) test_that("`values_to` is validated", { - expect_snapshot({ - (expect_error(unnest_longer(mtcars, mpg, values_to = 1))) - (expect_error(unnest_longer(mtcars, mpg, values_to = c("x", "y")))) + expect_snapshot(error = TRUE, { + unnest_longer(mtcars, mpg, values_to = 1) + unnest_longer(mtcars, mpg, values_to = c("x", "y")) }) }) test_that("`indices_to` is validated", { - expect_snapshot({ - (expect_error(unnest_longer(mtcars, mpg, indices_to = 1))) - (expect_error(unnest_longer(mtcars, mpg, indices_to = c("x", "y")))) + expect_snapshot(error = TRUE, { + unnest_longer(mtcars, mpg, indices_to = 1) + unnest_longer(mtcars, mpg, indices_to = c("x", "y")) }) }) test_that("`indices_include` is validated", { - expect_snapshot({ - (expect_error(unnest_longer(mtcars, mpg, indices_include = 1))) - (expect_error(unnest_longer(mtcars, mpg, indices_include = c(TRUE, FALSE)))) + expect_snapshot(error = TRUE, { + unnest_longer(mtcars, mpg, indices_include = 1) + unnest_longer(mtcars, mpg, indices_include = c(TRUE, FALSE)) }) }) test_that("`keep_empty` is validated", { - expect_snapshot({ - (expect_error(unnest_longer(mtcars, mpg, keep_empty = 1))) - (expect_error(unnest_longer(mtcars, mpg, keep_empty = c(TRUE, FALSE)))) + expect_snapshot(error = TRUE, { + unnest_longer(mtcars, mpg, keep_empty = 1) + unnest_longer(mtcars, mpg, keep_empty = c(TRUE, FALSE)) }) }) diff --git a/tests/testthat/test-unnest-wider.R b/tests/testthat/test-unnest-wider.R index a924bf392..41fe6c4ec 100644 --- a/tests/testthat/test-unnest-wider.R +++ b/tests/testthat/test-unnest-wider.R @@ -53,9 +53,7 @@ test_that("treats data frames like lists where we have type info about each elem test_that("unnest_wider - bad inputs generate errors", { df <- tibble(x = 1, y = list(mean)) - expect_snapshot((expect_error( - unnest_wider(df, y) - ))) + expect_snapshot(unnest_wider(df, y), error = TRUE) }) test_that("list of 0-length vectors yields no new columns", { @@ -94,10 +92,10 @@ test_that("names_sep creates unique names", { x = list("a", c("a", "b", "c")), y = list(c(a = 1), c(b = 2, a = 1)) ) - expect_warning(out <- unnest_wider(df, x, names_sep = "_"), NA) + expect_no_warning(out <- unnest_wider(df, x, names_sep = "_")) expect_named(out, c("x_1", "x_2", "x_3", "y")) - expect_warning(out <- unnest_wider(df, y, names_sep = "_"), NA) + expect_no_warning(out <- unnest_wider(df, y, names_sep = "_")) expect_named(out, c("x", "y_a", "y_b")) expect_equal(out$y_a, c(1, 1)) }) @@ -311,8 +309,14 @@ test_that("unnest_wider() validates its inputs", { df <- tibble(x = list(a = 1:2, b = 3:4)) expect_snapshot(error = TRUE, { unnest_wider(1) + }) + expect_snapshot(error = TRUE, { unnest_wider(df) + }) + expect_snapshot(error = TRUE, { unnest_wider(df, x, names_sep = 1) + }) + expect_snapshot(error = TRUE, { unnest_wider(df, x, strict = 1) }) }) diff --git a/tests/testthat/test-unnest.R b/tests/testthat/test-unnest.R index 76a505759..3ee670ef4 100644 --- a/tests/testthat/test-unnest.R +++ b/tests/testthat/test-unnest.R @@ -22,7 +22,7 @@ test_that("empty rows still affect output type", { test_that("bad inputs generate errors", { df <- tibble(x = 1, y = list(mean)) - expect_snapshot((expect_error(unnest(df, y)))) + expect_snapshot(unnest(df, y), error = TRUE) }) test_that("unnesting combines augmented vectors", { @@ -96,10 +96,10 @@ test_that("vectors become columns", { test_that("multiple columns must be same length", { df <- tibble(x = list(1:2), y = list(1:3)) - expect_snapshot((expect_error(unnest(df, c(x, y))))) + expect_snapshot(unnest(df, c(x, y)), error = TRUE) df <- tibble(x = list(1:2), y = list(tibble(y = 1:3))) - expect_snapshot((expect_error(unnest(df, c(x, y))))) + expect_snapshot(unnest(df, c(x, y)), error = TRUE) }) test_that("can use non-syntactic names", { @@ -114,7 +114,7 @@ test_that("unpacks df-cols (#1112)", { test_that("unnesting column of mixed vector / data frame input is an error", { df <- tibble(x = list(1, tibble(a = 1))) - expect_snapshot((expect_error(unnest(df, x)))) + expect_snapshot(unnest(df, x), error = TRUE) }) test_that("unnest() advises on outer / inner name duplication", {