diff --git a/R/utils-crs.R b/R/utils-crs.R index 88371ce..ce4f679 100644 --- a/R/utils-crs.R +++ b/R/utils-crs.R @@ -45,16 +45,18 @@ validate_crs <- function( return(list(spatialReference = c())) } if ( - !((inherits(crs, "character") || + !(inherits(crs, "character") || inherits(crs, "crs") || - inherits(crs, "numeric"))) + rlang::is_integerish(crs)) ) { - cli::cli_abort(c( - "Invalid CRS supplied: {.arg {arg}}", - "i" = "must be compatible with {.fn sf::st_crs}", + cli::cli_abort( + c( + "Invalid CRS supplied: {.arg {arg}}", + "i" = "must be compatible with {.fn sf::st_crs}" + ), arg = arg, call = call - )) + ) } crs <- sf::st_crs(crs) diff --git a/tests/testthat/_snaps/utils-crs.md b/tests/testthat/_snaps/utils-crs.md new file mode 100644 index 0000000..c2fbfcc --- /dev/null +++ b/tests/testthat/_snaps/utils-crs.md @@ -0,0 +1,21 @@ +# validate_crs() errors on invalid input + + Code + validate_crs(4326.5) + Condition + Error: + ! Invalid CRS supplied: `4326.5` + i must be compatible with `sf::st_crs()` + Code + validate_crs(TRUE) + Condition + Error: + ! Invalid CRS supplied: `TRUE` + i must be compatible with `sf::st_crs()` + Code + validate_crs(list(4326)) + Condition + Error: + ! Invalid CRS supplied: `list(4326)` + i must be compatible with `sf::st_crs()` + diff --git a/tests/testthat/test-utils-crs.R b/tests/testthat/test-utils-crs.R new file mode 100644 index 0000000..8f9692c --- /dev/null +++ b/tests/testthat/test-utils-crs.R @@ -0,0 +1,16 @@ +test_that("validate_crs() accepts crs, character, integer, and whole doubles", { + expected <- list(spatialReference = list(wkid = 4326L)) + + expect_identical(validate_crs(sf::st_crs(4326)), expected) + expect_identical(validate_crs("EPSG:4326"), expected) + expect_identical(validate_crs(4326L), expected) + expect_identical(validate_crs(4326), expected) +}) + +test_that("validate_crs() errors on invalid input", { + expect_snapshot(error = TRUE, { + validate_crs(4326.5) + validate_crs(TRUE) + validate_crs(list(4326)) + }) +})