Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#99 feat: Include argument 'comment' in assertions #211

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R/assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)$...
Expand All @@ -46,13 +46,15 @@ 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)) {
val = eval(dots[[i]], envir = env)
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)
}
}
Expand Down
6 changes: 4 additions & 2 deletions R/makeAssertion.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions man-roxygen/assert.R
Original file line number Diff line number Diff line change
@@ -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}}.