Skip to content

Commit

Permalink
(tentatively) adding testthat for as_api_object -- need to check argu…
Browse files Browse the repository at this point in the history
…ments? (#86)

* small change to documentation for as_api_object

* 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!

* testthat for as_api_object

* 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.

---------

Co-authored-by: Jon Harmon <[email protected]>
  • Loading branch information
jimrothstein and jonthegeek authored May 6, 2024
1 parent a5236ab commit 5cca05a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/testthat/test-as.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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"
)
})

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 5cca05a

Please sign in to comment.