Debian Codegraph @

tests/testthat/test_print_LogEvent.R

raw
1 context("print_LogEvent")
2
3
4
5 test_that("format.LogEvent works as expected", {
6 x <- LogEvent$new(
7 logger = lgr::lgr,
8 msg = "lorem skjdghsad akjsgh asdgjh asdgjshadk gklsd.",
9 blah = "blubb",
10 numbers = 1:100,
11 large_number = c(23.525325235213525235525, 930.824687923409867298367293406),
12 iris = iris,
13 logg = lgr::lgr,
14 letters = letters
15 )
16
17 expect_output(print(x))
18 expect_true(!crayon::has_style(format(x, colors = NULL)))
19 expect_output(expect_identical(x, print(x)))
20 })
21
22
23
24
25 test_that("format.LogEvent works with colored %k and %K paramters", {
26 old <- Sys.getenv("R_CLI_NUM_COLORS")
27 Sys.setenv(R_CLI_NUM_COLORS = "256")
28 on.exit(Sys.setenv(R_CLI_NUM_COLORS = as.character(old)))
29
30 x <- LogEvent$new(
31 level = 200,
32 logger = lgr::lgr,
33 msg = "lorem skjdghsad akjsgh asdgjh asdgjshadk gklsd.",
34 blah = "blubb",
35 numbers = 1:100,
36 large_number = c(23.525325235213525235525, 930.824687923409867298367293406),
37 iris = iris,
38 logg = lgr::lgr,
39 letters = letters
40 )
41
42 expect_silent(
43 expect_true(
44 crayon::has_style(format(x, fmt = "%k %m", colors = list(error = crayon::bgRed)))
45 )
46 )
47
48 expect_silent(
49 expect_true(
50 crayon::has_style(format(x, fmt = "%K %m", colors = list(error = crayon::bgRed)))
51 )
52 )
53 })
54
55
56 test_that("format.LogEvent - with excluded_fields - excludes those fields", {
57
58 # Arrange
59 x <- LogEvent$new(
60 level = 200,
61 logger = lgr::lgr,
62 msg = "lorem skjdghsad akjsgh asdgjh asdgjshadk gklsd.",
63 included_custom_field = "123",
64 excluded_custom_field = "abc"
65 )
66
67 # Act
68 res <- format(x, fmt = "%k %m %j %f", excluded_fields = "excluded_custom_field")
69
70 # Assert
71 expect_match(res, "included_custom_field")
72 expect_no_match(res, "excluded_custom_field")
73 })
74
75
76
77 test_that("toString.LogEvent works as expected", {
78 x <- LogEvent$new(
79 logger = lgr::lgr,
80 msg = "lorem skjdghsad akjsgh asdgjh asdgjshadk gklsd.",
81 blah = "blubb",
82 numbers = 1:100,
83 large_number = c(23.525325235213525235525, 930.824687923409867298367293406),
84 iris = iris,
85 logg = lgr::lgr,
86 letters = letters
87 )
88
89 expect_length(toString(x), 1L)
90 # just a rudimentary check because exact representation depends on console width
91 expect_match(toString(x), "level.*letters")
92 })
93
94
95
96
97 test_that("toString.LogEvent without custom fields does not end in whitespace", {
98 event_without_field <- LogEvent$new(
99 logger = lgr::lgr,
100 msg = "lorem skjdghsad akjsgh asdgjh asdgjshadk gklsd."
101 )
102 res <- format(event_without_field, fmt = "%m %f", colors = NULL)
103 expect_identical(
104 substr(res, nchar(res), nchar(res)),
105 "."
106 )
107
108 event_with_field <- LogEvent$new(
109 logger = lgr::lgr,
110 msg = "lorem skjdghsad akjsgh asdgjh asdgjshadk gklsd.",
111 foo = "bar"
112 )
113 res <- format(event_with_field, fmt = "msg %f", colors = NULL)
114 expect_identical(
115 substr(res, nchar(res), nchar(res)),
116 "}"
117 )
118 })