| 1 |
context("Filter") |
| 2 |
|
| 3 |
|
| 4 |
test_that("FilterForceLevel and FilterInject work as expected", { |
| 5 |
|
| 6 |
l <- Logger$new("foo") |
| 7 |
|
| 8 |
l$add_filter(FilterForceLevel$new("fatal")) |
| 9 |
l$add_filter(FilterInject$new(iris = iris, .list = list(cars = "foo"))) |
| 10 |
expect_output(l$info("blubb"), "FATAL.*cars.*data\\.frame") |
| 11 |
l$remove_filter(1) |
| 12 |
expect_output(l$info("blubb"), "INFO.*cars.*data\\.frame") |
| 13 |
l$remove_filter(1) |
| 14 |
expect_output(l$info("test"), "test\\s*$") |
| 15 |
}) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
test_that(".obj() works as expected", { |
| 20 |
l <- Logger$new("foo") |
| 21 |
|
| 22 |
f <- function(event) { |
| 23 |
cat(class_fmt(.obj())) |
| 24 |
TRUE |
| 25 |
} |
| 26 |
|
| 27 |
l$add_filter(f) |
| 28 |
|
| 29 |
expect_output(l$fatal("test"), "Logger/Filterable/R6") |
| 30 |
l$remove_filter(1) |
| 31 |
l$add_filter(EventFilter$new(f)) |
| 32 |
expect_output(l$fatal("test"), "Logger/Filterable/R6") |
| 33 |
}) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
test_that("FilterForceLevel and FilterInject work inside function", { |
| 38 |
lg <- get_logger("test") |
| 39 |
lg$set_threshold(NA) |
| 40 |
lg$set_propagate(FALSE) |
| 41 |
lg$add_appender(AppenderConsole$new(threshold = NA)) |
| 42 |
|
| 43 |
analyse <- function(){ |
| 44 |
lg$add_filter(FilterForceLevel$new("info"), "force") |
| 45 |
lg$add_filter(FilterInject$new(type = "analysis"), "inject") |
| 46 |
on.exit(lg$remove_filter(c("force", "inject"))) |
| 47 |
lg$debug("a debug message") |
| 48 |
lg$error("an error") |
| 49 |
} |
| 50 |
expect_output(analyse(), "INFO.*debug.*type.*INFO.*error.*type.*") |
| 51 |
expect_length(lgr$filters, 0) |
| 52 |
lg$config(logger_config()) |
| 53 |
}) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
test_that("standardize_filters_list works as expected", { |
| 59 |
f1 <- function(event) TRUE |
| 60 |
expect_identical(standardize_filters_list(f1), list(f1)) |
| 61 |
expect_identical( |
| 62 |
standardize_filters_list(list(f1, f1)), |
| 63 |
list(f1, f1) |
| 64 |
) |
| 65 |
expect_error( |
| 66 |
standardize_filters_list(list(f1, mean)), |
| 67 |
class = "ObjectIsNoFilterError" |
| 68 |
) |
| 69 |
}) |