Skip to content

Commit

Permalink
Use expect_equivalent instead of expect_equal to avoid fragile testin…
Browse files Browse the repository at this point in the history
…g of proj4strings. #88
  • Loading branch information
ateucher committed Aug 7, 2019
1 parent 9d75917 commit ed6621a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ Suggests:
VignetteBuilder: knitr
LazyData: TRUE
Roxygen: list(markdown = TRUE)
RoxygenNote: 6.1.0
RoxygenNote: 6.1.1
11 changes: 5 additions & 6 deletions tests/testthat/test-innerlines.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ test_that("ms_innerlines works with all classes", {

expect_is(ms_innerlines(unclass(poly_geo_json)), "geo_json")
expect_is(ms_innerlines(poly_geo_json), "geo_json")
expect_equal(ms_innerlines(poly_geo_list), geojson_list(expected_json))
expect_equal(ms_innerlines(poly_spdf), expected_sp)
expect_equal(ms_innerlines(poly_sp), expected_sp)

expect_equivalent(ms_innerlines(poly_geo_list), geojson_list(expected_json))
expect_equivalent(ms_innerlines(poly_spdf), expected_sp)
expect_equivalent(ms_innerlines(poly_sp), expected_sp)

expected_sf <- st_geometry(read_sf(unclass(expected_json)))
expect_equal(ms_innerlines(poly_sf), expected_sf)
expect_equal(ms_innerlines(poly_sfc), expected_sf)
expect_equivalent(ms_innerlines(poly_sf), expected_sf)
expect_equivalent(ms_innerlines(poly_sfc), expected_sf)
})

