| 1 |
library(testit) |
| 2 |
|
| 3 |
spin_text = function(..., format = "Rmd") { |
| 4 |
x = spin(text = c(...), knit = FALSE, format = format) |
| 5 |
xfun::split_lines(x) |
| 6 |
} |
| 7 |
|
| 8 |
assert("spin() detects lines for documentation", { |
| 9 |
(spin_text("#' test", "1 * 1", "#' test") %==% |
| 10 |
c("test", "", "```{r}", "1 * 1", "```", "", "test")) |
| 11 |
# a multiline string literal contains the pattern of doc or inline |
| 12 |
(spin_text("code <- \"", "#' test\"") %==% |
| 13 |
c("", "```{r}", "code <- \"", "#' test\"", "```", "")) |
| 14 |
(spin_text("code <- \"", "{{ 1 + 1 }}", "\"") %==% |
| 15 |
c("", "```{r}", "code <- \"", "{{ 1 + 1 }}", "\"", "```", "")) |
| 16 |
# a multiline symbol contains the pattern of doc or inline |
| 17 |
(spin_text("`", "#' test", "`") %==% |
| 18 |
c("", "```{r}", "`", "#' test", "`", "```", "")) |
| 19 |
(spin_text("`", "{{ 1 + 1 }}", "`") %==% |
| 20 |
c("", "```{r}", "`", "{{ 1 + 1 }}", "`", "```", "")) |
| 21 |
}) |
| 22 |
|
| 23 |
assert("spin() uses proper number of backticks", { |
| 24 |
(spin_text("{{ '`' }}") %==% c("``r '`' ``")) |
| 25 |
(spin_text("{{`x`}}") %==% c("``r `x` ``")) |
| 26 |
(spin_text("x <- '", "```", "'") %==% |
| 27 |
c("", "````{r}", "x <- '", "```", "'", "````", "")) |
| 28 |
}) |
| 29 |
|
| 30 |
assert("spin() generates code chunks with pipe comments `#|`", { |
| 31 |
( |
| 32 |
spin_text("", "#| echo: false", "#| message: false", "#| include: false", "1+1", "#| eval: false", "2 + 2", "", "#' Text") %==% |
| 33 |
c('', '```{r}', '#| echo: false', '#| message: false', '#| include: false', '1+1', '```', '```{r}', '#| eval: false', '2 + 2', '```', '', 'Text') |
| 34 |
) |
| 35 |
|
| 36 |
# https://github.com/yihui/knitr/issues/2314 |
| 37 |
( |
| 38 |
spin_text('#| echo: false', '1+1', '#| label: test', '1+1') %==% |
| 39 |
c('', '```{r}', '#| echo: false', '1+1', '```', '```{r}', '#| label: test', '1+1', '```', '') |
| 40 |
) |
| 41 |
|
| 42 |
# Has a `# %%` already |
| 43 |
( |
| 44 |
spin_text('# %%', '#| echo: false', '1+1', '#| label: test', '1+1') %==% |
| 45 |
c('', '```{r}', '#| echo: false', '1+1', '```', '```{r}', '#| label: test', '1+1', '```', '') |
| 46 |
) |
| 47 |
}) |