Skip to content

Commit a5f9f0f

Browse files
authored
Merge pull request #2288 from olivroy/tests
Various Tests lints
2 parents b4e355d + d1526f7 commit a5f9f0f

File tree

16 files changed

+197
-188
lines changed

16 files changed

+197
-188
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Suggests:
7575
blob,
7676
nanoarrow,
7777
covr,
78-
dplyr (>= 0.8-3),
78+
dplyr (>= 1.0.0),
7979
ggplot2,
8080
knitr,
8181
lwgeom (>= 0.2-1),

tests/sfc.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ p <- st_sample(x, 10)
200200
x = st_sfc(st_polygon(list(rbind(c(-180,-90),c(180,-90),c(180,90),c(-180,90),c(-180,-90)))),
201201
crs=st_crs(4326))
202202
#FIXME:
203-
# if (sf_extSoftVersion()["proj.4"] >= "4.9.0") # lwgeom breaks on this
203+
# if (sf_extSoftVersion()[["proj.4"]] >= "4.9.0") # lwgeom breaks on this
204204
# (p <- st_sample(x, 10))
205205
pt = st_multipoint(matrix(1:20,,2))
206206
st_sample(p, 3)

tests/testthat/test-normalize.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ test_that("normalize", {
66

77
p1 <- st_multipoint(matrix(runif(20, max = 25), ncol = 2))
88
p1_norm <- st_normalize(p1)
9-
expect_true(all(st_bbox(p1_norm) == c(0,0,1,1)))
9+
expect_equal(unclass(st_bbox(p1_norm)), c(0,0,1,1), check.attributes = FALSE)
1010

1111
p2 <- st_polygon(list(matrix(runif(10, max = 100), ncol = 2)[c(1:5, 1), ]))
1212
sfc <- st_sfc(p1, p2)
1313
sfc_norm <- st_normalize(sfc)
14-
expect_true(all(st_bbox(sfc_norm) == c(0,0,1,1)))
14+
expect_equal(unclass(st_bbox(sfc_norm)), c(0,0,1,1), check.attributes = FALSE)
1515

1616
sf <- st_sf(geometry = sfc)
1717
sf_norm <- st_normalize(sf)
18-
expect_true(all(st_bbox(sf_norm) == c(0,0,1,1)))
18+
expect_equal(unclass(st_bbox(sf_norm)), c(0,0,1,1), check.attributes = FALSE)
1919
expect_equal(sfc_norm, sf_norm$geometry)
2020

2121
})

