Skip to content

Commit

Permalink
Update tests to check for as_api_object-specific things.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonthegeek committed May 6, 2024
1 parent 23ffc78 commit 9b07d0f
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions tests/testthat/test-as.R
Original file line number Diff line number Diff line change
@@ -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"
)
})

0 comments on commit 9b07d0f

Please sign in to comment.