# civilytics_load_fonts() must not touch the network or global devices in tests: # both sysfonts::font_add_google() and showtext::showtext_auto() are mocked. with_font_mocks <- function(code, env = parent.frame()) { calls <- new.env() calls$showtext <- 0L calls$google <- character() old <- .cv_state$fonts_loaded .cv_state$fonts_loaded <- FALSE withr::defer(.cv_state$fonts_loaded <- old, envir = env) testthat::local_mocked_bindings( font_add_google = function(name, family = name, ...) { calls$google <- c(calls$google, family) }, .package = "sysfonts", .env = env ) testthat::local_mocked_bindings( showtext_auto = function(enable = TRUE) calls$showtext <- calls$showtext + 1L, .package = "showtext", .env = env ) calls } test_that("civilytics_load_fonts turns showtext on by default (#29)", { skip_if_not_installed("withr") calls <- with_font_mocks() expect_true(civilytics_load_fonts()) expect_length(calls$google, 4) expect_equal(calls$showtext, 1L) }) test_that("showtext = FALSE registers fonts but leaves showtext off (#29)", { skip_if_not_installed("withr") calls <- with_font_mocks() expect_true(civilytics_load_fonts(showtext = FALSE)) expect_length(calls$google, 4) expect_equal(calls$showtext, 0L) }) test_that("options(civilytics.showtext = FALSE) sets the default (#29)", { skip_if_not_installed("withr") calls <- with_font_mocks() withr::local_options(civilytics.showtext = FALSE) civilytics_load_fonts() expect_equal(calls$showtext, 0L) }) test_that("showtext must be a single TRUE or FALSE", { expect_error(civilytics_load_fonts(showtext = NA), "showtext") expect_error(civilytics_load_fonts(showtext = c(TRUE, FALSE)), "showtext") })