| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('prose_index() works', { |
| 4 |
x = c('a', '```', 'b', '```', 'c') |
| 5 |
(prose_index(x) %==% c(1L, 5L)) |
| 6 |
|
| 7 |
x = c('a', '````', '```r', '1+1', '```', '````', 'c') |
| 8 |
(prose_index(x) %==% c(1L, 7L)) |
| 9 |
|
| 10 |
x = c('a', '``', 'b', '``', 'c') |
| 11 |
(prose_index(x) %==% seq_along(x)) |
| 12 |
|
| 13 |
# a character vector of length zero |
| 14 |
(prose_index(character()) %==% integer()) |
| 15 |
|
| 16 |
# one backbrick |
| 17 |
x = c('`', 'a', '`') |
| 18 |
(prose_index(x) %==% seq_along(x)) |
| 19 |
|
| 20 |
# two backbrick |
| 21 |
x = c('``', 'a', '``') |
| 22 |
(prose_index(x) %==% seq_along(x)) |
| 23 |
|
| 24 |
# no code fences |
| 25 |
x = c('a', 'b') |
| 26 |
(prose_index(x) %==% seq_along(x)) |
| 27 |
|
| 28 |
# two code fences |
| 29 |
x = c('```', 'b', '```', '```', 'd', '```') |
| 30 |
(prose_index(x) %==% integer()) |
| 31 |
|
| 32 |
# code fences commented out |
| 33 |
x = c('```', 'b', '```', '<!--```', 'd', '```-->') |
| 34 |
(prose_index(x) %==% 4:6) |
| 35 |
|
| 36 |
# if the code fences are not balanced |
| 37 |
x = c('a', '```', 'b', '``', 'c') |
| 38 |
(has_warning(prose_index(x))) |
| 39 |
(prose_index(x) %==% seq_along(x)) |
| 40 |
|
| 41 |
# recognize ``` ``text`` ``` |
| 42 |
x = c('a', '``` md', 'b', '```', '``` ``text`` ```') |
| 43 |
(prose_index(x) %==% c(1L, 5L)) |
| 44 |
|
| 45 |
# <pre> should also be treated as code blocks |
| 46 |
x = c('<pre><code>```', '```', '```', '</code></pre>') |
| 47 |
(prose_index(x) %==% integer()) |
| 48 |
}) |
| 49 |
|
| 50 |
|
| 51 |
assert('protect_math() puts inline math expressions in backticks', { |
| 52 |
(protect_math('$x$') %==% '`\\(x\\)`') |
| 53 |
(protect_math('$x$', '===') %==% '`===\\(x\\)===`') |
| 54 |
(protect_math('($x$)') %==% '(`\\(x\\)`)') |
| 55 |
(protect_math(' $x$') %==% ' `\\(x\\)`') |
| 56 |
(protect_math('.$x$') %==% '.$x$') |
| 57 |
(protect_math('$`x`$') %==% '$`x`$') |
| 58 |
(protect_math('hi $x$ a') %==% 'hi `\\(x\\)` a') |
| 59 |
(protect_math('$ a $') %==% '$ a $') |
| 60 |
(protect_math(' $a$') %==% ' `\\(a\\)`') |
| 61 |
(protect_math('$ x$') %==% '$ x$') |
| 62 |
(protect_math('$x $') %==% '$x $') |
| 63 |
(protect_math('b$a$') %==% 'b$a$') # no space before $; unlikely to be math |
| 64 |
(protect_math('`$a$`') %==% '`$a$`') |
| 65 |
(protect_math('hi $x$9') %==% 'hi $x$9') |
| 66 |
(protect_math('$500 $600') %==% '$500 $600') |
| 67 |
|
| 68 |
(protect_math('$$a$$') %==% '`$$a$$`') |
| 69 |
(protect_math('$$a$') %==% '$$a$') |
| 70 |
(protect_math('($$a$$)') %==% '(`$$a$$`)') |
| 71 |
(protect_math(' $$a$$') %==% ' `$$a$$`') |
| 72 |
(protect_math('.$$a$$') %==% '.$$a$$') |
| 73 |
(protect_math('hi $$\alpha$$') %==% 'hi `$$\alpha$$`') |
| 74 |
(protect_math('hi $$\alpha $$') %==% 'hi $$\alpha $$') |
| 75 |
(protect_math('hi $$ \alpha$$') %==% 'hi $$ \alpha$$') |
| 76 |
(protect_math('hi $$\alpha$$ and $$ \alpha$$') %==% 'hi `$$\alpha$$` and $$ \alpha$$') |
| 77 |
(protect_math(c('$$a', 'b$$')) %==% c('`$$a', 'b$$`')) |
| 78 |
(protect_math(c('$$a', 'b$$'), 'asdf') %==% c('`asdf$$a', 'b$$asdf`')) |
| 79 |
(protect_math(c('$$a', 'b$$'), use_block = TRUE) %==% c('```{.md-math}\n$$a', 'b$$\n```')) |
| 80 |
(protect_math(c('$$a', 'b$$'), 'asdf', use_block = TRUE) %==% c('```{.md-math .asdf}\n$$a', 'b$$\n```')) |
| 81 |
(protect_math(c(' $$a', ' b$$'), use_block = TRUE) %==% c(' ```{.md-math}\n $$a', ' b$$\n ```')) |
| 82 |
}) |
| 83 |
|
| 84 |
|
| 85 |
assert('block_attr(x) turns a character vector into Pandoc attributes', { |
| 86 |
(block_attr(NULL) %==% '') |
| 87 |
(block_attr('.a') %==% ' {.a}') |
| 88 |
(block_attr('.a b="11"') %==% ' {.a b="11"}') |
| 89 |
(block_attr(c('.a', 'b="11"')) %==% ' {.a b="11"}') |
| 90 |
}) |
| 91 |
|
| 92 |
assert('make_fence() uses the right number of fence characters', { |
| 93 |
(make_fence('1+1') %==% '```') |
| 94 |
(make_fence(c('1+1', '`````')) %==% '``````') |
| 95 |
(make_fence(':::') %==% '```') |
| 96 |
(make_fence(':::', ':') %==% '::::') |
| 97 |
}) |
| 98 |
|
| 99 |
assert('fenced_block() wraps content inside fences', { |
| 100 |
(fenced_block('1+1') %==% c('', '```', '1+1', '```')) |
| 101 |
(fenced_block('<i>info</i>', '=html') %==% c('', '``` {=html}', '<i>info</i>', '```')) |
| 102 |
(fenced_block('1+1', c('.lang', '#id', 'foo="BAR"')) %==% c('', '``` {.lang #id foo="BAR"}', '1+1', '```')) |
| 103 |
}) |
| 104 |
|
| 105 |
assert('fenced_div() uses colons as fence character', { |
| 106 |
res = fenced_div('content') |
| 107 |
(res %==% c('', ':::', 'content', ':::')) |
| 108 |
}) |
| 109 |
|
| 110 |
assert('md_table() generates a Markdown pipe table', { |
| 111 |
# basic usage |
| 112 |
x = data.frame(a = 1:3, b = c('x', 'y', 'z'), stringsAsFactors = FALSE) |
| 113 |
res = md_table(x) |
| 114 |
(length(res) %==% 5L) # header + separator + 3 rows |
| 115 |
(grepl('^\\|', res[1])) # starts with pipe |
| 116 |
(grepl('\\|$', res[1])) # ends with pipe |
| 117 |
# numeric column is right-aligned |
| 118 |
(grepl('--:', res[2])) |
| 119 |
|
| 120 |
# empty columns |
| 121 |
(md_table(data.frame()) %==% character()) |
| 122 |
|
| 123 |
# error for non-2D objects |
| 124 |
(has_error(md_table(1:5))) |
| 125 |
|
| 126 |
# NA values in table |
| 127 |
x2 = data.frame(a = c(1, NA, 3), b = c('x', 'y', NA)) |
| 128 |
res2 = md_table(x2) |
| 129 |
(any(grepl('NA', res2) | grepl('', res2))) # NA shown as empty by default |
| 130 |
|
| 131 |
# row names (non-numeric) are included as first column |
| 132 |
x3 = data.frame(a = 1:2, row.names = c('r1', 'r2')) |
| 133 |
res3 = md_table(x3) |
| 134 |
(grepl('r1', res3[3])) # first data row (after header + separator) contains r1 |
| 135 |
|
| 136 |
# limit parameter |
| 137 |
x4 = data.frame(a = 1:10) |
| 138 |
res4 = md_table(x4, limit = 4) |
| 139 |
(length(res4) < 12L) # fewer rows than full table |
| 140 |
}) |
| 141 |
|
| 142 |
assert('fenced_div() uses colons and supports attributes', { |
| 143 |
(fenced_div('text', '.note') %==% c('', '::: {.note}', 'text', ':::')) |
| 144 |
}) |
| 145 |
|
| 146 |
assert('md_table() with newlines, NA, digits, and pipes', { |
| 147 |
x = data.frame(a = 'line1\nline2', stringsAsFactors = FALSE) |
| 148 |
(!any(grepl('\n', md_table(x)))) |
| 149 |
x = data.frame(a = c(1, NA)) |
| 150 |
(any(grepl('N/A', md_table(x, na = 'N/A')))) |
| 151 |
x = data.frame(a = 3.14159265) |
| 152 |
res = md_table(x, digits = 2) |
| 153 |
(any(grepl('3.14', res))) |
| 154 |
(!any(grepl('3.1415', res))) |
| 155 |
x = data.frame(a = 'a|b', stringsAsFactors = FALSE) |
| 156 |
(any(grepl('\\\\[|]', md_table(x)))) |
| 157 |
}) |
| 158 |
|
| 159 |
assert('md_table() escapes special Markdown characters', { |
| 160 |
x = data.frame(a = '<TaskRegr:mtcars[0]>', stringsAsFactors = FALSE) |
| 161 |
res = md_table(x, escape = TRUE) |
| 162 |
(any(grepl('\\\\<', res))) |
| 163 |
(any(grepl('\\\\\\[', res))) |
| 164 |
# escape by column name |
| 165 |
x = data.frame(a = '*bold*', b = '*bold*', stringsAsFactors = FALSE) |
| 166 |
res = md_table(x, escape = 'a') |
| 167 |
(any(grepl('\\\\[*]', res))) |
| 168 |
# column b should not be escaped |
| 169 |
(grepl('\\*bold\\*\\|$', res[3])) |
| 170 |
# escape = FALSE (default) does not escape |
| 171 |
(!any(grepl('\\\\<', md_table(data.frame(a = '<x>'), escape = FALSE)))) |
| 172 |
}) |
| 173 |
|
| 174 |
assert('md_table() with column limit', { |
| 175 |
x = as.data.frame(matrix(1:20, nrow = 2)) |
| 176 |
(any(grepl('[.][.][.]', md_table(x, limit = c(0, 4))))) |
| 177 |
}) |
| 178 |
|
| 179 |
assert('md_table() with row limit shows ellipsis', { |
| 180 |
(any(grepl('vellip', md_table(data.frame(a = 1:20), limit = 4)))) |
| 181 |
}) |
| 182 |
|
| 183 |
assert('protect_math() does not touch code blocks', { |
| 184 |
(protect_math(c('```', '$x$', '```')) %==% c('```', '$x$', '```')) |
| 185 |
}) |
| 186 |
|
| 187 |
assert('protect_math() handles \\begin/\\end environments', { |
| 188 |
x = c('\\begin{equation}', 'x = y', '\\end{equation}') |
| 189 |
(protect_math(x) %==% c('`\\begin{equation}', 'x = y', '\\end{equation}`')) |
| 190 |
|
| 191 |
# nested environments: only outermost should be protected (#57) |
| 192 |
x = c('\\begin{equation}', '\\begin{cases}', 'x = y', '\\end{cases}', '\\end{equation}') |
| 193 |
(protect_math(x) %==% c('`\\begin{equation}', '\\begin{cases}', 'x = y', '\\end{cases}', '\\end{equation}`')) |
| 194 |
}) |
| 195 |
|
| 196 |
assert('block_attr() works', { |
| 197 |
(block_attr('#myid') %==% ' {#myid}') |
| 198 |
(block_attr('lang') %==% ' lang') |
| 199 |
}) |
| 200 |
|
| 201 |
assert('embed_file() produces an anchor tag with base64 data', { |
| 202 |
f = tempfile(fileext = '.txt'); writeLines('hello', f) |
| 203 |
link = embed_file(f) |
| 204 |
(inherits(link, 'xfun_html')) |
| 205 |
(grepl('<a ', link) && grepl('download=', link) && grepl('data:text/plain;base64,', link)) |
| 206 |
unlink(f) |
| 207 |
}) |
| 208 |
|
| 209 |
assert('embed_files() compresses multiple files into a zip link', { |
| 210 |
d = tempfile(); dir.create(d) |
| 211 |
f1 = file.path(d, 'a.txt'); writeLines('aaa', f1) |
| 212 |
f2 = file.path(d, 'b.txt'); writeLines('bbb', f2) |
| 213 |
link = embed_files(c(f1, f2)) |
| 214 |
(inherits(link, 'xfun_html')) |
| 215 |
(grepl('\\.zip', link)) |
| 216 |
unlink(d, recursive = TRUE) |
| 217 |
}) |
| 218 |
|
| 219 |
assert('embed_dir() compresses a directory into a zip link', { |
| 220 |
d = tempfile(); dir.create(d) |
| 221 |
writeLines('aaa', file.path(d, 'file.txt')) |
| 222 |
link = embed_dir(d) |
| 223 |
(inherits(link, 'xfun_html')) |
| 224 |
(grepl('\\.zip', link)) |
| 225 |
unlink(d, recursive = TRUE) |
| 226 |
}) |