Skip to content

Commit

Permalink
Use expect_identical() and expect_named() where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisaloo authored and Matthias Grenié committed Sep 6, 2022
1 parent 830267c commit 0a58ab6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 61 deletions.
12 changes: 5 additions & 7 deletions tests/testthat/test-fdis.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ test_that("Functional Dispersion output format", {
fdis <- expect_silent(fd_fdis(traits_birds))

expect_s3_class(fdis, "data.frame")
expect_length(fdis, 2)
expect_equal(nrow(fdis), 1)
expect_equal(colnames(fdis), c("site", "FDis"))
expect_identical(dim(fdis), c(1L, 2L))
expect_named(fdis, c("site", "FDis"))

expect_equal(fdis$FDis, 146.2072, tolerance = 1e-7)
})
Expand Down Expand Up @@ -40,7 +39,7 @@ test_that("Functional Dispersion works for site with no species", {
fd_fdis(traits_plants, site_sp_plants[10,, drop = FALSE])
)

expect_equal(fdis$FDis[[1]], 0)
expect_identical(fdis$FDis[[1]], 0)
})

test_that("Functional Dispersion works in 1D", {
Expand All @@ -64,9 +63,8 @@ test_that("Functional Dispersion works with sparse matrices", {
fdis <- expect_silent(fd_fdis(traits_birds, sparse_site_sp))

expect_s3_class(fdis, "data.frame")
expect_length(fdis, 2)
expect_equal(nrow(fdis), 1)
expect_equal(colnames(fdis), c("site", "FDis"))
expect_identical(dim(fdis), c(1L, 2L))
expect_named(fdis, c("site", "FDis"))

expect_equal(
fd_fdis(traits_birds, sparse_site_sp),
Expand Down
12 changes: 5 additions & 7 deletions tests/testthat/test-fdiv.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ test_that("Functional Divergence output format", {
fdiv <- expect_silent(fd_fdiv(traits_birds))

expect_s3_class(fdiv, "data.frame")
expect_length(fdiv, 2)
expect_equal(nrow(fdiv), 1)
expect_equal(colnames(fdiv), c("site", "FDiv"))
expect_identical(dim(fdiv), c(1L, 2L))
expect_named(fdiv, c("site", "FDiv"))

expect_equal(fdiv$FDiv, 0.7282172, tolerance = 1e-7)
})
Expand Down Expand Up @@ -40,7 +39,7 @@ test_that("Functional Divergence works for site with no species", {
fd_fdiv(traits_plants, site_sp_plants[10,, drop = FALSE])
)

expect_equal(fdiv$FDiv[[1]], 0)
expect_identical(fdiv$FDiv[[1]], 0)
})

test_that("Functional Divergence works in 1D", {
Expand All @@ -64,9 +63,8 @@ test_that("Functional Divergence works with sparse matrices", {
fdiv <- expect_silent(fd_fdiv(traits_birds, sparse_site_sp))

expect_s3_class(fdiv, "data.frame")
expect_length(fdiv, 2)
expect_equal(nrow(fdiv), 1)
expect_equal(colnames(fdiv), c("site", "FDiv"))
expect_identical(dim(fdiv), c(1L, 2L))
expect_named(fdiv, c("site", "FDiv"))

expect_equal(
fd_fdiv(traits_birds, sparse_site_sp),
Expand Down
35 changes: 19 additions & 16 deletions tests/testthat/test-feve.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ test_that("Functional Evenness output format", {
feve <- expect_silent(fd_feve(traits_birds, sp_com = simple_site_sp))

expect_s3_class(feve, "data.frame")
expect_length(feve, 2)
expect_equal(nrow(feve), 1)
expect_equal(colnames(feve), c("site", "FEve"))
expect_identical(dim(feve), c(1L, 2L))
expect_named(feve, c("site", "FEve"))


feve <- expect_silent(fd_feve(traits_birds))

expect_s3_class(feve, "data.frame")
expect_length(feve, 2)
expect_equal(nrow(feve), 1)
expect_equal(colnames(feve), c("site", "FEve"))
expect_identical(dim(feve), c(1L, 2L))
expect_named(feve, c("site", "FEve"))

})

Expand All @@ -41,7 +39,7 @@ test_that("Functional Evenness computation are in line with other packages", {

abund_mat <- matrix(1, ncol = 3, dimnames = list("site1", letters[1:3]))

expect_equal(fd_feve(sp_com = abund_mat, dist_matrix = test_dissim)$FEve, 1)
expect_identical(fd_feve(sp_com = abund_mat, dist_matrix = test_dissim)$FEve, 1)
})

test_that("Functional Evenness works in 1D", {
Expand Down Expand Up @@ -71,9 +69,9 @@ test_that("Functional Evenness works on subset of site/species", {
test_that("Functional Evenness edge cases", {

# n_species = 2 < 3 so not possible to compute FEve
expect_equal(fd_feve(traits_birds[1:2,],
expect_identical(fd_feve(traits_birds[1:2,],
simple_site_sp[, 1:2, drop = FALSE])[["FEve"]],
NA_real_)
NA_real_)


# n_species = 0, so no definition of FEve
Expand All @@ -84,7 +82,7 @@ test_that("Functional Evenness edge cases", {
fd_feve(traits_plants, site_sp_plants[10,, drop = FALSE])
)

expect_equal(feve$FEve[[1]], NA_real_)
expect_identical(feve$FEve[[1]], NA_real_)
})

test_that("Functional Evenness works on sparse matrices", {
Expand All @@ -102,21 +100,26 @@ test_that("Functional Evenness works on sparse matrices", {
# Only site-species matrix is sparse
expect_silent(fd_feve(traits_birds, sparse_site_sp))

expect_equal(fd_feve(traits_birds, sparse_site_sp)$FEve, 0.3743341,
tolerance = 1e-6)
expect_equal(
fd_feve(traits_birds, sparse_site_sp),
fd_feve(traits_birds, site_sp)
)

# Only distance matrix is sparse
expect_silent(fd_feve(sp_com = site_sp, dist_matrix = sparse_dist_mat))

expect_equal(fd_feve(sp_com = site_sp, dist_matrix = sparse_dist_mat)$FEve,
0.3743341, tolerance = 1e-6)
expect_equal(
fd_feve(sp_com = site_sp, dist_matrix = sparse_dist_mat),
fd_feve(sp_com = site_sp, dist_matrix = dist(traits_birds))
)

# Both site-species and distance matrix are sparse
expect_silent(fd_feve(sp_com = sparse_site_sp, dist_matrix = sparse_dist_mat))

expect_equal(
fd_feve(sp_com = sparse_site_sp, dist_matrix = sparse_dist_mat)$FEve,
0.3743341, tolerance = 1e-6)
fd_feve(sp_com = sparse_site_sp, dist_matrix = sparse_dist_mat),
fd_feve(sp_com = site_sp, dist_matrix = dist(traits_birds))
)
})


Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-fric.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ test_that("Functional Richness output format", {
fric <- expect_silent(fd_fric(traits_birds))

expect_s3_class(fric, "data.frame")
expect_length(fric, 2)
expect_equal(nrow(fric), 1)
expect_equal(colnames(fric), c("site", "FRic"))
expect_identical(dim(fric), c(1L, 2L))
expect_named(fric, c("site", "FRic"))

expect_equal(fd_fric(traits_birds)$FRic, 230967.7, tolerance = 1e-6)
})
Expand Down Expand Up @@ -58,8 +57,8 @@ test_that("Functional Richness can standardize its values", {
fric <- fd_fric(traits_birds, stand = TRUE)
fric_low_1 <- suppressMessages(fd_fric(traits_birds, site_sp2, stand = TRUE))

expect_equal(fric$FRic[[1]], 1)
expect_equal(fric_low_1$FRic[[1]], 1)
expect_identical(fric$FRic[[1]], 1)
expect_identical(fric_low_1$FRic[[1]], 1)
expect_lt(fric_low_1$FRic[[2]], 1)
})

Expand Down Expand Up @@ -107,7 +106,7 @@ test_that("Functional Richness edge cases", {
fd_fric(traits_plants, site_sp_plants[10,, drop = FALSE])
)

expect_equal(fric$FRic[[1]], NA_real_)
expect_identical(fric$FRic[[1]], NA_real_)

})

Expand All @@ -121,10 +120,11 @@ test_that("Functional Richness works on sparse matrices", {

sparse_site_sp <- Matrix(site_sp, sparse = TRUE)

expect_silent(fd_fric(traits_birds, sparse_site_sp))
sparse_fric <- expect_silent(fd_fric(traits_birds, sparse_site_sp))

expect_equal(fd_fric(traits_birds, sparse_site_sp)$FRic, 230967.7,
tolerance = 1e-6)
fric <- expect_silent(fd_fric(traits_birds, site_sp))

expect_equal(fric, sparse_fric)
})


Expand Down
12 changes: 5 additions & 7 deletions tests/testthat/test-fric_intersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ test_that("Functional Richness Intersection output format", {
fric_int <- expect_silent(fd_fric_intersect(traits_birds))

expect_s3_class(fric_int, "data.frame")
expect_length(fric_int, 3)
expect_equal(nrow(fric_int), 1)
expect_equal(colnames(fric_int), c("first_site", "second_site",
"FRic_intersect"))
expect_identical(dim(fric_int), c(1L, 3L))
expect_named(fric_int, c("first_site", "second_site", "FRic_intersect"))

expect_equal(fd_fric_intersect(traits_birds, stand = TRUE)$FRic_intersect,
1, tolerance = 1e-6)
Expand Down Expand Up @@ -65,8 +63,8 @@ test_that("Functional Richness Intersection can standardize its values", {
fric_int_low_1 <- suppressMessages(fd_fric_intersect(traits_birds, site_sp2,
stand = TRUE))

expect_equal(fric_int$FRic_intersect[[1]], 1)
expect_equal(fric_int_low_1$FRic_intersect[[2]], 1)
expect_identical(fric_int$FRic_intersect[[1]], 1)
expect_identical(fric_int_low_1$FRic_intersect[[2]], 1)
expect_lt(fric_int_low_1$FRic_intersect[[3]], 1)
expect_lt(fric_int_low_1$FRic_intersect[[1]], 1)
})
Expand Down Expand Up @@ -131,7 +129,7 @@ test_that("Functional Richness Intersection edge cases", {
fd_fric_intersect(traits_plants, site_sp_plants[10,, drop = FALSE])
)

expect_equal(fric_inter$FRic_intersect[[1]], NA_real_)
expect_identical(fric_inter$FRic_intersect[[1]], NA_real_)
})

test_that("Functional Richness Intersection works on sparse matrices", {
Expand Down
29 changes: 16 additions & 13 deletions tests/testthat/test-raoq.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ test_that("Rao's entropy output format", {
rq <- expect_silent(fd_raoq(traits_birds, sp_com = simple_site_sp))

expect_s3_class(rq, "data.frame")
expect_length(rq, 2)
expect_equal(nrow(rq), 1)
expect_equal(colnames(rq), c("site", "Q"))
expect_identical(dim(rq), c(1L, 2L))
expect_named(rq, c("site", "Q"))


rq <- expect_silent(fd_raoq(traits_birds))

expect_s3_class(rq, "data.frame")
expect_length(rq, 2)
expect_equal(nrow(rq), 1)
expect_equal(colnames(rq), c("site", "Q"))
expect_identical(dim(rq), c(1L, 2L))
expect_named(rq, c("site", "Q"))

})

Expand Down Expand Up @@ -62,7 +60,7 @@ test_that("Rao's quadratric entropy works for sites with no species", {
fd_raoq(traits_plants, site_sp_plants[10,, drop = FALSE])
)

expect_equal(raoq$Q[[1]], 0)
expect_identical(raoq$Q[[1]], 0)
})

test_that("Rao's Quadratic Entropy works on sparse matrices", {
Expand All @@ -80,21 +78,26 @@ test_that("Rao's Quadratic Entropy works on sparse matrices", {
# Only site-species matrix is sparse
expect_silent(fd_raoq(traits_birds, sparse_site_sp))

expect_equal(fd_raoq(traits_birds, sparse_site_sp)$Q, 170.0519,
tolerance = 1e-6)
expect_equal(
fd_raoq(traits_birds, sparse_site_sp),
fd_raoq(traits_birds, site_sp)
)

# Only distance matrix is sparse
expect_silent(fd_raoq(sp_com = site_sp, dist_matrix = sparse_dist_mat))

expect_equal(fd_raoq(sp_com = site_sp, dist_matrix = sparse_dist_mat)$Q,
170.0519, tolerance = 1e-6)
expect_equal(
fd_raoq(sp_com = site_sp, dist_matrix = sparse_dist_mat),
fd_raoq(sp_com = site_sp, dist_matrix = dist(traits_birds))
)

# Both site-species and distance matrix are sparse
expect_silent(fd_raoq(sp_com = sparse_site_sp, dist_matrix = sparse_dist_mat))

expect_equal(
fd_raoq(sp_com = sparse_site_sp, dist_matrix = sparse_dist_mat)$Q,
170.0519, tolerance = 1e-6)
fd_raoq(sp_com = sparse_site_sp, dist_matrix = sparse_dist_mat),
fd_raoq(sp_com = site_sp, dist_matrix = dist(traits_birds))
)

})

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-remove_species_without_trait.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("remove_species_without_trait() works", {
data("traits_birds")

expect_silent(remove_species_without_trait(traits_birds))
expect_equal(remove_species_without_trait(traits_birds), traits_birds)
expect_identical(remove_species_without_trait(traits_birds), traits_birds)

traits_birds_missing <- traits_birds
traits_birds_missing[1, 1] <- NA
Expand All @@ -16,7 +16,7 @@ test_that("remove_species_without_trait() works", {

traits_birds_missing[2, 2] <- NA

expect_equal(
expect_identical(
suppressMessages(remove_species_without_trait(traits_birds_missing)),
traits_birds_missing[3:nrow(traits_birds), ])
expect_message(remove_species_without_trait(traits_birds_missing),
Expand Down

0 comments on commit 0a58ab6

Please sign in to comment.