From 1a7774e6fc0f56c92b40f1ced7247b331df77b0c Mon Sep 17 00:00:00 2001 From: Andrew Heiss Date: Fri, 31 May 2024 09:32:46 -0400 Subject: [PATCH] Add initial tests --- tests/testthat.R | 17 +++++++ tests/testthat/helper.R | 71 +++++++++++++++++++++++++++ tests/testthat/test-basic-count.R | 15 ++++++ tests/testthat/test-basic-count.qmd | 6 +++ tests/testthat/test-body-appendix.R | 15 ++++++ tests/testthat/test-body-appendix.qmd | 12 +++++ tests/testthat/test-body-notes.R | 15 ++++++ tests/testthat/test-body-notes.qmd | 6 +++ tests/testthat/test-body-refs.R | 15 ++++++ tests/testthat/test-body-refs.qmd | 24 +++++++++ 10 files changed, 196 insertions(+) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/helper.R create mode 100644 tests/testthat/test-basic-count.R create mode 100644 tests/testthat/test-basic-count.qmd create mode 100644 tests/testthat/test-body-appendix.R create mode 100644 tests/testthat/test-body-appendix.qmd create mode 100644 tests/testthat/test-body-notes.R create mode 100644 tests/testthat/test-body-notes.qmd create mode 100644 tests/testthat/test-body-refs.R create mode 100644 tests/testthat/test-body-refs.qmd diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..b06793a --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,17 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html + +library(testthat) + +# If running interactively, make sure you source tests/testthat/helper.R +# source(here::here("tests/testthat/helper.R")) + +# Run all the tests in the testthat/ folder. In regular testthat situations, +# this would use `test_check()`, but that's designed for R packages, and this +# extension isn't a package. +test_dir(here::here("tests/testthat")) diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R new file mode 100644 index 0000000..ce4bd8f --- /dev/null +++ b/tests/testthat/helper.R @@ -0,0 +1,71 @@ +#' Create a temporary directory for testing +#' +#' This function uses a test fixture +#' (https://testthat.r-lib.org/articles/test-fixtures.html) to create a +#' temporary directory and copies the "_extensions" folder into it for running +#' tests. Everything is deleted when the tests are finished. +#' +#' @param env The environment in which to run the deferred cleanup code. +#' Defaults to the parent frame. +#' @param test_file An optional file to copy into the temporary directory. +#' @return The path to the temporary directory. +create_local_quarto_project <- function(env = parent.frame(), test_file = NULL) { + # Create temporary folder + dir <- tempfile(pattern = "test-") + dir.create(dir) + + # Record the starting state + old_wd <- getwd() + + # Things to undo when this is all done + withr::defer( + { + setwd(old_wd) # Restore working directory (-B) + fs::dir_delete(dir) # Delete temporary folder (-A) + }, + envir = env + ) + + # Things to do + setwd(dir) # Change working directory (B) + + # Copy _extensions folder and test file to temporary folder (A) + file.copy(from = here::here("_extensions"), to = ".", recursive = TRUE) + if (!is.null(test_file)) { + file.copy(from = test_file$absolute, to = test_file$qmd) + } + + invisible(dir) +} + +#' Get the parts of a test file name +#' +#' This function takes a filename and returns a list containing the absolute +#' path, the base filename with the .qmd extension, and the base filename with +#' the .md extension. +#' +#' @param filename The name of the file. +#' @return 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( + absolute = filename, + qmd = basename(filename), + md = paste0(tools::file_path_sans_ext(basename(filename)), ".md") + ) +} + +#' 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 +get_wordcounts <- function(filename) { + yaml_metadata <- rmarkdown::yaml_front_matter(filename) + yaml_metadata <- yaml_metadata[grep("^wordcount", names(yaml_metadata))] + yaml_metadata +} diff --git a/tests/testthat/test-basic-count.R b/tests/testthat/test-basic-count.R new file mode 100644 index 0000000..0430d4e --- /dev/null +++ b/tests/testthat/test-basic-count.R @@ -0,0 +1,15 @@ +test_that("basic counting with no references or appendix or notes works", { + test_file <- test_file_parts(here::here("tests/testthat/test-basic-count.qmd")) + + create_local_quarto_project(test_file = test_file) + + quarto::quarto_render(input = test_file$qmd, quiet = TRUE) + + counts <- get_wordcounts(test_file$md) + + expect_equal(counts$wordcount_appendix_words, 0) + expect_equal(counts$wordcount_body_words, 4) + expect_equal(counts$wordcount_note_words, 0) + expect_equal(counts$wordcount_ref_words, 0) + expect_equal(counts$wordcount_total_words, 4) +}) diff --git a/tests/testthat/test-basic-count.qmd b/tests/testthat/test-basic-count.qmd new file mode 100644 index 0000000..e44a544 --- /dev/null +++ b/tests/testthat/test-basic-count.qmd @@ -0,0 +1,6 @@ +--- +title: Basic count +format: wordcount-markdown +--- + +Here are four words. diff --git a/tests/testthat/test-body-appendix.R b/tests/testthat/test-body-appendix.R new file mode 100644 index 0000000..3186d9f --- /dev/null +++ b/tests/testthat/test-body-appendix.R @@ -0,0 +1,15 @@ +test_that("counting the body text and appendix works", { + test_file <- test_file_parts(here::here("tests/testthat/test-body-appendix.qmd")) + + create_local_quarto_project(test_file = test_file) + + quarto::quarto_render(input = test_file$qmd, quiet = TRUE) + + counts <- get_wordcounts(test_file$md) + + expect_equal(counts$wordcount_appendix_words, 5) + expect_equal(counts$wordcount_body_words, 4) + expect_equal(counts$wordcount_note_words, 0) + expect_equal(counts$wordcount_ref_words, 0) + expect_equal(counts$wordcount_total_words, 9) +}) diff --git a/tests/testthat/test-body-appendix.qmd b/tests/testthat/test-body-appendix.qmd new file mode 100644 index 0000000..dd949c5 --- /dev/null +++ b/tests/testthat/test-body-appendix.qmd @@ -0,0 +1,12 @@ +--- +title: Body + appendix +format: wordcount-markdown +--- + +Here are four words. + +::: {#appendix-count} + +There are five words here. + +::: diff --git a/tests/testthat/test-body-notes.R b/tests/testthat/test-body-notes.R new file mode 100644 index 0000000..cad0a8e --- /dev/null +++ b/tests/testthat/test-body-notes.R @@ -0,0 +1,15 @@ +test_that("counting the body text and notes works", { + test_file <- test_file_parts(here::here("tests/testthat/test-body-notes.qmd")) + + create_local_quarto_project(test_file = test_file) + + quarto::quarto_render(input = test_file$qmd, quiet = TRUE) + + counts <- get_wordcounts(test_file$md) + + expect_equal(counts$wordcount_appendix_words, 0) + expect_equal(counts$wordcount_body_words, 4) + expect_equal(counts$wordcount_note_words, 5) + expect_equal(counts$wordcount_ref_words, 0) + expect_equal(counts$wordcount_total_words, 9) +}) diff --git a/tests/testthat/test-body-notes.qmd b/tests/testthat/test-body-notes.qmd new file mode 100644 index 0000000..7b323c5 --- /dev/null +++ b/tests/testthat/test-body-notes.qmd @@ -0,0 +1,6 @@ +--- +title: Body + notes +format: wordcount-markdown +--- + +Here are four words.^[And five in the notes.] diff --git a/tests/testthat/test-body-refs.R b/tests/testthat/test-body-refs.R new file mode 100644 index 0000000..87b88f6 --- /dev/null +++ b/tests/testthat/test-body-refs.R @@ -0,0 +1,15 @@ +test_that("counting the body text and references works", { + test_file <- test_file_parts(here::here("tests/testthat/test-body-refs.qmd")) + + create_local_quarto_project(test_file = test_file) + + quarto::quarto_render(input = test_file$qmd, quiet = TRUE) + + counts <- get_wordcounts(test_file$md) + + expect_equal(counts$wordcount_appendix_words, 0) + expect_equal(counts$wordcount_body_words, 6) + expect_equal(counts$wordcount_note_words, 0) + expect_equal(counts$wordcount_ref_words, 34) + expect_equal(counts$wordcount_total_words, 40) +}) diff --git a/tests/testthat/test-body-refs.qmd b/tests/testthat/test-body-refs.qmd new file mode 100644 index 0000000..d88f381 --- /dev/null +++ b/tests/testthat/test-body-refs.qmd @@ -0,0 +1,24 @@ +--- +title: Body + references +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].