Skip to content

Commit

Permalink
feat(helper-setup): Add the checkmate_err_msg helper
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusSkytte committed Oct 4, 2024
1 parent 9c5fe48 commit 7f2190e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions testthat/helper-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,30 @@ get_test_conns <- function() {

return(test_conns)
}


#' Parse checkmate assertions for testthat compatibility
#' @description
#' The error messages generated by `checkmate` are formatted to look nicely in the console by the
#' addition of `*` and `\n` characters.
#'
#' This means that checking these errors with `testthat::expect_error()` will often fail or will be harder to read
#' in the test since we need to manually insert `*` and `\n` to the comparison pattern to match the error message.
#'
#' This helper function intercepts the `checkmate` error message and removes the `*` and `\n` characters to allow for
#' human readable error checking.
#' @return
#' The checkmate error without `*` and `\n` characters.
#' @noRd
checkmate_err_msg <- function(expr) {
tryCatch(
expr,
error = \(e) {
e$message |>
stringr::str_remove_all(stringr::fixed("\n *")) |>
stringr::str_remove_all(stringr::fixed("* ")) |>
simpleError(message = _) |>
stop()
}
)
}

0 comments on commit 7f2190e

Please sign in to comment.