tests/testthat/test_crs.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_that("st_crs works", {
1717
expect_silent(st_crs(nc1) <- st_crs(nc1))
1818

1919
if (sf_extSoftVersion()[["GDAL"]] > "2.2.3") {
20-
expect_error(st_crs("+proj=ll"), "invalid crs")
20+
suppressWarnings(expect_error(st_crs("+proj=ll"), "invalid crs"))
2121
# expect_error(st_crs("+proj=longlat +datum=NAD26"))
2222
}
2323
expect_silent(st_crs("+proj=longlat"))
@@ -36,31 +36,31 @@ test_that("sf_proj_info works", {
3636
expect_silent(x <- sf_proj_info("datum"))
3737
expect_silent(x <- sf_proj_info("units"))
3838
expect_silent(path <- sf_proj_info("path")[1])
39-
expect_true(is.logical(sf_proj_info(path = path)))
40-
expect_true(is.logical(sf_proj_info("network")))
39+
expect_type(sf_proj_info(path = path), "logical")
40+
expect_type(sf_proj_info("network"), "logical")
4141
})
4242

4343
test_that("sf_proj_info works for datum files", {
44-
skip_if_not(sf_extSoftVersion()[["proj.4"]] < "6.0.0")
44+
skip_if(sf_extSoftVersion()[["proj.4"]] >= "6.0.0")
4545
expect_silent(x <- sf_proj_info("have_datum_files"))
4646
})
4747

4848
test_that("$.crs works", {
49-
skip_if_not(sf_extSoftVersion()[["proj.4"]] < "6.0.0")
50-
expect_true(!is.null(st_crs("+init=epsg:3857")$epsg))
51-
expect_true(is.character(st_crs("+init=epsg:3857")$proj4string))
49+
skip_if(sf_extSoftVersion()[["proj.4"]] >= "6.0.0")
50+
expect_false(is.null(st_crs("+init=epsg:3857")$epsg))
51+
expect_type(st_crs("+init=epsg:3857")$proj4string, "character")
5252
})
5353

5454
test_that("$.crs works with +units", {
55-
skip_if_not(sf_extSoftVersion()[["proj.4"]] < "6.0.0")
56-
expect_true(is.numeric(st_crs("+init=epsg:3857 +units=m")$b))
57-
expect_true(is.character(st_crs("+init=epsg:3857 +units=m")$units))
55+
skip_if(sf_extSoftVersion()[["proj.4"]] >= "6.0.0")
56+
expect_type(st_crs("+init=epsg:3857 +units=m")$b, "double")
57+
expect_type(st_crs("+init=epsg:3857 +units=m")$units, "character")
5858
})
5959

6060
test_that("$.crs works 2", {
6161
skip_if_not(sf_extSoftVersion()[["GDAL"]] < "2.5.0" && sf_extSoftVersion()[["proj.4"]] < "6.0.0")
62-
expect_true(is.numeric(st_crs("+init=epsg:3857 +units=km")$b))
63-
expect_true(is.character(st_crs("+init=epsg:3857 +units=km")$units))
62+
expect_type(st_crs("+init=epsg:3857 +units=km")$b, "double")
63+
expect_type(st_crs("+init=epsg:3857 +units=km")$units, "character")
6464
})
6565

6666
test_that("CRS comparison uses ellipsoid and datum (#180)", {
@@ -108,5 +108,5 @@ test_that("crs.Raster works", {
108108
library(raster)
109109
r = raster()
110110
x = st_crs(r)
111-
expect_equal(class(x), "crs")
111+
expect_s3_class(x, "crs", exact = TRUE)
112112
})

tests/testthat/test_gdal.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ test_that("st_transform works", {
1212
#attr(s2.tr, "crs")$proj4string = ""
1313
st_crs(s1.tr) = NA_crs_
1414
# st_crs(s2.tr) = NA_crs_
15-
# if (sf_extSoftVersion()["proj.4"] < "5.0.0") # FIXME:
15+
# if (sf_extSoftVersion()[["proj.4"]] < "5.0.0") # FIXME:
1616
# expect_equal(s1.tr, s2.tr)
1717

1818
toCrs = 3857
1919
s1.tr = st_transform(s, toCrs)
2020
#attr(s1.tr, "crs")$proj4string = ""
2121
st_crs(s1.tr) = NA_crs_
2222
# st_crs(s2.tr) = NA_crs_
23-
# if (sf_extSoftVersion()["proj.4"] < "5.0.0") # FIXME:
23+
# if (sf_extSoftVersion()[["proj.4"]] < "5.0.0") # FIXME:
2424
# expect_equal(s1.tr, s2.tr)
2525

2626
expect_silent({
@@ -42,8 +42,8 @@ test_that("st_wrap_dateline works", {
4242
})
4343

4444
test_that('gdal_subdatasets works', {
45-
skip_if_not(sf_extSoftVersion()[["GDAL"]] >= "2.1.0")
46-
skip_if_not(sf_extSoftVersion()[["GDAL"]] < "2.5.0") # FIXME:
45+
skip_if(sf_extSoftVersion()[["GDAL"]] < "2.1.0")
46+
skip_if(sf_extSoftVersion()[["GDAL"]] >= "2.5.0") # FIXME:
4747
skip_on_os("mac") # FIXME:
4848
fname = system.file("nc/cropped.nc", package = "sf")
4949
sd2 = gdal_subdatasets(fname)[[2]]

tests/testthat/test_geos.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_that("CPL_geos_is_valid works", {
1717

1818
test_that("geos ops give warnings and errors on longlat", {
1919
skip_if_not_installed("lwgeom")
20-
skip_if_not(!sf_use_s2())
20+
skip_if(sf_use_s2())
2121

2222
nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
2323
x = nc[1:2,]
@@ -92,9 +92,9 @@ test_that("geom operations work on sfg or sfc or sf", {
9292
expect_s3_class(st_boundary(gpnc), "sfc_MULTILINESTRING")
9393
expect_s3_class(st_boundary(gpnc[[1L]]), "MULTILINESTRING")
9494

95-
expect_true(inherits(st_convex_hull(pnc)$geometry, "sfc_POLYGON"))
96-
expect_true(inherits(st_convex_hull(gpnc), "sfc_POLYGON"))
97-
expect_true(inherits(st_convex_hull(gpnc[[1L]]), "POLYGON"))
95+
expect_s3_class(st_convex_hull(pnc)$geometry, "sfc_POLYGON")
96+
expect_s3_class(st_convex_hull(gpnc), "sfc_POLYGON")
97+
expect_s3_class(st_convex_hull(gpnc[[1L]]), "POLYGON")
9898

9999
expect_silent(st_simplify(pnc, FALSE, 1e4))
100100
expect_silent(st_simplify(gpnc, FALSE, 1e4))
@@ -172,7 +172,7 @@ test_that("st_difference works with partially overlapping geometries", {
172172
pl3 = st_polygon(list(matrix(c(0, 1.25, 2, 1.25, 1, 2.5, 0, 1.25), byrow = TRUE, ncol = 2)))
173173
in1 = st_sfc(list(pl1, pl2, pl3))
174174
in2 = st_sf(order = c("A", "B", "C"), geometry = st_sfc(list(pl1, pl2, pl3), crs = 4326), agr = "constant")
175-
if (package_version(gsub("[a-zA-Z]", "", sf_extSoftVersion()["GEOS"])) < "3.9.0") {
175+
if (package_version(gsub("[a-zA-Z]", "", sf_extSoftVersion()[["GEOS"]])) < "3.9.0") {
176176
correct_geom = st_sfc(list(
177177
st_polygon(list(matrix(c(0, 2, 1, 0, 0, 0, 1, 0), ncol = 2))),
178178
st_polygon(list(matrix(c(0.5, 0, 1, 2, 1.5, 1, 0.5, 0.5, 0.5, 1.5, 0.5, 0.5, 1, 0.5), ncol = 2))),
@@ -198,7 +198,7 @@ test_that("st_difference works with partially overlapping geometries", {
198198
expect_equal(attr(out1, "crs"), attr(in1, "crs"))
199199
expect_equal(st_crs(out2), st_crs(in2))
200200
# check that output geometries are actually correct
201-
expect_equal(length(out1), 3)
201+
expect_length(out1, 3)
202202
expect_equal(nrow(out2), 3)
203203
expect_equal(out1[[1]][[1]], correct_geom[[1]][[1]])
204204
expect_equal(out1[[2]][[1]], correct_geom[[2]][[1]])
@@ -228,8 +228,8 @@ test_that("st_difference works with fully contained geometries", {
228228
expect_equal(attr(out1, "crs"), attr(in1, "crs"))
229229
expect_equal(st_crs(out2), st_crs(in2))
230230
# check that output geometries are actually correct
231-
expect_equal(length(out1), 2)
232-
expect_equal(length(out2), 2)
231+
expect_length(out1, 2)
232+
expect_length(out2, 2)
233233
expect_equal(out1[[1]][[1]], correct_geom[[1]][[1]])
234234
#expect_equal(out1[[2]][[1]], correct_geom[[2]][[1]])
235235
#expect_equal(out2[[1]][[1]], correct_geom[[1]][[1]])
@@ -257,5 +257,5 @@ test_that("binary operations work on sf objects with common column names", {
257257

258258
test_that("binary operations on empty sfg objects return NA", {
259259
x = st_point() == st_linestring()
260-
expect_true(is.na(x))
260+
expect_equal(x, NA)
261261
})

tests/testthat/test_postgis_RPostgres.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ test_that("can read using driver", {
357357
RPostgres::Postgres(),
358358
host = "localhost",
359359
dbname = "empty"),
360-
silent=TRUE
360+
silent = TRUE
361361
)
362362
skip_if_not(
363363
can_con(empty),

tests/testthat/test_read.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
test_that("we can read a shapefile using st_read", {
2-
nc <- st_read(system.file("shape/nc.shp", package="sf"), "nc", crs = 4267, quiet = TRUE)
3-
expect_identical(class(nc), c("sf", "data.frame"))
2+
nc <- st_read(system.file("shape/nc.shp", package = "sf"), "nc", crs = 4267, quiet = TRUE)
3+
expect_s3_class(nc, c("sf", "data.frame"), exact = TRUE)
44
expect_equal(dim(nc), c(100, 15))
55
})
66

77
test_that("we can read shapefiles with a query string", {
88
nc <- st_read(system.file("shape/nc.shp", package="sf"), "nc", crs = 4267, quiet = TRUE)
9-
nc_all <- st_read(system.file("shape/nc.shp", package="sf"), "nc", query = "select * from nc", crs = 4267, quiet = TRUE)
10-
nc_some <- st_read(system.file("shape/nc.shp", package="sf"), "nc", query = "select * from nc where SID79 > 50", crs = 4267, quiet = TRUE)
9+
nc_all <- st_read(system.file("shape/nc.shp", package="sf"), query = "select * from nc", crs = 4267, quiet = TRUE)
10+
nc_some <- st_read(system.file("shape/nc.shp", package="sf"), query = "select * from nc where SID79 > 50", crs = 4267, quiet = TRUE)
1111
})
1212

1313
test_that("st_read.default gives error messages", {

tests/testthat/test_sample.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ test_that("st_sample works", {
55
sample_default = st_sample(x = nc, size = n)
66
expect_s3_class(sample_default, "sfc")
77
sample_exact = st_sample(x = nc, size = n, exact = TRUE)
8-
expect_equal(length(sample_exact), n)
8+
expect_length(sample_exact, n)
99
})

tests/testthat/test_sf.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_that("we can create points sf from data.frame", {
2323
meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992)
2424
meuse_sf[1:5,]
2525
summary(meuse_sf[1:5,])
26-
expect_identical(class(meuse_sf), c("sf", "data.frame"))
26+
expect_s3_class(meuse_sf, c("sf", "data.frame"), exact = TRUE)
2727
})
2828

2929
test_that("st_zm works", {
@@ -69,14 +69,14 @@ test_that("st_as_sf bulk points work", {
6969
xyz_sf = st_as_sf(x, coords = c("y", "x", "dist"))
7070
xym_sf = st_as_sf(x, coords = c("y", "x", "dist"), dim = "XYM")
7171
xyzm_sf = st_as_sf(x, coords = c("x", "y", "dist", "zinc"), dim = "XYZM")
72-
expect_identical(class(meuse_sf), c("sf", "data.frame"))
73-
expect_identical(class(xyz_sf), c("sf", "data.frame"))
74-
expect_identical(class(xym_sf), c("sf", "data.frame"))
75-
expect_identical(class(xyzm_sf), c("sf", "data.frame"))
76-
expect_length(unclass(st_geometry(meuse_sf)[[1]]), 2L)
77-
expect_length(unclass(st_geometry(xyz_sf)[[1]]), 3L)
78-
expect_length(unclass(st_geometry(xym_sf)[[1]]), 3L)
79-
expect_length(unclass(st_geometry(xyzm_sf)[[1]]), 4L)
72+
expect_s3_class(meuse_sf, c("sf", "data.frame"), exact = TRUE)
73+
expect_s3_class(xyz_sf, c("sf", "data.frame"), exact = TRUE)
74+
expect_s3_class(xym_sf, c("sf", "data.frame"), exact = TRUE)
75+
expect_s3_class(xyzm_sf, c("sf", "data.frame"), exact = TRUE)
76+
expect_length(st_geometry(meuse_sf)[[1]], 2L)
77+
expect_length(st_geometry(xyz_sf)[[1]], 3L)
78+
expect_length(st_geometry(xym_sf)[[1]], 3L)
79+
expect_length(st_geometry(xyzm_sf)[[1]], 4L)
8080

8181

8282
})
@@ -86,7 +86,7 @@ test_that("transform work", {
8686
data(meuse, package = "sp")
8787
x = st_as_sf(meuse, coords = c("x", "y"), crs = 28992)
8888
x2 = transform(x, elev2 = elev^2, lead_zinc = lead/zinc)
89-
expect_true(inherits(x, 'sf'))
89+
expect_s3_class(x, "sf")
9090
expect_identical(class(x2), class(x))
9191
expect_identical(st_bbox(x), st_bbox(x))
9292
expect_identical(st_crs(x), st_crs(x))
@@ -97,10 +97,10 @@ test_that("empty agr attribute is named after subset", {
9797
sf = st_sf(data.frame(x = st_sfc(st_point(1:2))))
9898
out = sf[, "geometry"]
9999
agr = attr(out, "agr")
100-
expect_identical(names(agr), character())
100+
expect_named(agr, character())
101101
})
102102
test_that("duplicated work",{
103103
sf = st_sf(data.frame(x = st_sfc(st_point(1:2))[rep(1,4)], a=gl(2,2), b=as.numeric(gl(2,2))))
104104
expect_identical(duplicated(sf), c(FALSE,TRUE,FALSE,TRUE))
105-
expect_s3_class(unique(sf),'sf')
105+
expect_s3_class(unique(sf), "sf")
106106
})

0 commit comments

Comments
 (0)