tests/testthat/test-grab_and_attach.R

test_that("grab_and_attach() reports package already installed", {

  grab_and_attach(parallel)

  out <- capture.output(grab_and_attach('parallel'))
  out <- Filter(function(i) grepl('[a-xA-x0-9]+', i), out)

  expect_equal(out, "Package parallel is already loaded")

})

test_that('grab_and_attach() reports a problem', {

  out <- capture.output(grab_and_attach('mispelled_package_XYZ'))
  prob_report <- grepl('The package .+ is most probably wrongly spelled, check the package“s name or check the error message above', out)

  expect_true(any(prob_report))

})

test_that('grab_and_attach() loads multiple packages', {

  # using R system libraries for this test, will not install external package, loading and attaching these is here considered safe.

  suppressPackageStartupMessages({ # neccessary for devtools::test(), devtools::check() gets the packages correctly from imports section of DESCRIPTION


  if('parallel' %in% .packages()) detach("package:parallel", unload = TRUE)
  if('mgcv' %in% .packages()) detach("package:mgcv", unload = TRUE)

  })

  out <- capture.output(grab_and_attach('parallel', mgcv))
  l <- length(grep('...package loaded!', out))

  expect_equal(l, 2L)

})

# potential additional testing candidates
#grab_and_attach(lubridate, magrittrERRROR, anytime)
#grab_and_attach(lubridate, magrittr, anytime, "dplyr")
#grab_and_attach("lubridate", "magrittr", "anytime")
#grab_and_attach(c("lubridate", "magrittr", "anytime"))
#grab_and_attach(data.table, xseq, "dplyr", ggplot2)
#grab_and_attach(yaml, reshape)
Patrikios/grab_and_attach documentation built on Jan. 3, 2022, 12:47 p.m.