test_that("special characters are escaped", {
out <- rd2html("a & b")
expect_equal(out, "a & b")
})
test_that("converts Rd unicode shortcuts", {
expect_snapshot(rd2html("``a -- b --- c''"))
})
test_that("simple tags translated to known good values", {
# Simple insertions
expect_equal(rd2html("\\ldots"), "...")
expect_equal(rd2html("\\dots"), "...")
expect_equal(rd2html("\\R"), "R")
expect_equal(rd2html("\\cr"), "
")
"Macros"
expect_equal(rd2html("\\newcommand{\\f}{'f'} \\f{}"), "'f'")
expect_equal(rd2html("\\renewcommand{\\f}{'f'} \\f{}"), "'f'")
})
test_that("comments converted to html", {
expect_equal(rd2html("a\n%b\nc"), c("a", "", "c"))
})
test_that("simple wrappers work as expected", {
expect_equal(rd2html("\\strong{x}"), "x")
expect_equal(rd2html("\\strong{\\emph{x}}"), "x")
})
test_that("subsection generates h3", {
expect_snapshot(cli::cat_line(rd2html("\\subsection{A}{B}")))
})
test_that("subsection generates h3", {
expect_snapshot(cli::cat_line(rd2html("\\subsection{A}{
p1
p2
}")))
})
test_that("subsection generates generated anchor", {
text <- c("
a | b |
a |
b |
c |
b
", "[1] 3", "") ) expect_equal( rd2html("\\Sexpr[results=verbatim]{cat(42)}"), c("
", "42", "") ) expect_equal( rd2html("\\Sexpr[results=verbatim]{cat('42!\n'); 3}"), c("
", "42!", "[1] 3", "") ) }) test_that("Sexpr can contain multiple expressions", { local_context_eval() expect_equal(rd2html("\\Sexpr{a <- 1; a}"), "1") }) test_that("Sexprs with multiple args are parsed", { local_context_eval() expect_equal(rd2html("\\Sexpr[results=hide,stage=build]{1}"), character()) }) test_that("Sexprs in file share environment", { local_context_eval() expect_equal(rd2html("\\Sexpr{x <- 1}\\Sexpr{x}"), c("1", "1")) local_context_eval() expect_error(rd2html("\\Sexpr{x}"), "not found") }) test_that("Sexprs run from package root", { local_context_eval(src_path = test_path("assets/reference")) # \packageTitle is built in macro that uses DESCRIPTION expect_equal( rd2html("\\packageTitle{testpackage}"), "A test package" ) }) # links ------------------------------------------------------------------- test_that("simple links generate ", { expect_equal( rd2html("\\href{http://bar.com}{BAR}"), "BAR" ) expect_equal( rd2html("\\email{foo@bar.com}"), "foo@bar.com" ) expect_equal( rd2html("\\url{http://bar.com}"), "http://bar.com" ) }) test_that("can convert cross links to online documentation url", { expect_equal( rd2html("\\link[base]{library}"), a("library", href = "https://rdrr.io/r/base/library.html") ) }) test_that("can convert cross links to the same package (#242)", { withr::local_options(list( "downlit.package" = "test", "downlit.topic_index" = c(x = "y", z = "z"), "downlit.rdname" = "z" )) expect_equal(rd2html("\\link{x}"), "x") expect_equal(rd2html("\\link[test]{x}"), "x") # but no self links expect_equal(rd2html("\\link[test]{z}"), "z") }) test_that("can parse local links with topic!=label", { withr::local_options(list( "downlit.topic_index" = c(x = "y") )) expect_equal(rd2html("\\link[=x]{z}"), "z") }) test_that("functions in other packages generates link to rdrr.io", { withr::local_options(list( "downlit.package" = "test", "downlit.topic_index" = c(x = "y", z = "z") )) expect_equal( rd2html("\\link[stats:acf]{xyz}"), a("xyz", downlit::href_topic("acf", "stats")) ) # Unless it's the current package expect_equal(rd2html("\\link[test:x]{xyz}"), "xyz") }) test_that("link to non-existing functions return label", { expect_equal(rd2html("\\link[xyzxyz:xyzxyz]{abc}"), "abc") expect_equal(rd2html("\\link[base:xyzxyz]{abc}"), "abc") }) test_that("code blocks autolinked to vignettes", { withr::local_options(list( "downlit.package" = "test", "downlit.article_index" = c("abc" = "abc.html") )) expect_equal( rd2html("\\code{vignette('abc')}"), "
vignette('abc')
"
)
})
test_that("link to non-existing functions return label", {
withr::local_options(list(
"downlit.package" = "test",
"downlit.topic_index" = c("TEST-class" = "test")
))
expect_equal(rd2html("\\linkS4class{TEST}"), "TEST")
})
test_that("bad specs throw errors", {
expect_snapshot(error = TRUE, {
rd2html("\\url{}")
rd2html("\\url{a\nb}")
rd2html("\\email{}")
rd2html("\\linkS4class{}")
})
})
# Paragraphs --------------------------------------------------------------
test_that("empty input gives empty output", {
expect_equal(flatten_para(character()), character())
})
test_that("empty lines break paragraphs", {
expect_equal(
flatten_para(rd_text("a\nb\n\nc")),
"a\nb
\nc
" ) }) test_that("indented empty lines break paragraphs", { expect_equal( flatten_para(rd_text("a\nb\n \nc")), "a\nb
\nc
" ) }) test_that("block tags break paragraphs", { out <- flatten_para(rd_text("a\n\\itemize{\\item b}\nc")) expect_equal(out, "a
b
c
") }) test_that("inline tags + empty line breaks", { out <- flatten_para(rd_text("a\n\n\\code{b}")) expect_equal(out, "a
\nb
a
\nb
One \nTwo
a
b
a
a
%>%
\n")
})
test_that("newlines are preserved in preformatted blocks", {
out <- flatten_para(rd_text("\\preformatted{^\n\nb\n\nc}"))
expect_equal(out, "^\n\nb\n\nc
\n")
})
test_that("spaces are preserved in preformatted blocks", {
out <- flatten_para(rd_text("\\preformatted{^\n\n b\n\n c}"))
expect_equal(out, "^\n\n b\n\n c
\n")
})
# Other -------------------------------------------------------------------
test_that("eqn", {
out <- rd2html(" \\eqn{\\alpha}{alpha}")
expect_equal(out, "\\(\\alpha\\)")
out <- rd2html(" \\eqn{x}")
expect_equal(out, "\\(x\\)")
})
test_that("deqn", {
out <- rd2html(" \\deqn{\\alpha}{alpha}")
expect_equal(out, "$$\\alpha$$")
out <- rd2html(" \\deqn{x}")
expect_equal(out, "$$x$$")
})
test_that("special", {
out <- rd2html("\\special{( \\dots )}")
expect_equal(out, "( ... )")
})
# figures -----------------------------------------------------------------
test_that("figures are converted to img", {
expect_equal(rd2html("\\figure{a}"), "")
expect_equal(rd2html("\\figure{a}{b}"), "")
expect_equal(
rd2html("\\figure{a}{options: height=1}"),
""
)
})
test_that("figures with multilines alternative text can be parsed", {
expect_equal(rd2html("\\figure{a}{blabla
blop}"), "")
})