From d4f10cf7efb95c78e0c57c213b55c92d4afc317c Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Fri, 30 Aug 2024 14:52:41 +0100 Subject: [PATCH] Add assertion --- R/standalone-utils-assert.R | 10 ++++++++++ tests/testthat/test-util-assert.R | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/R/standalone-utils-assert.R b/R/standalone-utils-assert.R index 8a7e382..a70e789 100644 --- a/R/standalone-utils-assert.R +++ b/R/standalone-utils-assert.R @@ -141,6 +141,16 @@ assert_is <- function(x, what, name = deparse(substitute(x)), arg = name, } +assert_list <- function(x, name = deparse(substitute(x)), arg = name, + call = parent.frame()) { + if (!is.list(x)) { + cli::cli_abort("Expected '{name}' to be a list", + arg = arg, call = call) + } + invisible(x) +} + + assert_named <- function(x, unique = FALSE, name = deparse(substitute(x)), arg = name, call = parent.frame()) { nms <- names(x) diff --git a/tests/testthat/test-util-assert.R b/tests/testthat/test-util-assert.R index 2dfe149..5a7ae92 100644 --- a/tests/testthat/test-util-assert.R +++ b/tests/testthat/test-util-assert.R @@ -103,3 +103,11 @@ test_that("assert_scalar_logical", { expect_no_error(assert_scalar_logical(TRUE)) expect_error(assert_scalar_logical(1), "to be logical") }) + + +test_that("assert_list", { + expect_silent(assert_list(list())) + expect_silent(assert_list(list(a = 1))) + x <- c(a = 1) + expect_error(assert_list(x), "Expected 'x' to be a list") +})