-
Notifications
You must be signed in to change notification settings - Fork 323
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
feat: mock_output_sequence()
similar to {mockery}'s multiple return values
#2061
Merged
+165
−2
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b321b22
feat: `mock_output_sequence()` similar to {mockery}'s multiple return…
maelle d2e4869
docs: add to pkgdown index
maelle 6f61047
docs: safeguard example
maelle 48b44af
test: add cases
maelle f20a031
refactor: use modulo
maelle 328e549
use rlang's dynamic dots
maelle 5982f64
update docs
maelle b95749a
rm useless test
maelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#' Mock a sequence of output from a function | ||
#' | ||
#' Specify multiple return values for mocking | ||
#' | ||
#' @param values vector of 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. | ||
#' | ||
#' @return A function that you can use within `local_mocked_bindings()` and | ||
#' `with_mocked_bindings()` | ||
#' @export | ||
#' | ||
#' @examples | ||
#' # inside local_mocked_bindings() | ||
#' \dontrun{ | ||
#' local_mocked_bindings(readline = mock_output_sequence(c("3", "This is a note", "n"))) | ||
#' } | ||
#' # for understanding | ||
#' mocked_sequence <- mock_output_sequence(c("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"), | ||
#' 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) | ||
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 = "You can set {.arg recycle} to {.code TRUE}." | ||
)) | ||
} | ||
index <- (i - 1) %% length(values) + 1 | ||
value <- rep_len(values, length.out = index)[[index]] | ||
i <<- i + 1 | ||
value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# mock_output_sequence() works | ||
|
||
Code | ||
mocked_sequence() | ||
Condition | ||
Error in `mocked_sequence()`: | ||
! Can't find value for 4th iteration. | ||
i `values` has only 3 values. | ||
i You can set `recycle` to `TRUE`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
test_that("mock_output_sequence() works", { | ||
mocked_sequence <- mock_output_sequence(c("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()'s recycling works", { | ||
mocked_sequence <- mock_output_sequence( | ||
c("3", "This is a note", "n"), | ||
recycle = TRUE | ||
) | ||
expect_equal(mocked_sequence(), "3") | ||
expect_equal(mocked_sequence(), "This is a note") | ||
expect_equal(mocked_sequence(), "n") | ||
expect_equal(mocked_sequence(), "3") | ||
expect_equal(mocked_sequence(), "This is a note") | ||
expect_equal(mocked_sequence(), "n") | ||
}) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use
...
instead ofvalues
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be sure I understand: do you mean that instead of writing
mock_output_sequence(c("blop", "bla"))
the user would writemock_output_sequence("blop", "bla")
?And using rlang's "Dynamic dots features"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and yes 😄