-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c79730
commit 1a7774e
Showing
10 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: Basic count | ||
format: wordcount-markdown | ||
--- | ||
|
||
Here are four words. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: Body + appendix | ||
format: wordcount-markdown | ||
--- | ||
|
||
Here are four words. | ||
|
||
::: {#appendix-count} | ||
|
||
There are five words here. | ||
|
||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: Body + notes | ||
format: wordcount-markdown | ||
--- | ||
|
||
Here are four words.^[And five in the notes.] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]. |