tests/testthat/test-r7-returned-condition.R

test_that("frame evaluation distinguishes returned values from raised conditions", {
  env <- new.env(parent = baseenv())
  env$err <- structure(list(message = "r7 probe", call = NULL, marker = 61L),
                       class = c("r7_probe", "error", "condition"))
  env$attempts <- 0L
  for (value in list(env$err, structure("returned text", class = "try-error"),
                    NULL, list(ok = FALSE, error = env$err), 42L)) {
    env$value <- value
    for (expr in list(quote(identity(value)), quote(value))) {
      out <- .np_try_eval_in_frames(expr, eval_env = env, search_frames = FALSE)
      expect_true(out$ok)
      expect_identical(out$value, value)
      expect_null(out$error)
    }
    out <- .np_try_eval_in_frames(quote(identity(value)),
      eval_env = list(value = value), enclos = baseenv(), search_frames = FALSE)
    expect_true(out$ok)
    expect_identical(out$value, value)
  }
  caught <- .np_try_eval_in_frames(
    quote(tryCatch(stop(err), error = identity)), env, search_frames = FALSE)
  expect_true(caught$ok)
  expect_identical(caught$value, env$err)
  out <- .np_try_eval_in_frames(
    quote({ attempts <- attempts + 1L; stop(err) }), env, search_frames = FALSE)
  expect_false(out$ok)
  expect_identical(out$error, env$err)
  expect_identical(env$attempts, 1L)
  env$x <- 9L
  x <- 99L
  expect_identical(.np_try_eval_in_frames(quote(x + 1L), env)$value, 10L)
})

Try the np package in your browser

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

np documentation built on Nov. 25, 2026, 1:06 a.m.