testthat::expect_s3_class()
exists specifically for testing the class
of S3 objects. testthat::expect_equal()
, testthat::expect_identical()
,
and testthat::expect_true()
can also be used for such tests,
but it is better to use the tailored function instead.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = 'expect_equal(class(x), "data.frame")',
linters = expect_s3_class_linter()
)
#> <text>:1:1: warning: [expect_s3_class_linter] expect_s3_class(x, k) is better than expect_equal(class(x), k). Note also expect_s4_class() available for testing S4 objects.
#> expect_equal(class(x), "data.frame")
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lint(
text = 'expect_equal(class(x), "numeric")',
linters = expect_s3_class_linter()
)
#> <text>:1:1: warning: [expect_s3_class_linter] expect_s3_class(x, k) is better than expect_equal(class(x), k). Note also expect_s4_class() available for testing S4 objects.
#> expect_equal(class(x), "numeric")
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# okay
lint(
text = 'expect_s3_class(x, "data.frame")',
linters = expect_s3_class_linter()
)
#> ℹ No lints found.
lint(
text = 'expect_type(x, "double")',
linters = expect_s3_class_linter()
)
#> ℹ No lints found.