From b3a45afe371b9b93857b39aedd5fe3db40f54742 Mon Sep 17 00:00:00 2001 From: catalamarti Date: Thu, 15 Aug 2024 15:10:16 -0700 Subject: [PATCH] start check_numbers_decimal --- R/standalone-types-check.R | 46 ++++++++++++++++++++ tests/testthat/test-standalone-types-check.R | 10 +++++ 2 files changed, 56 insertions(+) diff --git a/R/standalone-types-check.R b/R/standalone-types-check.R index 90a889f1b..ddc963286 100644 --- a/R/standalone-types-check.R +++ b/R/standalone-types-check.R @@ -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, @@ -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, @@ -241,6 +243,7 @@ check_number_whole <- function(x, ..., exit_code, allow_decimal, + vector, min, max, allow_na, @@ -253,6 +256,10 @@ check_number_whole <- function(x, what <- "a whole number" } + if (vector) { + what <- sprintf("%s vector") + } + if (exit_code == IS_NUMBER_oob) { min <- min %||% -Inf max <- max %||% Inf @@ -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, + min = min, + max = max, + allow_na = allow_na, + allow_null = allow_null, + arg = arg, + call = call + ) +} + # nocov end diff --git a/tests/testthat/test-standalone-types-check.R b/tests/testthat/test-standalone-types-check.R index ce0a3dc0a..0c787d50a 100644 --- a/tests/testthat/test-standalone-types-check.R +++ b/tests/testthat/test-standalone-types-check.R @@ -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({ + err(checker(, check_numbers_decimal)) + err(checker("10", check_numbers_decimal, allow_na = TRUE, allow_null = TRUE)) + }) +})