tests/testthat/test-error-codes.R

# Every refusal is a lago_error with a code from the catalog.

lago_codes_used <- function() {
  files <- list.files(testthat::test_path("..", "..", "R"), pattern = "[.]R$", full.names = TRUE)
  if (length(files) == 0) skip("the R sources are not available")
  # one string, since a call can put its code on the next line
  text <- paste(unlist(lapply(files, readLines, warn = FALSE)), collapse = "\n")
  # lago_abort("x", ...), lago_error("x", ...), and code = "x" in cli_abort()
  hits <- regmatches(text, gregexpr(
    "(lago_abort|lago_error)\\(\\s*\"[a-z0-9_]+\"|\\bcode = \"[a-z0-9_]+\"", text
  ))
  unique(sub(".*\"([a-z0-9_]+)\"", "\\1", unlist(hits)))
}

test_that("every code in the catalog is listed once and has a description", {
  codes <- lago_error_codes()
  expect_named(codes, c("code", "description"))
  expect_false(anyDuplicated(codes$code) > 0)
  expect_true(all(grepl("^[a-z][a-z0-9_]*$", codes$code)))
  expect_true(all(nzchar(codes$description)))
})

test_that("every code the package raises is in the catalog, and every listed code is raised", {
  used <- lago_codes_used()
  catalog <- lago_error_codes()$code
  expect_equal(setdiff(used, catalog), character(0))
  expect_equal(setdiff(setdiff(catalog, used), "internal"), character(0))
})

test_that("a LAGO error carries its code, argument and fix", {
  e <- tryCatch(
    LAGOtrials:::lago_abort("internal", "a message", argument = "x", fix = "Do this."),
    error = function(e) e
  )
  expect_s3_class(e, "lago_error")
  expect_s3_class(e, "lago_error_internal")
  expect_identical(conditionMessage(e), "a message")
  expect_identical(e$code, "internal")
  expect_identical(e$argument, "x")
  expect_identical(e$fix, "Do this.")
  expect_error(LAGOtrials:::lago_error("no_such_code", "m"), "unknown LAGO error code")
})

Try the LAGOtrials package in your browser

Any scripts or data that you put into this service are public.

LAGOtrials documentation built on Dec. 4, 2026, 5:10 p.m.