Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions tests/testthat/test-configurations.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,118 @@ test_that("test call option configuration", {
expect_silent(old <- set_call_options(.arg_list = list(log_call = FALSE, full_stack = FALSE), confirm = FALSE))
expect_identical(old, list(log_call = FALSE, full_stack = TRUE))
})
cleanup()

test_that("test call option configuration (effect on call handlers)", {
old <- set_call_options(log_call = TRUE, full_stack = FALSE, confirm = FALSE)
on.exit(set_call_options(.arg_list = old, confirm = FALSE))
# Test loggit (via function call to garantee a call object)
f <- function() loggit(log_lvl = "DEBUG", log_msg = "Test message with call", echo = FALSE)
g <- function() f()
g()
log_tmp <- read_logs()
expect_true(is.na(log_tmp[["log_call"]]))
cleanup()
# Test debuginfo
f <- function() debuginfo("Test message with call", echo = FALSE)
g <- function() f()
g()
log_tmp <- read_logs()
expect_identical(log_tmp[["log_call"]], "f()")
cleanup()
# Test message
f <- function() message("Test message with call", echo = FALSE)
g <- function() f()
suppressMessages(g())
log_tmp <- read_logs()
# Message uses an alternative call approach
expect_identical(log_tmp[["log_call"]], "message(\"Test message with call\", echo = FALSE)")
cleanup()
# Test warning
f <- function() warning("Test message with call", echo = FALSE)
g <- function() f()
suppressWarnings(g())
log_tmp <- read_logs()
expect_identical(log_tmp[["log_call"]], "f()")
cleanup()
# Test error
f <- function() stop("Test message with call", echo = FALSE)
g <- function() f()
try(g(), silent = TRUE)
log_tmp <- read_logs()
expect_identical(log_tmp[["log_call"]], "f()")
cleanup()
# Test stopifnot
f <- function() stopifnot(FALSE, "Test message with call", echo = FALSE)
g <- function() f()
try(g(), silent = TRUE)
log_tmp <- read_logs()
expect_identical(log_tmp[["log_call"]], "f()")
})
cleanup()

test_that("test call option configuration (effect on call handlers) II", {
old <- set_call_options(log_call = FALSE, confirm = FALSE)
on.exit(set_call_options(.arg_list = old, confirm = FALSE))
# Test loggit (via function call to garantee a call object)
loggit(log_lvl = "DEBUG", log_msg = "Test message with call", echo = FALSE)
debuginfo("Test message with call", echo = FALSE)
suppressMessages(message("Test message with call", echo = FALSE))
suppressWarnings(warning("Test message with call", echo = FALSE))
try(stop("Test message with call", echo = FALSE), silent = TRUE)
try(stopifnot(FALSE, "Test message with call", echo = FALSE), silent = TRUE)
log_tmp <- read_logs()
expect_false("log_call" %in% names(log_tmp))
})
cleanup()


test_that("test call option configuration (effect on call handlers) III", {
old <- set_call_options(log_call = TRUE, full_stack = TRUE, confirm = FALSE)
on.exit(set_call_options(.arg_list = old, confirm = FALSE))
# Test loggit (via function call to garantee a call object)
f <- function() loggit(log_lvl = "DEBUG", log_msg = "Test message with call", echo = FALSE)
g <- function() f()
g()
log_tmp <- read_logs()
expect_true(is.na(log_tmp[["log_call"]]))
cleanup()
# Test debuginfo
f <- function() debuginfo("Test message with call", echo = FALSE)
g <- function() f()
g()
log_tmp <- read_logs()
expect_true(grepl(log_tmp[["log_call"]], pattern = "\ng().*\nf()"))
cleanup()
# Test message
f <- function() message("Test message with call", echo = FALSE)
g <- function() f()
suppressMessages(g())
log_tmp <- read_logs()
# Message uses an alternative call approach
expect_true(grepl(
log_tmp[["log_call"]], pattern = "\ng\\(\\).*\nmessage\\(\"Test message with call\", echo = FALSE\\)"
))
cleanup()
# Test warning
f <- function() warning("Test message with call", echo = FALSE)
g <- function() f()
suppressWarnings(g())
log_tmp <- read_logs()
expect_true(grepl(log_tmp[["log_call"]], pattern = "\ng().*\nf()"))
cleanup()
# Test error
f <- function() stop("Test message with call", echo = FALSE)
g <- function() f()
try(g(), silent = TRUE)
log_tmp <- read_logs()
expect_true(grepl(log_tmp[["log_call"]], pattern = "\ng().*\nf()"))
cleanup()
# Test stopifnot
f <- function() stopifnot(FALSE, "Test message with call", echo = FALSE)
g <- function() f()
try(g(), silent = TRUE)
log_tmp <- read_logs()
expect_true(grepl(log_tmp[["log_call"]], pattern = "\ng().*\nf()"))
})
cleanup()
7 changes: 7 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ test_that("call_2_string (full_stack=TRUE) handels call not in current context",
)
})

test_that("call_2_string (full_stack=FALSE) handels call not in current context", {
ext_call <- quote(some_function(1L, 2L))
expect_true(
endsWith(call_2_string(ext_call, full_stack = FALSE), "some_function(1L, 2L)")
)
})

test_that("test call_2_string (cut off) via condition handler", {
old <- set_call_options(log_call = TRUE, full_stack = TRUE, confirm = FALSE)
# Function is needed to create a predictable call stack and a call to cut off
Expand Down