Skip to content

Commit

Permalink
Remove mockery (#59)
Browse files Browse the repository at this point in the history
* Remove mockery

Fixes #57

I don't love the implementation for package.R. If there's a less-kludgey way to do that, I'd be happy to do so!

* Address review comments.

* Slightly clearer commenting about is_debugged().

* Simplify testing, to aboid changing the package code

* Avoid whitespace changes

* Simpligy tests

---------

Co-authored-by: Gábor Csárdi <csardi.gabor@gmail.com>
  • Loading branch information
jonthegeek and gaborcsardi authored Aug 28, 2024
1 parent b4205eb commit 121258f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
^codecov\.yml$
^LICENSE\.md$
^screencast.gif$
^debugme\.Rproj$
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Imports:
grDevices
Suggests:
covr,
mockery,
R6,
testthat (>= 3.0.0),
withr
Expand Down
18 changes: 18 additions & 0 deletions debugme.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
19 changes: 8 additions & 11 deletions tests/testthat/test-colors.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
test_that("color palette is fine", {

val <- NULL

mockery::stub(initialize_colors, "assign",
function(x, value, envir, ...) val <<- value)
initialize_colors(c("foo", "bar"))
expect_equal(names(val), c("foo", "bar"))
expect_true(all(val %in% grDevices::colors()))
expect_equal(names(debug_data[["palette"]]), c("foo", "bar"))
expect_true(all(debug_data[["palette"]] %in% grDevices::colors()))

initialize_colors(letters)
expect_equal(names(val), letters)
expect_true(all(val %in% c("silver", grDevices::colors())))
expect_equal(names(debug_data[["palette"]]), letters)
expect_true(all(debug_data[["palette"]] %in% c("silver", grDevices::colors())))
})

## Quite an artificial test case...

test_that("get a package style", {

mockery::stub(get_package_style, "is_debugged", function(...) TRUE)
mockery::stub(get_package_style, "make_style", function(x) substitute(x))
local_mocked_bindings(
is_debugged = function(...) TRUE,
make_style = function(x) substitute(x)
)
ret <- get_package_style("pkg")
expect_equal(ret, quote(debug_data$palette[pkg]))
})
6 changes: 4 additions & 2 deletions tests/testthat/test-debug.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ test_that("debug indent", {

test_that("debug levels", {

mockery::stub(debug, "get_package_debug_level", 1)
local_mocked_bindings(
get_package_debug_level = function(...) 1
)
env <- new.env()
env$f1 <- function() debug("foobar", level = 1)
env$f2 <- function() debug("baz", level = 2)
Expand All @@ -68,7 +70,7 @@ test_that("get_timestamp_stamp", {

mytime <- structure(1477967634, class = c("POSIXct", "POSIXt"),
tzone = "UTC")
mockery::stub(get_timestamp_stamp, "Sys.time", mytime)
local_mocked_bindings(Sys.time = function() mytime, .package = "base")
expect_equal(
get_timestamp_stamp(),
"2016-11-01T02:33:54.54.000+00:00 "
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-levels.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test_that("log levels work properly", {
)

for (pkg_level in 1:6) {
mockery::stub(debug, "get_package_debug_level", pkg_level)
local_mocked_bindings(get_package_debug_level = function(...) pkg_level)
for (idx in seq_along(fs)) {
if (idx <= pkg_level) {
expect_output(fs[[idx]][[2]](), fs[[idx]][[1]])
Expand Down
26 changes: 11 additions & 15 deletions tests/testthat/test-package.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
test_that(".onLoad", {

val <- NULL

mockery::stub(refresh_pkg_info, "initialize_colors", function(pkgs) val <<- pkgs)
withr::with_envvar(
c("DEBUGME" = c("foo,bar")),
refresh_pkg_info()
)
expect_identical(val, c("foo", "bar"))
expect_identical(names(debug_data$palette), c("foo", "bar"))
})

test_that("debugme", {
Expand All @@ -18,13 +14,12 @@ test_that("debugme", {
env$notme <- "!DEBUG nonono"
env$.hidden <- function() { "!DEBUG foobar2" }

expect_silent(debugme(env))

mockery::stub(debugme, "is_debugged", TRUE)
debugme(env)
withr::local_envvar(DEBUGME = "debugme")
refresh_pkg_info()
debugme(env, pkg = "debugme")

expect_silent(env$f1())
expect_output(env$f2(), "debugme foobar \\+[0-9]+ms")
expect_output(env$f2(), "debugme foobar ")
expect_identical(env$notme, "!DEBUG nonono")
expect_output(env$.hidden(), "debugme foobar2 \\+[0-9]+ms")
})
Expand All @@ -35,14 +30,14 @@ test_that("instrument environments", {
env$env <- new.env()
env$env$fun <- function() { "!DEBUG coocoo" }

mockery::stub(debugme, "is_debugged", TRUE)
expect_silent(debugme(env))
withr::local_envvar(DEBUGME = "debugme")
refresh_pkg_info()
debugme(env, pkg = "debugme")

expect_output(env$env$fun(), "coocoo")
})

test_that("instrument R6 classes", {

env <- new.env()
env$class <- R6::R6Class(
"foobar",
Expand All @@ -61,8 +56,9 @@ test_that("instrument R6 classes", {
)
)

mockery::stub(debugme, "is_debugged", TRUE)
expect_silent(debugme(env))
withr::local_envvar(DEBUGME = "debugme")
refresh_pkg_info()
debugme(env, pkg = "debugme")

expect_output(x <- env$class$new("mrx"), "debugme.*creating mrx")
expect_output(x$hello(), "debugme.*hello mrx")
Expand Down

0 comments on commit 121258f

Please sign in to comment.