Skip to content

Commit

Permalink
Add a testcase based on configuration (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
F-Noelle authored May 14, 2024
1 parent ae58e20 commit 7bd8324
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tests/testthat/test-scalar_in_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ test_that("scalar_in_linter skips allowed usages", {
expect_lint("z %in% 1:3", NULL, linter)
# scalars on LHS are fine (often used as `"col" %in% names(DF)`)
expect_lint("3L %in% x", NULL, linter)

# this should be is.na(x), but it more directly uses the "always TRUE/FALSE, _not_ NA"
# aspect of %in%, so we delegate linting here to equals_na_linter()
expect_lint("x %in% NA", NULL, linter)
Expand All @@ -16,6 +15,7 @@ test_that("scalar_in_linter skips allowed usages", {

test_that("scalar_in_linter blocks simple disallowed usages", {
linter <- scalar_in_linter(in_operators = c("%chin%", "%notin%"))

lint_in_msg <- rex::rex("Use == to match length-1 scalars instead of %in%.")
lint_chin_msg <- rex::rex("%chin% behaves similar to %in%.")
lint_notin_msg <- rex::rex("%notin% behaves similar to %in%.")
Expand All @@ -25,6 +25,24 @@ test_that("scalar_in_linter blocks simple disallowed usages", {
expect_lint("x %notin% 1", lint_notin_msg, linter)
})

test_that("scalar_in_linter blocks or skips based on configuration", {
linter_default <- scalar_in_linter()
linter_config <- scalar_in_linter(in_operators = "%notin%")

lint_in_msg <- rex::rex("Use == to match length-1 scalars instead of %in%.")
lint_notin_msg <- rex::rex("%notin% behaves similar to %in%.")

# default
expect_lint("x %in% 1", lint_in_msg, linter_default)
expect_lint("x %notin% 1", NULL, linter_default)
expect_lint("x %notin% y", NULL, linter_default)

# configured
expect_lint("x %in% 1", lint_in_msg, linter_config)
expect_lint("x %notin% 1", lint_notin_msg, linter_config)
expect_lint("x %notin% y", NULL, linter_config)
})

test_that("multiple lints are generated correctly", {
linter <- scalar_in_linter(in_operators = "%chin%")

Expand Down

0 comments on commit 7bd8324

Please sign in to comment.