tests/testthat/test-utils.R

test_that("Message helper emits each supported message type", {
  skip_on_cran()
  skip_if_gisco_offline()
  expect_silent(make_msg(verbose = FALSE))
  expect_snapshot(make_msg(
    "generic",
    TRUE,
    "Hi",
    "I am a generic.",
    "See {.var avar}."
  ))
  expect_snapshot(make_msg("info", TRUE, "Info here.", "See {.pkg igoR}."))

  expect_snapshot(make_msg(
    "warning",
    TRUE,
    "Caution! A warning.",
    "But still OK."
  ))

  expect_snapshot(make_msg("danger", TRUE, "OOPS!", "I did it again :("))

  expect_snapshot(make_msg("success", TRUE, "Hooray!", "5/5 ;)"))
})

test_that("Argument matcher reports exact and invalid values", {
  skip_on_cran()
  skip_if_gisco_offline()
  my_fun <- function(arg_one = c(10, 1000, 3000, 5000)) {
    match_arg_pretty(arg_one)
  }

  # OK, returns character
  expect_identical(my_fun(1000), "1000")
  expect_identical(my_fun("1000"), "1000")
  expect_identical(my_fun(NULL), "10")
  expect_identical(my_fun(), "10")
  # Some errors here
  # Check an unmatched single value.
  expect_snapshot(my_fun("error here"), error = TRUE)

  # Check several unmatched values.
  expect_snapshot(my_fun(c("an", "error")), error = TRUE)

  # Match one value with a regular expression.
  expect_snapshot(my_fun("5"), error = TRUE)
  # Match several values with a regular expression.
  expect_snapshot(my_fun("00"), error = TRUE)

  my_fun2 <- function(year = 20) {
    match_arg_pretty(year)
  }

  # Pass more options than expected
  expect_snapshot(my_fun2(c(1, 2)), error = TRUE)

  # With custom options
  my_fun3 <- function(an_arg = 20) {
    match_arg_pretty(an_arg, c("30", "20"))
  }
  expect_identical(my_fun3(), "20")
  expect_snapshot(my_fun3("3"), error = TRUE)
  # Pass more options than expected
  expect_snapshot(my_fun2(c(1, 2)), error = TRUE)

  # Live action
  skip_on_cran()
  skip_if_gisco_offline()
  expect_snapshot(gisco_get_airports(2050), error = TRUE)
  expect_s3_class(gisco_get_airports(2013), "sf")
})

test_that("Argument matching returns defaults and exact values", {
  match_year <- function(year = c(2020, 2024)) {
    match_arg_pretty(year)
  }

  expect_identical(match_year(), "2020")
  expect_identical(match_year(NULL), "2020")
  expect_identical(match_year(2024), "2024")
})

test_that("Argument matching reports invalid values", {
  match_year <- function(year = c(2020, 2024)) {
    match_arg_pretty(year)
  }

  expect_snapshot(match_year(2030), error = TRUE)
  expect_snapshot(match_year(c(2020, 2030)), error = TRUE)
})

test_that("Resolution format helpers work", {
  expect_identical(format_unit_resolution(1), "01m")
  expect_identical(format_unit_resolution("20"), "20m")
  expect_identical(format_bulk_resolution("100"), "100k")
  expect_identical(format_bulk_resolution("3"), "03m")
  expect_identical(format_urau_unit_resolution(2013), "03M")
  expect_identical(format_urau_unit_resolution(2014), "100K")
  expect_identical(format_urau_unit_resolution(2024), "100k")
})

test_that("Deprecated cache helper warns only when cache is supplied", {
  expect_silent(warn_deprecated_cache(lifecycle::deprecated(), "x(cache)"))
  expect_snapshot(warn_deprecated_cache(TRUE, "x(cache)"))
})

test_that("rbind_fill combines sf inputs with different schemas", {
  skip_on_cran()
  skip_if_gisco_offline()
  gb <- giscoR::gisco_countries_2024[1, ]
  cos <- giscoR::gisco_nuts_2024[1, ]
  a_list <- list(gb, cos, gb, cos)
  expect_snapshot(err <- do.call(rbind, a_list), error = TRUE)
  expect_silent(binded <- rbind_fill(a_list))
  expect_s3_class(binded, "sf")
  expect_s3_class(binded, "tbl_df")
  expect_equal(nrow(binded), 4)
})

