From 8ab143a9bd033a33a4561e27d69d2e73cc21344b Mon Sep 17 00:00:00 2001 From: catalamarti Date: Thu, 15 Aug 2024 15:00:01 -0700 Subject: [PATCH] Replace `expect_error()` with `expect_snapshot()` (#1142) Closes #1132. --- tests/testthat/_snaps/adverb-auto-browse.md | 7 +++ tests/testthat/_snaps/coerce.md | 56 +++++++++++++++++++++ tests/testthat/_snaps/deprec-cross.md | 8 +++ tests/testthat/test-adverb-auto-browse.R | 2 +- tests/testthat/test-coerce.R | 14 +++--- tests/testthat/test-deprec-cross.R | 2 +- tests/testthat/test-deprec-prepend.R | 15 ++---- tests/testthat/test-deprec-utils.R | 8 +-- tests/testthat/test-deprec-when.R | 2 +- tests/testthat/test-every-some-none.R | 4 +- tests/testthat/test-map-depth.R | 4 +- tests/testthat/test-pluck.R | 4 +- 12 files changed, 94 insertions(+), 32 deletions(-) diff --git a/tests/testthat/_snaps/adverb-auto-browse.md b/tests/testthat/_snaps/adverb-auto-browse.md index 18381b53..3ca159c5 100644 --- a/tests/testthat/_snaps/adverb-auto-browse.md +++ b/tests/testthat/_snaps/adverb-auto-browse.md @@ -6,3 +6,10 @@ Error in `auto_browse()`: ! `.f` must not be a primitive function. +--- + + Code + auto_browse(identity)(NULL) + Output + NULL + diff --git a/tests/testthat/_snaps/coerce.md b/tests/testthat/_snaps/coerce.md index 0b35c341..1197f9da 100644 --- a/tests/testthat/_snaps/coerce.md +++ b/tests/testthat/_snaps/coerce.md @@ -1,3 +1,51 @@ +# can coerce to logical vectors + + Code + coerce_lgl(2L) + Condition + Error: + ! Can't coerce from an integer to a logical. + +--- + + Code + coerce_lgl(1.5) + Condition + Error: + ! Can't coerce from a number to a logical. + +--- + + Code + coerce_lgl("true") + Condition + Error: + ! Can't coerce from a string to a logical. + +# can coerce to integer vectors + + Code + coerce_int(1.5) + Condition + Error: + ! Can't coerce from a number to an integer. + +--- + + Code + coerce_int("1") + Condition + Error: + ! Can't coerce from a string to an integer. + +# can coerce to double vctrs + + Code + coerce_dbl("1.5") + Condition + Error: + ! Can't coerce from a string to a double. + # can coerce to character vectors Code @@ -34,3 +82,11 @@ Output [1] "1" "2" "3" "4" +# can't coerce to expressions + + Code + coerce(list(1), "expression") + Condition + Error: + ! Can't coerce from a list to expression. + diff --git a/tests/testthat/_snaps/deprec-cross.md b/tests/testthat/_snaps/deprec-cross.md index ed35e1e6..568a8d7d 100644 --- a/tests/testthat/_snaps/deprec-cross.md +++ b/tests/testthat/_snaps/deprec-cross.md @@ -6,3 +6,11 @@ Error in `cross()`: ! The filter function must return a single `TRUE` or `FALSE`, not a logical vector. +# filtering fails when filter function doesn't return a logical + + Code + cross3(1:3, 1:3, 1:3, .filter = filter) + Condition + Error in `cross()`: + ! The filter function must return a single `TRUE` or `FALSE`, not an integer. + diff --git a/tests/testthat/test-adverb-auto-browse.R b/tests/testthat/test-adverb-auto-browse.R index 0ec1d163..f4a4afcd 100644 --- a/tests/testthat/test-adverb-auto-browse.R +++ b/tests/testthat/test-adverb-auto-browse.R @@ -1,4 +1,4 @@ test_that("auto_browse() not intended for primitive functions", { expect_snapshot(auto_browse(log)(NULL), error = TRUE) - expect_error(auto_browse(identity)(NULL), NA) + expect_no_error(auto_browse(identity)(NULL)) }) diff --git a/tests/testthat/test-coerce.R b/tests/testthat/test-coerce.R index d4c7391f..5fe06814 100644 --- a/tests/testthat/test-coerce.R +++ b/tests/testthat/test-coerce.R @@ -2,12 +2,12 @@ test_that("can coerce to logical vectors",{ expect_equal(coerce_lgl(c(TRUE, FALSE, NA)), c(TRUE, FALSE, NA)) expect_equal(coerce_lgl(c(1L, 0L, NA)), c(TRUE, FALSE, NA)) - expect_error(coerce_lgl(2L), "Can't coerce") + expect_snapshot(coerce_lgl(2L), error = TRUE) expect_equal(coerce_lgl(c(1, 0, NA)), c(TRUE, FALSE, NA)) - expect_error(coerce_lgl(1.5), "Can't coerce") + expect_snapshot(coerce_lgl(1.5), error = TRUE) - expect_error(coerce_lgl("true"), "Can't coerce") + expect_snapshot(coerce_lgl("true"), error = TRUE) }) test_that("can coerce to integer vectors", { @@ -16,9 +16,9 @@ test_that("can coerce to integer vectors", { expect_identical(coerce_int(c(NA, 1L, 10L)), c(NA, 1L, 10L)) expect_identical(coerce_int(c(NA, 1, 10)), c(NA, 1L, 10L)) - expect_error(coerce_int(1.5), "Can't coerce") + expect_snapshot(coerce_int(1.5), error = TRUE) - expect_error(coerce_int("1"), "Can't coerce") + expect_snapshot(coerce_int("1"), error = TRUE) }) test_that("can coerce to double vctrs", { @@ -28,7 +28,7 @@ test_that("can coerce to double vctrs", { expect_identical(coerce_dbl(c(NA, 1.5)), c(NA, 1.5)) - expect_error(coerce_dbl("1.5"), "Can't coerce") + expect_snapshot(coerce_dbl("1.5"), error = TRUE) }) test_that("can coerce to character vectors", { @@ -61,5 +61,5 @@ test_that("warns once per vector", { }) test_that("can't coerce to expressions", { - expect_error(coerce(list(1), "expression")) + expect_snapshot(coerce(list(1), "expression"), error = TRUE) }) diff --git a/tests/testthat/test-deprec-cross.R b/tests/testthat/test-deprec-cross.R index 15e41596..cc4f2dba 100644 --- a/tests/testthat/test-deprec-cross.R +++ b/tests/testthat/test-deprec-cross.R @@ -25,7 +25,7 @@ test_that("filtering requires a predicate function", { test_that("filtering fails when filter function doesn't return a logical", { local_options(lifecycle_verbosity = "quiet") filter <- function(x, y, z) x + y + z - expect_error(cross3(1:3, 1:3, 1:3, .filter = filter)) + expect_snapshot(cross3(1:3, 1:3, 1:3, .filter = filter), error = TRUE) }) test_that("works with empty input", { diff --git a/tests/testthat/test-deprec-prepend.R b/tests/testthat/test-deprec-prepend.R index 0161aa3e..eb5ae0d7 100644 --- a/tests/testthat/test-deprec-prepend.R +++ b/tests/testthat/test-deprec-prepend.R @@ -33,17 +33,8 @@ test_that("prepend appends at the beginning for empty list by default", { test_that("prepend throws error if before param is neither NULL nor between 1 and length(x)", { local_options(lifecycle_verbosity = "quiet") - expect_error( - prepend(list(), 1, before = 1), - "is.null(before) || (before > 0 && before <= n) is not TRUE" - ) + expect_snapshot(prepend(list(), 1, before = 1), error = TRUE) x <- as.list(1:3) - expect_error( - x %>% prepend(4, before = 0), - "is.null(before) || (before > 0 && before <= n) is not TRUE" - ) - expect_error( - x %>% prepend(4, before = 4), - "is.null(before) || (before > 0 && before <= n) is not TRUE" - ) + expect_snapshot(x %>% prepend(4, before = 0), error = TRUE) + expect_snapshot(x %>% prepend(4, before = 4), error = TRUE) }) diff --git a/tests/testthat/test-deprec-utils.R b/tests/testthat/test-deprec-utils.R index cceae43e..343f9d5e 100644 --- a/tests/testthat/test-deprec-utils.R +++ b/tests/testthat/test-deprec-utils.R @@ -26,10 +26,10 @@ test_that("rdunif works", { test_that("rdunif fails if a and b are not unit length numbers", { local_options(lifecycle_verbosity = "quiet") - expect_error(rdunif(1000, 1, "a")) - expect_error(rdunif(1000, 1, c(0.5, 0.2))) - expect_error(rdunif(1000, FALSE, 2)) - expect_error(rdunif(1000, c(2, 3), 2)) + expect_snapshot(rdunif(1000, 1, "a"), error = TRUE) + expect_snapshot(rdunif(1000, 1, c(0.5, 0.2)), error = TRUE) + expect_snapshot(rdunif(1000, FALSE, 2), error = TRUE) + expect_snapshot(rdunif(1000, c(2, 3), 2), error = TRUE) }) diff --git a/tests/testthat/test-deprec-when.R b/tests/testthat/test-deprec-when.R index 036594e7..3f7cb81d 100644 --- a/tests/testthat/test-deprec-when.R +++ b/tests/testthat/test-deprec-when.R @@ -69,5 +69,5 @@ test_that("default values work without a formula", { test_that("error when named arguments have no matching conditions", { local_options(lifecycle_verbosity = "quiet") - expect_error(1:5 %>% when(a = sum(.) < 5 ~ 3)) + expect_snapshot(1:5 %>% when(a = sum(.) < 5 ~ 3), error = TRUE) }) diff --git a/tests/testthat/test-every-some-none.R b/tests/testthat/test-every-some-none.R index 9c5e8e96..f9aa68d6 100644 --- a/tests/testthat/test-every-some-none.R +++ b/tests/testthat/test-every-some-none.R @@ -17,8 +17,8 @@ test_that("none returns TRUE if all elements are FALSE", { }) test_that("every() requires logical value", { - expect_error(every(list(1:3), identity), "must return a single") - expect_error(every(list(function() NULL), identity), "must return a single") + expect_snapshot(every(list(1:3), identity), error = TRUE) + expect_snapshot(every(list(function() NULL), identity), error = TRUE) }) test_that("every() has the same behaviour as `&&` (#751)", { diff --git a/tests/testthat/test-map-depth.R b/tests/testthat/test-map-depth.R index 9940cb54..0a472b70 100644 --- a/tests/testthat/test-map-depth.R +++ b/tests/testthat/test-map-depth.R @@ -14,7 +14,7 @@ test_that("map_depth modifies values at specified depth", { test_that("default doesn't recurse into data frames, but can customise", { x <- list(data.frame(x = 1), data.frame(y = 2)) - expect_error(map_depth(x, 2, class), "not deep enough") + expect_snapshot(map_depth(x, 2, class), error = TRUE) x <- list(data.frame(x = 1), data.frame(y = 1)) expect_equal( @@ -73,7 +73,7 @@ test_that("vectorised operations on the recursive and atomic levels yield same r x <- list(list(list(1:3, 4:6))) exp <- list(list(list(11:13, 14:16))) expect_identical(modify_depth(x, 3, `+`, 10L), exp) - expect_error(modify_depth(x, 5, `+`, 10L), "not deep enough") + expect_snapshot(modify_depth(x, 5, `+`, 10L), error = TRUE) }) test_that("modify_depth() treats NULLs correctly", { diff --git a/tests/testthat/test-pluck.R b/tests/testthat/test-pluck.R index f636e39e..98dc148f 100644 --- a/tests/testthat/test-pluck.R +++ b/tests/testthat/test-pluck.R @@ -255,7 +255,7 @@ test_that("pluck() dispatches on vector methods", { length.test_pluck = function(x) NA, { expect_null(pluck(x, 1, 1)) - expect_error(chuck(x, 1, 1), "Length of S3 object must be a scalar integer") + expect_snapshot(chuck(x, 1, 1), error = TRUE) } ) @@ -266,7 +266,7 @@ test_that("pluck() dispatches on vector methods", { length.test_pluck = function(x) length(.subset2(x, 1)), { expect_null(pluck(x, 1, "b", 1)) - expect_error(chuck(x, 1, "b", 1), "unnamed vector") + expect_snapshot(chuck(x, 1, "b", 1), error = TRUE) } ) })