Require usage of expect_identical(x, y)
where appropriate
Source: R/expect_identical_linter.R
expect_identical_linter.Rd
This linter enforces the usage of testthat::expect_identical()
as the
default expectation for comparisons in a testthat suite. expect_true(identical(x, y))
is an equivalent but unadvised method of the same test. Further,
testthat::expect_equal()
should only be used when expect_identical()
is inappropriate, i.e., when x
and y
need only be numerically
equivalent instead of fully identical (in which case, provide the
tolerance=
argument to expect_equal()
explicitly). This also applies
when it's inconvenient to check full equality (e.g., names can be ignored,
in which case ignore_attr = "names"
should be supplied to
expect_equal()
(or, for 2nd edition, check.attributes = FALSE
).
Exceptions
The linter allows expect_equal()
in three circumstances:
A named argument is set (e.g.
ignore_attr
ortolerance
)Comparison is made to an explicit decimal, e.g.
expect_equal(x, 1.0)
(implicitly settingtolerance
)...
is passed (wrapper functions which might set arguments such asignore_attr
ortolerance
)
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "expect_equal(x, y)",
linters = expect_identical_linter()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
lint(
text = "expect_true(identical(x, y))",
linters = expect_identical_linter()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
# okay
lint(
text = "expect_identical(x, y)",
linters = expect_identical_linter()
)
lint(
text = "expect_equal(x, y, check.attributes = FALSE)",
linters = expect_identical_linter()
)
lint(
text = "expect_equal(x, y, tolerance = 1e-6)",
linters = expect_identical_linter()
)