From 178da4ec3ea7cbc5171c40fdfe474a81d64ce9c5 Mon Sep 17 00:00:00 2001 From: Petter Hopp Date: Thu, 26 Aug 2021 12:26:43 +0200 Subject: [PATCH 1/3] #99 doc: Included help text for comment Included help text for argument 'comment' in assertion functions. --- man-roxygen/assert.R | 2 ++ 1 file changed, 2 insertions(+) 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}}. From 64bfa04005829c4c98adff66400d98aadd681110 Mon Sep 17 00:00:00 2001 From: Petter Hopp Date: Thu, 26 Aug 2021 12:37:30 +0200 Subject: [PATCH 2/3] #99 feat: makeAssertion with argument comment I suggest to include the argument 'comment' in assertion-functions. The suggestion appends the comment after the standard message and may be considered as a solution of issue mllg#99. The original wish was to place the comment before the standard message. I have put the argument 'comment' before the argument 'add'. This may be a breaking change if users of the assertion functions has listed the arguments without the argument name, i.e has not used "... , add = collection", but just written (... , collection). --- R/makeAssertion.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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)) From f96450f2f4953407941510e866d4ee89db746206 Mon Sep 17 00:00:00 2001 From: Petter Hopp Date: Thu, 26 Aug 2021 12:45:56 +0200 Subject: [PATCH 3/3] #99 feat: Include argument 'comment' in assert Inclusion of the argument 'comment' in assert to make it in agrement with including the argument 'comment' in other assertion functions. In the case of 'combine = "or"', the comment is placed on a new line. In the case of 'combine = "add"', the comment is appended each assertion that fails and is on the same line. If there are more than one assertion that fails, this is not optimal. --- R/assert.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) } }