From 76a2b8885b928f685e9faba588e26d49824006f9 Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 13:33:16 -0500 Subject: [PATCH 1/6] Add package error test for missing dependencies --- tests/testthat/helper-requireNamespace-mock.R | 12 ++++++++++++ tests/testthat/test-plot.R | 4 ++++ tests/testthat/test-transform.R | 13 +++++++++++++ tests/testthat/test-usmap.R | 11 +++++++++++ 4 files changed, 40 insertions(+) create mode 100644 tests/testthat/helper-requireNamespace-mock.R create mode 100644 tests/testthat/test-usmap.R diff --git a/tests/testthat/helper-requireNamespace-mock.R b/tests/testthat/helper-requireNamespace-mock.R new file mode 100644 index 0000000..f8d74fd --- /dev/null +++ b/tests/testthat/helper-requireNamespace-mock.R @@ -0,0 +1,12 @@ + +# Mocks `requireNamespace` to return `FALSE` for +# provided dependency package "pkg" to test that +# error occurs when it's not installed. +expect_package_error <- function(pkg, code, msg) { + with_mocked_bindings({ + expect_error(code, paste0("`", pkg, "` must be installed")) + }, + requireNamespace = function(package, ...) { package != pkg }, + .package = "base" + ) +} diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index 68a4a6f..6345b32 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -1,5 +1,9 @@ context("Plotting US maps") +test_that("dependencies are verified", { + expect_package_error("ggplot2", { plot_usmap() }) +}) + library(proto) example_data <- data.frame( diff --git a/tests/testthat/test-transform.R b/tests/testthat/test-transform.R index 7b2b6a0..25de533 100644 --- a/tests/testthat/test-transform.R +++ b/tests/testthat/test-transform.R @@ -1,5 +1,12 @@ context("Transforming coordinate data frames") +test_that("dependencies are verified", { + expect_package_error("sf", { usmap_transform(data.frame()) }) + expect_package_error("sp", { usmap_transform(data.frame()) }) + expect_package_error("sf", { usmap_crs() }) + expect_package_error("sp", { usmap_crs() }) +}) + test_that("data frame with AK and HI points is transformed", { data <- data.frame( lon = c(-74.01, -95.36, -118.24, -87.65, -134.42, -157.86), @@ -104,3 +111,9 @@ test_that("error occurs for data with non-numeric columns", { expect_error(usmap_transform(invalid_data2)) expect_error(usmap_transform(invalid_data3)) }) + +test_that("sp evolution status is set correctly", { + sp::set_evolution_status(1L) + usmap_transform(data.frame(lon = 0, lat = 0)) + expect_equal(sp::get_evolution_status(), 2L) +}) diff --git a/tests/testthat/test-usmap.R b/tests/testthat/test-usmap.R new file mode 100644 index 0000000..11e059f --- /dev/null +++ b/tests/testthat/test-usmap.R @@ -0,0 +1,11 @@ +context("Retrieving US map data") + +test_that("dependencies are verified", { + expect_package_error("usmapdata", { us_map() }) +}) + +test_that("data frame is returned", { + data <- us_map() + expect_equal(length(data), 9) + expect_equal(length(data$x), 13663) +}) From 2d27f7ac01c68ca84af7ea3debaae6637eb67c0a Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 13:36:25 -0500 Subject: [PATCH 2/6] Fix lintr indentation warnings --- R/fips.R | 5 ++--- R/plot-map.R | 12 ++++++++---- R/transform.R | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/R/fips.R b/R/fips.R index a33e38e..7f9e86f 100644 --- a/R/fips.R +++ b/R/fips.R @@ -86,9 +86,8 @@ fips <- function(state, county = c()) { result <- c( result, df$fips[which( - (name %in% county_i | name %in% paste(county_i, "county")) - & - (state_abbr %in% state_ | state_full %in% state_) + (name %in% county_i | name %in% paste(county_i, "county")) & + (state_abbr %in% state_ | state_full %in% state_) )] ) } diff --git a/R/plot-map.R b/R/plot-map.R index 3b16e24..1e91b9b 100644 --- a/R/plot-map.R +++ b/R/plot-map.R @@ -123,7 +123,8 @@ plot_usmap <- function(regions = c("states", "state", "counties", "county"), centroid_labels <- centroid_labels[ centroid_labels$full %in% include | centroid_labels$abbr %in% include | - centroid_labels$fips %in% include, ] + centroid_labels$fips %in% include, + ] } if (length(exclude) > 0) { @@ -131,18 +132,21 @@ plot_usmap <- function(regions = c("states", "state", "counties", "county"), centroid_labels$full %in% exclude | centroid_labels$abbr %in% exclude | centroid_labels$fips %in% exclude | - substr(centroid_labels$fips, 1, 2) %in% exclude), ] + substr(centroid_labels$fips, 1, 2) %in% exclude + ), ] } if (regions_ == "county" || regions_ == "counties") { label_layer <- ggplot2::geom_text( data = centroid_labels, ggplot2::aes(x = .data$x, y = .data$y, label = sub(" County", "", .data$county)), - color = label_color) + color = label_color + ) } else { label_layer <- ggplot2::geom_text( data = centroid_labels, - ggplot2::aes(x = .data$x, y = .data$y, label = .data$abbr), color = label_color) + ggplot2::aes(x = .data$x, y = .data$y, label = .data$abbr), color = label_color + ) } } else { label_layer <- ggplot2::geom_blank() diff --git a/R/transform.R b/R/transform.R index addf21b..c34f573 100644 --- a/R/transform.R +++ b/R/transform.R @@ -153,9 +153,9 @@ usmap_transform.data.frame <- function(data, hawaii <- transformed[ transformed@coords[, 1] >= hi_bbox[1, 1] & - transformed@coords[, 1] <= hi_bbox[1, 2] & - transformed@coords[, 2] >= hi_bbox[2, 1] & - transformed@coords[, 2] <= hi_bbox[2, 2], + transformed@coords[, 1] <= hi_bbox[1, 2] & + transformed@coords[, 2] >= hi_bbox[2, 1] & + transformed@coords[, 2] <= hi_bbox[2, 2], ] if (length(hawaii) > 0) { From 4e2af319c5965e785150a64301356d07aa6d8f1a Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 14:32:55 -0500 Subject: [PATCH 3/6] Remove unnecessary braces --- .lintr | 4 ++-- tests/testthat/helper-requireNamespace-mock.R | 2 +- tests/testthat/test-transform.R | 8 ++++---- tests/testthat/test-usmap.R | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.lintr b/.lintr index c5dffa1..fdf3c19 100644 --- a/.lintr +++ b/.lintr @@ -2,8 +2,8 @@ linters: linters_with_defaults( cyclocomp_linter = NULL, line_length_linter(120), object_name_linter(c("camelCase", "snake_case", "symbols")) - ) +) exclusions: list( "revdep", "vignettes" - ) +) diff --git a/tests/testthat/helper-requireNamespace-mock.R b/tests/testthat/helper-requireNamespace-mock.R index f8d74fd..25eb9ba 100644 --- a/tests/testthat/helper-requireNamespace-mock.R +++ b/tests/testthat/helper-requireNamespace-mock.R @@ -6,7 +6,7 @@ expect_package_error <- function(pkg, code, msg) { with_mocked_bindings({ expect_error(code, paste0("`", pkg, "` must be installed")) }, - requireNamespace = function(package, ...) { package != pkg }, + requireNamespace = function(package, ...) package != pkg, .package = "base" ) } diff --git a/tests/testthat/test-transform.R b/tests/testthat/test-transform.R index 25de533..a27e2f4 100644 --- a/tests/testthat/test-transform.R +++ b/tests/testthat/test-transform.R @@ -1,10 +1,10 @@ context("Transforming coordinate data frames") test_that("dependencies are verified", { - expect_package_error("sf", { usmap_transform(data.frame()) }) - expect_package_error("sp", { usmap_transform(data.frame()) }) - expect_package_error("sf", { usmap_crs() }) - expect_package_error("sp", { usmap_crs() }) + expect_package_error("sf", usmap_transform(data.frame())) + expect_package_error("sp", usmap_transform(data.frame())) + expect_package_error("sf", usmap_crs()) + expect_package_error("sp", usmap_crs()) }) test_that("data frame with AK and HI points is transformed", { diff --git a/tests/testthat/test-usmap.R b/tests/testthat/test-usmap.R index 11e059f..5a8cb32 100644 --- a/tests/testthat/test-usmap.R +++ b/tests/testthat/test-usmap.R @@ -1,7 +1,7 @@ context("Retrieving US map data") test_that("dependencies are verified", { - expect_package_error("usmapdata", { us_map() }) + expect_package_error("usmapdata", us_map()) }) test_that("data frame is returned", { From f7f9324c2f1cba76318efa04d35579919dffd14e Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 14:34:48 -0500 Subject: [PATCH 4/6] Add explicit package to testthat helper calls --- tests/testthat/helper-requireNamespace-mock.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/helper-requireNamespace-mock.R b/tests/testthat/helper-requireNamespace-mock.R index 25eb9ba..ec0d4bf 100644 --- a/tests/testthat/helper-requireNamespace-mock.R +++ b/tests/testthat/helper-requireNamespace-mock.R @@ -3,8 +3,8 @@ # provided dependency package "pkg" to test that # error occurs when it's not installed. expect_package_error <- function(pkg, code, msg) { - with_mocked_bindings({ - expect_error(code, paste0("`", pkg, "` must be installed")) + testthat::with_mocked_bindings({ + testthat::expect_error(code, paste0("`", pkg, "` must be installed")) }, requireNamespace = function(package, ...) package != pkg, .package = "base" From e46a6917b477deb5408fdb6e47fb0e36b5b45f6e Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 14:35:34 -0500 Subject: [PATCH 5/6] Fix .lintr formatting --- .lintr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.lintr b/.lintr index fdf3c19..c5dffa1 100644 --- a/.lintr +++ b/.lintr @@ -2,8 +2,8 @@ linters: linters_with_defaults( cyclocomp_linter = NULL, line_length_linter(120), object_name_linter(c("camelCase", "snake_case", "symbols")) -) + ) exclusions: list( "revdep", "vignettes" -) + ) From 404e4899a3916e213b3bd55c99407a6236da1eb2 Mon Sep 17 00:00:00 2001 From: Paolo Di Lorenzo Date: Sun, 10 Dec 2023 14:38:43 -0500 Subject: [PATCH 6/6] Remove unnecessary braces --- tests/testthat/test-plot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index 6345b32..7946bd8 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -1,7 +1,7 @@ context("Plotting US maps") test_that("dependencies are verified", { - expect_package_error("ggplot2", { plot_usmap() }) + expect_package_error("ggplot2", plot_usmap()) }) library(proto)