Learn R Programming

xpectr (version 0.4.4)

strip_msg: Strip side-effect messages of non-alphanumeric characters and rethrow them

Description

lifecycle::badge("experimental")

Catches side effects (error, warnings, messages), strips the message strings of non-alphanumeric characters with strip() and regenerates them.

When numbers in error messages vary slightly between systems (and this variation isn't important to catch), we can strip the numbers as well.

Use case: Sometimes testthat tests have differences in punctuation and newlines on different systems. By stripping both the error message and the expected message (with strip()), we can avoid such failed tests.

Usage

strip_msg(
  x,
  remove_spaces = FALSE,
  remove_numbers = FALSE,
  remove_ansi = TRUE,
  lowercase = FALSE
)

Value

Returns NULL invisibly.

Arguments

x

Code that potentially throws warnings, messages, or an error.

remove_spaces

Whether to remove all whitespaces. (Logical)

remove_numbers

Whether to remove all numbers. (Logical)

remove_ansi

Whether to remove ANSI control sequences. (Logical)

lowercase

Whether to make the strings lowercase. (Logical)

Author

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other strippers: strip()

Examples

Run this code
# Attach packages
library(xpectr)
library(testthat)

# Note: The use of `try()` is just for package-testing purposes

# \donttest{
try(strip_msg(stop("this 'dot' .\n is removed! 123")))
try(strip_msg(warning("this 'dot' .\n is removed! 123")))
strip_msg(message("this 'dot' .\n is removed! 123"))
strip_msg(message("this 'dot' .\n is removed! 123"), remove_numbers = TRUE)
error_fn <- function(){stop("this 'dot' .\n is removed! 123")}
try(strip_msg(error_fn()))

# With testthat tests
expect_error(strip_msg(error_fn()),
             strip("this 'dot' .\n is removed! 123"))
expect_error(strip_msg(error_fn(), remove_numbers = TRUE),
             strip("this 'dot' .\n is removed! 123", remove_numbers = TRUE))
# }

Run the code above in your browser using DataLab