Skip to content

Commit ee27180

Browse files
committed
Lints to tests
1 parent 1e44daf commit ee27180

File tree

10 files changed

+44
-55
lines changed

10 files changed

+44
-55
lines changed

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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ 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"))
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", {
@@ -47,20 +47,20 @@ test_that("sf_proj_info works for datum files", {
4747

4848
test_that("$.crs works", {
4949
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))
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", {
5555
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))
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_geos.R

Lines changed: 8 additions & 8 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))
@@ -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_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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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))

tests/testthat/test_sfc.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test_that("we can print sfc objects", {
44
s1 = st_sf(a = c("x", "y"), geom = st_sfc(pt1, pt2))
55
expect_output(print(s1), "Simple feature collection")
66
expect_output(print(st_sfc()), "Geometry set for 0 features")
7-
expect_equal(length(st_sfc()), 0)
7+
expect_length(st_sfc(), 0)
88
})
99

1010
test_that("st_is_longlat works", {
@@ -33,8 +33,8 @@ test_that("st_as_binary handles non-native big endian", {
3333
as.raw(1)
3434
}
3535
r[2:5] = rev(r[2:5]) # swap bytes
36-
expect_identical(gc, st_as_sfc(structure(list(r), class = "WKB"), pureR = T)[[1]])
37-
expect_identical(gc, st_as_sfc(structure(list(r), class = "WKB"), pureR = T, EWKB = TRUE)[[1]])
36+
expect_identical(gc, st_as_sfc(structure(list(r), class = "WKB"), pureR = TRUE)[[1]])
37+
expect_identical(gc, st_as_sfc(structure(list(r), class = "WKB"), pureR = TRUE, EWKB = TRUE)[[1]])
3838
})
3939

4040
test_that("st_crs<- gives warnings on changing crs", {

tests/testthat/test_sfg.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ test_that("Ops work for sfc", {
6262
hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE)
6363
hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE)
6464
pts = list(outer, hole1, hole2)
65-
expect_true(inherits(st_multipolygon(list(pts)) * 2 + 3, "MULTIPOLYGON"))
65+
expect_s3_class(st_multipolygon(list(pts)) * 2 + 3, "MULTIPOLYGON")
6666
gc = st_geometrycollection(list(st_multipolygon(list(pts)), st_point(c(2,2))))
6767
m = matrix(0, 2, 2)
6868
diag(m) = c(1, 3)
69-
expect_true(inherits(gc * m - 3, "GEOMETRYCOLLECTION"))
69+
expect_s3_class(gc * m - 3, "GEOMETRYCOLLECTION")
7070
})
7171

7272
test_that("Ops work for sfg", {

tests/testthat/test_sp.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test_that("we can convert SpatialPolygons objects without SF comments to sfc and
4545
lp <- list(p1, p2, p13, p7, p7a, p6, p5, p4, p3, p8, p11, p12, p9, p9a, p10)
4646
spls <- SpatialPolygons(list(Polygons(lp, ID="1")))
4747
expect_equal(comment(spls), "FALSE")
48-
expect_true(is.null(comment(slot(spls, "polygons")[[1]])))
48+
expect_null(comment(slot(spls, "polygons")[[1]]))
4949
spls_sfc <- sf::st_as_sfc(spls)
5050
# rsbivand fork coerce_comments 2022-12-21
5151
spls_rt <- as(spls_sfc, "Spatial")

tests/testthat/test_tidy.R

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
33

44
test_that("select works", {
55
skip_if_not_installed("dplyr")
6-
expect_true(nc %>% select_("AREA", attr(., "sf_column")) %>% inherits("sf"))
7-
expect_true(nc %>% select(AREA) %>% inherits("sf"))
6+
expect_s3_class(nc %>% select("AREA", attr(., "sf_column")), "sf")
7+
expect_s3_class(nc %>% select(AREA), "sf")
88
})
99

1010
test_that("filter to sfc works", {
@@ -24,9 +24,9 @@ suppressMessages(require(tidyr, quietly = TRUE))
2424
test_that("separate and unite work", {
2525
skip_if_not_installed("dplyr")
2626
skip_if_not_installed("tidyr")
27-
expect_true(nc %>% separate(CNTY_ID, c("a", "b"), sep = 2) %>% inherits("sf"))
28-
expect_true(nc %>% separate(CNTY_ID, c("a", "b"), sep = 2) %>%
29-
unite(CNTY_ID_NEW, c("a", "b"), sep = "") %>% inherits("sf"))
27+
expect_s3_class(nc %>% separate(CNTY_ID, c("a", "b"), sep = 2), "sf")
28+
expect_s3_class(nc %>% separate(CNTY_ID, c("a", "b"), sep = 2) %>%
29+
unite(CNTY_ID_NEW, c("a", "b"), sep = ""), "sf")
3030
})
3131

3232
test_that("separate_rows work", {
@@ -39,9 +39,7 @@ test_that("separate_rows work", {
3939
st_point(c(2, 2)),
4040
st_point(c(3, 3))),
4141
stringsAsFactors = FALSE))
42-
expect_true(d %>%
43-
separate_rows(y, convert = TRUE) %>%
44-
inherits("sf"))
42+
expect_s3_class(separate_rows(d, y, convert = TRUE), "sf")
4543
expect_identical(d %>%
4644
separate_rows(y, convert = TRUE) %>%
4745
st_geometry(),
@@ -57,7 +55,7 @@ test_that("group/ungroup works", {
5755
skip_if_not_installed("dplyr")
5856
tbl = tibble(a = c(1,1,2,2), g = st_sfc(st_point(0:1), st_point(1:2), st_point(2:3), st_point(3:4)))
5957
d = st_sf(tbl)
60-
e <- d %>% group_by(a) %>% ungroup
58+
e <- d %>% group_by(a) %>% ungroup()
6159
expect_equal(as.data.frame(d), as.data.frame(e))
6260
})
6361

@@ -67,10 +65,10 @@ test_that("sample_n etc work", {
6765
d = st_sf(tbl)
6866

6967
expect_sampled <- function(x) {
70-
expect_true(inherits(x, c("sf", "tbl_df")))
68+
expect_s3_class(x, c("sf", "tbl_df"))
7169
expect_named(x, c("a", "g"))
7270
expect_equal(nrow(x), 2)
73-
expect_true(inherits(x$g, "sfc_POINT"))
71+
expect_s3_class(x$g, "sfc_POINT")
7472
}
7573

7674
expect_sampled(sample_n(d, 2))
@@ -86,16 +84,8 @@ test_that("nest() works", {
8684

8785
exp_data = list(d[1:2, "g"], d[3:4, "g"])
8886

89-
# Work around issues of tibble comparison in dplyr 0.8.5 (faulty
90-
# all.equal.tbl_df() method)
91-
if (utils::packageVersion("dplyr") < "0.8.99") {
92-
dfs = lapply(out$data, function(x) st_sf(as.data.frame(x)))
93-
exp_data = lapply(exp_data, function(x) st_sf(as.data.frame(x)))
94-
expect_identical(dfs, exp_data)
95-
} else {
96-
exp = tibble(a = c(1, 2), data = exp_data) %>% group_by(a)
97-
expect_identical(out, exp)
98-
}
87+
exp = tibble(a = c(1, 2), data = exp_data) %>% group_by(a)
88+
expect_identical(out, exp)
9989
})
10090

10191
test_that("st_intersection of tbl returns tbl", {
@@ -286,17 +276,17 @@ test_that("`select()` and `transmute()` observe back-stickiness of geometry colu
286276

287277
test_that("rowwise_df class is retained on row slice", {
288278
skip_if_not_installed("dplyr")
289-
expect_true(nc %>% rowwise() %>% slice(1) %>% inherits("rowwise_df"))
279+
expect_s3_class(slice(rowwise(nc), 1), "rowwise_df")
290280
})
291281

292282
test_that("grouped_df class is retained on row slice", {
293283
skip_if_not_installed("dplyr")
294-
expect_true(nc %>% group_by(PERIMETER > 2) %>% slice(1) %>% inherits("grouped_df"))
284+
expect_s3_class(nc %>% group_by(PERIMETER > 2) %>% slice(1), "grouped_df")
295285
})
296286

297287
test_that("rowwise_df class is retained on filtered rows", {
298288
skip_if_not_installed("dplyr")
299-
expect_true(nc %>% rowwise() %>% filter(AREA > .1) %>% inherits("rowwise_df"))
289+
expect_s3_class(nc %>% rowwise() %>% filter(AREA > .1), "rowwise_df")
300290
})
301291

302292
test_that("`group_split.sf()` ignores `.keep` for rowwise_df class", {

tests/testthat/test_zm_range.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ test_that("XYM-only objects correctly calculate M (and not Z)", {
156156

157157
expect_true( all( sf::st_m_range( sf_m ) == sf::st_z_range( sf_z ) ) )
158158

159-
expect_true( is.null( sf::st_z_range( sf_m ) ) )
160-
expect_true( is.null( sf::st_m_range( sf_z ) ) )
161-
159+
expect_null(sf::st_z_range(sf_m))
160+
expect_null(sf::st_m_range(sf_z))
162161
})

0 commit comments

Comments
 (0)