Debian Codegraph @

tests/test-cran/test-record.R

raw
1 library(testit)
2
3 record2 = function(...) unlist(record(...))
4
5 assert('record() works', {
6 (record2('quote(x)') %==% c('quote(x)', 'x'))
7 (record2('quote(x+y)') %==% c('quote(x+y)', 'x + y'))
8 })
9
10 assert('record() handles raw_string objects', {
11 s = raw_string(c('line 1', 'line 2\nwith newline', 'line 3 "with quotes"'))
12 rec = record('s')
13 (as.character(rec[[2]]) %==% as.character(s))
14 })
15
16 if (loadable('data.table')) assert('record() works with data.table', {
17 x = c('d = data.table::as.data.table(mtcars)', 'd[, mpg := 99]')
18 rec = record(x, envir = globalenv())
19 (length(rec) %==% 1L) # only source code, no output printed
20 (as.character(rec[[1]]) %==% x)
21 })
22
23 assert('record() avoids recording empty plots', {
24 rec = record('library(grid); g <- pointsGrob(); x <- convertUnit(g$x, "mm")')
25 (length(rec) %==% 1L) # only source code, no plot generated
26 })
27
28 assert('record() can selectively keep plots', {
29 f = function(i = TRUE) record(c('plot(1)', 'plot(2)'), dev.keep = i)
30 cl = function(x) sub('^record_', '', sapply(x, class))
31 (cl(f()) %==% rep(c('source', 'plot'), 2)) # keep all plots
32 (cl(f(-1)) %==% c('source', 'plot')) # remove 1st plot
33 (cl(f(c(FALSE, TRUE))) %==% c('source', 'plot')) # ditto
34 (cl(f('last')) %==% c('source', 'plot')) # keep the last plot only
35 (cl(f(-2)) %==% c('source', 'plot', 'source')) # remove 2nd plot
36 (cl(f(FALSE)) %==% 'source') # remove all plots
37 })
38
39 # the option try.outFile was introduced in R 3.4.0
40 opts = options(try.outFile = NULL)
41 if (getRversion() >= '3.4.0') assert('record() can capture try() messages', {
42 rec = record('try(stop("asdf qwer zxcv"))')
43 (length(rec) %==% 2L)
44 (grepl('asdf qwer zxcv', rec[[2]]))
45 })
46 options(opts)
47
48 assert('record() returns empty result for NULL code', {
49 res = record(NULL)
50 (inherits(res, 'xfun_record_results'))
51 (length(res) %==% 0L)
52 })
53
54 assert('record() captures messages', {
55 res = record('message("hello")', message = TRUE)
56 cls = sapply(res, function(x) sub('record_', '', class(x)))
57 ('message' %in% cls)
58 (any(grepl('hello', unlist(res))))
59 })
60
61 assert('record() suppresses messages when message = FALSE', {
62 res = record('message("hidden")', message = FALSE)
63 cls = sapply(res, function(x) sub('record_', '', class(x)))
64 (!('message' %in% cls))
65 })
66
67 assert('record() captures warnings', {
68 res = record('warning("oops")', warning = TRUE)
69 cls = sapply(res, function(x) sub('record_', '', class(x)))
70 ('warning' %in% cls)
71 (any(grepl('oops', unlist(res))))
72 })
73
74 assert('record() captures errors when error = TRUE', {
75 res = record('stop("boom")', error = TRUE)
76 cls = sapply(res, function(x) sub('record_', '', class(x)))
77 ('error' %in% cls)
78 (any(grepl('boom', unlist(res))))
79 })
80
81 assert('record() handles syntax errors gracefully', {
82 res = record('1 +', error = TRUE)
83 cls = sapply(res, function(x) sub('record_', '', class(x)))
84 ('source' %in% cls)
85 ('error' %in% cls)
86 })
87
88 assert('record() with verbose = 2 prints invisible values', {
89 res = record('invisible(42)', verbose = 2)
90 (any(grepl('42', unlist(res))))
91 })
92
93 assert('record() with verbose = 1 prints last invisible value', {
94 res = record(c('invisible(1)', 'invisible(2)'), verbose = 1)
95 (any(grepl('2', unlist(res))))
96 })
97
98 assert('record() captures text output', {
99 res = record('cat("hello world")')
100 cls = sapply(res, function(x) sub('record_', '', class(x)))
101 ('output' %in% cls)
102 (any(grepl('hello world', unlist(res))))
103 })
104
105 assert('format.xfun_record_results() works for text output', {
106 res = record(c('1 + 1', 'cat("hi")'))
107 txt = format(res, to = 'text')
108 (is.character(txt))
109 (any(grepl('1 \\+ 1', txt)))
110 (any(grepl('hi', txt)))
111 })
112
113 assert('format.xfun_record_results() works for markdown output', {
114 res = record(c('1 + 1'))
115 md = format(res, to = 'markdown')
116 (is.character(md))
117 (any(grepl('```', md)))
118 })
119
120 assert('format.xfun_record_results() works for html output', {
121 res = record(c('1 + 1'))
122 html = format(res, to = 'html')
123 (is.character(html))
124 (any(grepl('<pre', html)))
125 (any(grepl('</code></pre>', html)))
126 })
127
128 assert('format.xfun_record_results() html with template', {
129 res = record('1 + 1')
130 html = format(res, to = 'html', template = TRUE)
131 (any(grepl('<html', html) | grepl('<!DOCTYPE', html)))
132 })
133
134 assert('print.xfun_record_results() works in non-interactive mode', {
135 res = record('1 + 1')
136 out = capture.output(print(res, browse = FALSE))
137 (length(out) > 0L)
138 })
139
140 assert('record() with multiple messages in one expression', {
141 res = record('{message("a"); message("b")}', message = TRUE)
142 cls = sapply(res, function(x) sub('record_', '', class(x)))
143 ('message' %in% cls)
144 })
145
146 assert('new_record() creates record objects with correct class', {
147 r = new_record('hello', 'output')
148 (inherits(r, 'record_output'))
149 (r %==% structure('hello', class = 'record_output'))
150 })
151
152 assert('record_print.default() captures print output', {
153 out = record_print(1:3)
154 (is.character(out))
155 (any(grepl('1 2 3', out)))
156 })
157
158 assert('format() markdown output includes fenced blocks for messages/warnings', {
159 res = record(c('message("hi")', 'warning("yo")'), message = TRUE, warning = TRUE)
160 md = format(res, to = 'markdown')
161 (any(grepl('```', md)))
162 (any(grepl('[.]message', md)))
163 (any(grepl('[.]warning', md)))
164 })
165
166 assert('format() html output for messages and errors', {
167 res = record('stop("boom")', error = TRUE)
168 html = format(res, to = 'html')
169 (any(grepl('class="error"', html)))
170 (any(grepl('boom', html)))
171 })
172
173 assert('record() with error = NA halts on error', {
174 (has_error(record('stop("fail")', error = NA)))
175 })
176
177 assert('record() with message = NA does not capture messages', {
178 res = record('message("pass through")', message = NA)
179 cls = sapply(res, function(x) sub('record_', '', class(x)))
180 (!('message' %in% cls))
181 })