| matching-expectations {testthat} | R Documentation |
Expectation: does string/output/message/warning/error match a regular expression?
expect_match(object, regexp, ..., all = TRUE, info = NULL, label = NULL) expect_output(object, regexp, ..., info = NULL, label = NULL) expect_error(object, regexp = NULL, ..., info = NULL, label = NULL) expect_warning(object, regexp = NULL, ..., all = FALSE, info = NULL, label = NULL) expect_message(object, regexp = NULL, ..., all = FALSE, info = NULL, label = NULL)
object |
object to test |
regexp |
regular expression to test against. If omitted,
just asserts that code produces some output, messsage, warning or
error. Alternatively, you can specify |
... |
Additional arguments passed on to |
all |
should all elements of actual value match |
info |
extra information to be included in the message (useful when writing tests in loops). |
label |
object label. When |
Other expectations: equivalence,
expect_equal,
expect_equivalent,
expect_identical;
expect-compare, expect_gt,
expect_gte, expect_less_than,
expect_lt, expect_lte,
expect_more_than;
expect_equal_to_reference;
expect_false, expect_true;
expect_is; expect_named;
expect_null; expect_silent;
takes_less_than
expect_match("Testing is fun", "fun")
expect_match("Testing is fun", "f.n")
# Output --------------------------------------------------------------------
str(mtcars)
expect_output(str(mtcars), "32 obs")
expect_output(str(mtcars), "11 variables")
# You can use the arguments of grepl to control the matching
expect_output(str(mtcars), "11 VARIABLES", ignore.case = TRUE)
expect_output(str(mtcars), "$ mpg", fixed = TRUE)
# Messages ------------------------------------------------------------------
f <- function(x) {
if (x < 0) message("*x* is already negative")
-x
}
expect_message(f(-1))
expect_message(f(-1), "already negative")
expect_message(f(1), NA)
# You can use the arguments of grepl to control the matching
expect_message(f(-1), "*x*", fixed = TRUE)
expect_message(f(-1), "NEGATIVE", ignore.case = TRUE)
# Warnings --------------------------------------------------------------------
f <- function(x) {
if (x < 0) warning("*x* is already negative")
-x
}
expect_warning(f(-1))
expect_warning(f(-1), "already negative")
expect_warning(f(1), NA)
# You can use the arguments of grepl to control the matching
expect_warning(f(-1), "*x*", fixed = TRUE)
expect_warning(f(-1), "NEGATIVE", ignore.case = TRUE)
# Errors --------------------------------------------------------------------
f <- function() stop("My error!")
expect_error(f())
expect_error(f(), "My error!")
# You can use the arguments of grepl to control the matching
expect_error(f(), "my error!", ignore.case = TRUE)