Skip to content

Commit

Permalink
Add tests for terminal output
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheiss committed Jun 3, 2024
1 parent 4bd2698 commit 9567e45
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 3 deletions.
22 changes: 19 additions & 3 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ create_local_quarto_project <- function(env = parent.frame(), test_file = NULL)
#' the .md extension.
#'
#' @param filename The name of the file.
#' @return A list containing the absolute path, the base filename with the .qmd
#' @returns A list containing the absolute path, the base filename with the .qmd
#' extension, and the base filename with the .md extension.
test_file_parts <- function(filename) {
list(
Expand All @@ -55,15 +55,31 @@ test_file_parts <- function(filename) {
)
}

#' Extract word count section from terminal output
#'
#' This function locates the content between "Overall totals" and Quarto's
#' "Output created" line in the terminal output that Quarto emits.
#'
#' @param raw_output The results from `quarto::quarto_render()`, captured with
#' `capture.output()`
#' @returns A character object containing the word count section from the
#' terminal output
extract_output <- function(raw_output) {
start_index <- grep("^Overall totals:", raw_output)
end_index <- grep("^Output created:", raw_output) - 1
actual_output <- paste0(raw_output[start_index:end_index], collapse = "\n")
actual_output
}


#' Get word counts from a Markdown file
#'
#' This function parses the YAML front matter of a markdown file, which should
#' contain keys with `wordcount_*_words` entries with the calculated word
#' counts.
#'
#' @param filename The name of the file.
#' @return A list containing all the `wordcount__words*` entries.
#' @export
#' @returns A list containing all the `wordcount__words*` entries.
get_wordcounts <- function(filename) {
yaml_metadata <- rmarkdown::yaml_front_matter(filename)
yaml_metadata <- yaml_metadata[grep("^wordcount", names(yaml_metadata))]
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-terminal-output-all-sections.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: All sections
format: wordcount-markdown

references:
- id: Lovelace1842
author:
- family: Lovelace
given: Ada Augusta
citation-key: Lovelace1842
container-title: Taylor's Scientific Memoirs
issued:
- year: 1842
language: en-GB
page: 666–731
title: >-
Sketch of the analytical engine invented by Charles Babbage, by LF Menabrea,
officer of the military engineers, with notes upon the memoir by the
translator
type: article-journal
volume: 3
---

Here are four words [@Lovelace1842].^[A note]

::: {#appendix-count}

There are five words here.

:::
24 changes: 24 additions & 0 deletions tests/testthat/test-terminal-output-no-app-notes.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: No appendix, no notes
format: wordcount-markdown

references:
- id: Lovelace1842
author:
- family: Lovelace
given: Ada Augusta
citation-key: Lovelace1842
container-title: Taylor's Scientific Memoirs
issued:
- year: 1842
language: en-GB
page: 666–731
title: >-
Sketch of the analytical engine invented by Charles Babbage, by LF Menabrea,
officer of the military engineers, with notes upon the memoir by the
translator
type: article-journal
volume: 3
---

Here are four words [@Lovelace1842].
6 changes: 6 additions & 0 deletions tests/testthat/test-terminal-output-single-word.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Single word in notes
format: wordcount-markdown
---

Here are four words.^[Note]
70 changes: 70 additions & 0 deletions tests/testthat/test-terminal-output.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
test_that("terminal output is correct; no appendix, no notes", {
test_file <- test_file_parts(here::here("tests/testthat/test-terminal-output-no-app-notes.qmd"))

create_local_quarto_project(test_file = test_file)

actual_output <- capture.output(
quarto::quarto_render(input = test_file$qmd, quiet = FALSE)
) |> extract_output()

true_output <- "Overall totals:
--------------------------------
• 40 total words
• 6 words in body and notes
Section totals:
--------------------------------
• 6 words in text body
• 34 words in reference section
"

expect_equal(actual_output, true_output)
})

test_that("terminal output is correct; all sections", {
test_file <- test_file_parts(here::here("tests/testthat/test-terminal-output-all-sections.qmd"))

create_local_quarto_project(test_file = test_file)

actual_output <- capture.output(
quarto::quarto_render(input = test_file$qmd, quiet = FALSE)
) |> extract_output()

true_output <- "Overall totals:
--------------------------------
• 47 total words
• 8 words in body and notes
Section totals:
--------------------------------
• 6 words in text body
• 2 words in notes
• 34 words in reference section
• 5 words in appendix section
"

expect_equal(actual_output, true_output)
})

test_that("terminal output is correct; singular 'word' works", {
test_file <- test_file_parts(here::here("tests/testthat/test-terminal-output-single-word.qmd"))

create_local_quarto_project(test_file = test_file)

actual_output <- capture.output(
quarto::quarto_render(input = test_file$qmd, quiet = FALSE)
) |> extract_output()

true_output <- "Overall totals:
----------------------------
• 5 total words
• 5 words in body and notes
Section totals:
----------------------------
• 4 words in text body
• 1 word in notes
"

expect_equal(actual_output, true_output)
})

0 comments on commit 9567e45

Please sign in to comment.