Skip to content
Draft
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
46 changes: 46 additions & 0 deletions R/standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ check_number_decimal <- function(x,
...,
exit_code = exit_code,
allow_decimal = TRUE,
vector = FALSE,
min = min,
max = max,
allow_na = allow_na,
Expand Down Expand Up @@ -228,6 +229,7 @@ check_number_whole <- function(x,
...,
exit_code = exit_code,
allow_decimal = FALSE,
vector = FALSE,
min = min,
max = max,
allow_na = allow_na,
Expand All @@ -241,6 +243,7 @@ check_number_whole <- function(x,
...,
exit_code,
allow_decimal,
vector,
min,
max,
allow_na,
Expand All @@ -253,6 +256,10 @@ check_number_whole <- function(x,
what <- "a whole number"
}

if (vector) {
what <- sprintf("%s vector")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing an argument?

}

if (exit_code == IS_NUMBER_oob) {
min <- min %||% -Inf
max <- max %||% Inf
Expand Down Expand Up @@ -531,4 +538,43 @@ check_data_frame <- function(x,
)
}

check_numbers_decimal <- function(x,
...,
min = NULL,
max = NULL,
allow_infinite = TRUE,
allow_na = FALSE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()) {
if (missing(x)) {
exit_code <- IS_NUMBER_false
} else if (0 == (exit_code <- max(map_dbl(x, \(x) .standalone_types_check_dot_call(
ffi_standalone_check_number_1.0.7,
x,
allow_decimal = TRUE,
min,
max,
allow_infinite,
allow_na,
allow_null
))))) {
return(invisible(NULL))
}

.stop_not_number(
x,
...,
exit_code = exit_code,
allow_decimal = TRUE,
vector = TRUE,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential upgrade: Should probably pass the indices of the failing elements, as well as the reasons, so they can be reported in the error message. These should be truncated if there are too many.

But in a first pass it's fine to just fail saying all elements in the vector should follow the required invariants.

min = min,
max = max,
allow_na = allow_na,
allow_null = allow_null,
arg = arg,
call = call
)
}

# nocov end
10 changes: 10 additions & 0 deletions tests/testthat/test-standalone-types-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,13 @@ test_that("`check_data_frame()` checks", {
err(checker(list(data.frame(), data.frame()), check_data_frame, allow_null = TRUE))
})
})

test_that("`check_numbers_decimal` checks", {
expect_null(check_numbers_decimal(c(10, 5.5)))
expect_null(check_numbers_decimal(c(10L, 50L)))

expect_snapshot({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you still need to generate and commit this snapshot.

err(checker(, check_numbers_decimal))
err(checker("10", check_numbers_decimal, allow_na = TRUE, allow_null = TRUE))
})
})