Skip to content

Commit

Permalink
use rlang's dynamic dots
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Feb 28, 2025
1 parent f20a031 commit 328e549
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
14 changes: 7 additions & 7 deletions R/mock2-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Specify multiple return values for mocking
#'
#' @param values vector of values to return in sequence.
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Values to return in sequence.
#' @param recycle whether to recycle. If `TRUE`, once all values have been returned,
#' they will be returned again in sequence.
#'
Expand All @@ -13,31 +13,31 @@
#' @examples
#' # inside local_mocked_bindings()
#' \dontrun{
#' local_mocked_bindings(readline = mock_output_sequence(c("3", "This is a note", "n")))
#' local_mocked_bindings(readline = mock_output_sequence("3", "This is a note", "n"))
#' }
#' # for understanding
#' mocked_sequence <- mock_output_sequence(c("3", "This is a note", "n"))
#' mocked_sequence <- mock_output_sequence("3", "This is a note", "n")
#' mocked_sequence()
#' mocked_sequence()
#' mocked_sequence()
#' try(mocked_sequence())
#' recycled_mocked_sequence <- mock_output_sequence(
#' c("3", "This is a note", "n"),
#' "3", "This is a note", "n",
#' recycle = TRUE
#' )
#' recycled_mocked_sequence()
#' recycled_mocked_sequence()
#' recycled_mocked_sequence()
#' recycled_mocked_sequence()
#' @family mocking
mock_output_sequence <- function(values, recycle = FALSE) {
force(values)
mock_output_sequence <- function(..., recycle = FALSE) {
values <- rlang::list2(...)
i <- 1
function(...) {
if (i > length(values) && !recycle) {
cli::cli_abort(c(
"Can't find value for {i}th iteration.",
i = "{.arg values} has only {length(values)} values.",
i = "{.arg ...} has only {length(values)} values.",
i = "You can set {.arg recycle} to {.code TRUE}."
))
}
Expand Down
10 changes: 5 additions & 5 deletions man/mock_output_sequence.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/mock2-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
Condition
Error in `mocked_sequence()`:
! Can't find value for 4th iteration.
i `values` has only 3 values.
i `...` has only 3 values.
i You can set `recycle` to `TRUE`.

23 changes: 21 additions & 2 deletions tests/testthat/test-mock2-helpers.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
test_that("mock_output_sequence() works", {
mocked_sequence <- mock_output_sequence(c("3", "This is a note", "n"))
mocked_sequence <- mock_output_sequence("3", "This is a note", "n")
expect_equal(mocked_sequence(), "3")
expect_equal(mocked_sequence(), "This is a note")
expect_equal(mocked_sequence(), "n")
expect_snapshot(mocked_sequence(), error = TRUE)
})

test_that("mock_output_sequence() works -- force", {
a <- "3"
b <- "This is a note"
c <- "n"
mocked_sequence <- mock_output_sequence(a, b, c)
a <- "not 3"
expect_equal(mocked_sequence(), "3")
expect_equal(mocked_sequence(), "This is a note")
expect_equal(mocked_sequence(), "n")
})

test_that("mock_output_sequence() works -- list", {
x <- list("3", "This is a note", "n")
mocked_sequence <- mock_output_sequence(!!!x)
expect_equal(mocked_sequence(), "3")
expect_equal(mocked_sequence(), "This is a note")
expect_equal(mocked_sequence(), "n")
})

test_that("mock_output_sequence()'s recycling works", {
mocked_sequence <- mock_output_sequence(
c("3", "This is a note", "n"),
"3", "This is a note", "n",
recycle = TRUE
)
expect_equal(mocked_sequence(), "3")
Expand Down

0 comments on commit 328e549

Please sign in to comment.