Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests for NA/NaN #178

Merged
merged 3 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, any::decor, any::bench, local::.
extra-packages: any::pkgdown, any::cpp11, any::decor, any::bench, local::.
needs: website

- name: Build site
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# wk (development version)

* Fix tests for updated waldo package (#178).

# wk 0.7.2

* Fix use-after-free warnings.
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ recycle_common <- function(...) {
is_vector_class <- function(x) {
identical(class(x[integer(0)]), class(x))
}

# This helps when comparing with sf package objects, which tend to use
# NA rather than NaN.
expect_equal_ignore_na_nan <- function(actual, expected) {
testthat::expect_true(all.equal(actual, expected))
}
4 changes: 2 additions & 2 deletions tests/testthat/test-grd-subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ test_that("grd_cell_xy() works for grd_rct()", {
empty <- grd_rct(matrix(nrow = 0, ncol = 0))
expect_identical(
grd_cell_xy(empty, 0, 0),
xy(NA, NA)
xy(NaN, NaN)
)

grid <- grd(nx = 3, ny = 2)
Expand All @@ -397,7 +397,7 @@ test_that("grd_cell_xy() works for grd_xy()", {
empty <- grd_xy(matrix(nrow = 0, ncol = 0))
expect_identical(
grd_cell_xy(empty, 0, 0),
xy(NA, NA)
xy(NaN, NaN)
)

grid <- grd(nx = 3, ny = 2, type = "centers")
Expand Down
15 changes: 11 additions & 4 deletions tests/testthat/test-pkg-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,26 @@ test_that("conversion from bbox to rct works", {
test_that("conversion to sf works", {
skip_if_not_installed("sf")

# Use NaN/NaN instead of NA/NA because Waldo cares about this comparison
sfc <- sf::st_sfc(sf::st_point(), sf::st_point(c(0, 1)), NULL, crs = 4326)
sf <- sf::st_as_sf(new_data_frame(list(geometry = sfc)))
wkb <- as_wkb(c("POINT EMPTY", "POINT (0 1)", NA), crs = 4326)
wkt <- as_wkt(c("POINT EMPTY", "POINT (0 1)", NA), crs = 4326)

expect_equal(sf::st_as_sf(wkb), sf)
expect_equal(sf::st_as_sfc(wkb), sfc)
expect_equal_ignore_na_nan(sf::st_as_sf(wkb), sf)
expect_equal_ignore_na_nan(sf::st_as_sfc(wkb), sfc)
expect_equal(sf::st_as_sf(wkt), sf)
expect_equal(sf::st_as_sfc(wkt), sfc)

# xy
expect_equal(sf::st_as_sf(xy(c(NA, 0, NA), c(NA, 1, NA), crs = 4326)), sf)
expect_equal(sf::st_as_sfc(xy(c(NA, 0, NA), c(NA, 1, NA), crs = 4326)), sfc)
expect_equal_ignore_na_nan(
sf::st_as_sf(xy(c(NA, 0, NA), c(NA, 1, NA), crs = 4326)),
sf
)
expect_equal_ignore_na_nan(
sf::st_as_sfc(xy(c(NA, 0, NA), c(NA, 1, NA), crs = 4326)),
sfc
)

# xy with all !is.na() uses faster sf conversion with coords
expect_equal(sf::st_as_sf(xy(0, 1, crs = 4326)), sf[2,, , drop = FALSE])
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-sfc-writer.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("sfc_writer() works with fixed-length input", {
expect_identical(wk_handle(wkb(), sfc_writer()), sf::st_sfc())

# empties (equal because of NaN/NA difference for POINT)
expect_equal(
expect_equal_ignore_na_nan(
wk_handle(
as_wkb(
c("POINT EMPTY", "LINESTRING EMPTY", "POLYGON EMPTY",
Expand All @@ -25,7 +25,7 @@ test_that("sfc_writer() works with fixed-length input", {

# subtely different for WKT, since a point will fire zero coordinates
# whereas for WKB it will fire (NaN, NaN)
expect_equal(
expect_equal_ignore_na_nan(
wk_handle(
as_wkt(
c("POINT EMPTY", "LINESTRING EMPTY", "POLYGON EMPTY",
Expand Down Expand Up @@ -125,7 +125,7 @@ test_that("sfc_writer() turns NULLs into EMPTY", {
)

for (i in seq_along(all_types)) {
expect_equal(
expect_equal_ignore_na_nan(
wk_handle(c(all_types[i], wkb(list(NULL))), sfc_writer()),
wk_handle(c(all_types[i], all_types[i]), sfc_writer())
)
Expand Down Expand Up @@ -155,7 +155,7 @@ test_that("sfc_writer() turns NULLs into EMPTY", {

for (i in seq_along(all_types)) {
vec <- wk_handle(c(all_types_non_empty[i], wkb(list(NULL))), sfc_writer())
expect_equal(vec[[2]], wk_handle(all_types[i], sfc_writer())[[1]])
expect_equal_ignore_na_nan(vec[[2]], wk_handle(all_types[i], sfc_writer())[[1]])
expect_s3_class(vec, paste0("sfc_", types[i]))
}

Expand All @@ -169,7 +169,7 @@ test_that("sfc_writer() turns NULLs into EMPTY", {
)

for (i in seq_along(all_types)) {
expect_equal(
expect_equal_ignore_na_nan(
wk_handle(c(zm_types[i], wkb(list(NULL))), sfc_writer()),
wk_handle(c(zm_types[i], zm_types_empty[i]), sfc_writer())
)
Expand Down