From ce78e06afbb0fd7153c0f4231c1c9cd831b8e864 Mon Sep 17 00:00:00 2001 From: Chris Black Date: Wed, 4 Oct 2017 18:24:07 -0400 Subject: [PATCH] [#407, #499] import from methods to avoid error when not attached --- NAMESPACE | 2 ++ R/package.r | 2 +- tests/testthat/test-namespace.R | 50 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-namespace.R diff --git a/NAMESPACE b/NAMESPACE index 0138bb1c..6c1f22c0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -266,11 +266,13 @@ exportMethods(show) exportMethods(union) import(stringr) importFrom(Rcpp,sourceCpp) +importFrom(methods,"coerce<-") importFrom(methods,"slot<-") importFrom(methods,Arith) importFrom(methods,Compare) importFrom(methods,allNames) importFrom(methods,callGeneric) +importFrom(methods,initialize) importFrom(methods,is) importFrom(methods,new) importFrom(methods,setClass) diff --git a/R/package.r b/R/package.r index 135d5835..0cce70ea 100644 --- a/R/package.r +++ b/R/package.r @@ -155,7 +155,7 @@ #' Easy with lubridate. Journal of Statistical Software, 40(3), 1-25. #' \url{http://www.jstatsoft.org/v40/i03/}. #' @import stringr -#' @importFrom methods setClass setGeneric new show allNames callGeneric is slot slot<- slotNames validObject Compare Arith +#' @importFrom methods setClass setGeneric new show allNames callGeneric is slot slot<- slotNames validObject Compare Arith initialize coerce<- #' @importFrom utils packageVersion read.delim #' @importFrom stats na.omit setNames update #' @importFrom Rcpp sourceCpp diff --git a/tests/testthat/test-namespace.R b/tests/testthat/test-namespace.R new file mode 100644 index 00000000..0f7dcd66 --- /dev/null +++ b/tests/testthat/test-namespace.R @@ -0,0 +1,50 @@ +context("Namespace") + +if (file.exists("../../00check.log")) { + # test was invoked by R CMD check -> package is already built + R_test_lib <- normalizePath("../..") +} else { + # We're testing in the source directory -> need to build and install + R_test_lib <- file.path(tempdir(), "Rlib") + dir.create(R_test_lib) + on.exit(unlink(R_test_lib, recursive = TRUE)) + system2( + "R", + c("CMD install", + paste0("--library=", R_test_lib), + "--no-docs --no-help --no-demo --no-data --no-test-load", + normalizePath("../..")), + stdout = TRUE, stderr = TRUE) +} + +do_Rscript <- function(expr) { + system2( + "Rscript", + args = c("--vanilla", "--default-packages=NULL", "-e", shQuote(expr)), + env = c("R_TESTS=", paste0("R_LIBS=", R_test_lib, ":", Sys.getenv("R_LIBS"))), + stdout = TRUE, stderr = TRUE) +} + +test_that("methods is not attached", { + # Checking test assumptions. + # If this fails, namespace tests may not be needed anymore! + expect_match( + do_Rscript("'package:methods' %in% search()"), + "FALSE") +}) + +test_that("lubridate:: calls work when methods is not attached", { + expect_match( #314 + do_Rscript( + "lubridate::round_date(as.POSIXct('2017-10-03 03:01:13Z'), 'minute')"), + as.character(round_date(as.POSIXct('2017-10-03 03:01:13Z'), 'minute')), + fixed = TRUE) + expect_match( #407 + do_Rscript("lubridate::days(1)"), + as.character(days(1)), + fixed = TRUE) + expect_match( #499 + do_Rscript("lubridate::dhours(22)"), + as.character(dhours(22)), + fixed = TRUE) +})