test_that("rbind_fill combines tibble inputs with different schemas", {
  skip_on_cran()
  skip_if_gisco_offline()
  gb <- giscoR::gisco_countries_2024[1, ]
  gb <- sf::st_drop_geometry(gb)
  cos <- giscoR::gisco_nuts_2024[1, ]
  cos <- sf::st_drop_geometry(cos)
  a_list <- list(gb, cos, gb, cos)
  expect_snapshot(err <- do.call(rbind, a_list), error = TRUE)
  expect_silent(binded <- rbind_fill(a_list))
  expect_s3_class(binded, "tbl_df")
  expect_equal(nrow(binded), 4)
})

test_that("rbind_fill drops NULL entries from sf input lists", {
  skip_on_cran()
  skip_if_gisco_offline()
  gb <- giscoR::gisco_countries_2024[1, ]
  cos <- giscoR::gisco_nuts_2024[1, ]
  a_list <- list(gb, cos, gb, cos)
  a_list[[3]] <- NULL
  expect_snapshot(err <- do.call(rbind, a_list), error = TRUE)
  expect_silent(binded <- rbind_fill(a_list))
  expect_s3_class(binded, "sf")
  expect_s3_class(binded, "tbl_df")
  expect_equal(nrow(binded), 3)
})

test_that("rbind_fill drops NULL entries from tibble input lists", {
  skip_on_cran()
  skip_if_gisco_offline()
  gb <- giscoR::gisco_countries_2024[1, ]
  gb <- sf::st_drop_geometry(gb)
  cos <- giscoR::gisco_nuts_2024[1, ]
  cos <- sf::st_drop_geometry(cos)

  a_list <- list(gb, cos, gb, cos)
  a_list[[3]] <- NULL
  expect_snapshot(err <- do.call(rbind, a_list), error = TRUE)
  expect_silent(binded <- rbind_fill(a_list))
  expect_s3_class(binded, "tbl_df")
  expect_equal(nrow(binded), 3)

  # All NULLs return NULL
  new_l <- list(a = NULL, b = NULL)

  expect_null(rbind_fill(new_l))
})
test_that("ensure_null handles empty-like and non-empty values", {
  expect_null(ensure_null(NULL))
  expect_null(ensure_null(c(NULL, NA)))
  expect_null(ensure_null(c(NULL, NA, "")))
  expect_null(ensure_null(c("", character(0))))
  expect_identical(ensure_null(c(1, 2)), c(1, 2))
  expect_identical(letters, letters)
})

test_that("Logical scalar checker rejects missing and non-scalar values", {
  expect_true(is_bool(TRUE))
  expect_true(is_bool(FALSE))
  expect_false(is_bool(NA))
  expect_false(is_bool(logical()))
  expect_false(is_bool(c(TRUE, FALSE)))
  expect_false(is_bool(1))
})

test_that("cli_abort_if_not aborts when conditions fail", {
  skip_on_cran()
  skip_if_gisco_offline()

  expect_silent(cli_abort_if_not("Noting" = is.logical(TRUE)))

  expect_snapshot(
    error = TRUE,
    cli_abort_if_not(
      "Message supports {.cls inline} {.str markup}." = is.logical(1)
    )
  )

  # Correct call
  test_msg <- function(x, verbose = TRUE) {
    make_msg(type = "danger", verbose, x)
  }

  expect_snapshot(test_msg("Testing fun reference.", verbose = TRUE))
  expect_snapshot(
    error = TRUE,
    test_msg("Testing fun reference with error.", verbose = 1)
  )

  expect_error(cli_abort_if_not("No" = NA), class = "giscoR_error")
  expect_error(cli_abort_if_not("No" = logical()), class = "giscoR_error")
  expect_error(
    cli_abort_if_not("No" = c(TRUE, TRUE)),
    class = "giscoR_error"
  )

  expect_error(make_msg(verbose = NA), class = "giscoR_error")
  expect_error(make_msg(verbose = logical()), class = "giscoR_error")
  expect_error(make_msg(verbose = c(TRUE, FALSE)), class = "giscoR_error")
})

test_that("Package input errors have a common condition class", {
  expect_error(cli_abort_if_not("No" = FALSE), class = "giscoR_error")
})

Try the giscoR package in your browser

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

giscoR documentation built on Oct. 1, 2026, 5:13 p.m.