tests/testthat/test-cyclocomp_linter.R

test_that("returns the correct linting", {
  cc_linter_1 <- cyclocomp_linter(1L)
  cc_linter_2 <- cyclocomp_linter(2L)
  lint_msg <- rex::rex("Functions should have cyclomatic complexity")

  expect_lint("if (TRUE) 1 else 2", NULL, cc_linter_2)
  expect_lint("if (TRUE) 1 else 2", lint_msg, cc_linter_1)

  expect_lint(
    "function(x) {not parsing}",
    "unexpected symbol", cc_linter_2
  )

  complexity <- readLines(
    system.file("example/complexity.R", package = "lintr")
  )
  expect_lint(complexity, lint_msg, cc_linter_2)
  expect_lint(
    complexity,
    "should have cyclomatic complexity of less than 2, this has 10",
    cc_linter_2
  )
  expect_lint(complexity, NULL, cyclocomp_linter(10L))
})

Try the lintr package in your browser

Any scripts or data that you put into this service are public.

lintr documentation built on Nov. 7, 2023, 5:07 p.m.