tests/testthat/test-codes-output.R

# The refusals of lago_report(), visualize_cost() and lago_app() are LAGO
# errors with a code, the argument to change and a fix.

expect_output_code <- function(expr, code, argument) {
  e <- tryCatch(expr, error = function(e) e)
  expect_s3_class(e, "lago_error")
  expect_s3_class(e, paste0("lago_error_", code))
  expect_identical(e$code, code)
  expect_identical(e$argument, argument)
  expect_true(is.character(e$fix) && length(e$fix) == 1 && nzchar(e$fix))
  expect_true(code %in% lago_error_codes()$code)
  invisible(e)
}

# every check of lago_report() comes before the rendering, so a bare object
# of class lago is enough to reach them
codes_output_fake <- function() structure(list(), class = "lago")

test_that("lago_report() refusals carry codes", {
  x <- codes_output_fake()
  out <- tempfile(fileext = ".html")
  expect_output_code(lago_report(list(1)), "not_a_lago_result", "x")
  local({
    local_mocked_bindings(.lago_missing_packages = function(pkgs) c("knitr", "jsonlite"))
    e <- expect_output_code(lago_report(x, out), "package_missing", NULL)
    expect_match(conditionMessage(e), "knitr", fixed = TRUE)
  })
  local({
    local_mocked_bindings(.lago_report_template = function() "")
    expect_output_code(lago_report(x, out), "internal", NULL)
  })
  expect_output_code(lago_report(x, output_file = c("a.html", "b.html")), "output_file_invalid", "output_file")
  base <- tempfile("codes_output_")
  dir.create(base)
  on.exit(unlink(base, recursive = TRUE), add = TRUE)
  expect_output_code(lago_report(x, output_file = paste0(base, "/nodir/")), "output_folder_missing", "output_file")
  afile <- file.path(base, "afile")
  file.create(afile)
  expect_output_code(lago_report(x, output_file = file.path(afile, "x.html")), "output_folder_is_file", "output_file")
  expect_output_code(lago_report(x, output_file = file.path(base, "x.pdf")), "output_file_not_html", "output_file")
  bad <- rawToChar(as.raw(c(0x61, 0xff, 0xfe)))
  expect_output_code(lago_report(x, out, title = bad), "title_not_utf8", "title")
  expect_output_code(lago_report(x, out, "Title", FALSE, "extra"), "render_argument_unnamed", "...")
  expect_output_code(lago_report(x, out, qui = TRUE), "render_argument_partial", "qui")
  expect_output_code(lago_report(x, out, params = list()), "render_argument_reserved", "params")
  expect_output_code(lago_report(x, out, runtime = "shiny"), "render_argument_not_allowed", "runtime")
  expect_output_code(lago_report(x, out, quiet = NA), "quiet_not_logical", "quiet")
  expect_false(file.exists(out))
})

test_that("lago_report() refuses a folder it cannot write in, with a code", {
  skip_on_os("windows")
  x <- codes_output_fake()
  locked <- tempfile("codes_output_locked_")
  dir.create(locked)
  Sys.chmod(locked, "0555")
  on.exit(
    {
      Sys.chmod(locked, "0755")
      unlink(locked, recursive = TRUE)
    },
    add = TRUE
  )
  # root can write in any folder, so the lock means nothing there
  skip_if(file.access(locked, 2) == 0, "the folder stays writable for this user")
  expect_output_code(
    lago_report(x, output_file = file.path(locked, "r.html")),
    "output_folder_not_writable", "output_file"
  )
})

test_that("visualize_cost() refusals carry codes", {
  local_mocked_bindings(runApp = function(...) stop("no app"), .package = "shiny")
  vc <- function(...) {
    args <- list(
      component_names = c("a", "b"), unit_costs = c(1, 2),
      default_cost_fxn_type = "linear",
      intervention_lower_bounds = c(0, 0), intervention_upper_bounds = c(1, 1)
    )
    args[names(list(...))] <- list(...)
    do.call(visualize_cost, args)
  }
  expect_output_code(vc(component_names = 1:2), "component_names_not_character", "component_names")
  expect_output_code(vc(component_names = character(0)), "component_names_empty", "component_names")
  expect_output_code(vc(component_names = c("a", "")), "component_names_missing", "component_names")
  expect_output_code(vc(unit_costs = c("1", "2")), "unit_costs_not_numeric", "unit_costs")
  expect_output_code(vc(unit_costs = c(1, Inf)), "unit_costs_not_finite", "unit_costs")
  expect_output_code(vc(unit_costs = c(1, -1)), "unit_costs_negative", "unit_costs")
  expect_output_code(vc(unit_costs = 1), "unit_costs_length", "unit_costs")
  expect_output_code(vc(default_cost_fxn_type = c("linear", "cubic")), "cost_fxn_type_invalid", "default_cost_fxn_type")
  e <- expect_output_code(vc(default_cost_fxn_type = "square"), "cost_fxn_type_invalid", "default_cost_fxn_type")
  expect_identical(conditionMessage(e), "default cost function type must be either 'linear' or 'cubic'.")
  expect_output_code(vc(intervention_lower_bounds = "0"), "bounds_not_numeric", "intervention_lower_bounds")
  expect_output_code(vc(intervention_lower_bounds = 0), "bounds_length", "intervention_lower_bounds")
  expect_output_code(vc(intervention_lower_bounds = c(0, NA)), "bounds_not_finite", "intervention_lower_bounds")
  expect_output_code(vc(intervention_upper_bounds = "1"), "bounds_not_numeric", "intervention_upper_bounds")
  expect_output_code(vc(intervention_upper_bounds = 1), "bounds_length", "intervention_upper_bounds")
  expect_output_code(vc(intervention_upper_bounds = c(1, NaN)), "bounds_not_finite", "intervention_upper_bounds")
  e <- expect_output_code(
    visualize_cost(c("a", "b"), c(1, 2), "linear", c(0, 0), c(1, 0)),
    "bounds_order", "intervention_lower_bounds"
  )
  expect_identical(deparse(conditionCall(e)[[1]]), "visualize_cost")
})

test_that("lago_app() refusals carry codes", {
  local_mocked_bindings(runApp = function(...) stop("no app"), .package = "shiny")
  e <- expect_output_code(lago_app(codes_output_fake()), "lago_result_without_data", "data")
  expect_null(conditionCall(e))
  e <- expect_output_code(lago_app(1), "app_data_invalid", "data")
  expect_null(conditionCall(e))
})

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.