Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions tests/testthat/test-add_alttext.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,88 @@
# recursive = TRUE
# ), add = TRUE)
# })

test_that("add_alttext() adds tagged alt text and leaves existing text unchanged", {
dir <- tempfile()
dir.create(dir)
on.exit(unlink(dir, recursive = TRUE))

tex <- c(
"\\includegraphics[width=\\linewidth,keepaspectratio]{biomass.pdf}",
"\\caption{\\label{fig-biomass} Biomass}",
"\\includegraphics[width=\\linewidth,keepaspectratio,alt={'Existing text'}]{survey.pdf}",
"\\caption{\\label{fig-survey} Survey}"
)
writeLines(tex, file.path(dir, "source.tex"))
utils::write.csv(
data.frame(
label = c("biomass", "survey", "missing"),
alt_text = c("Biomass plot", "Updated text", "No figure")
),
file.path(dir, "alttext.csv"),
row.names = FALSE
)

add_alttext(
x = "source.tex",
dir = dir,
alttext_csv = file.path(dir, "alttext.csv"),
compile = FALSE,
rename = "renamed"
)

output <- readLines(file.path(dir, "renamed.tex"))
expect_true(any(grepl("keepaspectratio,alt={'Biomass plot'}", output, fixed = TRUE)))
expect_true(any(grepl("alt={'Existing text'}", output, fixed = TRUE)))
expect_false(any(grepl("Updated text", output, fixed = TRUE)))
expect_equal(readLines(file.path(dir, "source.tex")), tex)
})

test_that("add_alttext() supports the tooltip path", {
dir <- tempfile()
dir.create(dir)
on.exit(unlink(dir, recursive = TRUE))

writeLines(
c(
"\\pandocbounded{\\includegraphics[width=\\linewidth,keepaspectratio]{biomass.pdf}}",
"\\caption{\\label{fig-biomass} Biomass}"
),
file.path(dir, "source.tex")
)
utils::write.csv(
data.frame(label = "biomass", alt_text = "Biomass plot"),
file.path(dir, "alttext.csv"),
row.names = FALSE
)

add_alttext(
x = "source.tex",
dir = dir,
alttext_csv = file.path(dir, "alttext.csv"),
compile = FALSE,
tagged = FALSE
)

output <- readLines(file.path(dir, "source.tex"))
expect_true(any(grepl("Biomass plot", output, fixed = TRUE)))
})

test_that("add_alttext() reports missing input files", {
dir <- tempfile()
dir.create(dir)
on.exit(unlink(dir, recursive = TRUE))

expect_error(add_alttext(x = "missing.tex", dir = dir, compile = FALSE))

writeLines("text", file.path(dir, "source.tex"))
expect_error(add_alttext(
x = "source.tex",
dir = dir,
alttext_csv = file.path(dir, "missing.csv"),
compile = FALSE
))
})
#
# # test_that("add_alttext() renames altered tex file.", {
#
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-add_child.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("add_child() adds labeled child chunks and separates them", {
output <- add_child(
x = c("introduction.qmd", "tables.qmd"),
label = c("introduction", "tables")
)

expect_true(grepl("knit_child('introduction.qmd'", output, fixed = TRUE))
expect_true(grepl("knit_child('tables.qmd'", output, fixed = TRUE))
expect_true(grepl("{{< pagebreak >}}", output, fixed = TRUE))
expect_true(grepl("\\clearpage", output, fixed = TRUE))
})
61 changes: 61 additions & 0 deletions tests/testthat/test-add_tagging.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,64 @@
# recursive = TRUE
# ), add = TRUE)
# })

test_that("id_num_headers() tags tables and leaves documents without tables unchanged", {
no_tables <- c("\\section{Introduction}", "Text")
expect_identical(id_num_headers(no_tables), no_tables)

tables <- c(
"\\begin{table}",
"Table contents",
"\\end{table}",
"\\begin{longtable}",
"Long table contents",
"\\end{longtable}"
)
output <- id_num_headers(tables)

expect_equal(sum(grepl("table/header-rows", output, fixed = TRUE)), 2)
expect_lt(
match("\\tagpdfsetup{table/header-rows={1}}", output),
match("\\begin{table}", output)
)
expect_lt(
match("\\tagpdfsetup{table/header-rows={1}}", output),
match("\\begin{longtable}", output)
)
})

test_that("add_tagging() writes tagged TeX and accessibility metadata without compiling", {
dir <- tempfile()
dir.create(dir)
on.exit(unlink(dir, recursive = TRUE))

writeLines(
c(
"\\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}",
"\\hypersetup{linkcolor=}",
"\\IfFileExists{microtype.sty}{% use microtype if available",
"microtype line 2",
"microtype line 3",
"microtype line 4",
"\\begin{table}",
"Table contents",
"\\end{table}"
),
file.path(dir, "source.tex")
)

add_tagging(
x = "source.tex",
dir = dir,
compile = FALSE,
rename = "tagged"
)

output <- readLines(file.path(dir, "tagged.tex"))
expect_true(any(grepl("\\input{accessibility.tex}", output, fixed = TRUE)))
expect_true(any(grepl("\\hypersetup{linkcolor=.}", output, fixed = TRUE)))
expect_true(any(grepl("table/header-rows={1}", output, fixed = TRUE)))
expect_true(grepl("^% ", output[grep("microtype.sty", output)]))
expect_true(file.exists(file.path(dir, "accessibility.tex")))
expect_true(any(grepl("tagging = on", readLines(file.path(dir, "accessibility.tex")), fixed = TRUE)))
})
20 changes: 20 additions & 0 deletions tests/testthat/test-export_split_tbls.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@
# file.remove(fs::path(getwd(), "key_quantities.csv"))
# unlink(fs::path(getwd(), "tables"), recursive = T)
# })

test_that("export_split_tbls() exports a table that fits in one part", {
tables_dir <- tempfile()
dir.create(file.path(tables_dir, "tables"), recursive = TRUE)
on.exit(unlink(tables_dir, recursive = TRUE))

rda <- list(
table = gt::gt(data.frame(Year = 2020:2021, Estimate = c(1, 2)))
)
save(rda, file = file.path(tables_dir, "tables", "summary.rda"))

n_tables <- export_split_tbls(
tables_dir = tables_dir,
plot_name = "summary.rda",
essential_columns = 1
)

expect_equal(n_tables, 1)
expect_true(file.exists(file.path(tables_dir, "tables", "summary_split.rda")))
})
#
#
# test_that("Number of split tables is calculated correctly for 3 tables", {
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-format_quarto.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("format_quarto() builds PDF and HTML formatting", {
pdf <- format_quarto(format = "pdf", type = "pfmc")
pdf_sar <- format_quarto(format = "pdf", type = "sar")
html <- format_quarto(format = "html")

expect_true(grepl("support_files/pfmc.tex", pdf, fixed = TRUE))
expect_false(grepl("support_files/pfmc.tex", pdf_sar, fixed = TRUE))
expect_true(grepl("support_files/theme.scss", html, fixed = TRUE))
expect_false(grepl("support_files/pfmc.tex", html, fixed = TRUE))
})

test_that("format_quarto() reports unsupported formats", {
expect_error(format_quarto(format = "docx"), "not yet a functional format")
expect_error(format_quarto(format = "other"), "Invalid render format")
})
Loading