From 02f0af6d7f0e55bc2a689f622dd523fcb3c9cd36 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Tue, 12 Dec 2023 13:47:04 -0600 Subject: [PATCH] use the `assert(fact, {})` syntax for all tests --- tests/testit/test-cache.R | 30 ++++---- tests/testit/test-citation.R | 9 +-- tests/testit/test-closure.R | 64 +++++++--------- tests/testit/test-envir.R | 24 +++--- tests/testit/test-output.R | 107 +++++++++++--------------- tests/testit/test-params.R | 139 +++++++++++++++------------------- tests/testit/test-parser.R | 107 ++++++++++---------------- tests/testit/test-sql.R | 61 +++++++-------- tests/testit/test-templates.R | 23 ++++-- tests/testit/test-utils.R | 2 +- 10 files changed, 248 insertions(+), 318 deletions(-) diff --git a/tests/testit/test-cache.R b/tests/testit/test-cache.R index bf0da9f42d..d820ff5372 100644 --- a/tests/testit/test-cache.R +++ b/tests/testit/test-cache.R @@ -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(), { @@ -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', { diff --git a/tests/testit/test-citation.R b/tests/testit/test-citation.R index e12ba09d99..7a44dc1394 100644 --- a/tests/testit/test-citation.R +++ b/tests/testit/test-citation.R @@ -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) +}) diff --git a/tests/testit/test-closure.R b/tests/testit/test-closure.R index ab827e6e0a..9b07b7e148 100644 --- a/tests/testit/test-closure.R +++ b/tests/testit/test-closure.R @@ -2,45 +2,40 @@ 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() @@ -48,9 +43,8 @@ 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')) +}) diff --git a/tests/testit/test-envir.R b/tests/testit/test-envir.R index a94eaebde7..579a123d8d 100644 --- a/tests/testit/test-envir.R +++ b/tests/testit/test-envir.R @@ -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') diff --git a/tests/testit/test-output.R b/tests/testit/test-output.R index b1b2f8bc68..c4df711d06 100644 --- a/tests/testit/test-output.R +++ b/tests/testit/test-output.R @@ -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('<>=', '1+"a"', '@'), quiet = TRUE)) - ) -) + )) +}) -assert( - 'tidy=FALSE + eval=numeric should work', - identical( - '[1] 1\n[1] 2\n', - knit( - text = c('<>=', '1', '1+', '1', '1', '@'), - quiet = TRUE - ) - ) -) +assert('tidy=FALSE + eval=numeric should work', { + ('[1] 1\n[1] 2\n' %==% knit( + text = c('<>=', '1', '1+', '1', '1', '@'), + quiet = TRUE + )) +}) -assert( - 'opts_template options are used', - identical( - "\n[1] 2\n", - knit( - text = c( - "<>=","opts_template$set(quiet = list(echo=FALSE))", "@", - "<>=", "1+1", "@" - ), - quiet = TRUE - ) - ) -) +assert('opts_template options are used', { + ("\n[1] 2\n" %==% knit( + text = c( + "<>=","opts_template$set(quiet = list(echo=FALSE))", "@", + "<>=", "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( - "<>=","opts_template$set(quiet = list(echo=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( + "<>=","opts_template$set(quiet = list(echo=FALSE))", "@", + "<>=", "1+1", "@" + ), + quiet = TRUE + )) +}) # a shortcut k = function(text) { @@ -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} @@ -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} @@ -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) diff --git a/tests/testit/test-params.R b/tests/testit/test-params.R index 865b42ffa1..0e59660b7a 100644 --- a/tests/testit/test-params.R +++ b/tests/testit/test-params.R @@ -1,20 +1,17 @@ library(testit) # helper function to convert raw src to params list -read_params <- function(src, evaluate = TRUE) { - lines <- strsplit(src, "\n")[[1]] +read_params = function(src, evaluate = TRUE) { + lines = strsplit(src, "\n")[[1]] knit_params(lines, evaluate = evaluate) } # helper function to convert raw src yaml to params list -read_params_yaml <- function(src, evaluate = TRUE) { +read_params_yaml = function(src, evaluate = TRUE) { knit_params_yaml(src, evaluate = evaluate) } - -## test basic parameter parsing -------------------------------------------- - -params <- read_params(' +params = read_params(' --- params: a: 10 @@ -22,16 +19,16 @@ params: --- ' ) -assert(params[[1]]$name == 'a') -assert(params[[1]]$value == 10) -assert(params[[2]]$name == 'b') -assert(params[[2]]$value == 20) - -assert(identical(flatten_params(params), list(a = 10L, b = 20L))) +assert('basic params parsing works', { + (params[[1]]$name %==% 'a') + (params[[1]]$value %==% 10L) + (params[[2]]$name %==% 'b') + (params[[2]]$value %==% 20L) + (flatten_params(params) %==% list(a = 10L, b = 20L)) +}) -## test date custom type (these deprecated and here for backwards compt) -- - -params <- read_params(' +# test date custom type (these deprecated and here for backwards compt) -- +params = read_params(' --- params: start: !date 2015-01-01 @@ -39,17 +36,16 @@ params: --- ' ) -assert(params[[1]]$name == 'start') -assert('Date' %in% class(params[[1]]$value)) -assert(params[[1]]$value == as.Date("2015-01-01")) -assert(params[[2]]$name == 'end') -assert('POSIXct' %in% class(params[[2]]$value)) -assert(params[[2]]$value == as.POSIXct("2015-01-01 12:30:00", tz = "GMT")) - - -## test specifying value in sub-object and type at object level ------------ +assert('date/time params parsing works', { + (params[[1]]$name %==% 'start') + (inherits(params[[1]]$value, 'Date')) + (params[[1]]$value %==% as.Date("2015-01-01")) + (params[[2]]$name %==% 'end') + (inherits(params[[2]]$value, 'POSIXct')) + (params[[2]]$value %==% as.POSIXct("2015-01-01 12:30:00", tz = "GMT")) +}) -params <- read_params(' +params = read_params(' --- params: file1: @@ -57,13 +53,12 @@ params: --- ' ) -assert(params[[1]]$name == 'file1') -assert(params[[1]]$value == 'data1.csv') +assert('sub-option params parsing works', { + (params[[1]]$name %==% 'file1') + (params[[1]]$value %==% 'data1.csv') +}) - -## test parameters with length(value) > 1 ---------------------------------- - -params <- read_params(' +params = read_params(' --- params: regions: @@ -71,13 +66,14 @@ params: --- ' ) -assert(length(params[[1]]$value) == 2) -assert(params[[1]]$value[[2]] == 'South') - +assert('prams with length > 1 works', { + (length(params[[1]]$value) %==% 2L) + (params[[1]]$value[[2]] %==% 'South') +}) ## test including additional parameter attributes -------------------------- -params <- read_params(' +params = read_params(' --- params: regions: @@ -87,13 +83,12 @@ params: --- ' ) -assert(identical(params[[1]]$choices, c('North', 'South', 'East', 'West'))) -assert(params[[1]]$label == "Select Regions") - - -## test y/Y/n/N ------------------------------------------------------------ +assert('other types of params can be parsed', { + (params[[1]]$choices %==% c('North', 'South', 'East', 'West')) + (params[[1]]$label %==% "Select Regions") +}) -params <- read_params(' +params = read_params(' --- params: x: 1 @@ -106,15 +101,12 @@ params: ' ) -assert( - 'y/Y/n/N are not converted to booleans', - identical(unname(unlist(lapply(params, `[[`, 'name'))), c('x', 'y', 'z', 'n', 'Y', 'N')) -) - +assert('y/Y/n/N are not converted to booleans', { + (unname(unlist(lapply(params, `[[`, 'name'))) %==% c('x', 'y', 'z', 'n', 'Y', 'N')) +}) -## test handling of expressions -------------------------------------------- -params <- read_params(' +params = read_params(' --- params: today: !r Sys.Date() @@ -123,26 +115,26 @@ params: --- ' ) -assert(!is.null(params[[1]]$expr)) -assert('Date' %in% class(params[[1]]$value)) -assert(!is.null(params[[2]]$expr)) -assert('POSIXct' %in% class(params[[2]]$value)) -assert(is.null(params[[3]]$expr)) - -## test handling of unevaluated expressions -------------------------------------------- +assert('params as expressions can be parsed', { + (!is.null(params[[1]]$expr)) + (inherits(params[[1]]$value, 'Date')) + (!is.null(params[[2]]$expr)) + (inherits(params[[2]]$value, 'POSIXct')) + (is.null(params[[3]]$expr)) +}) -params <- read_params(' +params = read_params(' --- params: today: !r Sys.Date() --- ', evaluate = FALSE) -assert(identical(params$today$expr, "Sys.Date()")) -assert(identical(class(params$today$value), "expression")) +assert('unevaluated expressions in params can be parsed', { + (params$today$expr %==% "Sys.Date()") + (class(params$today$value) %==% "expression") +}) -## test handling of yaml parameters -------------------------------------------- - -params <- read_params_yaml(' +params = read_params_yaml(' params: x: 1 today: !r Sys.Date() @@ -152,19 +144,10 @@ params: map: { value: { a: 1, b: 2 } } ') # The direct map value is not supported; an explicit value field is necessary -assert(params$x$value == 1) -assert(identical(class(params$x$value), "integer")) -assert(identical(params$today$expr, "Sys.Date()")) -assert('Date' %in% class(params$today$value)) -assert(identical(params$posixlt$expr, 'strptime("2015-01-01", format = "%Y-%m-%d")')) -assert('POSIXlt' %in% class(params$posixlt$value)) - -## test handling of unevaluated yaml parameters -------------------------------------------- - -params <- read_params_yaml(' -params: - today: !r Sys.Date() -', evaluate = FALSE) -assert(identical(params$today$expr, "Sys.Date()")) -assert(identical(class(params$today$value), "expression")) - +assert('yaml parameters can be handled', { + (params$x$value %==% 1L) + (params$today$expr %==% "Sys.Date()") + (inherits(params$today$value, 'Date')) + (params$posixlt$expr %==% 'strptime("2015-01-01", format = "%Y-%m-%d")') + (inherits(params$posixlt$value, 'POSIXlt')) +}) diff --git a/tests/testit/test-parser.R b/tests/testit/test-parser.R index 1033e0a92b..fa6f73bc83 100644 --- a/tests/testit/test-parser.R +++ b/tests/testit/test-parser.R @@ -1,82 +1,55 @@ library(testit) -assert( - 'parse_params() parses chunk options to a list', - identical(parse_params('a-s-d,b=TRUE,c=def'), alist(label = 'a-s-d', b = TRUE, c = def)), - has_error(parse_params('a,b')), - has_error(parse_params('a,b,c=qwer')), - identical(parse_params('a,opt=c(1,3,5)'), alist(label = 'a', opt = c(1, 3, 5))), - identical(parse_params('label="xx",opt=zz'), alist(label = 'xx', opt = zz)), - identical(parse_params('label=foo'), alist(label = 'foo')), - identical(parse_params('a,b=2,c="qwer",asdf="efg"'), - alist(label = 'a', b = 2, c = 'qwer', asdf = 'efg')), - identical(parse_params('2a'), alist(label = '2a')), - identical(parse_params('abc-function,fig.path="foo/bar-"'), - alist(label = 'abc-function', fig.path = "foo/bar-")) -) - opts_knit$set(out.format = 'markdown') -assert( - 'parse_params() parses the language engine from ```{lang}', - identical( - parse_block(NULL, '', 'r, foo, a=1,')$params, - alist(label = 'foo', a = 1) - ), - identical( - parse_block(NULL, '', 'Rcpp, foo, a=1,')$params, - alist(label = 'foo', a = 1, engine = 'Rcpp') - ) -) +assert('parse_block() parses the language engine from ```{lang}', { + (parse_block(NULL, '', 'r, foo, a=1,')$params %==% alist(label = 'foo', a = 1)) + (parse_block(NULL, '', 'Rcpp, foo, a=1,')$params %==% alist(label = 'foo', a = 1, engine = 'Rcpp')) +}) res = split_file( c('abc', '```{r foo}', '1+1', '```{r bar}', '2+2', '```', 'def'), patterns = all_patterns$md ) -assert( - 'split_file() treats ``` as part of code chunk instead of beginning of text chunk', +assert('split_file() treats ``` as part of code chunk instead of beginning of text chunk', { # the foo chunk does not have a closing mark - knit_code$get('foo') == '1+1', - knit_code$get('bar') == '2+2', + (c(knit_code$get('foo')) %==% '1+1') # use c() to drop attributes + (c(knit_code$get('bar')) %==% '2+2') # before knitr v1.6, the text chunk was c('', 'def') - identical(res[[4]][['input']], 'def') -) + (res[[4]][['input']] %==% 'def') +}) opts_knit$restore() knit_code$restore(); knit_concord$restore() res = parse_inline(c('aaa \\Sexpr{x}', 'bbb \\Sexpr{NA} and \\Sexpr{1+2}', 'another expression \\Sexpr{rnorm(10)}'), all_patterns$rnw) -assert( - 'parse_inline() parses inline text', - identical(res$code, c('x', 'NA', '1+2', 'rnorm(10)')), - identical(nchar(res$input), 81L), +assert('parse_inline() parses inline text', { + (res$code %==% c('x', 'NA', '1+2', 'rnorm(10)')) + (nchar(res$input) %==% 81L) # empty inline code is not recognized - identical(parse_inline('\\Sexpr{}', all_patterns$rnw)$code, character(0)), + (parse_inline('\\Sexpr{}', all_patterns$rnw)$code %==% character(0)) # can use > in HTML inline code - identical(parse_inline('', all_patterns$html)$code, ' "" ') -) + (parse_inline('', all_patterns$html)$code %==% ' "" ') +}) res = parse_inline('inline expressions `r pi+1`, +r cos(0)+ in AsciiDoc', all_patterns$asciidoc) -assert( - 'both `r expression` and +r expression+ work for AsciiDoc', - identical(res$code, c('pi+1', 'cos(0)')) -) +assert('both `r expression` and +r expression+ work for AsciiDoc', { + (res$code %==% c('pi+1', 'cos(0)')) +}) knit_code$restore() read_chunk(lines = c('1+1')) -assert( - 'read_chunk() does not discard code without chunk headers', - identical(knit_code$get(), list('unnamed-chunk-1' = '1+1')) -) +assert('read_chunk() does not discard code without chunk headers', { + (knit_code$get() %==% list('unnamed-chunk-1' = '1+1')) +}) knit_code$restore() read_chunk(lines = c('# ---- foo ----', '1+1')) -assert( - 'read_chunk() can identify chunk labels', - identical(knit_code$get(), list(foo = '1+1')) -) +assert('read_chunk() can identify chunk labels', { + (knit_code$get() %==% list(foo = '1+1')) +}) knit_code$restore() @@ -87,12 +60,10 @@ knit_code$restore(list( )) pc = function(x) parse_chunk(x, all_patterns$rnw$ref.chunk) -assert( - 'parse_chunk() preserves indentation', - identical(pc(c('3*3', '<>', ' <>', 'if (T)', ' <>')), - c("3*3", "1+1", " 2-2", "if (T)", " 1+1" )), - identical(pc('<>'), c('function() {', ' if (T)', ' 1+1', '}')) -) +assert('parse_chunk() preserves indentation', { + (pc(c('3*3', '<>', ' <>', 'if (T)', ' <>')) %==% c("3*3", "1+1", " 2-2", "if (T)", " 1+1" )) + (pc('<>') %==% c('function() {', ' if (T)', ' 1+1', '}')) +}) knit_code$restore() @@ -100,18 +71,18 @@ knit_code$restore() knit_code$restore(list(a = '1+1')) -assert( - !has_error(parse_block(NULL, '', 'label = "a"')), - has_error(parse_block('2+2', '', 'label = "a"')), - has_error(parse_block(NULL, '','label = "a", code = "2+2"')), - has_error(parse_block(NULL, '','label = "a", file = "dummy.R"')) -) +assert('duplicated labels are not allowed by default', { + (!has_error(parse_block(NULL, '', 'label = "a"'))) + (has_error(parse_block('2+2', '', 'label = "a"'))) + (has_error(parse_block(NULL, '','label = "a", code = "2+2"'))) + (has_error(parse_block(NULL, '','label = "a", file = "dummy.R"'))) +}) op = options(knitr.duplicate.label = 'allow') -assert( - !has_error(parse_block('2+2', '', 'label = "a"')), - !has_error(parse_block(NULL, '','label = "a", code = "2+2"')), - !has_error(parse_block(NULL, '','label = "a", file = "dummy.R"')) -) +assert('duplicated labels are allowed after setting an option', { + (!has_error(parse_block('2+2', '', 'label = "a"'))) + (!has_error(parse_block(NULL, '','label = "a", code = "2+2"'))) + (!has_error(parse_block(NULL, '','label = "a", file = "dummy.R"'))) +}) options(op) knit_code$restore() diff --git a/tests/testit/test-sql.R b/tests/testit/test-sql.R index b65c815fb6..50f20ee74a 100644 --- a/tests/testit/test-sql.R +++ b/tests/testit/test-sql.R @@ -1,34 +1,31 @@ library(testit) -assert( - !is_sql_update_query('SELECT 1'), - !is_sql_update_query('SELECT * FROM foo'), - !is_sql_update_query(' SELECT 1'), - !is_sql_update_query('\nSELECT 1'), - !is_sql_update_query('\tSELECT 1') -) - -assert(!is_sql_update_query(c('-- Some SQL', 'SELECT 1'))) - -assert(!is_sql_update_query(c('/* ', ' Some SQL', '*/', 'SELECT 1'))) - -assert(!is_sql_update_query(c(' /* ', ' Some SQL', ' */', 'SELECT 1'))) - -assert(is_sql_update_query('UPDATE foo SET a=1')) -assert(is_sql_update_query(' UPDATE foo SET a=1')) -assert(is_sql_update_query('\n\nUPDATE foo SET a=1')) -assert(is_sql_update_query('\tUPDATE foo SET a=1')) -assert(is_sql_update_query('DELETE FROM foo')) -assert(is_sql_update_query('INSERT INTO foo values(1)')) - -assert(is_sql_update_query(c('-- SELECT 1', 'INSERT INTO foo values(1)'))) - -assert(is_sql_update_query(c('/*SELECT 1*/', ' INSERT INTO foo values(1)'))) - -assert(is_sql_update_query(c( - '/*', ' Insert records into table', '*/', '', ' INSERT INTO foo values(1)' -))) - -assert(is_sql_update_query('update foo set a=1')) -assert(is_sql_update_query('delete from foo')) -assert(is_sql_update_query('insert into foo values(1)')) +assert('validation of sql queries works', { + (!is_sql_update_query('SELECT 1')) + (!is_sql_update_query('SELECT * FROM foo')) + (!is_sql_update_query(' SELECT 1')) + (!is_sql_update_query('\nSELECT 1')) + (!is_sql_update_query('\tSELECT 1')) + (!is_sql_update_query(c('-- Some SQL', 'SELECT 1'))) + (!is_sql_update_query(c('/* ', ' Some SQL', '*/', 'SELECT 1'))) + (!is_sql_update_query(c(' /* ', ' Some SQL', ' */', 'SELECT 1'))) + + (is_sql_update_query('UPDATE foo SET a=1')) + (is_sql_update_query(' UPDATE foo SET a=1')) + (is_sql_update_query('\n\nUPDATE foo SET a=1')) + (is_sql_update_query('\tUPDATE foo SET a=1')) + (is_sql_update_query('DELETE FROM foo')) + (is_sql_update_query('INSERT INTO foo values(1)')) + + (is_sql_update_query(c('-- SELECT 1', 'INSERT INTO foo values(1)'))) + + (is_sql_update_query(c('/*SELECT 1*/', ' INSERT INTO foo values(1)'))) + + (is_sql_update_query(c( + '/*', ' Insert records into table', '*/', '', ' INSERT INTO foo values(1)' + ))) + + (is_sql_update_query('update foo set a=1')) + (is_sql_update_query('delete from foo')) + (is_sql_update_query('insert into foo values(1)')) +}) diff --git a/tests/testit/test-templates.R b/tests/testit/test-templates.R index e292d5fb5e..dc29277ef9 100644 --- a/tests/testit/test-templates.R +++ b/tests/testit/test-templates.R @@ -3,26 +3,33 @@ library(testit) template = 'The value of a is {{a}}, so a + 1 is {{a + 1}}' exp = knit_expand(text = template, a = 10) act = 'The value of a is 10, so a + 1 is 11' -assert('templates use data specified', exp == act) +assert('templates use data specified', { + (exp %==% act) +}) template = 'The value of a is <% a %>, so a + 1 is <% a + 1 %>' exp = knit_expand(text = template, a = 10, delim = c("<%", "%>")) act = 'The value of a is 10, so a + 1 is 11' -assert('templates respect custom delimiter pairs', exp == act) +assert('templates respect custom delimiter pairs', { + (exp %==% act) +}) template = 'hello $(LETTERS[24]) and $(pi)!' exp = knit_expand(text = template, delim = c("$(", ")")) act = "hello X and 3.14159265358979!" -assert('templates respect pypi delimiters', exp == act) +assert('templates respect pypi delimiters', { + (exp %==% act) +}) template = 'The value of a is <% a %>, so a + 1 is <% a + 1 %>' -assert( - 'error is thrown when delimiter is not a pair', - has_error(knit_expand(text = template, a = 10, delim = '<%')) -) +assert('error is thrown when delimiter is not a pair', { + (has_error(knit_expand(text = template, a = 10, delim = '<%'))) +}) template = 'The value of a is {{a}}, and b + 1 is {{b + 1}}' b = -1.21 exp = knit_expand(text = template, a = 10) act = "The value of a is 10, and b + 1 is -0.21" -assert('templates use data from parent frame', exp == act) +assert('templates use data from parent frame', { + (exp %==% act) +}) diff --git a/tests/testit/test-utils.R b/tests/testit/test-utils.R index 5d048f2701..c99d6abf81 100644 --- a/tests/testit/test-utils.R +++ b/tests/testit/test-utils.R @@ -39,7 +39,7 @@ assert('format_sci() coerces non-numeric and non-double values to characters', { # https://github.com/yihui/knitr/issues/1625 assert('format_sci() does not convert roman numerals to arabic numerals', { - format_sci(as.roman(c(1, 4, 7, 33, 100))) %==% c('I', 'IV', 'VII', 'XXXIII', 'C') + (format_sci(as.roman(c(1, 4, 7, 33, 100))) %==% c('I', 'IV', 'VII', 'XXXIII', 'C')) }) assert('format_sci() for Rnw does not add \\ensuremath{} at all', {