Skip to content

Commit

Permalink
use the assert(fact, {}) syntax for all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Dec 12, 2023
1 parent 71b2839 commit 02f0af6
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 318 deletions.
30 changes: 14 additions & 16 deletions tests/testit/test-cache.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
library(testit)

assert(
'find_globals() identifies global variables',
assert('find_globals() identifies global variables', {
# nothing from outside environment
identical(find_globals('x=1'), character(0)),
(find_globals('x=1') %==% character(0))
# qwer must be created outside somewhere
identical(find_globals('a=1; b=a; d=qwer'), 'qwer'),
identical(find_globals('a=function(){f=2;g}'), 'g'),
(find_globals('a=1; b=a; d=qwer') %==% 'qwer')
(find_globals('a=function(){f=2;g}') %==% 'g')
# y was assigned locally in z, but there is another y outside from nowhere
identical(find_globals('z=function(){y=1};y'), 'y'),
(find_globals('z=function(){y=1};y') %==% 'y')
# more complicated cases: operators, subscripts, ...
identical(find_globals(c('a=1%*%1%o%2 %in% d', 'b=d%%10+3%/%2-z[1:3]')), c('d', 'z'))
)
(find_globals(c('a=1%*%1%o%2 %in% d', 'b=d%%10+3%/%2-z[1:3]')) %==% c('d', 'z'))
})

assert(
'find_symbols() identifies all symbols',
find_symbols('x = x + 1; rnorm(1, std = z)') %==% c('x', 'rnorm', 'z')
)
assert('find_symbols() identifies all symbols', {
(find_symbols('x = x + 1; rnorm(1, std = z)') %==% c('x', 'rnorm', 'z'))
})

