|
| 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