tests/testthat/test-03_additionalLinters.R

test_that("double_space_linter detects double spaces correctly", {
  test_file <- system.file("testScript_double_space_linter.R", package = "INWTUtils")
  expect_true(file.exists(test_file))
  lint_results <- lint(test_file, linters = double_space_linter())
  lint_row_numbers <- unlist(unique(lapply(lint_results, function(x) x$line_number)))
  lint_col_numbers <- unlist(lapply(lint_results, function(x) x$column_number))
  expect_contains(lint_row_numbers, 3:13)
  expect_equal(lint_col_numbers, c(11, 14, 13, 6, 8, 16, 4, 18, 13, 10, 15, 25, 27))
})


# test_that("internal_function_linter", {
#   inputWrong <- list(filename = "An example object",
#                      # nolint start
#                      file_lines = c("INWTpackage:::foo",
#                                     "anINWTpkg:::foo",
#                                     "some code anINWTpkg:::foo",
#                                     "INWTpkg:::foo(",
#                                     "INWT some test dplyr:::foo"))
#   # nolint end
#   inputCorrect <- list(filename = "An example object",
#                        file_lines = c("INWTpkg::foo",
#                                       "# somePkg:::someFun",
#                                       "#' @examples INWTpkg:::someFun",
#                                       "#' INWTpkg:::someFun"))
#   expect_true(lapply(internal_function_linter()(inputWrong),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(internal_function_linter()(inputWrong) %>% length,
#                inputWrong$file_lines %>% length)
#   expect_true(lapply(internal_function_linter()(inputCorrect),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(internal_function_linter()(inputCorrect) %>% length, 0)
# })


# test_that("setwd_linter", {
#   # nolint start
#   inputWrong <- list(filename = "An example object",
#                      file_lines = c("setwd(path)",
#                                     'setwd("path"',
#                                     "  setwd(path)"))
#   # nolint end
#   inputCorrect <- list(filename = "An example object",
#                        file_lines = c("# Comment with the word setwd",
#                                       "#' Roxygen comment containing setwd",
#                                       "some code 123 # comment with setwd("))
#   expect_true(lapply(setwd_linter()(inputWrong),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(setwd_linter()(inputWrong) %>% length,
#                inputWrong$file_lines %>% length)
#   expect_true(lapply(setwd_linter()(inputCorrect),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(setwd_linter()(inputCorrect) %>% length, 0)
# })


# test_that("source_linter", {
#   # nolint start
#   inputWrong <- list(filename = "An example object",
#                      file_lines = c("source(file)",
#                                     'source("file"',
#                                     "  source(file)"))
#   # nolint end
#   inputCorrect <- list(filename = "An example object",
#                        file_lines = c("# Comment with the word source",
#                                       "#' Roxygen comment containing source",
#                                       "some code 123 # comment with source(",
#                                       paste0("PackageInstallArgs: --no-multiarch",
#                                              "--with-keep.source")))
#   expect_true(lapply(source_linter()(inputWrong),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(source_linter()(inputWrong) %>% length,
#                inputWrong$file_lines %>% length)
#   expect_true(lapply(source_linter()(inputCorrect),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(source_linter()(inputCorrect) %>% length, 0)
# })


# test_that("options_linter", {
#   # nolint start
#   inputWrong <- list(filename = "An example object",
#                      file_lines = c("options(scipen = 999)",
#                                     'options(editor = "nedit")',
#                                     "  options(warn = FALSE)"))
#   # nolint end
#   inputCorrect <- list(filename = "An example object",
#                        file_lines = c("# Comment with the word options",
#                                       "#' Roxygen comment containing options",
#                                       "some code 123 # comment with options("))
#   expect_true(lapply(options_linter()(inputWrong),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(options_linter()(inputWrong) %>% length,
#                inputWrong$file_lines %>% length)
#   expect_true(lapply(options_linter()(inputCorrect),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(options_linter()(inputCorrect) %>% length, 0)
# })


# test_that("sapply_linter", {
#   inputWrong <- list(filename = "An example object",
#                      file_lines = c("sapply(1:5, function(x)",
#                                     " %>% sapply(FUN = "))
#   inputCorrect <- list(filename = "An example object",
#                        file_lines = c("# sapply(",
#                                       "#' sapply(",
#                                       "'sapply('",
#                                       '"sapply(',
#                                       "some code # sapply("))
#   expect_true(lapply(sapply_linter()(inputWrong),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(sapply_linter()(inputWrong) %>% length,
#                inputWrong$file_lines %>% length)
#   expect_true(lapply(sapply_linter()(inputCorrect),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(sapply_linter()(inputCorrect) %>% length, 0)
#
# })


# test_that("trailing_whitespaces_linter", {
#   # nolint start
#   inputWrong <- list(filename = "An example object",
#                      file_lines = c("x <- 1  ",
#                                     "x <- 1 ",
#                                     "dat %>%  ",
#                                     "#'  ",
#                                     "x <- 5 %>%  ",
#                                     " x  "))
#   # nolint end
#   inputCorrect <- list(filename = "An example object",
#                        file_lines = c("x <- 1",
#                                       "x <- 5 %>% ",
#                                       "#' ",
#                                       "    ",
#                                       "   ",
#                                       "  "))
#   expect_true(lapply(trailing_whitespaces_linter()(inputWrong),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(trailing_whitespaces_linter()(inputWrong) %>% length,
#                inputWrong$file_lines %>% length)
#   expect_true(lapply(trailing_whitespaces_linter()(inputCorrect),
#                      function(x) class(x) == "lint") %>% unlist %>% all)
#   expect_equal(trailing_whitespaces_linter()(inputCorrect) %>% length, 0)
# })
INWT/INWTUtils documentation built on May 22, 2024, 4:45 p.m.