|
| 1 | +#' Mocking tools |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' `r lifecycle::badge("experimental")` |
| 5 | +#' |
| 6 | +#' These functions represent a second attempt at bringing mocking to testthat, |
| 7 | +#' incorporating what we've learned from the mockr, mockery, and mockthat package. |
| 8 | +#' |
| 9 | +#' `with_mocked_bindings()` and `local_mocked_bindings()` work by temporarily |
| 10 | +#' changing variable bindings in the namespace of namespace `.package`. |
| 11 | +#' Generally, it's only safe to mock packages that you own. If you mock other |
| 12 | +#' packages, we recommend using `skip_on_cran()` to avoid CRAN failures if the |
| 13 | +#' implementation changes. |
| 14 | +#' |
| 15 | +#' These functions do not currently affect registered S3 methods. |
| 16 | +#' |
| 17 | +#' @export |
| 18 | +#' @param ... Name-value pairs providing functions to mock. |
| 19 | +#' @param code Code to execute with specified bindings. |
| 20 | +#' @param .env Environment that defines effect scope. For expert use only. |
| 21 | +#' @param .package The name of the package where mocked functions should be |
| 22 | +#' inserted. Generally, you should not need to supply this as it will be |
| 23 | +#' automatically detected when whole package tests are run or when there's |
| 24 | +#' one package under active development (i.e. loaded with |
| 25 | +#' [pkgload::load_all()]). |
| 26 | +local_mocked_bindings <- function(..., .package = NULL, .env = caller_env()) { |
| 27 | + bindings <- list2(...) |
| 28 | + check_bindings(bindings) |
| 29 | + |
| 30 | + .package <- .package %||% dev_package() |
| 31 | + ns_env <- ns_env(.package) |
| 32 | + |
| 33 | + # Unlock bindings and set values |
| 34 | + nms <- names(bindings) |
| 35 | + locked <- env_binding_unlock(ns_env, nms) |
| 36 | + withr::defer(env_binding_lock(ns_env, nms[locked]), envir = .env) |
| 37 | + local_bindings(!!!bindings, .env = ns_env, .frame = .env) |
| 38 | + |
| 39 | + invisible() |
| 40 | +} |
| 41 | + |
| 42 | +#' @rdname local_mocked_bindings |
| 43 | +#' @export |
| 44 | +with_mocked_bindings <- function(code, ..., .package = NULL) { |
| 45 | + local_mocked_bindings(..., .package = .package) |
| 46 | + code |
| 47 | +} |
| 48 | + |
| 49 | +# helpers ----------------------------------------------------------------- |
| 50 | + |
| 51 | +dev_package <- function() { |
| 52 | + if (is_testing() && testing_package() != "") { |
| 53 | + testing_package() |
| 54 | + } else { |
| 55 | + loaded <- loadedNamespaces() |
| 56 | + is_dev <- map_lgl(loaded, function(x) !is.null(pkgload::dev_meta(x))) |
| 57 | + if (sum(is_dev) == 0) { |
| 58 | + cli::cli_abort("No packages loaded with pkgload") |
| 59 | + } else if (sum(is_dev) == 1) { |
| 60 | + loaded[is_dev] |
| 61 | + } else { |
| 62 | + cli::cli_abort("Multiple packages loaded with pkgload") |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +check_bindings <- function(x, error_call = caller_env()) { |
| 68 | + if (!is_named(x)) { |
| 69 | + cli::cli_abort( |
| 70 | + "All elements of {.arg ...} must be named.", |
| 71 | + call = error_call |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + is_fun <- map_lgl(x, is.function) |
| 76 | + if (!any(is_fun)) { |
| 77 | + cli::cli_abort( |
| 78 | + "All elements of {.arg ...} must be functions.", |
| 79 | + call = error_call |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +# In package |
| 85 | +mockable_generic <- function(x) { |
| 86 | + UseMethod("mockable_generic") |
| 87 | +} |
| 88 | +#' @export |
| 89 | +mockable_generic.integer <- function(x) { |
| 90 | + 1 |
| 91 | +} |
0 commit comments