diff --git a/tests/testthat/_snaps/expand.md b/tests/testthat/_snaps/expand.md index b072c8e1..24cf35a1 100644 --- a/tests/testthat/_snaps/expand.md +++ b/tests/testthat/_snaps/expand.md @@ -55,6 +55,14 @@ * `x` -> `x...1` * `x` -> `x...2` +# expand_grid() throws an error for invalid `.vary` parameter + + Code + expand_grid(x = 1:2, y = 1:2, .vary = "invalid") + Condition + Error in `expand_grid()`: + ! `.vary` must be one of "slowest" or "fastest", not "invalid". + # grid_dots() reject non-vector input Code @@ -64,11 +72,3 @@ Error: ! `..1` must be a vector, not a object. -# expand_grid() throws an error for invalid .vary parameter - - Code - expand_grid(x = 1:2, y = 1:2, .vary = "invalid") - Condition - Error in `expand_grid()`: - ! `.vary` must be one of "slowest" or "fastest", not "invalid". - diff --git a/tests/testthat/test-expand.R b/tests/testthat/test-expand.R index 3b9a93e8..0601a53f 100644 --- a/tests/testthat/test-expand.R +++ b/tests/testthat/test-expand.R @@ -390,6 +390,32 @@ test_that("expand_grid() works with 0 row tibbles", { expect_identical(expand_grid(df, x = 1:2), tibble(x = integer())) }) +test_that("expand_grid() respects `.vary` parameter", { + # Slowest + expect_identical( + expand_grid(x = 1:2, y = 1:2), + tibble( + x = c(1L, 1L, 2L, 2L), + y = c(1L, 2L, 1L, 2L) + ) + ) + + # Fastest + expect_identical( + expand_grid(x = 1:2, y = 1:2, .vary = "fastest"), + tibble( + x = c(1L, 2L, 1L, 2L), + y = c(1L, 1L, 2L, 2L) + ) + ) +}) + +test_that("expand_grid() throws an error for invalid `.vary` parameter", { + expect_snapshot(error = TRUE, { + expand_grid(x = 1:2, y = 1:2, .vary = "invalid") + }) +}) + # ------------------------------------------------------------------------------ # grid_dots() @@ -451,103 +477,3 @@ test_that("fct_unique() doesn't alter level order if `NA` is an existing level", expect_identical(fct_unique(x), x) expect_identical(levels(fct_unique(x)), c(NA, "x")) }) - - -# ------------------------------------------------------------------------------ -# .vary parameter - -test_that("expand_grid() respects .vary parameter", { - # Test default behavior (slowest varying) - out_default <- expand_grid(x = 1:3, y = 1:2) - expect_equal( - out_default, - tibble( - x = c(1, 1, 2, 2, 3, 3), - y = c(1, 2, 1, 2, 1, 2) - ) - ) - - # Test fastest varying - out_fastest <- expand_grid(x = 1:3, y = 1:2, .vary = "fastest") - expect_equal( - out_fastest, - tibble( - x = c(1, 2, 3, 1, 2, 3), - y = c(1, 1, 1, 2, 2, 2) - ) - ) -}) - -test_that("expand_grid() .vary parameter works with more than two variables", { - out_slowest <- expand_grid(x = 1:2, y = 1:2, z = 1:2) - expect_equal( - out_slowest, - tibble( - x = c(1, 1, 1, 1, 2, 2, 2, 2), - y = c(1, 1, 2, 2, 1, 1, 2, 2), - z = c(1, 2, 1, 2, 1, 2, 1, 2) - ) - ) - - out_fastest <- expand_grid(x = 1:2, y = 1:2, z = 1:2, .vary = "fastest") - expect_equal( - out_fastest, - tibble( - x = c(1, 2, 1, 2, 1, 2, 1, 2), - y = c(1, 1, 2, 2, 1, 1, 2, 2), - z = c(1, 1, 1, 1, 2, 2, 2, 2) - ) - ) -}) - -test_that("expand_grid() .vary parameter works with different input types", { - out_slowest <- expand_grid(x = 1:2, y = c("a", "b"), z = factor(c("low", "high"))) - expect_equal( - out_slowest, - tibble( - x = c(1, 1, 1, 1, 2, 2, 2, 2), - y = c("a", "a", "b", "b", "a", "a", "b", "b"), - z = factor(c("low", "high", "low", "high", "low", "high", "low", "high")) - ) - ) - - out_fastest <- expand_grid(x = 1:2, y = c("a", "b"), z = factor(c("low", "high")), .vary = "fastest") - expect_equal( - out_fastest, - tibble( - x = c(1, 2, 1, 2, 1, 2, 1, 2), - y = c("a", "a", "b", "b", "a", "a", "b", "b"), - z = factor(c("low", "low", "low", "low", "high", "high", "high", "high")) - ) - ) -}) - -test_that("expand_grid() .vary parameter works with data frames", { - df <- tibble(a = 1:2, b = c("x", "y")) - out_slowest <- expand_grid(df, z = 1:2) - expect_equal( - out_slowest, - tibble( - a = c(1, 1, 2, 2), - b = c("x", "x", "y", "y"), - z = c(1, 2, 1, 2) - ) - ) - - out_fastest <- expand_grid(df, z = 1:2, .vary = "fastest") - expect_equal( - out_fastest, - tibble( - a = c(1, 2, 1, 2), - b = c("x", "y", "x", "y"), - z = c(1, 1, 2, 2) - ) - ) -}) - -test_that("expand_grid() throws an error for invalid .vary parameter", { - expect_snapshot( - expand_grid(x = 1:2, y = 1:2, .vary = "invalid"), - error = TRUE - ) -})