-
-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make spin() recognize
#|
comments as code chunks (#2320)
Co-authored-by: Yihui Xie <xie@yihui.name>
- Loading branch information
Showing
5 changed files
with
59 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,47 @@ | ||
library(testit) | ||
|
||
spin_w_tempfile = function(..., format = "Rmd") { | ||
tmp = tempfile(fileext = ".R") | ||
writeLines(c(...), tmp) | ||
spinned = spin(tmp, knit = FALSE, format = format) | ||
result = readLines(spinned) | ||
file.remove(c(tmp, spinned)) | ||
result | ||
spin_text = function(..., format = "Rmd") { | ||
x = spin(text = c(...), knit = FALSE, format = format) | ||
xfun::split_lines(x) | ||
} | ||
|
||
assert("spin() detects lines for documentation", { | ||
(spin_w_tempfile("#' test", "1 * 1", "#' test") %==% | ||
(spin_text("#' test", "1 * 1", "#' test") %==% | ||
c("test", "", "```{r}", "1 * 1", "```", "", "test")) | ||
# a multiline string literal contains the pattern of doc or inline | ||
(spin_w_tempfile("code <- \"", "#' test\"") %==% | ||
(spin_text("code <- \"", "#' test\"") %==% | ||
c("", "```{r}", "code <- \"", "#' test\"", "```", "")) | ||
(spin_w_tempfile("code <- \"", "{{ 1 + 1 }}", "\"") %==% | ||
(spin_text("code <- \"", "{{ 1 + 1 }}", "\"") %==% | ||
c("", "```{r}", "code <- \"", "{{ 1 + 1 }}", "\"", "```", "")) | ||
# a multiline symbol contains the pattern of doc or inline | ||
(spin_w_tempfile("`", "#' test", "`") %==% | ||
(spin_text("`", "#' test", "`") %==% | ||
c("", "```{r}", "`", "#' test", "`", "```", "")) | ||
(spin_w_tempfile("`", "{{ 1 + 1 }}", "`") %==% | ||
(spin_text("`", "{{ 1 + 1 }}", "`") %==% | ||
c("", "```{r}", "`", "{{ 1 + 1 }}", "`", "```", "")) | ||
}) | ||
|
||
assert("spin() uses proper number of backticks", { | ||
(spin_w_tempfile("{{ '`' }}") %==% c("``r '`' ``")) | ||
(spin_w_tempfile("{{`x`}}") %==% c("``r `x` ``")) | ||
(spin_w_tempfile("x <- '", "```", "'") %==% | ||
(spin_text("{{ '`' }}") %==% c("``r '`' ``")) | ||
(spin_text("{{`x`}}") %==% c("``r `x` ``")) | ||
(spin_text("x <- '", "```", "'") %==% | ||
c("", "````{r}", "x <- '", "```", "'", "````", "")) | ||
}) | ||
|
||
assert("spin() generates code chunks with pipe comments `#|`", { | ||
( | ||
spin_text("", "#| echo: false", "#| message: false", "#| include: false", "1+1", "#| eval: false", "2 + 2", "", "#' Text") %==% | ||
c('', '```{r}', '#| echo: false', '#| message: false', '#| include: false', '1+1', '```', '```{r}', '#| eval: false', '2 + 2', '```', '', 'Text') | ||
) | ||
|
||
# https://github.com/yihui/knitr/issues/2314 | ||
( | ||
spin_text('#| echo: false', '1+1', '#| label: test', '1+1') %==% | ||
c('', '```{r}', '#| echo: false', '1+1', '```', '```{r}', '#| label: test', '1+1', '```', '') | ||
) | ||
|
||
# Has a `# %%` already | ||
( | ||
spin_text('# %%', '#| echo: false', '1+1', '#| label: test', '1+1') %==% | ||
c('', '```{r}', '#| echo: false', '1+1', '```', '```{r}', '#| label: test', '1+1', '```', '') | ||
) | ||
}) |