Learn R Programming

lintr (version 3.1.0)

conjunct_test_linter: Force && conditions in expect_true() and expect_false() to be written separately

Description

For readability of test outputs, testing only one thing per call to testthat::expect_true() is preferable, i.e., expect_true(A); expect_true(B) is better than expect_true(A && B), and expect_false(A); expect_false(B) is better than expect_false(A || B).

Usage

conjunct_test_linter(allow_named_stopifnot = TRUE)

Arguments

allow_named_stopifnot

Logical, TRUE by default. If FALSE, "named" calls to stopifnot(), available since R 4.0.0 to provide helpful messages for test failures, are also linted.

Tags

best_practices, configurable, package_development, readability

Details

Similar reasoning applies to && usage inside stopifnot() and assertthat::assert_that() calls.

See Also

linters for a complete list of linters available in lintr.

Examples

Run this code
# will produce lints
lint(
  text = "expect_true(x && y)",
  linters = conjunct_test_linter()
)

lint(
  text = "expect_false(x || (y && z))",
  linters = conjunct_test_linter()
)

lint(
  text = "stopifnot('x must be a logical scalar' = length(x) == 1 && is.logical(x) && !is.na(x))",
  linters = conjunct_test_linter(allow_named_stopifnot = FALSE)
)

# okay
lint(
  text = "expect_true(x || (y && z))",
  linters = conjunct_test_linter()
)

lint(
  text = 'stopifnot("x must be a logical scalar" = length(x) == 1 && is.logical(x) && !is.na(x))',
  linters = conjunct_test_linter(allow_named_stopifnot = TRUE)
)

Run the code above in your browser using DataLab