Skip to content

Commit

Permalink
Merge pull request #7 from rossellhayes/feat/check_always
Browse files Browse the repository at this point in the history
Add `check_always()` function to start checking all output automatically
  • Loading branch information
Paulj1989 authored Feb 13, 2024
2 parents bb89fc9 + a419b24 commit 8380795
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(check)
export(check_always)
export(check_df)
export(uncheck_always)
11 changes: 10 additions & 1 deletion R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
#' x <- c("ten", "twenty", "thirty", "sixty nine")
#' check(x)
check <- function(x) {
if (any(x %in% c(69, 0.69, 0.069, 0.0069, "Sixty Nine", "sixty nine"))) {
if (is_nice(x)) {
print("Nice!")
} else {
print("Not very nice.")
}
}

is_nice <- function(x) {
isTRUE(
try(
any(x %in% c(69, 0.69, 0.069, 0.0069, "Sixty Nine", "sixty nine")),
silent = TRUE
)
)
}
50 changes: 50 additions & 0 deletions R/check_always.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
nice_callback <- function(expr, value, ok, visible) {
if (visible && is_nice(value)) {
message("Nice!")
}

return(TRUE)
}

#' Always check whether R output is really nice
#'
#' `check_always()` creates a [task callback function][addTaskCallback()]
#' to check whether all R output is nice.
#' `uncheck_always()` disables the task callback function,
#' returning your R session to normal behavior.
#'
#' @param verbose If `TRUE`, prints a message when `check_always()` and
#' `uncheck_always()` are run.
#' Defaults to `TRUE`.
#'
#' @keywords nice
#'
#' @export
#'
#' @examples
#' check_always()
#' 23 * 3
#'
#' uncheck_always()
#' 23 * 3
check_always <- function(verbose = TRUE) {
if ("nice_callback" %in% getTaskCallbackNames()) {
if (isTRUE(verbose)) message("Already checking whether all output is nice")
return(invisible())
}

addTaskCallback(nice_callback, name = "nice_callback")
if (isTRUE(verbose)) message("Now checking whether all output is nice")
}

#' @rdname check_always
#' @export
uncheck_always <- function(verbose = TRUE) {
if (!"nice_callback" %in% getTaskCallbackNames()) {
if (isTRUE(verbose)) message("Not checking whether all output is nice")
return(invisible())
}

removeTaskCallback("nice_callback")
if (isTRUE(verbose)) message("No longer checking whether all output is nice")
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ reference:
contents:
- check
- check_df
- check_always
30 changes: 30 additions & 0 deletions man/check_always.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/test-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ test_that("check functions returns correct negative responses", {
expect_equal(check(0.420), "Not very nice.")
expect_equal(check(420), "Not very nice.")
expect_equal(check("four twenty"), "Not very nice.")
expect_equal(check(check), "Not very nice.")
})
70 changes: 70 additions & 0 deletions tests/testthat/test-check_always.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
test_that("check_always() adds callback", {
expect_message(
check_always(),
"Now checking whether all output is nice"
)

expect_true("nice_callback" %in% getTaskCallbackNames())
})

test_that("uncheck_always() removes callback", {
expect_message(check_always())

expect_message(
uncheck_always(),
"No longer checking whether all output is nice"
)

expect_false("nice_callback" %in% getTaskCallbackNames())
})

test_that("verbose argument controls messages", {
expect_silent(check_always(verbose = FALSE))
expect_true("nice_callback" %in% getTaskCallbackNames())

expect_silent(uncheck_always(verbose = FALSE))
expect_false("nice_callback" %in% getTaskCallbackNames())
})

test_that("check_always() doesn't add duplicate callback tasks", {
check_always(verbose = FALSE)

callback_list <- getTaskCallbackNames()

expect_message(
check_always(),
"Already checking whether all output is nice"
)

expect_identical(getTaskCallbackNames(), callback_list)
})

test_that("uncheck_always() does nothing if there is no task callback", {
uncheck_always(verbose = FALSE)

callback_list <- getTaskCallbackNames()

expect_message(
uncheck_always(),
"Not checking whether all output is nice"
)

expect_identical(getTaskCallbackNames(), callback_list)
})

test_that("nice_callback() gives message for nice values", {
expect_message(
nice_callback(quote(69), 69, TRUE, TRUE),
"Nice!"
)

expect_silent(
nice_callback(quote(420), 420, TRUE, TRUE)
)
})

test_that("nice_callback() doesn't give message for invisible values", {
expect_silent(
nice_callback(quote(invisible(69)), 69, TRUE, FALSE)
)
})

0 comments on commit 8380795

Please sign in to comment.