Debian Codegraph @

tests/testthat/test_utils.R

raw
1
2 context("utils")
3
4
5 test_that("utils works as expected", {
6
7 foo <- function() get_caller(-1L)
8 expect_identical(foo(), "foo")
9
10 lg <- Logger$new(
11 "test logger",
12 appenders = AppenderConsole$new(layout = LayoutFormat$new(fmt = "%c")),
13 propagate = FALSE
14 )
15
16
17 foobar <- function() lg$error("test")
18 expect_output(foobar(), "foobar")
19
20 fizzbuzz <- function() foobar()
21 expect_output(fizzbuzz(), "foobar")
22
23 blahblubb <- function() lg$log(200, "test")
24 expect_output(blahblubb(), "blahblubb")
25
26 expect_match(get_caller(-99), "shell")
27 })
28
29
30
31 test_that("standardize log_levels / threshold works", {
32 expect_error(standardize_log_level(NA))
33 expect_identical(standardize_threshold(NA), NA_integer_)
34
35 blubb <- 100
36 expect_identical(standardize_log_levels(blubb), 100L)
37 expect_identical(standardize_log_level(blubb), 100L)
38 expect_identical(standardize_threshold(blubb), 100L)
39
40 blubb <- NA
41 expect_error(standardize_log_levels(blubb), "blubb.*trace")
42 expect_error(standardize_log_level(blubb), "blubb.*trace")
43 expect_identical(standardize_threshold(blubb), NA_integer_)
44
45 blubb <- "all"
46 expect_error(standardize_log_levels(blubb), "blubb.*trace")
47 expect_error(standardize_log_level(blubb), "blubb.*trace")
48 expect_identical(standardize_threshold(blubb), NA_integer_)
49
50 blubb <- "off"
51 expect_error(standardize_log_levels(blubb), "blubb.*trace")
52 expect_error(standardize_log_level(blubb), "blubb.*trace")
53 expect_identical(standardize_threshold(blubb), 0L)
54
55 blubb <- 0
56 expect_error(standardize_log_levels(blubb), "blubb.*trace")
57 expect_error(standardize_log_level(blubb), "blubb.*trace")
58 expect_identical(standardize_threshold(blubb), 0L)
59
60
61 blubb <- "info"
62 expect_identical(standardize_log_levels(blubb), 400L)
63 expect_identical(standardize_log_level(blubb), 400L)
64 expect_identical(standardize_threshold(blubb), 400L)
65
66 blubb <- c("info", "warn")
67 expect_identical(standardize_log_levels(blubb), c(400L, 300L))
68 expect_error(standardize_log_level(blubb), "blubb.*scalar")
69 expect_error(standardize_threshold(blubb), "threshold.*scalar")
70
71 blubb <- c(400, 300)
72 expect_identical(standardize_log_levels(blubb), c(400L, 300L))
73 expect_error(standardize_log_level(blubb), "blubb.*scalar")
74 expect_error(standardize_threshold(blubb), "threshold.*scalar")
75
76 blubb <- c(0, 300)
77 expect_error(standardize_log_levels(blubb), "blubb.*trace")
78 expect_error(standardize_log_level(blubb), "blubb.*scalar")
79 expect_error(standardize_threshold(blubb), "threshold.*scalar")
80
81 blubb <- "bar"
82 expect_error(standardize_log_levels(blubb), "blubb.*trace")
83 expect_error(standardize_log_level(blubb), "blubb.*trace")
84 expect_error(standardize_threshold(blubb), "blubb.*trace")
85 })
86
87
88
89
90 test_that("get_caller() does something useful for corner cases", {
91 foo <- function(arg){get_caller()}
92 expect_identical(foo(), "foo")
93
94 r <- {function(arg){get_caller()}}()
95 expect_identical(r, "{...}")
96
97 r <- (function(arg){"sgnasdkgshdkghsakdghskdjghsdkag"; get_caller()})()
98 expect_length(r, 1)
99 })