Skip to content

Commit

Permalink
change default and check for input edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Nov 1, 2024
1 parent 66c8102 commit 746eba2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
6 changes: 3 additions & 3 deletions R/complex_conditional_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
#' has the added benefit of introducing a reusable abstraction.
#'
#' @param threshold Integer. The maximum number of logical operators (`&&` or `||`)
#' allowed in a conditional expression. The default is `1L`, meaning any conditional expression
#' with more than one logical operator will be flagged.
#' allowed in a conditional expression. The default is `2L`, meaning any conditional expression
#' with more than two logical operators will be flagged.
#'
#' @examples
#' # will produce lints
Expand Down Expand Up @@ -75,7 +75,7 @@
#' @evalRd rd_tags("complex_conditional_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
complex_conditional_linter <- function(threshold = 1L) {
complex_conditional_linter <- function(threshold = 2L) {
stopifnot(is.numeric(threshold), length(threshold) == 1L, threshold >= 1L)
threshold <- as.integer(threshold)

Expand Down
6 changes: 3 additions & 3 deletions man/complex_conditional_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 26 additions & 6 deletions tests/testthat/test-complex-conditional-linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ test_that("complex_conditional_linter doesn't lint simple conditionals", {
)
})

test_that("complex_conditional_linter finds complex conditions with default threshold", {
linter <- complex_conditional_linter()
test_that("complex_conditional_linter lints complex conditionals above set threshold", {
linter <- complex_conditional_linter(threshold = 1L)
lint_message <- rex::rex("Complex conditional with more than 1 logical operator(s)")

expect_lint(
Expand All @@ -59,13 +59,13 @@ test_that("complex_conditional_linter finds complex conditions with default thre

test_that("complex_conditional_linter handles nested conditionals", {
linter <- complex_conditional_linter()
lint_message <- rex::rex("Complex conditional with more than 1 logical operator(s)")
lint_message <- rex::rex("Complex conditional with more than 2 logical operator(s)")

# simple outer, complex inner
expect_lint(
trim_some("
if (x > 0) {
if (a == 1 && b == 2 && c == 3) {
if (a == 1 && b == 2 && c == 3 && d == 4) {
print('nested')
}
}
Expand All @@ -77,8 +77,8 @@ test_that("complex_conditional_linter handles nested conditionals", {
# multiple complex conditions
expect_lint(
trim_some("
if (x > 0 && y < 10 && z == TRUE) {
while (a && b && c) {
if (x > 0 && y < 10 && z == TRUE && !w) {
while (a && b && c || d) {
print('double complex')
}
}
Expand Down Expand Up @@ -168,3 +168,23 @@ test_that("complex_conditional_linter skips non-conditional expressions", {
linter
)
})

# styler: off
invalid_cases <- list(
list(name = "character", input = "2", error = "is.numeric"),
list(name = "logical", input = TRUE, error = "is.numeric"),
list(name = "vector", input = c(2, 3), error = "length"),

Check warning on line 176 in tests/testthat/test-complex-conditional-linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-complex-conditional-linter.R,line=176,col=39,[implicit_integer_linter] Use 2L or 2.0 to avoid implicit integers.

Check warning on line 176 in tests/testthat/test-complex-conditional-linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-complex-conditional-linter.R,line=176,col=42,[implicit_integer_linter] Use 3L or 3.0 to avoid implicit integers.
list(name = "empty", input = numeric(0), error = "length"),

Check warning on line 177 in tests/testthat/test-complex-conditional-linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-complex-conditional-linter.R,line=177,col=45,[implicit_integer_linter] Use 0L or 0.0 to avoid implicit integers.
list(name = "zero", input = 0, error = "threshold >= 1L"),

Check warning on line 178 in tests/testthat/test-complex-conditional-linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-complex-conditional-linter.R,line=178,col=37,[implicit_integer_linter] Use 0L or 0.0 to avoid implicit integers.
list(name = "negative", input = -1, error = "threshold >= 1L"),

Check warning on line 179 in tests/testthat/test-complex-conditional-linter.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-complex-conditional-linter.R,line=179,col=38,[implicit_integer_linter] Use -1L or -1.0 to avoid implicit integers.
list(name = "NA", input = NA_real_, error = "is.numeric"),
list(name = "NaN", input = NaN, error = "threshold >= 1L"),
list(name = "Inf", input = Inf, error = "threshold >= 1L")
)
# styler: on

patrick::with_parameters_test_that(
"complex_conditional_linter rejects invalid threshold arguments",
expect_error(complex_conditional_linter(input), regexp = invalid_cases$error),
.cases = invalid_cases
)

0 comments on commit 746eba2

Please sign in to comment.