Skip to content

Commit

Permalink
Support numeric args
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich committed Aug 31, 2023
1 parent 2ce9c49 commit 6a20d6c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
23 changes: 20 additions & 3 deletions R/standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,35 @@ check_function <- function(x,
}

actual_args <- fn_fmls_names(f) %||% character()
n_actual_args <- length(actual_args)

if (is.numeric(expected_args)) {
n_expected_args <- expected_args
if (identical(n_expected_args, n_actual_args)) {
return(invisible(NULL))
}

message <- sprintf(
"%s must have %i %s, not %i %s.",
format_arg(arg),
n_expected_args,
pluralise(n_actual_args, "argument", "arguments"),
n_actual_args,
pluralise(n_actual_args, "argument", "arguments")
)
abort(message, call = call, arg = arg)
}

if (identical(actual_args, expected_args)) {
return(invisible(NULL))
}

n_expected_args <- length(expected_args)
n_actual_args <- length(actual_args)

if (n_expected_args == 0) {
message <- sprintf(
"%s must have no arguments, not %i %s.",
format_arg(arg),
length(actual_args),
n_actual_args,
pluralise(n_actual_args, "argument", "arguments")
)
abort(message, call = call, arg = arg)
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/_snaps/standalone-types-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,33 @@
Error in `checker()`:
! `foo` must be a function, not a symbol.

---

Code
err(checker(function(x) x, args = 0L, check_function))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must have 0 argument, not 1 argument.
Code
err(checker(function(x, y) x, args = 0L, check_function))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must have 0 arguments, not 2 arguments.
Code
err(checker(function() x, args = 2, check_function))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must have 2 arguments, not 0 arguments.
Code
err(checker(function(x, y, z) x, args = 2, check_function))
Output
<error/rlang_error>
Error in `checker()`:
! `foo` must have 2 arguments, not 3 arguments.

---

Code
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ test_that("`check_function()` checks", {
err(checker(quote(foo), check_function))
})

expect_null(check_function(function() x, args = 0L))
expect_null(check_function(function(x, y) x, args = 2L))

expect_snapshot({
# should have no arguments
err(checker(function(x) x, args = 0L, check_function))
err(checker(function(x, y) x, args = 0L, check_function))

# should have arguments
err(checker(function() x, args = 2, check_function))
err(checker(function(x, y, z) x, args = 2, check_function))
})

expect_null(check_function(function() x, args = character()))
expect_null(check_function(function(x) x, args = "x"))
expect_null(check_function(function(x, y) x, args = c("x", "y")))
Expand Down

0 comments on commit 6a20d6c

Please sign in to comment.