From f93059ee9bf76b3c800f12e0fed096fc9bf26584 Mon Sep 17 00:00:00 2001 From: "Elian H. Thiele-Evans" <60372411+ElianHugh@users.noreply.github.com> Date: Sun, 24 Dec 2023 11:40:04 +1100 Subject: [PATCH] Tests, docs --- R/map.R | 8 +++++--- man/map_qto.Rd | 4 ++-- man/pmap_qto.Rd | 4 ++-- tests/testthat/test_map.R | 40 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/R/map.R b/R/map.R index 28f8d8c..c959e40 100644 --- a/R/map.R +++ b/R/map.R @@ -1,6 +1,8 @@ partial_qto_func <- function(f, collapse, sep) { - if (identical(f, qto_callout) || identical(f, qto_div)) { + if (identical(f, qto_callout)) { function(...) f(...) + } else if ((identical(f, qto_div))) { + function(...) f(..., collapse = collapse) } else { function(...) f(..., collapse = collapse, sep = sep) } @@ -38,8 +40,8 @@ resolve_mapping_function <- function(f = NULL, #' "heading". #' @param .sep,.collapse Additional parameters passed to [qto_block()] if .f #' does not return a quarto block class object. Ignored if .f does return a -#' quarto block class object. Also passed to the .type function if it is -#' a "heading" or "block". +#' quarto block class object. Also passed to the relevant .type function if it supports +#' the collapse and/or sep parameters. #' @inheritParams rlang::args_error_context #' @examples #' qto_list <- map_qto( diff --git a/man/map_qto.Rd b/man/map_qto.Rd index b0e51ca..d73bd5a 100644 --- a/man/map_qto.Rd +++ b/man/map_qto.Rd @@ -28,8 +28,8 @@ each element of the vector. Options include "block", "div", "callout", or \item{.sep, .collapse}{Additional parameters passed to \code{\link[=qto_block]{qto_block()}} if .f does not return a quarto block class object. Ignored if .f does return a -quarto block class object. Also passed to the .type function if it is -a "heading" or "block".} +quarto block class object. Also passed to the relevant .type function if it supports +the collapse and/or sep parameters.} \item{call}{The execution environment of a currently running function, e.g. \code{caller_env()}. The function will be diff --git a/man/pmap_qto.Rd b/man/pmap_qto.Rd index 8600590..237b516 100644 --- a/man/pmap_qto.Rd +++ b/man/pmap_qto.Rd @@ -28,8 +28,8 @@ each element of the vector. Options include "block", "div", "callout", or \item{.sep, .collapse}{Additional parameters passed to \code{\link[=qto_block]{qto_block()}} if .f does not return a quarto block class object. Ignored if .f does return a -quarto block class object. Also passed to the .type function if it is -a "heading" or "block".} +quarto block class object. Also passed to the relevant .type function if it supports +the collapse and/or sep parameters.} \item{call}{The execution environment of a currently running function, e.g. \code{caller_env()}. The function will be diff --git a/tests/testthat/test_map.R b/tests/testthat/test_map.R index 7bc7812..bc7dd33 100644 --- a/tests/testthat/test_map.R +++ b/tests/testthat/test_map.R @@ -8,11 +8,46 @@ check_types <- function(lst) { test_that("resolve_mapping_function works", { expect_type(resolve_mapping_function(f = ~ .x + 1L), "closure") - fn <- resolve_mapping_function(type = "block", sep = " ", collapse = " ") + block_fn <- resolve_mapping_function( + type = "block", + sep = " ", + collapse = " " + ) expect_identical( - fn("Hello", c("world", "!")), + block_fn("Hello", c("world", "!")), qto_block("Hello", c("world", "!"), sep = " ", collapse = " ") ) + + div_fn <- resolve_mapping_function( + type = "div", + sep = " ", + collapse = "bar" + ) + expect_identical( + div_fn("foo", "baz"), + qto_div("foo", "bar", "baz") + ) + + callout_fn <- resolve_mapping_function( + type = "callout", + sep = " ", + collapse = "bar" + ) + expect_identical( + callout_fn("foo", "baz"), + qto_callout("foo", "baz") + ) + + heading_fn <- resolve_mapping_function( + type = "heading", + sep = " ", + collapse = " " + ) + expect_identical( + heading_fn("foo", "baz"), + qto_heading("foo", "baz", collapse = " ", sep = " ") + ) + }) @@ -62,5 +97,4 @@ test_that("pmap_qto works", { expect_length(qto_list, 3L) expect_true(check_types(qto_list)) expect_snapshot(qto_list) - })