knit_lazy = function(lazy = TRUE) {
in_dir(tempdir(), {
Expand All @@ -29,10 +27,10 @@ knit_lazy = function(lazy = TRUE) {
x1 == x2 # x1 should not be updated
})
}
assert(
'cache.lazy = TRUE/FALSE works',
knit_lazy(TRUE), knit_lazy(FALSE)
)
assert('cache.lazy = TRUE/FALSE works', {
(knit_lazy(TRUE))
(knit_lazy(FALSE))
})

knit_code$set(a = 1, b = 2, c = 3)
assert('dep_prev() sets dependencies on previous chunks', {
Expand Down
9 changes: 4 additions & 5 deletions tests/testit/test-citation.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ library(testit)
pkgs = c(rownames(installed.packages(priority = 'high')), 'evaluate', 'knitr')
write_bib(pkgs, tempfile(), tweak = FALSE)

assert(
'& is escaped in title when write_bib(tweak = TRUE)',
length(grep(' & ', grep(
assert('& is escaped in title when write_bib(tweak = TRUE)', {
(length(grep(' & ', grep(
'^ title =', capture.output(write_bib(pkgs, tweak = TRUE)), value = TRUE
))) == 0
)
))) %==% 0L)
})
64 changes: 29 additions & 35 deletions tests/testit/test-closure.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,49 @@ library(testit)

z = new_defaults(list(a = 1))

assert(
'$set() and $get() methods set/get values',
identical(z$get(), list(a = 1)),
identical({z$set(a = 2); z$get('a')}, 2),
identical({z$set(b = 'f'); z$get(c('a', 'b'))}, list(a = 2, b = 'f'))
)

assert(
'$merge() does not change the object but returns a list of new values',
identical({z$merge(list(b = 'g')); z$get('b')}, 'f'),
identical(z$merge(list(b = 'g')), list(a = 2, b = 'g'))
)

assert(
'$get(default=TRUE) returns the initial value',
identical(z$get(default = TRUE), list(a = 1))
)

assert(
'$get(names) keeps the names if drop = FALSE and one or more names do not exist',
identical(z$get(c('a', 'c')), list(a = 2, c = NULL)),
identical(z$get('c'), NULL),
identical(z$get('c', drop = FALSE), list(c = NULL))
)
assert('$set() and $get() methods set/get values', {
(z$get() %==% list(a = 1))
({z$set(a = 2); z$get('a')} %==% 2)
({z$set(b = 'f'); z$get(c('a', 'b'))} %==% list(a = 2, b = 'f'))
})

assert('$merge() does not change the object but returns a list of new values', {
({z$merge(list(b = 'g')); z$get('b')} %==% 'f')
(z$merge(list(b = 'g')) %==% list(a = 2, b = 'g'))
})

assert('$get(default=TRUE) returns the initial value', {
(z$get(default = TRUE) %==% list(a = 1))
})

assert('$get(names) keeps the names if drop = FALSE and one or more names do not exist', {
(z$get(c('a', 'c')) %==% list(a = 2, c = NULL))
(z$get('c') %==% NULL)
(z$get('c', drop = FALSE) %==% list(c = NULL))
})

assert('$delete() deletes keys from the list', {
z$set(b1 = TRUE, b2 = FALSE); z$delete(c('b1', 'b2'))
(!any(c('b1', 'b2') %in% z$get()))
(intersect(c('b1', 'b2'), names(z$get())) %==% character())
})

assert(
'$restore() restores to the initial value',
identical({z$restore(); z$get()}, list(a = 1))
)
assert('$restore() restores to the initial value', {
({z$restore(); z$get()} %==% list(a = 1))
})

assert('$append() returns appended the chunk option', {
z$set(d = 1)
z$append(d = 2)
identical(z$get('d'), c(1, 2))
(z$get('d') %==% c(1, 2))
})

z = new_defaults()

# named argument in set()
z$set(a = list(b = 2, c = 'qwer'))

assert(
'a named argument of list in $set() method is not treated as a list of options',
identical(length(z$get()), 1L),
assert('a named argument of list in $set() method is not treated as a list of options', {
(length(z$get()) %==% 1L)
# unnamed argument in set()
identical({z$restore(); z$set(list(b = 2, c = 'qwer')); z$get()}, list(b = 2, c = 'qwer'))
)
({z$restore(); z$set(list(b = 2, c = 'qwer')); z$get()} %==% list(b = 2, c = 'qwer'))
})
24 changes: 11 additions & 13 deletions tests/testit/test-envir.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ library(testit)

rm(list = ls())
z = 5
assert(
'a list can be used as the parent frame for knit()',
assert('a list can be used as the parent frame for knit()', {
# evaluate in a new environment; should create an object in current envir
!has_error(with(list(y = 4:8), knit('knit-envir.Rmd', quiet = TRUE)))
)
(!has_error(with(list(y = 4:8), knit('knit-envir.Rmd', quiet = TRUE))))
})

env = new.env()
env$y = 1:3
knit('knit-envir.Rmd', envir = env, quiet = TRUE)
assert(
'knit() creates objects in its envir argument',
!exists('asdfqwerzxcv'), exists('asdfqwerzxcv', envir = env)
)
assert('knit() creates objects in its envir argument', {
(!exists('asdfqwerzxcv'))
(exists('asdfqwerzxcv', envir = env))
})

assert(
'undefined external objects should cause errors',
suppressMessages(has_error(knit('knit-envir.Rmd', quiet = TRUE))), # y is not found
!has_error(with(list(y = letters), knit('knit-envir.Rmd', quiet = TRUE)))
)
assert('undefined external objects should cause errors', {
(suppressMessages(has_error(knit('knit-envir.Rmd', quiet = TRUE)))) # y is not found
(!has_error(with(list(y = letters), knit('knit-envir.Rmd', quiet = TRUE))))
})

file.remove('knit-envir.md')
107 changes: 45 additions & 62 deletions tests/testit/test-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,52 @@ res = mapply(
c('abc.Rnw', 'abc.rnw', 'abc.rtex', 'abc.Rmd', 'abc.rhtm', 'abc.Rhtml', 'foo.abc.rhtml'),
USE.NAMES = FALSE
)
assert(
'auto_out_name() converts .Rfoo to .foo',
identical(res, c('abc.tex', 'abc.tex', 'abc.tex', 'abc.md', 'abc.htm', 'abc.html', 'foo.abc.html'))
)
assert('auto_out_name() converts .Rfoo to .foo', {
(res %==% c('abc.tex', 'abc.tex', 'abc.tex', 'abc.md', 'abc.htm', 'abc.html', 'foo.abc.html'))
})

res = mapply(
auto_out_name,
c('abc.tex', '_knit_abc.tex', '_knit_abc.md', 'foo_knit_.html'),
USE.NAMES = FALSE
)
assert(
'auto_out_name() converts .tex/.unknown to .txt, and removes _knit_',
identical(res, c('abc.txt', 'abc.tex', 'abc.md', 'foo.html')),
identical(auto_out_name('foo.bar'), 'foo.txt')
)
assert('auto_out_name() converts .tex/.unknown to .txt, and removes _knit_', {
(res %==% c('abc.txt', 'abc.tex', 'abc.md', 'foo.html'))
(auto_out_name('foo.bar') %==% 'foo.txt')
})

assert(
'chunks with include=FALSE should stop on error',
suppressMessages(
assert('chunks with include=FALSE should stop on error', {
(suppressMessages(
has_error(knit(text = c('<<include=F>>=', '1+"a"', '@'), quiet = TRUE))
)
)
))
})

assert(
'tidy=FALSE + eval=numeric should work',
identical(
'[1] 1\n[1] 2\n',
knit(
text = c('<<tidy=FALSE, eval=1:2, echo=FALSE, results="asis">>=', '1', '1+', '1', '1', '@'),
quiet = TRUE
)
)
)
assert('tidy=FALSE + eval=numeric should work', {
('[1] 1\n[1] 2\n' %==% knit(
text = c('<<tidy=FALSE, eval=1:2, echo=FALSE, results="asis">>=', '1', '1+', '1', '1', '@'),
quiet = TRUE
))
})

assert(
'opts_template options are used',
identical(
"\n[1] 2\n",
knit(
text = c(
"<<include=FALSE>>=","opts_template$set(quiet = list(echo=FALSE))", "@",
"<<results='asis', opts.label='quiet'>>=", "1+1", "@"
),
quiet = TRUE
)
)
)
assert('opts_template options are used', {
("\n[1] 2\n" %==% knit(
text = c(
"<<include=FALSE>>=","opts_template$set(quiet = list(echo=FALSE))", "@",
"<<results='asis', opts.label='quiet'>>=", "1+1", "@"
),
quiet = TRUE
))
})

assert(
'local chunk options override opts_template',
identical(
"\n\\begin{kframe}\n\\begin{alltt}\n\\hlnum{1}\\hlopt{+}\\hlnum{1}\n\\end{alltt}\n\\end{kframe}[1] 2\n",
knit(
text = c(
"<<include=FALSE>>=","opts_template$set(quiet = list(echo=FALSE))", "@",
"<<results='asis', opts.label='quiet', echo=TRUE, tidy=FALSE>>=", "1+1", "@"
),
quiet = TRUE
)
)
)
assert('local chunk options override opts_template', {
("\n\\begin{kframe}\n\\begin{alltt}\n\\hlnum{1}\\hlopt{+}\\hlnum{1}\n\\end{alltt}\n\\end{kframe}[1] 2\n" %==% knit(
text = c(
"<<include=FALSE>>=","opts_template$set(quiet = list(echo=FALSE))", "@",
"<<results='asis', opts.label='quiet', echo=TRUE, tidy=FALSE>>=", "1+1", "@"
),
quiet = TRUE
))
})

# a shortcut
k = function(text) {
Expand All @@ -81,10 +66,10 @@ k(c(
'x2 = opts_chunk$get("dpi")'
))

assert(
'using knit_child() does not reset global chunk options set in child documents',
x1 == FALSE, x2 == 200
)
assert('using knit_child() does not reset global chunk options set in child documents', {
(x1 %==% FALSE)
(x2 %==% 200)
})

txt = '%\\documentclass{article}
\\documentclass{article}
Expand All @@ -93,10 +78,9 @@ txt = '%\\documentclass{article}
\\end{document}'
res = strsplit(knit(text = txt, quiet = TRUE), '\n')[[1]]

assert(
'insert_header_latex() finds the correct \\documentclass{}',
identical(res[1], '%\\documentclass{article}')
)
assert('insert_header_latex() finds the correct \\documentclass{}', {
(res[1] %==% '%\\documentclass{article}')
})

txt = '\\documentclass{article}
\\begin{document}
Expand All @@ -107,10 +91,9 @@ txt = '\\documentclass{article}
\\end{document}'
res = strsplit(knit(text = txt, quiet = TRUE), '\n')[[1]]

assert(
'insert_header_latex() finds the correct \\documentclass{}',
identical(res[length(res) - 3], '\\documentclass{article}')
)
assert('insert_header_latex() finds the correct \\documentclass{}', {
(res[length(res) - 3] %==% '\\documentclass{article}')
})

assert('knit_meta_add() adds meta objects with the correct number of labels', {
knit_meta(clean = TRUE)
Expand Down
Loading

0 comments on commit 02f0af6

Please sign in to comment.