Skip to content

Commit 328e549

Browse files
committed
use rlang's dynamic dots
1 parent f20a031 commit 328e549

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

R/mock2-helpers.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' Specify multiple return values for mocking
44
#'
5-
#' @param values vector of values to return in sequence.
5+
#' @param ... <[`dynamic-dots`][rlang::dyn-dots]> Values to return in sequence.
66
#' @param recycle whether to recycle. If `TRUE`, once all values have been returned,
77
#' they will be returned again in sequence.
88
#'
@@ -13,31 +13,31 @@
1313
#' @examples
1414
#' # inside local_mocked_bindings()
1515
#' \dontrun{
16-
#' local_mocked_bindings(readline = mock_output_sequence(c("3", "This is a note", "n")))
16+
#' local_mocked_bindings(readline = mock_output_sequence("3", "This is a note", "n"))
1717
#' }
1818
#' # for understanding
19-
#' mocked_sequence <- mock_output_sequence(c("3", "This is a note", "n"))
19+
#' mocked_sequence <- mock_output_sequence("3", "This is a note", "n")
2020
#' mocked_sequence()
2121
#' mocked_sequence()
2222
#' mocked_sequence()
2323
#' try(mocked_sequence())
2424
#' recycled_mocked_sequence <- mock_output_sequence(
25-
#' c("3", "This is a note", "n"),
25+
#' "3", "This is a note", "n",
2626
#' recycle = TRUE
2727
#' )
2828
#' recycled_mocked_sequence()
2929
#' recycled_mocked_sequence()
3030
#' recycled_mocked_sequence()
3131
#' recycled_mocked_sequence()
3232
#' @family mocking
33-
mock_output_sequence <- function(values, recycle = FALSE) {
34-
force(values)
33+
mock_output_sequence <- function(..., recycle = FALSE) {
34+
values <- rlang::list2(...)
3535
i <- 1
3636
function(...) {
3737
if (i > length(values) && !recycle) {
3838
cli::cli_abort(c(
3939
"Can't find value for {i}th iteration.",
40-
i = "{.arg values} has only {length(values)} values.",
40+
i = "{.arg ...} has only {length(values)} values.",
4141
i = "You can set {.arg recycle} to {.code TRUE}."
4242
))
4343
}

man/mock_output_sequence.Rd

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/mock2-helpers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
Condition
66
Error in `mocked_sequence()`:
77
! Can't find value for 4th iteration.
8-
i `values` has only 3 values.
8+
i `...` has only 3 values.
99
i You can set `recycle` to `TRUE`.
1010

tests/testthat/test-mock2-helpers.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
test_that("mock_output_sequence() works", {
2-
mocked_sequence <- mock_output_sequence(c("3", "This is a note", "n"))
2+
mocked_sequence <- mock_output_sequence("3", "This is a note", "n")
33
expect_equal(mocked_sequence(), "3")
44
expect_equal(mocked_sequence(), "This is a note")
55
expect_equal(mocked_sequence(), "n")
66
expect_snapshot(mocked_sequence(), error = TRUE)
77
})
88

9+
test_that("mock_output_sequence() works -- force", {
10+
a <- "3"
11+
b <- "This is a note"
12+
c <- "n"
13+
mocked_sequence <- mock_output_sequence(a, b, c)
14+
a <- "not 3"
15+
expect_equal(mocked_sequence(), "3")
16+
expect_equal(mocked_sequence(), "This is a note")
17+
expect_equal(mocked_sequence(), "n")
18+
})
19+
20+
test_that("mock_output_sequence() works -- list", {
21+
x <- list("3", "This is a note", "n")
22+
mocked_sequence <- mock_output_sequence(!!!x)
23+
expect_equal(mocked_sequence(), "3")
24+
expect_equal(mocked_sequence(), "This is a note")
25+
expect_equal(mocked_sequence(), "n")
26+
})
27+
928
test_that("mock_output_sequence()'s recycling works", {
1029
mocked_sequence <- mock_output_sequence(
11-
c("3", "This is a note", "n"),
30+
"3", "This is a note", "n",
1231
recycle = TRUE
1332
)
1433
expect_equal(mocked_sequence(), "3")

0 commit comments

Comments
 (0)