diff --git a/R/assert.R b/R/assert.R index cca1afef..2f4a54a9 100644 --- a/R/assert.R +++ b/R/assert.R @@ -26,7 +26,7 @@ #' assert(checkChoice(x, c("a", "b")), checkDataFrame(x), add = collection) #' collection$getMessages() #' -assert = function(..., combine = "or", .var.name = NULL, add = NULL) { +assert = function(..., combine = "or", .var.name = NULL, comment = NULL, add = NULL) { assertChoice(combine, c("or", "and")) assertClass(add, "AssertCollection", .var.name = "add", null.ok = TRUE) dots = match.call(expand.dots = FALSE)$... @@ -46,6 +46,7 @@ assert = function(..., combine = "or", .var.name = NULL, add = NULL) { msgs = sprintf("%s(%s): %s", vapply(dots, function(x) as.character(x)[1L], FUN.VALUE = NA_character_), .var.name, msgs) msgs = paste0(c("One of the following must apply:", strwrap(msgs, prefix = " * ")), collapse = "\n") } + if (!is.null(comment)) {msgs = paste(paste0(msgs, '.'), comment, sep = "\n")} mstopOrPush(res = msgs, v_name = .var.name, collection = add) } else { for (i in seq_along(dots)) { @@ -53,6 +54,7 @@ assert = function(..., combine = "or", .var.name = NULL, add = NULL) { if (!isTRUE(val)) { if (is.null(.var.name)) .var.name = as.character(dots[[i]])[2L] + if (!is.null(comment)) {val = paste0(val, '. ', comment)} mstopOrPush(res = val, v_name = .var.name, collection = add) } } diff --git a/R/makeAssertion.R b/R/makeAssertion.R index 8f15ba69..915f4cb1 100644 --- a/R/makeAssertion.R +++ b/R/makeAssertion.R @@ -76,10 +76,12 @@ makeAssertionFunction = function(check.fun, c.fun = NULL, use.namespace = TRUE, } if (use.namespace) { - fun.args = c(fun.args, list(.var.name = bquote(checkmate::vname(.(as.name(x.name)))), add = NULL)) + fun.args = c(fun.args, list(.var.name = bquote(checkmate::vname(.(as.name(x.name)))), comment = NULL, add = NULL)) + body = paste0(body, "; if (!isTRUE(res) & !is.null(comment)) {res = paste0(res, '. ', comment) }") body = paste0(body, "; checkmate::makeAssertion") } else { - fun.args = c(fun.args, list(.var.name = bquote(vname(.(as.name(x.name)))), add = NULL)) + fun.args = c(fun.args, list(.var.name = bquote(vname(.(as.name(x.name)))), comment = NULL, add = NULL)) + body = paste0(body, "; if (!isTRUE(res) & !is.null(comment)) {res = paste0(res, '. ', comment) }") body = paste0(body, "; makeAssertion") } body = paste0(body, sprintf("(%s, res, .var.name, add)", x.name)) diff --git a/man-roxygen/assert.R b/man-roxygen/assert.R index 9edd8e46..2229b2c1 100644 --- a/man-roxygen/assert.R +++ b/man-roxygen/assert.R @@ -1,5 +1,7 @@ #' @param .var.name [\code{character(1)}]\cr #' Name of the checked object to print in assertions. Defaults to #' the heuristic implemented in \code{\link{vname}}. +#' @param comment [\code{character(1)}]\cr +#' Extra information to be appended to the standard error message in assertions. #' @param add [\code{AssertCollection}]\cr #' Collection to store assertion messages. See \code{\link{AssertCollection}}.