Skip to content

Commit

Permalink
tests for stop_if_not_in_slide_range()
Browse files Browse the repository at this point in the history
  • Loading branch information
markheckmann committed Jan 1, 2025
1 parent b409ea1 commit 2366f23
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
8 changes: 4 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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)}]"
Expand Down
6 changes: 3 additions & 3 deletions man/stop_if_not_in_slide_range.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,43 @@ test_that("misc", {
regex = "Expected integerish values but got <numeric>"
)
})


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 <numeric>",
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)
})

0 comments on commit 2366f23

Please sign in to comment.