diff --git a/R/function_checkers.R b/R/function_checkers.R index faec6e2..0eb87ba 100644 --- a/R/function_checkers.R +++ b/R/function_checkers.R @@ -14,18 +14,18 @@ #' input returns a numeric vector output of the same length as the input, with #' finite non-missing values \eqn{\geq} 0.0. #' -#' @param fn A function. This is expected to be a function evaluating the +#' @param func A function. This is expected to be a function evaluating the #' density of a distribution at numeric values. #' @param n_req_args The number of required arguments, i.e., arguments without #' default values. -#' @param n The number of elements over which to evaluate the function `fn`. -#' Defaults to 10, and `fn` is evaluated over `seq(n)`. +#' @param n The number of elements over which to evaluate the function `func`. +#' Defaults to 10, and `func` is evaluated over `seq(n)`. #' -#' @return A logical for whether the function `fn` fulfils conditions specified -#' in the respective checks. +#' @return A logical for whether the function `func` fulfils conditions +#' specified in the respective checks. #' @keywords internal -test_fn_req_args <- function(fn, n_req_args = 1) { - # NOTE: replacing checkmate::assert_count() +test_func_req_args <- function(func, n_req_args = 1) { + # check for a non-negative integerish value stopifnot( "`n_req_args` must be a single number" = (is.numeric(n_req_args)) && (length(n_req_args) == 1), @@ -34,31 +34,31 @@ test_fn_req_args <- function(fn, n_req_args = 1) { "`n_req_args` must be a natural number > 0" = (n_req_args > 0) && (n_req_args %% 1 == 0) ) - # NOTE: using formals(args(fn)) to allow checking args of builtin primitives - # for which formals(fn) would return NULL and cause the check to error + # NOTE: using formals(args(func)) to allow checking args of builtin primitives + # for which formals(func) would return NULL and cause the check to error # NOTE: errors non-informatively for specials such as `if` - is.function(fn) && + is.function(func) && Reduce( x = Map( function(x, y) { is.name(x) && y != "..." }, - formals(args(fn)), names(formals(args(fn))) + formals(args(func)), names(formals(args(func))) ), f = `+` ) == n_req_args } #' @name function_checkers -test_fn_num_out <- function(fn, n = 10) { - # NOTE: replacing checkmate::assert_count() +test_func_num_out <- function(func, n = 10) { + # check for a non-negative integerish value stopifnot( "`n` must be a single number" = (is.numeric(n)) && (length(n) == 1), "`n` must be finite and non-missing" = (is.finite(n)) && (!is.na(n)), "`n` must be a natural number > 0" = (n > 0) && (n %% 1 == 0), - "`fn` must be a function" = is.function(fn) + "`func` must be a function" = is.function(func) ) - out <- fn(seq(n)) + out <- func(seq(n)) # return logical output of conditions is.numeric(out) && (length(out) == n) && (!anyNA(out)) && diff --git a/man/function_checkers.Rd b/man/function_checkers.Rd index f499ca7..45362e6 100644 --- a/man/function_checkers.Rd +++ b/man/function_checkers.Rd @@ -2,27 +2,27 @@ % Please edit documentation in R/function_checkers.R \name{function_checkers} \alias{function_checkers} -\alias{test_fn_req_args} -\alias{test_fn_num_out} +\alias{test_func_req_args} +\alias{test_func_num_out} \title{Check functions passed to exported functions} \usage{ -test_fn_req_args(fn, n_req_args = 1) +test_func_req_args(func, n_req_args = 1) -test_fn_num_out(fn, n = 10) +test_func_num_out(func, n = 10) } \arguments{ -\item{fn}{A function. This is expected to be a function evaluating the +\item{func}{A function. This is expected to be a function evaluating the density of a distribution at numeric values.} \item{n_req_args}{The number of required arguments, i.e., arguments without default values.} -\item{n}{The number of elements over which to evaluate the function \code{fn}. -Defaults to 10, and \code{fn} is evaluated over \code{seq(n)}.} +\item{n}{The number of elements over which to evaluate the function \code{func}. +Defaults to 10, and \code{func} is evaluated over \code{seq(n)}.} } \value{ -A logical for whether the function \code{fn} fulfils conditions specified -in the respective checks. +A logical for whether the function \code{func} fulfils conditions +specified in the respective checks. } \description{ Internal helper function that check whether a function passed as diff --git a/tests/testthat/test-function_checkers.R b/tests/testthat/test-function_checkers.R index aaa8641..2e03371 100644 --- a/tests/testthat/test-function_checkers.R +++ b/tests/testthat/test-function_checkers.R @@ -1,60 +1,60 @@ -#### Checks on test_fn_req_args() #### +#### Checks on test_func_req_args() #### test_that("Function to check N required arguments", { # Test function to check number of required arguments expect_true( - test_fn_req_args( + test_func_req_args( function(x) dgamma(x, 5, 1) ) ) # `+` needs two arguments expect_true( - test_fn_req_args( + test_func_req_args( `+`, 2L ) ) expect_false( - test_fn_req_args( + test_func_req_args( `+`, 1L ) ) expect_false( - test_fn_req_args( + test_func_req_args( function(x, y, ...) dgamma(x, 5, 1, ...) + y ) ) expect_true( - test_fn_req_args( + test_func_req_args( dgamma, 2 ) ) # fails on poorly specified n expect_error( - test_fn_req_args("dummy_fn", seq(10)), + test_func_req_args("dummy_fn", seq(10)), regexp = "`n_req_args` must be a single number" ) expect_error( - test_fn_req_args("dummy_fn", -1), + test_func_req_args("dummy_fn", -1), regexp = "`n_req_args` must be a natural number > 0" ) expect_error( - test_fn_req_args("dummy_fn", NA_real_), + test_func_req_args("dummy_fn", NA_real_), regexp = "`n_req_args` must be finite and non-missing" ) expect_error( - test_fn_req_args("dummy_fn", Inf), + test_func_req_args("dummy_fn", Inf), regexp = "`n_req_args` must be finite and non-missing" ) # expect TRUE when an appropriate primitive is passed # NOTE: Passes as this primitive has one arg expect_true( - test_fn_req_args(is.list) + test_func_req_args(is.list) ) ## functions with ellipsis expect_true( - test_fn_req_args( + test_func_req_args( function(x, ...) dgamma(x, 5, 1, ...) ) ) @@ -64,35 +64,35 @@ test_that("Function to check N required arguments", { test_that("Function to check numeric output", { # well specified case expect_true( - test_fn_num_out( + test_func_num_out( function(x) dgamma(x, 5, 1) ) ) # false on wrong input type expect_error( - test_fn_num_out("dummy_fn"), - regexp = "`fn` must be a function" + test_func_num_out("dummy_fn"), + regexp = "`func` must be a function" ) # fails on poorly specified n expect_error( - test_fn_num_out("dummy_fn", seq(10)), + test_func_num_out("dummy_fn", seq(10)), regexp = "`n` must be a single number" ) expect_error( - test_fn_num_out("dummy_fn", -1), + test_func_num_out("dummy_fn", -1), regexp = "`n` must be a natural number > 0" ) expect_error( - test_fn_num_out("dummy_fn", NA_real_), + test_func_num_out("dummy_fn", NA_real_), regexp = "`n` must be finite and non-missing" ) expect_error( - test_fn_num_out("dummy_fn", Inf), + test_func_num_out("dummy_fn", Inf), regexp = "`n` must be finite and non-missing" ) # false on wrong return type expect_false( - test_fn_num_out( + test_func_num_out( function(x, ...) as.character(dgamma(x, 5, 1, ...)) ) ) @@ -102,6 +102,6 @@ test_that("Function to check numeric output", { a[-1] } expect_false( - test_fn_num_out(fn) + test_func_num_out(fn) ) })