1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
## -----------------------------------------------------------------------------
library(testthat)
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
# Pretend we're snapshotting
snapper <- local_snapshotter(fail_on_new = FALSE)
snapper$start_file("snapshotting.Rmd", "test")
# Pretend we're testing testthat so we can use mocking
Sys.setenv(TESTTHAT_PKG = "testthat")
## -----------------------------------------------------------------------------
check_installed <- function(pkg, min_version = NULL) {
if (!requireNamespace(pkg, quietly = TRUE)) {
stop(sprintf("{%s} is not installed.", pkg))
}
if (!is.null(min_version)) {
pkg_version <- packageVersion(pkg)
if (pkg_version < min_version) {
stop(sprintf(
"{%s} version %s is installed, but %s is required.",
pkg,
pkg_version,
min_version
))
}
}
invisible()
}
## -----------------------------------------------------------------------------
test_that("check_installed() checks package is installed", {
expect_no_error(check_installed("testthat"))
expect_snapshot(check_installed("doesntexist"), error = TRUE)
})
## -----------------------------------------------------------------------------
test_that("check_installed() checks minimum version", {
expect_no_error(check_installed("testthat", "1.0.0"))
expect_snapshot(check_installed("testthat", "99.99.999"), error = TRUE)
})
## -----------------------------------------------------------------------------
test_that("check_installed() checks minimum version", {
expect_no_error(check_installed("testthat", "1.0.0"))
expect_snapshot(
check_installed("testthat", "99.99.999"),
error = TRUE,
transform = function(lines) gsub(packageVersion("testthat"), "<version>", lines)
)
})
## -----------------------------------------------------------------------------
requireNamespace <- NULL
packageVersion <- NULL
## -----------------------------------------------------------------------------
test_that("check_installed() checks package is installed", {
local_mocked_bindings(requireNamespace = function(...) TRUE)
expect_no_error(check_installed("package-name"))
local_mocked_bindings(requireNamespace = function(...) FALSE)
expect_snapshot(check_installed("package-name"), error = TRUE)
})
## -----------------------------------------------------------------------------
test_that("check_installed() checks minimum version", {
local_mocked_bindings(
requireNamespace = function(...) TRUE,
packageVersion = function(...) numeric_version("2.0.0")
)
expect_no_error(check_installed("package-name", "1.0.0"))
expect_snapshot(check_installed("package-name", "3.4.5"), error = TRUE)
})
## -----------------------------------------------------------------------------
system_os <- NULL
## -----------------------------------------------------------------------------
# test_that("can skip on multiple oses", {
# local_mocked_bindings(system_os = function() "windows")
#
# expect_skip(skip_on_os("windows"))
# expect_skip(skip_on_os(c("windows", "linux")))
# expect_no_skip(skip_on_os("linux"))
# })
## -----------------------------------------------------------------------------
# local_mocked_bindings(
# get_revdeps = function() character(),
# gh_milestone_number = function(...) NA
# )
## -----------------------------------------------------------------------------
unix_time <- function() unclass(Sys.time())
unix_time()
## -----------------------------------------------------------------------------
elapsed <- function() {
start <- unix_time()
function() {
unix_time() - start
}
}
timer <- elapsed()
Sys.sleep(0.5)
timer()
## -----------------------------------------------------------------------------
test_that("elapsed() measures elapsed time", {
time <- 1
local_mocked_bindings(unix_time = function() time)
timer <- elapsed()
expect_equal(timer(), 0)
time <- 2
expect_equal(timer(), 1)
})
## -----------------------------------------------------------------------------
# old <- getFromNamespace("my_function", "mypackage")
# assignInNamespace("my_function", new, "mypackage")
#
# # run the test...
#
# # restore the previous value
# assignInNamespace("my_function", old, "mypackage")
|