| 1 |
context("simple_logging") |
| 2 |
|
| 3 |
|
| 4 |
test_that("add_log_levels(), get_log_levels() works as expected", { |
| 5 |
ml <- Logger$new("dummy") |
| 6 |
|
| 7 |
expect_error(ml$set_threshold("blubb")) |
| 8 |
expect_silent(add_log_levels(c(blubb = 250, schwupp = 341))) |
| 9 |
expect_silent(ml$set_threshold("blubb")) |
| 10 |
expect_identical(ml$threshold, 250L) |
| 11 |
expect_setequal( |
| 12 |
names(get_log_levels()), |
| 13 |
c("fatal", "error", "warn", "info", "debug", "trace", "blubb", "schwupp") |
| 14 |
) |
| 15 |
|
| 16 |
expect_error( |
| 17 |
add_log_levels(c("blubb", "schwupp")) |
| 18 |
) |
| 19 |
|
| 20 |
expect_message(add_log_levels(c(blubb = 250, schwupp = 341))) |
| 21 |
expect_true(all_are_distinct(get_log_levels())) |
| 22 |
expect_true(all_are_distinct(names(get_log_levels()))) |
| 23 |
|
| 24 |
remove_log_levels(c("blubb", "schwupp")) # cleanup |
| 25 |
expect_error(ml$set_threshold("blubb")) |
| 26 |
}) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
test_that("label_levels() & unlabel_levels() are symetirc", { |
| 32 |
tdat <- sample(c(seq(0, 600, by = 100), NA)) |
| 33 |
|
| 34 |
expect_silent({ |
| 35 |
x <- label_levels(tdat) |
| 36 |
expect_equal(names(x), as.character(tdat)) |
| 37 |
y <- unlabel_levels(x) |
| 38 |
expect_equal(unname(y), tdat) |
| 39 |
expect_equal(names(y), unname(x)) |
| 40 |
}) |
| 41 |
|
| 42 |
expect_silent(label_levels(NA_integer_)) |
| 43 |
expect_warning(label_levels(700)) |
| 44 |
expect_warning(unlabel_levels(NA_character_)) |
| 45 |
expect_warning(unlabel_levels("foo")) |
| 46 |
|
| 47 |
num <- c(93L, 200L) |
| 48 |
expect_warning(chr <- label_levels(num)) |
| 49 |
expect_warning( |
| 50 |
expect_identical(unname(unlabel_levels(chr)), num) |
| 51 |
) |
| 52 |
}) |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
test_that("colorize_levels() works as expected", { |
| 58 |
if (!crayon::has_color()) skip("Terminal does not support colors") |
| 59 |
# set colors manualy so that we also check on systems without real color support |
| 60 |
colors <- list( |
| 61 |
"fatal" = crayon::red, |
| 62 |
"error" = crayon::blue, |
| 63 |
"warn" = crayon::yellow, |
| 64 |
"debug" = crayon::silver, |
| 65 |
"trace" = crayon::white |
| 66 |
) |
| 67 |
|
| 68 |
tdat <- setdiff(sample(seq(100, 600, by = 100)), 400) # 400/info is not colored |
| 69 |
tr1 <- colorize_levels(tdat, colors = colors) |
| 70 |
walk(tr1, function(.x) expect_true(crayon::has_style(.x), info = .x)) |
| 71 |
tr2 <- colorize_levels(label_levels(tdat), colors = colors) |
| 72 |
walk(tr2, function(.x) expect_true(crayon::has_style(.x), info = .x)) |
| 73 |
|
| 74 |
expect_true(is.null(colorize_levels(NULL))) |
| 75 |
expect_identical(colorize_levels(integer()), integer()) |
| 76 |
expect_identical(colorize_levels(character()), character()) |
| 77 |
expect_identical(colorize_levels(tdat, NULL), tdat) |
| 78 |
}) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
test_that("named_union() works as expected", { |
| 84 |
x <- c("blah" = "blubb", "fizz" = "buzz") |
| 85 |
y <- c("foo" = "bar", "blah2" = "blubb") |
| 86 |
|
| 87 |
r <- named_union(x, y) |
| 88 |
|
| 89 |
expect_identical( |
| 90 |
r, |
| 91 |
setNames(c("blubb", "buzz", "bar"), c("foo", "fizz", "blah2")) |
| 92 |
) |
| 93 |
}) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
test_that("standardize_* doc examples", { |
| 99 |
|
| 100 |
expect_identical(standardize_threshold("info"), 400L) |
| 101 |
expect_true(is.na(standardize_threshold("all"))) |
| 102 |
|
| 103 |
expect_identical(standardize_log_level("info"), 400L) |
| 104 |
expect_error(is.na(standardize_log_level("all"))) |
| 105 |
|
| 106 |
expect_error(standardize_log_level(c("info", "fatal"))) |
| 107 |
expect_identical(standardize_log_levels(c("info", "fatal")), c(400L, 100L)) |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
}) |