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

Prefer inherits() over methods::is() #92

Closed
wants to merge 2 commits into from
Closed
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
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Imports:
purrr (>= 0.2.2),
tibble (>= 2.0),
ggrepel (>= 0.6.5),
methods (>= 3.4.3),
rlang(>= 0.4.0)
Suggests:
testthat (>= 2.1.0),
Expand Down
2 changes: 1 addition & 1 deletion R/utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' as_cordf(x)
#' as_cordf(x, diagonal = 1)
as_cordf <- function(x, diagonal = NA) {
if(methods::is(x, "cor_df")) {
if(inherits(x, "cor_df")) {
warning("x is already a correlation data frame.")
return(x)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-as_cordf.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ context("as_cordf")
d <- cor(mtcars)

test_that("Inherits correct classes", {
expect_is(as_cordf(d), "cor_df")
expect_is(as_cordf(d), "tbl")
expect_s3_class(as_cordf(d), "cor_df")
expect_s3_class(as_cordf(d), "tbl")
expect_warning(as_cordf(as_cordf(d)), "x is already a correlation")
})

Expand Down
54 changes: 27 additions & 27 deletions tests/testthat/test-as_matrix.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
context("as_matrix.R")
x <- correlate(mtcars, diagonal = 1)
y <- as_matrix(x)
test_that("Inherits correct classes", {
expect_is(y, "matrix")
})
test_that("Converts values accurately", {
expect_true(all(x[-1] == y, na.rm = TRUE))
})
test_that("Yields correct columns and rows", {
expect_equal(colnames(x), c("rowname", colnames(y)))
expect_equal(nrow(x), ncol(y))
expect_equal(x$rowname, colnames(y))
expect_equal(rownames(y), colnames(y))
})
test_that("Diagonal sets correctly", {
expect_true(all(diag(y) == 1))
expect_true(all(diag(as_matrix(x, diagonal = 100)) == 100))
})
test_that("as_matrix preservers diag from correlate", {
expect_equal(diag(as.matrix(x[-1])), unname(diag(y)))
context("as_matrix.R")

x <- correlate(mtcars, diagonal = 1)
y <- as_matrix(x)

test_that("Inherits correct classes", {
expect_s3_class(y, "matrix")
})

test_that("Converts values accurately", {
expect_true(all(x[-1] == y, na.rm = TRUE))
})

test_that("Yields correct columns and rows", {
expect_equal(colnames(x), c("rowname", colnames(y)))
expect_equal(nrow(x), ncol(y))
expect_equal(x$rowname, colnames(y))
expect_equal(rownames(y), colnames(y))
})

test_that("Diagonal sets correctly", {
expect_true(all(diag(y) == 1))
expect_true(all(diag(as_matrix(x, diagonal = 100)) == 100))
})

test_that("as_matrix preservers diag from correlate", {
expect_equal(diag(as.matrix(x[-1])), unname(diag(y)))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-dataframe.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
context("dataframe")

test_that("Fashion works", {
expect_is(fashion(mtcars), "data.frame")
expect_s3_class(fashion(mtcars), "data.frame")
})
2 changes: 1 addition & 1 deletion tests/testthat/test-fashion.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ test_that("Vectors and padding", {
})

test_that("Fashion works against matrix", {
expect_is(fashion(as.matrix(correlate(mtcars))), "data.frame")
expect_s3_class(fashion(as.matrix(correlate(mtcars))), "data.frame")
})
2 changes: 1 addition & 1 deletion tests/testthat/test-focus.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test_that("focus_if works", {
any_greater_than <- function(x, val) {
mean(abs(x), na.rm = TRUE) > val
}
expect_is(
expect_s3_class(
focus_if(d, any_greater_than, .6),
"tbl_df"
)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ d <- correlate(d)
context("network")

test_that("Network plot works", {
expect_is(network_plot(d), "ggplot")
expect_is(network_plot(d, colors = c("indianred2", "white", "skyblue1")), "ggplot")
expect_s3_class(network_plot(d), "ggplot")
expect_s3_class(network_plot(d, colors = c("indianred2", "white", "skyblue1")), "ggplot")
})

context("rplot")

test_that("Network plot works", {
expect_is(rplot(d), "ggplot")
expect_is(rplot(d, colors = c("indianred2", "white", "skyblue1")), "ggplot")
expect_s3_class(rplot(d), "ggplot")
expect_s3_class(rplot(d, colors = c("indianred2", "white", "skyblue1")), "ggplot")
})
4 changes: 2 additions & 2 deletions tests/testthat/test-stretch.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ test_that("na.rm", {
test_that("retract works", {
cd <- as_cordf(retract(stretch(d)))
expect_equal(d, cd)
expect_is(d, "cor_df")
expect_s3_class(cd, "cor_df")
})

test_that("remove.dups works", {
expect_is(stretch(d, remove.dups = TRUE), "data.frame")
expect_s3_class(stretch(d, remove.dups = TRUE), "data.frame")
})
2 changes: 1 addition & 1 deletion tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context("Utilities")

test_that("pair_n works", {
expect_is(
expect_s3_class(
pair_n(1),
"matrix"
)
Expand Down