1context("sanitise_dim")
2
3test_that("sanitise_dim returns NULL for zero-length inputs, with appropriate warnings", {
4  expect_identical(sanitise_dim(NULL), NULL)
5  n <- integer()
6  y <- expect_identical(suppressWarnings(sanitise_dim(n)), NULL)
7  expect_warning(sanitise_dim(n), "`n` has length zero and will be treated as NULL.")
8})
9
10test_that("sanitise_dim returns the first element or NULL for non-positive integer inputs, with appropriate warnings", {
11  n <- 1:2
12  expect_identical(suppressWarnings(sanitise_dim(n)), 1L)
13  expect_warning(sanitise_dim(n), "Only the first value of `n` will be used.")
14  n2 <- 0:1
15  expect_identical(suppressWarnings(sanitise_dim(n2)), NULL)
16  expect_warning(sanitise_dim(n2), "Only the first value of `n2` will be used.")
17  expect_warning(sanitise_dim(n2), "`n2` is missing or less than 1 and will be treated as NULL.")
18})
19
20test_that("sanitise_dim returns a NULL for missing inputs, with appropriate warnings", {
21  n <- NA_integer_
22  expect_identical(suppressWarnings(sanitise_dim(n)), NULL)
23  expect_warning(sanitise_dim(n), "`n` is missing or less than 1 and will be treated as NULL.")
24})
25
26test_that("sanitise_dim returns a positive integer or NULL for non-integer inputs, with appropriate warnings", {
27  n <- 1.5
28  expect_identical(suppressWarnings(sanitise_dim(n)), 1L)
29  expect_warning(sanitise_dim(n), "Coercing `n` to be an integer.")
30  n2 <- 0.9999999
31  expect_identical(suppressWarnings(sanitise_dim(n2)), NULL)
32  expect_warning(sanitise_dim(n2), "Coercing `n2` to be an integer.")
33  expect_warning(sanitise_dim(n2), "`n2` is missing or less than 1 and will be treated as NULL.")
34})
35