[education/rkward/devel/workspace_output] /: Add auto-test helpers for checking for expected errors
Thomas Friedrichsmeier
null at kde.org
Sat Nov 7 08:58:42 GMT 2020
Git commit b6cad1b3f6fa57075cb96b2273cd2062d3405060 by Thomas Friedrichsmeier.
Committed on 07/11/2020 at 08:57.
Pushed by tfry into branch 'devel/workspace_output'.
Add auto-test helpers for checking for expected errors
M +2 -1 ChangeLog
A +44 -0 rkward/rbackend/rpackages/rkwardtests/R/rktest.expectError.R
https://invent.kde.org/education/rkward/commit/b6cad1b3f6fa57075cb96b2273cd2062d3405060
diff --git a/ChangeLog b/ChangeLog
index 08e7d648..56e2090e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,7 +5,8 @@ TODOS for output directories:
- UI
- remove explicit warnings
-- Internal: Allow R-levels calls to support both subcommands, and a return value at the same time
+- rkwardtests library gains helper functions for checking for expected errors
+- Internal: Allow R-level calls to support both subcommands, and a return value at the same time
- Fixed: Calling (rk.)select.list() without a title would fail
- Hide or remove several purely internal functions (most can still be assessed from the rkward namespace as rkward:::xyz())
diff --git a/rkward/rbackend/rpackages/rkwardtests/R/rktest.expectError.R b/rkward/rbackend/rpackages/rkwardtests/R/rktest.expectError.R
new file mode 100644
index 00000000..94ba890b
--- /dev/null
+++ b/rkward/rbackend/rpackages/rkwardtests/R/rktest.expectError.R
@@ -0,0 +1,44 @@
+#' Helper functions to check for expected errors
+#'
+#' @description
+#' \code{rktest.commandFailed()} runs the given statement(s), which is expected to produce an error. Returns TRUE, if the command failed, FALSE, if it succeeded.
+#' \code{rktest.exptectError()} runs the given statement(s), which is expected to produce an error. If the command succeeds, and error is produced.
+#'
+#' @note In both cases, the error message of the original command will still be printed (as a warning), but execution will continue afterwards.
+#'
+#' @aliases rktest.commandFailed rktest.expectError
+#' @param expr Expression to evaluate
+#' @param message Error message to generate, if the expected error failed to occur. If NULL, the deparsed expression itself is cited.
+#' @rdname rktest.expectError
+#' @return TRUE if the command failed. rktest.commandFailed() returns FALSE, if the command did not fail.
+#' @author Thomas Friedrichsmeier \email{rkward-devel@@kde.org}
+#' @keywords utilities
+#' @seealso \code{\link[rkwardtests:rktest.makeplugintests]{rktest.makeplugintests}}
+#' @examples
+#' if(!rktest.commandFailed(stop("Expected error"))) stop("Failed to generate error")
+#' @export
+rktest.commandFailed <- function(expr){
+ failed = TRUE
+ try({
+ eval.parent(expr)
+ failed = FALSE
+ })
+ failed
+}
+
+#' @export
+#' @rdname rktest.expectError
+rktest.expectError <- function(expr, message=NULL){
+ failed = TRUE
+ try({
+ eval.parent(expr)
+ failed = FALSE
+ })
+ if(!failed) {
+ if(is.null(message)) {
+ message = deparse(substitute(expr))
+ }
+ stop(paste("Failed to generate expected error for: ", message))
+ }
+ invisible(TRUE)
+}
More information about the rkward-tracker
mailing list