Debian Codegraph @

inst/tests/test-evals.R

raw
1 library(testthat)
2 library(pander)
3
4 cache.dir <- evalsOptions('cache.dir')
5 graph.dir <- evalsOptions('graph.dir')
6 wd <- getwd()
7 setwd(tempdir())
8 evalsOptions('cache.dir', file.path(tempdir(), '.cache'))
9 evalsOptions('graph.dir', file.path(tempdir(), 'plots'))
10
11 context('eval.msgs')
12
13 test_that('returns', {
14 expect_equal(eval.msgs('1:5')$result, 1:5)
15 expect_equal(eval.msgs('mtcars')$result, mtcars)
16 expect_null(eval.msgs('x <- mtcars')$result)
17 expect_null(evals('plot(mtcars)')$result)
18 expect_equal(eval.msgs('cat(1:5);1:5')$result, 1:5)
19 expect_null(eval.msgs('#comment')$result)
20 expect_null(eval.msgs('')$result)
21 })
22
23 test_that('messages', {
24 expect_null(eval.msgs('1:5')$msg$messages)
25 expect_null(eval.msgs('mtcars')$msg$messages)
26 expect_null(eval.msgs('x <- mtcars')$msg$messages)
27 expect_null(evals('plot(mtcars)')$msg$messages)
28 expect_null(eval.msgs('warning("d");warning("f");1')$msg$messages)
29 expect_equal(eval.msgs('message("FOO")')$msg$messages, 'FOO')
30 expect_equal(eval.msgs(c('message("FOO")', '1:2'))$msg$messages, 'FOO')
31 expect_equal(eval.msgs(c('message("FOO");message("FOO");warning("FOO")', '1:2'))$msg$messages, c('FOO', 'FOO'))
32 })
33
34 test_that('warnings', {
35 expect_null(eval.msgs('1:5')$msg$warnings)
36 expect_null(eval.msgs('mtcars')$msg$warnings)
37 expect_null(eval.msgs('x <- mtcars')$msg$warnings)
38 expect_null(evals('plot(mtcars)')$msg$warnings)
39 expect_null(eval.msgs('message("FOO")')$msg$warnings)
40 expect_null(eval.msgs(c('message("FOO")', '1:2'))$msg$warnings)
41 expect_equal(eval.msgs('warning("d");warning("f");1')$msg$warnings, c('d', 'f'))
42 expect_equal(eval.msgs(c('warning("FOO")', '1:2'))$msg$warnings, 'FOO')
43 expect_equal(eval.msgs(c('warning("FOO");message("FOO");warning("FOO")', '1:2'))$msg$warnings, c('FOO', 'FOO'))
44 })
45
46 test_that('errors', {
47 expect_null(eval.msgs('1:5')$msg$errors)
48 expect_null(eval.msgs('mtcars')$msg$errors)
49 expect_null(eval.msgs('x <- mtcars')$msg$errors)
50 expect_null(evals('plot(mtcars)')$msg$errors)
51 expect_null(eval.msgs('warning("d");warning("f");1')$msg$errors)
52 expect_null(eval.msgs('message("FOO")')$msg$errors)
53 expect_null(eval.msgs(c('message("FOO")', '1:2'))$msg$errors)
54 expect_null(eval.msgs(c('warning("FOO")', '1:2'))$msg$errors)
55 expect_null(eval.msgs(c('warning("FOO");message("FOO");warning("FOO")', '1:2'))$msg$errors)
56 expect_is(eval.msgs('runiff(2)')$msg$errors, 'character')
57 expect_is(eval.msgs('runif would be nice to run')$msg$errors, 'character')
58 expect_is(eval.msgs('no.R.object.like.that')$msg$errors, 'character')
59 expect_error(evals())
60 expect_error(evals('1:5', hooks = 1:10))
61 expect_error(evals('plot(mtcars)', graph.name = 1))
62 env <- new.env()
63 env$plot <- 1
64 expect_error(evals(1:10, env = env))
65 })
66
67 test_that('output', {
68 expect_is(eval.msgs('1:5')$output, 'character')
69 expect_is(eval.msgs('mtcars')$output, 'character')
70 expect_is(eval.msgs('cat(1:5);1:5')$output, 'character')
71 expect_null(eval.msgs('x <- mtcars')$output)
72 expect_null(evals('plot(mtcars)')$output)
73 expect_null(eval.msgs('cat(1:5)')$output)
74 })
75
76 test_that('stdout', {
77 expect_null(eval.msgs('1:5')$stdout)
78 expect_null(eval.msgs('mtcars')$stdout)
79 expect_null(eval.msgs('x <- mtcars')$stdout)
80 expect_null(evals('plot(mtcars)')$stdout)
81 expect_equal(eval.msgs('cat(1:5)')$stdout, '1 2 3 4 5')
82 expect_equal(eval.msgs('cat(1:5);1:5')$stdout, '1 2 3 4 5')
83 })
84
85 context('evals')
86
87 test_that('Variable assignement', {
88 expect_null(evals('x <- rnorm(100)')[[1]]$result)
89 })
90
91 test_that('Multiple variable assignement', {
92 expect_null(evals(list(c('x <- rnorm(100)', 'y <- rnorm(10)')), parse = F)[[1]]$result)
93 })
94
95 test_that('Comment', {
96 expect_null(evals('## comment')[[1]]$result)
97 })
98
99 test_that('Comment & variable assignement', {
100 expect_null(evals(list(c('## comment', 'y <- rnorm(10)')), parse = F)[[1]]$result)
101 })
102
103 test_that('Basic: numerics', {
104 expect_is(evals('rnorm(100)')[[1]]$result, 'numeric')
105 })
106
107 test_that('Basic: character', {
108 expect_equal(evals('"Hello world!"')[[1]]$result, 'Hello world!')
109 })
110
111 test_that('Basic: list', {
112 expect_is(evals('lapply(rnorm(10), round)')[[1]]$result, 'list')
113 })
114
115 test_that('Basic: data.frame', {
116 expect_is(evals('mtcars')[[1]]$result, 'data.frame')
117 })
118
119 test_that('Plot: graphics package', {
120 expect_is(evals('plot(1:10)')[[1]]$result, 'image')
121 })
122
123 test_that('Plot: lattice package', {
124 expect_is(evals('require(lattice);+histogram(mtcars$hp)')[[1]]$result, 'image')
125 expect_is(evals('require(lattice);histogram(mtcars$hp)', parse = F)[[1]]$result, 'image')
126 })
127
128 test_that('Plot: multiline code', {
129 expect_is(evals(list(c('plot(cars, main="Stopping Distance versus Speed")',
130 'plot(cars, main="Stopping Distance versus Speed")')),
131 parse = F)[[1]]$result,
132 'image')
133 expect_is(evals('plot(cars, main="Stopping Distance versus Speed"); plot(cars, main="Stopping Distance versus Speed")')[[2]]$result, 'image') #nolint
134 })
135
136 test_that('Plot: pdf', {
137 expect_is(evals('plot(1:10)', graph.output = 'pdf')[[1]]$result, 'image')
138 })
139
140 test_that('Plot: pdf without plotting', {
141 expect_is(evals('runif(10)', graph.output = 'pdf')[[1]]$result, 'numeric')
142 })
143
144 test_that('NULL', {
145 expect_is(evals('rnorm(100); NULL')[[1]]$result, 'numeric')
146 expect_null(evals('rnorm(100); NULL')[[2]]$result)
147 expect_null(evals('rnorm(100); NULL', parse = FALSE)[[1]]$result)
148 })
149
150 test_that('## comment', {
151 expect_is(evals('rnorm(100);## comment')[[1]]$result, 'numeric')
152 expect_is(evals('rnorm(100);## comment', parse = FALSE)[[1]]$result, 'numeric')
153 })
154
155 test_that('var assignement', {
156 expect_is(evals('rnorm(100);x <- 1')[[1]]$result, 'numeric')
157 expect_null(evals('rnorm(100);x <- 1', parse = F)[[1]]$result)
158 })
159
160 test_that('creating function', {
161 expect_is(evals('rnorm(100);f <- function(x) mean(x)')[[1]]$result, 'numeric')
162 expect_null(evals('rnorm(100);f <- function(x) mean(x)')[[2]]$result)
163 expect_null(evals('rnorm(100);f <- function(x) mean(x)', parse = F)[[1]]$result)
164 })
165
166
167 test_that('string+num', {
168 expect_is(evals('"X";rnorm(100)')[[2]]$result, 'numeric')
169 expect_is(evals('"X";rnorm(100)')[[1]]$result, 'character')
170 expect_is(evals('"X";rnorm(100)', parse = FALSE)[[1]]$result, 'numeric')
171 })
172
173 test_that('data.frame+num', {
174 expect_is(evals(list(c('mtcars', 'rnorm(100)')), parse = FALSE)[[1]]$result, 'numeric')
175 })
176
177 test_that('simple error', {
178 expect_is(evals('dadaffaSFA+A')[[1]]$msg$error, 'character')
179 })
180
181 test_that('simple error', {
182 expect_is(evals('runiff(10)')[[1]]$msg$error, 'character')
183 })
184
185 test_that('multiple errors', {
186 expect_is(evals(list(c('runiff(20)', 'Old MacDonald had a farm?')), parse = F)[[1]]$msg$error, 'character')
187 })
188
189 test_that('multiple errors', {
190 expect_is(evals('no.R.object;no.R.function();very.mixed.up(stuff)')[[1]]$msg$error, 'character')
191 expect_is(evals('no.R.object;no.R.function();very.mixed.up(stuff)')[[2]]$msg$error, 'character')
192 expect_is(evals('no.R.object;no.R.function();very.mixed.up(stuff)')[[3]]$msg$error, 'character')
193 expect_is(evals('no.R.object;no.R.function();very.mixed.up(stuff)', parse = F)[[1]]$msg$error, 'character')
194 })
195
196 test_that('output+errors', {
197 expect_is(evals(list(c('mean(1:10)', 'no.R.function()')), parse = F)[[1]]$msg$error, 'character')
198 })
199
200 test_that('output+multiple errors', {
201 expect_is(evals(list(c('no.R.object', 'Old MacDonald had a farm\\dots', 'pi')),
202 parse = F)[[1]]$msg$error,
203 'character')
204 })
205
206 test_that('simple warning', {
207 expect_is(evals('chisq.test(mtcars$gear, mtcars$hp)')[[1]]$msg$warning, 'character')
208 })
209
210 test_that('multiple warnings+output', {
211 expect_is(evals(list(c('chisq.test(mtcars$gear, mtcars$am)', 'pi', 'chisq.test(mtcars$gear, mtcars$hp)')),
212 parse = F)[[1]]$msg$warnings,
213 'character')
214 expect_is(evals('chisq.test(mtcars$gear, mtcars$am);pi;chisq.test(mtcars$gear, mtcars$hp)')[[1]]$msg$warnings,
215 'character')
216 })
217
218 test_that('long lines with line breaks', {
219 expect_is(evals('if (TRUE) {\nset.caption("FOO")\nplot(c(1,2,3,4,5,6,7,8,9,10,11,12))\n}')[[1]]$result, 'image')
220 expect_equal(length(evals('x <- "this is a very-very looooooooooong line which might be split on `parse`, but that is not a big deal at all"\nx\n1')), 3) #nolint
221 })
222
223
224 test_that('cache.dir option works correctly', {
225 dir <- tempdir()
226 suppressWarnings(file.remove(list.files(dir)))
227 t1 <- system.time(evals('1:1e5', cache.mode = 'disk', cache.dir = dir, cache.time = 0))
228 lf <- list.files(dir)
229 expect_true(length(lf) > 0)
230 t2 <- system.time(evals('1:1e5', cache.mode = 'disk', cache.dir = dir, cache.time = 0))
231 expect_equal(list.files(dir), lf)
232 # plots
233 suppressWarnings(file.remove(list.files(dir)))
234 t1 <- system.time(evals('plot(mtcars)',
235 cache.mode = 'disk',
236 cache.dir = dir,
237 cache.time = 0,
238 cache.copy.images = T))
239 lf <- list.files(dir)
240 expect_true(length(lf) > 0)
241 t2 <- system.time(evals('plot(mtcars)',
242 cache.mode = 'disk',
243 cache.dir = dir,
244 cache.time = 0,
245 cache.copy.images = TRUE))
246 expect_equal(list.files(dir), lf)
247 })
248
249 test_that('caching works correctly', {
250 env <- new.env()
251 evals('x <- 1:10', env = env, cache.time = 0)
252 expect_true(length(ls(pander:::cached.results)) > 0)
253 evals('x <- 1:100', env = env, cache.time = 0)
254 res <- evals('x', env = env)
255 expect_equal(res[[1]]$result, 1:100)
256 t1 <- system.time(evals('plot(mtcars)', hi.res = T))
257 lf <- list.files('plots')
258 t2 <- system.time(evals('plot(mtcars)', hi.res = T))
259 expect_equal(list.files('plots'), lf)
260 })
261
262 test_that('plots', {
263 r1 <- evals('ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth(aes(wt,mpg), method = lm, se=FALSE)',
264 graph.unify = T)
265 r2 <- evals('xyplot(wt ~ mpg, mtcars, type = c("p","smooth"))', graph.unify = T)
266 expect_equal(r1$type, r2$type)
267 dir <- 'temp'
268 r1 <- evals('plot(mtcars)',
269 graph.recordplot = TRUE,
270 cache.time = 0,
271 cache.copy.images = TRUE,
272 cache.mode = 'disk',
273 cache.dir = dir)
274 lf <- list.files(dir)
275 expect_true(length(list.files(dir, pattern = 'recordedplot$')) > 0)
276 png(filename = file.path(dir, 'recorded.png'))
277 redraw.recordedplot(list.files(dir, pattern = 'recordedplot$', full.names = T)[1])
278 dev.off()
279 })
280
281 test_that('hooks work correctly', {
282 set.seed(1)
283 txt <- 'runif(1:4); matrix(runif(25), 5, 5); 1:5'
284 hooks <- list('numeric' = round, 'matrix' = pander_return)
285 res <- evals(txt, hooks = hooks)
286 set.seed(1)
287 expect_equal(res[[1]]$result, round(runif(1:4)))
288 expect_equal(res[[2]]$result, pander_return(matrix(runif(25), 5, 5)))
289 ## using pander's default hook
290 set.seed(1)
291 res <- evals(txt, hooks = list('default' = pander_return))
292 set.seed(1)
293 expect_equal(res[[1]]$result, pander_return(runif(1:4)))
294 expect_equal(res[[2]]$result, pander_return(matrix(runif(25), 5, 5)))
295 expect_equal(res[[3]]$result, pander_return(1:5))
296 })
297
298 test_that('output param works correctly', {
299 res <- evals('1:10', output = 'all')
300 expect_equal(names(res[[1]]), c('src', 'result', 'output', 'type', 'msg', 'stdout'))
301 res <- evals('1:10', output = c('src', 'result'))
302 expect_equal(names(res[[1]]), c('src', 'result'))
303 expect_error(evals('1:10', output = c('src', 'result', '123')))
304 res <- evals('matrix(5,5,5)',
305 hooks = list('matrix' = list(pander_return, 'Cap')))[[1]]$result
306 expect_equal(res[14], 'Table: Cap')
307 })
308
309 evalsOptions('cache.dir', cache.dir)
310 evalsOptions('graph.dir', graph.dir)
311 setwd(wd)
312
313 test_that('switching cache works', {
314 co <- evalsOptions('cache')
315 evalsOptions('cache', FALSE)
316 expect_false(evalsOptions('cache'))
317 evalsOptions('cache', TRUE)
318 expect_true(evalsOptions('cache'))
319 evalsOptions('cache', co)
320 })