diff --git a/tests/testthat/test-configurations.R b/tests/testthat/test-configurations.R index dce9ccd..6a1f37d 100644 --- a/tests/testthat/test-configurations.R +++ b/tests/testthat/test-configurations.R @@ -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() diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 6439d1f..6379b45 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -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