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
|
library(testthat)
library(logging)
context("Testing formatting of mess and exprs [test.automatic-format]")
test_setup <- function() {
test_env <- new.env(parent = emptyenv())
test_env$logged <- NULL
mock_action <- function(msg, handler, ...) {
if (length(list(...)) && "dry" %in% names(list(...)))
return(TRUE)
test_env$logged <- c(test_env$logged, msg)
}
mock_formatter <- function(record) {
paste(record$levelname, record$logger, record$msg, sep = ":")
}
logReset()
addHandler(mock_action,
level = "DEBUG",
formatter = mock_formatter)
return(test_env)
}
## test functions
test_that("autoformat/one_numeric_literal", {
env <- test_setup()
loginfo(12)
expect_equal(env$logged, "INFO::12: 12")
})
test_that("autoformat/more_numeric_literals", {
env <- test_setup()
loginfo(12, 1 + 1, 2 * 2)
expect_equal(env$logged,
c("INFO::12: 12", "INFO::1 + 1: 2", "INFO::2 * 2: 4"))
})
test_that("autoformat/one_numeric_variable", {
env <- test_setup()
a <- 1
loginfo(a)
expect_equal(env$logged, "INFO::a: 1")
})
test_that("autoformat/one_numeric_expression", {
env <- test_setup()
loginfo(3 + 5)
expect_equal(env$logged, "INFO::3 + 5: 8")
})
test_that("autoformat/explicit_msg_parameter", {
env <- test_setup()
loginfo(logger = getLogger(), msg = 3 + 5)
expect_equal(env$logged, "INFO::3 + 5: 8")
})
test_that("autoformat/shifted_msg_parameter", {
env <- test_setup()
loginfo(logger = getLogger(), 3 + 5)
expect_equal(env$logged, "INFO::3 + 5: 8")
})
test_that("autoformat/levellog", {
env <- test_setup()
levellog("INFO", 3 + 5)
expect_equal(env$logged, "INFO::3 + 5: 8")
})
|