Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect_error -> expect_snapshot #1142

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/adverb-auto-browse.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
Error in `auto_browse()`:
! `.f` must not be a primitive function.

---

Code
auto_browse(identity)(NULL)
Output
NULL

56 changes: 56 additions & 0 deletions tests/testthat/_snaps/coerce.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/deprec-cross.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

2 changes: 1 addition & 1 deletion tests/testthat/test-adverb-auto-browse.R
Original file line number Diff line number Diff line change
@@ -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))
})
14 changes: 7 additions & 7 deletions tests/testthat/test-coerce.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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", {
Expand All @@ -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", {
Expand Down Expand Up @@ -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)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-deprec-cross.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
15 changes: 3 additions & 12 deletions tests/testthat/test-deprec-prepend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
8 changes: 4 additions & 4 deletions tests/testthat/test-deprec-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})


Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-deprec-when.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-every-some-none.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-map-depth.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-pluck.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)

Expand All @@ -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)
}
)
})
Expand Down
Loading