Skip to content

Commit e448597

Browse files
FIX: purrr::map_lgl() instead of sapply() (#148) (#176)
* FIX: purrr::map_lgl() instead of sapply() (#148) * add tests for citation function * fix citation function --------- Co-authored-by: Egor Kotov <kotov.egor@gmail.com>
1 parent 2bf2d6a commit e448597

File tree

2 files changed

+145
-5
lines changed

2 files changed

+145
-5
lines changed

R/cite.R

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
#' @examples
1313
#' # Cite everything in all formats
1414
#' \dontrun{
15-
#' spod_cite()
15+
#' spod_cite()
1616
#' }
1717
#'
1818
#' # Cite just the package in BibTeX format
1919
#' \dontrun{
20-
#' spod_cite(what = "package", format = "bibtex")
20+
#' spod_cite(what = "package", format = "bibtex")
2121
#' }
2222
#'
2323
#' # Cite both methodologies in plain text
2424
#' \dontrun{
25-
#' spod_cite(what = c("methodology_v1", "methodology_v2"), format = "text")
25+
#' spod_cite(what = c("methodology_v1", "methodology_v2"), format = "text")
2626
#' }
2727
spod_cite <- function(
2828
what = "all",
@@ -53,11 +53,27 @@ spod_cite <- function(
5353
format <- setdiff(format, "all")
5454

5555
# 4. Get the citation object
56-
cit <- utils::citation("spanishoddata")
56+
cit <- tryCatch(
57+
{
58+
suppressWarnings(utils::citation("spanishoddata"))
59+
},
60+
error = function(e) {
61+
# Fallback for devtools::load_all() or test environments
62+
citation_file <- system.file("CITATION", package = "spanishoddata")
63+
if (citation_file == "") {
64+
stop("CITATION file not found.")
65+
}
66+
readCitationFile(citation_file, meta = list(Encoding = "UTF-8"))
67+
}
68+
)
5769

5870
# 5. Function to get citation by key
5971
get_citation_by_key <- function(key) {
60-
idx <- which(sapply(cit, function(x) x$key == key))
72+
idx <- which(purrr::map_lgl(cit, function(x) {
73+
# Try multiple ways to access the key
74+
entry_key <- x$key %||% attr(x, "key") %||% x[["key"]]
75+
isTRUE(entry_key == key)
76+
}))
6177
if (length(idx) > 0) {
6278
return(cit[idx])
6379
}

tests/testthat/test-cite.R

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Load testthat
2+
library(testthat)
3+
4+
# --- Tests for spod_cite function ---
5+
6+
test_that("spod_cite with default arguments prints all citations in all formats", {
7+
# Capture the output of a single function call
8+
output <- capture.output(spod_cite())
9+
output_str <- paste(output, collapse = "\n")
10+
11+
# Run all checks on the captured string
12+
expect_true(grepl("Plain text citations:", output_str))
13+
expect_true(grepl("Markdown citations:", output_str))
14+
expect_true(grepl("BibTeX citations:", output_str))
15+
expect_true(grepl("To cite the spanishoddata package", output_str))
16+
expect_true(grepl(
17+
"To cite the Ministry's mobility study website",
18+
output_str
19+
))
20+
expect_true(grepl("To cite the methodology for 2020-2021 data", output_str))
21+
expect_true(grepl(
22+
"To cite the methodology for 2022 and onwards data",
23+
output_str
24+
))
25+
})
26+
27+
test_that("spod_cite with specific 'what' and 'format' arguments works", {
28+
# Capture the specific output to a string for inspection
29+
output <- capture.output(spod_cite(what = "package", format = "bibtex"))
30+
output_str <- paste(output, collapse = "\n")
31+
32+
# Check for expected content
33+
expect_true(grepl("BibTeX citations:", output_str))
34+
expect_true(grepl("%% To cite the spanishoddata package", output_str))
35+
expect_true(grepl("@Manual", output_str))
36+
37+
# Check that other content is NOT present
38+
expect_false(grepl("Plain text citations:", output_str))
39+
expect_false(grepl("Markdown citations:", output_str))
40+
expect_false(grepl(
41+
"To cite the Ministry's mobility study website",
42+
output_str
43+
))
44+
})
45+
46+
test_that("spod_cite with multiple 'what' arguments works", {
47+
output <- capture.output(spod_cite(
48+
what = c("methodology_v1", "data"),
49+
format = "text"
50+
))
51+
output_str <- paste(output, collapse = "\n")
52+
53+
# Check for expected content
54+
expect_true(grepl("Plain text citations:", output_str))
55+
expect_true(grepl(
56+
"To cite the Ministry's mobility study website:",
57+
output_str
58+
))
59+
expect_true(grepl("To cite the methodology for 2020-2021 data:", output_str))
60+
61+
# Check that other content is NOT present
62+
expect_false(grepl("To cite the spanishoddata package", output_str))
63+
expect_false(grepl("BibTeX citations:", output_str))
64+
})
65+
66+
test_that("spod_cite handles invalid inputs gracefully", {
67+
# The function uses checkmate, which will throw an error on invalid input
68+
expect_error(spod_cite(what = "invalid_input"))
69+
expect_error(spod_cite(format = "invalid_format"))
70+
})
71+
72+
73+
test_that("spod_cite includes the special note for methodology_v2", {
74+
# Text format
75+
output_text <- capture.output(spod_cite(
76+
what = "methodology_v2",
77+
format = "text"
78+
))
79+
expect_true(any(grepl(
80+
"Note: A more up-to-date methodology document may be available",
81+
output_text
82+
)))
83+
84+
# Markdown format
85+
output_md <- capture.output(spod_cite(
86+
what = "methodology_v2",
87+
format = "markdown"
88+
))
89+
expect_true(any(grepl("> \\*\\*Note:\\*\\*", output_md)))
90+
91+
# BibTeX format
92+
output_bib <- capture.output(spod_cite(
93+
what = "methodology_v2",
94+
format = "bibtex"
95+
))
96+
expect_true(any(grepl(
97+
"%% Note: A more up-to-date methodology document may be available",
98+
output_bib
99+
)))
100+
})
101+
102+
test_that("spod_cite expands 'all' argument correctly", {
103+
# Test 'all' in 'what'
104+
output_what <- capture.output(spod_cite(
105+
what = c("package", "all"),
106+
format = "text"
107+
))
108+
output_what_str <- paste(output_what, collapse = "\n")
109+
expect_true(grepl("To cite the spanishoddata package", output_what_str))
110+
expect_true(grepl(
111+
"To cite the Ministry's mobility study website",
112+
output_what_str
113+
))
114+
115+
# Test 'all' in 'format'
116+
output_format <- capture.output(spod_cite(
117+
what = "package",
118+
format = c("text", "all")
119+
))
120+
output_format_str <- paste(output_format, collapse = "\n")
121+
expect_true(grepl("Plain text citations:", output_format_str))
122+
expect_true(grepl("Markdown citations:", output_format_str))
123+
expect_true(grepl("BibTeX citations:", output_format_str))
124+
})

0 commit comments

Comments
 (0)