-
Notifications
You must be signed in to change notification settings - Fork 339
Provide mocking for S3/S4 methods and R6 classes #2197
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7027914
Copy in basic implementation from issues
hadley 341417a
Move S4 classes/methods into R code
hadley 90864b1
More tests + docs
hadley 493244d
Merged origin/main into oo-mocking
hadley 71ccd92
Update reference index
hadley ded7f9f
Merged origin/main into oo-mocking
hadley 899e239
Apply suggestions from code review
hadley 122aa2e
Remove unused code
hadley 7633690
Re-document
hadley 39a9a94
Fix typo
hadley 97066ea
Merged origin/main into oo-mocking
hadley 26d7078
Feedback for claude
hadley 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 hidden or 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 hidden or 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 hidden or 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,131 @@ | ||
| #' Mock S3 and S4 methods | ||
| #' | ||
| #' These functions allow you to temporarily override S3 and S4 methods that | ||
| #' already exist. It works by using [registerS3method()]/[setMethod()] to | ||
| #' temporarily replace the original definition. | ||
| #' | ||
| #' @param generic A string giving the name of the generic. | ||
| #' @param signature A character vector giving the signature of the method. | ||
| #' @param definition A function providing the method definition. | ||
| #' @param frame Calling frame which determines the scope of the mock. | ||
| #' Only needed when wrapping in another local helper. | ||
| #' @export | ||
| #' @examples | ||
| #' x <- as.POSIXlt(Sys.time()) | ||
| #' local({ | ||
| #' local_mocked_s3_method("length", "POSIXlt", function(x) 42) | ||
| #' length(x) | ||
| #' }) | ||
| #' | ||
| #' length(x) | ||
| local_mocked_s3_method <- function( | ||
| generic, | ||
| signature, | ||
| definition, | ||
| frame = caller_env() | ||
| ) { | ||
| check_string(generic) | ||
| check_string(signature) | ||
| check_function(definition) | ||
|
|
||
| old <- getS3method(generic, signature, optional = TRUE) | ||
| if (is.null(old)) { | ||
| cli::cli_abort( | ||
| "Can't find existing S3 method {.code {generic}.{signature}()}." | ||
| ) | ||
| } | ||
| registerS3method(generic, signature, definition, envir = frame) | ||
| withr::defer(registerS3method(generic, signature, old, envir = frame), frame) | ||
| } | ||
|
|
||
| #' @rdname local_mocked_s3_method | ||
| #' @export | ||
| local_mocked_s4_method <- function( | ||
| generic, | ||
| signature, | ||
| definition, | ||
| frame = caller_env() | ||
| ) { | ||
| check_string(generic) | ||
| check_character(signature) | ||
| check_function(definition) | ||
|
|
||
| old <- getMethod(generic, signature, optional = TRUE) | ||
| if (is.null(old)) { | ||
| name <- paste0(generic, "(", paste0(signature, collapse = ","), ")") | ||
| cli::cli_abort( | ||
| "Can't find existing S4 method {.code {name}}." | ||
| ) | ||
| } | ||
| setMethod(generic, signature, definition, where = topenv(frame)) | ||
| withr::defer(setMethod(generic, signature, old, where = topenv(frame)), frame) | ||
| } | ||
|
|
||
|
|
||
| #' Mock an R6 class | ||
| #' | ||
| #' This function allows you to temporarily override an R6 class definition. | ||
| #' It works by creating a subclass then using [local_mocked_bindings()] to | ||
| #' temporarily replace the original definition. This means that it will not | ||
| #' affect subclasses of the original class; please file an issue if you need | ||
| #' this. | ||
| #' | ||
| #' @export | ||
| #' @param class An R6 class definition. | ||
| #' @param public,private A named list of public and private methods/data. | ||
| #' @inheritParams local_mocked_s3_method | ||
| local_mocked_r6_class <- function( | ||
| class, | ||
| public = list(), | ||
| private = list(), | ||
| frame = caller_env() | ||
| ) { | ||
| if (!inherits(class, "R6ClassGenerator")) { | ||
| stop_input_type(class, "an R6 class definition") | ||
| } | ||
| if (!is.list(public)) { | ||
| stop_input_type(public, "a list") | ||
| } | ||
| if (!is.list(private)) { | ||
| stop_input_type(private, "a list") | ||
| } | ||
|
|
||
| mocked_class <- mock_r6_class(class, public, private) | ||
| local_mocked_bindings("{class$classname}" := mocked_class, .env = frame) | ||
| } | ||
|
|
||
| mock_r6_class <- function(class, public = list(), private = list()) { | ||
| R6::R6Class( | ||
| paste0("Mocked", class$classname), | ||
| inherit = class, | ||
| private = private, | ||
| public = public | ||
| ) | ||
| } | ||
|
|
||
| # For testing ------------------------------------------------------------------ | ||
|
|
||
| TestMockClass <- R6::R6Class( | ||
| "TestMockClass", | ||
| public = list( | ||
| sum = function() { | ||
| self$public_fun() + | ||
| self$public_val + | ||
| private$private_fun() + | ||
| private$private_val | ||
| }, | ||
| public_fun = function() 1, | ||
| public_val = 20 | ||
| ), | ||
| private = list( | ||
| private_fun = function() 300, | ||
| private_val = 4000 | ||
| ) | ||
| ) | ||
|
|
||
| TestMockPerson <- methods::setClass( | ||
| "TestMockPerson", | ||
| slots = c(name = "character", age = "numeric") | ||
| ) | ||
| methods::setGeneric("mock_age", function(x) standardGeneric("mock_age")) | ||
| methods::setMethod("mock_age", "TestMockPerson", function(x) x@age) | ||
This file contains hidden or 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 hidden or 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 hidden or 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,64 @@ | ||
| # validates its inputs | ||
|
|
||
| Code | ||
| local_mocked_s3_method(1) | ||
| Condition | ||
| Error in `local_mocked_s3_method()`: | ||
| ! `generic` must be a single string, not the number 1. | ||
| Code | ||
| local_mocked_s3_method("mean", 1) | ||
| Condition | ||
| Error in `local_mocked_s3_method()`: | ||
| ! `signature` must be a single string, not the number 1. | ||
| Code | ||
| local_mocked_s3_method("mean", "bar", 1) | ||
| Condition | ||
| Error in `local_mocked_s3_method()`: | ||
| ! `definition` must be a function, not the number 1. | ||
| Code | ||
| local_mocked_s3_method("mean", "bar", function() { }) | ||
| Condition | ||
| Error in `local_mocked_s3_method()`: | ||
| ! Can't find existing S3 method `mean.bar()`. | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| local_mocked_s4_method(1) | ||
| Condition | ||
| Error in `local_mocked_s4_method()`: | ||
| ! `generic` must be a single string, not the number 1. | ||
| Code | ||
| local_mocked_s4_method("mean", 1) | ||
| Condition | ||
| Error in `local_mocked_s4_method()`: | ||
| ! `signature` must be a character vector, not the number 1. | ||
| Code | ||
| local_mocked_s4_method("mean", "bar", 1) | ||
| Condition | ||
| Error in `local_mocked_s4_method()`: | ||
| ! `definition` must be a function, not the number 1. | ||
| Code | ||
| local_mocked_s4_method("mean", "bar", function() { }) | ||
| Condition | ||
| Error in `local_mocked_s4_method()`: | ||
| ! Can't find existing S4 method `mean(bar)`. | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| local_mocked_r6_class(mean) | ||
| Condition | ||
| Error in `local_mocked_r6_class()`: | ||
| ! `class` must be an R6 class definition, not a function. | ||
| Code | ||
| local_mocked_r6_class(TestMockClass, public = 1) | ||
| Condition | ||
| Error in `local_mocked_r6_class()`: | ||
| ! `public` must be a list, not the number 1. | ||
| Code | ||
| local_mocked_r6_class(TestMockClass, private = 1) | ||
| Condition | ||
| Error in `local_mocked_r6_class()`: | ||
| ! `private` must be a list, not the number 1. | ||
|
|
This file contains hidden or 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,75 @@ | ||
| # S3 -------------------------------------------------------------------------- | ||
|
|
||
| test_that("can mock S3 methods", { | ||
| x <- as.POSIXlt(Sys.time()) | ||
|
|
||
| local({ | ||
| local_mocked_s3_method("length", "POSIXlt", function(x) 42) | ||
| expect_length(x, 42) | ||
| }) | ||
|
|
||
| expect_length(x, 1) | ||
| }) | ||
|
|
||
| test_that("validates its inputs", { | ||
| expect_snapshot(error = TRUE, { | ||
| local_mocked_s3_method(1) | ||
| local_mocked_s3_method("mean", 1) | ||
| local_mocked_s3_method("mean", "bar", 1) | ||
| local_mocked_s3_method("mean", "bar", function() {}) | ||
| }) | ||
| }) | ||
|
|
||
| # S4 -------------------------------------------------------------------------- | ||
|
|
||
| test_that("can mock S4 methods", { | ||
| jim <- TestMockPerson(name = "Jim", age = 32) | ||
|
|
||
| local({ | ||
| local_mocked_s4_method("mock_age", "TestMockPerson", function(x) 42) | ||
| expect_equal(mock_age(jim), 42) | ||
| }) | ||
|
|
||
| expect_equal(mock_age(jim), 32) | ||
| }) | ||
|
|
||
|
|
||
| test_that("validates its inputs", { | ||
| expect_snapshot(error = TRUE, { | ||
| local_mocked_s4_method(1) | ||
| local_mocked_s4_method("mean", 1) | ||
| local_mocked_s4_method("mean", "bar", 1) | ||
| local_mocked_s4_method("mean", "bar", function() {}) | ||
| }) | ||
| }) | ||
|
|
||
| # R6 -------------------------------------------------------------------------- | ||
|
|
||
| test_that("can mock R6 methods", { | ||
| local({ | ||
| local_mocked_r6_class(TestMockClass, public = list(sum = function() 2)) | ||
| obj <- TestMockClass$new() | ||
| expect_equal(obj$sum(), 2) | ||
| }) | ||
|
|
||
| obj <- TestMockClass$new() | ||
| expect_equal(obj$sum(), 4321) | ||
| }) | ||
|
|
||
| test_that("can mock all R6 components", { | ||
| local_mocked_r6_class( | ||
| TestMockClass, | ||
| public = list(public_fun = function() 0, public_val = 0), | ||
| private = list(private_fun = function() 0, private_val = 0) | ||
| ) | ||
| obj <- TestMockClass$new() | ||
| expect_equal(obj$sum(), 0) | ||
| }) | ||
|
|
||
| test_that("validates its inputs", { | ||
| expect_snapshot(error = TRUE, { | ||
| local_mocked_r6_class(mean) | ||
| local_mocked_r6_class(TestMockClass, public = 1) | ||
| local_mocked_r6_class(TestMockClass, private = 1) | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.