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

View source: R/strip_side_effect_messages.R

strip_msgR Documentation

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

Description

\Sexpr[results=rd, stage=render]{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
)

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)

Value

Returns NULL invisibly.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other strippers: strip()

Examples

# Attach packages
library(xpectr)
library(testthat)

## Not run: 
strip_msg(stop("this 'dot' .\n is removed! 123"))
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")}
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))

## End(Not run)

xpectr documentation built on Nov. 18, 2022, 5:10 p.m.