Skip to content

Commit

Permalink
build helper and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ijlyttle committed Jan 7, 2024
1 parent 8977068 commit bf11ba6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Imports:
tibble,
lifecycle,
jsonlite,
jose
jose,
cli
Suggests:
clipr (>= 0.3.0),
conflicted,
Expand Down
27 changes: 27 additions & 0 deletions R/boxr__internal_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ box_id <- function(x) {
return(as.character(bit64::as.integer64(x)))
}

# ref: https://rlang.r-lib.org/reference/topic-error-call.html
as_box_id <- function(x, arg = rlang::caller_arg(x),
call = rlang::caller_env()) {

# a box_id is a string that contains only digits

# do we need this?
if (is.null(x)) {
return(NULL)
}

id <- as.character(x)

has_only_digits <- stringr::str_detect(id, "^\\d+$")

if (!all(has_only_digits)) {
cli::cli_abort(
message = "{.arg {arg}} must contain only digits",
class = "boxr_id",
call = call
)
}

id
}


# helper to identify void values
is_void <- function(x) {
is.null(x) ||
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test_00_internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ test_that("stack_rows works", {
expect_identical(stack_rows_tbl(list(list_a, list_b)), tibble_ab)
expect_identical(stack_rows_df(list(list_a, list_b)), df_ab)

})


test_that("as_box_id works", {

expect_identical(as_box_id(c(1, 2, 3)), c("1", "2", "3"))
expect_identical(as_box_id(1407651371263), "1407651371263")

# TODO: update to expect_snapshot_error() when we use testthat 3e
expect_error(as_box_id("foo"), class = "boxr_id")
expect_error(as_box_id(3.4), class = "boxr_id")

})

0 comments on commit bf11ba6

Please sign in to comment.