-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created tests for new functions (export_rda and extract_caps_alttext)
- Loading branch information
1 parent
a5191ee
commit b45412c
Showing
2 changed files
with
162 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
test_that("export_rda works for figures", { | ||
# read in sample dataset | ||
dat <- utils::read.csv( | ||
system.file( | ||
"resources", | ||
"sample_data", | ||
"petrale_sole-after_2020.csv", | ||
package = "satf" | ||
) | ||
) | ||
|
||
topic_label <- "biomass" | ||
fig_or_table <- "figure" | ||
|
||
# run write_captions.R | ||
satf::write_captions(dat = dat, | ||
dir = getwd(), | ||
year = 2022) | ||
|
||
# extract this plot's caption and alt text | ||
caps_alttext <- extract_caps_alttext(topic_label = topic_label, | ||
fig_or_table = fig_or_table, | ||
dir = getwd()) | ||
|
||
# make a simple plot | ||
library(ggplot2) | ||
final <- ggplot2::ggplot(Orange, aes(circumference, age)) + | ||
ggplot2::geom_point() | ||
|
||
# export rda | ||
export_rda( | ||
final = final, | ||
caps_alttext = caps_alttext, | ||
rda_dir = getwd(), | ||
topic_label = topic_label, | ||
fig_or_table = fig_or_table | ||
) | ||
|
||
# expect that both rda_files dir and the biomass_figure.rda file exist | ||
expect_true(dir.exists(fs::path(getwd(), "rda_files"))) | ||
expect_true(file.exists(fs::path( | ||
getwd(), "rda_files", "biomass_figure.rda" | ||
))) | ||
|
||
# erase temporary testing files | ||
file.remove(fs::path(getwd(), "captions_alt_text.csv")) | ||
unlink(fs::path(getwd(), "rda_files"), recursive = T) | ||
|
||
}) | ||
|
||
test_that("export_rda works for tables", { | ||
# read in sample dataset | ||
dat <- utils::read.csv( | ||
system.file( | ||
"resources", | ||
"sample_data", | ||
"petrale_sole-after_2020.csv", | ||
package = "satf" | ||
) | ||
) | ||
|
||
topic_label <- "bnc" | ||
fig_or_table <- "table" | ||
|
||
# run write_captions.R | ||
satf::write_captions(dat = dat, | ||
dir = getwd(), | ||
year = 2022) | ||
|
||
# extract this plot's caption and alt text | ||
caps_alttext <- extract_caps_alttext(topic_label = topic_label, | ||
fig_or_table = fig_or_table, | ||
dir = getwd()) | ||
|
||
# make a simple table | ||
final <- flextable::flextable(data = data.frame(x = c(1, 2, 3), | ||
y = c(4, 5, 6))) | ||
|
||
# export rda | ||
export_rda( | ||
final = final, | ||
caps_alttext = caps_alttext, | ||
rda_dir = getwd(), | ||
topic_label = topic_label, | ||
fig_or_table = fig_or_table | ||
) | ||
|
||
# expect that both rda_files dir and the bnc_table.rda file exist | ||
expect_true(dir.exists(fs::path(getwd(), "rda_files"))) | ||
expect_true(file.exists(fs::path(getwd(), "rda_files", "bnc_table.rda"))) | ||
|
||
# erase temporary testing files | ||
file.remove(fs::path(getwd(), "captions_alt_text.csv")) | ||
unlink(fs::path(getwd(), "rda_files"), recursive = T) | ||
|
||
}) |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
test_that("extract_caps_alttext works for figures", { | ||
|
||
# read in sample dataset | ||
dat <- utils::read.csv( | ||
system.file("resources", "sample_data", "petrale_sole-after_2020.csv", package = "satf") | ||
) | ||
|
||
topic_label <- "biomass" | ||
fig_or_table <- "figure" | ||
|
||
# run write_captions.R | ||
satf::write_captions(dat = dat, | ||
dir = getwd(), | ||
year = 2022) | ||
|
||
# extract this plot's caption and alt text | ||
caps_alttext <- extract_caps_alttext(topic_label = topic_label, | ||
fig_or_table = fig_or_table, | ||
dir = getwd()) | ||
|
||
|
||
# expect that the caps_alttext is a list with two objects (caption, alt text) | ||
expect_true(length(caps_alttext) == 2) | ||
|
||
# expect the first 4 words of the caption | ||
expect_true("Biomass (B) time series." == stringr::word(caps_alttext[[1]], 1, 4)) | ||
|
||
# expect the first 3 words of the alt text | ||
expect_true("Line graph showing" == stringr::word(caps_alttext[[2]], 1, 3)) | ||
|
||
# erase temporary testing files | ||
file.remove(fs::path(getwd(), "captions_alt_text.csv")) | ||
|
||
}) | ||
|
||
test_that("extract_caps_alttext works for tables", { | ||
|
||
# read in sample dataset | ||
dat <- utils::read.csv( | ||
system.file("resources", "sample_data", "petrale_sole-after_2020.csv", package = "satf") | ||
) | ||
|
||
topic_label <- "bnc" | ||
fig_or_table <- "table" | ||
|
||
# run write_captions.R | ||
satf::write_captions(dat = dat, | ||
dir = getwd(), | ||
year = 2022) | ||
|
||
# extract this plot's caption and alt text | ||
caps_alttext <- extract_caps_alttext(topic_label = topic_label, | ||
fig_or_table = fig_or_table, | ||
dir = getwd()) | ||
|
||
|
||
# expect that the caps_alttext is a list with one object (caption) | ||
expect_true(length(caps_alttext) == 1) | ||
|
||
# expect the first 4 words of the caption | ||
expect_true("Historical biomass, abundance, and" == stringr::word(caps_alttext, 1, 4)) | ||
|
||
# erase temporary testing files | ||
file.remove(fs::path(getwd(), "captions_alt_text.csv")) | ||
|
||
}) |