| 1 |
library(testit) |
| 2 |
|
| 3 |
op = options(digits = 3, scipen = 0, knitr.digits.signif = TRUE) |
| 4 |
|
| 5 |
assert('format_sci() uses correct number of significant digits', { |
| 6 |
(format_sci(1) %==% '1') |
| 7 |
(format_sci(0) %==% '0') |
| 8 |
(format_sci(3.1415e2) %==% '314') |
| 9 |
(format_sci(3.1415) %==% '3.14') |
| 10 |
}) |
| 11 |
|
| 12 |
options(op) |
| 13 |
|
| 14 |
op = options(digits = 14, scipen = 0, knitr.digits.signif = TRUE) |
| 15 |
assert('format_sci() prints numerics at maximum number of significant digits', { |
| 16 |
(format_sci(3.14159265358979) %==% '3.1415926535898') |
| 17 |
}) |
| 18 |
options(op) |
| 19 |
|
| 20 |
op = options(digits = 4, scipen = 0) |
| 21 |
|
| 22 |
assert('format_sci() turns numbers into scientific notations', { |
| 23 |
(format_sci(c(1.84e8, 1e5, 2.34e3)) %==% c('1.84\\times 10^{8}', '10^{5}', '2340')) |
| 24 |
(format_sci(1.23456789 * 10^-5) %==% '1.2346\\times 10^{-5}') |
| 25 |
(format_sci(10^-5) %==% '10^{-5}') |
| 26 |
(format_sci(10^5) %==% '10^{5}') |
| 27 |
(format_sci(-10^-5) %==% '-10^{-5}') |
| 28 |
(format_sci(-10^5) %==% '-10^{5}') |
| 29 |
(format_sci(9.87654e6, 'html') %==% '9.8765 × 10<sup>6</sup>') |
| 30 |
(format_sci(9.87654e6, 'rst') %==% '9.8765 |times| 10 :sup:`6`') |
| 31 |
(format_sci(letters) %==% letters) |
| 32 |
(format_sci(NA_real_) %==% NA_character_) |
| 33 |
}) |
| 34 |
|
| 35 |
assert('format_sci() coerces non-numeric and non-double values to characters', { |
| 36 |
(format_sci(Sys.Date()) == as.character(Sys.Date())) |
| 37 |
(format_sci(1000000L) == '1000000') |
| 38 |
}) |
| 39 |
|
| 40 |
# https://github.com/yihui/knitr/issues/1625 |
| 41 |
assert('format_sci() does not convert roman numerals to arabic numerals', { |
| 42 |
(format_sci(as.roman(c(1, 4, 7, 33, 100))) %==% c('I', 'IV', 'VII', 'XXXIII', 'C')) |
| 43 |
}) |
| 44 |
|
| 45 |
assert('format_sci() for Rnw does not add \\ensuremath{} at all', { |
| 46 |
(!grepl('[\\]ensuremath', format_sci(c(1e4, 1.2345e10, 2 * pnorm(-(3:4)), -Inf)))) |
| 47 |
}) |
| 48 |
|
| 49 |
assert('the inline hook for Rnw applies \\ensuremath{} correctly', { |
| 50 |
(.inline.hook.tex(1e4) == '\\ensuremath{10^{4}}') |
| 51 |
(.inline.hook.tex(-Inf) == '\\ensuremath{-\\infty{}}') |
| 52 |
(.inline.hook.tex(-1.23) == '-1.23') |
| 53 |
(.inline.hook.tex(c(1.2345e10, 2 * pnorm(-(3:4)))) == |
| 54 |
"\\ensuremath{1.2345\\times 10^{10}}, 0.0027, \\ensuremath{6.3342\\times 10^{-5}}") |
| 55 |
}) |
| 56 |
|
| 57 |
assert('Infinity and NaN are formatted correctly', { |
| 58 |
(format_sci(-Inf) %==% '-\\infty{}') |
| 59 |
(format_sci(-Inf, 'html') %==% '-∞') |
| 60 |
(format_sci(-Inf, 'rst') %==% '-Inf') |
| 61 |
(format_sci(NaN) %==% 'NaN') |
| 62 |
}) |
| 63 |
|
| 64 |
assert('sanitize_fn() warns against spaces in filenames', { |
| 65 |
(has_warning(sanitize_fn('figure/a b'))) |
| 66 |
}) |
| 67 |
|
| 68 |
options(op) |
| 69 |
|
| 70 |
assert('fig_path() sanitizes paths', { |
| 71 |
(sanitize_fn('fig/foo') %==% 'fig/foo') |
| 72 |
(sanitize_fn('figure/a b c', FALSE) %==% 'figure/a_b__c') |
| 73 |
(sanitize_fn('fig space/a.b') %==% 'fig_space/a.b') |
| 74 |
(sanitize_fn('../c.d') %==% '../c.d') |
| 75 |
(sanitize_fn('./../c..d') %==% './../c..d') |
| 76 |
(sanitize_fn('C:/foo/bar') %==% 'C:/foo/bar') |
| 77 |
}) |
| 78 |
|
| 79 |
assert('fig_chunk() generates figure filenames for a code chunk', { |
| 80 |
(fig_chunk('foo') %==% 'figure/foo-1') |
| 81 |
(fig_chunk('foo', 'pdf') %==% 'figure/foo-1.pdf') |
| 82 |
(fig_chunk('foo', 'png', 2) %==% 'figure/foo-2.png') |
| 83 |
(fig_chunk('foo', 'svg', 1:5) %==% sprintf('figure/foo-%d.svg', 1:5)) |
| 84 |
(fig_chunk('foo', fig.path = 'my_figure/') %==% 'my_figure/foo-1') |
| 85 |
(fig_chunk('foo', '.pdf') %==%'figure/foo-1.pdf') |
| 86 |
}) |
| 87 |
|
| 88 |
assert('all_figs() generates all figure paths for a code chunk', { |
| 89 |
opts = list(fig.path = 'abc/', label = 'foo', fig.num = 3) |
| 90 |
(all_figs(opts, '.svg') %==% sprintf('abc/foo-%d.svg', 1:3)) |
| 91 |
(all_figs(opts, c('png', 'pdf')) %==% apply( |
| 92 |
expand.grid(1:3, c('.png', '.pdf')), 1, function(x) { |
| 93 |
paste0(c('abc/foo-', x), collapse = '') |
| 94 |
} |
| 95 |
)) |
| 96 |
}) |
| 97 |
|
| 98 |
assert('escape_latex() escapes special LaTeX characters', { |
| 99 |
(escape_latex('# $ % & ~ _ ^ \\ { }') %==% |
| 100 |
'\\# \\$ \\% \\& \\textasciitilde{} \\_ \\textasciicircum{} \\textbackslash{} \\{ \\}' |
| 101 |
) |
| 102 |
(escape_latex('a b', spaces = TRUE) %==% 'a b') |
| 103 |
(escape_latex('a b', spaces = TRUE) %==% 'a \\ \\ b') |
| 104 |
}) |
| 105 |
|
| 106 |
assert('indent_block() works when the first element is empty (#790)', { |
| 107 |
(indent_block(c('', 'a')) %==% c(' ', ' a')) |
| 108 |
(indent_block(c('', '')) %==% c(' ', ' ')) |
| 109 |
}) |
| 110 |
|
| 111 |
assert('current_input() returns NULL by default', { |
| 112 |
(is.null(current_input())) |
| 113 |
(suppressWarnings(is.null(current_input(TRUE)))) |
| 114 |
}) |
| 115 |
|
| 116 |
assert('color_def() generates LaTeX code to define a color variable', { |
| 117 |
(color_def(NA) %==% '') |
| 118 |
(color_def('red') %==% '\\definecolor{shadecolor}{rgb}{1, 0, 0}') |
| 119 |
(color_def('#00ff00') %==% '\\definecolor{shadecolor}{rgb}{0, 1, 0}') |
| 120 |
(color_def('.5,.6,.7', 'fgcolor') %==% '\\definecolor{fgcolor}{rgb}{.5, .6, .7}') |
| 121 |
}) |
| 122 |
|
| 123 |
opts = list( |
| 124 |
fig.cap = 'Figure "caption" <>.', fig.lp = 'Fig:', label = 'foo' |
| 125 |
) |
| 126 |
assert('.img.cap() generates the figure caption and alt attribute', { |
| 127 |
(.img.cap(list(fig.cap = NULL), FALSE) %==% "") |
| 128 |
(.img.cap(opts, FALSE) %==% opts$fig.cap) |
| 129 |
(.img.cap(opts, TRUE, TRUE) %==% 'Figure "caption" <>.') |
| 130 |
|
| 131 |
opts$fig.alt = 'Figure "alternative text" <>.' |
| 132 |
|
| 133 |
(.img.cap(opts, TRUE, TRUE) %==% 'Figure "alternative text" <>.') |
| 134 |
(.img.cap(opts, FALSE) %==% opts$fig.cap) |
| 135 |
|
| 136 |
(.img.cap(list(fig.cap = '', fig.alt = "alt"), FALSE) %==% "") |
| 137 |
(.img.cap(list(fig.cap = '', fig.alt = "alt"), TRUE) %==% "alt") |
| 138 |
}) |
| 139 |
|
| 140 |
z = as.strict_list(list(a = 1, aa = 2, bbb = 3)) |
| 141 |
assert('as.strict_list() does not allow partial matching', { |
| 142 |
(z$b %==% NULL) |
| 143 |
(z$bbb %==% 3) |
| 144 |
}) |
| 145 |
|
| 146 |
out = c('*hello*', raw_output('<special>content</special> *protect* me!'), '*world*') |
| 147 |
pre = extract_raw_output(out) |
| 148 |
pre$value = gsub('[*]([^*]+)[*]', '<em>\\1</em>', pre$value) # think this as Pandoc conversion |
| 149 |
# raw output was protected from the conversion (e.g. *protect* was not converted) |
| 150 |
assert('restore_raw_output() restores raw output', { |
| 151 |
(restore_raw_output(pre$value, pre$chunks) %==% |
| 152 |
'<em>hello</em>\n<special>content</special> *protect* me!\n<em>world</em>') |
| 153 |
}) |
| 154 |
|
| 155 |
assert('raw_block() returns a raw attribute block for Pandoc', { |
| 156 |
(c(raw_latex('\\emph{x}')) %==% '\n```{=latex}\n\\emph{x}\n```\n') |
| 157 |
(c(raw_html('<i>foo</i>')) %==% '\n```{=html}\n<i>foo</i>\n```\n') |
| 158 |
}) |
| 159 |
|
| 160 |
assert('block_class() turns a character vector into Pandoc attributes for code block classes', { |
| 161 |
(block_class(NULL) %==% NULL) |
| 162 |
(block_class('a') %==% 'a') |
| 163 |
(block_class('a', '.b') %==% '.a') |
| 164 |
(block_class('a b') %==% c('.a', '.b')) |
| 165 |
(block_class(c('a', 'b')) %==% c('.a', '.b')) |
| 166 |
}) |
| 167 |
|
| 168 |
assert('when collapse is TRUE, class.* and attr.* become NULL except for class.source and attr.source', { |
| 169 |
keys = unlist(lapply( |
| 170 |
c('class.', 'attr.'), paste0, c('source', 'output', 'message', 'warning', 'error') |
| 171 |
)) |
| 172 |
keys_source = c('class.source', 'attr.source') |
| 173 |
opts = fix_options(opts_chunk$merge(c(setNames(as.list(keys), keys), list(collapse = TRUE)))) |
| 174 |
(opts[keys_source] %==% as.list(setNames(keys_source, keys_source))) |
| 175 |
(!any(names(opts) %in% setdiff(keys, keys_source))) |
| 176 |
rm(keys, keys_source, opts) |
| 177 |
}) |
| 178 |
|
| 179 |
assert('default strip.white is conditional to collapse', { |
| 180 |
opts = opts_chunk$get(default = TRUE) |
| 181 |
(fix_options(opts)$strip.white %==% TRUE) |
| 182 |
opts$collapse = TRUE |
| 183 |
(fix_options(opts)$strip.white %==% FALSE) |
| 184 |
opts$strip.white = TRUE |
| 185 |
(fix_options(opts)$strip.white %==% TRUE) |
| 186 |
rm(opts) |
| 187 |
}) |
| 188 |
|
| 189 |
assert('pandoc_to gets the current Pandoc format', { |
| 190 |
opts = opts_knit$get(c('rmarkdown.pandoc.to', 'rmarkdown.pandoc.from')) |
| 191 |
opts_knit$delete(c("rmarkdown.pandoc.from", "rmarkdown.pandoc.to")) |
| 192 |
(pandoc_from() %==% 'markdown') |
| 193 |
(pandoc_to() %==% NULL) |
| 194 |
opts_knit$set(rmarkdown.pandoc.from = 'gfm', rmarkdown.pandoc.to = 'docx') |
| 195 |
(pandoc_from() %==% 'gfm') |
| 196 |
(pandoc_to() %==% 'docx') |
| 197 |
(pandoc_to('docx')) |
| 198 |
(!pandoc_to('pptx')) |
| 199 |
(pandoc_to(c('docx', 'pptx'))) |
| 200 |
opts_knit$set(opts) |
| 201 |
}) |
| 202 |
|
| 203 |
assert('comment_out() add prefix and newlines if asked', { |
| 204 |
(comment_out("a") %==% "## a\n") |
| 205 |
(comment_out("ab cd") %==% "## ab cd\n") |
| 206 |
(comment_out("ab cd\n") %==% "## ab cd\n") |
| 207 |
(comment_out("") %==% "## \n") |
| 208 |
(comment_out("\n") %==% "## \n") |
| 209 |
(comment_out(c("a", "b")) %==% c("## a\n", "## b\n")) |
| 210 |
(comment_out(c("a", "b"), which = 2) %==% c("a\n", "## b\n")) |
| 211 |
(comment_out("a", newline = FALSE) %==% "## a") |
| 212 |
(comment_out("a", prefix = "$") %==% "$ a\n") |
| 213 |
(comment_out("a", prefix = NULL) %==% "a\n") |
| 214 |
}) |
| 215 |
|
| 216 |
assert('remove_urls() removes the link', { |
| 217 |
(remove_urls(c('[a](b)', '[a b](c)')) %==% c('a', 'a b')) |
| 218 |
(remove_urls('a [b](c) d.') %==% 'a b d.') |
| 219 |
(remove_urls('a [b](c) d [e f+g](h) i.') %==% 'a b d e f+g i.') |
| 220 |
(remove_urls('a [b](c) `[d](e)` f.') %==% 'a b `[d](e)` f.') |
| 221 |
}) |
| 222 |
|
| 223 |
assert('options using `-` are converted to `.` and default value replaced', { |
| 224 |
opts = opts_chunk$merge(list('fig-cap' = 'caption', 'out-width' = 300)) |
| 225 |
(is.null(dot_names(opts)[['fig-cap']])) |
| 226 |
(is.null(dot_names(opts)[['out-width']])) |
| 227 |
(dot_names(opts)[['fig.cap']] %==% 'caption') |
| 228 |
(dot_names(opts)[['out.width']] %==% 300) |
| 229 |
rm(opts) |
| 230 |
}) |
| 231 |
|
| 232 |
assert('fig.format and fig.dpi', { |
| 233 |
opts = opts_chunk$merge(list('fig-format' = 'svg', 'fig-dpi' = 750)) |
| 234 |
(is.null(dot_names(opts)[['fig-format']])) |
| 235 |
(is.null(dot_names(opts)[['fig-dpi']])) |
| 236 |
(dot_names(opts)[['dev']] %==% 'svg') |
| 237 |
(dot_names(opts)[['dpi']] %==% 750) |
| 238 |
rm(opts) |
| 239 |
}) |
| 240 |
|
| 241 |
assert('options using `.` are converted to `-` and default value replaced', { |
| 242 |
opts = opts_chunk$merge(list('fig.cap' = 'caption', 'out.width' = 300)) |
| 243 |
(is.null(dash_names(opts)[['fig.cap']])) |
| 244 |
(is.null(dash_names(opts)[['out.width']])) |
| 245 |
(dash_names(opts)[['fig-cap']] %==% 'caption') |
| 246 |
(dash_names(opts)[['out-width']] %==% 300) |
| 247 |
rm(opts) |
| 248 |
}) |
| 249 |
|
| 250 |
assert('dev and dpi are convert to fig-format and fig-dpi', { |
| 251 |
opts = opts_chunk$merge(list('dev' = 'svg', 'dpi' = 750)) |
| 252 |
(is.null(dash_names(opts)[['dev']])) |
| 253 |
(is.null(dash_names(opts)[['dpi']])) |
| 254 |
(dash_names(opts)[['fig-format']] %==% 'svg') |
| 255 |
(dash_names(opts)[['fig-dpi']] %==% 750) |
| 256 |
rm(opts) |
| 257 |
}) |