From e0a2b5ec4506c839a70049f70fabcd5aa8c951e6 Mon Sep 17 00:00:00 2001 From: "Elian H. Thiele-Evans" <60372411+ElianHugh@users.noreply.github.com> Date: Fri, 29 Dec 2023 12:45:18 +1100 Subject: [PATCH] Revert partial function creation (#13) - Revert partial function creation changes made to map_qto during the pmap_qto PR - Add tests for sep and collapse calls - Revert docs --- R/map.R | 27 ++++---------------- man/map_qto.Rd | 3 +-- man/pmap_qto.Rd | 3 +-- tests/testthat/_snaps/map.md | 7 ++---- tests/testthat/test_map.R | 48 ++++++++++++++---------------------- 5 files changed, 27 insertions(+), 61 deletions(-) diff --git a/R/map.R b/R/map.R index c959e40..b5e4a2c 100644 --- a/R/map.R +++ b/R/map.R @@ -1,23 +1,11 @@ -partial_qto_func <- function(f, collapse, sep) { - if (identical(f, qto_callout)) { - function(...) f(...) - } else if ((identical(f, qto_div))) { - function(...) f(..., collapse = collapse) - } else { - function(...) f(..., collapse = collapse, sep = sep) - } -} - resolve_mapping_function <- function(f = NULL, type = NULL, - collapse = NULL, - sep = NULL, call = NULL) { f <- f %||% switch(type, - block = partial_qto_func(qto_block, collapse, sep), - div = partial_qto_func(qto_div, collapse, sep), - callout = partial_qto_func(qto_callout, collapse, sep), - heading = partial_qto_func(qto_heading, collapse, sep), + block = qto_block, + div = qto_div, + callout = qto_callout, + heading = qto_heading, ) if (!is_function(f)) { f <- as_function(f, call = call) @@ -40,8 +28,7 @@ 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 relevant .type function if it supports -#' the collapse and/or sep parameters. +#' quarto block class object. #' @inheritParams rlang::args_error_context #' @examples #' qto_list <- map_qto( @@ -64,8 +51,6 @@ map_qto <- function(.x, .f <- resolve_mapping_function( f = .f, type = .type, - collapse = .collapse, - sep = .sep, call = call ) map( @@ -123,8 +108,6 @@ pmap_qto <- function(.l, .f <- resolve_mapping_function( f = .f, type = .type, - collapse = .collapse, - sep = .sep, call = call ) pmap( diff --git a/man/map_qto.Rd b/man/map_qto.Rd index d73bd5a..ad23f80 100644 --- a/man/map_qto.Rd +++ b/man/map_qto.Rd @@ -28,8 +28,7 @@ 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 relevant .type function if it supports -the collapse and/or sep parameters.} +quarto block class object.} \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 237b516..af2abaf 100644 --- a/man/pmap_qto.Rd +++ b/man/pmap_qto.Rd @@ -28,8 +28,7 @@ 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 relevant .type function if it supports -the collapse and/or sep parameters.} +quarto block class object.} \item{call}{The execution environment of a currently running function, e.g. \code{caller_env()}. The function will be diff --git a/tests/testthat/_snaps/map.md b/tests/testthat/_snaps/map.md index 8d7f967..e7a007f 100644 --- a/tests/testthat/_snaps/map.md +++ b/tests/testthat/_snaps/map.md @@ -31,13 +31,10 @@ qto_list Output [[1]] - foo + foo bar baz [[2]] - bar - - [[3]] - baz + a b c # pmap_qto works diff --git a/tests/testthat/test_map.R b/tests/testthat/test_map.R index bc7dd33..ac48b7a 100644 --- a/tests/testthat/test_map.R +++ b/tests/testthat/test_map.R @@ -8,44 +8,28 @@ check_types <- function(lst) { test_that("resolve_mapping_function works", { expect_type(resolve_mapping_function(f = ~ .x + 1L), "closure") - block_fn <- resolve_mapping_function( - type = "block", - sep = " ", - collapse = " " - ) + block_fn <- resolve_mapping_function(type = "block") expect_identical( - block_fn("Hello", c("world", "!")), + block_fn("Hello", c("world", "!"), sep = " ", collapse = " "), qto_block("Hello", c("world", "!"), sep = " ", collapse = " ") ) - div_fn <- resolve_mapping_function( - type = "div", - sep = " ", - collapse = "bar" - ) + div_fn <- resolve_mapping_function(type = "div") expect_identical( - div_fn("foo", "baz"), - qto_div("foo", "bar", "baz") + div_fn("foo", "bar", "baz", collapse = " "), + qto_div("foo", "bar", "baz", collapse = " ") ) - callout_fn <- resolve_mapping_function( - type = "callout", - sep = " ", - collapse = "bar" - ) + callout_fn <- resolve_mapping_function(type = "callout") expect_identical( - callout_fn("foo", "baz"), - qto_callout("foo", "baz") + callout_fn("foo", "baz", collapse = TRUE), + qto_callout("foo", "baz", collapse = TRUE) ) - heading_fn <- resolve_mapping_function( - type = "heading", - sep = " ", - collapse = " " - ) + heading_fn <- resolve_mapping_function(type = "heading") expect_identical( - heading_fn("foo", "baz"), - qto_heading("foo", "baz", collapse = " ", sep = " ") + heading_fn("foo", "baz", sep = " ", collapse = " "), + qto_heading("foo", "baz", sep = " ", collapse = " ") ) }) @@ -62,10 +46,14 @@ test_that("map_qto works", { qto_list <- map_qto( - list("foo", "bar", "baz"), - .f = function(x) x + list( + c("foo", "bar", "baz"), + c("a", "b", "c") + ), + .f = function(x) x, + .collapse = " " ) - expect_length(qto_list, 3L) + expect_length(qto_list, 2L) expect_true(check_types(qto_list)) expect_snapshot(qto_list) })