R Under development (unstable) (2024-01-07 r85787 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(unittest, quietly = TRUE) > > expect_equal <- function (expr, expected) { + actual <- capture.output(expr) + if (!identical(all.equal(actual, as.character(expected)), TRUE)) { + str(actual, vec.len = 10000) + stop("output didn't match: ", actual) + } + } > > # NB: In an interactive session, the state of unittest isn't guaranteed > if (!interactive()) { + # By default, output in stdout + expect_equal({ + ok_group("Some tests", { + ok(ut_cmp_equal(4, 5), "4 == 5? Probably not") + }) + }, c( + "# Some tests", + "not ok - 4 == 5? Probably not", + "# Test returned non-TRUE value:", + "# Mean relative difference: 0.25", + "# --- 4", + "# +++ 5", + "# [1] [-4-]{+5+}")) + } > > # Redirect to tempfile, output ended up there > tf <- tempfile() > options(unittest.output = tf) > expect_equal({ + ok_group("Some tests", { + ok(ut_cmp_equal(5, 6), "5 == 6? Probably not") + }) + }, c()) > expect_equal(writeLines(readLines(tf)), c( + "# Some tests", + "not ok - 5 == 6? Probably not", + "# Test returned non-TRUE value:", + "# Mean relative difference: 0.2", + "# --- 5", + "# +++ 6", + "# [1] [-5-]{+6+}")) > > # Send back to stdout > options(unittest.output = NULL) > expect_equal({ + ok_group("Some tests", { + ok(ut_cmp_equal(8, 9), "8 == 9? Probably not") + }) + }, c( + "# Some tests", + "not ok - 8 == 9? Probably not", + "# Test returned non-TRUE value:", + "# Mean relative difference: 0.125", + "# --- 8", + "# +++ 9", + "# [1] [-8-]{+9+}")) > > test_output_ansi_color <- function (x) { + old_env <- c("NO_COLOR", "R_CLI_NUM_COLORS") ; names(old_env) <- old_env + old_env <- lapply(old_env, Sys.getenv) + on.exit(do.call(Sys.setenv, old_env)) + + old_opts <- c("cli.num_colors") ; names(old_opts) <- old_opts + old_opts <- lapply(old_opts, getOption) + on.exit(do.call(options, old_opts)) + + Sys.unsetenv(c("NO_COLOR", "R_CLI_NUM_COLORS")) + options("cli.num_colors" = NULL) + + x + } > > test_output_ansi_color({ + Sys.setenv(NO_COLOR=1) + stopifnot(isFALSE(unittest:::output_ansi_color())) + }) > > test_output_ansi_color({ + Sys.setenv(R_CLI_NUM_COLORS=8) + stopifnot(isTRUE(unittest:::output_ansi_color())) + }) > test_output_ansi_color({ + Sys.setenv(R_CLI_NUM_COLORS=1) + stopifnot(isFALSE(unittest:::output_ansi_color())) + }) > > test_output_ansi_color({ + options("cli.num_colors" = 8) + stopifnot(isTRUE(unittest:::output_ansi_color())) + }) > test_output_ansi_color({ + options("cli.num_colors" = 1) + stopifnot(isFALSE(unittest:::output_ansi_color())) + }) > > # Clear results of expected test failures > if (!interactive()) rm('outcomes', pos = unittest:::pkg_vars) > > proc.time() user system elapsed 0.29 0.04 1.01