From 3d6876b9914a1b4ea3033120ad50a05ed76418be Mon Sep 17 00:00:00 2001 From: jimrothstein Date: Fri, 19 Apr 2024 12:14:36 -0700 Subject: [PATCH 1/4] small change to documentation for as_api_object --- R/as.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/as.R b/R/as.R index 22f3773..5fae855 100644 --- a/R/as.R +++ b/R/as.R @@ -4,7 +4,7 @@ #' #' @inheritParams rlang::args_dots_empty #' @inheritParams rlang::args_error_context -#' @param x The object to coerce. Must be empty or have names corresponding to +#' @param x The named list to coerce. Must be empty or have names corresponding to #' the parameter of the `target_class`, or names that can be coerced to those #' names via [snakecase::to_snake_case()]. Extra names are ignored. #' @param target_class The S7 class to which the object should be converted. From bda5e25c61813690207a291b98a16aadd6872edb Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Fri, 19 Apr 2024 14:43:57 -0500 Subject: [PATCH 2/4] Tweak as_api_object documentation I originally wrote this for lists, but it also works for character vectors, and may eventually expand to other objects like data frames. Thanks for catching the mismatch! Hopefully this fix makes as much sense as this can! --- R/as.R | 9 +++++---- man/as_api_object.Rd | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/R/as.R b/R/as.R index 5fae855..8938b47 100644 --- a/R/as.R +++ b/R/as.R @@ -1,12 +1,13 @@ #' Convert to a rapid-style object #' -#' Convert a named list into an object with a rapid-style class. +#' Convert an object into an object with a rapid-style class. #' #' @inheritParams rlang::args_dots_empty #' @inheritParams rlang::args_error_context -#' @param x The named list to coerce. Must be empty or have names corresponding to -#' the parameter of the `target_class`, or names that can be coerced to those -#' names via [snakecase::to_snake_case()]. Extra names are ignored. +#' @param x The object to coerce. Must be empty, or be a named list or character +#' vector having names corresponding to the parameter of the `target_class`, +#' or names that can be coerced to those names via +#' [snakecase::to_snake_case()]. Extra names are ignored. #' @param target_class The S7 class to which the object should be converted. #' @param alternate_names Character vector (optional). An optional named #' character vector, where the names are the names as they might appear in diff --git a/man/as_api_object.Rd b/man/as_api_object.Rd index 3e238c4..47e5e63 100644 --- a/man/as_api_object.Rd +++ b/man/as_api_object.Rd @@ -14,9 +14,10 @@ as_api_object( ) } \arguments{ -\item{x}{The object to coerce. Must be empty or have names corresponding to -the parameter of the \code{target_class}, or names that can be coerced to those -names via \code{\link[snakecase:caseconverter]{snakecase::to_snake_case()}}. Extra names are ignored.} +\item{x}{The object to coerce. Must be empty, or be a named list or character +vector having names corresponding to the parameter of the \code{target_class}, +or names that can be coerced to those names via +\code{\link[snakecase:caseconverter]{snakecase::to_snake_case()}}. Extra names are ignored.} \item{target_class}{The S7 class to which the object should be converted.} @@ -39,5 +40,5 @@ mentioned in error messages as the source of the error. See the An object with the specified \code{target_class}. } \description{ -Convert a named list into an object with a rapid-style class. +Convert an object into an object with a rapid-style class. } From 197d3aa03ebac173af93f937f68d119eb179ce9f Mon Sep 17 00:00:00 2001 From: jimrothstein Date: Fri, 19 Apr 2024 13:16:55 -0700 Subject: [PATCH 3/4] testthat for as_api_object --- tests/testthat/test-as.R | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/testthat/test-as.R diff --git a/tests/testthat/test-as.R b/tests/testthat/test-as.R new file mode 100644 index 0000000..7948933 --- /dev/null +++ b/tests/testthat/test-as.R @@ -0,0 +1,10 @@ +test_that("as_api_object() checks argument `x` ", { + expect_error(as_api_object()) + expect_error(as_api_object(NULL)) + expect_error(as_api_object(list())) + + L <- list(a = "one") # named list + M <- c(a = "one") # named character vector + expect_error(as_api_object(L)) + expect_error(as_api_object(M)) +}) From 9b07d0fd8d81d4ab16727037c4bdbf06ec446247 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Mon, 6 May 2024 09:10:32 -0500 Subject: [PATCH 4/4] Update tests to check for as_api_object-specific things. Good idea, but the errors were being generated by base R/weren't saying anything useful (they were the general "missing argument" messages). I've updated them to focus on the things as_api_object does specifically, to separate a failure of this function from all the places it's used within the package. --- tests/testthat/test-as.R | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-as.R b/tests/testthat/test-as.R index 7948933..64eb274 100644 --- a/tests/testthat/test-as.R +++ b/tests/testthat/test-as.R @@ -1,10 +1,28 @@ -test_that("as_api_object() checks argument `x` ", { - expect_error(as_api_object()) - expect_error(as_api_object(NULL)) - expect_error(as_api_object(list())) +test_that("as_api_object() fails informatively with bad x", { + simple_class <- S7::new_class( + "range", + properties = list( + start = S7::class_numeric, + end = S7::class_numeric + ) + ) + expect_no_error(as_api_object(NULL, simple_class)) + expect_error( + as_api_object(1, simple_class), + class = "rapid_error_unknown_coercion" + ) +}) - L <- list(a = "one") # named list - M <- c(a = "one") # named character vector - expect_error(as_api_object(L)) - expect_error(as_api_object(M)) +test_that("as_api_object() warns about unexpected fields", { + simple_class <- S7::new_class( + "range", + properties = list( + start = S7::class_numeric, + end = S7::class_numeric + ) + ) + expect_warning( + {as_api_object(list(start = 1, end = 2, a = 1), simple_class)}, + class = "rapid_warning_extra_names" + ) })