Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure generate_author_yml retains affiliation order by person #570

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion R/generate_author_yml.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ generate_author_yml <- function(
)

authors <- sapply(seq_along(researchers), function(i) {
i_affil = paste(affil_numeric[affil_order %in% unlist(researchers[i])], collapse = ",")
i_affil <- affil_numeric[match(unlist(researchers[i]), names(affil_numeric))]
i_affil <- paste(i_affil, collapse = ",")

if (names(researchers)[i] == corres_name) {
extra <- as.character(
Expand Down
41 changes: 32 additions & 9 deletions tests/testthat/test-generate_author_yml.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,28 @@ test_that(

rmd <- paste(rmd, collapse = "\n")

input_path <- tempfile(fileext = ".Rmd")
write(rmd, file = input_path)
input_file <- tempfile(fileext = ".Rmd")
write(rmd, file = input_file)

output_path <- tempfile(fileext = ".pdf")
output_file <- gsub(input_file, pattern = "\\.Rmd$", replacement = ".pdf")

suppressWarnings(
try(
capture.output(
rmarkdown::render(
input = input_path,
output_file = output_path,
quiet = TRUE,
input = input_file,
quiet = TRUE
)
)
, silent = TRUE
)
)

expect_true(file.exists(output_path))
expect_true(file.exists(output_file))

# Clean up
file.remove(input_path)
file.remove(output_path)
file.remove(input_file)
file.remove(output_file)

}
)
Expand Down Expand Up @@ -129,3 +128,27 @@ test_that("generate_author_yml enforces unique authors/affiliations", {
)

})

test_that("generate_author_yml preserves affiliation order", {

text <- generate_author_yml(
researchers = list(
"James H. Conigrave" = c("IPPE", "UQ"),
"Michael Noetel" = c("UQ", "IPPE")
),
affiliations = list("IPPE" = "Institute for Postitive Psychology and Education",
"UQ" = "University of Queensland"),
corres_email = "james@conigrave.com",
corres_address = "33 Berry Street, North Sydney, NSW, Australia",
corres_name = "James H. Conigrave"
)

matches <- gregexpr("(?<=affiliation : \")\\d+(,\\d+)*", text, perl=TRUE)

all_affiliations <- regmatches(text, matches)[[1]]

# Get the second instance
second_affiliation <- all_affiliations[2]
expect_equal("2,1", second_affiliation)

})