From 2366f2332eed3a4ca177f9a06a2e5318ffc73b49 Mon Sep 17 00:00:00 2001 From: Mark Heckmann Date: Wed, 1 Jan 2025 16:57:55 +0100 Subject: [PATCH] tests for stop_if_not_in_slide_range() --- R/utils.R | 8 +++---- man/stop_if_not_in_slide_range.Rd | 6 ++--- tests/testthat/test-utils.R | 40 +++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/R/utils.R b/R/utils.R index 7a2bbaaf..2b328a9a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -347,12 +347,12 @@ stop_if_not_integerish <- function(x, arg = NULL) { } -#' Check slide indexes +#' Ensure valid slide indexes #' #' @param x An `rpptx` object. #' @param idx Slide indexes. #' @param arg Name of argument to use in error message (optional). -#' @param call Environment to display in error message-. Defaults to caller env. +#' @param call Environment to display in error message. Defaults to caller env. #' Set `NULL` to suppress (see [cli::cli_abort]). #' @keywords internal stop_if_not_in_slide_range <- function(x, idx, arg = NULL, call = parent.frame()) { @@ -367,8 +367,8 @@ stop_if_not_in_slide_range <- function(x, idx, arg = NULL, call = parent.frame() if (n_outside == 0) { return(invisible(NULL)) } - argname <- ifelse(is.null(arg), "", " of {.arg {arg}} ") - part_1 <- paste0("{n_outside} index{?es}", argname, "outside slide range: {.val {idx_outside}}") + argname <- ifelse(is.null(arg), "", "of {.arg {arg}} ") + part_1 <- paste0("{n_outside} index{?es} ", argname, "outside slide range: {.val {idx_outside}}") part_2 <- ifelse(n_slides == 0, "Presentation has no slides!", "Slide indexes must be in the range [{min(idx_available)}..{max(idx_available)}]" diff --git a/man/stop_if_not_in_slide_range.Rd b/man/stop_if_not_in_slide_range.Rd index bc1199e0..4079eeed 100644 --- a/man/stop_if_not_in_slide_range.Rd +++ b/man/stop_if_not_in_slide_range.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/utils.R \name{stop_if_not_in_slide_range} \alias{stop_if_not_in_slide_range} -\title{Check slide indexes} +\title{Ensure valid slide indexes} \usage{ stop_if_not_in_slide_range(x, idx, arg = NULL, call = parent.frame()) } @@ -13,10 +13,10 @@ stop_if_not_in_slide_range(x, idx, arg = NULL, call = parent.frame()) \item{arg}{Name of argument to use in error message (optional).} -\item{call}{Environment to display in error message-. Defaults to caller env. +\item{call}{Environment to display in error message. Defaults to caller env. Set \code{NULL} to suppress (see \link[cli:cli_abort]{cli::cli_abort}).} } \description{ -Check slide indexes +Ensure valid slide indexes } \keyword{internal} diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 2a1858fc..efb70879 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -36,3 +36,43 @@ test_that("misc", { regex = "Expected integerish values but got " ) }) + + +test_that("stop_if_not_in_slide_range", { + opts <- options(cli.num_colors = 1) # suppress colors for error message check + on.exit(options(opts)) + + x <- read_pptx() + expect_error( + regex = "Presentation has no slides", + stop_if_not_in_slide_range(x, 1) + ) + + x <- add_slide(x) + x <- add_slide(x) + expect_no_error(stop_if_not_in_slide_range(x, 1:2)) + expect_error( + regex = "1 index outside slide range", + stop_if_not_in_slide_range(x, -1) + ) + expect_error( + regex = "2 indexes of `my_arg` outside slide range", + stop_if_not_in_slide_range(x, 3:4, arg = "my_arg") + ) + expect_error( + regex = "Expected integerish values but got ", + stop_if_not_in_slide_range(x, 3.1) + ) + + foo <- function() { + stop_if_not_in_slide_range(x, 3:4) + } + error_text <- tryCatch(foo(), error = paste) + grepl("^Error in `foo()`:", error_text, fixed = TRUE) + + foo <- function() { + stop_if_not_in_slide_range(x, 3:4, call = NULL) + } + error_text <- tryCatch(foo(), error = paste) + grepl("^Error:", error_text, fixed = TRUE) +})