test_that("ms_innerlines errors correctly", {
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ test_that("ms_lines works with all classes", {

expect_is(ms_lines(unclass(poly_geo_json)), "geo_json")
expect_is(ms_lines(poly_geo_json), "geo_json")
expect_equal(ms_lines(poly_geo_list), geojson_list(expected_json))
expect_equal(ms_lines(poly_spdf), expected_sp)
expect_equal(ms_lines(poly_sp), as(expected_sp, "SpatialLines"))
expect_equivalent(ms_lines(poly_geo_list), geojson_list(expected_json))
expect_equivalent(ms_lines(poly_spdf), expected_sp)
expect_equivalent(ms_lines(poly_sp), as(expected_sp, "SpatialLines"))


expected_sf <- read_sf(unclass(expected_json))
expected_sf <- expected_sf[, setdiff(names(expected_sf), "rmapshaperid")]
expected_sfc <- st_geometry(expected_sf)

expect_equal(st_geometry(ms_lines(poly_sf)), expected_sfc)
expect_equal(ms_lines(poly_sfc), expected_sfc)
expect_equivalent(st_geometry(ms_lines(poly_sf)), expected_sfc)
expect_equivalent(ms_lines(poly_sfc), expected_sfc)
})

test_that("ms_lines works with fields specified", {
Expand All @@ -59,11 +59,11 @@ test_that("ms_lines works with fields specified", {
expected_sp <- geojson_sp(expected_json, stringsAsFactors = FALSE)

expect_is(ms_lines(poly_geo_json, "foo"), "geo_json")
expect_equal(ms_lines(poly_geo_list, "foo"), geojson_list(expected_json))
expect_equal(ms_lines(poly_spdf, "foo"),
expect_equivalent(ms_lines(poly_geo_list, "foo"), geojson_list(expected_json))
expect_equivalent(ms_lines(poly_spdf, "foo"),
expected_sp[, setdiff(names(expected_sp), "rmapshaperid")])

expect_equal(ms_lines(poly_sf, "foo")$RANK, c(2L,0L,0L,0L))
expect_equivalent(ms_lines(poly_sf, "foo")$RANK, c(2L,0L,0L,0L))
})

test_that("ms_lines errors correctly", {
Expand Down
24 changes: 12 additions & 12 deletions tests/testthat/test-points.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that("ms_points works with defaults", {

expect_is(ms_points(poly_geo_json), "geo_json")
expect_is(ms_points(unclass(poly_geo_json)), "geo_json")
expect_equal(ms_points(poly_geo_list), geojson_list(expected_json))
expect_equivalent(ms_points(poly_geo_list), geojson_list(expected_json))
expect_equivalent(ms_points(poly_spdf), expected_sp)
expect_equivalent(ms_points(poly_sp), as(expected_sp, "SpatialPoints"))

Expand All @@ -36,8 +36,8 @@ test_that("ms_points works with defaults", {
test_that("ms_points works with defaults with sf", {
expected_json <- structure("{\"type\":\"FeatureCollection\",\"features\":[\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-78.4154562738861,-53.95000746272258]},\"properties\":{\"x\":-78,\"y\":-53,\"rmapshaperid\":0}},\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-70.8687480648099,65.19505422895163]},\"properties\":{\"x\":-71,\"y\":65,\"rmapshaperid\":1}},\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[135.65518268439885,63.10517782011297]},\"properties\":{\"x\":135,\"y\":65,\"rmapshaperid\":2}}\n]}", class = c("json", "geo_json"))
expected_sf <- st_read(expected_json, quiet = TRUE, stringsAsFactors = FALSE)[1:2]
expect_equal(ms_points(poly_sf), expected_sf)
expect_equal(ms_points(poly_sfc), st_geometry(expected_sf))
expect_equivalent(ms_points(poly_sf), expected_sf)
expect_equivalent(ms_points(poly_sfc), st_geometry(expected_sf))

skip_if_not(has_sys_mapshaper())
expect_is(ms_points(poly_sf, sys = TRUE), "sf")
Expand All @@ -50,14 +50,14 @@ test_that("ms_points works with location=centroid", {
expected_sp <- geojson_sp(expected_json)
expected_sp <- expected_sp[, setdiff(names(expected_sp), "rmapshaperid")]

expect_equal(ms_points(poly_geo_json, location = "centroid"), ms_points(poly_geo_json))
expect_equivalent(ms_points(poly_geo_json, location = "centroid"), ms_points(poly_geo_json))
expect_is(ms_points(poly_geo_json, location = "centroid"), "geo_json")
expect_equal(ms_points(poly_geo_list, location = "centroid"), geojson_list(expected_json))
expect_equivalent(ms_points(poly_geo_list, location = "centroid"), geojson_list(expected_json))
expect_equivalent(ms_points(poly_spdf, location = "centroid"), expected_sp)

expected_sf <- st_read(expected_json, quiet = TRUE, stringsAsFactors = FALSE)[1:2]
expect_equal(ms_points(poly_sf, location = "centroid"), expected_sf)
expect_equal(ms_points(poly_sfc, location = "centroid"), st_geometry(expected_sf))
expect_equivalent(ms_points(poly_sf, location = "centroid"), expected_sf)
expect_equivalent(ms_points(poly_sfc, location = "centroid"), st_geometry(expected_sf))
})

test_that("ms_points works with location=inner", {
Expand All @@ -67,12 +67,12 @@ test_that("ms_points works with location=inner", {
expected_sp <- expected_sp[, setdiff(names(expected_sp), "rmapshaperid")]

expect_is(ms_points(poly_geo_json, location = "inner"), "geo_json")
expect_equal(ms_points(poly_geo_list, location = "inner"), geojson_list(expected_json))
expect_equivalent(ms_points(poly_geo_list, location = "inner"), geojson_list(expected_json))
expect_equivalent(ms_points(poly_spdf, location = "inner"), expected_sp)

expected_sf <- st_read(expected_json, quiet = TRUE, stringsAsFactors = FALSE)[1:2]
expect_equal(ms_points(poly_sf, location = "inner"), expected_sf, tolerance = 0.0001)
expect_equal(ms_points(poly_sfc, location = "inner"), st_geometry(expected_sf), tolerance = 0.0001)
expect_equivalent(ms_points(poly_sf, location = "inner"), expected_sf, tolerance = 0.0001)
expect_equivalent(ms_points(poly_sfc, location = "inner"), st_geometry(expected_sf), tolerance = 0.0001)
})

test_that("ms_points works with x and y", {
Expand All @@ -82,10 +82,10 @@ test_that("ms_points works with x and y", {
expected_sp <- expected_sp[, setdiff(names(expected_sp), "rmapshaperid")]

expect_is(ms_points(poly_geo_json, x = "x", y = "y"), "geo_json")
expect_equal(ms_points(poly_geo_list, x = "x", y = "y"), geojson_list(expected_json))
expect_equivalent(ms_points(poly_geo_list, x = "x", y = "y"), geojson_list(expected_json))
expect_equivalent(ms_points(poly_spdf, x = "x", y = "y"), expected_sp)

expect_equal(ms_points(poly_sf, x = "x", y = "y"),
expect_equivalent(ms_points(poly_sf, x = "x", y = "y"),
st_read(expected_json, quiet = TRUE, stringsAsFactors = FALSE)[1:2])
})

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-simplify.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ test_that("ms_simplify.SpatialPolygons works with defaults", {

expect_is(default_simplify_spdf, "SpatialPolygonsDataFrame")
expect_is(default_simplify_sp, "SpatialPolygons")
expect_equal(default_simplify_sp, as(default_simplify_spdf, "SpatialPolygons"))
expect_equivalent(default_simplify_sp, as(default_simplify_spdf, "SpatialPolygons"))
expect_equal(default_simplify_spdf@polygons[[1]]@Polygons[[1]]@coords,
structure(c(-7.1549869, -7.344442, 1.8629117, -6.3787009, -9.6292336,
-7.1549869, 45.4449053, 37.6863061, 35.5400723, 28.8026166, 41.0325088,
Expand Down Expand Up @@ -208,7 +208,7 @@ test_that("ms_simplify.SpatialPolygonsDataFrame works keep_shapes = FALSE and ig
out <- ms_simplify(multipoly_spdf, keep_shapes = FALSE, drop_null_geometries = TRUE)
expect_equal(length(out@polygons), 1)
out_nodrop <- ms_simplify(multipoly_spdf, keep_shapes = FALSE, drop_null_geometries = FALSE)
expect_equal(out, out_nodrop)
expect_equivalent(out, out_nodrop)
})

test_that("ms_simplify works with lines", {
Expand All @@ -217,7 +217,7 @@ test_that("ms_simplify works with lines", {
expect_equal(clean_ws(ms_simplify(line, keep = 0.1)), clean_ws(expected_json))
expect_equal(ms_simplify(line_list, keep = 0.1), geojson_list(expected_json), tolerance = 1e-7)
expect_equivalent(ms_simplify(line_spdf, keep = 0.1), geojson_sp(expected_json))
expect_equal(ms_simplify(line_sp, keep = 0.1), as(ms_simplify(line_spdf, keep = 0.1), "SpatialLines"))
expect_equivalent(ms_simplify(line_sp, keep = 0.1), as(ms_simplify(line_spdf, keep = 0.1), "SpatialLines"))
})

test_that("ms_simplify works correctly when all geometries are dropped", {
Expand Down

0 comments on commit ed6621a

Please sign in to comment.