repo_root <- normalizePath( file.path(testthat::test_path(), "..", ".."), winslash = "/", mustWork = TRUE ) r_files <- sort( list.files( path = file.path(repo_root, "R"), pattern = "[.][Rr]$", full.names = TRUE, recursive = TRUE ) ) r_files <- r_files[basename(r_files) != "load_subdirs.R"] .hydroeval_test_env <- new.env(parent = globalenv()) if (length(r_files)) { for (path in r_files) { sys.source(path, envir = .hydroeval_test_env) } if (!"hydroeval_test_env" %in% search()) { attach(.hydroeval_test_env, name = "hydroeval_test_env", warn.conflicts = FALSE) } } if (length(r_files) && !"hydroeval" %in% loadedNamespaces()) { for (spec in list( list(generic = "print", class = "hydroeval_gof", method = "print.hydroeval_gof"), list(generic = "as.matrix", class = "hydroeval_gof", method = "as.matrix.hydroeval_gof"), list(generic = "as.data.frame", class = "hydroeval_gof", method = "as.data.frame.hydroeval_gof"), list(generic = "print", class = "hydroeval_gof_compare", method = "print.hydroeval_gof_compare"), list(generic = "as.matrix", class = "hydroeval_gof_compare", method = "as.matrix.hydroeval_gof_compare"), list(generic = "as.data.frame", class = "hydroeval_gof_compare", method = "as.data.frame.hydroeval_gof_compare") )) { registerS3method( genname = spec$generic, class = spec$class, method = get(spec$method, envir = .hydroeval_test_env, mode = "function", inherits = FALSE) ) } } .hydroeval_test_reference_path <- function() { candidates <- c( file.path(repo_root, "inst", "REFERENCES.bib"), system.file("REFERENCES.bib", package = "hydroeval") ) existing <- candidates[nzchar(candidates) & file.exists(candidates)] if (!length(existing)) { stop("Could not locate inst/REFERENCES.bib for tests.", call. = FALSE) } existing[[1L]] } .hydroeval_test_namespace_exports <- function() { if ("hydroeval" %in% loadedNamespaces()) { return(getNamespaceExports("hydroeval")) } namespace_path <- file.path(repo_root, "NAMESPACE") if (!file.exists(namespace_path)) { stop("Could not locate NAMESPACE for tests.", call. = FALSE) } sub( "^export\\((.+)\\)$", "\\1", grep("^export\\(", readLines(namespace_path, warn = FALSE), value = TRUE) ) } .hydroeval_test_get_object <- function(name, mode = "any", prefer_namespace = FALSE) { if ( isTRUE(prefer_namespace) && "hydroeval" %in% loadedNamespaces() && exists(name, envir = asNamespace("hydroeval"), mode = mode, inherits = FALSE) ) { return(get(name, envir = asNamespace("hydroeval"), mode = mode, inherits = FALSE)) } if (exists(name, envir = .hydroeval_test_env, mode = mode, inherits = FALSE)) { return(get(name, envir = .hydroeval_test_env, mode = mode, inherits = FALSE)) } stop(sprintf("Could not locate test object `%s`.", name), call. = FALSE) }